嵌入式软件分层模块封装例子

2023-08-18 11:23:56 来源:嵌入式大杂烩

一、前言

STM32为例,打开网络上下载的例程或者是购买开发板自带的例程,都会发现应用层中会有stm32f10x.h或者stm32f10x_gpio.h,这些文件严格来时属于硬件层的,如果软件层出现这些文件会显得很乱。

使用过Linux的童鞋们肯定知道linux系统无法直接操作硬件层,打开linux或者rt_thread代码会发现代码中都会有device的源文件,没错,这就是驱动层。


(相关资料图)

二、实现原理

原理就是将硬件操作的接口全都放到驱动链表上,在驱动层实现device的open、read、write等操作。当然这样做也有弊端,就是驱动find的时候需要遍历一遍驱动链表,这样会增加代码运行时间。

三、代码实现

国际惯例,写代码先写头文件。rt_thread中使用的是双向链表,为了简单在这我只用单向链表。有兴趣的可以自行研究rt_thread

头文件接口:

本次只实现如下接口,device_open 和device_close等剩下的接口可以自行研究。这样就可以在应用层中只调用如下接口可实现:

/*驱动注册*/intcola_device_register(cola_device_t*dev);/*驱动查找*/cola_device_t*cola_device_find(constchar*name);/*驱动读*/intcola_device_read(cola_device_t*dev,intpos,void*buffer,intsize);/*驱动写*/intcola_device_write(cola_device_t*dev,intpos,constvoid*buffer,intsize);/*驱动控制*/intcola_device_ctrl(cola_device_t*dev,intcmd,void*arg);;

头文件cola_device.h:

#ifndef_COLA_DEVICE_H_#define_COLA_DEVICE_H_enumLED_state{LED_OFF,LED_ON,LED_TOGGLE,};typedefstructcola_devicecola_device_t;structcola_device_ops{int(*init)(cola_device_t*dev);int(*open)(cola_device_t*dev,intoflag);int(*close)(cola_device_t*dev);int(*read)(cola_device_t*dev,intpos,void*buffer,intsize);int(*write)(cola_device_t*dev,intpos,constvoid*buffer,intsize);int(*control)(cola_device_t*dev,intcmd,void*args);};structcola_device{constchar*name;structcola_device_ops*dops;structcola_device*next;};/*驱动注册*/intcola_device_register(cola_device_t*dev);/*驱动查找*/cola_device_t*cola_device_find(constchar*name);/*驱动读*/intcola_device_read(cola_device_t*dev,intpos,void*buffer,intsize);/*驱动写*/intcola_device_write(cola_device_t*dev,intpos,constvoid*buffer,intsize);/*驱动控制*/intcola_device_ctrl(cola_device_t*dev,intcmd,void*arg);#endif

源文件cola_device.c:

#include"cola_device.h"#include#includestructcola_device*device_list=NULL;/*查找任务是否存在*/staticboolcola_device_is_exists(cola_device_t*dev){cola_device_t*cur=device_list;while(cur!=NULL){if(strcmp(cur->name,dev->name)==0){returntrue;}cur=cur->next;}returnfalse;}staticintdevice_list_inster(cola_device_t*dev){cola_device_t*cur=device_list;if(NULL==device_list){device_list=dev;dev->next=NULL;}else{while(NULL!=cur->next){cur=cur->next;}cur->next=dev;dev->next=NULL;}return1;}/*驱动注册*/intcola_device_register(cola_device_t*dev){if((NULL==dev)||(cola_device_is_exists(dev))){return0;}if((NULL==dev->name)||(NULL==dev->dops)){return0;}returndevice_list_inster(dev);}/*驱动查找*/cola_device_t*cola_device_find(constchar*name){cola_device_t*cur=device_list;while(cur!=NULL){if(strcmp(cur->name,name)==0){returncur;}cur=cur->next;}returnNULL;}/*驱动读*/intcola_device_read(cola_device_t*dev,intpos,void*buffer,intsize){if(dev){if(dev->dops->read){returndev->dops->read(dev,pos,buffer,size);}}return0;}/*驱动写*/intcola_device_write(cola_device_t*dev,intpos,constvoid*buffer,intsize){if(dev){if(dev->dops->write){returndev->dops->write(dev,pos,buffer,size);}}return0;}/*驱动控制*/intcola_device_ctrl(cola_device_t*dev,intcmd,void*arg){if(dev){if(dev->dops->control){returndev->dops->control(dev,cmd,arg);}}return0;}

硬件注册方式:以LED为例,初始化接口void led_register(void),需要在初始化中调用。

#include"stm32f0xx.h"#include"led.h"#include"cola_device.h"#definePORT_GREEN_LEDGPIOC#definePIN_GREENLEDGPIO_Pin_13/*LED亮、灭、变化*/#defineLED_GREEN_OFF(PORT_GREEN_LED->BSRR=PIN_GREENLED)#defineLED_GREEN_ON(PORT_GREEN_LED->BRR=PIN_GREENLED)#defineLED_GREEN_TOGGLE(PORT_GREEN_LED->ODR^=PIN_GREENLED)staticcola_device_tled_dev;staticvoidled_gpio_init(void){GPIO_InitTypeDefGPIO_InitStructure;RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC,ENABLE);GPIO_InitStructure.GPIO_Pin=PIN_GREENLED;GPIO_InitStructure.GPIO_Mode=GPIO_Mode_OUT;GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_NOPULL;GPIO_Init(PORT_GREEN_LED,&GPIO_InitStructure);LED_GREEN_OFF;}staticintled_ctrl(cola_device_t*dev,intcmd,void*args){if(LED_TOGGLE==cmd){LED_GREEN_TOGGLE;}else{}return1;}staticstructcola_device_opsops={.control=led_ctrl,};voidled_register(void){led_gpio_init();led_dev.dops=&ops;led_dev.name="led";cola_device_register(&led_dev);}

应用层app代码:

#include#include"app.h"#include"config.h"#include"cola_device.h"#include"cola_os.h"statictask_ttimer_500ms;staticcola_device_t*app_led_dev;//led每500ms状态改变一次staticvoidtimer_500ms_cb(uint32_tevent){cola_device_ctrl(app_led_dev,LED_TOGGLE,0);}voidapp_init(void){app_led_dev=cola_device_find("led");assert(app_led_dev);cola_timer_create(&timer_500ms,timer_500ms_cb);cola_timer_start(&timer_500ms,TIMER_ALWAYS,500);}

这样app.c文件中就不需要调用led.h头文件了,rtt就是这样实现的。

四、总结

这样就可以实现软硬件分层了,是不是非常好用!

审核编辑:汤梓红

标签:

上一篇:PCB缺陷有哪些?如何检查PCB缺陷?
下一篇:最后一页