Skip to content

Commit

Permalink
PD Stuff works, still having issues with display driver...
Browse files Browse the repository at this point in the history
  • Loading branch information
bentwire committed Oct 24, 2023
1 parent 0f49e09 commit dd171bf
Show file tree
Hide file tree
Showing 5 changed files with 216 additions and 42 deletions.
33 changes: 30 additions & 3 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -33,7 +33,8 @@
"rttEnabled": true,
"options": {
"env": {
"DEFMT_LOG": "debug"
"DEFMT_LOG": "debug",
"RUST_LOG": "debug"
}
},
}
Expand All @@ -42,4 +43,30 @@
"wireProtocol": "Swd"
}
]
}
}
// {
// "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:<Serial>", //!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
// }
// ]
// }
15 changes: 12 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }

Expand Down
110 changes: 110 additions & 0 deletions src/display.rs
Original file line number Diff line number Diff line change
@@ -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::bank0::Gpio7, gpio::FunctionSpi, gpio::PullDown>,
gpio::Pin<gpio::bank0::Gpio4, gpio::FunctionSpi, gpio::PullDown>,
gpio::Pin<gpio::bank0::Gpio6, gpio::FunctionSpi, gpio::PullDown>,
),
8,
>,
gpio::Pin<gpio::bank0::Gpio9, gpio::FunctionSio<gpio::SioOutput>, gpio::PullDown>,
>;

//pub type Ssd1351 = ssd1351::mode::GraphicsMode<SSDSSPIInterface>;
pub type Ssd1351<SPI, DC> = ssd1351::mode::GraphicsMode<SpiInterface<SPI, DC>>;

pub struct Display<
SPI: embedded_hal::blocking::spi::write::Default<u8>,
DC: embedded_hal::digital::v2::OutputPin,
>(pub Ssd1351<SPI, DC>);

impl<
SPI: embedded_hal::blocking::spi::write::Default<u8>,
DC: embedded_hal::digital::v2::OutputPin,
> Display<SPI, DC>
{
pub fn new<RST>(spi: SPI, dc: DC, rst: &mut RST, delay: &mut cortex_m::delay::Delay) -> Self
where
SPI: embedded_hal::blocking::spi::Transfer<u8> + embedded_hal::blocking::spi::Write<u8>,
DC: embedded_hal::digital::v2::OutputPin,
RST: embedded_hal::digital::v2::OutputPin,
<RST as 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<u8>,
DC: embedded_hal::digital::v2::OutputPin,
> embedded_graphics::draw_target::DrawTarget for Display<SPI, DC>
{
type Color = Rgb565;

type Error = ();

fn draw_iter<I>(&mut self, pixels: I) -> Result<(), Self::Error>
where
I: IntoIterator<Item = embedded_graphics::Pixel<Self::Color>>,
{
self.0.draw_iter(pixels)
}
}

impl<
SPI: embedded_hal::blocking::spi::write::Default<u8>,
DC: embedded_hal::digital::v2::OutputPin,
> embedded_graphics::geometry::Dimensions for Display<SPI, DC>
{
fn bounding_box(&self) -> embedded_graphics::primitives::Rectangle {
self.0.bounding_box()
}
}

impl<
SPI: embedded_hal::blocking::spi::write::Default<u8>,
DC: embedded_hal::digital::v2::OutputPin,
> core::ops::Deref for Display<SPI, DC>
{
type Target = Ssd1351<SPI, DC>;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl<
SPI: embedded_hal::blocking::spi::write::Default<u8>,
DC: embedded_hal::digital::v2::OutputPin,
> core::ops::DerefMut for Display<SPI, DC>
{
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
87 changes: 51 additions & 36 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#![no_std]
#![no_main]

//mod display;

use defmt::*;
use defmt_rtt as _;

Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -169,6 +175,19 @@ mod app {
pins.gpio1.into_function::<gpio::FunctionI2C>(),
);

let spi0_pins = (
// SPI0 TX
pins.gpio7.into_function::<gpio::FunctionSpi>(),
// SPI0 RX
pins.gpio4.into_function::<gpio::FunctionSpi>(),
// SPI0 CLK
pins.gpio6.into_function::<gpio::FunctionSpi>(),
);

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,
Expand All @@ -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());
Expand Down Expand Up @@ -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!");

(
Expand All @@ -225,43 +263,20 @@ mod app {
//info!("IDLE!");
//loop {
let now = monotonics::MyMono::now();
let now: Instant<u64, 1, 1000> = Instant::<u64, 1, 1000>::from_ticks(now.ticks() / 10000);
let now: Instant<u64, 1, 1000> = Instant::<u64, 1, 1000>::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(),
Expand All @@ -288,7 +303,7 @@ mod app {
//let now = monotonics::MyMono::now();
//let foo: Instant<u64, 1, 1000> = Instant::<u64, 1, 1000>::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();
}
}

Expand Down
13 changes: 13 additions & 0 deletions usb-c-fusb302-pi.code-workspace
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"folders": [
{
"path": "."
},
{
"path": "../usb-pd-rs"
}
],
"settings": {
"rust-analyzer.cargo.target": "thumbv6m-none-eabi"
}
}

0 comments on commit dd171bf

Please sign in to comment.