Skip to content

Commit

Permalink
[gpio] Extract Open Drain shim into common file
Browse files Browse the repository at this point in the history
  • Loading branch information
salkinium committed Jan 23, 2022
1 parent 15ed250 commit d017602
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 54 deletions.
48 changes: 0 additions & 48 deletions src/modm/platform/gpio/at90_tiny_mega/base.hpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -73,52 +73,4 @@ struct Gpio
};
};

/**
* Gpio OpenDrain template, which remaps the behavior of the Gpio pin to
* simulate an open-drain output (with internal pullups if needed).
*
* @see BitBangI2cMaster
* @ingroup modm_platform_gpio
*/
template< class Pin >
class GpioOpenDrain : public Pin
{
static inline Gpio::InputType inputType = Gpio::InputType::Floating;
static_assert(Pin::direction == modm::Gpio::Direction::InOut, "Pin must inherit from modm::GpioIO");
public:
using IO = GpioOpenDrain<typename Pin::IO>;
using Output = IO;
using Input = IO;
using Type = typename Pin::Type;

static constexpr modm::Gpio::Direction direction = modm::Gpio::Direction::Out;

enum class
OutputType
{
PushPull,
OpenDrain,
};

public:
inline static void setInput() {}
inline static void setInput(Gpio::InputType type) { inputType = type; }
inline static void configure(Gpio::InputType type) { inputType = type; }

inline static void setOutput() {}
inline static void setOutput(OutputType) {}
inline static void setOutput(bool status) { set(status); }

/// maps to `setInput(InputType::Floating)` or `setInput(InputType::PullUp)`
inline static void set() { Pin::setInput(inputType); }
/// maps to `setOutput(::modm::Gpio::Low)`
inline static void reset() { Pin::setOutput(::modm::Gpio::Low); }
inline static void set(bool status) { status ? set() : reset(); }
inline static bool isSet()
{ return (Pin::getDirection() == modm::Gpio::Direction::In); }

inline static modm::Gpio::Direction getDirection()
{ return modm::Gpio::Direction::Out; }
};

} // namespace modm::platform
4 changes: 2 additions & 2 deletions src/modm/platform/gpio/at90_tiny_mega/module.lb
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ def build(env):
env.template("software_port.hpp.in")
env.template("set.hpp.in")
env.template("../common/unused.hpp.in", "unused.hpp")

env.copy("../common/inverted.hpp", "inverted.hpp")
env.template("../common/port_shim.hpp.in", "port_shim.hpp")
env.template("../common/connector.hpp.in", "connector.hpp",
filters={"formatPeripheral": "", "printSignalMap": ""})
env.copy("../common/open_drain.hpp", "open_drain.hpp")
env.copy("../common/inverted.hpp", "inverted.hpp")
67 changes: 67 additions & 0 deletions src/modm/platform/gpio/common/open_drain.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright (c) 2017, 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"

namespace modm::platform
{

/**
* Gpio OpenDrain template, which remaps the behavior of the Gpio pin to
* simulate an open-drain output (with internal pullups if needed).
*
* @see BitBangI2cMaster
* @ingroup modm_platform_gpio
*/
template< class Pin >
class GpioOpenDrain : public Pin
{
static inline Gpio::InputType inputType = Gpio::InputType::Floating;
static_assert(Pin::direction == modm::Gpio::Direction::InOut, "Pin must inherit from modm::GpioIO");
public:
using IO = GpioOpenDrain<typename Pin::IO>;
using Output = IO;
using Input = IO;
using Type = typename Pin::Type;

static constexpr modm::Gpio::Direction direction = modm::Gpio::Direction::Out;

enum class
OutputType
{
PushPull,
OpenDrain,
};

public:
inline static void setInput() {}
inline static void setInput(Gpio::InputType type) { inputType = type; }
inline static void configure(Gpio::InputType type) { inputType = type; }

inline static void setOutput() {}
inline static void setOutput(OutputType) {}
inline static void setOutput(bool status) { set(status); }

/// maps to `setInput(InputType::Floating)` or `setInput(InputType::PullUp)`
inline static void set() { Pin::setInput(inputType); }
/// maps to `setOutput(::modm::Gpio::Low)`
inline static void reset() { Pin::setOutput(::modm::Gpio::Low); }
inline static void set(bool status) { status ? set() : reset(); }
inline static bool isSet()
{ return (Pin::getDirection() == modm::Gpio::Direction::In); }

inline static modm::Gpio::Direction getDirection()
{ return modm::Gpio::Direction::Out; }
};

} // namespace modm::platform
1 change: 1 addition & 0 deletions src/modm/platform/i2c/at90_tiny_mega/i2c_master.hpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "i2c.hpp"
#include <modm/architecture/interface/i2c_master.hpp>
#include <modm/platform/gpio/connector.hpp>
#include <modm/platform/gpio/open_drain.hpp>
#include <algorithm>
#include <cmath>

Expand Down
7 changes: 5 additions & 2 deletions src/modm/platform/i2c/bitbang/bitbang_i2c_master.hpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@
#ifndef MODM_SOFTWARE_BITBANG_I2C_HPP
#define MODM_SOFTWARE_BITBANG_I2C_HPP

#include <modm/platform/gpio/connector.hpp>
#include <modm/architecture/interface/delay.hpp>
#include <modm/architecture/interface/gpio.hpp>
#include <modm/architecture/interface/i2c_master.hpp>
#include <modm/platform/gpio/connector.hpp>
%% if target.platform in ["avr"]
#include <modm/platform/gpio/open_drain.hpp>
%% endif

namespace modm
{
Expand All @@ -38,7 +41,7 @@ template< class Scl,
class Sda >
class BitBangI2cMaster : public modm::I2cMaster
{
%% if target["platform"] == "avr"
%% if target.platform in ["avr"]
using SCL = platform::GpioOpenDrain<Scl>;
using SDA = platform::GpioOpenDrain<Sda>;
%% else
Expand Down
7 changes: 5 additions & 2 deletions src/modm/platform/one_wire/bitbang/bitbang_master.hpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@
#define MODM_SOFTWARE_BITBANG_ONE_WIRE_HPP

#include <modm/architecture/utils.hpp>
#include <modm/platform/gpio/connector.hpp>
#include <modm/architecture/interface/delay.hpp>
#include <modm/platform/gpio/connector.hpp>
%% if target.platform in ["avr"]
#include <modm/platform/gpio/open_drain.hpp>
%% endif

namespace modm
{
Expand Down Expand Up @@ -45,7 +48,7 @@ namespace platform
template <typename Pin>
class BitBangOneWireMaster
{
%% if target["platform"] == "avr"
%% if target.platform in ["avr"]
using PIN = platform::GpioOpenDrain<Pin>;
%% else
using PIN = Pin;
Expand Down

0 comments on commit d017602

Please sign in to comment.