初學(xué)單片機(jī)的時(shí)候,當(dāng)你寫(xiě)簡(jiǎn)單的裸機(jī)代碼時(shí)可能沒(méi)有底層驅(qū)動(dòng)、中間層、應(yīng)用層這些概念。但當(dāng)你把項(xiàng)目做大了,你就會(huì)發(fā)現(xiàn),分層是非常有必要的。 今天酒類給大家分享一個(gè)常見(jiàn)的分層案例。
一、前言
以STM32為例,打開(kāi)網(wǎng)絡(luò)上下載的例程或者是購(gòu)買(mǎi)開(kāi)發(fā)板自帶的例程,都會(huì)發(fā)現(xiàn)應(yīng)用層中會(huì)有stm32f10x.h或者stm32f10x_gpio.h,這些文件嚴(yán)格來(lái)時(shí)屬于硬件層的,如果軟件層出現(xiàn)這些文件會(huì)顯得很亂。
使用過(guò)Linux的童鞋們肯定知道linux系統(tǒng)無(wú)法直接操作硬件層,打開(kāi)linux或者rt_thread代碼會(huì)發(fā)現(xiàn)代碼中都會(huì)有device的源文件,沒(méi)錯(cuò),這就是驅(qū)動(dòng)層。
二、實(shí)現(xiàn)原理
原理就是將硬件操作的接口全都放到驅(qū)動(dòng)鏈表上,在驅(qū)動(dòng)層實(shí)現(xiàn)device的open、read、write等操作。當(dāng)然這樣做也有弊端,就是驅(qū)動(dòng)find的時(shí)候需要遍歷一遍驅(qū)動(dòng)鏈表,這樣會(huì)增加代碼運(yùn)行時(shí)間。
三、代碼實(shí)現(xiàn)
國(guó)際慣例,寫(xiě)代碼先寫(xiě)頭文件。rt_thread中使用的是雙向鏈表,為了簡(jiǎn)單在這我只用單向鏈表。有興趣的可以自行研究rt_thread
頭文件接口:
本次只實(shí)現(xiàn)如下接口,device_open ?和device_close等剩下的接口可以自行研究。這樣就可以在應(yīng)用層中只調(diào)用如下接口可實(shí)現(xiàn):
/* ????驅(qū)動(dòng)注冊(cè) */ int?cola_device_register(cola_device_t?*dev); /* ????驅(qū)動(dòng)查找 */ cola_device_t?*cola_device_find(const?char?*name); /* ????驅(qū)動(dòng)讀 */ int?cola_device_read(cola_device_t?*dev,??int?pos,?void?*buffer,?int?size); /* ????驅(qū)動(dòng)寫(xiě) */ int?cola_device_write(cola_device_t?*dev,?int?pos,?const?void?*buffer,?int?size); /* ????驅(qū)動(dòng)控制 */ int?cola_device_ctrl(cola_device_t?*dev,??int?cmd,?void?*arg);;
頭文件cola_device.h:
#ifndef?_COLA_DEVICE_H_ #define?_COLA_DEVICE_H_ ? ? enum?LED_state { ????LED_OFF, ????LED_ON, ????LED_TOGGLE, ? }; ? typedef?struct?cola_device??cola_device_t; ? struct?cola_device_ops { ????int??(*init)???(cola_device_t?*dev); ????int??(*open)???(cola_device_t?*dev,?int?oflag); ????int??(*close)??(cola_device_t?*dev); ????int??(*read)???(cola_device_t?*dev,?int?pos,?void?*buffer,?int?size); ????int??(*write)??(cola_device_t?*dev,?int?pos,?const?void?*buffer,?int?size); ????int??(*control)(cola_device_t?*dev,?int?cmd,?void?*args); ? }; ? struct?cola_device { ????const?char?*?name; ????struct?cola_device_ops?*dops; ????struct?cola_device?*next; }; ? /* ????驅(qū)動(dòng)注冊(cè) */ int?cola_device_register(cola_device_t?*dev); /* ????驅(qū)動(dòng)查找 */ cola_device_t?*cola_device_find(const?char?*name); /* ????驅(qū)動(dòng)讀 */ int?cola_device_read(cola_device_t?*dev,??int?pos,?void?*buffer,?int?size); /* ????驅(qū)動(dòng)寫(xiě) */ int?cola_device_write(cola_device_t?*dev,?int?pos,?const?void?*buffer,?int?size); /* ????驅(qū)動(dòng)控制 */ int?cola_device_ctrl(cola_device_t?*dev,??int?cmd,?void?*arg); ? #endif?
源文件cola_device.c:
#include?"cola_device.h" #include?#include? ? ? struct?cola_device?*device_list?=?NULL; ? /* ????查找任務(wù)是否存在 */ static?bool?cola_device_is_exists(?cola_device_t?*dev?) { ????cola_device_t*?cur?=?device_list; ????while(?cur?!=?NULL?) ????{ ????????if(?strcmp(cur->name,dev->name)==0) ????????{ ????????????return?true; ????????} ????????cur?=?cur->next; ????} ????return?false; } ? ? static?int?device_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; ????} ????return?1; } ? /* ????驅(qū)動(dòng)注冊(cè) */ int?cola_device_register(cola_device_t?*dev) { ????if((NULL?==?dev)?||?(cola_device_is_exists(dev))) ????{ ????????return?0; ????} ? ????if((NULL?==?dev->name)?||??(NULL?==?dev->dops)) ????{ ????????return?0; ????} ????return?device_list_inster(dev); ? } /* ????驅(qū)動(dòng)查找 */ cola_device_t?*cola_device_find(const?char?*name) { ????cola_device_t*?cur?=?device_list; ????while(?cur?!=?NULL?) ????{ ????????if(?strcmp(cur->name,name)==0) ????????{ ????????????return?cur; ????????} ????????cur?=?cur->next; ????} ????return?NULL; } /* ????驅(qū)動(dòng)讀 */ int?cola_device_read(cola_device_t?*dev,??int?pos,?void?*buffer,?int?size) { ????if(dev) ????{ ????????if(dev->dops->read) ????????{ ????????????return?dev->dops->read(dev,?pos,?buffer,?size); ????????} ????} ????return?0; } /* ????驅(qū)動(dòng)寫(xiě) */ int?cola_device_write(cola_device_t?*dev,?int?pos,?const?void?*buffer,?int?size) { ????if(dev) ????{ ????????if(dev->dops->write) ????????{ ????????????return?dev->dops->write(dev,?pos,?buffer,?size); ????????} ????} ????return?0; } /* ????驅(qū)動(dòng)控制 */ int?cola_device_ctrl(cola_device_t?*dev,??int?cmd,?void?*arg) { ????if(dev) ????{ ????????if(dev->dops->control) ????????{ ????????????return?dev->dops->control(dev,?cmd,?arg); ????????} ????} ????return?0; }
硬件注冊(cè)方式:以LED為例,初始化接口void led_register(void),需要在初始化中調(diào)用。
? #include?"stm32f0xx.h" #include?"led.h" #include?"cola_device.h" ? ? #define?PORT_GREEN_LED?????????????????GPIOC??????????????????? #define?PIN_GREENLED???????????????????GPIO_Pin_13?????????????? ? /*?LED亮、滅、變化?*/ #define?LED_GREEN_OFF??????????????????(PORT_GREEN_LED->BSRR?=?PIN_GREENLED) #define?LED_GREEN_ON???????????????????(PORT_GREEN_LED->BRR??=?PIN_GREENLED) #define?LED_GREEN_TOGGLE???????????????(PORT_GREEN_LED->ODR?^=?PIN_GREENLED) ? ? static?cola_device_t?led_dev; ? static?void?led_gpio_init(void) { ????GPIO_InitTypeDef?GPIO_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; } ? static?int?led_ctrl(cola_device_t?*dev,?int?cmd,?void?*args) { ????if(LED_TOGGLE?==?cmd) ????{ ????????LED_GREEN_TOGGLE; ????} ????else? ????{ ???????? ????} ????return?1; } ? ? static?struct?cola_device_ops?ops?= { ????.control?=?led_ctrl, }; ? void?led_register(void) { ????led_gpio_init(); ????led_dev.dops?=?&ops; ????led_dev.name?=?"led"; ????cola_device_register(&led_dev); }
應(yīng)用層app代碼:
#include?#include?"app.h" #include?"config.h" #include?"cola_device.h" #include?"cola_os.h" ? static?task_t?timer_500ms; static?cola_device_t?*app_led_dev; ? //led每500ms狀態(tài)改變一次 static?void?timer_500ms_cb(uint32_t?event) { ????cola_device_ctrl(app_led_dev,LED_TOGGLE,0); } ? void?app_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文件中就不需要調(diào)用led.h頭文件了,rtt就是這樣實(shí)現(xiàn)的。
四、總結(jié)
這樣就可以實(shí)現(xiàn)軟硬件分層了,是不是非常好用!
編輯:黃飛
?
評(píng)論
查看更多