HardwareTimer Library PWM Settings

Post here first, or if you can't find a relevant section!
Post Reply
mrubio
Posts: 3
Joined: Tue Mar 22, 2022 7:14 am

HardwareTimer Library PWM Settings

Post by mrubio »

Hi,

So I have setup a PWM generating firmware on a Bluepill board (STM32F1) to control some ESC's, but when I measured the outcome with the oscilloscope, the pulse width was not as I calculated it. So to be a bit more concrete, I set up my timer as follows:

Code: Select all

// Using TIM4 and corresponding channels

PWMTim->setMode(channel, TIMER_OUTPUT_COMPARE_PWM1, pin);
PWMTim->setOverflow(500, HERTZ_FORMAT); // 500 Hz PWM Signal -> T_period = 2000 us
PWMTim->setCaptureCompare(channel, 1100, MICROSEC_COMPARE_FORMAT); // 1100 ns out of 2000 ns are high
PWMTim->resume();
as proposed here: https://github.com/stm32duino/wiki/wiki ... er-library . This indeed results in a PWM signal but the measured T_period, e.g. from the beginning of HIGH to the next end of LOW, is 3000 us instead of the expected 2000 us. Am I missing something or am I thinking something wrong? Also the pulse width is not 1100 us as set by capture-compare but it is around 1600 us. By the way I am also updating the Duty Cycle of the signal dynamically in the loop() by setting a new capture-compare value, as proposed in a earlier question: viewtopic.php?f=7&t=1469

Where could the mistake be most probably? In the measurement, in the setup code or in some other issue?

Thank you in advance.
by ABOSTM » Fri Apr 01, 2022 2:53 pm
Hi @mrubio,
Unfortunately, no, you don't have a bluepill, this is a different board, even if they have both the same chip (stm32f103C8).
Guys from UpBoard, reuse bluepill configuration (bluepill variant) by simplicity, to avoid to create their own,
but this is clearly a mistake:
On bluepill board there is an external oscillator HSE @8MHZ https://stm32-base.org/boards/STM32F103 ... -Pill.html
whereas your board seems to have an HSE @12MHZ
see https://github.com/up-board/up-communit ... Xtreme_MCU:
board_build.f_cpu = 48000000L
build_flags =
-D HSE_VALUE=12000000
This surely explain that you don't get the expected frequency.

By the way I don't know how they can say CPU frequency is 48MHZ, because frequency is configurable, and on bluepill configuration is made so that CPU freq = 72MHz
https://github.com/stm32duino/Arduino_C ... Cx.cpp#L82

So I am not surprised that your PWM is not at the expected frequency.

To solve this issue, it is necessary to create a new variant to support this board,
https://github.com/stm32duino/wiki/wiki ... 28board%29

and especially it is necessary to
* add this define in variant.h
#define HSE_VALUE 12000000
* regenerate SystemClock_Config() based on this new HSE value
Go to full post
User avatar
fpiSTM
Posts: 1723
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: HardwareTimer Library PWM Settings

Post by fpiSTM »

@ABOSTM could you answer to @mrubio, please?
ABOSTM
Posts: 60
Joined: Wed Jan 08, 2020 8:40 am
Answers: 7

Re: HardwareTimer Library PWM Settings

Post by ABOSTM »

Hi @mrubio,
I don't see issue in your code.
So I tested on my bluepill (stm32f103c8), and everything work as expected: period 2000us and pulse high 1100us
Below you can see the full sketch I used.
Note: I am using "BluePill F103C8" variant ("Board part number" in Arduino IDE menu) which run at 72MHz.
Could you confirm you are using the same variant ?
Can you try my sketch below?

Code: Select all

// Using TIM4 and corresponding channels
#define pin PB6
TIM_TypeDef *Instance = (TIM_TypeDef *)pinmap_peripheral(digitalPinToPinName(pin), PinMap_PWM);
uint32_t channel = STM_PIN_CHANNEL(pinmap_function(digitalPinToPinName(pin), PinMap_PWM));
HardwareTimer *MyTim = new HardwareTimer(Instance);

void setup() {
  // put your setup code here, to run once:
  MyTim->setMode(channel, TIMER_OUTPUT_COMPARE_PWM1, pin);
  MyTim->setOverflow(500, HERTZ_FORMAT); // 500 Hz PWM Signal -> T_period = 2000 us
  MyTim->setCaptureCompare(channel, 1100, MICROSEC_COMPARE_FORMAT); // 1100 us out of 2000 us are high
  MyTim->resume();
}

void loop() {
  // put your main code here, to run repeatedly:
}
mrubio
Posts: 3
Joined: Tue Mar 22, 2022 7:14 am

Re: HardwareTimer Library PWM Settings

Post by mrubio »

Hi @ABOSTM

Thank you for the response. I could only do some tests today. Unfortunately your code gives me the same result, e.g. a period of 3000 us. I thought all bluepill (stm32f103c8) boards run at the same frequency. So I maybe should have mentioned that I have an UpBoard Xtreme with an integrated blupill board on it. I am flashing the mcu with the PlatformIO settings as proposed here by the UpBoard guys: https://github.com/up-board/up-communit ... Xtreme_MCU . But even then the HardwareTimer library should work either way right?
ABOSTM
Posts: 60
Joined: Wed Jan 08, 2020 8:40 am
Answers: 7

Re: HardwareTimer Library PWM Settings

Post by ABOSTM »

Hi @mrubio,
Unfortunately, no, you don't have a bluepill, this is a different board, even if they have both the same chip (stm32f103C8).
Guys from UpBoard, reuse bluepill configuration (bluepill variant) by simplicity, to avoid to create their own,
but this is clearly a mistake:
On bluepill board there is an external oscillator HSE @8MHZ https://stm32-base.org/boards/STM32F103 ... -Pill.html
whereas your board seems to have an HSE @12MHZ
see https://github.com/up-board/up-communit ... Xtreme_MCU:
board_build.f_cpu = 48000000L
build_flags =
-D HSE_VALUE=12000000
This surely explain that you don't get the expected frequency.

By the way I don't know how they can say CPU frequency is 48MHZ, because frequency is configurable, and on bluepill configuration is made so that CPU freq = 72MHz
https://github.com/stm32duino/Arduino_C ... Cx.cpp#L82

So I am not surprised that your PWM is not at the expected frequency.

To solve this issue, it is necessary to create a new variant to support this board,
https://github.com/stm32duino/wiki/wiki ... 28board%29

and especially it is necessary to
* add this define in variant.h
#define HSE_VALUE 12000000
* regenerate SystemClock_Config() based on this new HSE value
mrubio
Posts: 3
Joined: Tue Mar 22, 2022 7:14 am

Re: HardwareTimer Library PWM Settings

Post by mrubio »

Thank you very much for the answer! This explains a lot of things... Hopefully UP will change their documentation in the future.

Have a great week!
Post Reply

Return to “General discussion”