diff --git a/.vscode/launch.json b/.vscode/launch.json index eee0bb2..265489d 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -24,7 +24,7 @@ "coreConfigs": [ { "coreIndex": 0, - "programBinary": "target/thumbv6m-none-eabi/debug/rp2040-project-template", + "programBinary": "target/thumbv6m-none-eabi/debug/usb-c-fusb302-pi", "chip": "RP2040", // Uncomment this if you've downloaded the SVD from // https://github.com/raspberrypi/pico-sdk/raw/1.3.1/src/rp2040/hardware_regs/rp2040.svd @@ -33,7 +33,8 @@ "rttEnabled": true, "options": { "env": { - "DEFMT_LOG": "debug" + "DEFMT_LOG": "debug", + "RUST_LOG": "debug" } }, } @@ -42,4 +43,30 @@ "wireProtocol": "Swd" } ] -} \ No newline at end of file +} +// { +// "version": "0.2.0", +// "configurations": [ +// { +// "type": "probe-rs-debug", +// "request": "attach", +// "name": "probe_rs Executable launch example", +// "cwd": "${workspaceFolder}", +// // "speed": 24000, //!MODIFY (or remove) +// "chip": "rp2040", //!MODIFY +// // "probe": "VID:PID:", //!MODIFY (or remove) +// "coreConfigs": [ +// { +// "coreIndex": 0, +// "programBinary": "Relative or fully qualified path to your programBinary", //!MODIFY +// "svdFile": "Relative or fully qualified path to your programBinary" //!MODIFY +// } +// ], +// "env": { +// //!MODIFY (or remove) +// "RUST_LOG": "info" // If you set this variable, check the VSCode console log window for the location of the log file. +// }, +// "consoleLogLevel": "Console" //Info, Debug +// } +// ] +// } \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml index 07f9556..2613bad 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,13 +11,22 @@ cortex-m-rtic = "1.1.4" embedded-hal = { version = "0.2.5", features = ["unproven"] } fugit = "0.3.7" #fusb302b = { version = "0.0.1" } -usb-pd = { git = "https://github.com/bentwire/usb-pd-rs.git" } -fusb302b = { git = "https://github.com/bentwire/usb-pd-rs.git" } +#ili9341 = "0.5.0" +#ssd1306 = "0.8.3" +#ssd1351 = { git = "https://github.com/ftmazzone/ssd1351-rust.git", feature = ["no-std"] } +ssd1351 = { version = "0.4.2", features = ["graphics"]} +embedded-graphics = "0.8.1" +#usb-pd = { git = "https://github.com/bentwire/usb-pd-rs.git" } +#fusb302b = { git = "https://github.com/bentwire/usb-pd-rs.git" } +usb-pd = { path = "../usb-pd-rs/usb-pd" } +fusb302b = { path = "../usb-pd-rs/fusb302b" } defmt = "0.3" defmt-rtt = "0.4" panic-probe = { version = "0.3", features = ["print-defmt"] } shared-bus = { version = "0.3.0", features = ["cortex-m"] } - +display-interface = "0.4.1" +display-interface-spi = "0.4.1" +embedded-graphics-core = "0.4.0" # We're using a Pico by default on this template rp-pico = { version = "0.8.0", features = ["rt"] } diff --git a/src/display.rs b/src/display.rs new file mode 100644 index 0000000..72900b9 --- /dev/null +++ b/src/display.rs @@ -0,0 +1,110 @@ +use embedded_graphics::pixelcolor::Rgb565; + +use core::fmt::Debug; +use embedded_graphics_core::draw_target::DrawTarget; +use embedded_graphics_core::geometry::Dimensions; + +use rp_pico::hal::{self, gpio, pac::SPI0, spi}; + +//use ssd1306::{mode::BufferedGraphicsMode, prelude::*, I2CDisplayInterface, Ssd1306}; + +use ssd1351::{ + builder::Builder, interface::SpiInterface, mode::GraphicsMode, properties::DisplayRotation, +}; + +pub type SSDSSPIInterface = SpiInterface< + hal::Spi< + spi::Enabled, + SPI0, + ( + gpio::Pin, + gpio::Pin, + gpio::Pin, + ), + 8, + >, + gpio::Pin, gpio::PullDown>, +>; + +//pub type Ssd1351 = ssd1351::mode::GraphicsMode; +pub type Ssd1351 = ssd1351::mode::GraphicsMode>; + +pub struct Display< + SPI: embedded_hal::blocking::spi::write::Default, + DC: embedded_hal::digital::v2::OutputPin, +>(pub Ssd1351); + +impl< + SPI: embedded_hal::blocking::spi::write::Default, + DC: embedded_hal::digital::v2::OutputPin, + > Display +{ + pub fn new(spi: SPI, dc: DC, rst: &mut RST, delay: &mut cortex_m::delay::Delay) -> Self + where + SPI: embedded_hal::blocking::spi::Transfer + embedded_hal::blocking::spi::Write, + DC: embedded_hal::digital::v2::OutputPin, + RST: embedded_hal::digital::v2::OutputPin, + ::Error: Debug, + { + let mut display: GraphicsMode<_> = Builder::new() + .with_rotation(DisplayRotation::Rotate0) + .with_size(ssd1351::properties::DisplaySize::Display128x96) + .connect_spi(spi, dc) + .into(); + + display.reset(rst, delay).expect("RESET FAIL"); + + display.init().unwrap(); + + Self(display) + } +} + +impl< + SPI: embedded_hal::blocking::spi::write::Default, + DC: embedded_hal::digital::v2::OutputPin, + > embedded_graphics::draw_target::DrawTarget for Display +{ + type Color = Rgb565; + + type Error = (); + + fn draw_iter(&mut self, pixels: I) -> Result<(), Self::Error> + where + I: IntoIterator>, + { + self.0.draw_iter(pixels) + } +} + +impl< + SPI: embedded_hal::blocking::spi::write::Default, + DC: embedded_hal::digital::v2::OutputPin, + > embedded_graphics::geometry::Dimensions for Display +{ + fn bounding_box(&self) -> embedded_graphics::primitives::Rectangle { + self.0.bounding_box() + } +} + +impl< + SPI: embedded_hal::blocking::spi::write::Default, + DC: embedded_hal::digital::v2::OutputPin, + > core::ops::Deref for Display +{ + type Target = Ssd1351; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +impl< + SPI: embedded_hal::blocking::spi::write::Default, + DC: embedded_hal::digital::v2::OutputPin, + > core::ops::DerefMut for Display +{ + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.0 + } +} diff --git a/src/main.rs b/src/main.rs index e5625f6..52e4241 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,8 @@ #![no_std] #![no_main] +//mod display; + use defmt::*; use defmt_rtt as _; @@ -93,24 +95,28 @@ mod app { >; use defmt::info; + use embedded_hal::digital::v2::OutputPin; // use embedded_hal::prelude::{ // _embedded_hal_blocking_delay_DelayMs, _embedded_hal_blocking_i2c_Read, // _embedded_hal_blocking_i2c_Write, _embedded_hal_blocking_i2c_WriteRead, // }; - use fugit::{ExtU64, Instant}; + use fugit::{ExtU64, Instant, RateExtU32}; use fusb302b::Fusb302b; - use rp2040_hal::Timer; + use rp_pico::hal::{ self, - clocks::init_clocks_and_plls, + clocks::{init_clocks_and_plls, Clock}, gpio, i2c, pac::I2C0, - sio, + sio, spi, timer::{monotonic::Monotonic, Alarm0}, watchdog::Watchdog, Sio, }; + + //use crate::display; + // use usb_pd::sink::{Driver, DriverState}; #[shared] @@ -169,6 +175,19 @@ mod app { pins.gpio1.into_function::(), ); + let spi0_pins = ( + // SPI0 TX + pins.gpio7.into_function::(), + // SPI0 RX + pins.gpio4.into_function::(), + // SPI0 CLK + pins.gpio6.into_function::(), + ); + + let _dis_cs = pins.gpio5.into_push_pull_output(); + let _dis_dc = pins.gpio9.into_push_pull_output(); + let _dis_rst = pins.gpio10.into_push_pull_output(); + let i2c0 = i2c::I2C::i2c0( c.device.I2C0, i2c0_pins.0, @@ -178,6 +197,15 @@ mod app { rp2040_hal::Clock::freq(&clocks.system_clock), ); + // let i2c1 = i2c::I2C::i2c1( + // c.device.I2C1, + // i2c1_pins.0, + // i2c1_pins.1, + // fugit::RateExtU32::Hz(100000_u32), + // &mut resets, + // rp2040_hal::Clock::freq(&clocks.system_clock), + // ); + let i2c_bus0: &'static _ = shared_bus::new_cortexm!(I2C0Dev = i2c0).unwrap(); //let mut fusb302 = fusb302b::Fusb302b::new(i2c_bus0.acquire_i2c()); @@ -206,11 +234,21 @@ mod app { let mut timer = rp2040_hal::Timer::new(c.device.TIMER, &mut resets, &clocks); let alarm = timer.alarm_0().unwrap(); - pd_task::spawn_after(1.millis()).unwrap(); - blink_led::spawn_after(500.millis()).unwrap(); + pd_task::spawn_after(ExtU64::millis(500)).unwrap(); + blink_led::spawn_after(ExtU64::millis(1000)).unwrap(); let i2c0 = i2c_bus0.acquire_i2c(); + let spi0 = spi::Spi::<_, _, _, 8>::new(c.device.SPI0, spi0_pins); + let _spi0 = spi0.init( + &mut resets, + clocks.system_clock.freq(), + 10_u32.MHz(), + embedded_hal::spi::MODE_0, + ); + + let _delay = cortex_m::delay::Delay::new(c.core.SYST, clocks.system_clock.freq().to_Hz()); + info!("INIT COMPLETE!"); ( @@ -225,43 +263,20 @@ mod app { //info!("IDLE!"); //loop { let now = monotonics::MyMono::now(); - let now: Instant = Instant::::from_ticks(now.ticks() / 10000); + let now: Instant = Instant::::from_ticks(now.ticks() / 1000); c.local.pdev.poll(now); - pd_task::spawn_after(1.micros()).unwrap(); - //} - //info!("i2c0"); - - // c.shared.i2c0.lock(|i2c0| { - // for i in 0..=127 { - // let mut readbuf: [u8; 1] = [0; 1]; - // let result = i2c0.read(i, &mut readbuf); - // if let Ok(_d) = result { - // let mut readbuf: [u8; 0x43] = [0; 0x43]; - // // Do whatever work you want to do with found devices - // info!("Device found at address {:?}", i); - // i2c0.write(i, &[0x0c, 0x01]).unwrap(); - // i2c0.write(i, &[0x0b, 0x0f]).unwrap(); - // i2c0.write(i, &[0x06, 0x00]).unwrap(); - // i2c0.write(i, &[0x09, 0x07]).unwrap(); - - // i2c0.write_read(i, &[0x01], &mut readbuf).unwrap(); - // info!("VER? {:x}", readbuf[0]); - // info!("SW0: {:x}", readbuf[1]); - // info!("SW1: {:x}", readbuf[2]); - // info!("MEA: {:x}", readbuf[3]) - // } - // } - // info!("Scan Done") - // }); + + //pd_task::spawn().unwrap(); + pd_task::spawn_after(50_u64.micros()).unwrap(); } #[task( - shared = [leds], + shared = [leds, i2c0], local = [tog: bool = false, which: u8 = 0], )] fn blink_led(mut c: blink_led::Context) { - info!("BLINK {:?}", *c.local.which); + //info!("BLINK {:?}", *c.local.which); if *c.local.tog { c.shared.leds.lock(|l| match *c.local.which { 0 => l.0.set_high().unwrap(), @@ -288,7 +303,7 @@ mod app { //let now = monotonics::MyMono::now(); //let foo: Instant = Instant::::from_ticks(now.ticks()); //c.shared.pd.lock(|pd| pd.poll(foo)); - blink_led::spawn_after(10000.millis()).unwrap(); + blink_led::spawn_after(1000_u64.millis()).unwrap(); } } diff --git a/usb-c-fusb302-pi.code-workspace b/usb-c-fusb302-pi.code-workspace new file mode 100644 index 0000000..b4a0f82 --- /dev/null +++ b/usb-c-fusb302-pi.code-workspace @@ -0,0 +1,13 @@ +{ + "folders": [ + { + "path": "." + }, + { + "path": "../usb-pd-rs" + } + ], + "settings": { + "rust-analyzer.cargo.target": "thumbv6m-none-eabi" + } +} \ No newline at end of file