Skip to content

Commit d017602

Browse files
committed
[gpio] Extract Open Drain shim into common file
1 parent 15ed250 commit d017602

File tree

6 files changed

+80
-54
lines changed

6 files changed

+80
-54
lines changed

src/modm/platform/gpio/at90_tiny_mega/base.hpp.in

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -73,52 +73,4 @@ struct Gpio
7373
};
7474
};
7575

76-
/**
77-
* Gpio OpenDrain template, which remaps the behavior of the Gpio pin to
78-
* simulate an open-drain output (with internal pullups if needed).
79-
*
80-
* @see BitBangI2cMaster
81-
* @ingroup modm_platform_gpio
82-
*/
83-
template< class Pin >
84-
class GpioOpenDrain : public Pin
85-
{
86-
static inline Gpio::InputType inputType = Gpio::InputType::Floating;
87-
static_assert(Pin::direction == modm::Gpio::Direction::InOut, "Pin must inherit from modm::GpioIO");
88-
public:
89-
using IO = GpioOpenDrain<typename Pin::IO>;
90-
using Output = IO;
91-
using Input = IO;
92-
using Type = typename Pin::Type;
93-
94-
static constexpr modm::Gpio::Direction direction = modm::Gpio::Direction::Out;
95-
96-
enum class
97-
OutputType
98-
{
99-
PushPull,
100-
OpenDrain,
101-
};
102-
103-
public:
104-
inline static void setInput() {}
105-
inline static void setInput(Gpio::InputType type) { inputType = type; }
106-
inline static void configure(Gpio::InputType type) { inputType = type; }
107-
108-
inline static void setOutput() {}
109-
inline static void setOutput(OutputType) {}
110-
inline static void setOutput(bool status) { set(status); }
111-
112-
/// maps to `setInput(InputType::Floating)` or `setInput(InputType::PullUp)`
113-
inline static void set() { Pin::setInput(inputType); }
114-
/// maps to `setOutput(::modm::Gpio::Low)`
115-
inline static void reset() { Pin::setOutput(::modm::Gpio::Low); }
116-
inline static void set(bool status) { status ? set() : reset(); }
117-
inline static bool isSet()
118-
{ return (Pin::getDirection() == modm::Gpio::Direction::In); }
119-
120-
inline static modm::Gpio::Direction getDirection()
121-
{ return modm::Gpio::Direction::Out; }
122-
};
123-
12476
} // namespace modm::platform

src/modm/platform/gpio/at90_tiny_mega/module.lb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,8 @@ def build(env):
153153
env.template("software_port.hpp.in")
154154
env.template("set.hpp.in")
155155
env.template("../common/unused.hpp.in", "unused.hpp")
156-
157-
env.copy("../common/inverted.hpp", "inverted.hpp")
158156
env.template("../common/port_shim.hpp.in", "port_shim.hpp")
159157
env.template("../common/connector.hpp.in", "connector.hpp",
160158
filters={"formatPeripheral": "", "printSignalMap": ""})
159+
env.copy("../common/open_drain.hpp", "open_drain.hpp")
160+
env.copy("../common/inverted.hpp", "inverted.hpp")
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright (c) 2017, Niklas Hauser
3+
*
4+
* This file is part of the modm project.
5+
*
6+
* This Source Code Form is subject to the terms of the Mozilla Public
7+
* License, v. 2.0. If a copy of the MPL was not distributed with this
8+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
*/
10+
// ----------------------------------------------------------------------------
11+
12+
#pragma once
13+
14+
#include "base.hpp"
15+
16+
namespace modm::platform
17+
{
18+
19+
/**
20+
* Gpio OpenDrain template, which remaps the behavior of the Gpio pin to
21+
* simulate an open-drain output (with internal pullups if needed).
22+
*
23+
* @see BitBangI2cMaster
24+
* @ingroup modm_platform_gpio
25+
*/
26+
template< class Pin >
27+
class GpioOpenDrain : public Pin
28+
{
29+
static inline Gpio::InputType inputType = Gpio::InputType::Floating;
30+
static_assert(Pin::direction == modm::Gpio::Direction::InOut, "Pin must inherit from modm::GpioIO");
31+
public:
32+
using IO = GpioOpenDrain<typename Pin::IO>;
33+
using Output = IO;
34+
using Input = IO;
35+
using Type = typename Pin::Type;
36+
37+
static constexpr modm::Gpio::Direction direction = modm::Gpio::Direction::Out;
38+
39+
enum class
40+
OutputType
41+
{
42+
PushPull,
43+
OpenDrain,
44+
};
45+
46+
public:
47+
inline static void setInput() {}
48+
inline static void setInput(Gpio::InputType type) { inputType = type; }
49+
inline static void configure(Gpio::InputType type) { inputType = type; }
50+
51+
inline static void setOutput() {}
52+
inline static void setOutput(OutputType) {}
53+
inline static void setOutput(bool status) { set(status); }
54+
55+
/// maps to `setInput(InputType::Floating)` or `setInput(InputType::PullUp)`
56+
inline static void set() { Pin::setInput(inputType); }
57+
/// maps to `setOutput(::modm::Gpio::Low)`
58+
inline static void reset() { Pin::setOutput(::modm::Gpio::Low); }
59+
inline static void set(bool status) { status ? set() : reset(); }
60+
inline static bool isSet()
61+
{ return (Pin::getDirection() == modm::Gpio::Direction::In); }
62+
63+
inline static modm::Gpio::Direction getDirection()
64+
{ return modm::Gpio::Direction::Out; }
65+
};
66+
67+
} // namespace modm::platform

