Skip to content

Commit 1e9f4fa

Browse files
committed
TIMER_TONE freq is twice frequency
Fix #27 Signed-off-by: Frederic.Pillon <[email protected]>
1 parent b30145b commit 1e9f4fa

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

Diff for: cores/arduino/stm32/timer.c

+6-2
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,9 @@ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
676676
void TimerPinInit(stimer_t *obj, uint32_t frequency, uint32_t duration)
677677
{
678678
uint8_t end = 0;
679+
uint32_t timClkFreq = 0;
680+
// TIMER_TONE freq is twice frequency
681+
uint32_t timFreq = 2*frequency;
679682
uint32_t prescaler = 1;
680683
uint32_t period = 0;
681684

@@ -687,16 +690,17 @@ void TimerPinInit(stimer_t *obj, uint32_t frequency, uint32_t duration)
687690

688691
//Calculate the toggle count
689692
if (duration > 0) {
690-
obj->pinInfo.count = ((frequency * duration) / 1000) * 2;
693+
obj->pinInfo.count = ((timFreq * duration) / 1000);
691694
}
692695
else {
693696
obj->pinInfo.count = -1;
694697
}
695698

696699
digital_io_init(obj->pin, GPIO_MODE_OUTPUT_PP, GPIO_NOPULL);
700+
timClkFreq = getTimerClkFreq(obj->timer);
697701

698702
while(end == 0) {
699-
period = ((uint32_t)(getTimerClkFreq(obj->timer) / frequency / prescaler)) - 1;
703+
period = ((uint32_t)( timClkFreq / timFreq / prescaler)) - 1;
700704

701705
if((period >= 0xFFFF) && (prescaler < 0xFFFF))
702706
prescaler++; //prescaler *= 2;

Diff for: cores/arduino/stm32/timer.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ struct timer_s{
6969
void (*irqHandle)(stimer_t *);
7070
void (*irqHandleOC)(stimer_t *, uint32_t);
7171
PinName pin;
72-
timerPinInfo_t pinInfo;
72+
volatile timerPinInfo_t pinInfo;
7373
};
7474

7575
/* Exported constants --------------------------------------------------------*/

0 commit comments

Comments
 (0)