MCU项目如何实现应用和硬件分层管理.docx
现在做项目,需求更复杂了,自然对项目的要求比以前更严格了。站在技术的角度,还是建议大家平时做项目要做到(应用和驱动分层,这样不仅仅方便维护、移植项目,更重耍的是提高自己技术的水平.今天就给大家分享一个开源的,轻量级的管理模块:co1.a/s一、概述以STM32为例,打开网络上卜.载的例程或者是购买开发板自带的例程,都会发现应用必中会有stm32f1.x.h或者stm32f1.Ox_gpio.h.这些文件严格来时属于硬件层的,如果软件层出现这些文件会显得很乱。使用过1.inux的童鞋们肯定知道Iinux系统无法直接操作硬件乂,打开IinUX或者rt_thread代码会发现代码中都会有device的源文件,没错,这就是驱动层。便动A应用星二、实现原理原理就是招硬件操作的接口全都放到驱动链表上,在驱动层实现device的open.read.Write等操作。当然这样做也有弊端,就是驱动find的时候需要遍历一遍驱动链表,这样会增加代码运行时间。三、代码实现国际惯例,写代码先写头文件。rtjhread中使用的是双向链我,为了简单在这我只用堆向链表。有兴趣的可以自行研究rt_thread头文件接口:本次只实现如卜接口,device-open和device-c1.ose等剩卜的接口可以自行研究,这样就可以在应用房中只调用如下接口可实现:/*驱动注册7intco1.a_device_register(co1.a_device_t*dev);/*驱动查找7co1.a_device_t*co1.a_device_find(constchar*name);*驱动读7intco1.a_device_read(co1.a_device_t*deintpos,void*buffer,intsize);*驱动写Vintco1.a_device_write(co1.a_device_t*dev,intpos,constvoid*buffer,intsize);驱动控制intco1.a_device_ctr1.(co1.a_device_t*dev,intcmd,voidarg);头文件co1.a_device.h:Mfndef_CO1.A_DEVICE_H_Wdefine_CO1.A_DEVICE_H_enum1.ED_state(1.EDJDFF,1.ED_ON,1.ED.TOGG1.E,typedefstructCO山.deviceco1.a_device_t;structco1.a_device_ops(int(*init)(co1.a_device_t*dev);int(*open)(co1.a_device_tdev,intof1.ag);int(*c1.ose)(co1.a_device_t*dev);int(*read)(co1.a_device_t,dev,intpos,void*buffer,intsize);int(*write)(co1.a_device_tdev,intpos,constvoid*buffer,intsize);int(*contro1.)(co1.a_device_t*deintcmd,void*args);structco1.a_device(constchar"name;structco1.a_device_ops*dops;structco1.a-device*next;郭动注册7intco1.a_device_register(co1.a_device_tedev);*期动查找7co1.a_device_t*co1.a_device_find(con$tchar*name);驱动读7intco1.a_device_read(co1.a_device_t*deintpos,void*buffer,intsize);*驱动写7intco1.a_device_write(co1.a_device_tedev,intpos,constvoid*buffer,intsize);*驱动控制7intco1.a_device_ctr1.(co1.a_device_t*de½intcmd,void*arg);Wendif源文件co1.a_device.c:Minc1.ude"co1.a-dev1.ce.h"inc1.ude<string.h>inc1.ude<stdboo1.h>structco1.a_device*devicejist=NU1.1.;查找任务是否存在7staticboo1.co1.a_device_is_exists(co1.a_device_t*dev)(co1.a_device_t*cur=devicejist;whi1.e(cur!=NU1.1.)(if(strcmp(cur->name,dev->name)=O)(returntrue;)cur=cur->next;returnfa1.se;staticintdevice_1.ist_inster(co1.a_device_t*dev)(co1.a_device_t*cur=device-1.ist;if(NU1.1.=device_1.i$t)devicejist=dev;dev->next=NU1.1.;e1.se(whi1.e(NU1.1.!=cur->next)(cur=cur->next;)cur->next=dev;dev->next=NU1.1.;)return1;邨动注册7intco1.a_device_register(co1.a_device_t*dev)if(NU1.1.=dev>name)11(NU1.1.=dev->dops)return0;returndeviceJistJnster(dev);驱动查找co1.a_device_t*co1.a_device_find(constchar*name)co1.a_device_t*cur=devicejist;whi1.e(cur!=NU1.1.)if(strcmp(cur->name,name)=O)returncur;cur=cur->next;returnNU1.1.;)*驱动读7intco1.a_device_read(co1.a_device_t*de½intpos,void*buffer,intsize)(if(dev)(if(dev->dops>read)(returndev->dops->read(dev,pos,buffer,size);)return0;)*驱动写Vintco1.a_device_write(co1.a_device_tdev,intpos,constvoid*buffer,intsize)if(dev)if(dev->dops>write)returndev>dops>write(dev,pos,buffer,size);return0;驱动控制intco1.a_device_ctr1.(co1.a_device_t*dev,intcmd,void*arg)if(dev)if(dev->dops->contro1.)returndev>dops->contro1.dev,cmd,arg);硬件注册方式:以1.ED为例,初始化接口VOid1.ed.register(VOid),需要在初始化中调用.inc1.ude"stm32fxx.h"inc1.ude"1.ed.h"inc1.ude"co1.a_device.h"WdefinePORT_GREEN_1.EDdefinePIN_GREEN1.ED/1.ED亮、灭、变化/Wdefine1.ED_GREEN_OFFPIN_GREEN1.ED)Wdefine1.ED_GREEN_ONP1.N_GREEN1.ED)define1.ED_GREEN_TOGG1.EPIN_GREEN1.ED)GPIOCGPI0_Pin_13PORT_GREEN_1.ED->BSRR=(PORT_GREEN_1.ED->BRR=(PORT_GREEN_1.ED->ODRK=staticco1.a_device_t1.ed_dev;staticvoid1.ed_gpio_init(void)(GPIOJnitTypeDefGPIO_1.nitStructure;RCC_AHBPeriphC1.ockCmd(RCC_AHBPeriph_GPIOC,ENAB1.E);GPIO_1.nitStructure.GPIO_Pin=PIN_GREEN1.ED;GPIO_1.nitStructure.GPIO_Mode=GPIO_Mode_OUT;GPIO_1.nitStructure.GPIO_Speed=GPIO_Speed_50MHz;GPIO_1.nitStructure.GPIO_OType=GPIO_OType_PP;GPIO_1.nitStructure.GPIO_PuPd=GPIO_PuPd_NOPU1.1.;GPIO_1.nit(PORT_GREEN_1.ED,&GPIO_1.nitStructure);1.ED_GREEN_OFF;)staticint1.ed_ctr1.(co1.a_device_t*dev,intcmd,void*args)(if(1.ED_TOGG1.E=cmd)1.ED_GREEN_TOGG1.E;e1.sestaticstructco1.a_device_opsops=(.contro1.=1.ed_ctr1.,;void1.ed-register(void)(1.ed_gpio_init();1.ed_dev.dops=&ops;1.ed-dev.name="1.ed"co1.a_device_register(&1.ed_dev);应用UaPP代码:inc1.ude<string.h>inc1.ude"app.h"inc1.ude"config.h"Sinc1.ude"co1.a_device.h"Minc1.ude"co1.a-os.h"statictask_ttimer_500ms;staticco1.a_device_t*app_1.ed_dev;/1.ed何500ms状态改变一次staticvoidtimer_500ms_cb(uint32_tevent)(co1.a_device_ctr1.(app_1.ed_dev,1.ED_TOGG1.E,0);)voidappJnit(void)(app_1.ed_dev=co1.a_device_find("1.ed");as$ert(app_1.ed_dev);co1.a_timer_create(&timer_500ms,timer_500ms_cb);co1.a_timer_start(&timer_500ms,TIMER_A1.WA