Skip to content

Commit

Permalink
[gpio] Refactor STM32 GPIO for fewer files
Browse files Browse the repository at this point in the history
  • Loading branch information
salkinium committed Sep 15, 2021
1 parent d49cbea commit 25f196f
Show file tree
Hide file tree
Showing 16 changed files with 422 additions and 666 deletions.
69 changes: 69 additions & 0 deletions src/modm/platform/gpio/at90_tiny_mega/connector.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright (c) 2017, 2021, Niklas Hauser
*
* 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 "base.hpp"
#include "unused.hpp"
#include <type_traits>

namespace modm::platform
{

/// @cond
namespace detail
{

template< Peripheral peripheral, Gpio::Signal signal, template<Peripheral _> class... Signals >
struct GpioGetSignal;
template< Peripheral peripheral, Gpio::Signal signal, template<Peripheral _> class SignalT, template<Peripheral _> class... Signals >
struct GpioGetSignal<peripheral, signal, SignalT, Signals...>
{
using Gpio = std::conditional_t<
(SignalT<peripheral>::Signal == signal),
typename SignalT<peripheral>::Gpio,
typename GpioGetSignal<peripheral, signal, Signals...>::Gpio
>;
};
template< Peripheral peripheral, Gpio::Signal signal >
struct GpioGetSignal<peripheral, signal>
{
using Gpio = GpioUnused;
};

} // namespace detail

template< Peripheral peripheral, template<Peripheral _> class... Signals >
struct GpioConnector
{
template< class GpioQuery >
static constexpr bool Contains = (
std::is_same_v<typename Signals<peripheral>::Gpio, typename GpioQuery::Type> or ...);

template< class GpioQuery >
static constexpr bool IsValid = not std::is_same_v<typename GpioQuery::Type, GpioUnused>;

template< Gpio::Signal signal >
using GetSignal = typename detail::GpioGetSignal<peripheral, signal, Signals...>::Gpio;

inline static void
connect() {}

inline static void
disconnect()
{
(Signals<peripheral>::Gpio::disconnect(), ...);
}
};
/// @endcond

} // namespace modm::platform

3 changes: 1 addition & 2 deletions src/modm/platform/gpio/at90_tiny_mega/module.lb
Original file line number Diff line number Diff line change
Expand Up @@ -156,5 +156,4 @@ def build(env):
env.template("unused.hpp.in")

env.copy("../common/inverted.hpp", "inverted.hpp")
env.copy("../common/connector.hpp", "connector.hpp")
env.template("../common/connector_detail.hpp.in", "connector_detail.hpp")
env.copy("connector.hpp")
27 changes: 8 additions & 19 deletions src/modm/platform/gpio/at90_tiny_mega/pin.hpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,7 @@ public:
inline static void disablePcInterrupt() {
uint8_t pcmsk = PCMSK{{af_reg}} & ~(1 << PCINT{{af_id}});
PCMSK{{af_reg}} = pcmsk;
if (!pcmsk) {
PCICR &= ~(1 << PCIE{{af_reg}});
}
if (pcmsk == 0) PCICR &= ~(1 << PCIE{{af_reg}});
}
inline static bool readPcInterruptFlag() {
return (PCIFR & (1 << PCIF{{af_reg}}));
Expand Down Expand Up @@ -151,21 +149,14 @@ public:
/// @}
#endif
/// @cond
template< Peripheral peripheral >
struct BitBang { static void connect();
static_assert(
(peripheral == Peripheral::BitBang),
"Gpio{{ port ~ pin }}::BitBang only connects to bit-bang drivers!");
};
template< Peripheral p >
struct BitBang { static_assert(
(p == Peripheral::BitBang), "Gpio{{ port ~ pin }}::BitBang only connects to bit-bang drivers!"); };
%% for signal_name, signal_group in signals.items()
template< Peripheral peripheral >
struct {{ signal_name }} { static void connect();
static_assert(
%% for signal in signal_group
(peripheral == Peripheral::{{ signal.driver }}){% if loop.last %},{% else %} ||{% endif %}
%% endfor
"Gpio{{ port ~ pin }}::{{ signal_name }} only connects to {% for signal in signal_group %}{{signal.driver}}{% if not loop.last %} or {% endif %}{% endfor %}!");
};
template< Peripheral p >
struct {{ signal_name }} { static_assert(
(p == Peripheral::{{ signal_group | map(attribute="driver") | join(") or (p == Peripheral::") }}),
"Gpio{{ port ~ pin }}::{{ signal_name }} only connects to {{ signal_group | map(attribute="driver") | join(" or ") }}!"); };
%% endfor
/// @endcond
};
Expand All @@ -176,7 +167,6 @@ struct Gpio{{ port ~ pin }}::BitBang<Peripheral::BitBang>
{
using Gpio = Gpio{{ port ~ pin }};
static constexpr Gpio::Signal Signal = Gpio::Signal::BitBang;
inline static void connect() {}
};
%% for signal_group in signals.values()
%% for signal in signal_group
Expand All @@ -185,7 +175,6 @@ struct Gpio{{ port ~ pin }}::{{ signal.name }}<Peripheral::{{ signal.driver }}>
{
using Gpio = Gpio{{ port ~ pin }};
static constexpr Gpio::Signal Signal = Gpio::Signal::{{ signal.name }};
inline static void connect() { /* tumbleweed */ }
};
%% endfor
%% endfor
Expand Down
52 changes: 0 additions & 52 deletions src/modm/platform/gpio/common/connector.hpp

This file was deleted.

99 changes: 0 additions & 99 deletions src/modm/platform/gpio/common/connector_detail.hpp.in

This file was deleted.

2 changes: 0 additions & 2 deletions src/modm/platform/gpio/hosted/module.lb
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,3 @@ def build(env):
env.template("base.hpp.in")
env.template("unused.hpp.in")
env.copy("../common/inverted.hpp", "inverted.hpp")
env.copy("../common/connector.hpp", "connector.hpp")
env.template("../common/connector_detail.hpp.in", "connector_detail.hpp")
2 changes: 0 additions & 2 deletions src/modm/platform/gpio/rpi/module.lb
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,3 @@ def build(env):

env.copy(".")
env.copy("../common/inverted.hpp", "inverted.hpp")
env.copy("../common/connector.hpp", "connector.hpp")
env.template("../common/connector_detail.hpp.in", "connector_detail.hpp")
Loading

0 comments on commit 25f196f

Please sign in to comment.