Skip to content

Commit

Permalink
Port pygamer to the v2 APIs
Browse files Browse the repository at this point in the history
Port the pygamer BSP to the `gpio::v2` and `sercom::v2::spi` APIs. While
implementing this, I discovered that the pygamer uses an undocumented
combination of pins for `Sercom1`. It uses PA17, PB22 & PB23. The
datasheet indicates that PA17 is in `IoSet1`, while the other two pins are
in `IoSet3`. It shouldn't be possible to use these together, but it
appears there is an undocumented `IoSet`. Add this as `IoSet5` for
`Sercom1`.

Also fix a warning from a trinket_m0 example.
  • Loading branch information
bradleyharden committed Jun 26, 2021
1 parent 8c40356 commit 1a9bcb3
Show file tree
Hide file tree
Showing 24 changed files with 639 additions and 414 deletions.
7 changes: 4 additions & 3 deletions boards/pygamer/examples/blinky_basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
#![no_std]
#![no_main]

use bsp::{entry, hal, pac, Pins, RedLed};
#[cfg(not(feature = "panic_led"))]
use panic_halt as _;
use pygamer::{self as hal, entry, pac, Pins};
use pygamer as bsp;

use hal::clock::GenericClockController;
use hal::delay::Delay;
Expand All @@ -27,8 +28,8 @@ fn main() -> ! {
let mut delay = Delay::new(core.SYST, &mut clocks);
delay.delay_ms(400u16);

let mut pins = Pins::new(peripherals.PORT);
let mut red_led = pins.d13.into_open_drain_output(&mut pins.port);
let pins = Pins::new(peripherals.PORT);
let mut red_led: RedLed = pins.d13.into();

let mut wdt = Watchdog::new(peripherals.WDT);
wdt.start(WatchdogTimeout::Cycles256 as u8);
Expand Down
12 changes: 6 additions & 6 deletions boards/pygamer/examples/button_rtic.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
#![no_std]
#![no_main]

use bsp::{hal, ButtonReader, Keys, Pins, RedLed};
#[cfg(not(feature = "panic_led"))]
use panic_halt as _;
use pygamer::{self as hal, pins::ButtonReader, pins::Keys, Pins};
use pygamer as bsp;

use hal::clock::GenericClockController;
use hal::gpio::{OpenDrain, Output, Pa23};
use hal::prelude::*;
use rtic::app;

#[app(device = crate::hal::pac, peripherals = true)]
const APP: () = {
struct Resources {
red_led: Pa23<Output<OpenDrain>>,
red_led: RedLed,
timer: hal::timer::TimerCounter3,
buttons: ButtonReader,
}
Expand Down Expand Up @@ -50,7 +50,7 @@ const APP: () = {
&mut device.NVMCTRL,
);

let mut pins = Pins::new(device.PORT).split();
let pins = Pins::new(device.PORT).split();

let gclk0 = clocks.gclk0();
let timer_clock = clocks.tc2_tc3(&gclk0).unwrap();
Expand All @@ -61,8 +61,8 @@ const APP: () = {
tc3.enable_interrupt();

init::LateResources {
buttons: pins.buttons.init(&mut pins.port),
red_led: pins.led_pin.into_open_drain_output(&mut pins.port),
buttons: pins.buttons.init(),
red_led: pins.led_pin.into(),
timer: tc3,
}
}
Expand Down
8 changes: 5 additions & 3 deletions boards/pygamer/examples/clock_out.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
#![no_std]
#![no_main]

use bsp::{entry, hal, pac, Pins};
#[cfg(not(feature = "panic_led"))]
use panic_halt as _;
use pygamer::{self as hal, entry, pac, Pins};
use pygamer as bsp;

use hal::clock::GenericClockController;
use hal::gpio::v2::AlternateM;
use pac::gclk::genctrl::SRC_A::DPLL0;
use pac::gclk::pchctrl::GEN_A::GCLK2;
use pac::Peripherals;
Expand All @@ -22,12 +24,12 @@ fn main() -> ! {
&mut peripherals.OSCCTRL,
&mut peripherals.NVMCTRL,
);
let mut pins = Pins::new(peripherals.PORT);
let pins = Pins::new(peripherals.PORT);

//3mhz
let _gclk2 = clocks
.configure_gclk_divider_and_source(GCLK2, 40, DPLL0, false)
.unwrap();
pins.d5.into_function_m(&mut pins.port);
pins.d5.into_mode::<AlternateM>();
loop {}
}
6 changes: 3 additions & 3 deletions boards/pygamer/examples/ferris_img.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
#![no_std]
#![no_main]

use bsp::{entry, hal, pac, Pins};
#[cfg(not(feature = "panic_led"))]
use panic_halt as _;
use pygamer::{self as hal, entry, pac, Pins};
use pygamer as bsp;

use embedded_graphics::pixelcolor::{Rgb565, RgbColor};
use embedded_graphics::prelude::*;
Expand All @@ -32,7 +33,7 @@ fn main() -> ! {
&mut peripherals.OSCCTRL,
&mut peripherals.NVMCTRL,
);
let mut pins = Pins::new(peripherals.PORT).split();
let pins = Pins::new(peripherals.PORT).split();
let mut delay = hal::delay::Delay::new(core.SYST, &mut clocks);

let (mut display, _backlight) = pins
Expand All @@ -43,7 +44,6 @@ fn main() -> ! {
&mut peripherals.MCLK,
peripherals.TC2,
&mut delay,
&mut pins.port,
)
.unwrap();

Expand Down
9 changes: 5 additions & 4 deletions boards/pygamer/examples/neopixel_adc_battery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
#![no_std]
#![no_main]

use bsp::{entry, hal, pac, Pins};
#[cfg(not(feature = "panic_led"))]
use panic_halt as _;
use pygamer::{self as hal, entry, pac, Pins};
use pygamer as bsp;

use hal::adc::Adc;
use hal::pac::gclk::pchctrl::GEN_A::GCLK11;
Expand All @@ -29,14 +30,14 @@ fn main() -> ! {
&mut peripherals.OSCCTRL,
&mut peripherals.NVMCTRL,
);
let mut pins = Pins::new(peripherals.PORT).split();
let pins = Pins::new(peripherals.PORT).split();

let mut adc0 = Adc::adc0(peripherals.ADC0, &mut peripherals.MCLK, &mut clocks, GCLK11);
let mut battery = pins.battery.init(&mut pins.port);
let mut battery = pins.battery.init();

// neopixels
let timer = SpinTimer::new(4);
let mut neopixel = pins.neopixel.init(timer, &mut pins.port);
let mut neopixel = pins.neopixel.init(timer);

let mut delay = Delay::new(core.SYST, &mut clocks);

Expand Down
10 changes: 6 additions & 4 deletions boards/pygamer/examples/neopixel_adc_light.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
#![no_std]
#![no_main]

use bsp::{entry, hal, pac, Pins};
#[cfg(not(feature = "panic_led"))]
use panic_halt as _;
use pygamer::{self as hal, entry, pac, Pins};
use pygamer as bsp;

use embedded_hal::digital::v1_compat::OldOutputPin;
use hal::adc::Adc;
use hal::gpio::v2::AlternateB;
use hal::prelude::*;
use hal::timer::SpinTimer;
use hal::{clock::GenericClockController, delay::Delay};
Expand All @@ -32,13 +34,13 @@ fn main() -> ! {
&mut peripherals.OSCCTRL,
&mut peripherals.NVMCTRL,
);
let mut pins = Pins::new(peripherals.PORT);
let pins = Pins::new(peripherals.PORT);

let mut adc1 = Adc::adc1(peripherals.ADC1, &mut peripherals.MCLK, &mut clocks, GCLK11);
let mut light = pins.light.into_function_b(&mut pins.port);
let mut light = pins.light.into_mode::<AlternateB>();

let timer = SpinTimer::new(4);
let neopixel_pin: OldOutputPin<_> = pins.neopixel.into_push_pull_output(&mut pins.port).into();
let neopixel_pin: OldOutputPin<_> = pins.neopixel.into_push_pull_output().into();
let mut neopixel = ws2812::Ws2812::new(timer, neopixel_pin);

let mut delay = Delay::new(core.SYST, &mut clocks);
Expand Down
13 changes: 7 additions & 6 deletions boards/pygamer/examples/neopixel_button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@
#![no_std]
#![no_main]

use bsp::{entry, hal, pac, Keys, Pins};
#[cfg(not(feature = "panic_led"))]
use panic_halt as _;
use pygamer::{self as hal, entry, pac, pins::Keys, Pins};
use pygamer as bsp;

use bsp::util::map_from;
use hal::adc::Adc;
use hal::prelude::*;
use hal::timer::SpinTimer;
use hal::util::map_from;
use hal::{clock::GenericClockController, delay::Delay};
use pac::gclk::pchctrl::GEN_A::GCLK11;
use pac::{CorePeripherals, Peripherals};
Expand All @@ -38,17 +39,17 @@ fn main() -> ! {
);

let mut delay = Delay::new(core_peripherals.SYST, &mut clocks);
let mut pins = Pins::new(peripherals.PORT).split();
let pins = Pins::new(peripherals.PORT).split();

let mut buttons = pins.buttons.init(&mut pins.port);
let mut buttons = pins.buttons.init();

let mut adc1 = Adc::adc1(peripherals.ADC1, &mut peripherals.MCLK, &mut clocks, GCLK11);
let mut joystick = pins.joystick.init(&mut pins.port);
let mut joystick = pins.joystick.init();

// neopixels
let timer = SpinTimer::new(4);

let mut neopixel = pins.neopixel.init(timer, &mut pins.port);
let mut neopixel = pins.neopixel.init(timer);

const NUM_LEDS: usize = 5;
let mut pos_button: usize = 2;
Expand Down
7 changes: 4 additions & 3 deletions boards/pygamer/examples/neopixel_easing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
#![no_std]
#![no_main]

use bsp::{entry, hal, pac, Pins};
#[cfg(not(feature = "panic_led"))]
use panic_halt as _;
use pygamer::{self as hal, entry, pac, Pins};
use pygamer as bsp;

use core::f32::consts::FRAC_PI_2;
use hal::clock::GenericClockController;
Expand All @@ -33,10 +34,10 @@ fn main() -> ! {
&mut peripherals.NVMCTRL,
);

let mut pins = Pins::new(peripherals.PORT).split();
let pins = Pins::new(peripherals.PORT).split();
let timer = SpinTimer::new(4);

let mut neopixel = pins.neopixel.init(timer, &mut pins.port);
let mut neopixel = pins.neopixel.init(timer);
let mut delay = Delay::new(core.SYST, &mut clocks);

let trng = Trng::new(&mut peripherals.MCLK, peripherals.TRNG);
Expand Down
41 changes: 14 additions & 27 deletions boards/pygamer/examples/neopixel_rainbow_spi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
#![no_std]
#![no_main]

use bsp::{entry, hal, pac, Pins};
#[cfg(not(feature = "panic_led"))]
use panic_halt as _;
use pygamer::{self as hal, entry, pac, Pins};
use pygamer as bsp;

use hal::prelude::*;
use hal::sercom::PadPin;
use hal::time::MegaHertz;
use hal::sercom::v2::spi;
use hal::{clock::GenericClockController, delay::Delay};
use pac::{CorePeripherals, Peripherals};
use smart_leds::hsv::{hsv2rgb, Hsv};
Expand All @@ -31,32 +31,20 @@ fn main() -> ! {
&mut peripherals.OSCCTRL,
&mut peripherals.NVMCTRL,
);
let mut pins = Pins::new(peripherals.PORT);
let pins = Pins::new(peripherals.PORT);
let mclk = &mut peripherals.MCLK;
let gclk = clocks.gclk0();

let spi: hal::sercom::SPIMaster2<
hal::sercom::Sercom2Pad0<hal::gpio::Pa12<hal::gpio::PfC>>,
hal::sercom::Sercom2Pad3<hal::gpio::Pa15<hal::gpio::PfC>>,
hal::sercom::Sercom2Pad1<hal::gpio::Pa13<hal::gpio::PfC>>,
> = hal::sercom::SPIMaster2::new(
&clocks.sercom2_core(&gclk).unwrap(),
MegaHertz(3),
embedded_hal::spi::Mode {
phase: embedded_hal::spi::Phase::CaptureOnFirstTransition,
polarity: embedded_hal::spi::Polarity::IdleLow,
},
peripherals.SERCOM2,
&mut peripherals.MCLK,
(
pins.sda.into_pad(&mut pins.port),
pins.neopixel.into_pad(&mut pins.port),
pins.scl.into_pad(&mut pins.port),
),
);

let clock = &clocks.sercom2_core(&gclk).unwrap();
let sercom2 = peripherals.SERCOM2;
let pads = spi::Pads::default()
.data_in(pins.sda)
.data_out(pins.neopixel)
.sclk(pins.scl);
let spi = spi::Config::new(mclk, sercom2, pads, clock.freq())
.baud(3.mhz())
.enable();
let mut neopixel = ws2812::Ws2812::new(spi);
let mut delay = Delay::new(core.SYST, &mut clocks);

loop {
for j in 0..255u8 {
let colors = [
Expand Down Expand Up @@ -88,7 +76,6 @@ fn main() -> ! {
val: 32,
}),
];

neopixel.write(colors.iter().cloned()).unwrap();
delay.delay_ms(5u8);
}
Expand Down
7 changes: 4 additions & 3 deletions boards/pygamer/examples/neopixel_rainbow_timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
#![no_std]
#![no_main]

use bsp::{entry, hal, Pins};
#[cfg(not(feature = "panic_led"))]
use panic_halt as _;
use pygamer::{self as hal, entry, Pins};
use pygamer as bsp;

use hal::pac::{CorePeripherals, Peripherals};
use hal::prelude::*;
Expand All @@ -30,14 +31,14 @@ fn main() -> ! {
&mut peripherals.OSCCTRL,
&mut peripherals.NVMCTRL,
);
let mut pins = Pins::new(peripherals.PORT).split();
let pins = Pins::new(peripherals.PORT).split();

let gclk0 = clocks.gclk0();
let timer_clock = clocks.tc2_tc3(&gclk0).unwrap();
let mut timer = TimerCounter::tc3_(&timer_clock, peripherals.TC3, &mut peripherals.MCLK);
timer.start(3.mhz());

let mut neopixel = pins.neopixel.init(timer, &mut pins.port);
let mut neopixel = pins.neopixel.init(timer);
let mut delay = Delay::new(core.SYST, &mut clocks);

loop {
Expand Down
8 changes: 4 additions & 4 deletions boards/pygamer/examples/neopixel_tilt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
#![no_std]
#![no_main]

use bsp::{entry, hal, pac, Pins};
#[cfg(not(feature = "panic_led"))]
use panic_halt as _;
use pygamer::{self as hal, entry, pac, Pins};
use pygamer as bsp;

use hal::prelude::*;
use hal::time::KiloHertz;
Expand All @@ -34,19 +35,18 @@ fn main() -> ! {
);

let mut delay = Delay::new(core_peripherals.SYST, &mut clocks);
let mut pins = Pins::new(peripherals.PORT).split();
let pins = Pins::new(peripherals.PORT).split();

// neopixels
let timer = SpinTimer::new(4);
let mut neopixel = pins.neopixel.init(timer, &mut pins.port);
let mut neopixel = pins.neopixel.init(timer);

// i2c
let i2c = pins.i2c.init(
&mut clocks,
KiloHertz(400),
peripherals.SERCOM2,
&mut peripherals.MCLK,
&mut pins.port,
);

let mut lis3dh = Lis3dh::new(i2c, 0x19).unwrap();
Expand Down
Loading

0 comments on commit 1a9bcb3

Please sign in to comment.