analogRead and analogWrite troubles

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

analogRead and analogWrite troubles

Post by kvv213 »

Hello Every and One!

I'm using a custom board based on STM32F103RB chip with developer version of STM32Duino maintained by Platformio (it is here https://github.com/platformio/platform-ststm32).

I have two problems that seems to be connected:

1. I can't do analogWrite to PA4 and PA5 pins. If I set them via analogWrite to something less than 2000 items (I use 12 bit resolution) than I receive zero PWM, if I set the value to something more than 2000 then I receive 100% of load. PA4 is physical pin 20 and PA5 is 21. As I understand analogWrite works with Arduino analog enabled pins and/or with hardware PWM supported pins. It seems that PA4 and PA5 are HW-supported but their behaviour more looks like digital pin. The behaviour is the same regardless to pinMode(PA4, OUTPUT).
What can be wrong and/or shall I use PWM_start routine from analog.h?

2. I can't do analogRead from pins PA6 and PA7. Actually I can do analogRead from them but I always receive 4095 and 2017 values (12bit resolution) from these two pins whatever I connect to them. At the same time I receive correct values from the same sketch and the same board but from PB0 and PB1.
What can be wrong?

mrburnette
Posts: 38
Joined: Thu Dec 19, 2019 1:23 am

Re: analogRead and analogWrite troubles

Post by mrburnette »

