学习MindSDK下的I2C读写24c02测试。
* 硬件电路
开发板上I2C硬件电路
使用PC6和PC7端口的I2C1接口
* 程序部分
引脚配置
void BOARD_InitPins(void) { /* PB6 - UART1_TX. */ GPIO_Init_Type gpio_init; gpio_init.Pins = GPIO_PIN_6; gpio_init.PinMode = GPIO_PinMode_AF_PushPull; gpio_init.Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &gpio_init); GPIO_PinAFConf(GPIOB, gpio_init.Pins, GPIO_AF_7); /* PB7 - UART1_RX. */ gpio_init.Pins = GPIO_PIN_7; gpio_init.PinMode = GPIO_PinMode_In_Floating; gpio_init.Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &gpio_init); GPIO_PinAFConf(GPIOB, gpio_init.Pins, GPIO_AF_7); /* PC6 - I2C1_SCL. */ gpio_init.Pins = GPIO_PIN_6; gpio_init.PinMode = GPIO_PinMode_AF_OpenDrain; gpio_init.Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOC, &gpio_init); GPIO_PinAFConf(GPIOC, gpio_init.Pins, GPIO_AF_4); /* PC7 - I2C1_SDA. */ gpio_init.Pins = GPIO_PIN_7; gpio_init.PinMode = GPIO_PinMode_AF_OpenDrain; gpio_init.Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOC, &gpio_init); GPIO_PinAFConf(GPIOC, gpio_init.Pins, GPIO_AF_4); }
main.c
int main(void) { uint32_t i; uint8_t i2c_txdat[8]; uint8_t i2c_rxdat[8]; BOARD_Init(); app_i2c_init(); printf("\r\ni2c_master_basic example\r\n"); /* Initialize I2C. */ i2c_txdat[0] = 0x00; i2c_txdat[1] = 0x01; i2c_txdat[2] = 0x55; i2c_txdat[3] = 0xaa; i2c_txdat[4] = 0xff; i2c_txdat[5] = 0x5f; i2c_txdat[6] = 0xf5; i2c_txdat[7] = 0xaa; if( false == app_i2c_write(8, i2c_txdat) ) { printf("I2C write failed.\r\n"); } else { printf("write data: "); for (i = 0u; i < 8; i++) { printf("0x%02X, ", i2c_txdat[i]); } printf("\r\n"); } if ( false == app_i2c_read(7, i2c_rxdat) ) /* Received data successfully. */ { printf("I2C read failed.\r\n"); } else { printf("read data: "); for (i = 0u; i < 7; i++) { printf("0x%02X, ", i2c_rxdat[i]); } printf("\r\n"); } while(1); }
初始化和收发代码
/* Initialize I2C */ void app_i2c_init(void) { /* Setup I2C initialization values. */ I2C_Master_Init_Type i2c_init; i2c_init.ClockFreqHz = BOARD_I2C_FREQ; i2c_init.BaudRate = I2C_BaudRate_100K; /* Initialize I2C master. */ I2C_InitMaster(BOARD_I2C_PORT, &i2c_init); /* The target device address needs to be configured before enabling. */ I2C_SetTargetAddr(BOARD_I2C_PORT, APP_I2C_TARGET_ADDR); /* Enable I2C. */ I2C_Enable(BOARD_I2C_PORT, true); } /* Write data to target device, true to writing succeed, false to writing failed. */ bool app_i2c_write(uint8_t txlen, uint8_t *txbuf) { app_i2c_xfer.WaitTimes = APP_I2C_TIMEOUT_TIME; app_i2c_xfer.TxBuf = txbuf; app_i2c_xfer.TxLen = txlen; I2C_Enable(BOARD_I2C_PORT, true); /* Disable I2C to clear tx fifo, and enabled I2C to perform the write operation again. */ if ( false == I2C_MasterWriteBlocking(BOARD_I2C_PORT, &app_i2c_xfer) ) { return false; } else { return true; } } /* Read data to target device, true to reading succeed, false to reading failed. */ bool app_i2c_read(uint8_t rxlen, uint8_t *rxbuf) { app_i2c_xfer.WaitTimes = APP_I2C_TIMEOUT_TIME; app_i2c_xfer.RxBuf = rxbuf; app_i2c_xfer.RxLen = rxlen; I2C_Enable(BOARD_I2C_PORT, true); /* Disable I2C to clear tx fifo, and enabled I2C to perform the read operation again. */ if ( false == I2C_MasterReadBlocking(BOARD_I2C_PORT, &app_i2c_xfer) ) { return false; } else { return true; } }
* 运行结果
开发板上电,先向地址0x00开始写8个字节的数据,在从地址0x00读出8个字节的数据。