问题描述:
采用DMA1 channel4 作为uart1发送DMA
发现如果启动DMA1 channel4 发送后,如果不查询等待发送结束,接收数据完整
疑问是:DMA是用来释放CPU资源,但是现在还需要等待DMA发送完成,不能释放CPU,DMA存在的意义在哪里?
完整代码:
代码截取如下:
static void DMA_TX_INIT(uint16_t cnt) {
DMA_InitTypeDef DMA_InitStructure = { 0 };
USART_DMACmd(USART1, USART_DMAReq_Tx, DISABLE); //关闭UART1 DMA发送
DMA_Cmd(DMA1_Channel4, DISABLE); /* DMA1 Channel4 关闭 */
DMA_DeInit(DMA1_Channel4);
DMA_InitStructure.DMA_PeripheralBaseAddr = (u32) (&USART1->DATAR);
DMA_InitStructure.DMA_MemoryBaseAddr = (u32) TxBuffer1;//发送数据地址
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
DMA_InitStructure.DMA_BufferSize = cnt;//发送数据长度
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_VeryHigh;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel4, &DMA_InitStructure);
DMA_Cmd(DMA1_Channel4, ENABLE); /* 使能DMA1 Channel4 */
USART_DMACmd(USART1, USART_DMAReq_Tx, ENABLE); //使能UART1 DMA发送
while(DMA_GetFlagStatus(DMA1_FLAG_TC4) == RESET) /* 等待发送完成,如果去掉这一句,接收就不完整 */
{
}
}
void UART1_DMA_Send(u8 *buf, u16 cnt) {
if (cnt > TxSize1)
return;
memcpy(TxBuffer1, buf, cnt);
DMA_TX_INIT(cnt);
}
VOID RFBLE_Task(VOID) {
while(1) {
//LOS_WAIT_FOREVER
uint32_t ret=LOS_SemPend(g_semId, 100);//信号量超时
if(ret==LOS_OK)
UART1_DMA_Send(RxBuffer1Handle,rxDataCnt);//接收回环测试,实际上不给单片机发数据不会触发
else if(ret==LOS_ERRNO_SEM_TIMEOUT)//信号量超时,发送下面内容
{
u8 * RxBuffer1Handle1="hello harmonyOS\r\n";//发送内容
UART1_DMA_Send(RxBuffer1Handle1,strlen(RxBuffer1Handle1));
}
}
}
问题复现100%:
1.去掉红色代码,接收乱了
[12:14:12.889]接收←hel
[12:14:13.140]接收←lo
[12:14:14.078]接收←harmhe
[12:14:14.328]接收←llo
[12:14:15.278]接收← harhe
[12:14:15.528]接收←llo
[12:14:16.484]接收← harhe
[12:14:16.720]接收←llo
[12:14:17.675]接收← harhe
[12:14:17.925]接收←llo
[12:14:18.863]接收← harhe
[12:14:19.123]接收←llo
[12:14:20.070]接收← harhe
[12:14:20.318]接收←llo
[12:14:21.256]接收← harhe
[12:14:21.506]接收←llo
2.加上红色代码,接收正常
[12:15:36.330]接收←hello harmonyOS
[12:15:37.533]接收←hello harmonyOS
[12:15:38.721]接收←hello harmonyOS
[12:15:39.915]接收←hello harmonyOS