Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stm32 pwm complementary channel #1018

Merged
merged 7 commits into from
May 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/modm/platform/gpio/stm32/module.lb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#
# Copyright (c) 2016-2018, Niklas Hauser
# Copyright (c) 2017, Fabian Greif
# Copyright (c) 2020, Christopher Durand
# Copyright (c) 2020-2023, Christopher Durand
#
# This file is part of the modm project.
#
Expand Down Expand Up @@ -164,6 +164,9 @@ def prepare(module, options):
":cmsis:device",
":math:utils",
":platform:rcc")

module.add_query(EnvironmentQuery(name="all_signals", factory=lambda env: bprops["all_signals"]))

return True

def validate(env):
Expand Down
6 changes: 6 additions & 0 deletions src/modm/platform/timer/stm32/advanced.cpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ modm::platform::Timer{{ id }}::disable()
Rcc::disable<Peripheral::Tim{{id}}>();
}

bool
modm::platform::Timer{{ id }}::isEnabled()
{
return Rcc::isEnabled<Peripheral::Tim{{id}}>();
}

// ----------------------------------------------------------------------------
void
modm::platform::Timer{{ id }}::setMode(Mode mode, SlaveMode slaveMode,
Expand Down
9 changes: 9 additions & 0 deletions src/modm/platform/timer/stm32/advanced.hpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ public:
static void
disable();

static bool
isEnabled();

static inline void
pause()
{
Expand Down Expand Up @@ -231,6 +234,12 @@ public:
TIM{{ id }}->CNT = value;
}

static constexpr bool
hasAdvancedPwmControl()
{
return true;
}

static inline void
enableOutput()
{
Expand Down
6 changes: 6 additions & 0 deletions src/modm/platform/timer/stm32/basic.cpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ modm::platform::Timer{{ id }}::disable()
Rcc::disable<Peripheral::Tim{{id}}>();
}

bool
modm::platform::Timer{{ id }}::isEnabled()
{
return Rcc::isEnabled<Peripheral::Tim{{id}}>();
}

void
modm::platform::Timer{{ id }}::setMode(Mode mode)
{
Expand Down
9 changes: 9 additions & 0 deletions src/modm/platform/timer/stm32/basic.hpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ public:
static void
disable();

static bool
isEnabled();

static inline void
pause()
{
Expand Down Expand Up @@ -172,6 +175,12 @@ public:
TIM{{ id }}->CNT = value;
}

static constexpr bool
hasAdvancedPwmControl()
{
return false;
}

static void
enableInterruptVector(bool enable, uint32_t priority);

Expand Down
13 changes: 13 additions & 0 deletions src/modm/platform/timer/stm32/basic_base.hpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ public:
static void
disable();

/**
* Check, whether clock has been enabled.
*/
static bool
isEnabled();

/**
* Pause timer operation
*
Expand Down Expand Up @@ -203,6 +209,13 @@ public:
static inline void
setValue(Value value);

/**
* Allows to check, whether the timer has BDTR and DTR2 registers
* for PWM deadtime, break and output enable control.
*/
static constexpr bool
hasAdvancedPwmControl();

/**
* Enables or disables the Interrupt Vector.
*
Expand Down
41 changes: 40 additions & 1 deletion src/modm/platform/timer/stm32/general_purpose.cpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ modm::platform::Timer{{ id }}::disable()
Rcc::disable<Peripheral::Tim{{id}}>();
}

bool
modm::platform::Timer{{ id }}::isEnabled()
{
return Rcc::isEnabled<Peripheral::Tim{{id}}>();
}

// ----------------------------------------------------------------------------
void
modm::platform::Timer{{ id }}::setMode(Mode mode, SlaveMode slaveMode,
Expand Down Expand Up @@ -197,6 +203,39 @@ modm::platform::Timer{{ id }}::configureOutputChannel(uint32_t channel,
}
}

%% if id in [15, 16, 17]
void
modm::platform::Timer{{ id }}::configureOutputChannel(uint32_t channel,
OutputCompareMode mode,
PinState out, OutputComparePolarity polarity,
PinState out_n, OutputComparePolarity polarity_n,
OutputComparePreload preload)
{
modm_assert(channel == 1, "Timer{{ id }}", "This timer has complementary output only on channel 1!", "{{ id }}");

channel -= 1;

// disable output
TIM{{ id }}->CCER &= ~(0xf << (channel * 4));

uint32_t flags = static_cast<uint32_t>(mode) | static_cast<uint32_t>(preload);

const uint32_t offset = 8 * channel;

flags <<= offset;
flags |= TIM{{ id }}->CCMR1 & ~(0xff << offset);

TIM{{ id }}->CCMR1 = flags;

// CCER Flags (Enable/Polarity)
flags = (static_cast<uint32_t>(polarity_n) << 2) |
(static_cast<uint32_t>(out_n) << 2) |
static_cast<uint32_t>(polarity) | static_cast<uint32_t>(out);

TIM{{ id }}->CCER |= flags << (channel * 4);
}
%% endif

// ----------------------------------------------------------------------------
void
modm::platform::Timer{{ id }}::enableInterruptVector(bool enable, uint32_t priority)
Expand Down Expand Up @@ -240,4 +279,4 @@ modm::platform::Timer{{ id }}::isChannelConfiguredAsInput(uint32_t channel)
}
return isInput;
}
%% endif
%% endif
38 changes: 38 additions & 0 deletions src/modm/platform/timer/stm32/general_purpose.hpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Copyright (c) 2013-2014, 2016, Kevin Läufer
* Copyright (c) 2014, 2022, Sascha Schade
* Copyright (c) 2022, Christopher Durand
* Copyright (c) 2023, Sergey Pluzhnikov
*
* This file is part of the modm project.
*
Expand Down Expand Up @@ -137,6 +138,9 @@ public:
static void
disable();

static bool
isEnabled();

static inline void
pause()
{
Expand Down Expand Up @@ -255,6 +259,12 @@ public:


%% if target.family not in ["l0", "l1"] and id in [15, 16, 17]
static constexpr bool
hasAdvancedPwmControl()
{
return true;
}

static inline void
enableOutput()
{
Expand Down Expand Up @@ -340,6 +350,12 @@ public:
flags |= (deadTime & bitmask) | static_cast<uint32_t>(resolution);
TIM{{ id }}->BDTR = flags;
}
%% else
static constexpr bool
hasAdvancedPwmControl()
{
return false;
}
%% endif


Expand Down Expand Up @@ -387,6 +403,28 @@ public:
configureOutputChannel(channel, mode, compareValue, out, enableComparePreload);
}

%% if id in [15, 16, 17]
static void
configureOutputChannel(uint32_t channel, OutputCompareMode mode,
PinState out, OutputComparePolarity polarity,
PinState out_n,
OutputComparePolarity polarity_n = OutputComparePolarity::ActiveHigh,
OutputComparePreload preload = OutputComparePreload::Disable);

template<typename Signal>
static void
configureOutputChannel(OutputCompareMode mode,
PinState out, OutputComparePolarity polarity,
PinState out_n,
OutputComparePolarity polarity_n = OutputComparePolarity::ActiveHigh,
OutputComparePreload preload = OutputComparePreload::Disable)
{
constexpr auto channel = signalToChannel<Peripheral::Tim{{ id }}, Signal>();
static_assert(channel == 1, "Timer{{ id }} has complementary output only on channel 1");
configureOutputChannel(channel, mode, out, polarity, out_n, polarity_n, preload);
}
%% endif

/// Switch to Pwm Mode 2
///
/// While upcounting channel will be active as long as the time value is
Expand Down
42 changes: 31 additions & 11 deletions src/modm/platform/timer/stm32/general_purpose_base.hpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
* Copyright (c) 2013, Kevin Läufer
* Copyright (c) 2014, Fabian Greif
* Copyright (c) 2014-2017, Niklas Hauser
* Copyright (c) 2022, Christopher Durand
* Copyright (c) 2022-2023, Christopher Durand
* Copyright (c) 2023, Sergey Pluzhnikov
*
* This file is part of the modm project.
*
Expand Down Expand Up @@ -50,7 +51,7 @@ public:
CaptureCompare1 = TIM_DIER_CC1IE,
CaptureCompare2 = TIM_DIER_CC2IE,
CaptureCompare3 = TIM_DIER_CC3IE,
CaptureCompare4= TIM_DIER_CC4IE,
CaptureCompare4 = TIM_DIER_CC4IE,
Trigger = TIM_DIER_TIE,
%% if target.family not in ["l0", "l1"]
COM = TIM_DIER_COMIE,
Expand Down Expand Up @@ -269,22 +270,41 @@ public:
static uint16_t
getCompareValue(uint32_t channel);

public:
template< typename Signal>
static consteval bool
isComplementaryChannel()
{
%% for signal, number in signals if signal in ["Ch1n","Ch2n","Ch3n","Ch4n"]
%% if loop.first
if constexpr (Signal::Signal == Gpio::Signal::{{ signal }}) {
return true;
%% else
} else if constexpr (Signal::Signal == Gpio::Signal::{{ signal }}) {
return true;
%% endif
%% if loop.last and loop.length > 0
}
%% endif
%% endfor
return false;
}

protected:
template<Peripheral p, typename Signal>
static consteval int
signalToChannel()
{
modm::platform::detail::SignalConnection<Signal, p>{};
if constexpr (Signal::Signal == Gpio::Signal::Ch1) {
return 1;
} else if constexpr (Signal::Signal == Gpio::Signal::Ch2) {
return 2;
} else if constexpr (Signal::Signal == Gpio::Signal::Ch3) {
return 3;
%% if target.family != "l0" or target.pin != "d"
} else if constexpr (Signal::Signal == Gpio::Signal::Ch4) {
return 4;
%% for signal, number in signals
%% if loop.first
if constexpr (Signal::Signal == Gpio::Signal::{{ signal }}) {
return {{ number }};
%% else
} else if constexpr (Signal::Signal == Gpio::Signal::{{ signal }}) {
return {{ number }};
%% endif
%% endfor
} else {
// assert is always false, static_assert(false) would not compile
static_assert(!sizeof(Signal), "Invalid timer channel");
Expand Down
23 changes: 7 additions & 16 deletions src/modm/platform/timer/stm32/module.lb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# -*- coding: utf-8 -*-
#
# Copyright (c) 2016-2018, Niklas Hauser
# Copyright (c) 2023, Christopher Durand
#
# This file is part of the modm project.
#
Expand All @@ -11,22 +12,9 @@
# -----------------------------------------------------------------------------

from collections import defaultdict
props = {}
import re

def get_connectors(instance):
if instance in [1, 8]:
return ["Channel1", "Channel1N", "Channel2", "Channel2N",
"Channel3", "Channel3N", "Channel4", "Channel4N",
"ExternalTrigger", "BreakIn"]
elif instance in [2, 3, 4, 5, 19]:
return ["Channel1", "Channel2", "Channel3", "Channel4", "ExternalTrigger"]
elif instance in [9, 12]:
return ["Channel1", "Channel2"]
elif instance in [10, 11, 13, 14]:
return ["Channel1"]
elif instance in [15, 16, 17]:
return ["Channel1", "Channel1N", "Channel2", "BreakIn"]
return []
props = {}

class Instance(Module):
def __init__(self, driver, instance):
Expand Down Expand Up @@ -73,7 +61,6 @@ class Instance(Module):
def build(self, env):
global props
props["id"] = self.instance
props["connectors"] = get_connectors(self.instance)
props["vectors"] = self.vectors
props["types"].add(self.type)

Expand Down Expand Up @@ -129,6 +116,10 @@ def build(env):
env.substitutions = props
env.outbasepath = "modm/src/modm/platform/timer"

pattern = re.compile("^(Ch([1-6])n{0,1})$")
all_signals = env.query(":platform:gpio:all_signals")
props["signals"] = [pattern.match(s).groups() for s in all_signals if pattern.match(s)]

# Only generate the base types for timers that were generated
types = props["types"]
if "advanced" in types:
Expand Down