The Arduino core implementation (“SDuino”) is a stripped-down C reimplementation of the original one, but missing some featuers. tone()
is one of the 6 things / modules not implemented in that core, as documented in sduino/status-todo.md at development · tenbaht/sduino · GitHub.
The Arduino-avr implementation looks rather complex with direct access to the TIMER1/TIMER0. But maybe it can be ported to STM8?
Other PWM functions like analogWrite()
and the Servo library are available though.
Note that the original analogWrite()
implementation is here. Maybe you can modify some timer register / prescaler to get it the frequency you want. Per docs a tone(pin, freq, duration)
is equivalent to a PWM signal with duty cycle 50% and a variable PWM frequency. So an analogWrite()
with a value of 128 and a change of the timer’s prescaler might do the trick?
Edit: Depending on which pin you start the PWM outupt you’ll be using a different base timer. The Servo library actually has code
that referes to setting up the prescaler. With the help of the datasheet and the knowledge of some clock values one should be able to figure out what the input clock for the PWM peripheral is and what prescaler value is needed to get to something in between 100 and 2000 Hz, and then write that as high-low value (16 bit into two 8-bit values) into the prescalars.
Per
these are the possible PWM pins. SUPPORT_ALTERNATE_MAPPINGS
is disabled by default so you have pin 2, 5, 6 and 12, which correspond to these pin names in the array at index 2, 5 etc.
so PA3 would be PWM capable pin e.g. and would use
Timer 2 channel 3 (“TIMER23”).