i2c demo例程中,如下:i2c收发完成后都是进行一个延时操作,等待收发完成。这样对于需要i2c持续操作的任务,就降低了收发的效率
- bflb_i2c_transfer(i2c0, msgs, 2);
- printf("write over\r\n");
- bflb_mtimer_delay_ms(100);
- /* Read page 0 */
- msgs[1].addr = 0x50;
- msgs[1].flags = I2C_M_READ;
- msgs[1].buffer = read_data;
- msgs[1].length = EEPROM_TRANSFER_LENGTH;
- bflb_i2c_transfer(i2c0, msgs, 2);
- printf("read over\r\n");
- bflb_mtimer_delay_ms(100);
复制代码 考虑是否能将 bflb_i2c_transfer 前面加一个等待 i2c 非busy 然后进行下一步的的操作呢
- int bflb_i2c_transfer(struct bflb_device_s *dev, struct bflb_i2c_msg_s *msgs, int count)
- {
- uint16_t subaddr = 0;
- uint16_t subaddr_size = 0;
- bool is_addr_10bit = false;
- int ret = 0;
- bflb_i2c_disable(dev);
- for (uint16_t i = 0; i < count; i++) {
- if (msgs[i].flags & I2C_M_TEN) {
- is_addr_10bit = true;
- } else {
- is_addr_10bit = false;
- }
- if (msgs[i].flags & I2C_M_NOSTOP) {
- subaddr = 0;
- for (uint8_t j = 0; j < msgs[i].length; j++) {
- subaddr += msgs[i].buffer[j] << (j * 8);
- }
- subaddr_size = msgs[i].length;
- bflb_i2c_addr_config(dev, msgs[i].addr, subaddr, subaddr_size, is_addr_10bit);
- i++;
- } else {
- subaddr = 0;
- subaddr_size = 0;
- bflb_i2c_addr_config(dev, msgs[i].addr, subaddr, subaddr_size, is_addr_10bit);
- }
- if (msgs[i].length > 256) {
- return -EINVAL;
- }
- bflb_i2c_set_datalen(dev, msgs[i].length);
- if (msgs[i].flags & I2C_M_READ) {
- bflb_i2c_set_dir(dev, 1);
- if ((msgs[i].flags & I2C_M_DMA) == 0) {
- ret = bflb_i2c_read_bytes(dev, msgs[i].buffer, msgs[i].length);
- if (ret < 0) {
- return ret;
- }
- } else {
- bflb_i2c_enable(dev);
- }
- } else {
- bflb_i2c_set_dir(dev, 0);
- if ((msgs[i].flags & I2C_M_DMA) == 0) {
- ret = bflb_i2c_write_bytes(dev, msgs[i].buffer, msgs[i].length);
- if (ret < 0) {
- return ret;
- }
- } else {
- bflb_i2c_enable(dev);
- }
- }
- }
- return 0;
- }
复制代码
- /* 0xC : i2c_bus_busy */
- #define I2C_STS_I2C_BUS_BUSY (1 << 0U)
- #define I2C_CR_I2C_BUS_BUSY_CLR (1 << 1U)
复制代码 纯属个人想法,不知是否可行或者会带来其他稳定性问题,各位坛友可以帮忙一起看看,谢谢!
|