cancel
Showing results for 
Search instead for 
Did you mean: 

Uart recive

mahmoud boroumand
Associate III
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);

}

6 REPLIES 6
T J
Lead
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 ?

Posted on March 12, 2018 at 06:53

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

yes i use cube example

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
Posted on March 12, 2018 at 07:39

can you give me an example with DMA circular ?

Posted on March 12, 2018 at 07:49

you have to configure it in the cube

this is not trivial for novice,

I cant do all the work for you, but here are some pointers from the '091 and '767 processes..

in the cube eonfiguration tab, Enable Rx DMA for circular buffer, enable DMA interrupt and Usart interrupt.

(I use TxDMA as a normal buffer.)

void initUart3RxDMABuffer(void) {

    if (HAL_UART_Receive_DMA(&huart4, (uint8_t *)Usart3RxDMABuffer, U3RxBufSize) != HAL_OK)

    {

        // Transfer error in reception process

        //_Error_Handler(__FILE__, __LINE__);

        printf(string, 'initUart3RxDMABuffer Failed\n');

    }

    else

        printf(string, 'initUart3RxDMABuffer OK!\n');

}

char readU3(void) {

    char readByte = Usart3RxDMABuffer[U3RxBufferPtrOUT++];

    if (U3RxBufferPtrOUT >= U3RxBufSize) U3RxBufferPtrOUT = 0;

    return readByte;

}

char readableU3(void) {

    U3RxBufferPtrIN =  U3RxBufSize - huart4.hdmarx->Instance->CNDTR;

    return U3RxBufferPtrIN - U3RxBufferPtrOUT;

}

I poll the RxDMA buffer size in my foreground loop, every mS

   if(readableU3()) // have at least one byte available in RxDMA buffer.

mahmoud boroumand
Associate III
Posted on March 12, 2018 at 09:03

Thanks