嵌入式 ARM 平臺(tái) libsoc 開發(fā)示例
By Toradex秦海
1). 簡(jiǎn)介
在之前的一篇文章中已經(jīng)介紹過如何基于嵌入式Linux部署libsoc支持, 本文就繼續(xù)基于NXP IMX6平臺(tái)演示基于libsoc分別對(duì)GPIO,I2C,PWM以及SPI進(jìn)行開發(fā)示例。
本文所演示的ARM平臺(tái)同樣來自于Toradex 基于NXP iMX6 ARM處理器的Apalis iMX6 ARM嵌入式平臺(tái)。
2. 準(zhǔn)備
a). Apalis iMX6Q ARM核心版配合Apalis Evaluation Board載板,連接調(diào)試串口UART1(載板X29)到開發(fā)主機(jī)方便調(diào)試。更多關(guān)于Apalis iMX6配合Apalis Evaluation Board載板的說明請(qǐng)參考Datasheet和開發(fā)上手指南。
b). Apalis iMX6Q ARM核心版配合Ixora載板,連接調(diào)試串口UART1(載板X22)到開發(fā)主機(jī)方便調(diào)試。 更多關(guān)于Apalis iMX6配合Ixora載板的說明請(qǐng)參考Datasheet和開發(fā)上手指南。
c). Apalis iMX6Q 安裝Toradex Linux BSP V2.8b5,系統(tǒng)中已經(jīng)默認(rèn)包含了本文測(cè)試所需要的libsoc相關(guān)庫支持,詳細(xì)安裝方法請(qǐng)參考這里,編譯libsoc相關(guān)demo所需要的SDK配置請(qǐng)參考簡(jiǎn)介中提到的之前文章說明,本文不再贅述。
3). GPIO 測(cè)試示例
a). 基于Apalis Evaluation Board測(cè)試,硬件連接配置如下,基于C代碼的GPIO測(cè)試請(qǐng)參考這里,本文是基于libsoc來重新測(cè)試。
X4 GPIO05 <-> X34 SW5
X4 GPIO06 <-> X34 LED1
b). 源代碼請(qǐng)參考如下,實(shí)現(xiàn)功能就是按鍵交替點(diǎn)亮和關(guān)閉LED燈。
https://github.com/simonqin09/libsoc-examples/blob/master/gpiotest/gpiointtest.c
c). Makefile 請(qǐng)參考如下,直接下載的libsoc官方示例里面包含的Makefile需要進(jìn)行一些修改,這里也將修改的patch附上。
./ Makefile
https://github.com/simonqin09/libsoc-examples/blob/master/gpiotest/Makefile
./ Patch file
https://github.com/simonqin09/libsoc-examples/blob/master/gpiotest/libsoc-examples_Makefile.patch
d). 測(cè)試運(yùn)行結(jié)果如下:
-----------------------
root@apalis-imx6:~# ./gpiointtest
Waiting for interrupt. Press 'q' and 'Enter' at any time to exit
LED turns on
Interrupt occurred 1 times
LED turns off
Interrupt occurred 2 times
…
-----------------------
4). PWM 測(cè)試示例
a). 基于Apalis Evaluation Board測(cè)試,硬件連接配置如下, 基于C代碼的PWM測(cè)試請(qǐng)參考這里,本文是基于libsoc來重新測(cè)試。
X4 GPIO05 <-> X34 SW5
X4 PWM1 <-> X34 LED1
b). 源代碼和Makefile請(qǐng)參考如下,實(shí)現(xiàn)功能就是程序運(yùn)行后,PWM以50%占空比輸出,通過按鍵讓PWM輸出占空比按照50%,75%,25% 交替調(diào)整。
https://github.com/simonqin09/libsoc-examples/blob/master/pwmtest/pwmtest.c
https://github.com/simonqin09/libsoc-examples/blob/master/pwmtest/Makefile
PWM2和PWM3也都可以測(cè)試,只是PWM3默認(rèn)被ov5640_mipi驅(qū)動(dòng)占用,需要修改device tree釋放。
d). 測(cè)試運(yùn)行結(jié)果如下:
-----------------------
root@apalis-imx6:~# ./pwmtest
Waiting for interrupt. Press 'q' and 'Enter' at any time to exit
Interrupt occurred 1 times
PWM Duty set to 75%
Interrupt occurred 2 times
PWM Duty set to 25%
Interrupt occurred 3 times
PWM Duty set to 50%
Interrupt occurred 4 times
PWM Duty set to 75%
…
-----------------------
5). I2C 測(cè)試示例
a). 基于 Ixora 載板測(cè)試,針對(duì)載板上面連接到i2c-1的總線上面的EEPROM進(jìn)行讀寫,基于C代碼的詳細(xì)測(cè)試文檔可以參考這里,本文重新基于libsoc代碼進(jìn)行測(cè)試。
b). 源代碼和Makefile請(qǐng)參考如下,實(shí)現(xiàn)功能就是程序運(yùn)行后,針對(duì)指定的page號(hào)碼寫入數(shù)據(jù),然后再讀取后驗(yàn)證是否讀寫一致。
I2ctest.c - https://github.com/simonqin09/libsoc-examples/blob/master/i2ctest/i2ctest.c
eeprom.c - https://github.com/simonqin09/libsoc-examples/blob/master/i2ctest/eeprom.c
eeprom.h - https://github.com/simonqin09/libsoc-examples/blob/master/i2ctest/eeprom.h
Makefile - https://github.com/simonqin09/libsoc-examples/blob/master/i2ctest/Makefile
d). 測(cè)試運(yùn)行結(jié)果如下:
-----------------------
root@apalis-imx6:~# ./i2ctest 15
Writing to page starting at byte address: 0xf0
Waiting for data to be written
libsoc-i2c-debug: No such device or address
Waiting...
libsoc-i2c-debug: No such device or address
Waiting...
libsoc-i2c-debug: No such device or address
Waiting...
libsoc-i2c-debug: No such device or address
Waiting...
Data sucessfully written!
Reading page starting at byte address: 0xf0
data_write[0] = 0xf0 : data_read[0] = 0xf0 : Correct
data_write[1] = 0xf1 : data_read[1] = 0xf1 : Correct
data_write[2] = 0xf2 : data_read[2] = 0xf2 : Correct
data_write[3] = 0xf3 : data_read[3] = 0xf3 : Correct
data_write[4] = 0xf4 : data_read[4] = 0xf4 : Correct
data_write[5] = 0xf5 : data_read[5] = 0xf5 : Correct
data_write[6] = 0xf6 : data_read[6] = 0xf6 : Correct
data_write[7] = 0xf7 : data_read[7] = 0xf7 : Correct
data_write[8] = 0xf8 : data_read[8] = 0xf8 : Correct
data_write[9] = 0xf9 : data_read[9] = 0xf9 : Correct
data_write[10] = 0xfa : data_read[10] = 0xfa : Correct
data_write[11] = 0xfb : data_read[11] = 0xfb : Correct
data_write[12] = 0xfc : data_read[12] = 0xfc : Correct
data_write[13] = 0xfd : data_read[13] = 0xfd : Correct
data_write[14] = 0xfe : data_read[14] = 0xfe : Correct
data_write[15] = 0xff : data_read[15] = 0xff : Correct
-----------------------
6). SPI Master 測(cè)試示例
a). 基于Apalis Evaluation Board測(cè)試,需要配合Toradex另外一個(gè)基于NXP iMX7的平臺(tái)作為SPI slave進(jìn)行共同測(cè)試,關(guān)于硬件連接和SPI slave的配制和相關(guān)代碼請(qǐng)參考下面文章,本文是基于libsoc來測(cè)試 SPI Master 端。
https://www.toradex.com/zh-cn/blog/nxp-imx7-m4-he-xin-spi-slave-ce-shi
b). 源代碼和Makefile請(qǐng)參考如下,實(shí)現(xiàn)功能就是程序運(yùn)行后,通過Apalis iMX6 SPI0.0 向 SPI slave寫入一組隨機(jī)數(shù)據(jù),然后再次通訊讀出剛才寫入的數(shù)據(jù)后確認(rèn)讀寫是否一致。
spitest.c - https://github.com/simonqin09/libsoc-examples/blob/master/spitest/spitest.c
Makefile - https://github.com/simonqin09/libsoc-examples/blob/master/spitest/Makefile
d). 測(cè)試運(yùn)行結(jié)果如下:
-----------------------
root@apalis-imx6:~# ./spitest
Start to transfer!
data_send[0] = 0xca : data_read[0] = 0xca : Correct
data_send[1] = 0x03 : data_read[1] = 0x03 : Correct
data_send[2] = 0x01 : data_read[2] = 0x01 : Correct
data_send[3] = 0x56 : data_read[3] = 0x56 : Correct
data_send[4] = 0xb2 : data_read[4] = 0xb2 : Correct
data_send[5] = 0x64 : data_read[5] = 0x64 : Correct
data_send[6] = 0xf9 : data_read[6] = 0xf9 : Correct
data_send[7] = 0x34 : data_read[7] = 0x34 : Correct
data_send[8] = 0x91 : data_read[8] = 0x91 : Correct
data_send[9] = 0x84 : data_read[9] = 0x84 : Correct
data_send[10] = 0x19 : data_read[10] = 0x19 : Correct
data_send[11] = 0x0c : data_read[11] = 0x0c : Correct
data_send[12] = 0xb1 : data_read[12] = 0xb1 : Correct
data_send[13] = 0xd3 : data_read[13] = 0xd3 : Correct
data_send[14] = 0x1f : data_read[14] = 0x1f : Correct
data_send[15] = 0xf4 : data_read[15] = 0xf4 : Correct
-----------------------
7). 總結(jié)
如上述示例,通過基于iMX6測(cè)試了GPIO,I2C,PWM和SPI接口,對(duì)比C代碼,libsoc使用和配置起來更方便簡(jiǎn)潔。
*博客內(nèi)容為網(wǎng)友個(gè)人發(fā)布,僅代表博主個(gè)人觀點(diǎn),如有侵權(quán)請(qǐng)聯(lián)系工作人員刪除。
電路圖符號(hào)相關(guān)文章:電路圖符號(hào)大全