src/modm/platform/i2c/at90_tiny_mega/i2c_master.hpp.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "i2c.hpp"
1919
#include <modm/architecture/interface/i2c_master.hpp>
2020
#include <modm/platform/gpio/connector.hpp>
21+
#include <modm/platform/gpio/open_drain.hpp>
2122
#include <algorithm>
2223
#include <cmath>
2324

src/modm/platform/i2c/bitbang/bitbang_i2c_master.hpp.in

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,13 @@
1616
#ifndef MODM_SOFTWARE_BITBANG_I2C_HPP
1717
#define MODM_SOFTWARE_BITBANG_I2C_HPP
1818

19-
#include <modm/platform/gpio/connector.hpp>
2019
#include <modm/architecture/interface/delay.hpp>
2120
#include <modm/architecture/interface/gpio.hpp>
2221
#include <modm/architecture/interface/i2c_master.hpp>
22+
#include <modm/platform/gpio/connector.hpp>
23+
%% if target.platform in ["avr"]
24+
#include <modm/platform/gpio/open_drain.hpp>
25+
%% endif
2326

2427
namespace modm
2528
{
@@ -38,7 +41,7 @@ template< class Scl,
3841
class Sda >
3942
class BitBangI2cMaster : public modm::I2cMaster
4043
{
41-
%% if target["platform"] == "avr"
44+
%% if target.platform in ["avr"]
4245
using SCL = platform::GpioOpenDrain<Scl>;
4346
using SDA = platform::GpioOpenDrain<Sda>;
4447
%% else

src/modm/platform/one_wire/bitbang/bitbang_master.hpp.in

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@
1515
#define MODM_SOFTWARE_BITBANG_ONE_WIRE_HPP
1616

1717
#include <modm/architecture/utils.hpp>
18-
#include <modm/platform/gpio/connector.hpp>
1918
#include <modm/architecture/interface/delay.hpp>
19+
#include <modm/platform/gpio/connector.hpp>
20+
%% if target.platform in ["avr"]
21+
#include <modm/platform/gpio/open_drain.hpp>
22+
%% endif
2023

2124
namespace modm
2225
{
@@ -45,7 +48,7 @@ namespace platform
4548
template <typename Pin>
4649
class BitBangOneWireMaster
4750
{
48-
%% if target["platform"] == "avr"
51+
%% if target.platform in ["avr"]
4952
using PIN = platform::GpioOpenDrain<Pin>;
5053
%% else
5154
using PIN = Pin;

0 commit comments

Comments
 (0)