cancel
Showing results for 
Search instead for 
Did you mean: 

HELP UART HM10 dynamic size data receive

ISHA
Associate

HELLO, 

i am new to STM, i am stuck in receiving dynamic size data from HM10 to stm32f407VG over UART2 communication , i tried  HAL_UART_Receive_IT(&huart2, &UART2_rxBuffer[rxIndex], 1); but not able to receive multiple char as 3rd part of this HAL_UART_Receive_IT(&huart2, &UART2_rxBuffer[rxIndex], 1); is 1 if i make it 12 then if i recive 12 char then data is visible and i want to receive any size of data string . 

please help

THANKS

 

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2024 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under BSD 3-Clause license,
  * the "License"; You may not use this file except in compliance with the
  * License. You may obtain a copy of the License at:
  *                        opensource.org/licenses/BSD-3-Clause
  *
  ******************************************************************************
  */
/* USER CODE END Header */
 
/* Includes ------------------------------------------------------------------*/
#include "main.h"
 
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <string.h>
 
#include"liquidcrystal_i2c.h"
//#include "stdio.h"
 
/* USER CODE END Includes */
 
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
 
/* USER CODE END PTD */
 
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
 
/* USER CODE END PD */
 
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
 
/* USER CODE END PM */
 
/* Private variables ---------------------------------------------------------*/
I2C_HandleTypeDef hi2c1;
 
UART_HandleTypeDef huart2;
 
/* USER CODE BEGIN PV */
uint8_t UART2_rxBuffer[2] = {0};//should be 4 if sending isha
#define RX_BUFFER_SIZE 100
uint8_t data1Received = 0;
uint8_t data2Received = 0;
//#define RX_BUFFER_SIZE 50
//char rxBuffer[RX_BUFFER_SIZE];
//uint8_t rxIndex = 0;
//volatile uint8_t rxComplete = 0;
//#define LED12_PIN GPIO_PIN_12
//#define LED15_PIN GPIO_PIN_15
//char Rx_data[25] = "";
/* USER CODE END PV */
 
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART2_UART_Init(void);
static void MX_I2C1_Init(void);
/* USER CODE BEGIN PFP */
 
/* USER CODE END PFP */
 
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
//uint8_t Data[64];
//void Delay_ms(volatile int time_ms);
 
//void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
//{
// if (huart->Instance == USART2)
// {
// HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_SET);
// //HAL_UART_Transmit(&huart2, Data, Size, 1000);
// //HAL_UART_Transmit(&huart2, Data, Size, 1000);
// }
//// else if (huart2->Instance == USART2)
//// {
////
//// //HAL_UART_Transmit(&huart1, Data, Size, 1000);
//// }
// //HAL_UART_Receive_IT(huart2, Data, 64);
// HAL_UART_Receive_IT(huart, Data, 64);
//}
 
//void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart)
//{
//    // Handle UART errors here
//    // Print error message on LCD
//    HD44780_Clear();
//    HD44780_SetCursor(0, 0);
//    HD44780_PrintStr("UART error");
//}
 
 
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
HAL_UART_Transmit(&huart2, UART2_rxBuffer, 2, 50);//
   // HAL_UART_Receive_IT(&huart2, UART2_rxBuffer, 2);//should be 4 if sending data
HD44780_Clear();
    HD44780_SetCursor(0,0);
    HD44780_PrintStr("gio");
    if (strncmp((char *)UART2_rxBuffer, "ON", 2) == 0)
            {
                // Turn on LED connected to pin 15
                HAL_GPIO_WritePin(GPIOD, GPIO_PIN_15, GPIO_PIN_SET);
    //data1Received = 1; // Set flag for "isha" received
    //data2Received = 0; // Clear flag for "jeet" received
            }
            // Check if "jeet" is received
            else if (strncmp((char *)UART2_rxBuffer, "OF", 2) == 0)
            {
                // Turn on LED connected to pin 14
            HAL_GPIO_WritePin(GPIOD, GPIO_PIN_15, GPIO_PIN_RESET);
                //HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, GPIO_PIN_SET); // ID if task is simple
            //data2Received = 1; // Set flag for "jeet" received// ID IF task is time taking
            //data1Received = 0; // Clear flag for "isha" received // ID IF task is time taking
            }
    if (huart->Instance == USART2)
    {
    HD44780_Clear();
    HD44780_SetCursor(0,0);
    HD44780_PrintStr("instance");
 
        //HD44780_Clear();
        HD44780_SetCursor(0, 1);
        HD44780_PrintStr("Received: ");
        HD44780_PrintStr((char *)UART2_rxBuffer);
 
    }
    HAL_UART_Receive_IT(&huart2, UART2_rxBuffer, 2);//should be 4 if sending data
}
 
