一、前言
以STM32為例,打開網(wǎng)絡(luò)上下載的例程或者是購(gòu)買開發(fā)板自帶的例程,都會(huì)發(fā)現(xiàn)應(yīng)用層中會(huì)有stm32f10x.h或者stm32f10x_gpio.h,這些文件嚴(yán)格來時(shí)屬于硬件層的,如果軟件層出現(xiàn)這些文件會(huì)顯得很亂。
使用過Linux的童鞋們肯定知道linux系統(tǒng)無法直接操作硬件層,打開linux或者rt_thread代碼會(huì)發(fā)現(xiàn)代碼中都會(huì)有device的源文件,沒錯(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ó)際慣例,寫代碼先寫頭文件。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(constchar*name); /* 驅(qū)動(dòng)讀 */ int cola_device_read(cola_device_t *dev, int pos, void *buffer, int size); /* 驅(qū)動(dòng)寫 */ 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_ 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; }; /* 驅(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)寫 */ 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) { returntrue; } cur=cur->next; } returnfalse; } 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; } return1; } /* 驅(qū)動(dòng)注冊(cè) */ int cola_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); } /* 驅(qū)動(dòng)查找 */ 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; } /* 驅(qū)動(dòng)讀 */ int cola_device_read(cola_device_t *dev, int pos, void *buffer, int size) { if(dev) { if(dev->dops->read) { returndev->dops->read(dev,pos,buffer,size); } } return0; } /* 驅(qū)動(dòng)寫 */ int cola_device_write(cola_device_t *dev, int pos, const void *buffer, int size) { if(dev) { if(dev->dops->write) { returndev->dops->write(dev,pos,buffer,size); } } return0; } /* 驅(qū)動(dòng)控制 */ int cola_device_ctrl(cola_device_t *dev, int cmd, void *arg) { if(dev) { if(dev->dops->control) { returndev->dops->control(dev,cmd,arg); } } return0; }
硬件注冊(cè)方式:以LED為例,初始化接口void led_register(void),需要在初始化中調(diào)用。
#include"stm32f0xx.h" #include"led.h" #include"cola_device.h" #definePORT_GREEN_LEDGPIOC #define PIN_GREENLED GPIO_Pin_13 /*LED亮、滅、變化*/ #defineLED_GREEN_OFF(PORT_GREEN_LED->BSRR=PIN_GREENLED) #defineLED_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_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; } static int led_ctrl(cola_device_t *dev, int cmd, void *args) { if(LED_TOGGLE==cmd) { LED_GREEN_TOGGLE; } else{ } return1; } staticstructcola_device_opsops= { .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" statictask_ttimer_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)軟硬件分層了,是不是非常好用!
五、代碼下載鏈接
https://gitee.com/schuck/cola_os
審核編輯:劉清
-
嵌入式
+關(guān)注
關(guān)注
5060文章
18980瀏覽量
302239 -
STM32
+關(guān)注
關(guān)注
2264文章
10858瀏覽量
354391 -
C語言
+關(guān)注
關(guān)注
180文章
7595瀏覽量
135925
原文標(biāo)題:分享一個(gè)通用的嵌入式驅(qū)動(dòng)層
文章出處:【微信號(hào):技術(shù)讓夢(mèng)想更偉大,微信公眾號(hào):技術(shù)讓夢(mèng)想更偉大】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論