In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
How to understand the device model rt_device in RT-Thread? aiming at this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.
Preface
Recently, after looking at the kernel source code and temporarily avoiding the more complex implementation methods such as mental task scheduling and memory management, it is found that the implementation of rt_device device framework is very simple.
Rt_device, the framework (model) of equipment management, provides standard equipment operation interface API, and some peripherals can be abstracted into devices for unified management operations, such as LCD, Touch, Sensor and so on.
The structure of rt_device
Rt_device is derived from kernel objects, so some operations are just manipulating kernel objects. The last few notes studied the management of kernel objects, and now I find it easy to read device.c files.
The use of rt_device
RT-Thread 's PIN, CAN, Serial, I2C, SPI, PM and so on 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; / * derives from the specific operation interface of rt_device * / const struct rt_pin_ops * ops; / * devices, and other members can be added as needed * /}
So users can derive the device framework they want and add the operation interface of specific devices: ops, specific properties: structure members.
You need to register the specific device with the kernel container, where the registration interface of rt_device is called.
Such as:
/ * when using, you need to input * / 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; / * device type, in order to distinguish the device type * / _ hw_pin.parent.rx_indicate = RT_NULL / * receive callback. Generally, serial port and CAN will send callback with * / _ hw_pin.parent.tx_complete = RT_NULL; / *. Serial port and CAN will generally have * / # ifdef RT_USING_DEVICE_OPS _ hw_pin.parent.ops = & pin_ops;#else _ hw_pin.parent.init = RT_NULL. / * the following standard rt_device device operation interfaces are implemented as needed * / _ 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; / * operation interface, the unique operation interface of the device * / _ hw_pin.parent.user_data = user_data; / * is not necessary user data * / / * register a character device * / rt_device_register (& _ hw_pin.parent, name, RT_DEVICE_FLAG_RDWR) / * device registration API: register as a specific device * / return 0;}
Specific equipment docking equipment framework
/ * OPS implementation of specific devices * / 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,}; / * Registration method of actual devices * / rt_device_pin_register ("pin", & _ stm32_pin_ops, RT_NULL)
After the device is registered, you can view it at: list_device
Other
Before rt_device_read rt_device_write and other operations, rt_device_open is required.
Rt_device_open rt_device_close operations are best seen in pairs because there is a reference count within rt_device, such as you open twice, close once, and the count is 1, and there is no real close.
Generally through rt_device_find, through the device name, find the device, get the operation handle of the device, that is, the device structure pointer, so that you can further operate the device's operation interface ops or through the device's standard operation interface.
There are many types of devices in RT-Thread, and various device models (frameworks) can be derived, so that many devices can be registered and mounted, and it is convenient to achieve read-write control and other operations, such as control hardware, sensors and so on.
This is the answer to the question on how to understand the rt_device of the device model in RT-Thread. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.