芯片是TSSOP20封装,采用内部晶振。想测试IIC通信,用I2C1.但是无法通信,I2C引脚是PA9,PA10。采用中断方式。有人能帮忙看一下吗?非常感谢!
看手册,PA9,PA10作为IIC,MUX_NUM应该未MUX4。这个应该没错吧!
这个是定义
#define I2C_TIMEOUT 0xFFFFFFFF
#define I2Cx_SPEED 100000
#define I2Cx_ADDRESS 2
#define I2Cx_PORT I2C1
#define I2Cx_CLK CRM_I2C1_PERIPH_CLOCK
#define I2Cx_SCL_PIN GPIO_PINS_9
#define I2Cx_SCL_PIN_SOURCE GPIO_PINS_SOURCE9
#define I2Cx_SCL_PIN_MUX_NUM GPIO_MUX_4
#define I2Cx_SCL_GPIO_PORT GPIOA
#define I2Cx_SCL_GPIO_CLK CRM_GPIOA_PERIPH_CLOCK
#define I2Cx_SDA_PIN GPIO_PINS_10
#define I2Cx_SDA_PIN_SOURCE GPIO_PINS_SOURCE10
#define I2Cx_SDA_PIN_MUX_NUM GPIO_MUX_4
#define I2Cx_SDA_GPIO_PORT GPIOA
#define I2Cx_SDA_GPIO_CLK CRM_GPIOA_PERIPH_CLOCK
#define BUF_SIZE 8
初始化如下:
void i2c_lowlevel_init(i2c_handle_type* hi2c)
{
gpio_init_type gpio_initstructure;
if(hi2c->i2cx == I2Cx_PORT)
{
/* i2c periph clock enable */
crm_periph_clock_enable(I2Cx_CLK, TRUE);
crm_periph_clock_enable(I2Cx_SCL_GPIO_CLK, TRUE);
crm_periph_clock_enable(I2Cx_SDA_GPIO_CLK, TRUE);
/* gpio configuration */
gpio_initstructure.gpio_out_type = GPIO_OUTPUT_OPEN_DRAIN;
gpio_initstructure.gpio_pull = GPIO_PULL_UP;
gpio_initstructure.gpio_mode = GPIO_MODE_MUX;
gpio_initstructure.gpio_drive_strength = GPIO_DRIVE_STRENGTH_MODERATE;
/* configure i2c pins: scl */
gpio_initstructure.gpio_pins = I2Cx_SCL_PIN;
gpio_init(I2Cx_SCL_GPIO_PORT, &gpio_initstructure);
gpio_pin_mux_config(I2Cx_SCL_GPIO_PORT, I2Cx_SCL_PIN_SOURCE, I2Cx_SCL_PIN_MUX_NUM);
/* configure i2c pins: sda */
gpio_initstructure.gpio_pins = I2Cx_SDA_PIN;
gpio_init(I2Cx_SDA_GPIO_PORT, &gpio_initstructure);
gpio_pin_mux_config(I2Cx_SDA_GPIO_PORT, I2Cx_SDA_PIN_SOURCE, I2Cx_SDA_PIN_MUX_NUM);
/* configure and enable i2c interrupt */
nvic_irq_enable(I2C1_EVT_IRQn, 0, 0);
nvic_irq_enable(I2C1_ERR_IRQn, 0, 0);
i2c_init(hi2c->i2cx, I2C_FSMODE_DUTY_2_1, I2Cx_SPEED);
i2c_own_address1_set(hi2c->i2cx, I2C_ADDRESS_MODE_7BIT, I2Cx_ADDRESS);
}
}
工程代码附件如下
|