analogRead and analogWrite troubles

Post here all questions related to STM32 core if you can't find a relevant section!
kvv213
Posts: 38
Joined: Thu Dec 26, 2019 10:58 pm

Re: analogRead and analogWrite troubles

Post by kvv213 »

fpiSTM wrote: Fri Dec 27, 2019 3:28 pm Hi,

For analogWrite on PA4 and PA5, this is the normal behaviour as there is no timer linked to those pins:
https://github.com/stm32duino/Arduino_T ... c#L88-L136
So it operates like a digital pin: 0-2047 -> low else high

analogWrite use pin capabilities in this order: DAC if any, PWM if any else digital.


For analogRead on PA6 and PA7 this should work if your variant is well formed:
https://github.com/stm32duino/Arduino_T ... .c#L39-L42
Probably the way you define the PA6 and PA7 does not match te index of PA_6 and PA_7 in the digitalPin array.
Now the root of the problem is clear. I have wrong definition of the chip so not all of its hardware is working as it should be. So I must create new definition for STM32Duino and put it (some how) to Platfromio...

Help! Help! Help :shock:

PS. It seems that STM32Duino supports "Generic F103R(8-B-C-E)T6" but I don't have this board at my PIO installation.
kvv213
Posts: 38
Joined: Thu Dec 26, 2019 10:58 pm

Re: analogRead and analogWrite troubles

Post by kvv213 »

Hello EveryOne again!

In order to make the things work I did the following:

1. Take ordinary Arduino IDE.
2. Add STM32Duino framework to it (the latest stable version 1.8.0).
3. Start a new sketch:
Annotation 2019-12-28 141420.png
Annotation 2019-12-28 141420.png (12.37 KiB) Viewed 8592 times
My chip is STM32F103RET6
4. Copy/paste my test code into Arduino IDE.

Run my test sketch to the board. And I get the following:

