rt-thread的IO設備管理模塊為應用提供了一個對設備進行訪問的通用接口,,並通過定義的數據結構對設備驅動程序和設備信息進行管理。從系統整體位置來說I/O管理模塊相當於設備驅動程序和上層應用之間的一個中間層。
I/O管理模塊實現了對設備驅動程序的封裝:設備驅動程序的實現與I/O管理模塊獨立,提高了模塊的可移植性。應用程序通過I/O管理模塊提供的標准接口訪問底層設備,設備驅動程序的升級不會對上層應用產生影響。這種方式使得與設備的硬件操作相關的代碼與應用相隔離,雙方只需各自關注自己的功能,這降低了代碼的復雜性,提高了系統的可靠性。
1 IO設備管理控制塊
[cpp]
typedef struct rt_device *rt_device_t;
/**
* Device structure
*/
struct rt_device
{
struct rt_object parent; /**< inherit from rt_object *///內核對象
enum rt_device_class_type type; /**< device type *///IO設備類型
rt_uint16_t flag; /**< device flag *///設備標志
rt_uint16_t open_flag; /**< device open flag *///打開標志
rt_uint8_t device_id; /**< 0 - 255 *///設備ID
/* device call back */
rt_err_t (*rx_indicate)(rt_device_t dev, rt_size_t size);//數據接收回調函數
rt_err_t (*tx_complete)(rt_device_t dev, void *buffer);//數據發送完回調函數
/* common device interface */
rt_err_t (*init) (rt_device_t dev);//初始化通用接口
rt_err_t (*open) (rt_device_t dev, rt_uint16_t oflag);//打開通用接口
rt_err_t (*close) (rt_device_t dev);//關閉通用接口
rt_size_t (*read) (rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size);//讀通用接口
rt_size_t (*write) (rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size);//寫通用接口
rt_err_t (*control)(rt_device_t dev, rt_uint8_t cmd, void *args);//控制通用接口
#ifdef RT_USING_DEVICE_SUSPEND
rt_err_t (*suspend) (rt_device_t dev);//掛起設備
rt_err_t (*resumed) (rt_device_t dev);//還原設備
#endif
void *user_data; /**< device private data *///私有數據
};
其中設備類型type為一枚舉類型,有如下定義:
[cpp]
/**
* @addtogroup Device
*/
/*@{*/
/**
* device (I/O) class type
*/
enum rt_device_class_type
{
RT_Device_Class_Char = 0, /**< character device */
RT_Device_Class_Block, /**< block device */
RT_Device_Class_NetIf, /**< net interface */
RT_Device_Class_MTD, /**< memory device */
RT_Device_Class_CAN, /**< CAN device */
RT_Device_Class_RTC, /**< RTC device */
RT_Device_Class_Sound, /**< Sound device */
RT_Device_Class_Graphic, /**< Graphic device */
RT_Device_Class_I2CBUS, /**< I2C bus device */
RT_Device_Class_USBDevice, /**< USB slave device */
RT_Device_Class_USBHost, /**< USB host bus */
RT_Device_Class_SPIBUS, /**< SPI bus device */
RT_Device_Class_SPIDevice, /**< SPI device */
RT_Device_Class_SDIO, /**< SDIO bus device */
RT_Device_Class_PM, /**< PM pseudo device */
RT_Device_Class_Unknown /**< unknown device */
};
2 接口源碼分析
2.1 注冊設備
在一個設備能夠被上層應用訪問前,需要先把這個設備注冊到系統中,並添加一些相應的屬性。這些注冊的設備均可以采用“查找設備接口”通過設備名來查找設備,獲得該設備控制塊.
其源碼如下:
[cpp]
/**
* This function registers a device driver with specified name.
*
* @param dev the pointer of device driver structure
* @param name the device driver's name
* @param flags the flag of device
*
* @return the error code, RT_EOK on initialization successfully.
*/
rt_err_t rt_device_register(rt_device_t dev,
const char *name,
rt_uint16_t flags)
{
if (dev == RT_NULL)
return -RT_ERROR;
if (rt_device_find(name) != RT_NULL)//嘗試通過設備名查找該設備,如果查到,則說明已經注冊過,所以返回錯誤
return -RT_ERROR;
rt_object_init(&(dev->parent), RT_Object_Class_Device, name);//初始化內核對象,此過程會對內核對象添加到內核對象管理系統中,見之前的文章
dev->flag = flags;
return RT_EOK;
}
2.2 卸載設備
與注冊設備相反,卸載設備是將原先注冊好的一個設備從設備管理系統中移除:
[cpp]
/**
* This function removes a previously registered device driver
*
* @param dev the pointer of device driver structure
*
* @return the error code, RT_EOK on successfully.
*/
rt_err_t rt_device_unregister(rt_device_t dev)
{
RT_ASSERT(dev != RT_NULL);
rt_object_detach(&(dev->parent));//脫離內核對象,該過程會將內核對象從內核對象系統中移除
return RT_EOK;
}
2.3 初始化所有設備
初始化所有已經注冊到系統中的設備,該函數在rt-thread的啟動中會被調用.
[cpp]
/**
* This function initializes all registered device driver
*
* @return the error code, RT_EOK on successfully.
*/
rt_err_t rt_device_init_all(void)
{
struct rt_device *device;
struct rt_list_node *node;
struct rt_object_information *information;
register rt_err_t result;
extern struct rt_object_information rt_object_container[];
information = &rt_object_container[RT_Object_Class_Device];//通過類型找到對應的內核對象容器
/* for each device */
for (node = information->object_list.next;//依次掃描各個已經注冊的設備
node != &(information->object_list);
node = node->next)
{
rt_err_t (*init)(rt_device_t dev);
device = (struct rt_device *)rt_list_entry(node,//獲取設備控制塊
struct rt_object,
list);
/* get device init handler */
init = device->init;
if (init != RT_NULL && !(device->flag & RT_DEVICE_FLAG_ACTIVATED))//如果設備控制塊中已設置了初始化函數,則調用初始化函數進行初始化
{
result = init(device);
if (result != RT_EOK)
{
rt_kprintf("To initialize device:%s failed. The error code is %d\n",
device->parent.name, result);
}
else
{
device->flag |= RT_DEVICE_FLAG_ACTIVATED;//設置設備標志為已激活標志
}
}
}
return RT_EOK;
}
從上面源碼可知,此函數會從內核對象容器中逐個掃描注冊好的設備,然後調用其初始化函數進行初始化。
2.4 查找設備
此函數實現通過指定設備名找到對應的設備結構控制塊。
[cpp]
/**
* This function finds a device driver by specified name.
*
* @param name the device driver's name
*
* @return the registered device driver on successful, or RT_NULL on failure.
*/
rt_device_t rt_device_find(const char *name)
{
struct rt_object *object;
struct rt_list_node *node;
struct rt_object_information *information;
extern struct rt_object_information rt_object_container[];
/* enter critical */
if (rt_thread_self() != RT_NULL)//如果當前正在有線程在運行,則進入臨界區,即停止線程調度
rt_enter_critical();
/* try to find device object */
information = &rt_object_container[RT_Object_Class_Device];//獲取對應類型的內核對象容器
for (node = information->object_list.next;//依次掃描各個注冊好的設備
node != &(information->object_list);
node = node->next)
{
object = rt_list_entry(node, struct rt_object, list);//得到設備內核對象
if (rt_strncmp(object->name, name, RT_NAME_MAX) == 0)//比較名字
{
/* leave critical */
if (rt_thread_self() != RT_NULL)//離開臨界區,即使用調度器
rt_exit_critical();
return (rt_device_t)object;//返回當前設備控制塊
}
}
/* leave critical */
if (rt_thread_self() != RT_NULL)//離開臨界區,即使用調度器
rt_exit_critical();
/* not found */
return RT_NULL;
}
2.5 設備初始化
[cpp]
/**
* This function will initialize the specified device
*
* @param dev the pointer of device driver structure
*
* @return the result
*/
rt_err_t rt_device_init(rt_device_t dev)
{
rt_err_t result = RT_EOK;
RT_ASSERT(dev != RT_NULL);
/* get device init handler */
if (dev->init != RT_NULL)
{
if (!(dev->flag & RT_DEVICE_FLAG_ACTIVATED))//如果當前設備沒有激活
{
result = dev->init(dev);//調用其初始化函數進行初始化
if (result != RT_EOK)
{
rt_kprintf("To initialize device:%s failed. The error code is %d\n",
dev->parent.name, result);
}
else
{
dev->flag |= RT_DEVICE_FLAG_ACTIVATED;//設備設備激活標志
}
}
}
return result;
}
2.6 打開設備
[cpp]
/**
* This function will open a device
*
* @param dev the pointer of device driver structure
* @param oflag the flags for device open
*
* @return the result
*/
rt_err_t rt_device_open(rt_device_t dev, rt_uint16_t oflag)
{
rt_err_t result = RT_EOK;
RT_ASSERT(dev != RT_NULL);
/* if device is not initialized, initialize it. */
if (!(dev->flag & RT_DEVICE_FLAG_ACTIVATED))//如果當前設備沒有激活
{
if (dev->init != RT_NULL)
{
result = dev->init(dev);//調用其初始化函數進行初始化
if (result != RT_EOK)
{
rt_kprintf("To initialize device:%s failed. The error code is %d\n",
dev->parent.name, result);
return result;
}
}
dev->flag |= RT_DEVICE_FLAG_ACTIVATED;//設備激活標志
}
/* device is a stand alone device and opened *///如果設備已經打開
if ((dev->flag & RT_DEVICE_FLAG_STANDALONE) &&
(dev->open_flag & RT_DEVICE_OFLAG_OPEN))
{
return -RT_EBUSY;
}
/* call device open interface */
if (dev->open != RT_NULL)
{
result = dev->open(dev, oflag);//打開設備
}
/* set open flag */
if (result == RT_EOK || result == -RT_ENOSYS)
dev->open_flag = oflag | RT_DEVICE_OFLAG_OPEN;//設備打開標志
return result;
}
2.7 關閉設備
[cpp]
/**
* This function will close a device
*
* @param dev the pointer of device driver structure
*
* @return the result
*/
rt_err_t rt_device_close(rt_device_t dev)
{
rt_err_t result = RT_EOK;
RT_ASSERT(dev != RT_NULL);
/* call device close interface */
if (dev->close != RT_NULL)
{
result = dev->close(dev);//關閉設備
}
/* set open flag */
if (result == RT_EOK || result == -RT_ENOSYS)
dev->open_flag = RT_DEVICE_OFLAG_CLOSE;//設置打開標志為關閉狀態
return result;
}
2.8 讀設備
[cpp]
/**
* This function will read some data from a device.
*
* @param dev the pointer of device driver structure
* @param pos the position of reading
* @param buffer the data buffer to save read data
* @param size the size of buffer
*
* @return the actually read size on successful, otherwise negative returned.
*
* @note since 0.4.0, the unit of size/pos is a block for block device.
*/
rt_size_t rt_device_read(rt_device_t dev,
rt_off_t pos,
void *buffer,
rt_size_t size)
{
RT_ASSERT(dev != RT_NULL);
/* call device read interface */
if (dev->read != RT_NULL)//如果當前存在讀取接口
{
return dev->read(dev, pos, buffer, size);//調用讀接口進行讀操作
}
/* set error code */
rt_set_errno(-RT_ENOSYS);//如果當前不存在讀取接口,則設置錯誤碼為-RT_ENOSYS
return 0;
}
2.9 寫設備
[cpp]
/**
* This function will write some data to a device.
*
* @param dev the pointer of device driver structure
* @param pos the position of written
* @param buffer the data buffer to be written to device
* @param size the size of buffer
*
* @return the actually written size on successful, otherwise negative returned.
*
* @note since 0.4.0, the unit of size/pos is a block for block device.
*/
rt_size_t rt_device_write(rt_device_t dev,
rt_off_t pos,
const void *buffer,
rt_size_t size)
{
RT_ASSERT(dev != RT_NULL);
/* call device write interface */
if (dev->write != RT_NULL)//如果當前存在寫接口
{
return dev->write(dev, pos, buffer, size);//調用寫接口進行寫操作
}
/* set error code */
rt_set_errno(-RT_ENOSYS);//如果當前不存在寫接口,則設備當前錯誤碼為-RT_ENOSYS
return 0;
}
2.10 控制設備
[cpp]
/**
* This function will perform a variety of control functions on devices.
*
* @param dev the pointer of device driver structure
* @param cmd the command sent to device
* @param arg the argument of command
*
* @return the result
*/
rt_err_t rt_device_control(rt_device_t dev, rt_uint8_t cmd, void *arg)
{
RT_ASSERT(dev != RT_NULL);
/* call device write interface */
if (dev->control != RT_NULL)
{
return dev->control(dev, cmd, arg);//調用控制接口進行操作
}
return RT_EOK;
}
2.11 設備接收回調函數
如果設備接收到數據時,將會主動調用此回調函數進行處理,但一般只是進行些簡單的操作,如發送信號量,而讓另一個接收線程來處理接收到的數據.該接口只是給設備控制塊設置此回調函數
[cpp]
/**
* This function will set the indication callback function when device receives
* data.
*
* @param dev the pointer of device driver structure
* @param rx_ind the indication callback function
*
* @return RT_EOK
*/
rt_err_t
rt_device_set_rx_indicate(rt_device_t dev,
rt_err_t (*rx_ind)(rt_device_t dev, rt_size_t size))
{
RT_ASSERT(dev != RT_NULL);
dev->rx_indicate = rx_ind;
return RT_EOK;
}
2.12 設備發送回調函數
與接收回調函數對應,但設備發送完數據時,也會調用此回調函數。此接口只是用來給設備控制塊設備此回調函數。
[cpp]
/**
* This function will set the indication callback function when device has
* written data to physical hardware.
*
* @param dev the pointer of device driver structure
* @param tx_done the indication callback function
*
* @return RT_EOK
*/
rt_err_t
rt_device_set_tx_complete(rt_device_t dev,
rt_err_t (*tx_done)(rt_device_t dev, void *buffer))
{
RT_ASSERT(dev != RT_NULL);
dev->tx_complete = tx_done;
return RT_EOK;
}
3 設備驅動實現的步驟
上述內容已經比較詳細地介紹了設備控制塊的數據結構及其相關的操作接口,那麼在具體實現中,又是如何實現一個設備的驅動的呢?www.2cto.com
步驟1:根據rt_device定義的結構定義一設備變量,根據設備公共接口,實現各個接口,當然也可以是空函數。
步驟2:根據自己的設備類型定義自己的私有數據域。特別是可以有多個相同設備的情況下,設備接口可以用同一套,不同的只是各自的數據域(例如寄存器基地址)。
步驟3: 按照RT-Thread的對象模型,擴展一個對象有兩種方式:
(a) 定義自己的私有數據結構,然後賦值到RT-Thread設備控制塊的private指針上。
(b) 從struct rt device結構中進行派生。
步驟4: 根據設備的類型,注冊到RT-Thread設備框架中,即調用rt_device_register接口進行注冊.
完!