Skip to content

Commit

Permalink
[gpio] Fix CI + formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
henrikssn committed Oct 5, 2020
1 parent 9d9722b commit e382657
Show file tree
Hide file tree
Showing 13 changed files with 287 additions and 179 deletions.
2 changes: 1 addition & 1 deletion .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ BinPackParameters: true
BraceWrapping:
AfterCaseLabel: false
AfterClass: true
AfterControlStatement: false
AfterControlStatement: true
AfterEnum: true
AfterFunction: true
AfterNamespace: true
Expand Down
9 changes: 6 additions & 3 deletions examples/samd/interrupt/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,13 @@ main()
ExternalInterrupt::initialize();
ExtInt<3>::initialize(&isr);
ExtInt<3>::connect<D12>();
while (1) {
if (blink) {
while (1)
{
if (blink)
{
Led::toggle();
} else {
} else
{
Led::set(0);
}
modm::delay(100ms);
Expand Down
1 change: 1 addition & 0 deletions src/modm/board/feather_m0/board.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ namespace Board
{

using namespace modm::literals;
using namespace modm::platform::gpio;

using ARef = Gpio<A03>;
using A0 = Gpio<A02>;
Expand Down
2 changes: 1 addition & 1 deletion src/modm/platform/extint/sam/extint.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class ExtInt : private ExternalInterrupt
template<class GpioPin>
static void connect()
{
using Eic = Peripheral::Eic;
using Eic = Peripherals::Eic;
using Pin = typename GpioPin::template As<PeripheralPin::ExtInt>;
Pin::template Connector<Eic, Eic::Extint<instance>>::connect();
}
Expand Down
6 changes: 4 additions & 2 deletions src/modm/platform/extint/sam/extint_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ ExtInt<instance>::initialize(std::function<void()> handler,
InputTrigger trigger, bool wakeupEnabled)
{
handlers_[instance] = handler;
if (wakeupEnabled) {
if (wakeupEnabled)
{
EIC->WAKEUP.reg |= EIC_WAKEUP_WAKEUPEN(1u << instance);
} else {
} else
{
EIC->WAKEUP.reg &= ~EIC_WAKEUP_WAKEUPEN(1u << instance);
}
constexpr int sensePos = instance * EIC_CONFIG_SENSE1_Pos;
Expand Down
82 changes: 47 additions & 35 deletions src/modm/platform/gpio/sam/config.hpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,30 @@ namespace modm::platform

typedef uint32_t Instance;

enum class PinFunction {
enum class PinFunction
{
%% for function in functions
{{ function }},
{{ function }},
%% endfor
};

struct Peripheral {
struct Peripherals
{
%% for name, peripheral in peripherals.items()
%% if "instances" in peripheral
template<int instance>
template<int instance>
%% endif
struct {{ name }} {
struct {{ name }}
{
%% for signal, index_list in peripheral["signals"].items()
%% if not index_list
struct {{ signal }} {};
struct {{ signal }} {};
%% else
template<int index>
struct {{ signal }};
template<int index>
struct {{ signal }};
%% endif
%% endfor
};
};

%% endfor
};
Expand All @@ -51,58 +54,67 @@ struct Peripheral {
%% if index_list
%% for index in index_list
template<> template<>
struct Peripheral::{{ name }}<{{ instance }}>::{{ signal }}<{{ index }}> {};
struct Peripherals::{{ name }}<{{ instance }}>::{{ signal }}<{{ index }}> {};
%% endfor
%% else
template<>
struct Peripheral::{{ name }}<{{ instance }}>::{{ signal }} {};
struct Peripherals::{{ name }}<{{ instance }}>::{{ signal }} {};
%% endif
%% endfor
%% endfor
%% else
%% for signal, index_list in peripheral["signals"].items()
%% for index in index_list
template<>
struct Peripheral::{{ name }}::{{ signal }}<{{ index }}> {};
struct Peripherals::{{ name }}::{{ signal }}<{{ index }}> {};
%% endfor
%% endfor
%% endif
%% endfor


enum class PortName {
A,
B,
enum class PortName
{
A,
B,
};

template<class _>
class Gpio;

%% for gpio in gpios
struct {{ gpio["port"] ~ gpio["pin"] }} {
static constexpr PortName port = PortName::{{ gpio["port"] }};
static constexpr uint32_t pin = {{ gpio["pin"] | int }};
namespace gpio {
struct {{ gpio["port"] ~ gpio["pin"] }}
{
static constexpr PortName port = PortName::{{ gpio["port"] }};
static constexpr uint32_t pin = {{ gpio["pin"] | int }};

%% for signal in gpio["signal"]
struct {{ signal["full_name"] }} {
static constexpr PinFunction function{PinFunction::{{ signal["function"] }}};
%% if "instance" in signal
using peripheral = Peripheral::{{ signal["peripheral"] }}<{{ signal["instance"] }}>;
%% else
using peripheral = Peripheral::{{ signal["peripheral"] }};
%% endif
%% if "index" in signal
using signal = peripheral::{{ signal["name"] }}<{{ signal["index"] }}>;
%% else
using signal = peripheral::{{ signal["name"] }};
%% endif
};
struct {{ signal["full_name"] }}
{
static constexpr PinFunction function{PinFunction::{{ signal["function"] }}};
%% if "instance" in signal
using peripheral = Peripherals::{{ signal["peripheral"] }}<{{ signal["instance"] }}>;
%% else
using peripheral = Peripherals::{{ signal["peripheral"] }};
%% endif
%% if "index" in signal
using signal = peripheral::{{ signal["name"] }}<{{ signal["index"] }}>;
%% else
using signal = peripheral::{{ signal["name"] }};
%% endif
};
%% endfor

using Signals =
std::tuple<
using Signals =
std::tuple<
%% for signal in gpio["signal"]
{{ signal["full_name"] }}{{ ">;" if loop.last else ","}}
{{ signal["full_name"] }}{{ ">;" if loop.last else ","}}
%% endfor
};
} // namespace gpio
using Gpio{{ gpio["port"] ~ gpio["pin"] }} = Gpio<gpio::{{ gpio["port"] ~ gpio["pin"] }}>;

%% endfor

} // namespace modm::platform
} // namespace modm::platform
38 changes: 38 additions & 0 deletions src/modm/platform/gpio/sam/connector.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (c) 2020, Erik Henriksson
*
* This file is part of the modm project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
// ----------------------------------------------------------------------------

#pragma once

#include "pin.hpp"

namespace modm
{

namespace platform
{

// For backwards compability.
using Peripheral = PeripheralPin;

// GpioConnector only exists for backwards compability with bitbang API.
template<Peripheral peripheral, template<Peripheral _> class... Signals>
struct GpioConnector
{
template<class GpioQuery>
static constexpr bool Contains = true;

inline static void connect() {}
inline static void disconnect() {}
};

} // namespace platform

} // namespace modm
7 changes: 7 additions & 0 deletions src/modm/platform/gpio/sam/module.lb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ def build(env):
"function": signal["function"].capitalize(),
"name": signal["name"].capitalize(),
})
# This is a hack for L variant devices, which have a Ac without instance *and*
# a Ac with instance 1. Why???
if (device.identifier.get("variant") == "l"
and signal["peripheral"] == "Ac"
and (not "instance" in signal or signal["instance"] == "")):
signal["instance"] = "0"
signal["full_name"] = signal["peripheral"]
peripheral = peripherals.setdefault(signal["peripheral"], dict())
if "instance" in signal:
Expand All @@ -73,5 +79,6 @@ def build(env):

bprops["gpios"] = driver["gpio"]
env.template("config.hpp.in")
env.copy("connector.hpp")
env.copy("pin.hpp")
env.copy("unused.hpp")
2 changes: 1 addition & 1 deletion src/modm/platform/gpio/sam/module.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ find that `Gpio<A01>` is actually using the `toggle()` method from `GpioSet<A01>
!!! note "Should normally be done by the UART peripheral!"

```cpp
using Sercom = Peripheral::Sercom<1>;
using Sercom = Peripherals::Sercom<1>;
using Conn = Gpio<A01>::Tx::Connector<Sercom, Sercom::Pad<0>, Sercom::Pad<2>>;
Conn::::connect();
```
Expand Down
33 changes: 21 additions & 12 deletions src/modm/platform/gpio/sam/pin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ enum class OutputSpeed

enum class PeripheralPin
{
BitBang,
Rx,
Tx,
ExtInt,
Expand Down Expand Up @@ -114,9 +115,11 @@ struct PinCfgMixin<PinConfig, PinConfigs...>
{
inline static void set(uint8_t cfg)
{
if constexpr (PinConfig::port == PortName::A) {
if constexpr (PinConfig::port == PortName::A)
{
PORT->Group[0].PINCFG[PinConfig::pin].reg = cfg;
} else if constexpr (PinConfig::port == PortName::B) {
} else if constexpr (PinConfig::port == PortName::B)
{
PORT->Group[1].PINCFG[PinConfig::pin].reg = cfg;
}
PinCfgMixin<PinConfigs...>::set(cfg);
Expand All @@ -137,22 +140,22 @@ class GpioSet : protected PinCfgMixin<PinConfigs...>

inline static void setPortReg(size_t offset)
{
if constexpr (mask(PortName::A) != 0) {
*(uint32_t *)(&PORT->Group[0] + offset) = mask(PortName::A);
}
if constexpr (mask(PortName::B) != 0) {
*(uint32_t *)(&PORT->Group[1] + offset) = mask(PortName::B);
}
if constexpr (mask(PortName::A) != 0)
{ *(uint32_t *)(&PORT->Group[0] + offset) = mask(PortName::A); }
if constexpr (mask(PortName::B) != 0)
{ *(uint32_t *)(&PORT->Group[1] + offset) = mask(PortName::B); }
}

template<PortName port>
inline static uint32_t readPortReg(size_t offset)
{
if constexpr (port == PortName::A) {
if constexpr (port == PortName::A)
{
static_assert(mask(PortName::A) != 0,
"Trying to read port which is not in the GpioSet!");
return *(uint32_t *)(&PORT->Group[0] + offset) & mask(PortName::A);
} else if constexpr (port == PortName::B) {
} else if constexpr (port == PortName::B)
{
static_assert(mask(PortName::B) != 0,
"Trying to read port which is not in the GpioSet!");
return *(uint32_t *)(&PORT->Group[1] + offset) & mask(PortName::A);
Expand Down Expand Up @@ -212,6 +215,10 @@ class Gpio : public GpioSet<PinConfig>, public ::modm::GpioIO
using Base = GpioSet<PinConfig>;

public:
// For backwards compability with bitbang API
using InputType = ::modm::platform::InputType;
using OutputType = ::modm::platform::OutputType;

template<PeripheralPin peripheral_pin_v>
struct As;

Expand Down Expand Up @@ -281,11 +288,13 @@ struct Gpio<PinConfig>::As : public Gpio<PinConfig>
inline static void connect()
{
Base::PinCfg::set(PORT_PINCFG_INEN);
if constexpr (PinConfig::pin >> 1) {
if constexpr (PinConfig::pin >> 1)
{
PORT->Group[uint32_t(PinConfig::port)]
.PMUX[PinConfig::pin >> 1]
.bit.PMUXO = uint32_t(PinSignal::function);
} else {
} else
{
PORT->Group[uint32_t(PinConfig::port)]
.PMUX[PinConfig::pin >> 1]
.bit.PMUXE = uint32_t(PinSignal::function);
Expand Down
Loading

0 comments on commit e382657

Please sign in to comment.