cancel
Showing results for 
Search instead for 
Did you mean: 

Serial Communication from ST-Link-V2 to print DMA ADC values from STM32MP1

goldmine
Associate

Hi,

I am using STM32MP1F157-DK2 focusing on the ARM Cortex M4 chip and I am facing some issues while wanting to read ADC values from my sensors. I am relatively new to this and any advise or help would be greatly appreciated. I flashed the starter package into the STM32 discovery board initially based on documentation here. The following is the code performed on Arduino that I wanted to perform on the STM32 discovery board. 

 

void setup() {
  // initialize the serial communication:
  Serial.begin(9600);
  pinMode(10, INPUT); // Setup for leads off detection LO +
  pinMode(11, INPUT); // Setup for leads off detection LO -

}

void loop() {
  
  if((digitalRead(10) == 1)||(digitalRead(11) == 1)){
    Serial.println('!');
  }
  else{
    // send the value of analog input 0:
      Serial.println(analogRead(A0));
  }
  //Wait for a bit to keep serial data from saturating
  delay(1);
}

 

Based on the schematic of the STM32MP157F-DK2, I found out that the STLINK RX and TX is connected to PA2 and PA3 on the ST-Link MCU and it is connected to UART4 RX and TX. Based on the datasheet, I found out that the port PA2 has alternate function of USART2_TX and hence I configure the pins as shown below, assuming my ADC values can be displayed through the serial monitor when code is run. I have configure the baud rate as 115200.

goldmine_0-1700453727787.png

goldmine_1-1700454007738.png

goldmine_2-1700454170596.png

Next, I am connecting my sensor onto the STM32 discovery board on the Arduino connector pins at the bottom of the board. 

goldmine_3-1700454465665.png

goldmine_4-1700454668477.png

 

Based on the datasheet attached as image above, I connected LO + to PE11 on the STM and configure it as GPIO INPUT while connecting LO- to PE14 and configure it as GPIO INPUT as well. Next I connected AN0 to PF14 and configuring it as shown below. However, based on the datasheet, I should configure it as ADC1_IN0 but it is not shown in the pinout view so I configure it as ADC2INP6 single-ended. 

goldmine_5-1700454748411.png

I also want the ADC the store into DMA so this is how I configure my DMA2.

goldmine_6-1700454870652.png

Finally, I have written a simple script for the STM as shown below:

 

int main(void)
{
  /* USER CODE BEGIN 1 */
	uint16_t buf[1024]; //array to store dma levels
	float buf_Value[1024]; //array to store voltage value
	char arr[100]; //array for dma sprintf
  /* 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 */

  if(IS_ENGINEERING_BOOT_MODE())
  {
    /* Configure the system clock */
    SystemClock_Config();
  }

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_DMA_Init();
  MX_USART2_UART_Init();
  MX_ADC2_Init();
  MX_UART4_Init();
  /* USER CODE BEGIN 2 */
  /////////////////////////////////store in adc in dma////////////////////////////////
    HAL_ADC_Start_DMA(&hadc2, (uint32_t *)buf, 1024);
    HAL_Delay(50);
    ////////////////////////////////////////////////////////////////////////////////////
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
  	/////////////////////////////////read adc voltage////////////////////////////////

  		  for (int i = 0; i<1024; i++) {
  			  buf_Value[i] = (float)(buf[i])*3.3/4096;

  	/////////////////////////////////print adc voltage////////////////////////////////

  			  int lo_plus = HAL_GPIO_ReadPin(GPIOE, GPIO_PIN_11);
  			  int lo_min =  HAL_GPIO_ReadPin(GPIOE, GPIO_PIN_14);

				  char *tmpSign = (buf_Value[i] < 0) ? "-" : "";
				  float tmpVal = (buf_Value[i] < 0) ? -buf_Value[i] : buf_Value[i];

				  int tmpInt1 = tmpVal;                  // Get the integer (678).
				  float tmpFrac = tmpVal - tmpInt1;      // Get fraction (0.0123).
				  int tmpInt2 = trunc(tmpFrac * 10000);  // Turn into integer (123).

  			  if ((lo_plus == 1) || (lo_min ==1 )) {
  				  sprintf(arr, "!\n");
  			  }
  			  else {
  				  // Print as parts, note that you need 0-padding for fractional bit.
  				  sprintf (arr, "adc_read = %s%d.%04d\r\n", tmpSign, tmpInt1, tmpInt2);
  				  }


  			  //	    		  sprintf(arr, "%g\r\n", buf_Value[i]);
  			  HAL_UART_Transmit(&huart2, (uint8_t*)arr, strlen(arr), HAL_MAX_DELAY);
  			  HAL_Delay(1);
  		  }
  	//////////////////////////////////////////////////////////////////////////////////////
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

 

When running the code in CM4, it does not display the ADC values that I want but only load into the terminal of the Yocto OS.

goldmine_7-1700455141599.png

What can I do to display my ADC values onto my serial monitor via ST Link and is my configuration for the Arduino connectors correct?

Thanks in advance.

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Erwan SZYMANSKI
ST Employee

Hello @goldmine ,
If I well understand, your issue is more related to access to the M4 console, than an issue with ADC. 
Can you check this topic : https://community.st.com/t5/stm32-mpus-products/enable-m4-log-on-stm32mp1-stm32mp157-dk2-dev-kit/td-p/353135. I think it can provide you some guidelines.

Kind regards,
Erwan.

In order 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.

View solution in original post

1 REPLY 1
Erwan SZYMANSKI
ST Employee

Hello @goldmine ,
If I well understand, your issue is more related to access to the M4 console, than an issue with ADC. 
Can you check this topic : https://community.st.com/t5/stm32-mpus-products/enable-m4-log-on-stm32mp1-stm32mp157-dk2-dev-kit/td-p/353135. I think it can provide you some guidelines.

Kind regards,
Erwan.

In order 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.