stm32实现回调功能

为了代码易维护 可读性 将app和设备驱动功能分开 不直接调用函数

而是采用回调和注册回调方式

dev_io.h

c 复制代码
#ifndef __DEV_IO__H__
#define __DEV_IO__H__

#include "main.h"


//回调
typedef void (*Fun)(uint16_t GPIO_Pin);		// 回调函数类型
uint8_t BatAdd(Fun func);					//注册回调

#endif

dev_io.c

c 复制代码
#include <stdio.h>
#include <stdlib.h>
//#include "stdio.h"
#include <string.h>
#include <stdarg.h>
#include "dev_io.h"

//当前app是否打印
#if 1
#define debug  printf
#else
static void debug(const char *format, ...){}
#endif
//打印16进制
static void debugH(uint8_t *buf,int len){
	for(int i=0; i < len; i++){
		debug("0x%0X ",buf[i]);
	}
	debug("\n");
}





//*********************回调 */
typedef struct {  
	//EventID ID; 		// 回调事件标志id
    Fun fun; 			// 指向函数的指针  
} CallbackStruct;  		//回调结构

CallbackStruct *Bat = NULL;	//回调表
uint8_t BatLen = 0;				//回调表成员数

//释放回调 表
void BatFree(void) {
    free(Bat);
    Bat = NULL;
    BatLen = 0;
}


/****************
注册回调函数,成功返回ID 失败返回0
****************/
uint8_t BatAdd(Fun func) {
    if (Bat == NULL) {										//分配初始内存
        Bat = malloc(sizeof(CallbackStruct));
        if (Bat == NULL) {
            return 0; 										// 内存分配失败,返回0或错误代码
        }
        BatLen = 0; 										// 初始化大小为0(但实际上在第一次添加时会被加1)
    }
 
    CallbackStruct *temp = 
	realloc(Bat, (BatLen + 1) * sizeof(CallbackStruct));	// 为回调表分配更多内存
    if (temp == NULL) {
        return 0; 											// realloc失败,返回0或错误代码
    }
    Bat = temp;
    Bat[BatLen].fun = func;									// 添加回调并更新大小
    BatLen++;
    return BatLen;											// 返回回调的ID(索引+1)
}
 


/****************
触发回调
****************/
static void CallFun(uint16_t GPIO_Pin) {
	for(uint8_t i=0;i < BatLen;i++){					//遍历储存在变量的回调函数
		if (Bat[i].fun != NULL ) {
			Bat[i].fun(GPIO_Pin);						//调用
		}
	}
}

//*********************回调 */








// 外部中断回调函数
/*
    - UART8 作为输出,当io倍触发时通过UART发出数据
*/
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
	CallFun(GPIO_Pin);
}

在app上使用

c 复制代码
//io回调
static void Callio(uint16_t GPIO_Pin){
	
}

BatAdd(Callio);										//注册io回调

这样每设备驱动文件回调都不会直接调用app的函数

app主动采用注册回调方式

实现设备驱动和app彻底分开

相关推荐
zcmodeltech25 分钟前
火电厂模型多设备时序控制系统设计:基于STM32与Modbus RTU的燃煤-燃机全流程联动方案
分布式·stm32·单片机·嵌入式硬件·物联网
芯岭技术10 小时前
Wi-Fi+BLE双模无线芯片 BK7238,低功耗、高性能,集成MCU和Flash
单片机·嵌入式硬件
云空10 小时前
《ARM Cortex‑M 系列完整梳理》
stm32·单片机·嵌入式硬件·mcu
单片机杂货铺10 小时前
基于单片机的智能花盆浇水系统控制器设
stm32·单片机·嵌入式硬件·毕业设计·51单片机·硬件工程
Dr.kangder11 小时前
嵌入式处理器仿真技术——Hypervisor 原理与实践
服务器·嵌入式硬件·架构·嵌入式
Hello_Damon_Nikola13 小时前
TM1637芯片从入门到精通
单片机·嵌入式硬件
智购科技自动售卖机厂家16 小时前
从STM32到RK3588——自动售货机嵌入式主控方案的架构演进~YH
stm32·嵌入式硬件·物联网·架构·lua·零售·symfony
振南的单片机世界17 小时前
2MHz/50MHz玄机:GPIO速度档选错,信号直接“烂尾”
arm开发·stm32·单片机·嵌入式硬件
Run_Teenage18 小时前
嵌入式:理解STM32文档和社区生态,工具链的选择与安装
stm32·单片机·嵌入式硬件
ACP广源盛1392462567318 小时前
DeepSeek‑V4‑Flash 公测@ACP#昇腾 950 国产算力组合落地,国产 PCIe 交换芯片 IX9104 有哪些硬件机会
大数据·数据库·人工智能·分布式·单片机·嵌入式硬件·microsoft