FPGA I2C Read/Write Demo
1 Demo overview
Functional description: this demo shows how to read and write FPGA registers from the ARM side via the I2C protocol. Based on the standard Linux I2C tools (such as i2cset, i2cget, i2cdetect), the demo provides a custom test program with a complete I2C communication test flow that verifies the correctness of I2C communication.
2. Read/write the FPGA with standard I2C tools
In I2C communication, the FPGA acts as the slave; you can view its slave address with the following command. As shown in the figure, its device address is 0x66.
i2cdetect -r -y -a 0
The code currently defines three register addresses: 0x00, 0x01, and 0x02. Among them, 0x02 is read-only. For the 0x00 register you can test whether the read/write data is correct — for example, if you write 1 and read back 1, the communication is normal.
Run the following command to write the value 0x40 to the 0x00 register.
i2cset -r -y -a 0 0x66 0x00 0x40
Run the following command to read the value of register 0x00; you can see it reads 0x40, consistent with the written value.
i2cget -f -y -a 0 0x66 0x00You can also run the following command to view the values of all registers on the device.
i2cdump -f -y -a 0 0x66
Among them, the 0x01 register controls the state of the carrier-board LED. You can write a value to the 0x01 register to control the LED state.
#点亮
i2cset -f -y -a 0 0x66 0x01 0x01
#熄灭
i2cset -f -y -a 0 0x66 0x01 0x013 Demo procedure
Based on the standard Linux I2C tools, this demo optimizes the command design to be more intuitive and user-friendly. You can modify the source program as needed.
Open a terminal and copy the executable smdt_fpga_i2c from this demo's bin directory (05-Development Materials\Software Development Materials\linux_demo\smdt_fpga_i2c_demo\bin) to the board's filesystem (the source code is available under the src path).
#修改 Linux 内核日志的显示级别,内核的日志级别被设置为只显示紧急或更高级别的消息
echo 1 4 1 7 > /proc/sys/kernel/printkIn the terminal, run the following commands to switch to the directory containing the smdt_fpga_i2c executable.
#切换到 smdt_fpga_i2c 可执行程序所在目录
cd ‘可执行文件所在目录’
#查看 smdt_fpga_i2c 是否在该目录下
lsIf the executable smdt_fpga_i2c is in the current directory, change its permissions.
#修改文件权限
chmod 777 smdt_fpga_i2c
#查询是否修改成功
ls -ld smdt_fpga_i2cAfter confirming the permission change succeeded, run ./smdt_fpga_i2c -h to view the program's help information.
./smdt_fpga_i2c -hThe result is shown below:

Run the following commands to perform read/write operations on device register 0x00.
#写操作
./smdt_fpga_i2c -write 0x00 0x11
#读操作
./smdt_fpga_i2c -read 0x00Run the following commands to write to device register 0x01 and control the carrier-board LED on/off.
#点亮
./smdt_fpga_i2c -led ON
#熄灭
./smdt_fpga_i2c -led OFF