Update dependencies
This commit is contained in:
@ -31,8 +31,8 @@ async fn main(_spawner: Spawner) {
|
||||
let _led = Output::new(board.d13, Level::High);
|
||||
let _neopixel_power = Output::new(board.neopixel_power, Level::High);
|
||||
|
||||
let mut neopixel = Ws2812::new(board.PIO0, board.DMA_CH0, board.neopixel.degrade());
|
||||
let neopixels_d5 = Ws2812::new(board.PIO1, board.DMA_CH1, board.d5.degrade());
|
||||
let mut neopixel = Ws2812::new(board.PIO0, board.DMA_CH0, board.neopixel);
|
||||
let neopixels_d5 = Ws2812::new(board.PIO1, board.DMA_CH1, board.d5);
|
||||
|
||||
neopixel.write(&[Rgb::new(0xFF, 0x00, 0x00)]).await;
|
||||
|
||||
|
||||
@ -31,8 +31,8 @@ async fn main(_spawner: Spawner) {
|
||||
let _led = Output::new(board.d13, Level::High);
|
||||
let _neopixel_power = Output::new(board.neopixel_power, Level::High);
|
||||
|
||||
let mut neopixel = Ws2812::new(board.PIO0, board.DMA_CH0, board.neopixel.degrade());
|
||||
let neopixels_d5 = Ws2812::new(board.PIO1, board.DMA_CH1, board.d5.degrade());
|
||||
let mut neopixel = Ws2812::new(board.PIO0, board.DMA_CH0, board.neopixel);
|
||||
let neopixels_d5 = Ws2812::new(board.PIO1, board.DMA_CH1, board.d5);
|
||||
|
||||
neopixel.write(&[Rgb::new(0xFF, 0x00, 0x00)]).await;
|
||||
|
||||
|
||||
@ -6,7 +6,7 @@ use alloc::{boxed::Box, vec::Vec};
|
||||
use embassy_executor::Spawner;
|
||||
use embassy_rp::{
|
||||
gpio::{AnyPin, Input, Pin, Pull},
|
||||
pio::PioInstanceBase,
|
||||
peripherals::PIO1,
|
||||
};
|
||||
use embassy_sync::pubsub::{ImmediatePublisher, PubSubChannel, Subscriber};
|
||||
use embassy_time::{Duration, Timer};
|
||||
@ -28,7 +28,7 @@ pub struct KeyboardConfig {
|
||||
pub pins: [AnyPin; SWITCH_COUNT],
|
||||
/// Array of LED indices of each switch
|
||||
pub led_map: [usize; SWITCH_COUNT],
|
||||
pub led_driver: Ws2812<PioInstanceBase<1>>,
|
||||
pub led_driver: Ws2812<PIO1>,
|
||||
pub layers: Vec<Layer>,
|
||||
}
|
||||
|
||||
@ -39,7 +39,7 @@ struct State {
|
||||
layers: &'static [Layer],
|
||||
/// Array of LED indices of each switch
|
||||
led_map: [usize; SWITCH_COUNT],
|
||||
lights: Lights<PioInstanceBase<1>, SWITCH_COUNT>,
|
||||
lights: Lights<PIO1, SWITCH_COUNT>,
|
||||
}
|
||||
|
||||
/// A keyboard half.
|
||||
|
||||
12
src/lib.rs
12
src/lib.rs
@ -1,6 +1,11 @@
|
||||
#![no_std]
|
||||
#![feature(type_alias_impl_trait)]
|
||||
|
||||
use embassy_rp::{
|
||||
bind_interrupts,
|
||||
peripherals::{UART0, USB},
|
||||
};
|
||||
|
||||
extern crate alloc;
|
||||
|
||||
pub mod allocator;
|
||||
@ -13,3 +18,10 @@ pub mod uart;
|
||||
pub mod usb;
|
||||
pub mod util;
|
||||
pub mod ws2812;
|
||||
|
||||
bind_interrupts! {
|
||||
pub struct Irqs {
|
||||
UART0_IRQ => embassy_rp::uart::BufferedInterruptHandler<UART0>;
|
||||
USBCTRL_IRQ => embassy_rp::usb::InterruptHandler<USB>;
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,19 +1,19 @@
|
||||
use crate::ws2812::Ws2812;
|
||||
use embassy_rp::pio::PioInstance;
|
||||
use embassy_rp::pio;
|
||||
use embassy_sync::mutex::Mutex;
|
||||
|
||||
use crate::{util::CS, ws2812::Rgb};
|
||||
|
||||
pub struct Lights<P: PioInstance, const N: usize> {
|
||||
pub struct Lights<P: pio::Instance + 'static, const N: usize> {
|
||||
state: Mutex<CS, State<P, N>>,
|
||||
}
|
||||
|
||||
struct State<P: PioInstance, const N: usize> {
|
||||
struct State<P: pio::Instance + 'static, const N: usize> {
|
||||
colors: [Rgb; N],
|
||||
driver: Ws2812<P>,
|
||||
}
|
||||
|
||||
impl<P: PioInstance, const N: usize> Lights<P, N> {
|
||||
impl<P: pio::Instance, const N: usize> Lights<P, N> {
|
||||
pub const fn new(driver: Ws2812<P>) -> Self {
|
||||
Lights {
|
||||
state: Mutex::new(State {
|
||||
|
||||
@ -1,7 +1,6 @@
|
||||
use core::mem::{size_of, transmute};
|
||||
|
||||
use embassy_executor::Spawner;
|
||||
use embassy_rp::interrupt;
|
||||
use embassy_rp::peripherals::{PIN_0, PIN_1, UART0};
|
||||
use embassy_rp::uart::{self, BufferedUart, DataBits, Parity, StopBits};
|
||||
use embassy_time::{with_timeout, Duration, TimeoutError};
|
||||
@ -11,6 +10,7 @@ use log::{error, info};
|
||||
use static_cell::StaticCell;
|
||||
|
||||
use crate::keyboard::{self, Half, KbEvents};
|
||||
use crate::Irqs;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
enum Message {
|
||||
@ -29,7 +29,7 @@ pub async fn start(tx: PIN_0, rx: PIN_1, uart: UART0, board: Half, events: KbEve
|
||||
|
||||
let uart = embassy_rp::uart::BufferedUart::new(
|
||||
uart,
|
||||
interrupt::take!(UART0_IRQ),
|
||||
Irqs,
|
||||
tx,
|
||||
rx,
|
||||
TX_BUF.init_with(|| [0u8; 1024]),
|
||||
|
||||
@ -1,9 +1,9 @@
|
||||
use embassy_executor::Spawner;
|
||||
use embassy_rp::{interrupt, peripherals::USB, usb::Driver};
|
||||
use embassy_rp::{peripherals::USB, usb::Driver};
|
||||
use embassy_usb::{Builder, Config, UsbDevice};
|
||||
use static_cell::StaticCell;
|
||||
|
||||
use crate::keyboard::KbEvents;
|
||||
use crate::{keyboard::KbEvents, Irqs};
|
||||
|
||||
pub mod keyboard;
|
||||
pub mod logger;
|
||||
@ -59,7 +59,7 @@ pub fn builder(usb: USB) -> Builder<'static, Driver<'static, USB>> {
|
||||
config.device_protocol = 0x01;
|
||||
config.composite_with_iads = true;
|
||||
|
||||
let driver = Driver::new(usb, interrupt::take!(USBCTRL_IRQ));
|
||||
let driver = Driver::new(usb, Irqs);
|
||||
|
||||
Builder::new(
|
||||
driver,
|
||||
|
||||
@ -3,16 +3,13 @@ use core::mem::transmute;
|
||||
use core::ops::Div;
|
||||
|
||||
use embassy_rp::dma::{self, AnyChannel};
|
||||
use embassy_rp::pio::{
|
||||
FifoJoin, PioInstance, PioPeripheral, PioStateMachine, PioStateMachineInstance, ShiftDirection,
|
||||
SmInstanceBase,
|
||||
};
|
||||
use embassy_rp::pio_instr_util;
|
||||
use embassy_rp::pio::{self, FifoJoin, Instance, Pio, PioPin, ShiftConfig, ShiftDirection};
|
||||
use embassy_rp::relocate::RelocatedProgram;
|
||||
use embassy_rp::{gpio, PeripheralRef};
|
||||
use embassy_rp::{Peripheral, PeripheralRef};
|
||||
use fixed::FixedU32;
|
||||
|
||||
pub struct Ws2812<P: PioInstance> {
|
||||
sm: PioStateMachineInstance<P, SmInstanceBase<0>>,
|
||||
pub struct Ws2812<P: pio::Instance + 'static> {
|
||||
sm: pio::StateMachine<'static, P, 0>,
|
||||
dma: PeripheralRef<'static, AnyChannel>,
|
||||
}
|
||||
|
||||
@ -21,16 +18,17 @@ pub struct Ws2812<P: PioInstance> {
|
||||
#[derive(Clone, Copy, PartialEq, Eq)]
|
||||
pub struct Rgb(u32);
|
||||
|
||||
impl<P: PioInstance> Ws2812<P> {
|
||||
pub fn new<PP: PioPeripheral<Pio = P>>(
|
||||
pio: PP,
|
||||
impl<P: Instance> Ws2812<P> {
|
||||
pub fn new(
|
||||
pio: impl Peripheral<P = P> + 'static,
|
||||
dma: impl dma::Channel,
|
||||
pin: gpio::AnyPin,
|
||||
pin: impl PioPin,
|
||||
) -> Self {
|
||||
let (_, mut sm, ..) = pio.split();
|
||||
let mut pio = Pio::new(pio);
|
||||
let mut sm = pio.sm0;
|
||||
// prepare the PIO program
|
||||
let side_set = pio::SideSet::new(false, 1, false);
|
||||
let mut a: pio::Assembler<32> = pio::Assembler::new_with_side_set(side_set);
|
||||
let side_set = ::pio::SideSet::new(false, 1, false);
|
||||
let mut a: ::pio::Assembler<32> = ::pio::Assembler::new_with_side_set(side_set);
|
||||
|
||||
const T1: u8 = 2; // start bit
|
||||
const T2: u8 = 5; // data bit
|
||||
@ -40,14 +38,14 @@ impl<P: PioInstance> Ws2812<P> {
|
||||
let mut wrap_target = a.label();
|
||||
let mut wrap_source = a.label();
|
||||
let mut do_zero = a.label();
|
||||
a.set_with_side_set(pio::SetDestination::PINDIRS, 1, 0);
|
||||
a.set_with_side_set(::pio::SetDestination::PINDIRS, 1, 0);
|
||||
a.bind(&mut wrap_target);
|
||||
// Do stop bit
|
||||
a.out_with_delay_and_side_set(pio::OutDestination::X, 1, T3 - 1, 0);
|
||||
a.out_with_delay_and_side_set(::pio::OutDestination::X, 1, T3 - 1, 0);
|
||||
// Do start bit
|
||||
a.jmp_with_delay_and_side_set(pio::JmpCondition::XIsZero, &mut do_zero, T1 - 1, 1);
|
||||
a.jmp_with_delay_and_side_set(::pio::JmpCondition::XIsZero, &mut do_zero, T1 - 1, 1);
|
||||
// Do data bit = 1
|
||||
a.jmp_with_delay_and_side_set(pio::JmpCondition::Always, &mut wrap_target, T2 - 1, 1);
|
||||
a.jmp_with_delay_and_side_set(::pio::JmpCondition::Always, &mut wrap_target, T2 - 1, 1);
|
||||
a.bind(&mut do_zero);
|
||||
// Do data bit = 0
|
||||
a.nop_with_delay_and_side_set(T2 - 1, 0);
|
||||
@ -55,15 +53,8 @@ impl<P: PioInstance> Ws2812<P> {
|
||||
|
||||
let prg = a.assemble_with_wrap(wrap_source, wrap_target);
|
||||
|
||||
let relocated = RelocatedProgram::new(&prg);
|
||||
sm.write_instr(relocated.origin() as usize, relocated.code());
|
||||
pio_instr_util::exec_jmp(&mut sm, relocated.origin());
|
||||
|
||||
// Pin config
|
||||
let out_pin = sm.make_pio_pin(pin);
|
||||
sm.set_set_pins(&[&out_pin]);
|
||||
sm.set_sideset_base_pin(&out_pin);
|
||||
sm.set_sideset_count(1);
|
||||
let relocated_prg = RelocatedProgram::new(&prg);
|
||||
let loaded_prg = pio.common.load_program(&relocated_prg);
|
||||
|
||||
// Clock config
|
||||
// TODO CLOCK_FREQ should come from embassy_rp
|
||||
@ -79,16 +70,19 @@ impl<P: PioInstance> Ws2812<P> {
|
||||
int = 0;
|
||||
}
|
||||
|
||||
sm.set_clkdiv((int << 8) | frac);
|
||||
let pio::Wrap { source, target } = relocated.wrap();
|
||||
sm.set_wrap(source, target);
|
||||
|
||||
// FIFO config
|
||||
sm.set_autopull(true);
|
||||
sm.set_fifo_join(FifoJoin::TxOnly);
|
||||
sm.set_pull_threshold(24);
|
||||
sm.set_out_shift_dir(ShiftDirection::Left);
|
||||
let mut config = pio::Config::default();
|
||||
config.clock_divider = FixedU32::from_bits((int << 8) | frac);
|
||||
config.fifo_join = FifoJoin::TxOnly;
|
||||
config.shift_out = ShiftConfig {
|
||||
threshold: 24,
|
||||
direction: ShiftDirection::Left,
|
||||
auto_fill: true,
|
||||
};
|
||||
let out_pin = pio.common.make_pio_pin(pin);
|
||||
config.set_set_pins(&[&out_pin]);
|
||||
config.use_program(&loaded_prg, &[&out_pin]);
|
||||
|
||||
sm.set_config(&config);
|
||||
sm.set_enable(true);
|
||||
|
||||
Self {
|
||||
@ -99,7 +93,7 @@ impl<P: PioInstance> Ws2812<P> {
|
||||
|
||||
pub async fn write(&mut self, colors: &[Rgb]) {
|
||||
let colors = Rgb::slice_as_u32s(colors);
|
||||
self.sm.dma_push(self.dma.reborrow(), colors).await;
|
||||
self.sm.tx().dma_push(self.dma.reborrow(), colors).await;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user