"RT-Thread Device Driver Development Guide" Advanced Chapter - Hands-on Driver for LCD Peripherals
I. Overview
After expounding the previous article " Basics of "RT-Thread Device Driver Development Guide" - Taking the hwtimer device of Xianji bsp as an example ", you can roughly understand the methods and steps of RT-thread device driver development, and the progress in the development guide. The peripherals in the first chapter are mainly peripheral devices that are more complex than the peripherals in the basic chapter, including SDIO, Touch, LCD, sensor, MTD nor, MTD nand, pulse encoder, encryption and decryption equipment, and PM equipment.
For the advanced peripherals, this article does not elaborate on the advanced peripherals that Xianji already supports. Instead, it conducts a hands-on practice of driving a driver peripheral that Xianji has not yet adapted - LCD, to further develop RT-thread. Device driver.
Here’s an effect:
Based on the official example of wifi_web_camera_demo, add the LCD driver to display the camera
2. Development methods
(1) Introduction to the LCD driver framework of RT-thread
The LCD driver is similar to other peripherals. It is nothing more than the IO device management framework-->LCD device driver framework->LCD device driver.
The IO device management layer mainly provides a unified operation interface for the device framework, including rt_device_read/write/open/close/control, etc. The ultimate goal of making a peripheral driver is to use these unified operation interfaces in applications without paying attention to the underlying operations.
The LCD device driver framework layer is RT-thread's abstraction of the basic functions of the LCD. It is a general software layer that has nothing to do with the hardware platform. It abstracts the type definition and specific operation methods of the LCD device. Its framework source code is in rtdef.h
The LCD information structure rt_device_graphic_info defines the pixel format, bit width, width and height, video memory address, etc.
The LCD operation interface rt_device_graphic_ops defines LCD drawing operations, such as pixel points, line drawing and other operations.
LCD control commands, such as updating video memory, switching LCD on and off, obtaining information, waiting for VSYNC, etc.
LCD pixel format
It can be seen that RT-thread is relatively intuitive for the abstract operation of LCD. After completing the above operations, and then registering with the registration interface rt_device_register provided by the IO device management layer, you can use the IO device management interface to operate the LCD.
(2) First introduction to LCDC peripherals
For the first LCDC peripheral hardware controller
There is an RGB controller on the HPM6750, which supports a 24-bit RGB display interface.
On HPM6800, supports 2 4 Lane MIPI-DSI/LVDS-Tx display interfaces
For RT-thread, the panel component of hpm_sdk is also supported, covering the display adaptation of hpm6750 and hpm6800.
For an introduction to the panel component of hpm_sdk, you can refer to the article " [hpm_application] Choose first!" Cool running 1080P or 720P mipi screen microcontroller 》
For screens that use LCD hardware controllers (RGB, MIPI DSI) to communicate, the LCD device driver only needs to implement the LCD device operating method struct rt_device_ops. For example, updating the cache area of the hardware controller in the control interface can control screen drawing.
In this way, with the RT-thread BSP adapted first, the LCD driver can be easily adapted. According to the driver of the BSP first, create a drv_lcd.
(3) Create LCD device
Let's create the first LCD device. The LCD device model is derived from the struct rt_device structure and adds its own private data.
The members mainly include the base address of the LCD, interrupt number, mutex lock, LCD name, etc.
Instantiate an LCD device array. There can be multiple LCD devices, which contains the initialization parameters of the device.
(4) Implement the operation method of LCD equipment
According to the above definition, an hpm_lcd_ops structure has been instantiated, and then the corresponding operation function needs to be implemented, which is the LCD device definition operation method. For LCD devices, only the init and control operation methods are meaningful, and this article implements these two interfaces, namely hpm_lcd_init and hpm_lcd_control.
1. init: initialize LCD device
For this operation method, the LCD driver code does not need to perform any operations, it only needs to initialize the LCD during registration.
2. control: control LCD device
The application can also control the LCD device through the control method. The prototype is as follows:
The parameter cmd contains the method of controlling the LCD. The following macro definitions are available for general device commands:
Of course, several commands have also been updated in the latest rtthread 5.0.2
This article mainly implements the following commands here
(1) RTGRAPHIC_CTRL_RECT_UPDATE updates video memory
For the update of video memory, you can use the video memory defined by the controller itself or use the incoming video memory address. The shadow register loading enable function is mentioned in the Xianji manual, that is, the video memory will be loaded when the next VSYNC arrives. This can Do a complete update of the display.
This API is also provided in the hpmicro driver: lcdc_layer_set_next_buffer. The driver adapted here uses the first layer.
Therefore it can be achieved as follows:
(2) RTGRAPHIC_CTRL_WAIT_VSYNC waits for the frame to complete
In order to ensure high efficiency and anti-tearing of transmission, an interrupt method is used to transmit one frame and wait for VSYNC to complete before updating the next frame.
(3)RTGRAPHIC_CTRL_GET_INFO Get LCD information
Mainly obtains the pixel format, width and height of the LCD and passes it to the args parameter
(4)RTGRAPHIC_CTRL_POWERON bright screen
RTGRAPHIC_CTRL_POWEROFF screen off
Just call the panel component interface of hpm_sdk.
(5) Register LCD equipment
Use rt_device_register of the IO device framework to complete the registration, mainly focusing on the assignment part of the device operation method, and saving the LCD device example and assigning it to the user_data member of LCD.
(6) Driver configuration
Enable LCD in the Kconfig of the board. This macro controls whether LCD driver related code is added to the project.
In addition, add the panel component. This macro has been implemented first. The hpm6750 is used here, so it only has RGB interface.
Add the LCD driver judgment option in the SConscript file of the driver. If enabled, drv_lcd.c will be added to the project.
(7) Driver verification
After registering the device, the LCD device will appear in the IO device manager. We use the official example of wifi_web_camera_demo of hpm6750evkmini as verification and burning verification. Use the list device command to see that the registered device already contains the LCD device.
In the example of wifi_web_camera_demo, in the interface for obtaining the camera image, add the device operation of LCD to display the image on the LCD. You only need to open the LCD first, and then control. The display can be achieved with two statements, which is particularly convenient for operation.
Finally, it is possible to operate the LCD using simple IO devices