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

WIP: RP2040 PIO support #1212

Draft
wants to merge 7 commits into
base: develop
Choose a base branch
from
Draft
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
79 changes: 79 additions & 0 deletions examples/rp_pico/pio_ws2812/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Copyright (c) 2016, Sascha Schade
* 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/.
*/
// ----------------------------------------------------------------------------

#include <modm/board.hpp>
#include "ws2812.pio.hpp"
using namespace Board;


struct HW
{
using PIO = modm::platform::Pio0;
using PIO_SM = PIO::StateMachine<0>;
using DataGpio = modm::platform::GpioOutput23;
};


int
main()
{


Board::initialize();

HW::DataGpio::setOutput(Gpio::OutputType::PushPull, Gpio::SlewRate::Fast);
HW::DataGpio::setDriveStrength(Gpio::DriveStrength::mA_12);

auto pio_program_offset = HW::PIO::addProgram(ws2812::program);

ws2812::init<Board::SystemClock,HW::PIO,HW::PIO_SM,HW::DataGpio>(pio_program_offset);

constexpr auto delay_val = 5ms;

while (true)
{
uint32_t clr = 0;
while (clr!=0xff0000) {
clr = clr + 0x010000;
HW::PIO_SM::writeBlocking(clr);
modm::delay(delay_val);
}
while (clr!=0x000000) {
clr = clr - 0x010000;
HW::PIO_SM::writeBlocking(clr);
modm::delay(delay_val);
}
while (clr!=0x00ff00) {
clr = clr + 0x000100;
HW::PIO_SM::writeBlocking(clr);
modm::delay(delay_val);
}
while (clr!=0x000000) {
clr = clr - 0x000100;
HW::PIO_SM::writeBlocking(clr);
modm::delay(delay_val);
}
while (clr!=0x0000ff) {
clr = clr + 0x000001;
HW::PIO_SM::writeBlocking(clr);
modm::delay(delay_val);
}
while (clr!=0x000000) {
clr = clr - 0x000001;
HW::PIO_SM::writeBlocking(clr);
modm::delay(delay_val);
}

}

return 0;
}
10 changes: 10 additions & 0 deletions examples/rp_pico/pio_ws2812/project.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<library>
<extends>modm:rp-pico</extends>
<options>
<option name="modm:build:build.path">../../../build/rp_pico/pio_ws2812</option>
</options>
<modules>
<module>modm:build:scons</module>
<module>modm:platform:pio:0</module>
</modules>
</library>
96 changes: 96 additions & 0 deletions examples/rp_pico/pio_ws2812/ws2812.pio.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#pragma once

#include <modm/board.hpp>

/* from https://github.com/raspberrypi/pico-examples/blob/master/pio/ws2812/ws2812.pio */

/* .program ws2812 */
namespace ws2812
{
using namespace modm::platform::pio;

// constants WS2812
// static constexpr uint32_t T0H = 350; // ns
// static constexpr uint32_t T0L = 800;
// static constexpr uint32_t T1H = 700;
// static constexpr uint32_t T1L = 600;

// constants WS2812B
static constexpr uint32_t T0H = 400; // ns
static constexpr uint32_t T0L = 850;
static constexpr uint32_t T1H = 850;
static constexpr uint32_t T1L = 400;

static constexpr uint32_t TimeScale = 50;

static constexpr uint32_t TH_Common = T0H/TimeScale;
static constexpr uint32_t TH_Add = (T1H-T0H)/TimeScale;
static constexpr uint32_t TL_Common = T1L/TimeScale;
static constexpr uint32_t TL_Add = (T0L-T1L)/TimeScale;

// labels
struct bitloop {};
struct do_one {};
struct do_zero {};

/*
.side_set 1
.wrap_target
bitloop:
out x, 1 side 0 [T3 - 1] ; Side-set still takes place when instruction stalls
jmp !x do_zero side 1 [T1 - 1] ; Branch on the bit we shifted out. Positive pulse
do_one:
jmp bitloop side 1 [T2 - 1] ; Continue driving high, for a long pulse
do_zero:
nop side 0 [T2 - 1] ; Or drive low, for a short pulse
.wrap
*/

// PIO program
static constexpr auto program = PIOProgram::begin()
.sideset<1>
.wrapTarget<>
.label<bitloop>
.instr(pio::Out.x<1> .side<0>.delay<TL_Common-1>)
.instr(pio::Jmp.not_x.to<do_zero> .side<1>.delay<TH_Common-1>)
.label<do_one>
.instr(pio::Jmp.to<bitloop> .side<1>.delay<TH_Add-1>)
.label<do_zero>
.instr(pio::Nop .side<0>.delay<TL_Add-1>)
.wrap<>
.end();

static_assert(program.code[0]==0x6721,"invalid program code[0]");
static_assert(program.code[1]==0x1723,"invalid program code[1]");
/*
pio_gpio_init(pio, pin);
pio_sm_set_consecutive_pindirs(pio, sm, pin, 1, true);

pio_sm_config c = ws2812_program_get_default_config(offset);
sm_config_set_sideset_pins(&c, pin);
sm_config_set_out_shift(&c, false, true, rgbw ? 32 : 24);
sm_config_set_fifo_join(&c, PIO_FIFO_JOIN_TX);

int cycles_per_bit = ws2812_T1 + ws2812_T2 + ws2812_T3;
float div = clock_get_hz(clk_sys) / (freq * cycles_per_bit);
sm_config_set_clkdiv(&c, div);

pio_sm_init(pio, sm, offset, &c);
pio_sm_set_enabled(pio, sm, true);
*/
// initialization code
template <typename SystemClock,typename PIO,typename SM,typename DataGpio>
static constexpr void init(uint16_t offset) {
PIO::template connect<typename DataGpio::Pad>();
SM::template addOutput<DataGpio>();

SM::config(offset,program)
.template setSidesetPins<DataGpio>()
.template setOutShift<false,true,24>()
.setFifoJoinTx()
.template setFrequency<SystemClock,1000000000/TimeScale>()
.init(offset+program.getOffset<bitloop>());
SM::setEnabled(true);
}

}
7 changes: 0 additions & 7 deletions src/modm/platform/gpio/rp/base.hpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,6 @@ struct Gpio
PushPull
};