1. PA6 and PA7 started to work as they should work.
2. I receive freezing of the chip trying to do analogRead from PA3. The rest of analog reads from other pins work perfectly.
3. I still receive the same behaviour of analogWrite for PA4 & PA5 in the strict behaviour of analogRead code (https://github.com/stm32duino/Arduino_C ... log.c#L182). But at this chip I have DAC and it should call to dac_write_value. But it doesn't.

So, I'm completely confused...
User avatar
fpiSTM
Posts: 1738
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: analogRead and analogWrite troubles

Post by fpiSTM »

kvv213 wrote: Sat Dec 28, 2019 11:24 am 2. I receive freezing of the chip trying to do analogRead from PA3. The rest of analog reads from other pins work perfectly.
Seems strange if other works.

kvv213 wrote: Sat Dec 28, 2019 11:24 am 3. I still receive the same behaviour of analogWrite for PA4 & PA5 in the strict behaviour of analogRead code (https://github.com/stm32duino/Arduino_C ... log.c#L182). But at this chip I have DAC and it should call to dac_write_value. But it doesn't.
In fact the HAL DAC module is not enabled that's why it's not work. It was forgot when STM32F103Rx were added.
I will add it in the variant.h:

Code: Select all

/* Extra HAL modules */
#ifdef STM32F103xE
#define HAL_DAC_MODULE_ENABLED
#endif
As a workaround you can add an hal_conf_extra.h at sketch level and add

Code: Select all

#define HAL_DAC_MODULE_ENABLED
https://github.com/stm32duino/wiki/wiki ... ion--150-1
kvv213
Posts: 38
Joined: Thu Dec 26, 2019 10:58 pm

Re: analogRead and analogWrite troubles

Post by kvv213 »

fpiSTM wrote: Sat Dec 28, 2019 12:56 pm
kvv213 wrote: Sat Dec 28, 2019 11:24 am 2. I receive freezing of the chip trying to do analogRead from PA3. The rest of analog reads from other pins work perfectly.
Seems strange if other works.
Yes, it is.

This code newer reaches to "Done". It hands at second line of analogRead. If I change it to digitalRead - it works (of course in not desired way).

Code: Select all

#define Power_1_Current PA2
#define Power_2_Current PA3
#define UART_RX PA10
#define UART_TX PA9
void setup() {
  // put your setup code here, to run once:
  Serial.setRx(UART_RX);
  Serial.setTx(UART_TX);
  Serial.begin(115200);
  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:
}
Checked under Arduino IDE 1.8.9 with STM32Duino 1.8.0 and 1.7.0


If I compile it as STM32F103RB then it works fine. I think that there can be a mistake in the board definition somewhere around this pin or ADC definition.
Last edited by kvv213 on Sat Dec 28, 2019 6:48 pm, edited 1 time in total.
kvv213
Posts: 38
Joined: Thu Dec 26, 2019 10:58 pm

Re: analogRead and analogWrite troubles

Post by kvv213 »

fpiSTM wrote: Sat Dec 28, 2019 12:56 pm
As a workaround you can add an hal_conf_extra.h at sketch level and add

Code: Select all

#define HAL_DAC_MODULE_ENABLED
https://github.com/stm32duino/wiki/wiki ... ion--150-1
Yes, that helped. Testing the behaviour.
kvv213
Posts: 38
Joined: Thu Dec 26, 2019 10:58 pm

Re: analogRead and analogWrite troubles

Post by kvv213 »

The last thing I need to solve. It works more or less fine (except analogRead of PA3) in Arduino IDE with 1.7.0 and 1.8.0 cores. Now I need to move back to PlatformIO with RE board. I don't know how :(
User avatar
fpiSTM
Posts: 1738
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: analogRead and analogWrite troubles

Post by fpiSTM »

FYI,

Code: Select all

  pinMode(Power_1_Current,INPUT_ANALOG);
  pinMode(Power_2_Current,INPUT_ANALOG);
is not required.

For PIO usage, I could not help as I do not use it.

For PA3 analogRead, I have no clue as it should works like for other. Maybe an issue on your hardware?
kvv213
Posts: 38
Joined: Thu Dec 26, 2019 10:58 pm

Re: analogRead and analogWrite troubles

Post by kvv213 »

fpiSTM wrote: Sat Dec 28, 2019 7:09 pm FYI,

Code: Select all

  pinMode(Power_1_Current,INPUT_ANALOG);
  pinMode(Power_2_Current,INPUT_ANALOG);
is not required.

For PIO usage, I could not help as I do not use it.

For PA3 analogRead, I have no clue as it should works like for other. Maybe an issue on your hardware?
Anyway you helped a lot. Thank you.

For "For PA3 analogRead, I have no clue as it should works like for other. Maybe an issue on your hardware?" it is not definitely in hardware because if I compile the same code but use STM32F103RB board definition (but run at STM32F103RE) then it works well.
User avatar
fpiSTM
Posts: 1738
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: analogRead and analogWrite troubles

Post by fpiSTM »

Ok sorry though you use a RE.
So for the RB, use the Generic F103RBT6 (Blue Button) then PA3 will work. It is not the same ADCx instance between RB and RE.
But the RB has no DAC.

I don't know what you exactly done with PIO.
First use the Arduino IDE with the core installed properly then use the correct variant: Generic F103RBT6 (Blue Button)
All should work as expected.

analogWrite on PA4 and PA5 will not be a DAC or PWM as pins have not those capabilities. Only digital pins.
analogRead should works on PA6 and PA7. The pins definitions are corrects in the variant. If you get correct value from PB0 and PB1, maybe there is an issue on your hardware.

Have you a schematics?
kvv213
Posts: 38
Joined: Thu Dec 26, 2019 10:58 pm

Re: analogRead and analogWrite troubles

Post by kvv213 »

fpiSTM wrote: Sun Dec 29, 2019 8:37 am Ok sorry though you use a RE.
So for the RB, use the Generic F103RBT6 (Blue Button) then PA3 will work. It is not the same ADCx instance between RB and RE.
But the RB has no DAC.

I don't know what you exactly done with PIO.
First use the Arduino IDE with the core installed properly then use the correct variant: Generic F103RBT6 (Blue Button)
All should work as expected.

analogWrite on PA4 and PA5 will not be a DAC or PWM as pins have not those capabilities. Only digital pins.
analogRead should works on PA6 and PA7. The pins definitions are corrects in the variant. If you get correct value from PB0 and PB1, maybe there is an issue on your hardware.

Have you a schematics?
Hi!

Sorry for my misleading explanations.

I physically use F103RET6. And with your help it now works with DAC well (almost well except filtration of pikes of voltage at low voltage values that are not well filtered without additional fine tuning of DAC via HAL or special programming filter).

But I still have a problem with ADC at pin PA3. The MCU stop responding when I try to use analogRead(PA3). And this behaviour is true when I use compilation option F103RET6. If I switch the compilation option to F103RBT6 I receive correct values from this operation analogRead(PA3).

So, here I can say that the hardware works fine but there can be a wrong initialization/description of pins somewhere at F103RET6 definition. Unfortunately I don't have any debugger in Arduino IDE so I can't follow the whole way of analogRead and compare behaviour for different boards compilation.
Post Reply

Return to “General discussion”