串口DMA卡死问题

该博客探讨了在单片机串口使用DMA发送数据时遇到的卡死问题。当out_buf->wait信号量未释放且get指针指向末尾时,串口无输出。问题源于DMA中断处理函数中回调函数的执行不及时,导致信号量无法释放。解决方案是在中断处理函数开始时先清理中断,确保回调函数能正确配置DMA并释放信号量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题描述

串口发送使用DMA出现卡死的问题,串口无输出,out_buf->wait信号量未释放,out_buf的get指针指向末尾

原因分析:

DMA本次搬运完,在中断处理函数中调用对应的回调函数,在回调函数里进行buf指针的调整,若还有剩余数据则配置DMA继续搬运
out_buf的get指针指向末尾时,DMA下次搬运数据量较少,很快完成,本次进入DMA中断处理函数中没有及时清理中断,导致下一次中断到来时一起被清理,无法进入callback函数,释放out_buf->wait信号量,此时任务中再次调用serial_write时卡死

解决方案:

将清理中断调整到回调函数之前,进入中断处理函数先清理中断,再在回调函数中配置DMA,dma_isr中部分代码如下

if ((val32 & (0x1 << DMAC_INT_STATUS_TFR_BIT)) 
### STM32 UART DMA Transfer Stuck Solution For issues where the STM32 UART DMA transfer gets stuck, several factors can contribute to this problem. One common cause is improper handling of interrupts and flags that manage the completion or errors during data transmission. In cases when a DMA transfer completes successfully but appears to be stuck, ensuring proper callback function implementation like `I2C_DMAMasterTransmitCplt` for I2C operations might provide insights into correct practices for managing DMA transfers[^1]. However, since the concern here pertains specifically to UART rather than I2C, similar principles apply regarding how callbacks should operate post-transfer: - Verify that all necessary interrupt sources are enabled within the NVIC (Nested Vectored Interrupt Controller). This includes both the UART error interrupts as well as those related directly to DMA. - Confirm whether any overrun/underrun conditions have occurred by checking status registers associated with either peripheral after each transaction attempt; these could prevent further progress until resolved properly. A practical approach involves implementing robust error-checking mechanisms alongside efficient buffer management techniques such as double buffering which allows continuous data flow without interruption between CPU processing cycles and external communications interfaces. Below demonstrates an example setup using HAL library functions tailored towards configuring UART with DMA support on STM32 microcontrollers while incorporating essential checks against potential pitfalls leading up to hanging scenarios: ```c // Enable clocks for USARTx & DMAy peripherals __HAL_RCC_USARTX_CLK_ENABLE(); __HAL_RCC_DMAy_CLK_ENABLE(); static void MX_USARTx_UART_Init(void){ huartx.Instance = USARTx; // Configure parameters including baud rate, word length etc., according to application requirements if (HAL_UART_Init(&huartx) != HAL_OK){ Error_Handler(); // Handle initialization failure appropriately } } static void MX_DMA_Init(void){ __HAL_LINKDMA(huartx, hdmar_tx, hdma_usartx_tx); __HAL_LINKDMA(huartx, hdmar_rx, hdma_usartx_rx); /* Initialize DMA controller clock */ __HAL_RCC_DMAx_CLK_ENABLE(); HDMA_INSTANCE->CCR |= DMA_CCR_EN; if(HAL_DMA_Init(&hdma_usartx_tx) != HAL_OK || HAL_DMA_Init(&hdma_usartx_rx)!= HAL_OK){ Error_Handler(); // Manage DMA configuration failures accordingly } // Additional configurations... } ``` To ensure smooth operation under varying load conditions, consider employing circular mode for DMA channels involved in UART transactions whenever feasible. Circular mode enables automatic retriggering upon reaching end-of-buffer points thus maintaining uninterrupted communication streams even at high throughput rates. Furthermore, always validate received/transmitted byte counts match expected values following every completed session before proceeding forward logically inside software applications built around embedded systems utilizing serial ports interfaced via direct memory access methods.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值