Skip to content

Commit

Permalink
[wip] More complete Gpio class
Browse files Browse the repository at this point in the history
  • Loading branch information
salkinium committed May 12, 2021
1 parent ae6002e commit bf44fe8
Show file tree
Hide file tree
Showing 6 changed files with 315 additions and 160 deletions.
2 changes: 1 addition & 1 deletion examples/nucleo_f446ze/random_access_gpio_port/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ main()
const auto port = LedPort{};
port[1].set();

GpioAccessor pin = port[0];
RtGpio pin = port[0];
pin.set();

modm::delay(500ms);
Expand Down
27 changes: 18 additions & 9 deletions examples/stm32f3_discovery/blink/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,39 @@

using namespace Board;

RtGpio north = Board::LedNorth();
RtGpio northEast = Board::LedNorthEast();
RtGpio east = Board::LedEast();
RtGpio southEast = Board::LedSouthEast();
RtGpio south = Board::LedSouth();
RtGpio southWest = Board::LedSouthWest();
RtGpio west = Board::LedWest();
RtGpio northWest = Board::LedNorthWest();

int
main()
{
Board::initialize();

Board::LedNorth::set();
north.set();

while (true)
{
Board::LedNorth::toggle();
north.toggle();
modm::delay(100ms);
Board::LedNorthEast::toggle();
northEast.toggle();
modm::delay(100ms);
Board::LedEast::toggle();
east.toggle();
modm::delay(100ms);
Board::LedSouthEast::toggle();
southEast.toggle();
modm::delay(100ms);
Board::LedSouth::toggle();
south.toggle();
modm::delay(100ms);
Board::LedSouthWest::toggle();
southWest.toggle();
modm::delay(100ms);
Board::LedWest::toggle();
west.toggle();
modm::delay(100ms);
Board::LedNorthWest::toggle();
northWest.toggle();
modm::delay(100ms);
}

Expand Down
119 changes: 0 additions & 119 deletions src/modm/platform/gpio/stm32/gpio_accessor.hpp.in

This file was deleted.

2 changes: 1 addition & 1 deletion src/modm/platform/gpio/stm32/module.lb
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ def build(env):
env.template("unused.hpp.in")
if len(env["enable_ports"]):
env.template("enable.cpp.in")
env.template("gpio_accessor.hpp.in")
env.template("runtime_gpio.hpp.in")

env.copy("random_access_port.hpp")
env.copy("../common/inverted.hpp", "inverted.hpp")
Expand Down
60 changes: 30 additions & 30 deletions src/modm/platform/gpio/stm32/random_access_port.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include <algorithm>

#include "software_port.hpp"
#include "gpio_accessor.hpp"
#include "runtime_gpio.hpp"

namespace modm::platform
{
Expand All @@ -37,7 +37,7 @@ namespace modm::platform
*
* Port port;
* // set pin C8
* GpioAccessor pin = port[0];
* RtGpio pin = port[0];
* pin.set();
*
* auto reversedPins = port | std::views::reverse;
Expand All @@ -56,10 +56,10 @@ class RandomAccessPort : public SoftwareGpioPort<Gpios...>
{
public:
using Index = int_fast8_t;
static constexpr inline size_t Size{sizeof...(Gpios)};
static constexpr size_t Size{sizeof...(Gpios)};

private:
static constexpr inline std::array<GpioAccessor, Size> GpioPins = [](){
static constexpr std::array<RtGpio, Size> gpioPins = [](){
auto gpios = makeGpioArray<Gpios...>();
// Reverse pin order to match SoftwareGpioPort indexing
std::reverse(gpios.begin(), gpios.end());
Expand All @@ -77,53 +77,53 @@ class RandomAccessPort : public SoftwareGpioPort<Gpios...>
public:
using iterator_category = std::random_access_iterator_tag;
using difference_type = Index;
using value_type = GpioAccessor;
using reference = const GpioAccessor&;
using value_type = RtGpio;
using reference = RtGpio&;

constexpr GpioIterator() = default;
constexpr explicit GpioIterator(Index index) : index_{index}
{}

constexpr inline GpioIterator&
constexpr GpioIterator&
operator+=(difference_type diff) { index_ += diff; return *this; }

constexpr inline GpioIterator&
constexpr GpioIterator&
operator-=(difference_type diff) { index_ -= diff; return *this; }

constexpr inline reference
operator*() const { return RandomAccessPort::GpioPins[index_]; }
constexpr reference
operator*() const { return RandomAccessPort::gpioPins[index_]; }

constexpr inline reference
operator[](difference_type diff) const { return RandomAccessPort::GpioPins[index_ + diff]; }
constexpr reference
operator[](difference_type diff) const { return RandomAccessPort::gpioPins[index_ + diff]; }

constexpr inline GpioIterator&
constexpr GpioIterator&
operator++() { ++index_; return *this; }

constexpr inline GpioIterator&
constexpr GpioIterator&
operator--() { --index_; return *this; }

constexpr inline GpioIterator
constexpr GpioIterator
operator++(int) { GpioIterator tmp{*this}; ++index_; return tmp; }

constexpr inline GpioIterator
constexpr GpioIterator
operator--(int) { GpioIterator tmp{*this}; --index_; return tmp; }

constexpr inline difference_type
constexpr difference_type
operator-(const GpioIterator& rhs) const { return index_ - rhs.index_; }

constexpr inline GpioIterator
constexpr GpioIterator
operator+(difference_type diff) const { return GpioIterator{index_ + diff}; }

constexpr inline GpioIterator
constexpr GpioIterator
operator-(difference_type diff) const { return GpioIterator{index_ - diff}; }

friend constexpr inline GpioIterator
friend constexpr GpioIterator
operator+(difference_type lhs, const GpioIterator& rhs) { return GpioIterator{lhs + rhs.index_}; }

friend constexpr inline GpioIterator
friend constexpr GpioIterator
operator-(difference_type lhs, const GpioIterator& rhs) { return GpioIterator{lhs - rhs.index_}; }

constexpr inline auto
constexpr auto
operator<=>(const GpioIterator& rhs) const = default;
};
static_assert(std::random_access_iterator<GpioIterator>);
Expand All @@ -134,40 +134,40 @@ class RandomAccessPort : public SoftwareGpioPort<Gpios...>
constexpr GpioIterator
end() const { return GpioIterator{Size}; }

constexpr const GpioAccessor&
constexpr const RtGpio&
operator[](Index index) const
{
return GpioPins[index];
return gpioPins[index];
}

static void set(Index index, bool enable)
{
GpioPins[index].set(enable);
gpioPins[index].set(enable);
}

static void set(Index index)
{
GpioPins[index].set();
gpioPins[index].set();
}

static void reset(Index index)
{
GpioPins[index].reset();
gpioPins[index].reset();
}

static void toggle(Index index)
{
GpioPins[index].toggle();
gpioPins[index].toggle();
}

static bool read(Index index)
{
return GpioPins[index].read();
return gpioPins[index].read();
}

static bool isSet(Index index)
{
return GpioPins[index].isSet();
return gpioPins[index].isSet();
}
};

Expand Down
Loading

0 comments on commit bf44fe8

Please sign in to comment.