3. Introduction to I2C of M601 Module

1 Use of I2C


1.1 Overview


The module supports a hardware I2C interface. You can only use PINNAME_I2C_SCL as SCL, and
PINNAME_I2C_SDA as SDA. If you want to use other pins, you can only use the analog I2C interface.
Developers can add them in the APP or contact our company to provide them.
The data structure and API introduced in this article can refer to the Zyf_I2C.h file in the SDK.


1.2 Usage


The steps for using the I2C function are as follows:
Step 1: Initialize the I2C interface. Call the ZYF_I2clnit function to initialize the I2C channel, including special GPIO pins and channel numbers.
Step 2: Configure the I2C interface. Call ZYF_I2cConfig function to configure the parameters needed by the slave. For more information, please refer to the API description.
Step 3: Read data. Call the ZYF_I2cReadBytes function to read data from a specific slave.
Step 4: Send data. Call the ZYF_I2cWriteBytes function to send data to a specific slave.

1.3 API functions


1.3.1 ZYF_I2cInit I2C initialization
function prototype
int32_t ZYF_I2cInit (uint32_t chnnlNo, Enum_PinName pinSCL , Enum_PinName pinSDA, bool I2Ctype)
parameters
chnnlNo:
[Input] I2C channels
pinSCL:
[Input] I2C the SCL pin
pinSDA:
[Input] I2C the SDA pin
I2Ctype :
[Input] I2C type, 0: analog; 1: hardware;
return result
ZYF_RET_OK, this function is successful.
ZYFIRETERR_NOSUPPORTPIN, the input pin is invalid.
ZYF_RET_ERR_NOSUPPORTMODE, the input pin does not have I2C mode
ZYF_RET_ERR_PINALREADYSUBCRIBE, this pin has been used in other locations. For example, this pin has been used as EINT or gpio.

1.3.2 ZYF_I2cConfig Configure I2C

Function prototype
int32_t ZYF_I2cConfig(uint32_t chnnlNo,uint32_t chnnlNo,uint8_t slaveAddr, uint32_t speed)

Parameter
chnnlNo:
[input] I2C channel
slaveAddr:
[input] slave address.
speed:
[input] speed, 100Kbps~3.5Mbps
returns the result
ZYF_RET_OK, this function is successful.
ZYFIRETERR_I2C_SAME_SLAVE_ADDRESS, the slave address you want to set has been set, or the address has been used.
ZYF__RET_ERR_NORIGHTOPERATE, PIN is not in I2C mode or not initialized.

1.3.3 ZYF_I2cReadBytes Get and read I2C data
function prototype
int32_t ZYF_I2cReadBytes(uint32_t chnnlNo,uint32_t chnnlNo,uint8_t
RegAddr, uint8_t *pBuffer, uint16_t len)
parameter
chnnlNo: [input
register ] I2C input address: [input register
] The data read from the specified register
len:[input] Read data length. 1 <= len <= 8. The I2C controller supports up to 8 bytes per transmission.
Return result
If there is no error, it returns the length of the read data.
ZYF_RET_ERR_PARAM, parameter error.
ZYF__RET_ERR_I2CHWFAILED, maybe there is a hardware problem.
ZYF_RET_ERR_NORIGHTOPERATE, PIN is not in I2C mode or not initialized.
ZYF_RET_ERR I2C_SLAVE NOT_FOUND, the specified slave cannot be found.

1.3.4 ZYF_I2cWriteBytes Write and write I2C data

Function prototype
int32_t ZYF_I2cWriteBytes(uint32_t chnnlNo,uint32_t
chnnlNo,uint8_t RegAddr, uint8_t *pData, uint16_t len)
Parameters
chnnlNo:[input] I2C channel
RegAddr:[input] data input of register
pData:[
len ] write register ] Data length. 1 <= len <= 8. The I2C controller supports up to 8 bytes per transmission. The
return result
is the length of the written data if there is no error return. ZYF_RET_ERR_PARAM, parameter error. ZYF_RET_ERR_I2CHWFAILED, maybe there is a hardware problem. ZYF_RET_ERR_NORIGHTOPERATE, PIN is not in I2C mode or not initialized. ZYF_RET_ERR_I2C_SLAVE_NOT_FOUND, the specified slave was not found.

1.3.5 ZYF_I2cUninit release I2C
function prototype
int32_t ZYF_I2cUninit(uint32_t chnnlNo)
parameter
chnnlNo: [input] I2C channel
return result
ZYF_RET_OK success
ZYF_RET_ERR_PINALREADYSUBCRIBE The pin has been used in other locations. For example, this pin has been used as EINT or gpio.

2 Introduction to I2C routines


