analogRead and analogWrite troubles

Post here all questions related to STM32 core if you can't find a relevant section!
User avatar
fpiSTM
Posts: 1757
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: analogRead and analogWrite troubles

Post by fpiSTM »

OK, I think I found the issue.
The variant you used is for this board:
https://stm32-base.org/boards/STM32F103 ... eric-Board

Firstly, do you have an HSE ?
The SystemClock_Config(void) use the HSE as clock source.
I guess you have one as it works.

Now, I saw the ADC clock is not configured while it should be.
As the RE has an ADC3, I think it is not configured by default that why it's not work.
I will review it when back from vacation.
You can try to set it at sketch level.
kvv213
Posts: 38
Joined: Thu Dec 26, 2019 10:58 pm

Re: analogRead and analogWrite troubles

Post by kvv213 »

Yes, I have HSE. Even two of them:
Annotation 2019-12-29 223808.png
Annotation 2019-12-29 223808.png (32.08 KiB) Viewed 9013 times
"You can try to set it at sketch level." if I know how I would do it :) A small example would help me.

PS. Have a nice vacation.
User avatar
fpiSTM
Posts: 1757
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: analogRead and analogWrite troubles

Post by fpiSTM »

kvv213 wrote: Sun Dec 29, 2019 7:39 pm Yes, I have HSE. Even two of them:
No you have only one HSE. The other is the LSE ;)
kvv213 wrote: Sun Dec 29, 2019 7:39 pm "You can try to set it at sketch level." if I know how I would do it :) A small example would help me.

Code: Select all

extern "C" void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {};
  RCC_PeriphCLKInitTypeDef PeriphClkInit = {};

  /* Initializes the CPU, AHB and APB busses clocks */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  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_DIV2;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) {
    Error_Handler();
  }
  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC | RCC_PERIPHCLK_USB;
  PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV6;
  PeriphClkInit.UsbClockSelection = RCC_USBCLKSOURCE_PLL_DIV1_5;
  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) {
    Error_Handler();
  }
}
kvv213 wrote: Sun Dec 29, 2019 7:39 pm PS. Have a nice vacation.
Thanks
kvv213
Posts: 38
Joined: Thu Dec 26, 2019 10:58 pm

Re: analogRead and analogWrite troubles

Post by kvv213 »

Thank you, will try to implement this initialization and return back.
kvv213
Posts: 38
Joined: Thu Dec 26, 2019 10:58 pm

Re: analogRead and analogWrite troubles

Post by kvv213 »

"As a workaround you can add an hal_conf_extra.h at sketch level and add
CODE: SELECT ALL

#define HAL_DAC_MODULE_ENABLED"

Hi again,

I've managed to add STM32F103RE to PlatformIO and it works well. But I can't enable HAL DAC module for analogWrite compatibility.
If it works with hal_conf_extra.h with Arduino IDE then it doesn't work with PIO at all. I was trying several options with definition of #define HAL_DAC_MODULE_ENABLED with no success. Even putting #define HAL_DAC_MODULE_ENABLED clearly at sketch level doesn't help with DAC.

Is there a way how to enable DAC not modifying core files? At sketch level?
User avatar
fpiSTM
Posts: 1757
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: analogRead and analogWrite troubles

Post by fpiSTM »

kvv213 wrote: Thu Jan 02, 2020 9:47 am Is there a way how to enable DAC not modifying core files? At sketch level?
No as the ino file is simply converted to cpp one, no extra definition could be added there. That's why I provided 2 ways to customize the core (build_opt.h and hal_conf_extra.h which are a bit differents).

Fpr PIO, I saw you can add it in your platformio.ini file:

Code: Select all

build_flags = -D HAL_DAC_MODULE_ENABLED
https://docs.platformio.org/en/latest/p ... ion-system
User avatar
fpiSTM
Posts: 1757
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: analogRead and analogWrite troubles

Post by fpiSTM »

Is the ADC3 now works with the clock config I gave you ?

