cancel
Showing results for 
Search instead for 
Did you mean: 

hi how to implement fifo with usart in stm32f0308

SSRIN.2
Associate III

Iam new to stm32 microcontrollers, i have to read uart continuously and store to array. and size of the data received from uart is variable. if i use the fifo with uart i can solve problem.

so please help me how program to receive data from uart in fifo

18 REPLIES 18
SSRIN.2
Associate III

actuall when i press button controller sends bytes like ee b1 10 00 01 00 01 1b 01 ff fc ff ff

when i press button each time i receive bytes like this ee b1 10 00 01 00 01 1b 01 ff fc ff ff

i have to copy above bytes to an array and compare.

some times i receive 15 byets and 20 bytes.

here 13bytes data is ee b1 10 00 01 00 01 1b 01 ff fc ff ff

15 bytes data is EE B1 10 00 01 00 04 11 10 00 00 3D FF FC FF FF

20 BYTES DATA IS EE B1 10 00 01 00 08 11 E5 48 00 F0 BF 6B 5F 5F FF FC FF FF FF

whatever the size i receive i have store like location 0 to location 19

if 20 bytes received means i have to store like this

ARR[0]=EE

ARR[1]=B1

ARR[2]=10

ARR[3]=00

ARR[4]=01

ARR[5]=00

ARR[6]=08

ARR[7]=11

ARR[8]=E5

ARR[9]=48

ARR[10]=00

ARR[11]=F0

ARR[12]=BF

ARR[13]=6B

ARR[14]=5F

ARR[15]=5F

ARR[16]=FF

ARR[17]=FC

ARR[18]=FF

ARR[19]=FF

if 15 bytes received in same array it should store like this

ARR[0]=EE

ARR[1]=B1

ARR[2]=10

ARR[3]=00

ARR[4]=01

ARR[5]=00

ARR[6]=04

ARR[7]=11

ARR[8]=10

ARR[9]=00

ARR[10]=00

ARR[11]=36

ARR[12]=FF

ARR[13]=FC

ARR[14]=FF

ARR[15]=FF

please help me to tackle above

 

@SSRIN.2 "iam using HAL_UART_Receive_DMA(&huart1, rxbuff,20)"

How, exactly, are you using it? Post your code.

Did you see the guide that @KnarfB​ linked? That covers DMA...

0693W000008xsqBQAQ.png

Fill the array in a byte-wise fashion, using ARR[6] byte, as it arrives, to determine the overall length of the packet, and how many bytes remain to complete the frame.

As each frame is completed move it to a holding buffer for processing whilst you continue to collect data for the next frame.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2022 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include<string.h>
#include "stdio.h"
 
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
 
/* USER CODE END Includes */
 
/* Private typedef -----------------------------------------------------------*/
/* USER CO
 
#define rxbuff_size 20
uint8_t mainbuff[mainbuff_size];
uint8_t rxbuff[rxbuff_size];
 
//uint8_t compa;
/* 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 ---------------------------------------------------------*/
 UART_HandleTypeDef huart1;
UART_HandleTypeDef huart2;
 
/* USER CODE BEGIN PV */
 
/* USER CODE END PV */
 
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART1_UART_Init(void);
static void MX_USART2_UART_Init(void);
/* USER CODE BEGIN PFP */
 
/* USER CODE END PFP */
 
/* Private user code ---------------------------------------------------------*/
 
 
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
	HAL_UART_Receive_DMA(&huart1,rxbuff,20);
			memcpy(mainbuff,rxbuff,rxbuff_size);
}
/* 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_USART1_UART_Init();
  MX_USART2_UART_Init();
  /* USER CODE BEGIN 2 */
  HAL_UART_Receive_DMA(&huart1,rxbuff,20);
  /* USER CODE END 2 */
 
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* 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};
  RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
 
  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  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.PLLMUL = RCC_PLL_MUL12;
  RCC_OscInitStruct.PLL.PREDIV = RCC_PREDIV_DIV1;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
 
  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
 
  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  {
    Error_Handler();
  }
  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1;
  PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK1;
  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  {
    Error_Handler();
  }
}
 
/**
  * @brief USART1 Initialization Function
  * @param None
  * @retval None
  */
static void MX_USART1_UART_Init(void)
{
 
  /* USER CODE BEGIN USART1_Init 0 */
 
  /* USER CODE END USART1_Init 0 */
 
  /* USER CODE BEGIN USART1_Init 1 */
 
  /* USER CODE END USART1_Init 1 */
  huart1.Instance = USART1;
  huart1.Init.BaudRate = 19200;
  huart1.Init.WordLength = UART_WORDLENGTH_8B;
  huart1.Init.StopBits = UART_STOPBITS_1;
  huart1.Init.Parity = UART_PARITY_NONE;
  huart1.Init.Mode = UART_MODE_TX_RX;
  huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  if (HAL_UART_Init(&huart1) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN USART1_Init 2 */
 
  /* USER CODE END USART1_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 = 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;
  huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  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_GPIOC_CLK_ENABLE();
  __HAL_RCC_GPIOA_CLK_ENABLE();
 
  /*Configure GPIO pins : TEETH_SELECTION_Pin AGE_SELECTION_Pin UP_Pin DOWN_Pin
                           MODE_SELECTION_Pin */
  GPIO_InitStruct.Pin = TEETH_SELECTION_Pin|AGE_SELECTION_Pin|UP_Pin|DOWN_Pin
                          |MODE_SELECTION_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  HAL_GPIO_Init(GPIOA, &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 */
  __disable_irq();
  while (1)
  {
  }
  /* 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,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

Johi
Senior III

Or you use the fifo's in the UART or you use DMA, both together can create trouble.

Now problem with DMA can be that you do not know how many bytes you are about to receive. Therefore, for uart there is   HAL_UARTEx_ReceiveToIdle_DMA(m_pUart, m_dmaRxBuf, UART_RX_BUF_SIZE); receive to idle does what it says: receive until idle is detected on your UART. Using this will save you the trouble created by various lengths.

Best regards,

Johi.

can you please suggest me any example code

@Johi​  "use the fifo's in the UART"

Does an STM32F030R8T6 have any FIFOs in the UART?

0693W00000QLEDjQAP.png

SSRIN.2
Associate III

"how to implement fifo with usart in stm32f0308"

Hi,

for above uart problem solved, I used IDLE DMA UART RECEPTION, in this data reception with different sizes is resolved

i used HAL_UARTEx_ReceiveToIdle_DMA for data reception.

but in this, data reception UART stops suddenly. i didnt understand why this happening

please help me how to solve this "UART STOPS RECEIVE DATA". if we reset device it starts working

@SSRIN.2​ "above uart problem solved"

That's good - now please mark the solution (see below).

"please help me how to solve this "UART STOPS RECEIVE DATA"

It seems you've started a separate thread on that:

https://community.st.com/s/question/0D53W00001gvnRhSAI/hi-everyonewhy-uart-using-dma-stops-receiving-data-suddenly-in-idle-mode

0693W000008y9fZQAQ.png