2025-08-26 8:47 AM - last edited on 2025-08-26 9:22 AM by Imen.D
void HAL_UART_MspInit(UART_HandleTypeDef* huart)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
if(huart->Instance==USART2)
{
/* USER CODE BEGIN USART2_MspInit 0 */
/* USER CODE END USART2_MspInit 0 */
/* Peripheral clock enable */
__HAL_RCC_USART2_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
/**USART2 GPIO Configuration
PB6 ------> USART2_TX
PB7 ------> USART2_RX
*/
GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF0_USART2;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/* USART2 DMA Init */
/* USART2_RX Init */
hdma_usart2_rx.Instance = DMA1_Channel3;// DMA1_Channel5 work
hdma_usart2_rx.Init.Request = DMA_REQUEST_4;
hdma_usart2_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
hdma_usart2_rx.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_usart2_rx.Init.MemInc = DMA_MINC_ENABLE;
hdma_usart2_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
hdma_usart2_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
hdma_usart2_rx.Init.Mode = DMA_NORMAL;
hdma_usart2_rx.Init.Priority = DMA_PRIORITY_VERY_HIGH;
if (HAL_DMA_Init(&hdma_usart2_rx) != HAL_OK)
{
Error_Handler();
}
__HAL_LINKDMA(huart,hdmarx,hdma_usart2_rx);
/* USART2 interrupt Init */
HAL_NVIC_SetPriority(USART2_IRQn, 2, 0);
HAL_NVIC_EnableIRQ(USART2_IRQn);
/* USER CODE BEGIN USART2_MspInit 1 */
/* USER CODE END USART2_MspInit 1 */
}
}
void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
{
//recv size
uint8_t send_buf[2];
send_buf[0]=Size&0xFF;
send_buf[1]=Size>>8;
HAL_UART_Transmit(huart,send_buf,2,20);
HAL_UARTEx_ReceiveToIdle_DMA(&huart2,buf,32);
}
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_DMA_Init();
MX_USART2_UART_Init();
/* USER CODE BEGIN 2 */
HAL_UARTEx_ReceiveToIdle_DMA(&huart2,buf,32);//start recv
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
Edited by ST Moderator to apply source code formatting - please see this guideline How to insert source code.
2025-08-26 9:19 AM - edited 2025-08-26 9:20 AM
Hello @ElectronicABC,
When USART2 uses DMA channel 3 for data reception, data transfers are blocked after the first byte has been
received. As a result, DMA channel 3 cannot be used for USART2 data reception.
This is mentioned in the STM32L011x3 errata sheet:
"
2.11.6 DMA channel 3 (CH3) not functional when USART2_RX used for data reception
Description
When USART2 uses DMA channel 3 for data reception, data transfers are blocked after the first byte has been
received. As a result, DMA channel 3 cannot be used for USART2 data reception.
Workaround
Use DMA channel 5 (CH5) for USART2 data reception.
"