想要請問,當我利用DMA做USART傳輸時,當我定義BufferSize大小為DataLen時,當我從PC端寫入一串數據如"123456",但是傳送出來的數據一定是"0123456012"剛好填滿我設定的buffer 大小。請問是哪裡出問題呢?謝謝我的程式如下: #define DataLen 10 u8 SendBuff[DataLen]; u8 RecviceBuff[DataLen]; void USART_DMA_Configuration(void) { DMA_InitTypeDef DMA_InitStructure; DMA_DeInit(DMA1_Channel4); DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&USART1->DR; DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)SendBuff; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; DMA_InitStructure.DMA_BufferSize = DataLen; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; DMA_Init(DMA1_Channel4, &DMA_InitStructure); DMA_ITConfig(DMA1_Channel4, DMA_IT_TC, ENABLE); DMA_ITConfig(DMA1_Channel4, DMA_IT_TE, ENABLE); USART_DMACmd(USART1, USART_DMAReq_Tx, ENABLE); DMA_Cmd(DMA1_Channel4, DISABLE); DMA_DeInit(DMA1_Channel5); DMA_InitStructure.DMA_PeripheralBaseAddr = USART1_DT_Base; DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t) RecviceBuff; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStructure.DMA_BufferSize = DataLen; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; DMA_Init(DMA1_Channel5, &DMA_InitStructure); DMA_Cmd(DMA1_Channel5, ENABLE); /*****************************************************************/ **************主流程 PC端寫入資料並由SMT32F1輸出至PC端 if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) { DMA_Cmd(DMA1_Channel5, DISABLE); DATA_LEN=DataLen - DMA_GetCurrDataCounter(DMA1_Channel5); if(DATA_LEN > 0) { for(i=0;i<DATA_LEN;i++) { SendBuff[i]= RecviceBuff[i]; // Let Memory receive data copy to Send memory } DMA_Cmd(DMA1_Channel4, DISABLE); DMA1_Channel4->CNDTR=DATA_LEN; USART_DMACmd(USART1, USART_DMAReq_Tx, ENABLE); DMA_Cmd(DMA1_Channel4, ENABLE); } |
你的"0123456012"数据是不是你转换的,因为PC发送"123456"的话,应该手册hex的 31 32 33 34 35 36,即使转换也不应该有‘0,’,另外使用了DMA的话,如果能已知接收到的数据长度,建议使用DMA接收中断,高效且不易出错... |
謝謝您的建議! |