cancel
Showing results for 
Search instead for 
Did you mean: 

RFC, Receiving 1 byte at a time with UART6 and HAL, working

LMI2
Lead

For STM32F746 Discovery. This seems to work with buffers from 1 to 20 or so. If you have comments I am happy hear them. Unfortunately for UART6, because that is easily available in Discovery board.

I used UART_TwoBoards_ComIT example as a basis, so you need to replace its main.c with this.

#include "main.h"
UART_HandleTypeDef UartHandle;
__IO ITStatus UartReady = RESET;
__IO uint32_t UserButtonStatus = 0;  /* set to 1 after User Button interrupt  */
/* Buffer used for reception */
volatile uint8_t aRxBuffer[52];
volatile uint8_t total[300];
void SystemClock_Config(void);
static void Error_Handler(void);
 
static void CPU_CACHE_Enable(void);
volatile int counte;
int lask=0;
int aa=0;
int main(void)
{
  /* Enable the CPU Cache */
  CPU_CACHE_Enable();
 
  HAL_Init();
  SystemClock_Config();
  
  /* Configure LED1 */
  BSP_LED_Init(LED1);
counte=0;
  UartHandle.Instance        = USARTx;
  UartHandle.Init.BaudRate   = 9600;
  UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
  UartHandle.Init.StopBits   = UART_STOPBITS_1;
  UartHandle.Init.Parity     = UART_PARITY_NONE;
  UartHandle.Init.HwFlowCtl  = UART_HWCONTROL_NONE;
  UartHandle.Init.Mode       = UART_MODE_TX_RX;
  UartHandle.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT; 
  if(HAL_UART_DeInit(&UartHandle) != HAL_OK)
  {
    Error_Handler();
  }  
  if(HAL_UART_Init(&UartHandle) != HAL_OK)
  {
    Error_Handler();
  }
  UartReady = SET;
while (1)
{
 
  while (UartReady == SET)
  {
		 if(HAL_UART_Receive_IT(&UartHandle, (uint8_t *)aRxBuffer, 1) != HAL_OK)
  {
    Error_Handler();
  }
 
  UartReady = RESET;
  BSP_LED_Toggle(LED1); 
  }  
while(counte>27)
{	
    BSP_LED_Toggle(LED1); 
    HAL_Delay(100); 
  /* Infinite loop */
}
}
}
void SystemClock_Config(void)
{
  RCC_ClkInitTypeDef RCC_ClkInitStruct;
  RCC_OscInitTypeDef RCC_OscInitStruct;
  HAL_StatusTypeDef ret = HAL_OK;
 
  /* Enable HSE Oscillator and activate PLL with HSE as source */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLM = 25;
  RCC_OscInitStruct.PLL.PLLN = 432;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLQ = 9;
  
  ret = HAL_RCC_OscConfig(&RCC_OscInitStruct);
  if(ret != HAL_OK)
  {
    while(1) { ; }
  }
  
  
  ret = HAL_PWREx_EnableOverDrive();
  if(ret != HAL_OK)
  {
    while(1) { ; }
  }
  
  RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;  
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2; 
  
  ret = HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_7);
  if(ret != HAL_OK)
  {
    while(1) { ; }
  }  
}
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *UartHandle)
{
  /* Set transmission flag: transfer complete */
 
}
 
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *UartHandle)
{
	while  (aa<1)	
{
total[counte]=aRxBuffer[aa];
counte++;  
aa++;
}
UartReady = SET;
aa=0;
}
void HAL_UART_ErrorCallback(UART_HandleTypeDef *UartHandle)
 
	{
  /* Set transmission flag: transfer complete */
       HAL_Delay(1000);
    BSP_LED_Toggle(LED1); 
}
 
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
  if(GPIO_Pin == KEY_BUTTON_PIN)
  {  
    UserButtonStatus = 1;
  }
}
static void Error_Handler(void)
{
  /* Turn LED1 on */
  BSP_LED_On(LED1);
  while(1)
  {
    /* Error if LED1 is slowly blinking (1 sec. period) */
    BSP_LED_Toggle(LED1); 
    HAL_Delay(1000); 
  }  
}
static void CPU_CACHE_Enable(void)
{
  /* Enable I-Cache */
  SCB_EnableICache();
 
  /* Enable D-Cache */
  SCB_EnableDCache();
}
 
void assert_failed(uint8_t* file, uint32_t line)
{ 
  /* Infinite loop */
  while (1)
  {
  }
}

In time I plan to test end of line /n to catch whole sentences but that is an other story. And finally, using a loop of one is silly, I know.

4 REPLIES 4
LMI2
Lead

Not much comments. When I searched for receiving with UART and using HAL API, I found only a lot of questions but not much good answers. This works. But still I would like hear if some else has tried this.

Your buffer/method looks to be poorly bounded, I'd approach this differently.

>>Not much comments.

I get tired of repeating myself. Once upon a time I had a thread "GPS NMEA Decoder Demo for STM32F746G-DISCO" and a similar one for the L4 that demonstrated an efficient interrupt based line parser.

The forum migration seems to have destroyed a lot of content. The HAL USART implementation is overly heavy and convoluted, when it is easier to code a better solution, one usually does.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
T J
Lead

Check the F091 thread, I showed some Uart code there...

> The HAL USART implementation is overly heavy and convoluted, when it is easier to code a better solution, one usually does.

Which makes more experienced users ignore threads related to HAL/Cube - based UART code.