cancel
Showing results for 
Search instead for 
Did you mean: 

USART1 initialization problem

swer
Associate II

Hello,

I use stm32f303 discovery board and try to enable usart 1 as described in ..stm32cuvef3/projects/STM32303C_EVAL/uart/hyperterminalDma example.

When I set USART1 and it's relative pin and ports I see the output as expected but I am not able to send anything to the microcontroller. Receive interrupt doesn't even fire.

#define USARTx_TX_PIN                    GPIO_PIN_4
#define USARTx_TX_GPIO_PORT              GPIOC
#define USARTx_TX_AF                     GPIO_AF7_USART1
#define USARTx_RX_PIN                    GPIO_PIN_5
#define USARTx_RX_GPIO_PORT              GPIOC
#define USARTx_RX_AF                     GPIO_AF7_USART1

I try to swap the receive pin to PE1 which also has the alternative functionality of USART1

it doesn't trigger the interrupt, again.

Finally when I change the port to USART2 and its relative pins as..

#define USARTx_TX_PIN                    GPIO_PIN_2
#define USARTx_TX_GPIO_PORT              GPIOA
#define USARTx_TX_AF                     GPIO_AF7_USART2
#define USARTx_RX_PIN                    GPIO_PIN_3
#define USARTx_RX_GPIO_PORT              GPIOA
#define USARTx_RX_AF                     GPIO_AF7_USART2

in this way I can send and receive using dma as expected without problem.

What is the issue here?

Is there anybody who is faced the problem and solved it ?

Thanks in advance :grimacing_face:

2 REPLIES 2
AvaTar
Lead

Are you seeing incoming data on the RX pin, with a proper level ?

> Finally when I change the port to USART2 and its relative pins as..

I think you noticed that your definitions for UART1 utilize GPIOC, and that for UART2 use GPIOA.

Perhaps you access the wrong port somewhere (GPIO init, peripheral clock enable).

The full initialization code would be helpful here, too.

swer
Associate II

Yes the incoming signal levels are good in both ways.

I have done no edition on the code which st provided. It can be found in the stm32cubef3 code bundle.

What I don' t understand is that the code they provide is fully portable, I mean one can be use easily without doing large changes on the code. Only some define variables are needed to set to make the code work as you can see in my question.

this is the interrupt definitions of the code

  * @brief  This function handles DMA RX interrupt request.
  * @param  None
  * @retval None
  * @Note   This function is redefined in "main.h" and related to DMA stream
  *         used for USART data transmission
  */
void USARTx_DMA_RX_IRQHandler(void)
{
  HAL_DMA_IRQHandler(UartHandle.hdmarx);
}
 
/**
  * @brief  This function handles DMA TX interrupt request.
  * @param  None
  * @retval None
  * @Note   This function is redefined in "main.h" and related to DMA stream
  *         used for USART data reception
  */
void USARTx_DMA_TX_IRQHandler(void)
{
  HAL_DMA_IRQHandler(UartHandle.hdmatx);
}
 
/**
  * @brief  This function handles USARTx interrupt request.  
  * @param  None
  * @retval None
  * @Note   This function is redefined in "main.h" and related to DMA  
  *         used for USART data transmission     
  */
void USARTx_IRQHandler(void)
{
  HAL_UART_IRQHandler(&UartHandle);
}

uart initialize function

