Writing an ILI9341 driver for the Radxa Rock 5B involves creating a Linux kernel module to interface with the ILI9341 TFT LCD controller via the board's SPI bus. The Radxa Rock 5B is based on the Rockchip RK3588 SoC, which supports SPI communication, making this feasible. However, before diving into custom driver development, it's worth checking if an existing driver can be used, as the Linux kernel already has support for ILI9341 in many cases.
I'll outline the process step by step. This assumes you have experience with Linux kernel development, C programming, and embedded systems. If you're new to this, start with some tutorials on kernel module writing. Note that this is a high-level guide; full driver code would require customization based on your hardware setup.
1. Prerequisites and Preparation
-
Hardware Setup: Ensure your ILI9341 display is connected to the Radxa Rock 5B. Typically, ILI9341 uses:
- SPI (MOSI, MISO, SCK, and CS pins).
- Additional GPIO pins for reset (RST), data/command (DC), and backlight control.
- Check the Radxa Rock 5B pinout (e.g., via the official documentation) and connect accordingly. For example:
- SPI0 or SPI1 on the 40-pin header (e.g., SPI0_MOSI on pin 19, SPI0_SCK on pin 23).
- Assign GPIOs for RST and DC.
-
Software Environment:
- Install the necessary tools on a Linux host machine (e.g., Ubuntu).
- Use a cross-compiler for ARM64 (e.g.,
aarch64-linux-gnu-gcc).
- Get the Linux kernel source for Rockchip RK3588. You can use the Radxa-provided kernel or the mainline kernel:
- Clone the Radxa kernel:
git clone https://github.com/radxa/kernel.git (branch for Rock 5B, e.g., linux-5.10-rk3588).
- Or use the mainline kernel:
git clone https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git and checkout a recent version (e.g., 5.15 or later).
- Install build dependencies:
sudo apt install build-essential bc bison flex libssl-dev libncurses5-dev
-
Check for Existing Drivers:
- The ILI9341 is supported by the
fbtft (Framebuffer TFT) driver in the Linux kernel. Search for it in your kernel source (grep -r "ILI9341" drivers/) or check the kernel config.
- If your kernel has
CONFIG_FB_TFT_ILI9341=y or as a module, you might not need to write a new driver. Instead:
- Enable it in your kernel config (
make menuconfig).
- Configure the device tree to include the ILI9341 (more on this below).
- Compile and boot the kernel, then load the module (e.g.,
modprobe fbtft_device name=adafruit22 reset=2 dc=25).
If it's not available or doesn't work with your setup, proceed to writing a custom driver.
2. Understand the Device Tree
The Radxa Rock 5B uses a Device Tree (DT) to describe hardware. You'll need to add or modify entries for SPI and the ILI9341.
- Edit the device tree source file (e.g.,
arch/arm64/boot/dts/rockchip/rk3588-rock-5b.dts).
- Enable SPI: Ensure the SPI controller is enabled, e.g.:
&spi0 {
status = "okay";
pinctrl-names = "default";
pinctrl-0 = <&spi0_pins>;
ili9341: ili9341@0 {
compatible = "ilitek,ili9341";
reg = <0>; // Chip select
spi-max-frequency = <50000000>; // Adjust based on your display
spi-cpha; // Clock phase, adjust as needed
spi-cpol; // Clock polarity
dc-gpios = <&gpio3 25 GPIO_ACTIVE_HIGH>; // Example: GPIO for DC pin
reset-gpios = <&gpio3 2 GPIO_ACTIVE_LOW>; // Example: GPIO for RST pin
rotate = <90>; // Optional: Rotation
fps = <60>; // Frames per second
buswidth = <8>; // 8-bit or 9-bit mode
bgr; // If your display uses BGR order
};
};
- Replace GPIO numbers with your actual pin assignments (e.g., using
gpio3 25 for the pin on GPIO bank 3, pin 25).
- Compile the DTB:
dtc -O dtb -o rk3588-rock-5b.dtb -b 0 -@ rk3588-rock-5b.dts.
This tells the kernel how to access the ILI9341 via SPI.
3. Writing the Custom Driver
If the existing fbtft driver isn't suitable, write a kernel module. This involves creating a framebuffer driver for the ILI9341.
-
Basic Structure:
A kernel driver typically includes:
- Probe function: Initializes the hardware.
- Remove function: Cleans up.
- Functions for SPI communication and display control.
Create a new file in drivers/video/fbdev/ (e.g., drivers/video/fbdev/ili9341.c).
Example skeleton (heavily simplified; you'll need to expand it):
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/spi/spi.h>
#include <linux/fb.h>
#include <linux/gpio/consumer.h>
static struct spi_driver ili9341_driver;
static int ili9341_probe(struct spi_device *spi)
{
struct fb_info *info;
struct gpio_desc *reset_gpio, *dc_gpio;
int ret;
// Get GPIOs
reset_gpio = devm_gpiod_get(&spi->dev, "reset", GPIOD_OUT_HIGH);
dc_gpio = devm_gpiod_get(&spi->dev, "dc", GPIOD_OUT_LOW);
if (!reset_gpio || !dc_gpio) return -ENODEV;
// Reset the display
gpiod_set_value_cansleep(reset_gpio, 1); // Active high or low?
msleep(10);
gpiod_set_value_cansleep(reset_gpio, 0);
msleep(10);
// Initialize SPI
spi->mode = SPI_MODE_0; // Adjust mode
spi_setup(spi);
// Send initialization commands via SPI (e.g., using spi_write)
// Example: Write to ILI9341 registers
uint8_t init_cmd[] = {0x01}; // Software reset command
spi_write(spi, init_cmd, sizeof(init_cmd));
// Set up framebuffer
info = framebuffer_alloc(0, &spi->dev);
if (!info) return -ENOMEM;
// Configure fb_info structure (e.g., info->screen_base, info->fix, etc.)
info->fix.smem_len = 320 * 240 * 2; // Example for 320x240 RGB565
info->var.xres = 320;
info->var.yres = 240;
// ... More configuration
ret = register_framebuffer(info);
if (ret < 0) {
framebuffer_release(info);
return ret;
}
return 0;
}
static int ili9341_remove(struct spi_device *spi)
{
struct fb_info *info = spi_get_drvdata(spi);
unregister_framebuffer(info);
framebuffer_release(info);
return 0;
}
static const struct of_device_id ili9341_of_match[] = {
{ .compatible = "ilitek,ili9341" },
{ }
};
MODULE_DEVICE_TABLE(of, ili9341_of_match);
static struct spi_driver ili9341_driver = {
.driver = {
.name = "ili9341",
.of_match_table = ili9341_of_match,
},
.probe = ili9341_probe,
.remove = ili9341_remove,
};
module_spi_driver(ili9341_driver);
MODULE_LICENSE("GPL");
- Key Parts:
- SPI Communication: Use
spi_write() or spi_w8r8() to send commands and data to the ILI9341. Refer to the ILI9341 datasheet for the full command set (e.g., for initialization, setting resolution, etc.).
- Framebuffer: Integrate with the Linux framebuffer subsystem to make the display usable as a /dev/fbX device.
4. Load and Test the Driver
- Boot your Radxa Rock 5B with the new kernel.
- Load the module:
insmod ili9341.ko.
- Verify: Check
dmesg for errors and look for the framebuffer device (e.g., /dev/fb1).
- Test the display: Use tools like
fbset to configure the framebuffer or write a simple program to draw on it (e.g., using open(/dev/fb1) in C).
5. Troubleshooting and Tips
- Common Issues:
- Pin conflicts: Ensure SPI pins aren't used for other functions (use
pinctrl in DT).
- SPI Configuration: Test SPI with userspace tools like
spidev_test before kernel development.
- Timing: ILI9341 requires precise timing for commands; use
msleep() or udelay() as needed.
- Debugging: Add printks in your driver and use
dmesg.
- Alternatives to Full Driver:
- If you don't need a kernel driver, use userspace libraries like WiringPi or libgpiod with spidev to control the display directly. This is simpler for prototyping.
- Example userspace code: Use
spidev to send commands via /dev/spidevX.Y.
- Resources:
- ILI9341 Datasheet: Search for it online.
- Linux Kernel Documentation:
Documentation/driver-api/index.rst, especially for SPI and Framebuffer.
- Radxa Rock 5B Docs: https://docs.radxa.com/en/rock5/5b/overview
- Example Drivers: Look at existing ones in the kernel source (e.g.,
drivers/video/fbdev/fbtft/fb_ili9341.c).
Warning: Kernel development can lead to system instability or boot issues. Always work on a backup image of your SD card and test incrementally. If you're not comfortable, consider contributing to or modifying existing drivers rather than starting from scratch. If you provide more details (e.g., your current setup or specific issues), I can refine this advice!