enum class
OutputSpeed
{
Slow = 0,
Fast = 1,
};

enum class
DriveStrength
{
Expand Down
11 changes: 8 additions & 3 deletions src/modm/platform/gpio/rp/set.hpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@ public:
setOutput();
}

static void setOutput(OutputType type, OutputSpeed speed = OutputSpeed::Slow)
static void setOutput(OutputType type, SlewRate speed = SlewRate::Slow)
{
setOutput();
configure(type, speed);
}

static void configure(OutputType, OutputSpeed speed = OutputSpeed::Slow)
static void configure(OutputType, SlewRate speed = SlewRate::Slow)
{
(PortRegs<Gpios::port>::set_speed(Gpios::pin, speed), ...);
(PortRegs<Gpios::port>::set_slewrate(Gpios::pin, uint8_t(speed)), ...);
}

static void setInput()
Expand All @@ -96,6 +96,11 @@ public:
(PortRegs<Gpios::port>::set_slewrate(Gpios::pin, uint8_t(rate)), ...);
}

static void setDriveStrength(DriveStrength drive)
{
(PortRegs<Gpios::port>::set_drive(Gpios::pin, uint8_t(drive)), ...);
}

static void set()
{
%% for port, id in ports.items()
Expand Down
6 changes: 3 additions & 3 deletions src/modm/platform/gpio/rp/static.hpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ public:

static void setFunction(uint8_t func) { Regs::set_funcsel(pin, func); }
static void setDriveStrength(DriveStrength strength)
{ Regs::set_drive(pin, strength); }
{ Regs::set_drive(pin, uint8_t(strength)); }

public:
static void setOutput() { PinSet::setOutput(); }
static void setOutput(bool value) { PinSet::setOutput(value); }
static void setOutput(OutputType type, OutputSpeed speed=OutputSpeed::Slow)
static void setOutput(OutputType type, SlewRate speed=SlewRate::Slow)
{ PinSet::setOutput(type, speed); }
static void configure(OutputType type, OutputSpeed speed=OutputSpeed::Slow)
static void configure(OutputType type, SlewRate speed=SlewRate::Slow)
{ PinSet::configure(type, speed); }

static void set() { PinSet::set(); }
Expand Down
66 changes: 66 additions & 0 deletions src/modm/platform/pio/rp/module.lb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright (c) 2024, Andrey Kunitsyn
#
# 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/.
# -----------------------------------------------------------------------------


class Instance(Module):
def __init__(self, instance):
self.instance = instance

def init(self, module):
module.name = str(self.instance)
module.description = "PIO {} instance".format(self.instance)

def prepare(self, module, options):
return True

def build(self, env):
properties = {
"id": self.instance,
}
env.substitutions = properties
env.outbasepath = "modm/src/modm/platform/pio"

env.template("pio.hpp.in", "pio_{}.hpp".format(self.instance))
env.template("pio.cpp.in", "pio_{}.cpp".format(self.instance))



def init(module):
module.name = ":platform:pio"
module.description = "Programmable IO block (PIO)"

def prepare(module, options):
device = options[":target"]
if not device.has_driver("pio:rp*"):
return False

module.depends(
":platform:gpio",
":platform:clockgen",
":architecture:interrupt",
":processing:resumable")

for instance in listify(device.get_driver("pio")["instance"]):
module.add_submodule(Instance(instance))

return True

def build(env):
env.substitutions = {
"use_fiber": env.query(":processing:fiber:__enabled", False)
}
env.outbasepath = "modm/src/modm/platform/pio"
env.copy("pio_asm.hpp")
env.copy("pio_program.hpp")
env.copy("pio_sm.hpp")
env.template("pio_sm_impl.hpp.in", "pio_sm_impl.hpp")
pass
39 changes: 39 additions & 0 deletions src/modm/platform/pio/rp/pio.cpp.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (c) 2022, Andrey Kunitsyn
*
* 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/architecture/driver/atomic/queue.hpp>
#include <modm/architecture/interface/atomic_lock.hpp>
#include <modm/architecture/interface/interrupt.hpp>
#include <modm/platform/core/resets.hpp>

#include "../device.hpp"

#include "pio_{{ id }}.hpp"

// ----------------------------------------------------------------------------

namespace modm::platform
{
uint32_t Pio{{ id }}::used_instruction_space = 0;
}



// ----------------------------------------------------------------------------
MODM_ISR(PIO{{ id }}_IRQ_0)
{

}

MODM_ISR(PIO{{ id }}_IRQ_1)
{

}
Loading
Loading