本帖最后由 fleepv7 于 2020-12-12 11:05 编辑
你好,我试图将I2C总线与GD32F330CBT6中的DMA一起使用,但没有成功。 仅在将其固定在DMA的FTF标志之后的等待BTC标志的底线上之后,它才第一次工作。 下面是源代码:
初始化代码
void i2cbus_ll_init(void)
{
dma_parameter_struct dma_init_struct;
// RCU Part
rcu_periph_clock_enable(I2C_PERIPH_RCU);
rcu_periph_clock_enable(I2C_PORT_RCU);
rcu_periph_clock_enable(RCU_DMA);
// GPIO Part
gpio_af_set(I2C_PORT, GPIO_AF_1, I2C_CLOCK_PIN);
gpio_af_set(I2C_PORT, GPIO_AF_1, I2C_DATA_PIN);
gpio_mode_set(I2C_PORT, GPIO_MODE_AF, GPIO_PUPD_PULLUP, I2C_CLOCK_PIN);
gpio_output_options_set(I2C_PORT, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ, I2C_CLOCK_PIN);
gpio_mode_set(I2C_PORT, GPIO_MODE_AF, GPIO_PUPD_PULLUP, I2C_DATA_PIN);
gpio_output_options_set(I2C_PORT, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ, I2C_DATA_PIN);
// DMA Part
// -> RX
dma_deinit(DMA_CH4);
dma_struct_para_init(&dma_init_struct);
dma_init_struct.direction = DMA_PERIPHERAL_TO_MEMORY;
dma_init_struct.memory_addr = (uint32_t)i2c_rxbuffer;
dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
dma_init_struct.memory_width = DMA_MEMORY_WIDTH_8BIT;
dma_init_struct.number = 24;
dma_init_struct.periph_addr = I2C1_DATA_ADDRESS;
dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
dma_init_struct.periph_width = DMA_PERIPHERAL_WIDTH_8BIT;
dma_init_struct.priority = DMA_PRIORITY_ULTRA_HIGH;
dma_init(DMA_CH4, &dma_init_struct);
// -> TX
dma_deinit(DMA_CH3);
dma_init_struct.direction = DMA_MEMORY_TO_PERIPHERAL;
dma_init_struct.memory_addr = (uint32_t)i2c_txbuffer;
dma_init_struct.periph_addr = I2C1_DATA_ADDRESS;
dma_init_struct.priority = DMA_PRIORITY_HIGH;
dma_init(DMA_CH3, &dma_init_struct);
dma_circulation_disable(DMA_CH4);
dma_circulation_disable(DMA_CH3);
dma_memory_to_memory_disable(DMA_CH4);
dma_memory_to_memory_disable(DMA_CH3);
dma_interrupt_enable(DMA_CH3, DMA_INT_FTF);
dma_interrupt_enable(DMA_CH4, DMA_INT_FTF);
// I2C Part
i2c_clock_config(I2C_PERIPH, 400000U, I2C_DTCY_2);
i2c_mode_addr_config(I2C_PERIPH, I2C_I2CMODE_ENABLE, I2C_ADDFORMAT_7BITS, 0x70);
i2c_enable(I2C_PERIPH);
// NVIC Part
//nvic_irq_enable(I2C_NVIC_EVENT_IRQ, 1, 1);
nvic_irq_enable(I2C_NVIC_ERROR_IRQ, 1, 2);
//nvic_irq_enable(DMA_Channel3_4_IRQn, 0, 0);
}
编写功能代码
i2cbus_error_t i2cbus_write_data(uint8_t addr, uint8_t *data, uint8_t count)
{
i2cbus_error_t err = I2C_OperationTimedOutError;
dma_parameter_struct dma_init_struct;
// Copying data to static buffer
for (int i = 0; i < count; i++)
i2c_txbuffer[i] = *data++;
// DMA Transfer configuration according to GD32F3x0 User Manual - Page 158/159.
dma_channel_disable(DMA_CH3); // 1
dma_memory_to_memory_disable(DMA_CH3); // 2
dma_transfer_direction_config(DMA_CH3, DMA_MEMORY_TO_PERIPHERAL); // 2
dma_circulation_disable(DMA_CH3); // 3
dma_priority_config(DMA_CH3, DMA_PRIORITY_HIGH); // 4
dma_periph_increase_disable(DMA_CH3); // 5
dma_periph_width_config(DMA_CH3, DMA_PERIPHERAL_WIDTH_8BIT); // 5
dma_memory_increase_enable(DMA_CH3); // 5
dma_memory_width_config(DMA_CH3, DMA_MEMORY_WIDTH_8BIT); // 5
dma_interrupt_enable(DMA_CH3, DMA_INT_FTF); // 6
dma_interrupt_enable(DMA_CH3, DMA_INT_ERR); // 6
dma_periph_address_config(DMA_CH3, I2C1_DATA_ADDRESS); // 7
dma_memory_address_config(DMA_CH3, (uint32_t)i2c_txbuffer); // 8
dma_transfer_number_config(DMA_CH3, count); // 9
dma_channel_enable(DMA_CH3); // 10
// Wait for bus to be available
while(i2c_flag_get(I2C1, I2C_FLAG_I2CBSY));
// Start
i2c_start_on_bus(I2C1);
while(!i2c_flag_get(I2C1, I2C_FLAG_SBSEND));
// Address
i2c_master_addressing(I2C1, addr, I2C_TRANSMITTER);
while(!i2c_flag_get(I2C1, I2C_FLAG_ADDSEND));
i2c_flag_clear(I2C1, I2C_FLAG_ADDSEND);
// DMA
i2c_dma_enable(I2C1, I2C_DMA_ON);
i2c_dma_last_transfer_config(I2C1, I2C_DMALST_OFF);
dma_channel_enable(DMA_CH3);
while (!dma_interrupt_flag_get(DMA_CH3, DMA_INT_FLAG_FTF));
dma_flag_clear(DMA_CH3, DMA_INT_FLAG_FTF);
while(!i2c_flag_get(I2C1, I2C_FLAG_BTC));
i2c_stop_on_bus(I2C_PERIPH);
while(I2C_CTL0(I2C_PERIPH) & 0x0200);
return err;
}
它卡在了线上:while(!i2c_flag_get(I2C1,I2C_FLAG_BTC));;第二次我尝试写作。有人遇到过这个问题吗?
如果我不这样说的话。 DMA不发送数据。
|
|