1.Hardware interface description:
The J2302 socket on the motherboard corresponds to the I2C3 of the CPU, and the socket wiring sequence is explained as follows:
Serial Number | Description | Remarks |
---|---|---|
1 | VCC | Output voltage 3.3V |
2 | I2C3_ SCL | This PIN requires an external 4.7K resistor to be pulled up to VCC |
3 | I2C3_ SDA | This PIN requires an external 4.7K resistor to be pulled up to the VCC |
4 | GND | Power reference ground |
2、 Download the source code of the testing program:
Download the source code of Linux i2c testing software:
linux-i2c-test-src-File
Source code directory structure description:
File | Description |
---|---|
I2c. h | i2c low-level operation encapsulation |
I2c. c | i2c low-level operation encapsulation |
I2cCtrl. h | i2c application interface header file |
I2cCtrl. cpp | i2c application interface source file |
Test_ I2c. cpp | Test code main |
Makefile | Make Compilation Script File |
Before performing the compilation operation, it is necessary to first install the basic compilation tools. Please refer to the instructions in "Linux Application Development (Calling Hardware)" for the installation method
Compile and run the test program as follows:
#Compilation
jb@X1:~/linx-i2c$ make
#Execution
jb@X1:~/linx-i2c$ sudo ./test_i2c
If the execution fails, please check if the linked i2c device name is correct
I2C device name, which can be viewed from $ls/dev/i2c *
Here,/dev/i2c-4 is used
The sample program reads 8 bytes of data from the 0X50 address device and the 0X00 register address (requires a real device link)
If there are no errors in the above process, the result of executing the program is as follows;
$ sudo ./test_i2c
打开i2c 总线成功,读取寄存器
读取内容:00 01 02 03 04 05 06 07
bye
bye
Interface file I2cCtrl. h description
#ifndef _I2C_CTRL_H
#define _I2C_CTRL_H
#include <stdint.h>
#include "i2c.h"
class I2cCtrl
{
i2c_t *i2c;
int8_t slave_addr;
public:
I2cCtrl();
~I2cCtrl();
public:
/**
* @ brief: Turn on the I2C bus,/dev/i2c-x
* @param {char} *path:
* @return {*}
**/
int32_t Open(const char *path);
/**
* @brief : Release the bus
* @return {*}
**/
void Release();
/**
* @brief : s the bus on
* @return {*} true opened, false not opened
**/
bool isOpen();
/**
*@ brief: Read data from register
*@ param {uint8_t} devAddr: Slave 7-bit address
*@ param {uint16_t} regAddr: 16-bit register address
*@ param {uint8_t} * pData: Read data memory
*@ param {int32_t} len: Data length
*@ return {*} returns 0 as success, others as errors
**/
int32_t I2C_Read(uint8_t devAddr, uint16_t regAddr, uint8_t *pData, int32_t len);
/**
*@ brief:
*@ param {uint8_t} devAddr: Slave 7-bit address
*@ param {uint16_t} regAddr: 16-bit register address
*@ param {uint8_t} * pData: Memory for data to be written
*@ param {int32_t} len: Data length
*@ return {*}
**/
int32_t I2C_Write(uint8_t devAddr, uint16_t regAddr, uint8_t *pData, int32_t len);
/**
*@ brief:
*@ param {uint8_t} devAddr: Slave 7-bit address
*@ param {uint8_t} regAddr: 8-bit register address
*@ param {uint8_t} * pData: Read data memory
*@ param {int32_t} len: Data length
*@ return {*}
**/
int32_t I2C_Read_RegAddr8bit(uint8_t devAddr, uint8_t regAddr, uint8_t *pData, int32_t len);
/**
*@ brief:
*@ param {uint8_t} devAddr: Slave 7-bit address
*@ param {uint8_t} regAddr: 8-bit register address
*@ param {uint8_t} * pData: Memory for data to be written
*@ param {int32_t} len: Data length
*@ return {*}
**/
int32_t I2C_Write_RegAddr8bit(uint8_t devAddr, uint8_t regAddr, uint8_t *pData, int32_t len);
/**
*@ brief:
*@ param {uint8_t} devAddr: Slave 7-bit address
*@ param {uint8_t} * pData: Memory for data to be written
*@ param {int32_t} len: Data length
*@ return {*}
**/
int32_t I2C_Write_only(uint8_t devAddr,uint8_t *pData, int32_t len);
};
#endif
3. I2C test program Qt interface: