cancel
Showing results for 
Search instead for 
Did you mean: 

Get value from a buffer

HØdeg.1
Associate

I have an ADC input that is read continuously and put in a buffer by the help of DMA. This is my buffer:

uint16_t adc_buf[ADC_BUF_LEN];        //ADC_BUF_LEN = 1

The value in the buffer is replaced continuously by the NEW sampled value from the ADC.

I want to use this value in the buffer, so how can I get out the value. I was thinking something like this:

int value = add_buf[0]; 

and then I can use the variable "value" for stuff, like printing it to the terminal on my computer or do calculations. But how do I do this? I am not able to print the value to the serial terminal on my computer...

1 ACCEPTED SOLUTION

Accepted Solutions
elso
Associate III

A simple way to code the behaviour you wanted.

/* USER CODE BEGIN PD */
#define ADC_BUF_FULL_LEN 4096 //65535 is max due to DMA buffer limitations
/* USER CODE END PD */

/* USER CODE BEGIN PV */
uint8_t adc_buf[ADC_BUF_LEN]; //Or uint32_t, uint16_t etc. depending on your ADC data.
volatile bool adc_buf_half_full = false;
uint8_t wtext[10]; 
/* USER CODE END PV */

/* USER CODE BEGIN 2 */
HAL_ADC_Start_DMA(&hadc3, (uint32_t*)adc_buf, ADC_BUF_LEN);
while(!adc_buf_half_full); //Wait for interrupt
sprintf(wtext, "Data: %u", adc_buf[0]);
HAL_UART_Transmit(&huart3, (uint8_t *)wtext, strlen(wtext), 1000);
/* USER CODE END 2 */

/* USER CODE BEGIN 4 */
void HAL_ADC_ConvHalfCpltCallback(ADC_HandleTypeDef* hadc){
     adc_buf_half_full = true;
}
/* USER CODE END 4 */

 Some important configs:

elso_0-1698836573223.pngelso_1-1698836604039.png

Hope this helps!

View solution in original post

4 REPLIES 4
Rim LANDOLSI
ST Employee

Hello @HØdeg.1  and welcome to the community,

 

In debug session, you can use the Live Expressions view which displays expression values sampled and updated regularly during the program execution.  Also, you can display the value in the Expressions view.
If you want to print the variable value, you could follow these steps:

  • In main.c add:
#include <stdio.h>
…
while (1)
  {
    /* USER CODE END WHILE */
		printf("value is: %d\n", value);
    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
  • In syscalls.c add :
#include "stm32f4xx.h"
…
__attribute__((weak)) int _write(int file, char *ptr, int len)
{
  (void)file;
  int DataIdx;

  for (DataIdx = 0; DataIdx < len; DataIdx++)
  {
    //__io_putchar(*ptr++);
	  ITM_SendChar((*ptr++));
  }
  return len;
}

 

Hope this helps,

Thanks,

Rim.

EXUE.2
ST Employee

firstly, this question is described not very clearly, it is you can't get the correct value in adc_buf[] by ADC? or you has get the correct value but can't send it to PC?

but I think there are some points to be payed attention:

1.  it is not necessary to add "int value = add_buf[0]" to get the value, just define "add_buf[]" as global variables and then transfer the "add_buf[]" value to print function is enough.

2. to print the message to PC, generally, we use UART port, so you can send the message to PC by "print()" function or "HAL_UART_Transmit()" function directly after you have initialed the reference UART port.

hope can help you.

elso
Associate III

Hi HØdeg.1,

To write to serial port you can find the datasheet of your board. Lets say you have a STM32H7XX nucleo-144 board then USART3 is the port you want to configure to use the USB cable of your nucleo-144 board. 

For Nucleo-144 H7 boards:

https://www.st.com/resource/en/user_manual/um2407-stm32h7-nucleo144-boards-mb1364-stmicroelectronics.pdf

Then go to this chapter:

elso_0-1698832709669.png

Then you will see this:

elso_1-1698832777611.png

Here you can see that USART3 TX and RX configured to pins PD8 and PD9 will let you send data using the function HAL_Uart_transmit().

If you open something like PuTTy you can then read the data you send.

elso
Associate III

A simple way to code the behaviour you wanted.

/* USER CODE BEGIN PD */
#define ADC_BUF_FULL_LEN 4096 //65535 is max due to DMA buffer limitations
/* USER CODE END PD */

/* USER CODE BEGIN PV */
uint8_t adc_buf[ADC_BUF_LEN]; //Or uint32_t, uint16_t etc. depending on your ADC data.
volatile bool adc_buf_half_full = false;
uint8_t wtext[10]; 
/* USER CODE END PV */

/* USER CODE BEGIN 2 */
HAL_ADC_Start_DMA(&hadc3, (uint32_t*)adc_buf, ADC_BUF_LEN);
while(!adc_buf_half_full); //Wait for interrupt
sprintf(wtext, "Data: %u", adc_buf[0]);
HAL_UART_Transmit(&huart3, (uint8_t *)wtext, strlen(wtext), 1000);
/* USER CODE END 2 */

/* USER CODE BEGIN 4 */
void HAL_ADC_ConvHalfCpltCallback(ADC_HandleTypeDef* hadc){
     adc_buf_half_full = true;
}
/* USER CODE END 4 */

 Some important configs:

elso_0-1698836573223.pngelso_1-1698836604039.png

Hope this helps!