1.使用STM32CubeMX配置的串口引脚设置和dma的设置会生成在usart.c。
1)如果DMA接收想采用循环缓冲区的方式,可以直接将RX-DMA设置成Circle方式,然后数据就会硬件上自动实现环形缓冲区的功能,省了不少时间。
2)DMA在采用Normal模式的时候,当一次任务完成后,DMA->DMA_BufferSize自动清零,并且DMA自动停止。如果想再次设置DMA的BufferSize的话,必须要进行如下操作:
step1:DMA_CMD(DMAx_Channely,DISABLE);
step2: 设置DMA_BufferLen
step3:DMA_CMD(DMAx_Channely,ENABLE)
3)DMA采用Circle模式的时候,在发送或者接受完成之后,仍然保存着BufferSize,并且DMA还处于使能状态,一直连续工作,直到用户停止DMA
else if(uartHandle->Instance==USART1)
{
/* USER CODE BEGIN USART1_MspInit 0 */
/* USER CODE END USART1_MspInit 0 */
/* USART1 clock enable */
__HAL_RCC_USART1_CLK_ENABLE();
/**USART1 GPIO Configuration
PA9 ------> USART1_TX
PA10 ------> USART1_RX
*/
GPIO_InitStruct.Pin = GPIO_PIN_9|GPIO_PIN_10;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF7_USART1;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* USART1 DMA Init */
/* USART1_RX Init */
hdma_usart1_rx.Instance = DMA2_Stream2;
hdma_usart1_rx.Init.Channel = DMA_CHANNEL_4;
hdma_usart1_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
hdma_usart1_rx.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_usart1_rx.Init.MemInc = DMA_MINC_ENABLE;
hdma_usart1_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
hdma_usart1_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
hdma_usart1_rx.Init.Mode = DMA_NORMAL; //这里设成DMA_CIRCULAR和DMA_NORMAL好像没有对双缓冲造成影响
hdma_usart1_rx.Init.Priority = DMA_PRIORITY_LOW;
hdma_usart1_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
if (HAL_DMA_Init(&hdma_usart1_rx) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
__HAL_LINKDMA(uartHandle,hdmarx,hdma_usart1_rx);
/* USART1_TX Init */
hdma_usart1_tx.Instance = DMA2_Stream7;
hdma_usart1_tx.Init.Channel = DMA_CHANNEL_4;
hdma_usart1_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
hdma_usart1_tx.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_usart1_tx.Init.MemInc = DMA_MINC_ENABLE;
hdma_usart1_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
hdma_usart1_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
hdma_usart1_tx.Init.Mode = DMA_NORMAL;
hdma_usart1_tx.Init.Priority = DMA_PRIORITY_LOW;
hdma_usart1_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
if (HAL_DMA_Init(&hdma_usart1_tx) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
__HAL_LINKDMA(uartHandle,hdmatx,hdma_usart1_tx);
/* USART1 interrupt Init */
HAL_NVIC_SetPriority(USART1_IRQn, 5, 0);
HAL_NVIC_EnableIRQ(USART1_IRQn);
/* USER CODE BEGIN USART1_MspInit 1 */
/* USER CODE END USART1_MspInit 1 */
}
2.下面是关于DMA双缓冲机制的配置,特别说明一下,我先__HAL_DMA_DISABLE(hdma); 再配置后面的参数。DMA才正常接收数据了。
static HAL_StatusTypeDef DMAEx_MultiBufferStart_IT(DMA_HandleTypeDef *hdma,
uint32_t SrcAddress,
uint32_t DstAddress,
uint32_t SecondMemAddress,
uint32_t DataLength)
{
HAL_StatusTypeDef status = HAL_OK;
/* Memory-to-memory transfer not supported in double buffering mode */
if (hdma->Init.Direction == DMA_MEMORY_TO_MEMORY)
{
hdma->ErrorCode = HAL_DMA_ERROR_NOT_SUPPORTED;
return HAL_ERROR;
}
/* Set the UART DMA transfer complete callback */
/* Current memory buffer used is Memory 1 callback */
hdma->XferCpltCallback = dma_m0_rxcplt_callback; //第一个缓冲区填满后会调用这个函数
/* Current memory buffer used is Memory 0 callback */
hdma->XferM1CpltCallback = dma_m1_rxcplt_callback; ////第二个缓冲区填满后会调用这个函数
/* Check callback functions */
if ((NULL == hdma->XferCpltCallback) || (NULL == hdma->XferM1CpltCallback))
{
hdma->ErrorCode = HAL_DMA_ERROR_PARAM;
return HAL_ERROR;
}
/* Process locked */
__HAL_LOCK(hdma);
/* Enable the peripheral */
__HAL_DMA_DISABLE(hdma); //先要禁止DMA后面的设置才会生效
//if(HAL_DMA_STATE_READY == hdma->State)
//{
/* Change DMA peripheral state */
hdma->State = HAL_DMA_STATE_BUSY;
/* Initialize the error code */
hdma->ErrorCode = HAL_DMA_ERROR_NONE;
/* Enable the Double buffer mode */
hdma->Instance->CR |= (uint32_t)DMA_SxCR_DBM;
/* Configure DMA Stream destination address */
hdma->Instance->M1AR = SecondMemAddress;
/* Configure DMA Stream data length */
hdma->Instance->NDTR = DataLength;
/* Configure the source, destination address */
if((hdma->Init.Direction) == DMA_MEMORY_TO_PERIPH)
{
hdma->Instance->PAR = DstAddress;
hdma->Instance->M0AR = SrcAddress;
}
else
{
hdma->Instance->PAR = SrcAddress;
hdma->Instance->M0AR = DstAddress;
}
/* Clear TC flags */
__HAL_DMA_CLEAR_FLAG (hdma, __HAL_DMA_GET_TC_FLAG_INDEX(hdma));
/* Enable TC interrupts*/
hdma->Instance->CR |= DMA_IT_TC;
/* Enable the peripheral */
__HAL_DMA_ENABLE(hdma);
/* Change the DMA state */
hdma->State = HAL_DMA_STATE_READY;
//}
//else
//{
// /* Return error status */
// status = HAL_BUSY;
//}
/* Process unlocked */
__HAL_UNLOCK(hdma);
return status;
}
void debug_uart_init(void)
{
//open uart idle it
__HAL_UART_CLEAR_IDLEFLAG(&DEBUG_HUART);
__HAL_UART_ENABLE_IT(&DEBUG_HUART, UART_IT_IDLE);//设置了空闲中断。进入空闲中断就可以考虑处理数据了。如果数据量大就放到专门的任务里处理。
// Enable the DMA transfer for the receiver request
SET_BIT(DEBUG_HUART.Instance->CR3, USART_CR3_DMAR);
DMAEx_MultiBufferStart_IT(DEBUG_HUART.hdmarx,
(uint32_t)&DEBUG_HUART.Instance->DR,
(uint32_t)debug_dma_rxbuff[0],
(uint32_t)debug_dma_rxbuff[1],
UART_RX_DMA_SIZE);
}
上一篇:STM32F407各定时器的时钟频率
下一篇:STM32之TIM 舵机控制PWM
推荐阅读最新更新时间:2024-11-09 02:57
设计资源 培训 开发板 精华推荐
- STEVAL-MKI158V1,用于标准 DIL24 插座的 AIS3624DQ 适配器板
- 使用 Asahi Kasei Microdevices Corporation 的 AK4103AVF 的参考设计
- FMC-ADC01-7-1-0,用于 ADS6422、12 位、4 通道、65 MSPS 模数转换器的 FPGA 夹层卡
- BR262CPP01GEVK、BelaSigna R262 客户原型平台评估套件
- AD9277-50EBZ,用于评估 AD9277 八通道 LNA/VGA/AAF、14 位 ADC、CW I/Q 解调器的评估板
- 【课程设计】空气净化器
- DC1649A-A,用于 LTC6945 超低噪声和杂散 0.35GHz 至 6GHz 整数 N 合成器的演示板
- LTC3418EUHF 演示板、8A 单片式、同步降压型稳压器、2.25V 至 5.5V、跳线可选、1.2、1.5、1.8Vout @ 8A(最大值)
- AM2M-0505SH30-NZ 5 Vout、2W 单路输出 DC-DC 转换器的典型应用
- 亮度可调台灯