6626配置I2C的方法介绍
2. 初始化配置(端口定义) /// Test pad i2c scl #define PAD_I2C0_SCL 16 /// Test pad mux i2c scl #define MUX_I2C0_SCL PINMUX_PAD16_I2C0_SCK_CFG /// Test pad i2c sda #define PAD_I2C0_SDA 9 /// Test pad mux i2c sda #define MUX_I2C0_SDA PINMUX_PAD9_I2C0_SDA_CFG /* Use AT24C02 */ /// Address of eeprom #define EEPROM_ADDR 0x50U /// Capacity of eeprom #define EEPROM_CAPACITY 256U /// Test address in eeprom #define TEST_ADDR 0x40 /// Test pad for gpio input trigger #define PAD_GPIO_TRIG 15 /********************************************************************* * LOCAL VARIABLES */ static evt_timer_t evt_timer_0; /// Buffer that stored the data to be written static uint8_t write_buf[100]; /// Buffer that stored the data to be read static uint8_t read_buf[100]; /// Buffer that stores data to be sent static uint8_t eeprom_tx_buf[EEPROM_CAPACITY]; /// Buffer that stores data to be read static uint8_t eeprom_rx_buf[EEPROM_CAPACITY]; /// I2C pin configuration static const pin_config_t pin_cfg [] = { {PAD_I2C0_SCL, {MUX_I2C0_SCL}, PMU_PIN_MODE_PU, PMU_PIN_DRIVER_CURRENT_NORMAL}, {PAD_I2C0_SDA, {MUX_I2C0_SDA}, PMU_PIN_MODE_PU, PMU_PIN_DRIVER_CURRENT_NORMAL}, }; /// Pinmux Configuration static pin_config_t pin_config[] = { {PAD_GPIO_TRIG, {PINMUX_GPIO_MODE_CFG}, PMU_PIN_MODE_PU, PMU_PIN_DRIVER_CURRENT_NORMAL}, }; /// GPIO Configuration static gpio_config_t gpio_config[] = { {OM_GPIO0, PAD_GPIO_TRIG, GPIO_DIR_INPUT, GPIO_LEVEL_HIGH, GPIO_TRIG_RISING_FAILING_EDGE}, }; static gpio_config_t gpio_config_2[] = { {OM_GPIO0, PAD_GPIO_TRIG, GPIO_DIR_INPUT, GPIO_LEVEL_HIGH, GPIO_TRIG_NONE}, }; void example_i2c(void) { drv_pin_init(pin_cfg, sizeof(pin_cfg) / sizeof(pin_cfg[0])); i2c_config_t cfg = { .mode = I2C_MODE_MASTER, // 7-bit addressing mode .speed = I2C_SPEED_400K, }; drv_i2c_init(OM_I2C0, &cfg); } 3.写I2C数据示例 // 调用后就能进入中断 void set_ppg_cod(void) { //{寄存器地址,16bit数据的高8位,16bit数据的低8位} uint8_t data1[3] = {0x00, 0x00, 0x01};// 0x00 uint8_t data2[3] = {0x04, 0x01, 0x40};// 0x04 uint8_t data3[3] = {0x0A, 0x00, 0x00};// 0x0A uint8_t data4[3] = {0x0B, 0x00, 0x41};// 0x0B uint8_t data5[3] = {0x0F, 0x00, 0x01};// 0x0F uint8_t data6[3] = {0x27, 0x38, 0x17};// 0x27 uint8_t data7[3] = {0x2D, 0x00, 0x00};// 0x2D uint8_t data8[3] = {0x2E, 0x03, 0xFF};// 0x2E uint8_t data9[3] = {0x2F, 0x10, 0x13};// 0x2F uint8_t data10[3] = {0x10, 0x00, 0x02};// 0x10 uint8_t data11[3] = {0x19, 0x43, 0x43};// 0x19 uint8_t data12[3] = {0x1A, 0x43, 0x43};// 0x1A uint8_t data13[3] = {0x1B, 0x41, 0x41};// 0x1B uint8_t data14[3] = {0x1C, 0x41, 0x41};// 0x1C uint8_t data15[3] = {0x25, 0x0F, 0xF0};// 0x25 uint8_t data16[3] = {0x02, 0x00, 0x91};// 0x02 uint8_t recv[2] = {0}; uint16_t tmp = 0; uint8_t rd_addr = 0x00;
// 设备地址0x5B,缓冲,字节数 drv_i2c_master_write(OM_I2C0, 0x5B, data1, 3); drv_i2c_master_write(OM_I2C0, 0x5B, data2, 3); drv_i2c_master_write(OM_I2C0, 0x5B, data3, 3); drv_i2c_master_write(OM_I2C0, 0x5B, data4, 3); drv_i2c_master_write(OM_I2C0, 0x5B, data5, 3); drv_i2c_master_write(OM_I2C0, 0x5B, data6, 3); drv_i2c_master_write(OM_I2C0, 0x5B, data7, 3); drv_i2c_master_write(OM_I2C0, 0x5B, data8, 3); drv_i2c_master_write(OM_I2C0, 0x5B, data9, 3); drv_i2c_master_write(OM_I2C0, 0x5B, data10, 3); drv_i2c_master_write(OM_I2C0, 0x5B, data11, 3); drv_i2c_master_write(OM_I2C0, 0x5B, data12, 3); drv_i2c_master_write(OM_I2C0, 0x5B, data13, 3); drv_i2c_master_write(OM_I2C0, 0x5B, data14, 3); drv_i2c_master_write(OM_I2C0, 0x5B, data15, 3); drv_i2c_master_write(OM_I2C0, 0x5B, data16, 3); }
4.读示例 void ZSDataHandle(void) { uint16_t tmp; uint8_t recv[2]; memset(recv, 0, sizeof(recv)); uint8_t rd_addr = 0x06; drv_i2c_master_read(OM_I2C0, 0x5B, &rd_addr, 1, recv, 2); // 读出中断寄存器标志位 tmp = (recv[0] << 8) | recv[1]; // 转为16位数据 if (tmp & 0x0001) { // TSA溢出中断 memset(recv, 0, sizeof(recv)); rd_addr = 0x51; // 读取带读取FIFO的字节个数长度//FIFO_BYTE_COUNT_TSA[10:0] drv_i2c_master_read(OM_I2C0, 0x5B, &rd_addr, 1, recv, 2); } rd_addr = 0x61; uint8_t fifo[20] = {0}; memset(fifo, 0, sizeof(fifo)); tmp = (recv[0] << 8) | recv[1]; // 转为16位数据 drv_i2c_master_read(OM_I2C0, 0x5B, &rd_addr, 1, fifo, 20); // 读取FIFO内容,一次读取20个字节 OM_LOG_DEBUG_ARRAY_EX("FIFO_BYTE_COUNT51", fifo, tmp); // 打印fifo的数据 } 5.测试波形 |