Initial commit

This commit is contained in:
JP Stringham
2026-02-21 22:39:12 -05:00
commit 6751fb88ef
7 changed files with 1408 additions and 0 deletions

147
src/main.rs Normal file
View File

@@ -0,0 +1,147 @@
//! This example toggles the GPIO25 pin, using a PIO program compiled via pio::pio_asm!().
//!
//! If a LED is connected to that pin, like on a Pico board, the LED should blink.
#![no_std]
#![no_main]
use defmt::println;
use defmt_rtt as _;
use hal::Sio;
use hal::gpio::{FunctionPio0, Pin};
use hal::pac;
use hal::pio::PIOExt;
use panic_probe as _;
use rp2040_hal as hal;
#[unsafe(link_section = ".boot_loader")]
#[used]
pub static BOOT2: [u8; 256] = rp2040_boot2::BOOT_LOADER_W25Q080;
const WAVE_TABLE: [i8; 256] = [
0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54, 57, 60, 62, 65, 68, 70,
73, 75, 78, 80, 83, 85, 87, 90, 92, 94, 96, 98, 100, 102, 104, 105, 107, 109, 110, 112, 113,
115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 124, 125, 125, 126, 126, 126, 126, 126, 126,
126, 126, 126, 126, 125, 125, 124, 124, 123, 123, 122, 121, 120, 119, 118, 117, 115, 114, 113,
111, 110, 108, 106, 105, 103, 101, 99, 97, 95, 93, 91, 89, 86, 84, 82, 79, 77, 74, 72, 69, 66,
64, 61, 58, 56, 53, 50, 47, 44, 41, 38, 35, 32, 29, 26, 23, 20, 17, 14, 11, 8, 4, 1, -1, -4,
-7, -10, -13, -16, -20, -23, -26, -29, -32, -35, -38, -41, -44, -47, -50, -52, -55, -58, -61,
-63, -66, -69, -71, -74, -76, -79, -81, -84, -86, -88, -90, -93, -95, -97, -99, -101, -103,
-104, -106, -108, -109, -111, -112, -114, -115, -116, -118, -119, -120, -121, -122, -122, -123,
-124, -124, -125, -125, -126, -126, -126, -126, -126, -126, -126, -126, -126, -126, -125, -125,
-124, -124, -123, -122, -121, -120, -119, -118, -117, -116, -115, -113, -112, -110, -109, -107,
-106, -104, -102, -100, -98, -96, -94, -92, -90, -88, -85, -83, -81, -78, -76, -73, -71, -68,
-65, -63, -60, -57, -54, -52, -49, -46, -43, -40, -37, -34, -31, -28, -25, -22, -19, -16, -12,
-9, -6, -3, 0,
];
/// Entry point to our bare-metal application.
///
/// The `#[rp2040_hal::entry]` macro ensures the Cortex-M start-up code calls this function
/// as soon as all global variables and the spinlock are initialised.
#[rp2040_hal::entry]
fn main() -> ! {
let mut pac = pac::Peripherals::take().unwrap();
let sio = Sio::new(pac.SIO);
let pins = hal::gpio::Pins::new(
pac.IO_BANK0,
pac.PADS_BANK0,
sio.gpio_bank0,
&mut pac.RESETS,
);
// configure LED pin for Pio0.
let led: Pin<_, FunctionPio0, _> = pins.gpio25.into_function();
// PIN id for use inside of PIO
let led_pin_id = led.id().num;
// Define a PIO program which reads data from the TX FIFO bit by bit
let program = pio::pio_asm!(
".wrap_target",
" out pins, 8",
" out pins, 8",
" out pins, 8",
" out pins, 8",
".wrap"
);
let dyn_pin_array = [
pins.gpio3.into_function::<FunctionPio0>().into_dyn_pin(),
pins.gpio4.into_function::<FunctionPio0>().into_dyn_pin(),
pins.gpio5.into_function::<FunctionPio0>().into_dyn_pin(),
pins.gpio6.into_function::<FunctionPio0>().into_dyn_pin(),
pins.gpio7.into_function::<FunctionPio0>().into_dyn_pin(),
pins.gpio8.into_function::<FunctionPio0>().into_dyn_pin(),
pins.gpio9.into_function::<FunctionPio0>().into_dyn_pin(),
pins.gpio10.into_function::<FunctionPio0>().into_dyn_pin(),
pins.gpio11.into_function::<FunctionPio0>().into_dyn_pin(),
pins.gpio12.into_function::<FunctionPio0>().into_dyn_pin(),
pins.gpio13.into_function::<FunctionPio0>().into_dyn_pin(),
];
// Initialize and start PIO
let (mut pio, sm0, _, _, _) = pac.PIO0.split(&mut pac.RESETS);
let installed = pio.install(&program.program).unwrap();
let (int, frac) = (1, 0);
let (mut sm, _, mut tx) = rp2040_hal::pio::PIOBuilder::from_installed_program(installed)
.set_pins(led_pin_id, 1)
.out_pins(dyn_pin_array[3].id().num, 8)
.out_shift_direction(rp2040_hal::pio::ShiftDirection::Left)
.out_sticky(true)
.autopull(true)
.buffers(rp2040_hal::pio::Buffers::OnlyTx)
.clock_divisor_fixed_point(int, frac)
.build(sm0);
// The GPIO pin needs to be configured as an output.
sm.set_pindirs([(led_pin_id, hal::pio::PinDir::Output)]);
let mut pindirs: [(u8, hal::pio::PinDir); 12] = [(0, hal::pio::PinDir::Output); 12];
dyn_pin_array.iter().enumerate().for_each(|(i, p)| {
pindirs[i] = (p.id().num, hal::pio::PinDir::Output);
});
sm.set_pindirs(pindirs);
tx.write(0b1111_1111_1111_1111_1111_1111_1111_1111);
sm.start();
// let pgroup: PinGroup = PinGroup::new();
// let pgroup = pgroup.add_pin(pins.gpio3.into_push_pull_output());
// let pgroup = pgroup.add_pin(pins.gpio4.into_push_pull_output());
// let pgroup = pgroup.add_pin(pins.gpio5.into_push_pull_output());
// let pgroup = pgroup.add_pin(pins.gpio6.into_push_pull_output());
// let pgroup = pgroup.add_pin(pins.gpio7.into_push_pull_output());
// let pgroup = pgroup.add_pin(pins.gpio8.into_push_pull_output());
// let pgroup = pgroup.add_pin(pins.gpio9.into_push_pull_output());
// let pgroup = pgroup.add_pin(pins.gpio10.into_push_pull_output());
// let pgroup = pgroup.add_pin(pins.gpio11.into_push_pull_output());
// let pgroup = pgroup.add_pin(pins.gpio12.into_push_pull_output());
// let mut pgroup = pgroup.add_pin(pins.gpio13.into_push_pull_output());
let mut processed_wave_table = [0u32; 256];
for (i, w) in WAVE_TABLE.iter().enumerate() {
processed_wave_table[i] = ((*w as i32) + 127) as u32;
}
println!("alive");
println!("{:?}", processed_wave_table);
let mut tick = 0usize;
// PIO runs in background, independently from CPU
loop {
while !tx.is_full() {
let mut out = 0;
for i in 0..4 {
let val = processed_wave_table[tick % 256];
out |= val.rotate_right(8 * i);
// out |= ((tick % 256) as u32).rotate_right(8 * i);
tick = tick.wrapping_add(1);
// println!("{}", val);
}
tx.write(out);
}
// pgroup.set_u32(out.rotate_left(6));
}
}