21ic问答首页 - gd32f350R 使用iic1
gd32f350R 使用iic1
GDmaker2022-09-14
使用GD32F350R系列的开发板,调试iic协议,使用iic0的时候两个开发板可以通信(都使用iic0接口,一个主,一个从),但是换成iic1就不可以了,不论iic1做主机还是从机都不行。iic1做主机时发现在i2c_master_addressing之后,抓出来的波形只有一个start信号,并没有从机地址的信号发出来以下是主机程序:
#include <stdio.h>
#include "gd32f3x0.h"
#include "gd32f350r_eval.h"
#define I2C1_OWN_ADDRESS7 0x72
#define I2C1_SLAVE_ADDRESS7 0x45
uint8_t i2c_transmitter[16];
void rcu_config(void);
void gpio_config(void);
void i2c_config(void);
/*!
\brief main function
\param[in] none
\param[out] none
\retval none
*/
int main(void)
{
int i;
gd_eval_com_init(EVAL_COM);
/* RCU configure */
rcu_config();
/* GPIO configure */
gpio_config();
/* I2C configure */
i2c_config();
for(i = 0; i < 16; i++) {
i2c_transmitter = i + 0x80;
}
/* wait until I2C bus is idle */
while(i2c_flag_get(I2C1, I2C_FLAG_I2CBSY));
/* send a start condition to I2C bus */
i2c_start_on_bus(I2C1);
/* wait until SBSEND bit is set */
while(!i2c_flag_get(I2C1, I2C_FLAG_SBSEND));
/* send slave address to I2C bus */
i2c_master_addressing(I2C1, 0x45, I2C_TRANSMITTER);
/* wait until ADDSEND bit is set */
while(!i2c_flag_get(I2C1, I2C_FLAG_ADDSEND));
/* clear ADDSEND bit */
i2c_flag_clear(I2C1, I2C_FLAG_ADDSEND);
/* wait until the transmit data buffer is empty */
while(!i2c_flag_get(I2C1, I2C_FLAG_TBE));
printf("start to transfer\r\n");
for(i = 0; i < 16; i++) {
/* data transmission */
i2c_data_transmit(I2C1, i2c_transmitter);
/* wait until the TBE bit is set */
while(!i2c_flag_get(I2C1, I2C_FLAG_TBE));
}
/* send a stop condition to I2C bus */
i2c_stop_on_bus(I2C1);
/* wait until stop condition generate */
while(I2C_CTL0(I2C1) & 0x0200);
/* infinite loop */
while(1) {
}
}
/*!
\brief enable the peripheral clock
\param[in] none
\param[out] none
\retval none
*/
void rcu_config(void)
{
/* enable GPIOB clock */
rcu_periph_clock_enable(RCU_GPIOB);
/* enable I2C1 clock */
rcu_periph_clock_enable(RCU_I2C1);
}
/*!
\brief configure the GPIO ports
\param[in] none
\param[out] none
\retval none
*/
void gpio_config(void)
{
/* connect PB6 to I2C1_SCL */
gpio_af_set(GPIOB, GPIO_AF_1, GPIO_PIN_10);
/* connect PB7 to I2C1_SDA */
gpio_af_set(GPIOB, GPIO_AF_1, GPIO_PIN_11);
/* configure GPIO pins of I2C1 */
gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_10);
gpio_output_options_set(GPIOB, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ, GPIO_PIN_10);
gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_11);
gpio_output_options_set(GPIOB, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ, GPIO_PIN_11);
}
/*!
\brief configure the I2C1 and I2C1 interfaces
\param[in] none
\param[out] none
\retval none
*/
void i2c_config(void)
{
/* I2C clock configure */
i2c_clock_config(I2C1, 100000, I2C_DTCY_2);
/* I2C address configure */
i2c_mode_addr_config(I2C1, I2C_I2CMODE_ENABLE, I2C_ADDFORMAT_7BITS, I2C1_SLAVE_ADDRESS7);
/* enable I2C1 */
i2c_enable(I2C1);
/* enable acknowledge */
i2c_ack_config(I2C1, I2C_ACK_ENABLE);
}
/* retarget the C library printf function to the USART */
int fputc(int ch, FILE *f)
{
usart_data_transmit(EVAL_COM, (uint8_t) ch);
while(RESET == usart_flag_get(EVAL_COM, USART_FLAG_TBE));
return ch;
}
以下是从机程序:
#include <stdio.h>
#include "gd32f3x0.h"
#include "gd32f350r_eval.h"
#define I2C0_OWN_ADDRESS7 0x45
uint8_t i2c_receiver[16];
void rcu_config(void);
void gpio_config(void);
void i2c_config(void);
/*!
\brief main function
\param[in] none
\param[out] none
\retval none
*/
int main(void)
{
int i;
gd_eval_com_init(EVAL_COM);
/* RCU configure */
rcu_config();
/* GPIO configure */
gpio_config();
/* I2C configure */
i2c_config();
i = 0;
printf("start to wait\r\n");
/* wait until ADDSEND bit is set */
while(!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND));
/* clear ADDSEND bit */
printf("address match\r\n");
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
printf("receive:\r\n");
for(i = 0; i < 16; i++) {
/* wait until the RBNE bit is set */
while(!i2c_flag_get(I2C0, I2C_FLAG_RBNE));
/* read a data byte from I2C_DATA */
i2c_receiver = i2c_data_receive(I2C0);
printf("%x ", i2c_receiver);
}
/* wait until the STPDET bit is set */
while(!i2c_flag_get(I2C0, I2C_FLAG_STPDET));
/* clear the STPDET bit */
i2c_enable(I2C0);
printf("\n");
while(1) {
}
}
/*!
\brief enable the peripheral clock
\param[in] none
\param[out] none
\retval none
*/
void rcu_config(void)
{
/* enable GPIOB clock */
rcu_periph_clock_enable(RCU_GPIOB);
/* enable I2C0 clock */
rcu_periph_clock_enable(RCU_I2C0);
}
/*!
\brief configure the GPIO ports
\param[in] none
\param[out] none
\retval none
*/
void gpio_config(void)
{
/* connect PB6 to I2C0_SCL */
gpio_af_set(GPIOB, GPIO_AF_1, GPIO_PIN_6);
/* connect PB7 to I2C0_SDA */
gpio_af_set(GPIOB, GPIO_AF_1, GPIO_PIN_7);
/* configure GPIO pins of I2C0 */
gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_6);
gpio_output_options_set(GPIOB, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ, GPIO_PIN_6);
gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_7);
gpio_output_options_set(GPIOB, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ, GPIO_PIN_7);
}
/*!
\brief configure the I2C0 interface
\param[in] none
\param[out] none
\retval none
*/
void i2c_config(void)
{
/* I2C clock configure */
i2c_clock_config(I2C0, 100000, I2C_DTCY_2);
/* I2C address configure */
i2c_mode_addr_config(I2C0, I2C_I2CMODE_ENABLE, I2C_ADDFORMAT_7BITS, I2C0_OWN_ADDRESS7);
/* enable I2C0 */
i2c_enable(I2C0);
/* enable acknowledge */
i2c_ack_config(I2C0, I2C_ACK_ENABLE);
}
/* retarget the C library printf function to the USART */
int fputc(int ch, FILE *f)
{
usart_data_transmit(EVAL_COM, (uint8_t) ch);
while(RESET == usart_flag_get(EVAL_COM, USART_FLAG_TBE));
return ch;
}
按照我的理解,主机不论是否有从机存在,应该必定会有从机地址发出来的信号,有ACK回应那么说明从机识别成功。
我的iic1的SCL和SDA都外加了上拉电阻到3.3V。
iic1是否有什么需要特殊处理的地方?是否是管脚不对?我从例程里查看它使用iic1的时候也是使用的PB10和PB11
#include <stdio.h>
#include "gd32f3x0.h"
#include "gd32f350r_eval.h"
#define I2C1_OWN_ADDRESS7 0x72
#define I2C1_SLAVE_ADDRESS7 0x45
uint8_t i2c_transmitter[16];
void rcu_config(void);
void gpio_config(void);
void i2c_config(void);
/*!
\brief main function
\param[in] none
\param[out] none
\retval none
*/
int main(void)
{
int i;
gd_eval_com_init(EVAL_COM);
/* RCU configure */
rcu_config();
/* GPIO configure */
gpio_config();
/* I2C configure */
i2c_config();
for(i = 0; i < 16; i++) {
i2c_transmitter = i + 0x80;
}
/* wait until I2C bus is idle */
while(i2c_flag_get(I2C1, I2C_FLAG_I2CBSY));
/* send a start condition to I2C bus */
i2c_start_on_bus(I2C1);
/* wait until SBSEND bit is set */
while(!i2c_flag_get(I2C1, I2C_FLAG_SBSEND));
/* send slave address to I2C bus */
i2c_master_addressing(I2C1, 0x45, I2C_TRANSMITTER);
/* wait until ADDSEND bit is set */
while(!i2c_flag_get(I2C1, I2C_FLAG_ADDSEND));
/* clear ADDSEND bit */
i2c_flag_clear(I2C1, I2C_FLAG_ADDSEND);
/* wait until the transmit data buffer is empty */
while(!i2c_flag_get(I2C1, I2C_FLAG_TBE));
printf("start to transfer\r\n");
for(i = 0; i < 16; i++) {
/* data transmission */
i2c_data_transmit(I2C1, i2c_transmitter);
/* wait until the TBE bit is set */
while(!i2c_flag_get(I2C1, I2C_FLAG_TBE));
}
/* send a stop condition to I2C bus */
i2c_stop_on_bus(I2C1);
/* wait until stop condition generate */
while(I2C_CTL0(I2C1) & 0x0200);
/* infinite loop */
while(1) {
}
}
/*!
\brief enable the peripheral clock
\param[in] none
\param[out] none
\retval none
*/
void rcu_config(void)
{
/* enable GPIOB clock */
rcu_periph_clock_enable(RCU_GPIOB);
/* enable I2C1 clock */
rcu_periph_clock_enable(RCU_I2C1);
}
/*!
\brief configure the GPIO ports
\param[in] none
\param[out] none
\retval none
*/
void gpio_config(void)
{
/* connect PB6 to I2C1_SCL */
gpio_af_set(GPIOB, GPIO_AF_1, GPIO_PIN_10);
/* connect PB7 to I2C1_SDA */
gpio_af_set(GPIOB, GPIO_AF_1, GPIO_PIN_11);
/* configure GPIO pins of I2C1 */
gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_10);
gpio_output_options_set(GPIOB, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ, GPIO_PIN_10);
gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_11);
gpio_output_options_set(GPIOB, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ, GPIO_PIN_11);
}
/*!
\brief configure the I2C1 and I2C1 interfaces
\param[in] none
\param[out] none
\retval none
*/
void i2c_config(void)
{
/* I2C clock configure */
i2c_clock_config(I2C1, 100000, I2C_DTCY_2);
/* I2C address configure */
i2c_mode_addr_config(I2C1, I2C_I2CMODE_ENABLE, I2C_ADDFORMAT_7BITS, I2C1_SLAVE_ADDRESS7);
/* enable I2C1 */
i2c_enable(I2C1);
/* enable acknowledge */
i2c_ack_config(I2C1, I2C_ACK_ENABLE);
}
/* retarget the C library printf function to the USART */
int fputc(int ch, FILE *f)
{
usart_data_transmit(EVAL_COM, (uint8_t) ch);
while(RESET == usart_flag_get(EVAL_COM, USART_FLAG_TBE));
return ch;
}
以下是从机程序:
#include <stdio.h>
#include "gd32f3x0.h"
#include "gd32f350r_eval.h"
#define I2C0_OWN_ADDRESS7 0x45
uint8_t i2c_receiver[16];
void rcu_config(void);
void gpio_config(void);
void i2c_config(void);
/*!
\brief main function
\param[in] none
\param[out] none
\retval none
*/
int main(void)
{
int i;
gd_eval_com_init(EVAL_COM);
/* RCU configure */
rcu_config();
/* GPIO configure */
gpio_config();
/* I2C configure */
i2c_config();
i = 0;
printf("start to wait\r\n");
/* wait until ADDSEND bit is set */
while(!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND));
/* clear ADDSEND bit */
printf("address match\r\n");
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
printf("receive:\r\n");
for(i = 0; i < 16; i++) {
/* wait until the RBNE bit is set */
while(!i2c_flag_get(I2C0, I2C_FLAG_RBNE));
/* read a data byte from I2C_DATA */
i2c_receiver = i2c_data_receive(I2C0);
printf("%x ", i2c_receiver);
}
/* wait until the STPDET bit is set */
while(!i2c_flag_get(I2C0, I2C_FLAG_STPDET));
/* clear the STPDET bit */
i2c_enable(I2C0);
printf("\n");
while(1) {
}
}
/*!
\brief enable the peripheral clock
\param[in] none
\param[out] none
\retval none
*/
void rcu_config(void)
{
/* enable GPIOB clock */
rcu_periph_clock_enable(RCU_GPIOB);
/* enable I2C0 clock */
rcu_periph_clock_enable(RCU_I2C0);
}
/*!
\brief configure the GPIO ports
\param[in] none
\param[out] none
\retval none
*/
void gpio_config(void)
{
/* connect PB6 to I2C0_SCL */
gpio_af_set(GPIOB, GPIO_AF_1, GPIO_PIN_6);
/* connect PB7 to I2C0_SDA */
gpio_af_set(GPIOB, GPIO_AF_1, GPIO_PIN_7);
/* configure GPIO pins of I2C0 */
gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_6);
gpio_output_options_set(GPIOB, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ, GPIO_PIN_6);
gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_7);
gpio_output_options_set(GPIOB, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ, GPIO_PIN_7);
}
/*!
\brief configure the I2C0 interface
\param[in] none
\param[out] none
\retval none
*/
void i2c_config(void)
{
/* I2C clock configure */
i2c_clock_config(I2C0, 100000, I2C_DTCY_2);
/* I2C address configure */
i2c_mode_addr_config(I2C0, I2C_I2CMODE_ENABLE, I2C_ADDFORMAT_7BITS, I2C0_OWN_ADDRESS7);
/* enable I2C0 */
i2c_enable(I2C0);
/* enable acknowledge */
i2c_ack_config(I2C0, I2C_ACK_ENABLE);
}
/* retarget the C library printf function to the USART */
int fputc(int ch, FILE *f)
{
usart_data_transmit(EVAL_COM, (uint8_t) ch);
while(RESET == usart_flag_get(EVAL_COM, USART_FLAG_TBE));
return ch;
}
按照我的理解,主机不论是否有从机存在,应该必定会有从机地址发出来的信号,有ACK回应那么说明从机识别成功。
我的iic1的SCL和SDA都外加了上拉电阻到3.3V。
iic1是否有什么需要特殊处理的地方?是否是管脚不对?我从例程里查看它使用iic1的时候也是使用的PB10和PB11
您需要登录后才可以回复 登录 | 注册