//void toggleLEDs(void)
//{
//    // Toggle LED 15 if "isha" received
//    if (data1Received)
//    {
//        HAL_GPIO_TogglePin(GPIOD, GPIO_PIN_15);
//    }
//
//    // Toggle LED 14 if "jeet" received
//    if (data2Received)
//    {
//        HAL_GPIO_TogglePin(GPIOD, GPIO_PIN_14);
//    }
//}
 
/* USER CODE END 0 */
 
/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */
 
  /* USER CODE END 1 */
  
 
  /* MCU Configuration--------------------------------------------------------*/
 
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();
 
  /* USER CODE BEGIN Init */
 
  /* USER CODE END Init */
 
  /* Configure the system clock */
  SystemClock_Config();
 
  /* USER CODE BEGIN SysInit */
 
  /* USER CODE END SysInit */
 
  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_USART2_UART_Init();
  MX_I2C1_Init();
  /* USER CODE BEGIN 2 */
  HAL_UART_Receive_IT (&huart2, UART2_rxBuffer, 2);
  HD44780_Init(2);
        HD44780_Clear();
        HD44780_SetCursor(0,0);
        HD44780_PrintStr("HELLO");
        HAL_Delay(200);
  /* USER CODE END 2 */
 
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
//   toggleLEDs(); // Toggle LEDs based on received strings
//
//             // Delay for 500 milliseconds (0.5 second)
//             HAL_Delay(500);
//
//             // Repeat for a total of 6 times to create a 3-second toggle
//             for (int i = 0; i < 6; i++)
//             {
//                 toggleLEDs(); // Toggle LEDs based on received strings
//
//                 // Delay for 500 milliseconds (0.5 second)
//                 HAL_Delay(100);
//             }
//
//             // Clear the flags after 3 seconds
//           data1Received = 0;
//         data2Received = 0;
//      if (rxComplete) {
//          rxComplete = 0; // Reset flag
//
//          // Process received data
//          if (strcmp(rxBuffer, "hello") == 0) {
//              HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_SET); // Turn on LED12
//              HAL_Delay(1000); // Delay for 1 second
//              HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_RESET); // Turn off LED12
//          }
//          else if (strcmp(rxBuffer, "hii") == 0) {
//              HAL_GPIO_WritePin(GPIOD, GPIO_PIN_15, GPIO_PIN_SET); // Turn on LED15
//              HAL_Delay(1000); // Delay for 1 second
//              HAL_GPIO_WritePin(GPIOD, GPIO_PIN_15, GPIO_PIN_RESET); // Turn off LED15
//          }
//
//          // Clear the buffer
//          memset(rxBuffer, 0, sizeof(rxBuffer));
//          rxIndex = 0;
//
//          // Start another reception
//          HAL_UART_Receive_IT(&huart2, (uint8_t*)&rxBuffer[rxIndex], 1);
//      }
 
//   HAL_UART_Receive(&huart2,(uint8_t*)rxBuffer,RX_BUFFER_SIZE,100);
//   printf(" %s\n", rxBuffer);
//      if (strcmp(rxBuffer, "hello") == 0) {
//          HAL_GPIO_WritePin(GPIOD, LED12_PIN, GPIO_PIN_SET); // Turn on LED12
//          HAL_Delay(1000); // Delay for 1 second
//          HAL_GPIO_WritePin(GPIOD, LED12_PIN, GPIO_PIN_RESET); // Turn off LED12
//      }
//      else if (strcmp(rxBuffer, "hii") == 0) {
//          HAL_GPIO_WritePin(GPIOD, LED15_PIN, GPIO_PIN_SET); // Turn on LED15
//          HAL_Delay(1000); // Delay for 1 second
//          HAL_GPIO_WritePin(GPIOD, LED15_PIN, GPIO_PIN_RESET); // Turn off LED15
//      }
    /* USER CODE END WHILE */
 
    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}
 