This chapter mainly introduces how to use example_i2c.c in the SDK to test the I2C function separately.
Compiling method: .\examples\build\ corresponds to the .bat file by double-clicking to execute or opening it to compile.
Generated file: .\out\corresponding directory\hex\M601_example_**.pac

  Use ZYF_I2cInit() to initialize I2C channels and pins. Use ZYF_I2cConfig() to configure rate-related parameters. The test is as follows: Use AT24xx_WriteTest() to write data to the specified address, then use AT24xx_ReadTest() to read the specified address, and finally release the I2C through ZYF_I2cUninit().


#include <stdint.h>

#include "zyf_trace.h"
#include "zyf_iic.h"
#include "zyf_gpio.h"
#include "zyf_app.h"
#include "zyf_uart.h"
#include "zyf_thread.h"
#include "OLED_I2C.h"


static Uart_Param_t g_uart1param;

void UartWriteCallBack(void* Param) // general com
{
    Uart_Param_t *uartparam = (Uart_Param_t *)Param; 
    if(Param == NULL)
    {
        return;
    }    

    ZYF_UartWrite(uartparam->port,(uint8_t *)"UartWrite succeed\r\n",strlen("UartWrite succeed\r\n"));
    ZYF_UartWriteCallbackSwitch(uartparam->port,false);

}

void UartReadCallBack(void* Param) // 
{
    uint32_t recvlen = 0;
    Uart_Param_t *uartparam = (Uart_Param_t *)Param; 

    ZYF_LOG("Uart%d recv",uartparam->port);

    while(ZYF_UartRead(uartparam->port, &(uartparam->uartbuf[recvlen]), 1))
    {
        ZYF_LOG("recv :%02x",uartparam->uartbuf[recvlen]);
        recvlen++;
    }
    ZYF_UartWrite(uartparam->port,uartparam->uartbuf,recvlen);
    ZYF_UartWriteCallbackSwitch(uartparam->port,true);
}


static void AppUartInit(void)
{
    int32_t ret;
    g_uart1param.port = DEBUG_PORT;
    ZYF_UartRegister(g_uart1param.port, UartReadCallBack,&g_uart1param);
    ZYF_UartWriteCbRegister(g_uart1param.port,UartWriteCallBack,&g_uart1param);
    ZYF_UartOpen(g_uart1param.port, 115200, ZYF_FC_NONE);

    ZYF_LOG("AppUartInit");
    return;
}

/*
void AT24xx_WriteTest(void)
{
    uint8_t data[8] = {0x3A, 0x4B, 0x1C, 0xD6, 0x7F, 0x5E, 0xD9, 0xA4};
    uint16_t len;

    ZYF_LOG("I2C write test ...");
    len = ZYF_I2cWriteBytes(ZYF_I2C_1, 0, data, 8);
    if (8 == len) {
        ZYF_LOG("I2C write OK !");
    }
}

void AT24xx_ReadTest(void)
{
    uint8_t data[8] = {0};
    uint16_t len;
    
    ZYF_LOG("I2C read test ...");
    len = ZYF_I2cReadBytes(ZYF_I2C_1, 0, data, 8);
    if (8 == len) {
        ZYF_LOG("I2C read OK ! => %02X,%02X,%02X,%02X,%02X,%02X,%02X,%02X", 
                data[0],data[1],data[2],data[3],data[4],data[5],data[6],data[7]);
    }
}

void ZYF_AppI2cTest(void *param)
{
    ZYF_I2cInit(ZYF_I2C_1, PINNAME_I2C_SCL, PINNAME_I2C_SDA, ZYF_I2C_TYPE_HW);
    ZYF_I2cConfig(ZYF_I2C_1, 0xA0 >> 1, ZYF_I2C_BPS_100K);
    AT24xx_WriteTest();
    ZYF_ThreadSleep(500);
    AT24xx_ReadTest();
    ZYF_ThreadSleep(500);
    ZYF_I2cUninit(ZYF_I2C_1);
}
*/
void IICThread_Example(void * Param)
{
    ZYF_MsgQ_t *ptMsg;
    ZYF_AppMsg_t tMsg;
    int iRet = -1;
    ptMsg = ZYF_MsgQCreate(10, sizeof(ZYF_AppMsg_t));
    ZYF_LOG("thread enter!");

//    ZYF_AppI2cinit();
    OLED_Init();
    
    OLED_Test();


}

static void prvInvokeGlobalCtors(void)
{
    extern void (*__init_array_start[])();
    extern void (*__init_array_end[])();

    size_t count = __init_array_end - __init_array_start;
    for (size_t i = 0; i < count; ++i)
        __init_array_start[i]();
}

int appimg_enter(void *param)
{
    AppUartInit();
    ZYF_LOG("application image enter, param 0x%x", param);

    prvInvokeGlobalCtors();

    ZYF_ThreadCreate("IICThread_Example", IICThread_Example, NULL, ZYF_PRIORITY_NORMAL, 4*1024);
    return 0;
}

void appimg_exit(void)
{
    OSI_LOGI(0, "application image exit");
}


 

Guess you like

Origin blog.csdn.net/w_hizyf_m/article/details/107082992