I2C operation

[!Note]

  1. Currently only SV50P series modulessupport this function.
  2. Before use, you need to enable the TWI function in Module Configuration, and upgrade with the new system package generated before it can be used normally.
  3. MoreUsage Tutorialabout modules.

Introducing header files

  #include "utils/I2CHelper.h"

Specific operations

  #include "utils/I2CHelper.h"

  #define CFG_L        0x47
  #define CFG_H        0x80
  #define VER_L        0x41
  #define VER_H        0x81

  static void testI2C() {
    uint8_t tx[512], rx[512];
    memset(tx, 0, 512);
    memset(rx, 0, 512);

    /**
     * Define variables
     * 
     * Parameter 1: i2c bus number
     * Parameter 2: Slave address, be sure to pay        attention to the 7bit address
     * Parameter 3: Timeout time, unit: ms
     * Parameter 4: Number of retries: ms
     */
    I2CHelper i2c(0, 0x5e, 1000, 5);

    tx[0] = CFG_H;
    tx[1] = CFG_L;

    /**
     * Simplex writing
     *
     * Parameter 1: Write data address
      * Parameter 2: Data length
     */
    if (!i2c.write(tx, 2)) {
        LOGD("i2c tx cfg error!\n");
    }

    /**
     * Single work
     *
     * Parameter 1: Read data address
     * Parameter 2: Data length
     */
    if (!i2c.read(rx, 1)) {
        LOGD("i2c rx cfg error!\n");
    }

    LOGD("i2c reg[0x%x%x]=%x\n", CFG_H, CFG_L, rx[0]);
    memset(rx, 0, 512);

    /**
    * Half-duplex transmission, that is, shared     read and write, no stop signal in the middle
    *
    * Parameter 1: Write data address
    * Parameter 2: Write data length
    * Parameter 3: Read data address
    * Parameter 4: Read data length
     */
    if (!i2c.transfer(tx, 2, rx, 1)) {
        LOGD("i2c i2c_transfer cfg error!\n");
    }

    LOGD("i2c reg[0x%x%x]=%x\n", CFG_H, CFG_L, rx[0]);

    tx[0] = VER_H;
    tx[1] = VER_L;
    if (!i2c.write(tx, 2)) {
        LOGD("i2c tx ver error!\n");
    }

    if (!i2c.read(rx, 1)) {
        LOGD("i2c rx ver error!\n");
    }

    LOGD("i2c reg[0x%x%x]=%x\n", VER_H, VER_L, rx[0]);
    memset(rx, 0, 512);
    if (!i2c.transfer(tx, 2, rx, 1)) {
        LOGD("twi i2c_transfer ver error!\n");
    }

    LOGD("i2c reg[0x%x%x]=%x\n", VER_H, VER_L, rx[0]);
  }

For other interface operations, please refer to the header file notes.

EEPROM Read and write case

In the case of the AD24C32 memory, we focus on the device address (slave address) and the electrical characteristics of the device.

AD24C32

//Example of an I2C object, parameter 2 slave address is 7bit address, from Figure 3 and Figure 10 we can see that the device address is 1010000R/W, the lowest is read/write bit, the bottom of the read/write bit will be based on the call of the method read/write, the result of the right shift of this address is 0101 0000 or 0x50
static I2CHelper i2c(1, 0x50, 1000, 5);

read

write

Applianceproperty

/**
 * read operation
 * The I2C object has been passed the device address at construction time, and the device address is automatically concatenated
   when the read/write method is called
 * Parameter 1: Word address, that is, the start address of the read data
 * Parameter 2: container for storing read data
 * Parameter 3: length of the read data
 */
int i2c_readReg(unsigned short reg_addr, unsigned char *buf, int len)
{
    int res = 0;
    //As can be seen from Figure 14, before reading data, it is necessary to write the address, that is, to tell the chip from what position to start reading data
    unsigned char buff[2];
    buff[0] = (reg_addr >> 8);
    buff[1] = reg_addr;
    //Write a 2-byte word address
    i2c.write(buff, 2);
    usleep(2*1000);
    res = i2c.read(buf,len);
    return res;
}

/**
 * Page write operation
 * Parameter 1: Word address, that is, the start address of the read data
 * Parameter 2: Data to be written
 * Parameter 3: Length of the data to be written
 */
int i2c_writeReg(unsigned short reg_addr, unsigned char *buf, int len)
{
    int res = 0,i;
    unsigned char *buff = 0;
    buff = (unsigned char *)malloc((len+2));
    //As can be seen from Figure 12, when writing data, the data frame needs to have a word address, that is, to tell the chip from what position to start writing data
    buff[0] = (reg_addr >> 8);
    buff[1] = reg_addr;
    //As can be seen from Figure 12, the word address is immediately followed by the data content to be written
    for(i = 0; i < len; i++)
        buff[(i+2)] = buf[i];
    res = i2c.write(buff, (len+2));
    free(buff);
    return res;
}

/**
 * Test code
 * The test code writes data first and then reads it, comparing the content of the data written and read
 */
void I2C_Test_EEPROM(){
    char wbuf[32] = {0};
    memset(wbuf,0,32);
    strcpy(wbuf,"www.zkswe.com");
    LOGD("\n");
    if(i2c_writeReg(0,(unsigned char*)wbuf,strlen("www.zkswe.com"))){
        LOGD("write \"%s\" success!",wbuf);
    }
    //According to the electrical characteristics, the write cycle time is 2-5ms, and there is a write operation before reading below, so a certain time delay is required here
    usleep(5*1000);
    unsigned char rbuf[32] = {0};
    memset(rbuf,0,32);
    if(i2c_readReg(0,rbuf,32)){
        LOGD("read content:");
        for(unsigned int j= 0;j<strlen("www.zkswe.com");j++){
            LOGD("%d:%c\n",j,rbuf[j]);
        }
    }else{
        LOGD("Read FAIL");
    }
    LOGD("\n");
}

/**
 * Journal
 * According to the log, the read data is correct compared with the written data.
 * In summary, I2C operation mainly depends on the communication protocol of the slave device, and it is also necessary to pay attention to the electrical characteristics of the slave device, otherwise the communication may be abnormal.
 */
D/zkgui   (  917):
D/zkgui   (  917): write "www.zkswe.com" success!
D/zkgui   (  917): read content:
D/zkgui   (  917): 0:w
D/zkgui   (  917): 1:w
D/zkgui   (  917): 2:w
D/zkgui   (  917): 3:.
D/zkgui   (  917): 4:z
D/zkgui   (  917): 5:k
D/zkgui   (  917): 6:s
D/zkgui   (  917): 7:w
D/zkgui   (  917): 8:e
D/zkgui   (  917): 9:.
D/zkgui   (  917): 10:c
D/zkgui   (  917): 11:o
D/zkgui   (  917): 12:m
D/zkgui   (  917):
powered by Gitbooklast modified: 2023-08-03 15:40:02

results matching ""

    No results matching ""