/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
 
  /** Configure the main internal regulator output voltage 
  */
  __HAL_RCC_PWR_CLK_ENABLE();
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  /** Initializes the CPU, AHB and APB busses clocks 
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  RCC_OscInitStruct.PLL.PLLM = 8;
  RCC_OscInitStruct.PLL.PLLN = 168;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLQ = 4;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
  /** Initializes the CPU, AHB and APB busses clocks 
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |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;
 
  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
  {
    Error_Handler();
  }
}
 
/**
  * @brief I2C1 Initialization Function
  * @PAram None
  * @retval None
  */
static void MX_I2C1_Init(void)
{
 
  /* USER CODE BEGIN I2C1_Init 0 */
 
  /* USER CODE END I2C1_Init 0 */
 
  /* USER CODE BEGIN I2C1_Init 1 */
 
  /* USER CODE END I2C1_Init 1 */
  hi2c1.Instance = I2C1;
  hi2c1.Init.ClockSpeed = 100000;
  hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
  hi2c1.Init.OwnAddress1 = 0;
  hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  hi2c1.Init.OwnAddress2 = 0;
  hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  if (HAL_I2C_Init(&hi2c1) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN I2C1_Init 2 */
 
  /* USER CODE END I2C1_Init 2 */
 
}
 
/**
  * @brief USART2 Initialization Function
  * @PAram None
  * @retval None
  */
static void MX_USART2_UART_Init(void)
{
 
  /* USER CODE BEGIN USART2_Init 0 */
 
  /* USER CODE END USART2_Init 0 */
 
  /* USER CODE BEGIN USART2_Init 1 */
 
  /* USER CODE END USART2_Init 1 */
  huart2.Instance = USART2;
  huart2.Init.BaudRate = 9600;
  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();
  }
  /* USER CODE BEGIN USART2_Init 2 */
 
  /* USER CODE END USART2_Init 2 */
 
}
 
/**
  * @brief GPIO Initialization Function
  * @PAram None
  * @retval None
  */
static void MX_GPIO_Init(void)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};
 
  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOH_CLK_ENABLE();
  __HAL_RCC_GPIOA_CLK_ENABLE();
  __HAL_RCC_GPIOD_CLK_ENABLE();
  __HAL_RCC_GPIOB_CLK_ENABLE();
 
  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15, GPIO_PIN_RESET);
 
  /*Configure GPIO pins : PD12 PD13 PD14 PD15 */
  GPIO_InitStruct.Pin = GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
 
}
 
/* USER CODE BEGIN 4 */
 
/* USER CODE END 4 */
 
/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
 
  /* USER CODE END Error_Handler_Debug */
}
 
#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @PAram  file: pointer to the source file name
  * @PAram  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
 
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

 

2 REPLIES 2
Karl Yamashita
Lead II

Use code insert so your code is formatted for easy reading.

Code Snippet.jpg

If you're receiving strings, hopefully with a LF, you can use a ring buffer/queue to save what you received. Then you can parse the message outside of the interrupt. Right now you're doing way to much processing inside the interrupt so you have a high chance of missing the next few bytes.

See this project https://github.com/karlyamashita/ParseRawData/wiki

 

Or you can use the Idle interrupt using the DMA

https://github.com/karlyamashita/Nucleo-G431RB_Three_UART/wiki

 

If you find my answers useful, click the accept button so that way others can see the solution.

How are you going to determine the length/end of the input? Construct a line buffer holding that until the end, then process.

Avoid doing all this time wasting stuff in the interrupt or callback. If it take more than a byte-time you run the risk of losing data or overrunning.

The STM32F4 is going to interrupt for each byte

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