modm-io / modm

modm: a C++23 library generator for AVR and ARM Cortex-M devices
https://modm.io
Mozilla Public License 2.0
748 stars 132 forks source link

STM32G4: modm interface hides some of Timer features #1011

Closed ser-plu closed 1 year ago

ser-plu commented 1 year ago
  1. Timers 15/16/17 have complementary outputs, but use configureOutputChannel function from simple timers (cannot enable the complementary channel). configureOutputChannel from AdvancedControlTimer is more appropriate. There could be also more similarities between the timers.
  2. GeneralPurposeTimer::signalToChannel does not work with complementary channels. Need to add || Signal::Signal == Gpio::Signal::Ch1n to every if check.
rleh commented 1 year ago

Do you want to propose a fix?

hshose commented 1 year ago

I just ran into the same issue; hacky workaround is:

using Timer = Timer1;
Timer::enable();
Timer::setMode(Timer::Mode::UpCounter);
// Timer clock: APB2 timer clock (170MHz)
Timer::setPrescaler(10);
// Prescaler: 1 -> Timer counter frequency: 170MHz
Timer::setOverflow(4250);
// Pwm frequency: 170MHz / 4250 / 10 = 4kHz

// configureOutputChannel for ChXn channels does not work
// Timer::configureOutputChannel<GpioC13::Ch1n>(Timer::OutputCompareMode::Pwm, 0);

// However, the configure output channel with Ch1 pin disabled works as expected
Timer::configureOutputChannel(1,
    Timer::OutputCompareMode::Pwm,
    Timer::PinState::Disable, // disable Ch1 output
    Timer::OutputComparePolarity::ActiveHigh, // this doesn't matter
    Timer::PinState::Enable, // enable Ch1n output
    Timer::OutputComparePolarity::ActiveHigh, // OutputComparePolarity for Ch1n
    Timer::OutputComparePreload::Disable
    );
Timer::applyAndReset();

Timer::setCompareValue(1,0);
Timer::applyAndReset();
Timer::pause();
Timer::enableOutput();
Timer::connect<GpioC13::Ch1n>(); // it works to just connect the Ch1n channel
Timer::start();
salkinium commented 1 year ago

This has been fixed, right?