Based on RT-Thread Software I2C Practice | Technical Collection
I. Hardware and Software Introduction
(1) I2C (Software)
I2C is a short-distance serial communication protocol widely used in embedded systems. It supports multi-master and multi-slave device mode. The host exchanges data with the slave through the clock signal SCL and the data line SDA. In the RT-Thread operating system, we can quickly set the pins of software I2C through the menu configuration tool menuconfig, and conveniently complete the invocation and development with the internally implemented driver.
(2) FRDM-MCXA153
FRDM-MCXA153 is a compact and scalable development board that enables rapid prototyping based on MCX A14 and A15 MCUs. They provide industry-standard interfaces to easily access MCU I/O, integrated open standard serial interfaces, external flash, and onboard MCU-Link debugger. This article mainly tests the I2C (software) function and SSD1306 OLED based on NXP FRDM-MCXA153 development board X RT-Thread.
Through the MCUXpresso Developer Experience, other tools are provided, such as the expansion board center for add-on boards and the application code center for software examples.
(3) SSD1306 OLED
SSD1306 is a monolithic CMOS OLED/PLED driver with an organic/polymer light-emitting controller diode dot-matrix graphic display system. SSD1306 has built-in contrast control, display RAM, and oscillator, reducing external components and power consumption. It has 256 levels of brightness control. Data/command is sent from a general-purpose microcontroller through a hardware-optional 6800/8000 series compatible parallel interface, I2C interface, or serial peripheral interface. It is suitable for many compact portable applications, such as mobile phone sub-displays, MP3 players, calculators, etc.
II. Project Creation
At present, RT-Thread Studio does not yet support the NXP FRDM-MCXA153 development board BSP. It is necessary to download the latest mainline from gitee and create the project through the scons command. After downloading the mainline source code, open the scons environment in the directory rt-thread\bsp\nxp\mcx\mcxa\frdm-mcxa153, configure the software I2C driver and SSD1306 OLED.
(1) Software I2C Configuration
On the onboard mikroBUS socket of the NXP FRDM-MCXA153 development board, find 2 pins as the SCL/SDA of the software I2C interface. Considering the need for 3.3V/GND, directly select P1_0 and P1_2. You can see this specifically from the pin diagram, the pins in the red box below.
Since it is software I2C, directly select “Use GPIO to soft simulate I2C” in the device driver of menuconfig, and directly configure it to the I2C1 bus in the settings. Previously P1_0 and P1_2 were selected as SCL/SDA. Configure according to the formula ((port*32)+pin). Specifically as shown in the figure below.
Other specific implementations of software I2C are implemented in the components of RT-Thread and will not be introduced further.
(2) SSD1306 OLED Configuration
In menuconfig, select enable SSD1306, and set the I2C bus name. The I2C address defaults to 0x3c and does not need modification. At the same time, enable the SSD1306 test demo.
After the above settings are completed, select save. Execute the following two commands respectively:
pkgs —update
scons —target=mdk5After the Keil project is generated, open the project to compile the program.
Program Size: Code=83324 RO-data=25164 RW-data=784 ZI-data=7876
Finished: 0 information, 2 warning and 0 error messages.
".\build\rtthread.axf" - 0 Error(s), 6 Warning(s).
Build Time Elapsed: 00:00:04Then download it to the NXP FRDM-MCXA153 development board.
III. Software I2C Test
After the firmware is downloaded, reset the system, and you can see that the software I2C has been successfully configured.
sram heap, begin: 0x0x200019d4, end: 0x0x20005c00
\ | /
- RT - Thread Operating System
/ | \ 5.2.0 build Jul 21 2024 15:49:40
2006 - 2024 Copyright by RT-Thread team
I/I2C: I2C bus [i2c1] registered
D/I2C_S: Software simulation i2c1 init done, SCL pin: 0x20, SDA pin: 0x22
I/I2C: I2C bus [i2c0] registered
using armclang, version: 6180002
MCXA153 HelloWorld
msh >After executing list device, you can see that I2C1 has already started.
msh >list device
device type ref count
-------- -------------------- ----------
i2c0 I2C Bus 0
i2c1 I2C Bus 0
pin Pin Device 0
uart0 Character Device 2
msh >Run the demo ssd1306_TestAll.
Compared with hardware I2C, the screen refresh speed of software I2C is indeed much slower.
