求助 GD32F4系列I2C+DMA中断模式I2C初始化配置代码
static void GD_I2C_Init(void)
{
/* enable clock */
rcu_periph_clock_enable(I2C_SCL_GPIO_CLK);
rcu_periph_clock_enable(I2C_SDA_GPIO_CLK);
/* enable I2C0 clock */
rcu_periph_clock_enable(I2C_CLK);
/* connect PB6 to I2C0_SCL */
gpio_af_set(I2C_SCL_GPIO_PORT, GPIO_AF_4, I2C_SCL_PIN);
/* connect PB7 to I2C0_SDA */
gpio_af_set(I2C_SDA_GPIO_PORT, GPIO_AF_4, I2C_SDA_PIN);
/* configure GPIO pins of I2C0 */
gpio_mode_set(I2C_SCL_GPIO_PORT, GPIO_MODE_AF, GPIO_PUPD_PULLUP, I2C_SCL_PIN);
gpio_output_options_set(I2C_SCL_GPIO_PORT, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ, I2C_SCL_PIN);
gpio_mode_set(I2C_SDA_GPIO_PORT, GPIO_MODE_AF, GPIO_PUPD_PULLUP, I2C_SDA_PIN);
gpio_output_options_set(I2C_SDA_GPIO_PORT, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ, I2C_SDA_PIN);
/* configure I2C clock */
i2c_clock_config(I2C_PORT,I2C_SPEED,I2C_DTCY_2);
/* configure I2C address */
i2c_mode_addr_config(I2C_PORT,I2C_I2CMODE_ENABLE,I2C_ADDFORMAT_7BITS,I2C_SLAVE_ADDRESS7);
/* enable I2C0 */
i2c_enable(I2C_PORT);
/* enable acknowledge */
i2c_ack_config(I2C_PORT,I2C_ACK_ENABLE);
nvic_priority_group_set(NVIC_PRIGROUP_PRE2_SUB2);
nvic_irq_enable(I2C_EV_IRQn, 1, 0);
i2c_interrupt_enable(I2C_PORT, I2C_INT_EV);
}
void GD_I2C_DMA_Init(void)
{
/* enable DMA clock */
rcu_periph_clock_enable(RCU_DMA0);
dma_single_data_parameter_struct dma_init_struct;
/* initialize DMA channel1 */
dma_deinit(DMA0, DMA_CH7);
dma_single_data_para_struct_init(&dma_init_struct);
dma_init_struct.direction = DMA_MEMORY_TO_PERIPH;
dma_init_struct.memory0_addr = (uint32_t)I2CSentBuff;
dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
dma_init_struct.periph_memory_width = DMA_MEMORY_WIDTH_8BIT;
dma_init_struct.number = I2CSentBuff_Size;
dma_init_struct.periph_addr = (uint32_t)&I2C_DATA(I2C_PORT);//
dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
dma_init_struct.priority = DMA_PRIORITY_ULTRA_HIGH;
dma_single_data_mode_init(DMA0, DMA_CH7, &dma_init_struct);
/* configure DMA mode */
dma_circulation_disable(DMA0, DMA_CH7);
dma_channel_subperipheral_select(DMA0, DMA_CH7, DMA_SUBPERI1);//选择DMA0~1,CH0~7,Pherien0~7
/* initialize DMA channel2 */
dma_deinit(DMA0, DMA_CH0);
dma_init_struct.direction = DMA_PERIPH_TO_MEMORY;
dma_init_struct.memory0_addr = (uint32_t)I2CRecvBuff;
dma_init_struct.periph_addr = (uint32_t)&I2C_DATA(I2C_PORT);//
dma_init_struct.priority = DMA_PRIORITY_ULTRA_HIGH;
dma_single_data_mode_init(DMA0, DMA_CH0, &dma_init_struct);
dma_circulation_disable(DMA0, DMA_CH0);
dma_channel_subperipheral_select(DMA0, DMA_CH0, DMA_SUBPERI1);//选择DMA0~1,CH0~7,Pherien0~7
nvic_irq_enable(DMA0_Channel0_IRQn, 1, 0);
nvic_irq_enable(DMA0_Channel7_IRQn, 1, 0);
dma_interrupt_enable(DMA0, DMA_CH0, DMA_CHXCTL_FTFIE);
dma_interrupt_enable(DMA0, DMA_CH7, DMA_CHXCTL_FTFIE);
}
中断后的通讯流程不知道应该如何处理?
if(i2c_flag_get(i2c_periph, I2C_FLAG_ADDSEND))
{
if(ADDSEND_FLAG<2)
{
ADDSEND_FLAG++;
}
/* clear ADDSEND bit */
i2c_flag_clear(i2c_periph, I2C_FLAG_ADDSEND);
i2c_dma_config(I2C0,I2C_DMA_ON);
i2c_dma_last_transfer_config(I2C0,I2C_DMALST_ON);
/* enable DMA channel2 */
dma_channel_enable(DMA0, DMA_CH0);
/* enable DMA channel1 */
dma_channel_enable(DMA0, DMA_CH7);
}
if(i2c_flag_get(i2c_periph, I2C_FLAG_STPDET))
{
i2c_enable(i2c_periph);
i2c_interrupt_disable(i2c_periph, I2C_INT_EV);
i2c_dma_config(I2C0,I2C_DMA_OFF);
i2c_dma_last_transfer_config(I2C0,I2C_DMALST_OFF);
/* enable DMA channel2 */
dma_channel_disable(DMA0, DMA_CH0);
/* enable DMA channel1 */
dma_channel_disable(DMA0, DMA_CH7);
}
|
共1人点赞
|