求助用过这个芯片的大佬,串口接收配置为dma方式,当接收到的数据长度与配置长度相等时。输出来始终多着2个字节。请问什么原因
串口配置如下:
void usart0_init(u32 bound)
{
dma_single_data_parameter_struct dma_init_struct;
/* enable USART clock */
rcu_periph_clock_enable(RCU_USART0);
/* configure the USART0 Tx pin and USART0 Rx pin */
gpio_af_set(GPIOA, GPIO_AF_7, GPIO_PIN_9);
gpio_af_set(GPIOA, GPIO_AF_7, GPIO_PIN_10);
/* configure USART0 Tx as alternate function push-pull */
gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_9|GPIO_PIN_10);
gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_200MHZ, GPIO_PIN_9|GPIO_PIN_10);
/* USART configure */
usart_deinit(USART0);
usart_baudrate_set(USART0, bound);
usart_receive_config(USART0, USART_RECEIVE_ENABLE);
usart_transmit_config(USART0, USART_TRANSMIT_ENABLE);
usart_enable(USART0);
/* enable DMA1 */
rcu_periph_clock_enable(RCU_DMA1);
/* deinitialize DMA channel7(USART0 tx) */
dma_deinit(DMA1, DMA_CH7); //dma寄存器初始化
dma_init_struct.direction = DMA_MEMORY_TO_PERIPH; //传输模式,存储到外设(发送)
dma_init_struct.memory0_addr = (uint32_t)txbuffer; //dma内存地址
dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE; //内存地址增量模式
dma_init_struct.periph_memory_width = DMA_PERIPH_WIDTH_8BIT; //dma外设宽度8位
dma_init_struct.number = 100; //长度
dma_init_struct.periph_addr = (uint32_t)&USART_DATA(USART0); //外设基地址
dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE; //外设地址增量禁用
dma_init_struct.priority = DMA_PRIORITY_HIGH; //优先级高
dma_single_data_mode_init(DMA1, DMA_CH7, &dma_init_struct);
/* configure DMA mode */
dma_circulation_disable(DMA1, DMA_CH7); //循环模式禁用
dma_channel_subperipheral_select(DMA1, DMA_CH7, DMA_SUBPERI4); //通道7 外设4 USART0_TX
dma_interrupt_enable(DMA1, DMA_CH7, DMA_CHXCTL_FTFIE); //传输完成中断
nvic_irq_enable(DMA1_Channel7_IRQn, 5, 0); //5 抢占优先级,(组4只有抢占优先级)
usart_dma_transmit_config(USART0, USART_DENT_ENABLE);
/* deinitialize DMA1 channel2 (USART0 rx) */
dma_deinit(DMA1, DMA_CH2);
dma_init_struct.direction = DMA_PERIPH_TO_MEMORY; //传输模式,外设到存储(接收)
dma_init_struct.memory0_addr = (uint32_t)rxbuffer;
dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
dma_init_struct.number = 100;
dma_init_struct.periph_addr = (uint32_t)&USART_DATA(USART0);
dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
dma_init_struct.periph_memory_width = DMA_PERIPH_WIDTH_8BIT;
dma_init_struct.priority = DMA_PRIORITY_ULTRA_HIGH;
dma_single_data_mode_init(DMA1, DMA_CH2, &dma_init_struct);
/* configure DMA mode */
dma_circulation_disable(DMA1, DMA_CH2);
dma_channel_subperipheral_select(DMA1, DMA_CH2, DMA_SUBPERI4);
usart_interrupt_enable(USART0, USART_INT_IDLE);
/* USART interrupt configuration */
nvic_irq_enable(USART0_IRQn, 6, 0);
/* enable DMA1 channel2 */
dma_channel_enable(DMA1, DMA_CH2);
usart_dma_receive_config(USART0, USART_DENR_ENABLE);
}
//串口0 DMA接收,只触发空闲中断
void USART0_IRQHandler(void){
u8 revLen;
if(RESET != usart_flag_get(USART0, USART_FLAG_IDLE))
{
USART_STAT0(USART0);
usart_data_receive(USART0); //清除串口中断
revLen = 100 - dma_transfer_number_get(DMA1, DMA_CH2);
dma_channel_disable(DMA1, DMA_CH2);
dma_transfer_number_config(DMA1, DMA_CH2, 100);
dma_flag_clear(DMA1,DMA_CH2,DMA_INTF_FTFIF);
dma_channel_enable(DMA1, DMA_CH2);
// printf("len:%d %s\r\n",revLen,rxbuffer);
printf("%s",rxbuffer);
memset(rxbuffer,0,100);
}
}
printf输出的信息。当接收数据小于100字节时。都是正确的。当接收数据大于等于100字节时。能输出接收到的100字节数据。但是还多了两个字节0xC4 10
请问各位要怎么解决
|
共1人点赞
|