Skip to main content
mahmoud boroumand
Associate III
March 11, 2018
Question

Uart recive

  • March 11, 2018
  • 6 replies
  • 1734 views
Posted on March 11, 2018 at 14:33

hello

i use stm32f107 and use to standard standard peripheral library but now i have use hal drive and i new on it.

i want to build a transmitter receiver.i want when receive data in mcu  i get an interrupt .when i use

standard peripheral library i don't have problem and i get interrupt every a byte . i use this code  but i get interrupt only one time 

how can i fix that?

GPIO_InitTypeDef GPIO_InitStruct;

/* USER CODE END USART2_MspInit 0 */

/* Peripheral clock enable */

__HAL_RCC_USART2_CLK_ENABLE();

__HAL_RCC_GPIOA_CLK_ENABLE();

/**USART2 GPIO Configuration

PA2 ------> USART2_TX

PA3 ------> USART2_RX

*/

GPIO_InitStruct.Pin = GPIO_PIN_2;

GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;

HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

GPIO_InitStruct.Pin = GPIO_PIN_3;

GPIO_InitStruct.Mode = GPIO_MODE_INPUT;

GPIO_InitStruct.Pull = GPIO_NOPULL;

HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

/* USART2 interrupt Init */

HAL_NVIC_SetPriority(USART2_IRQn, 1, 0);

HAL_NVIC_EnableIRQ(USART2_IRQn);

huart2.Instance = USART2;

huart2.Init.BaudRate = 115200;

huart2.Init.WordLength = UART_WORDLENGTH_8B;

huart2.Init.StopBits = UART_STOPBITS_1;

huart2.Init.Parity = UART_PARITY_NONE;

huart2.Init.Mode = UART_MODE_TX_RX;

huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;

huart2.Init.OverSampling = UART_OVERSAMPLING_16;

if (HAL_UART_Init(&huart2) != HAL_OK)

{

_Error_Handler(__FILE__, __LINE__);

}

if(HAL_UART_Receive_IT(&huart2, RxBuffer1 , 2) != HAL_OK)

{

Error_Handler();

}

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *UartHandle)

{

///////Receive Data Here 

}

void USART2_IRQHandler()

{

HAL_UART_IRQHandler(&huart2);

}

    This topic has been closed for replies.

    6 replies

    T J
    Senior III
    March 11, 2018
    Posted on March 11, 2018 at 21:17

    You have to re-enable the interrupt., it becomes cumbersome/slow and not efficient.

    I don't do it that way, I use a circular DMA buffer for receive.

    You can monitor the DMA pointer to see if a byte has arrived.

    I check every mS,

    Did you use the Cube ?

    mahmoud boroumand
    Associate III
    March 12, 2018
    Posted on March 12, 2018 at 06:53

    i can't use dma because i use for another purpose .

    yes i use cube example

    T J
    Senior III
    March 12, 2018
    Posted on March 12, 2018 at 06:55

    the only DMA has 12  channels;

    from the datasheet;

    • DMA: 12-channel DMA controller

    – Supported peripherals: timers, ADCs, DAC,

    I2Ss, SPIs, I2Cs and USARTs
    mahmoud boroumand
    Associate III
    March 12, 2018
    Posted on March 12, 2018 at 09:03

    Thanks