cancel
Showing results for 
Search instead for 
Did you mean: 

HAL UART IT function

td
Associate II
Posted on December 15, 2015 at 14:21

Hi all, trying to use the UART interrupt fuction, i set up cube and got a close look at theSTM32Cube_FW_F4_V1.9.0\Projects\STM32F4-Discovery\Examples\UART\UART_TwoBoards_ComIT

this is my code, that does not work...

#define TXBUFFERSIZE 2
#define RXBUFFERSIZE 2
UART_HandleTypeDef huart2;
__IO ITStatus UartReady = RESET;
uint8_t *USART_Tx_Pointer;
uint8_t *USART_Rx_Pointer;
uint8_t USART_TxBuffer[7];
uint8_t USART_RxBuffer[7];
static void MX_USART2_UART_Init(void);
///////// main ////////
HAL_Init();
SystemClock_Config();
MX_USART2_UART_Init();
USART_TxBuffer[2] = 0x0A;
USART_TxBuffer[3] = 0x0A;
USART_Tx_Pointer = &USART_TxBuffer[0];
USART_Rx_Pointer = &USART_RxBuffer[0];
while (1)
{
if(HAL_UART_Transmit_IT(&huart2, USART_Tx_Pointer, TXBUFFERSIZE)!= HAL_OK)
{
//Error_Handler();
}
/*##-3- Wait for the end of the transfer ###################################*/ 
while (UartReady != SET)
{
////// stucks HERE
}
/* Reset transmission flag */
UartReady = RESET;
////////// Initialize //////////////
void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
__GPIOD_CLK_ENABLE();
////////
}
void MX_USART2_UART_Init(void)
{
huart2.Instance = USART2;
huart2.Init.BaudRate = 19200;
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;
HAL_UART_Init(&huart2);
}
void HAL_UART_MspInit(UART_HandleTypeDef* huart)
{
GPIO_InitTypeDef GPIO_InitStruct;
if(huart->Instance==USART2)
{
/* Peripheral clock enable */
__USART2_CLK_ENABLE();
/**USART2 GPIO Configuration 
PD5 ------> USART2_TX
PD6 ------> USART2_RX 
*/
GPIO_InitStruct.Pin = GPIO_PIN_5|GPIO_PIN_6;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF7_USART2;
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
/* Peripheral interrupt init*/
HAL_NVIC_SetPriority(USART2_IRQn, 0, 1);
HAL_NVIC_EnableIRQ(USART2_IRQn);
}
}

#usart
4 REPLIES 4
td
Associate II
Posted on December 16, 2015 at 12:47

I tried the HAL_UART_Receive, HAL_UART_Transmit and they send-receive data. although when transmiting it sends the LSB first, anyone know how to change the orfer of transmission?

ckim
Associate II
Posted on December 17, 2015 at 08:22

Hi, sending LSB first is the UART standard. Why don't you just swap the bits?

td
Associate II
Posted on December 21, 2015 at 13:44

Yes after reading a bit i realize LSB first is standard, why thought UART_IT function does not work ? I need the interrupt function for receive.

Is there ANY HELP for the HAL libraries... ??

rbenesh
Associate II
Posted on January 19, 2016 at 19:21

The HAL library has a receive function that requires the received interrupt is triggered only when the buffer size specified is completely full. If you debug, you may find it has received some data, but the interrupt has not been triggered. You could try setting up a ring buffer in the RX callback function. However, this would require setting the buffersize lower and letting the callback code you create piece the sections together. Best thing to do going forward is to ditch the HAL receive driver and make your own including a ring buffer implementation.