QCC512x QCC302x 使用I2C驱动外设(Bitserial)
1、旧接口 I2cTransfer
ADK6.x 版本对 i2c的库进行了重写,在 app 层不能够直接使用 #include 里面的 API了。
uint16 I2cTransfer(uint16 address, const uint8 * tx, uint16 tx_len, uint8 * rx, uint16 rx_len);
开发底层库的话,依然可以调用 I2cTransfer ,需要在 subsys7_config3.htf 指定 I2C pios ,第一个是 SCL、第二个是 SDA。类似于用 PSTool 的设置。
2、新接口 Bitserial
在 app 层调用的话,只能用 Bitserial API,需要包含此头文件 #include
使用 Bitserial 需要将此宏打开 #if TRAPSET_BITSERIAL ,在 该头文件内定义(默认打开)
Bitserial 同时支持 I2C 与 SPI 两种通信。
Bitserial 提供的功能有以下几个,常用为前三个。
 I2C 初始化分为几步2.1、I2C 初始化
- 设置 PIO 由硬件控制;
PioSetMapPins32Bank(bank, mask, 0);
- 设置 PIO 功能;
PioSetFunction(pio,func);
- 设置 PIO 方向;
PioSetDir32Bank(bank, mask, 0);
- 设置 PIO 电平;
PioSet32Bank(bank, mask, mask);
PioSetStrongBias32Bank(bank, mask, mask);
- 设置 Bitserial 模式为 I2C;
bsconfig.mode = BITSERIAL_MODE_I2C_MASTER;
- 设置 Bitserial 速度;
bsconfig.clock_frequency_khz = prox->config->i2c_clock_khz;
- 设置 Bitserial 从地址;
bsconfig.u.i2c_cfg.i2c_address = I2C_ADDRESS;
- 调用 BitserialOpen 并返回句柄。
handle = BitserialOpen((bitserial_block_index)BITSERIAL_BLOCK_1, &bsconfig);
2.2、I2C 读写函数
可以直接调用 BitserialTransfer 来实现 I2C 的写操作。
static uint8 user_i2c_write_byte(bitserial_handle handle, uint8 reg, uint8 value)
{
uint8 ret;
uint8 txbuf[2];
txbuf[0] = reg;
txbuf[1] = value;
ret = BitserialTransfer(handle, 0, txbuf, 2, NULL, 0);
return ret;
}
同时可以实现连续写入多个寄存器操作。
static uint8 user_i2c_write_buffer(bitserial_handle handle, uint8 buffer[], uint16 buflen)
{
uint8 ret;
ret = BitserialTransfer(handle, 0, buffer, buflen, NULL, 0);
return ret;
}
2.3、I2C 读函数
可以直接调用 BitserialTransfer 来实现 I2C 的读操作。
static uint8 user_i2c_read(bitserial_handle handle, uint8 txdat,uint8 rxbuf[], uint8 rxlen)
{
uint8 ret;
uint8 txbuf[1];
txbuf[0] = txdat;
memset(rxbuf,0,rxlen);
ret = BitserialTransfer(handle, 0, txbuf, 1, rxbuf, rxlen);
return ret;
}
3、总结
对比:

I2C地址相关注意事项:
I2cTransfer:传入设备的地址为8位写地址。
Bitserial :传入设备的地址为7位地址。 来源:https://blog.csdn.net/qq_29225913/article/details/102639717 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
|