Skip to content

Commit 3dd5651

Browse files
authored
dma support (#66)
* add dma support for all available targets
1 parent fd39672 commit 3dd5651

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+1465
-1149
lines changed

esp-wrover-kit/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "spooky-wrover-kit"
3-
version = "0.7.0"
3+
version = "0.8.0"
44
authors = ["Juraj Michálek <[email protected]>"]
55
edition = "2021"
66
license = "MIT"
@@ -27,7 +27,7 @@ panic-halt = "0.2"
2727
shared-bus = { version = "0.3.0" }
2828
spooky-core = { path = "../spooky-core", default-features = false, features = [ "static_maze" ] }
2929
spooky-embedded = { path = "../spooky-embedded", default-features = false, features = [ "static_maze" ] }
30-
heapless = { version = "0.7.14", default-features = false }
30+
spi-dma-displayinterface = { path = "../spi-dma-displayinterface", features = [ "esp32" ] }
3131

3232
[features]
3333
default = [ "esp_wrover_kit" ]

esp-wrover-kit/src/app.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
use crate::types::ConfiguredPins;
2-
use embedded_graphics::{pixelcolor::Rgb565, prelude::DrawTarget};
2+
use embedded_graphics::pixelcolor::Rgb565;
33
use spooky_core::{engine::Engine, spritebuf::SpriteBuf, universe::Universe};
44
use embedded_graphics_framebuf::FrameBuf;
55
use embedded_hal::digital::v2::InputPin;
6+
use display_interface::WriteOnlyDataCommand;
7+
use mipidsi::models::Model;
8+
use embedded_hal::digital::v2::OutputPin;
69
use crate::setup::{setup_movement_controller, setup_button_keyboard};
710
use embedded_graphics::prelude::RgbColor;
811

9-
pub fn app_loop<UP, DP, LP, RP, DB, TP, DISP>(
12+
pub fn app_loop<UP, DP, LP, RP, DB, TP, DI, M, RST>(
13+
display: &mut mipidsi::Display<DI, M, RST>,
14+
lcd_h_res:u16,
15+
lcd_v_res:u16,
1016
configured_pins: ConfiguredPins<UP, DP, LP, RP, DB, TP>,
11-
display: &mut DISP,
1217
seed_buffer: [u8; 32])
1318
where
1419
UP: InputPin,
@@ -17,7 +22,9 @@ where
1722
RP: InputPin,
1823
DB: InputPin,
1924
TP: InputPin,
20-
DISP: DrawTarget<Color = Rgb565>,
25+
DI: WriteOnlyDataCommand,
26+
M: Model<ColorFormat = Rgb565>,
27+
RST: OutputPin,
2128
{
2229
let button_keyboard = setup_button_keyboard(configured_pins);
2330

@@ -34,7 +41,7 @@ where
3441
universe.initialize();
3542

3643
loop {
37-
let _ = display
38-
.draw_iter(universe.render_frame().into_iter());
44+
let pixel_iterator = universe.render_frame().get_pixel_iter();
45+
let _ = display.set_pixels(0, 0, lcd_v_res-1, lcd_h_res, pixel_iterator);
3946
}
4047
}

esp-wrover-kit/src/main.rs

Lines changed: 56 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,20 @@
66
#[global_allocator]
77
static ALLOCATOR: esp_alloc::EspHeap = esp_alloc::EspHeap::empty();
88

9+
// use display_interface_spi::SPIInterfaceNoCS;
10+
use spi_dma_displayinterface::spi_dma_displayinterface::SPIInterfaceNoCS;
11+
912
use esp_backtrace as _;
10-
use hal::{psram, prelude::*, peripherals::Peripherals,
13+
use hal::{psram, prelude::*,
14+
peripherals::Peripherals,
15+
dma::DmaPriority,
16+
pdma::Dma,
1117
spi::{
12-
master::Spi,
18+
master::{prelude::*, Spi},
1319
SpiMode,
1420
},
1521
clock::{ClockControl, CpuClock}, Delay, Rng, IO};
16-
use display_interface_spi::SPIInterfaceNoCS;
22+
1723
use embedded_graphics::{
1824
mono_font::{ascii::FONT_8X13, MonoTextStyle},
1925
prelude::{Point, RgbColor},
@@ -25,7 +31,8 @@ mod setup;
2531
mod types;
2632
mod app;
2733
use app::app_loop;
28-
use setup::*;
34+
35+
use crate::types::ConfiguredPins;
2936

3037
pub fn init_psram_heap() {
3138
unsafe {
@@ -41,29 +48,64 @@ fn main() -> ! {
4148
init_psram_heap();
4249

4350
let system = peripherals.SYSTEM.split();
44-
let clocks = ClockControl::configure(system.clock_control, CpuClock::Clock240MHz).freeze();
51+
52+
// With DMA we have sufficient throughput, so we can clock down the CPU to 160MHz
53+
let clocks = ClockControl::configure(system.clock_control, CpuClock::Clock160MHz).freeze();
4554

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

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

51-
let spi = Spi::new_no_cs_no_miso(
59+
let lcd_h_res = 240;
60+
let lcd_v_res = 320;
61+
62+
let lcd_sclk = io.pins.gpio19;
63+
let lcd_mosi = io.pins.gpio23;
64+
let lcd_miso = io.pins.gpio25;
65+
let lcd_cs = io.pins.gpio22;
66+
let lcd_dc = io.pins.gpio21.into_push_pull_output();
67+
let _lcd_backlight = io.pins.gpio5.into_push_pull_output();
68+
let lcd_reset = io.pins.gpio18.into_push_pull_output();
69+
70+
let dma = Dma::new(system.dma);
71+
let dma_channel = dma.spi2channel;
72+
73+
let mut descriptors = [0u32; 8 * 3];
74+
let mut rx_descriptors = [0u32; 8 * 3];
75+
76+
let configured_pins = ConfiguredPins {
77+
up_button: io.pins.gpio14.into_pull_up_input(),
78+
down_button: io.pins.gpio12.into_pull_up_input(),
79+
left_button: io.pins.gpio13.into_pull_up_input(),
80+
right_button: io.pins.gpio15.into_pull_up_input(),
81+
dynamite_button: io.pins.gpio26.into_pull_up_input(),
82+
teleport_button: io.pins.gpio27.into_pull_up_input(),
83+
};
84+
85+
let spi = Spi::new(
5286
peripherals.SPI2,
53-
unconfigured_pins.sclk,
54-
unconfigured_pins.mosi,
87+
lcd_sclk,
88+
lcd_mosi,
89+
lcd_miso,
90+
lcd_cs,
5591
60u32.MHz(),
5692
SpiMode::Mode0,
5793
&clocks,
58-
);
94+
// );
95+
).with_dma(dma_channel.configure(
96+
false,
97+
&mut descriptors,
98+
&mut rx_descriptors,
99+
DmaPriority::Priority0,
100+
));
59101

60-
let di = SPIInterfaceNoCS::new(spi, configured_system_pins.dc);
102+
let di = SPIInterfaceNoCS::new(spi, lcd_dc);
61103

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

83-
app_loop(configured_pins, &mut display, seed_buffer);
125+
app_loop(&mut display, lcd_h_res, lcd_v_res, configured_pins, seed_buffer);
84126
loop {}
85127
}

esp-wrover-kit/src/setup.rs

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,8 @@
1-
use crate::types::{UnconfiguredPins, ConfiguredPins, ConfiguredSystemPins};
2-
use embedded_hal::digital::v2::{OutputPin, InputPin};
3-
use hal::gpio::{self, Pins};
1+
use crate::types::ConfiguredPins;
2+
use embedded_hal::digital::v2::InputPin;
43
use spooky_embedded::{ button_keyboard::ButtonKeyboard, embedded_movement_controller::EmbeddedMovementController };
54
use spooky_core;
65

7-
pub fn setup_pins(pins: Pins) -> (UnconfiguredPins<gpio::Unknown>, ConfiguredPins<impl InputPin, impl InputPin, impl InputPin, impl InputPin, impl InputPin,
8-
impl InputPin>, ConfiguredSystemPins<impl OutputPin, impl OutputPin, impl OutputPin>) {
9-
let unconfigured_pins = UnconfiguredPins {
10-
sclk: pins.gpio19,
11-
mosi: pins.gpio23,
12-
};
13-
14-
let configured_pins = ConfiguredPins {
15-
up_button: pins.gpio14.into_pull_up_input(),
16-
down_button: pins.gpio12.into_pull_up_input(),
17-
left_button: pins.gpio13.into_pull_up_input(),
18-
right_button: pins.gpio15.into_pull_up_input(),
19-
dynamite_button: pins.gpio26.into_pull_up_input(),
20-
teleport_button: pins.gpio27.into_pull_up_input(),
21-
};
22-
23-
let configured_system_pins = ConfiguredSystemPins {
24-
dc: pins.gpio21.into_push_pull_output(),
25-
backlight: pins.gpio5.into_push_pull_output(),
26-
reset: pins.gpio18.into_push_pull_output(),
27-
};
28-
29-
(unconfigured_pins, configured_pins, configured_system_pins)
30-
}
31-
326
pub fn setup_button_keyboard<Up: InputPin, Down: InputPin, Left: InputPin, Right: InputPin, Dyn: InputPin, Tel: InputPin>(
337
configured_pins: ConfiguredPins<Up, Down, Left, Right, Dyn, Tel>
348
) -> ButtonKeyboard<Up, Down, Left, Right, Dyn, Tel> {

esp-wrover-kit/src/types.rs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
1-
use hal::gpio;
2-
use embedded_hal::digital::v2::{ InputPin, OutputPin };
3-
4-
// Generic type for unconfigured pins
5-
pub struct UnconfiguredPins<MODE> {
6-
pub sclk: gpio::Gpio19<MODE>,
7-
pub mosi: gpio::Gpio23<MODE>,
8-
}
1+
use embedded_hal::digital::v2::InputPin;
92

103
pub struct ConfiguredPins<Up: InputPin, Down: InputPin, Left: InputPin, Right: InputPin, Dyn: InputPin, Tel: InputPin> {
114
pub up_button: Up,
@@ -15,9 +8,3 @@ pub struct ConfiguredPins<Up: InputPin, Down: InputPin, Left: InputPin, Right: I
158
pub dynamite_button: Dyn,
169
pub teleport_button: Tel,
1710
}
18-
19-
pub struct ConfiguredSystemPins<Dc: OutputPin, Bckl: OutputPin, Reset: OutputPin> {
20-
pub dc: Dc,
21-
pub backlight: Bckl,
22-
pub reset: Reset,
23-
}

esp32-c3-devkit-rust/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "spooky-esp32-c3"
3-
version = "0.7.0"
3+
version = "0.8.0"
44
authors = ["Juraj Michálek <[email protected]>"]
55
edition = "2021"
66
license = "MIT"
@@ -27,7 +27,7 @@ panic-halt = "0.2"
2727
shared-bus = { version = "0.3.0" }
2828
spooky-core = { path = "../spooky-core", default-features = false, features = ["static_maze"]}
2929
spooky-embedded = { path = "../spooky-embedded", default-features = false, features = [ "static_maze" ] }
30-
heapless = { version = "0.7.14", default-features = false }
30+
spi-dma-displayinterface = { path = "../spi-dma-displayinterface", features = ["esp32c3"] }
3131

3232
[features]
3333
default = [ "esp32c3_ili9341" ]

esp32-c3-devkit-rust/src/app.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,22 @@ use embedded_graphics::{pixelcolor::Rgb565, prelude::DrawTarget};
33
use spooky_core::{engine::Engine, spritebuf::SpriteBuf, universe::Universe};
44
use embedded_graphics_framebuf::FrameBuf;
55
use embedded_graphics::prelude::RgbColor;
6+
use display_interface::WriteOnlyDataCommand;
7+
use embedded_hal::digital::v2::OutputPin;
8+
use mipidsi::models::Model;
69
use crate::accel_movement_controller::AccelMovementController;
710
use crate::Accelerometer;
811

9-
pub fn app_loop<DISP>(
10-
display: &mut DISP,
12+
pub fn app_loop<DI, M, RST>(
13+
display: &mut mipidsi::Display<DI, M, RST>,
14+
lcd_h_res:u16,
15+
lcd_v_res:u16,
1116
seed_buffer: [u8; 32],
12-
icm: impl Accelerometer // You'll need to pass your accelerometer device here
13-
)
14-
where
15-
DISP: DrawTarget<Color = Rgb565>,
17+
icm: impl Accelerometer
18+
) where
19+
DI: WriteOnlyDataCommand,
20+
M: Model<ColorFormat = Rgb565>,
21+
RST: OutputPin,
1622
{
1723
let accel_movement_controller = AccelMovementController::new(icm, 0.2);
1824

@@ -30,7 +36,7 @@ where
3036
universe.initialize();
3137

3238
loop {
33-
let _ = display
34-
.draw_iter(universe.render_frame().into_iter());
39+
let pixel_iterator = universe.render_frame().get_pixel_iter();
40+
let _ = display.set_pixels(0, 0, lcd_v_res, lcd_h_res, pixel_iterator);
3541
}
3642
}

0 commit comments

Comments
 (0)