RT-Thread kernel study notes-understanding of the device model rt_device

RT-Thread Kernel Study Notes-Kernel Object rt_object

RT-Thread Kernel Study Notes-Kernel Object Management

RT-Thread Kernel Study Notes-Kernel Object Operation API

RT-Thread kernel study notes-kernel object initialization linked list organization

RT-Thread kernel study notes-in-depth understanding of the structure of the kernel object linked list

RT-Thread kernel study notes-understanding of the device model rt_device

RT-Thread Kernel Study Notes-Understanding defunct zombie threads

 

Preface

  • Recently, I was looking at the kernel source code, temporarily avoiding more complicated implementation methods such as task scheduling and memory management, and found that the implementation of the rt_device device framework is very simple.
  • rt_device, the device management framework (model), provides standard device operation interface APIs. Some peripherals can be abstracted into devices for unified management operations, such as LCD, Touch, Sensor, etc.

 

The structure of rt_device

  • rt_device is derived from the kernel object. Therefore, some operations are operating on the kernel object. The last few notes studied the management of kernel objects, and now I find that it is easy to understand by looking at the device.c file.

device.png

rt_device_class_type.png

 

Use of rt_device

  • RT-Thread's PIN, CAN, Serial, I2C, SPI, PM, etc. are all abstracted into a device model. These device models can be derived from rt_device.

Pin device model: The structure is as follows:

/* pin device and operations for RT-Thread */
struct rt_device_pin
{
    struct rt_device parent; /* 派生于rt_device */
    const struct rt_pin_ops *ops; /* 设备特有的操作接口,还可以根据需要增加其他成员 */
};
  • Therefore, users can derive the device framework they want, add specific device operation interfaces: ops, and specific attributes: structure members.
  • The specific device needs to be registered to the kernel container, and the registration interface of rt_device is called here.

Such as:

/* 使用时,需要把设备名称、操作接口等,传入 */
int rt_device_pin_register(const char *name, const struct rt_pin_ops *ops, void *user_data)
{
    _hw_pin.parent.type         = RT_Device_Class_Miscellaneous; /* 设备类型,为了区分设备种类 */
    _hw_pin.parent.rx_indicate  = RT_NULL; /* 接收回调,串口、CAN一般会有 */
    _hw_pin.parent.tx_complete  = RT_NULL; /* 发送回调,串口、CAN一般会有 */

#ifdef RT_USING_DEVICE_OPS
    _hw_pin.parent.ops          = &pin_ops;
#else
    _hw_pin.parent.init         = RT_NULL; /* 以下标准的rt_device设备操作接口,根据需要实现 */
    _hw_pin.parent.open         = RT_NULL;
    _hw_pin.parent.close        = RT_NULL;
    _hw_pin.parent.read         = _pin_read;
    _hw_pin.parent.write        = _pin_write;
    _hw_pin.parent.control      = _pin_control;
#endif

    _hw_pin.ops                 = ops;  /* 操作接口,设备的特有操作接口 */
    _hw_pin.parent.user_data    = user_data; /* 不是必要的用户数据 */

    /* register a character device */
    rt_device_register(&_hw_pin.parent, name, RT_DEVICE_FLAG_RDWR);  /* 设备注册接口:注册为具体设备 */

    return 0;
}
  • Specific equipment docking equipment framework
/* 具体设备的OPS 实现 */
const static struct rt_pin_ops _stm32_pin_ops =
{
    stm32_pin_mode,
    stm32_pin_write,
    stm32_pin_read,
    stm32_pin_attach_irq,
    stm32_pin_dettach_irq,
    stm32_pin_irq_enable,
};

/* 实际设备的注册方法 */
rt_device_pin_register("pin", &_stm32_pin_ops, RT_NULL);
  • After the device is registered, you can view it through: list_device

 

other

  • Before rt_device_read rt_device_write and other operations, you need: rt_device_open
  • rt_device_open rt_device_close operations are best to appear in pairs, because there is a reference count inside rt_device. For example, if you open twice and close once, the count is 1, there is no real close.
  • Generally, through rt_device_find, through the device name, the device is searched, and the operation handle of the device, which is the device structure pointer, can be obtained, so that the operation interface ops of the device can be further operated or the device can be operated through the standard operation interface of the device.
  • There are many types of RT-Thread devices, which can derive various device models (framework), so that many devices can be registered and mounted, and operations such as read-write control can be easily implemented, such as controlling hardware, sensors, etc.

 

to sum up

  • The device is derived from the kernel object: rt_object, familiar with the kernel object, it is conducive to be familiar with the operation of rt_device
  • Continue to study the RT-Thread core, continue to learn, and gain a lot.

Guess you like

Origin blog.csdn.net/tcjy1000/article/details/114046629