1、IO设备类型

  1. RT_Device_Class_Char = 0, /**< character device */
  2. RT_Device_Class_Block, /**< block device */
  3. RT_Device_Class_NetIf, /**< net interface */
  4. RT_Device_Class_MTD, /**< memory device */
  5. RT_Device_Class_CAN, /**< CAN device */
  6. RT_Device_Class_RTC, /**< RTC device */
  7. RT_Device_Class_Sound, /**< Sound device */
  8. RT_Device_Class_Graphic, /**< Graphic device */
  9. RT_Device_Class_I2CBUS, /**< I2C bus device */
  10. RT_Device_Class_USBDevice, /**< USB slave device */
  11. RT_Device_Class_USBHost, /**< USB host bus */
  12. RT_Device_Class_SPIBUS, /**< SPI bus device */
  13. RT_Device_Class_SPIDevice, /**< SPI device */
  14. RT_Device_Class_SDIO, /**< SDIO bus device */
  15. RT_Device_Class_Timer, /**< Timer device */
  16. RT_Device_Class_Miscellaneous, /**< misc device */
  17. RT_Device_Class_Sensor, /**< Sensor device */
  18. RT_Device_Class_Touch, /**< Touch device */
  19. RT_Device_Class_Unknown /**< unknown device */

2、设备被创建后,需要实现它访问硬件的操作方法

  1. rt_err_t (*init) (rt_device_t dev);
  2. rt_err_t (*open) (rt_device_t dev, rt_uint16_t oflag);
  3. rt_err_t (*close) (rt_device_t dev);
  4. rt_size_t (*read) (rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size);
  5. rt_size_t (*write) (rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size);
  6. rt_err_t (*control)(rt_device_t dev, int cmd, void *args);

3、设备被创建后,需要注册到 I/O 设备管理器中,应用程序才能够访问

  1. /**
  2. * This function registers a device driver with specified name.
  3. *
  4. * @param dev the pointer of device driver structure
  5. * @param name the device driver's name
  6. * @param flags the capabilities flag of device 设备模式标志
  7. *
  8. * @return the error code, RT_EOK on initialization successfully.
  9. */
  10. rt_err_t rt_device_register(rt_device_t dev,
  11. const char *name,
  12. rt_uint16_t flags)
  13. #define RT_DEVICE_FLAG_RDONLY 0x001 /*只读*/
  14. #define RT_DEVICE_FLAG_WRONLY 0x002 /*只写*/
  15. #define RT_DEVICE_FLAG_RDWR 0x003 /*读写*/
  16. #define RT_DEVICE_FLAG_REMOVABLE 0x004 /*可移除*/
  17. #define RT_DEVICE_FLAG_STANDALONE 0x008 /*独立*/
  18. #define RT_DEVICE_FLAG_SUSPENDED 0x020 /*挂起*/
  19. #define RT_DEVICE_FLAG_STREAM 0x040 /*流模式*/
  20. #define RT_DEVICE_FLAG_INT_RX 0x100 /*中断接收*/
  21. #define RT_DEVICE_FLAG_DMA_RX 0x200 /*DMA接收*/
  22. #define RT_DEVICE_FLAG_INT_TX 0x400 /*中断发送*/
  23. #define RT_DEVICE_FLAG_DMA_TX 0x800 /* DMA发送*/

4、设备注销后的,设备将从设备管理器中移除,也就不能再通过设备查找搜索到该设备。注销设备不会释放设备控制块占用的内存

  1. /**
  2. * This function removes a previously registered device driver
  3. *
  4. * @param dev the pointer of device driver structure
  5. *
  6. * @return the error code, RT_EOK on successfully.
  7. */
  8. rt_err_t rt_device_unregister(rt_device_t dev)

5、数据收发回调,当硬件设备收到数据时,可以通过如下函数回调另一个函数来设置数据接收指示,通知上层应用线程有数据到达

  1. /**
  2. * This function will set the reception indication callback function.
  3. * This callback function
  4. * is invoked when this device receives data.
  5. *
  6. * @param dev the pointer of device driver structure
  7. * @param rx_ind the indication callback function
  8. *
  9. * @return RT_EOK
  10. */
  11. rt_err_t
  12. rt_device_set_rx_indicate(rt_device_t dev,
  13. rt_err_t (*rx_ind)(rt_device_t dev, rt_size_t size))
  14. /**
  15. * This function will set the indication callback function when device has
  16. * written data to physical hardware.
  17. *
  18. * @param dev the pointer of device driver structure
  19. * @param tx_done the indication callback function
  20. *
  21. * @return RT_EOK
  22. */
  23. rt_err_t
  24. rt_device_set_tx_complete(rt_device_t dev,
  25. rt_err_t (*tx_done)(rt_device_t dev, void *buffer))

示例

  1. #include <rtthread.h>
  2. #define DBG_TAG "main"
  3. #define DBG_LVL DBG_LOG
  4. #include <rtdbg.h>
  5. int main(void)
  6. {
  7. rt_device_t dev;
  8. dev = rt_device_find("demo");
  9. rt_device_init(dev);
  10. rt_device_open(dev,RT_DEVICE_OFLAG_RDWR);
  11. rt_device_close(dev);
  12. //rt_device_destroy(dev);
  13. return 0;
  14. }
  1. #include <rtdevice.h>
  2. rt_err_t demo_init(rt_device_t dev)
  3. {
  4. rt_kprintf("demo_init...\n");
  5. return 0;
  6. }
  7. rt_err_t demo_open(rt_device_t dev, rt_uint16_t oflag)
  8. {
  9. rt_kprintf("demo_open...\n");
  10. return 0;
  11. }
  12. rt_err_t demo_close(rt_device_t dev)
  13. {
  14. rt_kprintf("demo_close...\n");
  15. return 0;
  16. }
  17. int rt_demo_init(void)
  18. {
  19. rt_device_t demo_dev;
  20. demo_dev = rt_device_create(RT_Device_Class_Char, 512);
  21. demo_dev->init = demo_init;
  22. demo_dev->open = demo_open;
  23. demo_dev->close = demo_close;
  24. rt_device_register(demo_dev, "demo", RT_DEVICE_FLAG_RDWR);
  25. return 0;
  26. }
  27. INIT_BOARD_EXPORT(rt_demo_init);