By Toradex秦海
1). 简介
在之前的一篇文章中已经介绍过如何基于嵌入式Linux部署libsoc支持, 本文就继续基于NXP IMX6平台演示基于libsoc分别对GPIO,I2C,PWM以及SPI进行开发示例。
本文所演示的ARM平台同样来自于Toradex 基于NXP iMX6 ARM处理器的Apalis iMX6 ARM嵌入式平台。
2. 准备
a). Apalis iMX6Q ARM核心版配合Apalis Evaluation Board载板,连接调试串口UART1(载板X29)到开发主机方便调试。更多关于Apalis iMX6配合Apalis Evaluation Board载板的说明请参考Datasheet和开发上手指南。
b). Apalis iMX6Q ARM核心版配合Ixora载板,连接调试串口UART1(载板X22)到开发主机方便调试。 更多关于Apalis iMX6配合Ixora载板的说明请参考Datasheet和开发上手指南。
c). Apalis iMX6Q 安装Toradex Linux BSP V2.8b5,系统中已经默认包含了本文测试所需要的libsoc相关库支持,详细安装方法请参考这里,编译libsoc相关demo所需要的SDK配置请参考简介中提到的之前文章说明,本文不再赘述。
3). GPIO 测试示例
a). 基于Apalis Evaluation Board测试,硬件连接配置如下,基于C代码的GPIO测试请参考这里,本文是基于libsoc来重新测试。
X4 GPIO05 <-> X34 SW5
X4 GPIO06 <-> X34 LED1
b). 源代码请参考如下,实现功能就是按键交替点亮和关闭LED灯。
https://github.com/simonqin09/libsoc-examples/blob/master/gpiotest/gpiointtest.c
c). Makefile 请参考如下,直接下载的libsoc官方示例里面包含的Makefile需要进行一些修改,这里也将修改的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). 测试运行结果如下:
-----------------------
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 测试示例
a). 基于Apalis Evaluation Board测试,硬件连接配置如下, 基于C代码的PWM测试请参考这里,本文是基于libsoc来重新测试。
X4 GPIO05 <-> X34 SW5
X4 PWM1 <-> X34 LED1
b). 源代码和Makefile请参考如下,实现功能就是程序运行后,PWM以50%占空比输出,通过按键让PWM输出占空比按照50%,75%,25% 交替调整。
https://github.com/simonqin09/libsoc-examples/blob/master/pwmtest/pwmtest.c
https://github.com/simonqin09/libsoc-examples/blob/master/pwmtest/Makefile
PWM2和PWM3也都可以测试,只是PWM3默认被ov5640_mipi驱动占用,需要修改device tree释放。
d). 测试运行结果如下:
-----------------------
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 测试示例
a). 基于 Ixora 载板测试,针对载板上面连接到i2c-1的总线上面的EEPROM进行读写,基于C代码的详细测试文档可以参考这里,本文重新基于libsoc代码进行测试。
b). 源代码和Makefile请参考如下,实现功能就是程序运行后,针对指定的page号码写入数据,然后再读取后验证是否读写一致。
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). 测试运行结果如下:
-----------------------
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 测试示例
a). 基于Apalis Evaluation Board测试,需要配合Toradex另外一个基于NXP iMX7的平台作为SPI slave进行共同测试,关于硬件连接和SPI slave的配制和相关代码请参考下面文章,本文是基于libsoc来测试 SPI Master 端。
https://www.toradex.com/zh-cn/blog/nxp-imx7-m4-he-xin-spi-slave-ce-shi
b). 源代码和Makefile请参考如下,实现功能就是程序运行后,通过Apalis iMX6 SPI0.0 向 SPI slave写入一组随机数据,然后再次通讯读出刚才写入的数据后确认读写是否一致。
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). 测试运行结果如下:
-----------------------
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). 总结
如上述示例,通过基于iMX6测试了GPIO,I2C,PWM和SPI接口,对比C代码,libsoc使用和配置起来更方便简洁。
作者: hai.qin_651820742, 来源:面包板社区
链接: https://mbb.eet-china.com/blog/uid-me-1864768.html
版权声明:本文为博主原创,未经本人允许,禁止转载!
curton 2019-5-19 12:27
curton 2019-5-16 21:51