cancel
Showing results for 
Search instead for 
Did you mean: 

HAL_UART_Receive_DMA() not able to receive data in the array or in the SRAM

AVerm.4
Associate III

Hi,

I'm trying to receive data from UART peripheral to DMA using HAL Library. I'm using STM32F410RB.

I tried all the possible configuration as in reference manual -

dma3.png

Code part -

//#define OFFSET 0x800

//#define DEST_ADDR (SRAM1_BASE + OFFSET) //Tried writing to SRAM


static void MX_USART2_UART_Init(void)

{

huart2.Instance = USART2;

huart2.Init.BaudRate = 115200; //Tried with other Baud rates

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();

}

}


static void MX_DMA_Init(void)

{

/* DMA controller clock enable */

__HAL_RCC_DMA1_CLK_ENABLE();

 

/* DMA interrupt init */

/* DMA1_Stream7_IRQn interrupt configuration */

HAL_NVIC_SetPriority(DMA1_Stream7_IRQn, 0, 0); //Tried Stream5 as in RM

HAL_NVIC_EnableIRQ(DMA1_Stream7_IRQn);

 

}



void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)

{

UNUSED(huart);

}




int main(void)

{

 

/* MCU Configuration--------------------------------------------------------*/

 

/* Reset of all peripherals, Initializes the Flash interface and the Systick. */

HAL_Init();

 

SystemClock_Config();

 

/* Initialize all configured peripherals */

MX_GPIO_Init();

MX_USART2_UART_Init();

MX_DMA_Init();

while (1)

{

uint8_t data[255]={0};

HAL_UART_Receive_DMA(&huart2, data, 255);

}

}

Terminal setting -

term_set.png

 

17 REPLIES 17

please make this 

HAL_UART_Receive_DMA(&huart2, data, 255);

as

 

HAL_UART_Receive_DMA(&huart2, data, 4);

 

in your while(1){

if(flag == 1)

{

flag = 0;

HAL_UART_Transmit(&huart2, data, 4, 1000);

HAL_UART_Receive_DMA(&huart1, data, 4);

}

 

Connect to 

If you feel a post has answered your question, please click "Accept as Solution".

https://controllerstech.com/stm32-uart-4-receive-data-using-dma/

If you feel a post has answered your question, please click "Accept as Solution".

this part was missing in your code and an empty while loop as per the example quoted by you.. how do you test if you have received the characters, do you use debugger?

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
    HAL_UART_Transmit(&huart1, UART1_rxBuffer, 12, 100);
    HAL_UART_Receive_DMA(&huart1, UART1_rxBuffer, 12);
}

 

STM3

If you feel a post has answered your question, please click "Accept as Solution".

Ok. What i'm doing is sending the bytes through the teraterm on my pc. and trying to read it at any memory location. i tried 2-3 different configurations. i began with this -

 

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
    HAL_UART_Transmit(&huart1, UART1_rxBuffer, 12, 100);
    HAL_UART_Receive_DMA(&huart1, UART1_rxBuffer, 12);
}

 

then i defined a SRAM with an offset -

 

#define OFFSET 0x800
#define DATABUFF (SRAM1_BASE+OFFSET)

 

tried with different UART configs -

 

static void MX_USART2_UART_Init(void)
{
huart2.Instance = USART2;
huart2.Init.BaudRate = 115200; //Tried with other Baud rate - 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();
}
}

 

different stream -

 

static void MX_DMA_Init(void)
{
/* DMA controller clock enable */
__HAL_RCC_DMA1_CLK_ENABLE();
/* DMA interrupt init */
/* DMA1_Stream7_IRQn interrupt configuration */
HAL_NVIC_SetPriority(DMA1_Stream7_IRQn, 0, 0); //Tried Stream5 as in RM
HAL_NVIC_EnableIRQ(DMA1_Stream7_IRQn);
}

 

