使用Linux內(nèi)核里的spi屏驅(qū)動(dòng)-fbtft
Linux
內(nèi)核里已經(jīng)提供spi接口小屏的設(shè)備驅(qū)動(dòng),在內(nèi)核的配置選項(xiàng):
make menuconfig ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf-
Device Drivers --->
Graphics support --->
<*> Support for small TFT LCD display modules --->
... //屏的驅(qū)動(dòng)IC型號(hào)
<*> FB driver for the ILI9340 LCD Controller //drivers/video/fbtft/fb_ili9340.c
<*> FB driver for the ST7735R LCD Controller //drivers/video/fbtft/fb_st7735r.c
...
<M> Module to for adding FBTFT devices //drivers/video/fbtft/fbtft_device.c
保存退出后,重編內(nèi)核鏡像和編譯驅(qū)動(dòng)模塊.
使用新內(nèi)核鏡像啟動(dòng)系統(tǒng)后:
加載驅(qū)動(dòng)模塊并指定參數(shù), 如型號(hào)為ili9340的屏:
modprobe fbtft_device name=adafruit22a gpios="reset:8,dc:7" busnum=0
加載成功后, 會(huì)產(chǎn)生一個(gè)/dev/fb8設(shè)備文件。
修改QT環(huán)境變量: export QT_QPA_PLATFORM=linuxfb:fb=/dev/fb8
生效后,執(zhí)行Qt程序,效果如下:
在內(nèi)核源碼目錄里:
[yyx@localhost linux-3.4.112]$ vim drivers/video/fbtft/fb
fb_bd663474.c fb_ili9486.c fb_ssd1351.c fbtft-io.c
fb_hx8340bn.c fb_pcd8544.c fb_st7735r.c fbtft-sysfs.c
fb_hx8347d.c fb_ra8875.c fbtft-bus.c fb_tinylcd.c
fb_hx8353d.c fb_s6d02a1.c fbtft-core.c fb_tls8204.c
fb_ili9320.c fb_s6d1121.c fbtft_device.c fb_upd161704.c
fb_ili9325.c fb_ssd1289.c fbtft_device.ko fb_watterott.c
fb_ili9340.c fb_ssd1306.c fbtft_device.mod.c
fb_ili9341.c fb_ssd1331.c fbtft.h
fb_xxxx.c就是具體型號(hào)的驅(qū)動(dòng)IC設(shè)備驅(qū)動(dòng):
每個(gè)設(shè)備驅(qū)動(dòng)源碼里都會(huì)調(diào)用init_display函數(shù)用于初始化屏,我們可以根據(jù)此函數(shù)里設(shè)置的寄存器和值,確認(rèn)屏驅(qū)動(dòng)IC的型號(hào):
static int init_display(struct fbtft_par *par)
{
...
par->fbtftops.reset(par);
write_reg(par, 0xEF, 0x03, 0x80, 0x02);
write_reg(par, 0xCF, 0x00 , 0XC1 , 0X30);
...
}
每個(gè)屏由一個(gè)struct fbtft_display對(duì)象來(lái)描述:
static struct fbtft_display display = {
.regwidth = 8,
.width = WIDTH,
.height = HEIGHT,
.fbtftops = {
.init_display = init_display, //屏的初始化
.set_addr_win = set_addr_win, //設(shè)置屏的顯示開始坐標(biāo)
.set_var = set_var, //設(shè)置屏的翻轉(zhuǎn)角度
},
};
FBTFT_REGISTER_DRIVER(DRVNAME, &display);
調(diào)用宏FBTFT_REGISTER_DRIVER后都會(huì)產(chǎn)生和注冊(cè)一個(gè)struct spi_driver對(duì)象和一個(gè)struct platform_driver對(duì)象.
#define FBTFT_REGISTER_DRIVER(_name, _display) \
\
static int fbtft_driver_probe_spi(struct spi_device *spi) \
{ \
return fbtft_probe_common(_display, spi, NULL); \
} \
\
static int fbtft_driver_remove_spi(struct spi_device *spi) \
{ \
struct fb_info *info = spi_get_drvdata(spi); \
\
return fbtft_remove_common(&spi->dev, info); \
} \
\
static int fbtft_driver_probe_pdev(struct platform_device *pdev) \
{ \
return fbtft_probe_common(_display, NULL, pdev); \
} \
\
static int fbtft_driver_remove_pdev(struct platform_device *pdev) \
{ \
struct fb_info *info = platform_get_drvdata(pdev); \
\
return fbtft_remove_common(&pdev->dev, info); \
}
static struct spi_driver fbtft_driver_spi_driver = { \
.driver = { \
.name = _name, \
.owner = THIS_MODULE, \
}, \
.probe = fbtft_driver_probe_spi, \
.remove = fbtft_driver_remove_spi, \
}; \
\
static struct platform_driver fbtft_driver_platform_driver = { \
.driver = { \
.name = _name, \
.owner = THIS_MODULE, \
}, \
.probe = fbtft_driver_probe_pdev, \
.remove = fbtft_driver_remove_pdev, \
};
static int __init fbtft_driver_module_init(void) \
{ \
int ret; \
\
ret = spi_register_driver(&fbtft_driver_spi_driver); \
if (ret < 0) \
return ret; \
return platform_driver_register(&fbtft_driver_platform_driver); \
} \
\
static void __exit fbtft_driver_module_exit(void) \
{ \
spi_unregister_driver(&fbtft_driver_spi_driver); \
platform_driver_unregister(&fbtft_driver_platform_driver); \
} \
\
module_init(fbtft_driver_module_init); \
module_exit(fbtft_driver_module_exit);
我們可以通過描述spi設(shè)備或者平臺(tái)設(shè)備來(lái)與屏的設(shè)備驅(qū)動(dòng)匹配,但描述設(shè)備的參數(shù)有點(diǎn)復(fù)雜,所以內(nèi)核里提供了fbtft_device.c,它把我們提供的模塊參數(shù),生成相應(yīng)的spi設(shè)備或平臺(tái)設(shè)備,并且提供相關(guān)的資源信息.
在fbtft_device.c里,它已定好多的設(shè)備名, 每個(gè)設(shè)備使用一個(gè)具體的屏型號(hào):
static struct fbtft_device_display displays[] = {
{
.name = "adafruit18", //設(shè)備名
.spi = &(struct spi_board_info) {
.modalias = "fb_st7735r", //屏的型號(hào)
.max_speed_hz = 32000000,
.mode = SPI_MODE_0,
.platform_data = &(struct fbtft_platform_data) {
.display = {
.buswidth = 8,
.backlight = 1,
},
.gpios = (const struct fbtft_gpio []) {
{ "reset", 25 },
{ "dc", 24 },
{ "led", 18 },
{},
},
.gamma = ADAFRUIT18_GAMMA,
}
}
}, {
.name = "adafruit18_green",
.spi = &(struct spi_board_info) {
.modalias = "fb_st7735r",
...
加載此驅(qū)動(dòng)模塊時(shí),除了需要指定設(shè)備名外,還需要指定多個(gè)參數(shù),具體可以通過modinfo查看:
modinfo /lib/modules/3.4.112/kernel/drivers/video/fbtft/fbtft_device.
ko
常用的模塊參數(shù)有:
gpios用于指定reset, dc, led等具體使用哪個(gè)io口
mode用于指定spi使用哪種時(shí)序
busnum用于指定使用第幾個(gè)spi控制器
rotate用于指定翻轉(zhuǎn)角度
name用于指定設(shè)備名
如:modprobe fbtft_device name=adafruit22a gpios="reset:8,dc:7" busnum=0 debug=0 rotate=90
gpios="reset:8,dc:7"表示reset引腳是接在GPIOA(8), dc引腳是接在GPIOA(7)
在頭文件里: arch/arm/mach-sunxi/include/mach/gpio.h
#define SUNXI_PA_BASE 0
#define GPIOA(n) (SUNXI_PA_BASE + (n))
所以得出GPIOA(8)的值是8
注意,不同的內(nèi)核fbtft放置的目錄可能不同,比如在周立功的A6G2C的內(nèi)核下,fbtft在如下目錄:
linux-src-a0722e0\A7-linux-src\drivers\staging\fbtft
嵌入式韋東山老師視頻購(gòu)買
(好消息,最近上新了多款新開發(fā)板,韋老師正在加緊錄制中,歡迎購(gòu)買)
嵌入式物聯(lián)網(wǎng)技術(shù)微信交流群
由于微信拉群有限制超過100人就不允許掃碼進(jìn)群了,如果有朋友想進(jìn)來(lái)的話,請(qǐng)加我私人微信:morixinguan,備注加群,我會(huì)拉你進(jìn)群,認(rèn)識(shí)更多的小伙伴,實(shí)現(xiàn)資源共享。
免責(zé)聲明:本文內(nèi)容由21ic獲得授權(quán)后發(fā)布,版權(quán)歸原作者所有,本平臺(tái)僅提供信息存儲(chǔ)服務(wù)。文章僅代表作者個(gè)人觀點(diǎn),不代表本平臺(tái)立場(chǎng),如有問題,請(qǐng)聯(lián)系我們,謝謝!