PA4 / PA5 in the libmaple core version do not have timers (Roger's core):

See master pin map heading:
http://docs.leaflabs.com/static.leaflab ... er-pin-map

A more detailed answer is here:
https://electronics.stackexchange.com/q ... pin/116410

PlatformIO:
Switching between Arduino cores
There are two different Arduino cores for STM32 microcontrollers: STM32Duino and Arduino STM32 (maple). Both of them have been developed independently, therefore, have different functionality and set of internal libraries. By default, official STM32Duino core is used. Some of the boards support both cores. To change the core you can use a board_build.core option that needs be added to build_flags:

ag123
Posts: 240
Joined: Thu Dec 19, 2019 5:30 am

Re: analogRead and analogWrite troubles

Post by ag123 »

my guess is 'analog write' would need some kind of RC network at the pin, e.g.:

Code: Select all

+ --- R --- + ---> out
            C
            +gnd
basically a low pass filter

(i'd think 'analog write' is a 'synthetic' functionality, i.e. it is either done in software, or emulated using a timer, and that
stm32f103rb don't actually have a dac
https://www.st.com/en/microcontrollers- ... 103rb.html
if you go one bound higher at least stm32f103rc, u'd get that dac
https://www.st.com/content/st_com/en/pr ... 103rc.html
nevertheless, it is rather likely that you'd need to use specific codes that drive the dac rather than simply analog write (i.e. it may not (yet) be implemented)

for analog read, did you set the pinMode()?

kvv213
Posts: 31
Joined: Thu Dec 26, 2019 10:58 pm

Re: analogRead and analogWrite troubles

Post by kvv213 »

mrburnette wrote:
Fri Dec 27, 2019 1:05 am
PA4 / PA5 in the libmaple core version do not have timers (Roger's core):

See master pin map heading:
http://docs.leaflabs.com/static.leaflab ... er-pin-map

A more detailed answer is here:
https://electronics.stackexchange.com/q ... pin/116410

PlatformIO:
Switching between Arduino cores
There are two different Arduino cores for STM32 microcontrollers: STM32Duino and Arduino STM32 (maple). Both of them have been developed independently, therefore, have different functionality and set of internal libraries. By default, official STM32Duino core is used. Some of the boards support both cores. To change the core you can use a board_build.core option that needs be added to build_flags:
But I beleive that I use Duino, not mapple core. It is clearly indicated in platformio.ini:
platform = ststm32
board = genericSTM32F103RB
platform = https://github.com/platformio/platform-ststm32.git
framework = arduino
board_build.core = stm32

PS. Both of the functions - read from PA6/PA7 and PWM on PA4/PA5 works at the same board configured via Cube and generated code in Keil.

kvv213
Posts: 31
Joined: Thu Dec 26, 2019 10:58 pm

Re: analogRead and analogWrite troubles

Post by kvv213 »

ag123 wrote:
Fri Dec 27, 2019 6:03 am
my guess is 'analog write' would need some kind of RC network at the pin, e.g.:

Code: Select all

+ --- R --- + ---> out
            C
            +gnd
basically a low pass filter

(i'd think 'analog write' is a 'synthetic' functionality, i.e. it is either done in software, or emulated using a timer, and that
stm32f103rb don't actually have a dac
https://www.st.com/en/microcontrollers- ... 103rb.html
if you go one bound higher at least stm32f103rc, u'd get that dac
https://www.st.com/content/st_com/en/pr ... 103rc.html
nevertheless, it is rather likely that you'd need to use specific codes that drive the dac rather than simply analog write (i.e. it may not (yet) be implemented)

for analog read, did you set the pinMode()?
The RC filters are implemented :) The board has the same design for PA6/PA7 pins (doesn't work from Arduino) and PB0/PB1 (works from Arduino). All four pins are designed for a thermistor 10k usage.
I've tried to use pinMode with output and output_analog - without any success. Plus PB0/PB1 works without pinMode (as it should be for analogRead).

Yep, I see that there is no DAC at this chip. And as I see from Cube/Keil code (that was carefully passed by the board developer to me) it uses HAL_DAC for PWM:
HAL_DAC_SetValue(&hdac,DAC_CHANNEL_1,DAC_ALIGN_12B_R,refVoltage1);

Where refVoltage1 is the PWM load. I'm not very familiar with Cube/Keil but I thin that it uses Timer for PWM generation. I'd like to avoid of usage of HAL functions directly (at least this stage) plus I'm not sure that I'll be able to use them directly in my code without any troubles and prefer to use Arduino functions.
Due to some research I've found that there is such a functions in analog.cpp of the core:

Code: Select all

void pwm_start(PinName pin, uint32_t PWM_freq, uint32_t value)
{
  TIM_TypeDef *Instance = (TIM_TypeDef *)pinmap_peripheral(pin, PinMap_PWM);
  HardwareTimer *HT;
  uint32_t index = get_timer_index(Instance);
  if (HardwareTimer_Handle[index] == NULL) {
    HardwareTimer_Handle[index]->__this = new HardwareTimer((TIM_TypeDef *)pinmap_peripheral(pin, PinMap_PWM));
  }

  HT = (HardwareTimer *)(HardwareTimer_Handle[index]->__this);

  uint32_t channel = STM_PIN_CHANNEL(pinmap_function(pin, PinMap_PWM));

  HT->setMode(channel, TIMER_OUTPUT_COMPARE_PWM1, pin);
  HT->setOverflow(PWM_freq, HERTZ_FORMAT);
  HT->setCaptureCompare(channel, value, RESOLUTION_12B_COMPARE_FORMAT);
  HT->resume();
}
It seems that it does the thing I need with PWM. What do you think?

kvv213
Posts: 31
Joined: Thu Dec 26, 2019 10:58 pm

Re: analogRead and analogWrite troubles

Post by kvv213 »

I've tied to use pwm_start:

pwm_start(PA_4,1000,500);

brings to freezing of the board.... and I'm not sure that PA4 refers to PA_4 that is required by pwm_start...

kvv213
Posts: 31
Joined: Thu Dec 26, 2019 10:58 pm

Re: analogRead and analogWrite troubles

Post by kvv213 »

Diggin deeper I found out that I have STM32F103RE Not RB version and it has 2 DACs. The question is how to use its DAC from Arduino and how to add F103RE to arduino in PIO.

That can be also the reason that PA6/PA7 are not readed.

User avatar
fpiSTM
Posts: 194
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 4
Location: Le Mans
Contact:

Re: analogRead and analogWrite troubles

Post by fpiSTM »

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.

ag123
Posts: 240
Joined: Thu Dec 19, 2019 5:30 am

Re: analogRead and analogWrite troubles

Post by ag123 »

it seemed that selecting an appropriate board / board file would help as the stm32f103c 8 and B series don't have dac, while C, D, E have that
https://github.com/stm32duino/Arduino_T ... R(C-D-E)Tx
with the dac, pwm isn' t needed, so no rc filter is needed, this is the ultimate, set the value you want e.g. analogWrite() and you get the voltage at the pin

kvv213
Posts: 31
Joined: Thu Dec 26, 2019 10:58 pm

Re: analogRead and analogWrite troubles

Post by kvv213 »

ag123 wrote:
Fri Dec 27, 2019 4:26 pm
it seemed that selecting an appropriate board / board file would help as the stm32f103c 8 and B series don't have dac, while C, D, E have that
https://github.com/stm32duino/Arduino_T ... R(C-D-E)Tx
with the dac, pwm isn' t needed, so no rc filter is needed, this is the ultimate, set the value you want e.g. analogWrite() and you get the voltage at the pin
Yes, I think that the problem is in the board definition. As I mentioned I use "board = genericSTM32F103RB" board definition in my platformio.ini. It is obviously wrong because now I have different chip.

But I'm only guessing how can I switch to the right (STM32F103R(C-D-E)Tx) board definition. I work actively with GitHub and I need to make this adjustments in the project folder in order to avoid of the problems at other locations I have (plus I need to add some other people to the development).

My current platformio.ini is the following:

[env:KDS]
platform = ststm32
;board = genericSTM32F103RB
board=genericSTM32F103RE
platform = https://github.com/platformio/platform-ststm32.git
framework = arduino
upload_protocol = stlink
debug_tool = stlink
board_build.core = stm32
build_flags = -fexceptions
monitor_speed = 115200
monitor_port = 20

If I enable F103RB it compiles well. If I enable F103RE it doesn't compile until I point to core for use (stm32) variant.h is missing from Arduino.h... If I disable stm32 definition then I receive a bunch of errors claiming that TIM, analogReadResolution and other STM32Duino (I guess) features.

Post Reply

Return to “General discussion”