void HAL_UART_MspInit(UART_HandleTypeDef *huart)
{
  static DMA_HandleTypeDef hdma_tx;
  static DMA_HandleTypeDef hdma_rx;
 
  GPIO_InitTypeDef  GPIO_InitStruct;
 
  /*##-1- Enable peripherals and GPIO Clocks #################################*/
  /* Enable GPIO clock */
  USARTx_TX_GPIO_CLK_ENABLE();
  USARTx_RX_GPIO_CLK_ENABLE();
 
  /* Enable USARTx clock */
  USARTx_CLK_ENABLE();
 
  /* Enable DMA clock */
  DMAx_CLK_ENABLE();
 
  /*##-2- Configure peripheral GPIO ##########################################*/
  /* UART TX GPIO pin configuration  */
  GPIO_InitStruct.Pin       			 = USARTx_TX_PIN;
  GPIO_InitStruct.Mode      			 = GPIO_MODE_AF_PP;
  GPIO_InitStruct.Pull      			 = GPIO_PULLUP;
  GPIO_InitStruct.Speed     			 = GPIO_SPEED_FREQ_HIGH;  
  GPIO_InitStruct.Alternate 			 = USARTx_TX_AF;
  HAL_GPIO_Init(USARTx_TX_GPIO_PORT, &GPIO_InitStruct);
 
  /* UART RX GPIO pin configuration  */
  GPIO_InitStruct.Pin 						 = USARTx_RX_PIN;
  GPIO_InitStruct.Alternate 			 = USARTx_RX_AF;
  HAL_GPIO_Init(USARTx_RX_GPIO_PORT, &GPIO_InitStruct);
 
  /*##-3- Configure the DMA ##################################################*/
  /* Configure the DMA handler for Transmission process */
  hdma_tx.Instance                 = USARTx_TX_DMA_CHANNEL;
  hdma_tx.Init.Direction           = DMA_MEMORY_TO_PERIPH;
  hdma_tx.Init.PeriphInc           = DMA_PINC_DISABLE;
  hdma_tx.Init.MemInc              = DMA_MINC_ENABLE;
  hdma_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
  hdma_tx.Init.MemDataAlignment    = DMA_MDATAALIGN_BYTE;
  hdma_tx.Init.Mode                = DMA_NORMAL;
  hdma_tx.Init.Priority            = DMA_PRIORITY_LOW;
//	hdma_tx.XferCpltCallback				 = dma_tx_xfercmplt;
  HAL_DMA_Init(&hdma_tx);
 
  /* Associate the initialized DMA handle to the UART handle */
  __HAL_LINKDMA(huart, hdmatx, hdma_tx);
 
  /* Configure the DMA handler for reception process */
  hdma_rx.Instance                 = USARTx_RX_DMA_CHANNEL;
  hdma_rx.Init.Direction           = DMA_PERIPH_TO_MEMORY;
  hdma_rx.Init.PeriphInc           = DMA_PINC_DISABLE;
  hdma_rx.Init.MemInc              = DMA_MINC_ENABLE;
  hdma_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
  hdma_rx.Init.MemDataAlignment    = DMA_MDATAALIGN_BYTE;
  hdma_rx.Init.Mode                = DMA_NORMAL;
  hdma_rx.Init.Priority            = DMA_PRIORITY_HIGH;
//	hdma_rx.XferCpltCallback				 = dma_rx_xfercmplt;
	
  HAL_DMA_Init(&hdma_rx);
 
  /* Associate the initialized DMA handle to the the UART handle */
  __HAL_LINKDMA(huart, hdmarx, hdma_rx);
 
  /*##-4- Configure the NVIC for DMA #########################################*/
  /* NVIC configuration for DMA transfer complete interrupt (USARTx_TX) */
  HAL_NVIC_SetPriority(USARTx_DMA_TX_IRQn, 0, 1);
  HAL_NVIC_EnableIRQ	(USARTx_DMA_TX_IRQn			 );
 
  /* NVIC configuration for DMA transfer complete interrupt (USARTx_RX) */
  HAL_NVIC_SetPriority(USARTx_DMA_RX_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ	(USARTx_DMA_RX_IRQn			 );
 
  /* NVIC configuration for USART, to catch the TX complete */
  HAL_NVIC_SetPriority(USARTx_IRQn, 0, 1);
  HAL_NVIC_EnableIRQ	(USARTx_IRQn			);
}

and full usart definitions

		#define USARTx                           USART2
		#define USARTx_CLK_ENABLE()              __HAL_RCC_USART2_CLK_ENABLE();
		#define DMAx_CLK_ENABLE()                __HAL_RCC_DMA1_CLK_ENABLE()
		#define USARTx_RX_GPIO_CLK_ENABLE()      __HAL_RCC_GPIOA_CLK_ENABLE()
		#define USARTx_TX_GPIO_CLK_ENABLE()      __HAL_RCC_GPIOA_CLK_ENABLE()
 
		#define USARTx_FORCE_RESET()             __HAL_RCC_USART2_FORCE_RESET()
		#define USARTx_RELEASE_RESET()           __HAL_RCC_USART2_RELEASE_RESET()
 
 
		#define USARTx_TX_PIN                    GPIO_PIN_2
		#define USARTx_TX_GPIO_PORT              GPIOA
		#define USARTx_TX_AF                     GPIO_AF7_USART2
		#define USARTx_RX_PIN                    GPIO_PIN_3
		#define USARTx_RX_GPIO_PORT              GPIOA
		#define USARTx_RX_AF                     GPIO_AF7_USART2
 
 
		#define USARTx_TX_DMA_CHANNEL            DMA1_Channel7
		#define USARTx_RX_DMA_CHANNEL            DMA1_Channel6
 
 
		#define USARTx_DMA_TX_IRQn               DMA1_Channel7_IRQn
		#define USARTx_DMA_RX_IRQn               DMA1_Channel6_IRQn
		#define USARTx_DMA_TX_IRQHandler         DMA1_Channel7_IRQHandler
		#define USARTx_DMA_RX_IRQHandler         DMA1_Channel6_IRQHandler
 
		#define USARTx_IRQn                      USART2_IRQn
		#define USARTx_IRQHandler                USART2_IRQHandler

I set the pin and clock defines as respect to the datasheet and the cube program. So there should be something else to overlook

Thank you!