嵌入式 Arm 平台 libsoc 开发示例

Mittwoch, 15. Mai 2019

简介

在之前的一篇文章中已经介绍过如何基于嵌入式 Linux 部署 libsoc 支持, 本文就继续基于 NXP IMX6 平台演示基于 libsoc 分别对 GPIO,I2C,PWM 以及 SPI 进行开发示例。

本文所演示的 Arm 平台同样来自于 Toradex 基于 NXP iMX6 Arm 处理器的 Apalis iMX6 Arm 嵌入式平台。

准备

Apalis iMX6Q Arm 核心版配合 Apalis Evaluation Boar d载板,连接调试串口 UART1(载板X29)到开发主机方便调试。更多关于 Apalis iMX6 配合 Apalis Evaluation Board 载板的说明请参考 Datasheet开发上手指南

Apalis iMX6Q Arm 核心版配合 Ixora 载板,连接调试串口 UART1(载板X22)到开发主机方便调试。 更多关于 Apalis iMX6 配合 Ixora 载板的说明请参考 Datasheet开发上手指南

Apalis iMX6Q 安装Toradex Linux BSP V2.8b5,系统中已经默认包含了本文测试所需要的 libsoc 相关库支持,详细安装方法请参考这里,编译 libsoc 相关 demo 所需要的 SDK 配置请参考简介中提到的之前文章说明,本文不再赘述。


GPIO 测试示例

基于 Apalis Evaluation Board 测试,硬件连接配置如下,基于 C 代码的 GPIO 测试请参考这里,本文是基于libsoc来重新测试。

X4 GPIO05 <-> X34 SW5
X4 GPIO06 <-> X34 LED1

源代码请参考如下,实现功能就是按键交替点亮和关闭 LED 灯。
https://github.com/simonqin09/libsoc-examples/blob/master/gpiotest/gpiointtest.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

测试运行结果如下:

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

PWM 测试示例

基于 Apalis Evaluation Board 测试,硬件连接配置如下, 基于 C 代码的 PWM 测试请参考这里,本文是基于 libsoc 来重新测试。

X4 GPIO05 <-> X34 SW5
X4 PWM1 <-> X34 LED1

源代码和 Makefile 请参考如下,实现功能就是程序运行后,PWM以50% 占空比输出,通过按键让P WM 输出占空比按照 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 释放。

测试运行结果如下:

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%



I2C 测试示例

基于 Ixora 载板测试,针对载板上面连接到 i2c-1 的总线上面的 EEPROM 进行读写,基于 C 代码的详细测试文档可以参考这里,本文重新基于 libsoc 代码进行测试。

源代码和 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

测试运行结果如下:

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

SPI Master 测试示例

基于 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

源代码和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

测试运行结果如下:

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


总结

如上述示例,通过基于 iMX6 测试了 GPIO,I2C,PWM 和 SPI 接口,对比 C 代码,libsoc 使用和配置起来更方便简洁。

Author 秦海,技术销售工程师,韬睿(上海)

Leave a comment

Your email ID will be kept confidential. Required fields are marked *


Bitte geben Sie die angezeigten Buchstaben ein. Gross/Kleinschreibung ist unbedeutend.



* Your comment will be reviewed and then added. Thank you.