Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[rpi] Adds basic support for GPIO on Raspberry Pi boards #444

Merged
merged 3 commits into from
Sep 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ STM32 devices, however, there are different levels of support and testing.
| STM32F2 | ★★★★ | STM32F3 | ★★★★★ | STM32F4 | ★★★★★ |
| STM32F7 | ★★★★ | STM32L1 | ★★★★ | STM32L4 | ★★★★ |
| STM32L4+ | ★★★★ | STM32G0 | ★★★★ | STM32G4 | ★★★★ |
| SAMD21 | ★★ | | | | |
| SAMD21 | ★★ | Raspberry Pi | ★ | | |

</center>

Expand Down Expand Up @@ -157,6 +157,7 @@ documentation.
<td align="center">NUCLEO-L476RG</td>
<td align="center">OLIMEXINO-STM32</td>
</tr><tr>
<td align="center">RASPBERRYPI</td>
<td align="center">SAMD21-MINI</td>
<td align="center">STM32F030F4P6-DEMO</td>
</tr>
Expand Down
28 changes: 28 additions & 0 deletions examples/rpi/blinky/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* 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</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/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/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</module>
</modules>
</library>
29 changes: 29 additions & 0 deletions src/modm/board/raspberrypi/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"
module.description = """\
# Raspberry Pi
"""

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:rpi"):
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