and what @Techn mentioned - to add a flag, also shortened the data[255] to data [10]. Only thing is to understand the 
https://github.com/STMicroelectronics/STM32CubeF4/blob/master/Projects/STM32F410xx-Nucleo/Examples/UART/UART_TwoBoards_ComDMA/Src/main.c 

 

not able to capture with this though led blink is working-

#include "main.h"
#include "string.h"

//#define OFFSET 0x800
//#define DEST_ADDR (SRAM1_BASE + OFFSET)

UART_HandleTypeDef huart2;
DMA_HandleTypeDef hdma_usart2_rx;

void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_DMA_Init(void);
static void MX_USART2_UART_Init(void);

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart);

//uint8_t UART2_rxBuffer[12] = {0};
//uint8_t data[10]={0};
uint8_t RxData[256];
uint8_t FinalBuf[4096];

int FTC=0;

uint32_t size = 0;
uint32_t indx = 0;
int isSizeRxed = 0;

int main(void)
{
  HAL_Init();
  SystemClock_Config();

  MX_GPIO_Init();
  MX_DMA_Init();
  MX_USART2_UART_Init();

  HAL_UART_Receive_DMA(&huart2, RxData, 256);

  while (1)
  {
  if (((size-indx)>0) && ((size-indx)<128)){
      if (FTC==1)
	{
	  strcpy ((char *)FinalBuf+indx, (char *)RxData);
	  indx = size;
	  isSizeRxed = 0;
	  FTC = 0;
	  HAL_UART_DMAStop(&huart2);
	  HAL_UART_Receive_DMA(&huart2, RxData, 256);
	}
     }
	  HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
	  HAL_Delay(1000);
  }
//  HAL_UART_RxCpltCallback(&huart2);
}

void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  __HAL_RCC_PWR_CLK_ENABLE();
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

  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 = 100;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLQ = 4;
  RCC_OscInitStruct.PLL.PLLR = 2;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }

  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_DIV2;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3) != HAL_OK)
  {
    Error_Handler();
  }
}

static void MX_USART2_UART_Init(void)
{
  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();
  }
}

/**
  * Enable DMA controller clock
  */
static void MX_DMA_Init(void)
{

  /* DMA controller clock enable */
  __HAL_RCC_DMA1_CLK_ENABLE();

  /* DMA interrupt init */
  /* DMA1_Stream5_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(DMA1_Stream5_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(DMA1_Stream5_IRQn);

}

/**
  * @brief GPIO Initialization Function
  * @PAram None
  * @retval None
  */
static void MX_GPIO_Init(void)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};

  __HAL_RCC_GPIOC_CLK_ENABLE();
  __HAL_RCC_GPIOH_CLK_ENABLE();
  __HAL_RCC_GPIOA_CLK_ENABLE();
  __HAL_RCC_GPIOB_CLK_ENABLE();

  HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_RESET);

  GPIO_InitStruct.Pin = B1_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct);

  /*Configure GPIO pin : LD2_Pin */
  GPIO_InitStruct.Pin = LD2_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(LD2_GPIO_Port, &GPIO_InitStruct);

/* USER CODE BEGIN MX_GPIO_Init_2 */
/* USER CODE END MX_GPIO_Init_2 */
}

/* USER CODE BEGIN 4 */
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
	memcpy (FinalBuf+indx, RxData+128, 128);
	memset(RxData+128, '\0', 128);
	indx += 128;
	FTC=1;
}
/* 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 */

teraterm-

tt2.png

 

Still not answering that question: 

How much data are you sending from the PC to the MCU? 255 chars?

HAL_UART_Receive_DMA(&huart2, data, 255);

The Rx callback never fired till you receive all that 255 bytes packet.

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.

Yes i reduced it to 10 chars for convenience and sent 10 chars from Teraterm 

I suggest you begun with simple example to echo the characters. Then you buildup on that. This method help both we and you to be in sync during discussion.  

If you feel a post has answered your question, please click "Accept as Solution".