Skip to content

Commit

Permalink
dma support (#66)
Browse files Browse the repository at this point in the history
* add dma support for all available targets
  • Loading branch information
georgik authored Nov 10, 2023
1 parent fd39672 commit 3dd5651
Show file tree
Hide file tree
Showing 61 changed files with 1,465 additions and 1,149 deletions.
4 changes: 2 additions & 2 deletions esp-wrover-kit/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "spooky-wrover-kit"
version = "0.7.0"
version = "0.8.0"
authors = ["Juraj Michálek <[email protected]>"]
edition = "2021"
license = "MIT"
Expand All @@ -27,7 +27,7 @@ panic-halt = "0.2"
shared-bus = { version = "0.3.0" }
spooky-core = { path = "../spooky-core", default-features = false, features = [ "static_maze" ] }
spooky-embedded = { path = "../spooky-embedded", default-features = false, features = [ "static_maze" ] }
heapless = { version = "0.7.14", default-features = false }
spi-dma-displayinterface = { path = "../spi-dma-displayinterface", features = [ "esp32" ] }

[features]
default = [ "esp_wrover_kit" ]
Expand Down
19 changes: 13 additions & 6 deletions esp-wrover-kit/src/app.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
use crate::types::ConfiguredPins;
use embedded_graphics::{pixelcolor::Rgb565, prelude::DrawTarget};
use embedded_graphics::pixelcolor::Rgb565;
use spooky_core::{engine::Engine, spritebuf::SpriteBuf, universe::Universe};
use embedded_graphics_framebuf::FrameBuf;
use embedded_hal::digital::v2::InputPin;
use display_interface::WriteOnlyDataCommand;
use mipidsi::models::Model;
use embedded_hal::digital::v2::OutputPin;
use crate::setup::{setup_movement_controller, setup_button_keyboard};
use embedded_graphics::prelude::RgbColor;

pub fn app_loop<UP, DP, LP, RP, DB, TP, DISP>(
pub fn app_loop<UP, DP, LP, RP, DB, TP, DI, M, RST>(
display: &mut mipidsi::Display<DI, M, RST>,
lcd_h_res:u16,
lcd_v_res:u16,
configured_pins: ConfiguredPins<UP, DP, LP, RP, DB, TP>,
display: &mut DISP,
seed_buffer: [u8; 32])
where
UP: InputPin,
Expand All @@ -17,7 +22,9 @@ where
RP: InputPin,
DB: InputPin,
TP: InputPin,
DISP: DrawTarget<Color = Rgb565>,
DI: WriteOnlyDataCommand,
M: Model<ColorFormat = Rgb565>,
RST: OutputPin,
{
let button_keyboard = setup_button_keyboard(configured_pins);

Expand All @@ -34,7 +41,7 @@ where
universe.initialize();

loop {
let _ = display
.draw_iter(universe.render_frame().into_iter());
let pixel_iterator = universe.render_frame().get_pixel_iter();
let _ = display.set_pixels(0, 0, lcd_v_res-1, lcd_h_res, pixel_iterator);
}
}
70 changes: 56 additions & 14 deletions esp-wrover-kit/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,20 @@
#[global_allocator]
static ALLOCATOR: esp_alloc::EspHeap = esp_alloc::EspHeap::empty();

// use display_interface_spi::SPIInterfaceNoCS;
use spi_dma_displayinterface::spi_dma_displayinterface::SPIInterfaceNoCS;

use esp_backtrace as _;
use hal::{psram, prelude::*, peripherals::Peripherals,
use hal::{psram, prelude::*,
peripherals::Peripherals,
dma::DmaPriority,
pdma::Dma,
spi::{
master::Spi,
master::{prelude::*, Spi},
SpiMode,
},
clock::{ClockControl, CpuClock}, Delay, Rng, IO};
use display_interface_spi::SPIInterfaceNoCS;

use embedded_graphics::{
mono_font::{ascii::FONT_8X13, MonoTextStyle},
prelude::{Point, RgbColor},
Expand All @@ -25,7 +31,8 @@ mod setup;
mod types;
mod app;
use app::app_loop;
use setup::*;

use crate::types::ConfiguredPins;

pub fn init_psram_heap() {
unsafe {
Expand All @@ -41,29 +48,64 @@ fn main() -> ! {
init_psram_heap();

let system = peripherals.SYSTEM.split();
let clocks = ClockControl::configure(system.clock_control, CpuClock::Clock240MHz).freeze();

// With DMA we have sufficient throughput, so we can clock down the CPU to 160MHz
let clocks = ClockControl::configure(system.clock_control, CpuClock::Clock160MHz).freeze();

let mut delay = Delay::new(&clocks);

let io = IO::new(peripherals.GPIO, peripherals.IO_MUX);
let (unconfigured_pins, configured_pins, configured_system_pins) = setup_pins(io.pins);

let spi = Spi::new_no_cs_no_miso(
let lcd_h_res = 240;
let lcd_v_res = 320;

let lcd_sclk = io.pins.gpio19;
let lcd_mosi = io.pins.gpio23;
let lcd_miso = io.pins.gpio25;
let lcd_cs = io.pins.gpio22;
let lcd_dc = io.pins.gpio21.into_push_pull_output();
let _lcd_backlight = io.pins.gpio5.into_push_pull_output();
let lcd_reset = io.pins.gpio18.into_push_pull_output();

let dma = Dma::new(system.dma);
let dma_channel = dma.spi2channel;

let mut descriptors = [0u32; 8 * 3];
let mut rx_descriptors = [0u32; 8 * 3];

let configured_pins = ConfiguredPins {
up_button: io.pins.gpio14.into_pull_up_input(),
down_button: io.pins.gpio12.into_pull_up_input(),
left_button: io.pins.gpio13.into_pull_up_input(),
right_button: io.pins.gpio15.into_pull_up_input(),
dynamite_button: io.pins.gpio26.into_pull_up_input(),
teleport_button: io.pins.gpio27.into_pull_up_input(),
};

let spi = Spi::new(
peripherals.SPI2,
unconfigured_pins.sclk,
unconfigured_pins.mosi,
lcd_sclk,
lcd_mosi,
lcd_miso,
lcd_cs,
60u32.MHz(),
SpiMode::Mode0,
&clocks,
);
// );
).with_dma(dma_channel.configure(
false,
&mut descriptors,
&mut rx_descriptors,
DmaPriority::Priority0,
));

let di = SPIInterfaceNoCS::new(spi, configured_system_pins.dc);
let di = SPIInterfaceNoCS::new(spi, lcd_dc);

let mut display = match mipidsi::Builder::ili9341_rgb565(di)
.with_display_size(240 as u16, 320 as u16)
.with_display_size(lcd_h_res as u16, lcd_v_res as u16)
.with_orientation(mipidsi::Orientation::Landscape(false))
.with_color_order(mipidsi::ColorOrder::Bgr)
.init(&mut delay, Some(configured_system_pins.reset)) {
.init(&mut delay, Some(lcd_reset)) {
Ok(disp) => { disp },
Err(_) => { panic!() },
};
Expand All @@ -80,6 +122,6 @@ fn main() -> ! {
let mut seed_buffer = [1u8; 32];
rng.read(&mut seed_buffer).unwrap();

app_loop(configured_pins, &mut display, seed_buffer);
app_loop(&mut display, lcd_h_res, lcd_v_res, configured_pins, seed_buffer);
loop {}
}
30 changes: 2 additions & 28 deletions esp-wrover-kit/src/setup.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,8 @@
use crate::types::{UnconfiguredPins, ConfiguredPins, ConfiguredSystemPins};
use embedded_hal::digital::v2::{OutputPin, InputPin};
use hal::gpio::{self, Pins};
use crate::types::ConfiguredPins;
use embedded_hal::digital::v2::InputPin;
use spooky_embedded::{ button_keyboard::ButtonKeyboard, embedded_movement_controller::EmbeddedMovementController };
use spooky_core;

pub fn setup_pins(pins: Pins) -> (UnconfiguredPins<gpio::Unknown>, ConfiguredPins<impl InputPin, impl InputPin, impl InputPin, impl InputPin, impl InputPin,
impl InputPin>, ConfiguredSystemPins<impl OutputPin, impl OutputPin, impl OutputPin>) {
let unconfigured_pins = UnconfiguredPins {
sclk: pins.gpio19,
mosi: pins.gpio23,
};

let configured_pins = ConfiguredPins {
up_button: pins.gpio14.into_pull_up_input(),
down_button: pins.gpio12.into_pull_up_input(),
left_button: pins.gpio13.into_pull_up_input(),
right_button: pins.gpio15.into_pull_up_input(),
dynamite_button: pins.gpio26.into_pull_up_input(),
teleport_button: pins.gpio27.into_pull_up_input(),
};

let configured_system_pins = ConfiguredSystemPins {
dc: pins.gpio21.into_push_pull_output(),
backlight: pins.gpio5.into_push_pull_output(),
reset: pins.gpio18.into_push_pull_output(),
};

(unconfigured_pins, configured_pins, configured_system_pins)
}

pub fn setup_button_keyboard<Up: InputPin, Down: InputPin, Left: InputPin, Right: InputPin, Dyn: InputPin, Tel: InputPin>(
configured_pins: ConfiguredPins<Up, Down, Left, Right, Dyn, Tel>
) -> ButtonKeyboard<Up, Down, Left, Right, Dyn, Tel> {
Expand Down
15 changes: 1 addition & 14 deletions esp-wrover-kit/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
use hal::gpio;
use embedded_hal::digital::v2::{ InputPin, OutputPin };

// Generic type for unconfigured pins
pub struct UnconfiguredPins<MODE> {
pub sclk: gpio::Gpio19<MODE>,
pub mosi: gpio::Gpio23<MODE>,
}
use embedded_hal::digital::v2::InputPin;

pub struct ConfiguredPins<Up: InputPin, Down: InputPin, Left: InputPin, Right: InputPin, Dyn: InputPin, Tel: InputPin> {
pub up_button: Up,
Expand All @@ -15,9 +8,3 @@ pub struct ConfiguredPins<Up: InputPin, Down: InputPin, Left: InputPin, Right: I
pub dynamite_button: Dyn,
pub teleport_button: Tel,
}

pub struct ConfiguredSystemPins<Dc: OutputPin, Bckl: OutputPin, Reset: OutputPin> {
pub dc: Dc,
pub backlight: Bckl,
pub reset: Reset,
}
4 changes: 2 additions & 2 deletions esp32-c3-devkit-rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "spooky-esp32-c3"
version = "0.7.0"
version = "0.8.0"
authors = ["Juraj Michálek <[email protected]>"]
edition = "2021"
license = "MIT"
Expand All @@ -27,7 +27,7 @@ panic-halt = "0.2"
shared-bus = { version = "0.3.0" }
spooky-core = { path = "../spooky-core", default-features = false, features = ["static_maze"]}
spooky-embedded = { path = "../spooky-embedded", default-features = false, features = [ "static_maze" ] }
heapless = { version = "0.7.14", default-features = false }
spi-dma-displayinterface = { path = "../spi-dma-displayinterface", features = ["esp32c3"] }

[features]
default = [ "esp32c3_ili9341" ]
Expand Down
22 changes: 14 additions & 8 deletions esp32-c3-devkit-rust/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,22 @@ use embedded_graphics::{pixelcolor::Rgb565, prelude::DrawTarget};
use spooky_core::{engine::Engine, spritebuf::SpriteBuf, universe::Universe};
use embedded_graphics_framebuf::FrameBuf;
use embedded_graphics::prelude::RgbColor;
use display_interface::WriteOnlyDataCommand;
use embedded_hal::digital::v2::OutputPin;
use mipidsi::models::Model;
use crate::accel_movement_controller::AccelMovementController;
use crate::Accelerometer;

pub fn app_loop<DISP>(
display: &mut DISP,
pub fn app_loop<DI, M, RST>(
display: &mut mipidsi::Display<DI, M, RST>,
lcd_h_res:u16,
lcd_v_res:u16,
seed_buffer: [u8; 32],
icm: impl Accelerometer // You'll need to pass your accelerometer device here
)
where
DISP: DrawTarget<Color = Rgb565>,
icm: impl Accelerometer
) where
DI: WriteOnlyDataCommand,
M: Model<ColorFormat = Rgb565>,
RST: OutputPin,
{
let accel_movement_controller = AccelMovementController::new(icm, 0.2);

Expand All @@ -30,7 +36,7 @@ where
universe.initialize();

loop {
let _ = display
.draw_iter(universe.render_frame().into_iter());
let pixel_iterator = universe.render_frame().get_pixel_iter();
let _ = display.set_pixels(0, 0, lcd_v_res, lcd_h_res, pixel_iterator);
}
}
Loading

0 comments on commit 3dd5651

Please sign in to comment.