Skip to content

Commit

Permalink
[rpi] Adds basic support for GPIO on Raspberry Pi boards
Browse files Browse the repository at this point in the history
  • Loading branch information
Erik Henriksson committed Aug 14, 2020
1 parent 824f6b5 commit 8f79fca
Show file tree
Hide file tree
Showing 11 changed files with 403 additions and 1 deletion.
26 changes: 26 additions & 0 deletions examples/rpi/blinky/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* 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/.
*/

#include <modm/debug/logger.hpp>
#include <modm/board.hpp>

using namespace Board;

int main()
{
Board::initialize();
GpioPin<0>::setOutput();
MODM_LOG_INFO << "Blink blink...";
for (int i = 0; i < 10; ++i) {
GpioPin<0>::toggle();
modm::delay(500ms);
}
return 0;
}
9 changes: 9 additions & 0 deletions examples/rpi/blinky/project.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<library>
<extends>modm:raspberrypi-4</extends>
<options>
<option name="modm:build:build.path">../../../build/rpi/blinky</option>
</options>
<modules>
<module>modm:build:scons</module>
</modules>
</library>
32 changes: 32 additions & 0 deletions src/modm/board/raspberrypi_4/board.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* 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 <modm/debug/logger.hpp>
#include <modm/platform.hpp>

#include <wiringPi.h>

using namespace modm::platform;

namespace Board
{

struct SystemClock {};

inline void
initialize()
{
wiringPiSetup();
}

} // Board namespace
14 changes: 14 additions & 0 deletions src/modm/board/raspberrypi_4/board.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<library>
<repositories>
<repository>
<path>../../../../repo.lb</path>
</repository>
</repositories>

<options>
<option name="modm:target">hosted-rpi</option>
</options>
<modules>
<module>modm:board:raspberrypi-4</module>
</modules>
</library>
29 changes: 29 additions & 0 deletions src/modm/board/raspberrypi_4/module.lb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# 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/.
# -----------------------------------------------------------------------------

def init(module):
module.name = ":board:raspberrypi-4"
module.description = """\
# Raspberry Pi 4
"""

def prepare(module, options):
if options[":target"].partname != "hosted-rpi":
return False

module.depends(":platform:core", ":platform:gpio", ":debug")
return True

def build(env):
env.outbasepath = "modm/src/modm/board"
env.copy('.')

2 changes: 1 addition & 1 deletion src/modm/platform/core/hosted/delay_impl.hpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

#define MODM_DELAY_NS_IS_ACCURATE 0

%% if target.family in ["darwin", "linux"]
%% if target.family in ["darwin", "linux", "rpi"]
extern "C" {
#include <unistd.h>
}
Expand Down
69 changes: 69 additions & 0 deletions src/modm/platform/gpio/rpi/base.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* 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 <modm/architecture/interface/gpio.hpp>

namespace modm
{

namespace platform
{

/// @ingroup modm_platform_gpio
enum class
Peripheral
{
BitBang,
// ...
};

/// @ingroup modm_platform_gpio
struct Gpio
{
/// Each Input Pin can be configured in one of these states.
enum class
InputType : uint8_t
{
Floating, ///< The input pin is left floating
};

enum class
OutputType : uint8_t
{
PushPull ///< push-pull on output
};

/// Available ports on this device.
enum class
Port
{
// ...
};

enum class
Signal
{
BitBang,
// ...
};

/// @endcond
};

/// @}

} // namespace platform

} // namespace modm

#endif
36 changes: 36 additions & 0 deletions src/modm/platform/gpio/rpi/module.lb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# 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/.
# -----------------------------------------------------------------------------


def init(module):
module.name = ":platform:gpio"
module.description = "Hosted GPIO for Raspberry Pi"


def prepare(module, options):
if not options[":target"].has_driver("gpio"):
return False

module.depends(":architecture:gpio")
return True


def build(env):
env.substitutions = {"target": env[":target"].identifier}
env.outbasepath = "modm/src/modm/platform/gpio"

env.collect(":build:library", "wiringPi")

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")
81 changes: 81 additions & 0 deletions src/modm/platform/gpio/rpi/pin.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright (c) 2017, Niklas Hauser
* Copyright (c) 2018, Fabian Greif
*
* 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 <modm/architecture/interface/gpio.hpp>
#include <wiringPi.h>

#include "base.hpp"


namespace modm
{

namespace platform
{

template< int Pin >
class GpioPin : public Gpio, public modm::GpioIO
{
static Gpio::InputType inputType;
public:
using Output = GpioPin<Pin>;
using Input = GpioPin<Pin>;
using IO = GpioPin<Pin>;
using Type = GpioPin<Pin>;

public:
inline static void configure(Gpio::InputType type) {}
modm_always_inline static void setInput() {
pinMode(Pin, INPUT);
}
inline static void setInput(Gpio::InputType type) {
setInput();
}
modm_always_inline static void setOutput() {
pinMode(Pin, OUTPUT);
}
modm_always_inline static void setOutput(OutputType) {
setOutput();
}
modm_always_inline static void setOutput(bool status) {
setOutput();
set(status);
}
inline static void set() {
digitalWrite(Pin, HIGH);
}
modm_always_inline static void reset() {
digitalWrite(Pin, LOW);
}
inline static void set(bool status) {
digitalWrite(Pin, status);
}
inline static bool isSet() {
return digitalRead(Pin);
}
inline static void toggle() {
if (isSet()) { set(); }
else { reset(); }
}
modm_always_inline static modm::Gpio::Direction getDirection() {
return modm::Gpio::Direction::Out;
}
};

/// @}

} // namespace platform

} // namespace modm

Loading

0 comments on commit 8f79fca

Please sign in to comment.