FYI, I've enabled the DAC by default in the master:
https://github.com/stm32duino/Arduino_C ... 7cb5e3f87d

And made a PR about ADC clock config:
https://github.com/stm32duino/Arduino_C ... 2/pull/847
kvv213
Posts: 38
Joined: Thu Dec 26, 2019 10:58 pm

Re: analogRead and analogWrite troubles

Post by kvv213 »

fpiSTM wrote: Thu Jan 02, 2020 10:15 am
kvv213 wrote: Thu Jan 02, 2020 9:47 am Is there a way how to enable DAC not modifying core files? At sketch level?
No as the ino file is simply converted to cpp one, no extra definition could be added there. That's why I provided 2 ways to customize the core (build_opt.h and hal_conf_extra.h which are a bit differents).

Fpr PIO, I saw you can add it in your platformio.ini file:

Code: Select all

build_flags = -D HAL_DAC_MODULE_ENABLED
https://docs.platformio.org/en/latest/p ... ion-system
Yes, specification via additional flag helped. Thank you.
kvv213
Posts: 38
Joined: Thu Dec 26, 2019 10:58 pm

Re: analogRead and analogWrite troubles

Post by kvv213 »

fpiSTM wrote: Thu Jan 02, 2020 10:20 am Is the ADC3 now works with the clock config I gave you ?

FYI, I've enabled the DAC by default in the master:
https://github.com/stm32duino/Arduino_C ... 7cb5e3f87d

And made a PR about ADC clock config:
https://github.com/stm32duino/Arduino_C ... 2/pull/847
Not yet. Was busy with integrating RE-board into PIO with STM32Duino framework. Hope to test the solution this week :)

PS. I work under PIO and I will see your amendments not soon because in PIO we still have 1.7.0 version of STM32Duino. It will take some time while the amendments arrive to end users under PIO. So I must test your solution with clocks because I need them in my code.
kvv213
Posts: 38
Joined: Thu Dec 26, 2019 10:58 pm

Re: analogRead and analogWrite troubles

Post by kvv213 »

fpiSTM wrote: Thu Jan 02, 2020 10:20 am Is the ADC3 now works with the clock config I gave you ?
I've checked the code you provided. Unfortunately it doesn't help. After receiving the value (correct from my point of view) from PA_3 it receives unexpected interrupt and went to

Code: Select all

Infinite_Loop:
  b Infinite_Loop
section of startup_stm32f103xe.s

What I did:

1. Try to insert the code into Arduino IDE code:

Code: Select all

#define Power_1_Current PA2
#define Power_2_Current PA3
#define UART_RX PA10
#define UART_TX PA9
extern "C" void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {};
  RCC_PeriphCLKInitTypeDef PeriphClkInit = {};

  /* Initializes the CPU, AHB and APB busses clocks */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  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_DIV2;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) {
    Error_Handler();
  }
  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC | RCC_PERIPHCLK_USB;
  PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV6;
  PeriphClkInit.UsbClockSelection = RCC_USBCLKSOURCE_PLL_DIV1_5;
  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) {
    Error_Handler();
  }
}
void setup() {
  // put your setup code here, to run once:
  Serial.setRx(UART_RX);
  Serial.setTx(UART_TX);
  Serial.begin(115200);
  SystemClock_Config();
  analogReadResolution(12); // 0-4095
  pinMode(Power_1_Current,INPUT_ANALOG);
  pinMode(Power_2_Current,INPUT_ANALOG); 
  Serial.println("Current1: "+(String)analogRead(Power_1_Current));
  delay(1000);
  Serial.println("Current2: "+(String)analogRead(Power_2_Current));  
  delay(1000);
  Serial.println("Done");
}

void loop() {
  // put your main code here, to run repeatedly:

}
It didn't help. Or may be I did something wrong.

Then I do debugging (step by step) inside analogRead and deeper under PIO debugger. The code find more or less correct value and then fallen into 'infinite loop' due to an uncaught exception at returning the value.
Post Reply

Return to “General discussion”