21ic问答首页 - GD32F的IIC模块读取是,读的字节数为2,将I2C_CTL0的POAP置为1?
GD32F的IIC模块读取是,读的字节数为2,将I2C_CTL0的POAP置为1?
威风ets2021-01-08
GD32F103的IIC模块在进行eeprom读取的时候,当读取的字节数为2时,需要将I2C_CTL0的POAP置为1;POAP的定义如下:
0:ACKEN位决定对当前正在接收的字节发送ACK;
1: ACKEN位决定是否对下一个字节发送ACK.
我的理解时:当读取的字节数为2时,该位置1,表示接收第一个字节后,发送ACK;接收第二个字节后,不发送ACK。这样就能保证时序的正确性。
不知道各位大佬是如何理解的,请赐教。谢谢!
代码如下:
void eeprom_buffer_read(uint8_t* p_buffer, uint8_t read_address, uint16_t number_of_byte)
{
/* wait until I2C bus is idle */
while(i2c_flag_get(I2C0, I2C_FLAG_I2CBSY));
if(2 == number_of_byte)
{
i2c_ackpos_config(I2C0,I2C_ACKPOS_NEXT); //如果接收的的字节数等于2,则将将I2C_CTL0的POAP置为1。
}
/* send a start condition to I2C bus */
i2c_start_on_bus(I2C0);
/* wait until SBSEND bit is set */
while(!i2c_flag_get(I2C0, I2C_FLAG_SBSEND));
/* send slave address to I2C bus */
i2c_master_addressing(I2C0, eeprom_address, I2C_TRANSMITTER);
/* wait until ADDSEND bit is set */
while(!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND));
/* clear the ADDSEND bit */
i2c_flag_clear(I2C0,I2C_FLAG_ADDSEND);
/* wait until the transmit data buffer is empty */
while(SET != i2c_flag_get( I2C0 , I2C_FLAG_TBE));
/* enable I2C0*/
i2c_enable(I2C0);
/* send the EEPROM's internal address to write to */
i2c_data_transmit(I2C0, read_address);
/* wait until BTC bit is set */
while(!i2c_flag_get(I2C0, I2C_FLAG_BTC));
/* send a start condition to I2C bus */
i2c_start_on_bus(I2C0);
/* wait until SBSEND bit is set */
while(!i2c_flag_get(I2C0, I2C_FLAG_SBSEND));
/* send slave address to I2C bus */
i2c_master_addressing(I2C0, eeprom_address, I2C_RECEIVER);
if(number_of_byte < 3){
/* disable acknowledge */
i2c_ack_config(I2C0,I2C_ACK_DISABLE);
}
/* wait until ADDSEND bit is set */
while(!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND));
/* clear the ADDSEND bit */
i2c_flag_clear(I2C0,I2C_FLAG_ADDSEND);
if(1 == number_of_byte){
/* send a stop condition to I2C bus */
i2c_stop_on_bus(I2C0);
}
/* while there is data to be read */
while(number_of_byte){
if(3 == number_of_byte){
/* wait until BTC bit is set */
while(!i2c_flag_get(I2C0, I2C_FLAG_BTC));
/* disable acknowledge */
i2c_ack_config(I2C0,I2C_ACK_DISABLE);
}
if(2 == number_of_byte){
/* wait until BTC bit is set */
while(!i2c_flag_get(I2C0, I2C_FLAG_BTC));
/* send a stop condition to I2C bus */
i2c_stop_on_bus(I2C0);
}
/* wait until the RBNE bit is set and clear it */
if(i2c_flag_get(I2C0, I2C_FLAG_RBNE)){
/* read a byte from the EEPROM */
*p_buffer = i2c_data_receive(I2C0);
/* point to the next location where the byte read will be saved */
p_buffer++;
/* decrement the read bytes counter */
number_of_byte--;
}
}
/* wait until the stop condition is finished */
while(I2C_CTL0(I2C0)&0x0200);
/* enable acknowledge */
i2c_ack_config(I2C0,I2C_ACK_ENABLE);
i2c_ackpos_config(I2C0,I2C_ACKPOS_CURRENT);
}
0:ACKEN位决定对当前正在接收的字节发送ACK;
1: ACKEN位决定是否对下一个字节发送ACK.
我的理解时:当读取的字节数为2时,该位置1,表示接收第一个字节后,发送ACK;接收第二个字节后,不发送ACK。这样就能保证时序的正确性。
不知道各位大佬是如何理解的,请赐教。谢谢!
代码如下:
void eeprom_buffer_read(uint8_t* p_buffer, uint8_t read_address, uint16_t number_of_byte)
{
/* wait until I2C bus is idle */
while(i2c_flag_get(I2C0, I2C_FLAG_I2CBSY));
if(2 == number_of_byte)
{
i2c_ackpos_config(I2C0,I2C_ACKPOS_NEXT); //如果接收的的字节数等于2,则将将I2C_CTL0的POAP置为1。
}
/* send a start condition to I2C bus */
i2c_start_on_bus(I2C0);
/* wait until SBSEND bit is set */
while(!i2c_flag_get(I2C0, I2C_FLAG_SBSEND));
/* send slave address to I2C bus */
i2c_master_addressing(I2C0, eeprom_address, I2C_TRANSMITTER);
/* wait until ADDSEND bit is set */
while(!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND));
/* clear the ADDSEND bit */
i2c_flag_clear(I2C0,I2C_FLAG_ADDSEND);
/* wait until the transmit data buffer is empty */
while(SET != i2c_flag_get( I2C0 , I2C_FLAG_TBE));
/* enable I2C0*/
i2c_enable(I2C0);
/* send the EEPROM's internal address to write to */
i2c_data_transmit(I2C0, read_address);
/* wait until BTC bit is set */
while(!i2c_flag_get(I2C0, I2C_FLAG_BTC));
/* send a start condition to I2C bus */
i2c_start_on_bus(I2C0);
/* wait until SBSEND bit is set */
while(!i2c_flag_get(I2C0, I2C_FLAG_SBSEND));
/* send slave address to I2C bus */
i2c_master_addressing(I2C0, eeprom_address, I2C_RECEIVER);
if(number_of_byte < 3){
/* disable acknowledge */
i2c_ack_config(I2C0,I2C_ACK_DISABLE);
}
/* wait until ADDSEND bit is set */
while(!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND));
/* clear the ADDSEND bit */
i2c_flag_clear(I2C0,I2C_FLAG_ADDSEND);
if(1 == number_of_byte){
/* send a stop condition to I2C bus */
i2c_stop_on_bus(I2C0);
}
/* while there is data to be read */
while(number_of_byte){
if(3 == number_of_byte){
/* wait until BTC bit is set */
while(!i2c_flag_get(I2C0, I2C_FLAG_BTC));
/* disable acknowledge */
i2c_ack_config(I2C0,I2C_ACK_DISABLE);
}
if(2 == number_of_byte){
/* wait until BTC bit is set */
while(!i2c_flag_get(I2C0, I2C_FLAG_BTC));
/* send a stop condition to I2C bus */
i2c_stop_on_bus(I2C0);
}
/* wait until the RBNE bit is set and clear it */
if(i2c_flag_get(I2C0, I2C_FLAG_RBNE)){
/* read a byte from the EEPROM */
*p_buffer = i2c_data_receive(I2C0);
/* point to the next location where the byte read will be saved */
p_buffer++;
/* decrement the read bytes counter */
number_of_byte--;
}
}
/* wait until the stop condition is finished */
while(I2C_CTL0(I2C0)&0x0200);
/* enable acknowledge */
i2c_ack_config(I2C0,I2C_ACK_ENABLE);
i2c_ackpos_config(I2C0,I2C_ACKPOS_CURRENT);
}
赞0
0:ACKEN位决定对当前正在接收的字节发送ACK;
1: ACKEN位决定是否对下一个字节发送ACK.
查看源码,在配置POPA位后
if(number_of_byte < 3){
/* disable acknowledge */
i2c_ack_config(I2C0,I2C_ACK_DISABLE);
}
其中i2c_ack_config()的定义如下:
void i2c_ack_config(uint32_t i2c_periph, uint32_t ack)
{
if(I2C_ACK_ENABLE == ack){
I2C_CTL0(i2c_periph) |= I2C_CTL0_ACKEN;
}else{
I2C_CTL0(i2c_periph) &= ~(I2C_CTL0_ACKEN);
}
}
是用来配置ACKEN位的。
结合两段代码,此时ACKEN位配置的结果是下一个字节不发送ACK。
总结:
单单配置POPA为是没办法影响到ACK的发送的。ACKEN位才能影响ACK发送与否。POPA决定ACKEN是作用在当前字节还是下一字节。
评论
2022-05-15
@JinxinWang :https://racede.me/talk_about_stm32_i2c_peripheral.html 这个网页的资料。可能是DR和DSR存了两位的数据。例程考虑的还是很详细的。虽然还是不太懂这个破自行车是怎么开起来的。
@JinxinWang :https://racede.me/talk_about_stm32_i2c_peripheral.html#:~:text=%E6%88%91%E4%BB%AC%E5%8F%AF%E4%BB%A5%E7%9C%8B%E8%A7%81STM32%E7%9A%84%E7%A1%AC%E4%BB%B6I2C%E6%9C%89%E4%B8%A4%E4%B8%AA%E5%92%8C%E6%95%B0%E6%8D%AE%E6%9C%89%E5%85%B3%E7%9A%84%E5%AF%84%E5%AD%98%E5%99%A8%E2%80%9C%E6%95%B0%E6%8D%AE%E5%AF%84%E5%AD%98%E5%99%A8%20%28Data%20register%29%E2%80%9D%20%28DR%29%E5%92%8C%E2%80%9C%E6%95%B0%E6%8D%AE%E7%A7%BB%E4%BD%8D%E5%AF%84%E5%AD%98%E5%99%A8,%28Data%20shift%20register%29%E2%80%9D%20%...
比较困扰我的是: if(3 == number_of_byte){ /* wait until BTC bit is set */ while(!i2c_flag_get(I2C0, I2C_FLAG_BTC)); /* disable acknowledge */ i2c_ack_config(I2C0,I2C_ACK_DISABLE); } 这段代码实在看不懂,注释掉后会出现卡死在 /* wait until the stop condition is finished */ while(I2C_CTL0(I2C0)&0x0200); 希望有缘人能解答。
赞0
pdf.gif (1.91 KB )
下载附件
2021-1-8 11:58 上传
pdf.gif (1.91 KB )
下载附件
2021-1-8 11:58 上传
pdf.gif (1.91 KB )
下载附件
2021-1-8 11:58 上传
pdf.gif (1.91 KB )
下载附件
2021-1-8 11:58 上传
pdf.gif (1.91 KB )
下载附件
2021-1-8 11:58 上传
pdf.gif (1.91 KB )
下载附件
2021-1-8 11:58 上传
pdf.gif (1.91 KB )
下载附件
2021-1-8 11:58 上传
rar.gif (2.15 KB )
下载附件
2021-1-8 11:58 上传
pdf.gif (1.91 KB )
下载附件
2021-1-8 11:58 上传
pdf.gif (1.91 KB )
下载附件
2021-1-8 11:58 上传
pdf.gif (1.91 KB )
下载附件
2021-1-8 11:58 上传
pdf.gif (1.91 KB )
下载附件
2021-1-8 11:58 上传
pdf.gif (1.91 KB )
下载附件
2021-1-8 11:58 上传
评论
2021-01-08
您需要登录后才可以回复 登录 | 注册