Page 1 of 1

[Solved]Problem with tone() and new STM32 core

Posted: Sat Jun 10, 2017 11:51 pm
by Rick Kimball
I'm trying to add a variant to the https://github.com/stm32duino/Arduino_Core_STM32. However, I'm having issue with the tone function. It seems to run at half the frequency I'd expect. Has anyone measured the tone output with an existing supported board to see if the frequency is accurate? I tested with the toneMelody demo sketch. However, all the notes play at half the frequency of what they should be.

If i modify stm32/timer.c

Code: Select all

$ git diff stm32/timer.c
diff --git a/cores/arduino/stm32/timer.c b/cores/arduino/stm32/timer.c
index c45ba3e..fd0d028 100644
--- a/cores/arduino/stm32/timer.c
+++ b/cores/arduino/stm32/timer.c
@@ -704,7 +704,7 @@ void TimerPinInit(stimer_t *obj, uint32_t frequency, uint32_t duration)
 
   if((period < 0xFFFF) && (prescaler < 0xFFFF)) {
     obj->irqHandle = HAL_TIMx_PeriodElapsedCallback;
-    TimerHandleInit(obj, period, prescaler-1);
+    TimerHandleInit(obj, period/2, prescaler-1);
   }
   else {
     TimerHandleDeinit(obj);
$ 
With the above change, I can use tone to generate valid frequencies from about 40 -> 65535 (my chip max speed is 48MHz)

So has anyone tried it?

Re: Issue with tone() and new STM32 core

Posted: Sun Jun 11, 2017 12:00 am
by RogerClark
Probably best to raise an issue on Github if you haven't already done so.


BTW. Which MCU are you using for this? Did you make your own F103 version?

Re: Issue with tone() and new STM32 core

Posted: Sun Jun 11, 2017 12:10 am
by Rick Kimball
I have a nucleo-f030r8 so I thought that might be interesting to get it working. There is another thread where I described what I have changed.

I didn't really want to raise an issue before I got confirmation that it works with an existing board.

Re: Issue with tone() and new STM32 core

Posted: Sun Jun 11, 2017 12:32 am
by RogerClark
Sorry Rick,

I've not tried that, and I'm in the middle of debugging some other code, otherwise I'd get a board out and try it

Re: Issue with tone() and new STM32 core

Posted: Sun Jun 11, 2017 12:40 am
by Rick Kimball
No problem Roger. I just don't have any of the supported boards.

Re: Problem with tone() and new STM32 core

Posted: Mon Jun 12, 2017 6:58 am
by fpiSTM
Hi Rick,

Sorry do not saw this post this week end.
You're right there is an issue on the frequency.

Some correction has been done last week around that but it seem I missed this init: :roll:
https://github.com/stm32duino/Arduino_C ... /issues/18

Now, corrected with:
https://github.com/stm32duino/Arduino_C ... /issues/27