PlatformIO Community

analogRead and analogWrite are not working with STM32 Arduino

Hello Every and One!

I’m using a custom board based on STM32F103RB chip with developer version of STM32 Arduino wth core STM32Duino (it is here https://github.com/platformio/platform-ststm32). Using genericSTM32F103RB as a board.

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? Pin mapping? How can I check the mapping? I assume that PA6 is 22 HW-pin and PA7 is 23.

Correction. I use STM32F103RE not RB. The question is how to add RE board to Arduino then.

After some days of evaluation of the problem I found out that the roots of the problem is in that actually I habe F103RE chip but still develop for F103RB chip. They are little different. RE has DACs (PA4 and PA5) and ADC at PA6+PA7.

So, to solve of my problem I have to introduce RE-chip to PlatfromIO and STM32Duino Arduino core.

My PIO.ini now is the following:
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

Working option is RB (that is wrong) and I need to switch it to RE (somehow). If I just change board definition then I have problem with .core=stm32. Duino doesn’t know RE-chip and doesn’t compile arguing to variants.h And I can’t compile under mapple due to some Duino specific code I don’t want get rid of.

What is the right way to migrate to the new chip?

I’ve managed to solve this (analogWrite on DAC) here https://www.stm32duino.com/viewtopic.php?f=62&t=46