Skip to content

Commit c466dfb

Browse files
authored
Add M5Stack Core S3 (#70)
* initialize aw9523 GPIO Expander * add m5stack cores3 support
1 parent 5690489 commit c466dfb

File tree

6 files changed

+311
-0
lines changed

6 files changed

+311
-0
lines changed

m5stack-cores3/.cargo/config.toml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
[target.'cfg(target_arch = "xtensa")']
2+
runner = "espflash flash --monitor"
3+
rustflags = [
4+
"-C", "link-arg=-Tlinkall.x",
5+
"-C", "link-arg=-nostartfiles",
6+
]
7+
8+
[env]
9+
# Use clean build after changing ESP_LOGLEVEL
10+
ESP_LOGLEVEL="DEBUG"
11+
12+
[build]
13+
# Uncomment the target if you'd like to use automatic code hinting in your IDE
14+
#target = "xtensa-esp32-none-elf"
15+
# target = "xtensa-esp32s2-none-elf"
16+
target = "xtensa-esp32s3-none-elf"
17+
# target = "riscv32imac-unknown-none-elf"
18+
19+
[unstable]
20+
build-std = [ "core", "alloc" ]

m5stack-cores3/Cargo.toml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
[package]
2+
name = "spooky-m5stack-cores3"
3+
version = "0.10.0"
4+
authors = ["Juraj Michálek <[email protected]>"]
5+
edition = "2021"
6+
license = "MIT"
7+
8+
[target.xtensa-esp32s3-none-elf.dependencies]
9+
hal = { package = "esp32s3-hal", version = "0.13.0" }
10+
esp-backtrace = { version = "0.9.0", features = [
11+
"esp32s3",
12+
"panic-handler",
13+
"print-uart",
14+
] }
15+
16+
[dependencies]
17+
embedded-graphics = "0.8.0"
18+
embedded-graphics-framebuf = { version = "0.3.0", git = "https://github.com/georgik/embedded-graphics-framebuf.git", branch = "feature/embedded-graphics-0.8" }
19+
embedded-hal = "0.2"
20+
log = { version = "0.4.18" }
21+
display-interface = "0.4"
22+
display-interface-spi = "0.4"
23+
mpu9250 = { version = "0.25.0", default-features = false, features = [
24+
"i2c",
25+
], optional = true }
26+
mpu6050 = { version = "0.1.6", optional = true }
27+
mpu6886 = { version = "0.1.0", optional = true }
28+
mipidsi = "0.7.1"
29+
panic-halt = "0.2"
30+
shared-bus = { version = "0.3.0" }
31+
spooky-core = { path = "../spooky-core", default-features = false, features = [
32+
"static_maze",
33+
] }
34+
spooky-embedded = { path = "../spooky-embedded", default-features = false, features = [ "esp32s3", "static_maze", "resolution_320x240" ] }
35+
heapless = { version = "0.8.0", default-features = false }
36+
esp-println = { version = "0.7.1", features = [ "esp32s3", "log" ] }
37+
axp2101 = { git = "https://github.com/georgik/axp2101-rs.git" }
38+
aw9523 = { git = "https://github.com/georgik/aw9523-rs.git" }
39+
spi-dma-displayinterface = { path = "../spi-dma-displayinterface", features = [ "esp32s3" ] }
40+
41+
[features]
42+
default = []

m5stack-cores3/diagram.json

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
{
2+
"version": 1,
3+
"author": "Kirill Mikhailov [playfulFence]",
4+
"editor": "wokwi",
5+
"parts": [
6+
{
7+
"type": "wokwi-esp32-devkit-v1",
8+
"id": "esp",
9+
"top": -494.32,
10+
"left": -455.03,
11+
"attrs": { "builder": "rust-std-esp32" }
12+
},
13+
{
14+
"type": "wokwi-ili9341",
15+
"id": "lcd1",
16+
"top": -546.22,
17+
"left": -134.92,
18+
"rotate": 90,
19+
"attrs": {}
20+
},
21+
{ "type": "wokwi-mpu6050", "id": "imu1", "top": -235.29, "left": -316.38, "attrs": {} },
22+
{
23+
"type": "wokwi-pushbutton",
24+
"id": "btn1",
25+
"top": -234.07,
26+
"left": -490.67,
27+
"attrs": { "color": "green" }
28+
},
29+
{
30+
"type": "wokwi-pushbutton",
31+
"id": "btn2",
32+
"top": -234.73,
33+
"left": -402,
34+
"attrs": { "color": "red" }
35+
}
36+
],
37+
"connections": [
38+
[ "esp:TX0", "$serialMonitor:RX", "", [] ],
39+
[ "esp:RX0", "$serialMonitor:TX", "", [] ],
40+
[ "esp:3V3", "lcd1:VCC", "green", [] ],
41+
[ "esp:GND.1", "lcd1:GND", "black", [] ],
42+
[ "esp:D18", "lcd1:SCK", "blue", [] ],
43+
[ "esp:D23", "lcd1:MOSI", "orange", [] ],
44+
[ "esp:D5", "lcd1:CS", "red", [] ],
45+
[ "esp:D15", "lcd1:D/C", "magenta", [] ],
46+
[ "esp:D4", "lcd1:RST", "yellow", [] ],
47+
[ "lcd1:LED", "esp:3V3", "white", [] ],
48+
[ "imu1:VCC", "esp:3V3", "red", [ "v0" ] ],
49+
[ "imu1:GND", "esp:GND.2", "black", [ "v0" ] ],
50+
[ "imu1:SDA", "esp:D21", "green", [ "v-144.89", "h227.76" ] ],
51+
[ "imu1:SCL", "esp:D22", "green", [ "v0" ] ],
52+
[ "btn1:1.l", "esp:VIN", "green", [ "h0" ] ],
53+
[ "btn1:2.r", "esp:D34", "green", [ "v0" ] ],
54+
[ "btn2:1.l", "esp:VIN", "green", [ "h0" ] ],
55+
[ "btn2:2.r", "esp:D35", "green", [ "v0" ] ]
56+
],
57+
"serialMonitor": { "display": "terminal" },
58+
"dependencies": {}
59+
}

m5stack-cores3/rust-toolchain.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[toolchain]
2+
channel = "esp"
3+
components = ["rustfmt", "rustc-dev"]
4+
targets = ["xtensa-esp32-none-elf"]
5+
# targets = ["xtensa-esp32-none-elf", "xtensa-esp32s2-none-elf","xtensa-esp32s3-none-elf"]

m5stack-cores3/src/main.rs

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
#![no_std]
2+
#![no_main]
3+
4+
// https://shop.m5stack.com/products/m5stack-cores3-esp32s3-lotdevelopment-kit
5+
6+
use spi_dma_displayinterface::spi_dma_displayinterface;
7+
8+
use embedded_graphics::{
9+
mono_font::{ascii::FONT_8X13, MonoTextStyle},
10+
prelude::{Point, RgbColor},
11+
text::Text,
12+
Drawable,
13+
};
14+
15+
use hal::{
16+
clock::{ClockControl, CpuClock},
17+
dma::DmaPriority,
18+
gdma::Gdma,
19+
i2c,
20+
peripherals::Peripherals,
21+
prelude::*,
22+
spi::{
23+
master::{prelude::*, Spi},
24+
SpiMode,
25+
},
26+
Delay, Rng, IO, gpio::PushPull,
27+
};
28+
29+
use esp_backtrace as _;
30+
use log::info;
31+
32+
#[cfg(feature = "mpu9250")]
33+
use mpu9250::{ImuMeasurements, Mpu9250};
34+
35+
#[cfg(feature = "mpu6050")]
36+
use mpu6050::Mpu6050;
37+
38+
#[cfg(feature = "mpu6886")]
39+
use mpu6886::Mpu6886;
40+
41+
use spooky_core::engine::Engine;
42+
43+
use shared_bus::BusManagerSimple;
44+
45+
use embedded_hal::digital::v2::OutputPin;
46+
47+
use spooky_embedded::{
48+
app::app_loop,
49+
controllers::{
50+
accel::AccelMovementController,
51+
composites::accel_composite::AccelCompositeController
52+
},
53+
embedded_display::{LCD_H_RES, LCD_V_RES, LCD_MEMORY_SIZE},
54+
};
55+
56+
use axp2101::{ I2CPowerManagementInterface, Axp2101 };
57+
use aw9523::I2CGpioExpanderInterface;
58+
59+
#[entry]
60+
fn main() -> ! {
61+
let peripherals = Peripherals::take();
62+
63+
let system = peripherals.SYSTEM.split();
64+
let clocks = ClockControl::configure(system.clock_control, CpuClock::Clock160MHz).freeze();
65+
66+
esp_println::logger::init_logger_from_env();
67+
68+
let mut delay = Delay::new(&clocks);
69+
70+
let io = IO::new(peripherals.GPIO, peripherals.IO_MUX);
71+
72+
73+
let lcd_sclk = io.pins.gpio36;
74+
let lcd_mosi = io.pins.gpio37;
75+
let lcd_cs = io.pins.gpio3;
76+
let lcd_miso = io.pins.gpio17; // random unused pin
77+
let lcd_dc = io.pins.gpio35.into_push_pull_output();
78+
let lcd_reset = io.pins.gpio15.into_push_pull_output();
79+
80+
// I2C
81+
let sda = io.pins.gpio12;
82+
let scl = io.pins.gpio11;
83+
84+
let dma = Gdma::new(peripherals.DMA);
85+
let dma_channel = dma.channel0;
86+
87+
let mut descriptors = [0u32; 8 * 3];
88+
let mut rx_descriptors = [0u32; 8 * 3];
89+
90+
let i2c_bus = i2c::I2C::new(
91+
peripherals.I2C0,
92+
sda,
93+
scl,
94+
400u32.kHz(),
95+
&clocks,
96+
);
97+
98+
let bus = BusManagerSimple::new(i2c_bus);
99+
100+
info!("Initializing AXP2101");
101+
let axp_interface = I2CPowerManagementInterface::new(bus.acquire_i2c());
102+
let mut axp = Axp2101::new(axp_interface);
103+
axp.init().unwrap();
104+
105+
info!("Initializing GPIO Expander");
106+
let aw_interface = I2CGpioExpanderInterface::new(bus.acquire_i2c());
107+
let mut aw = aw9523::Aw9523::new(aw_interface);
108+
aw.init().unwrap();
109+
110+
// M5Stack CORE 2 - https://docs.m5stack.com/en/core/core2
111+
// let mut backlight = io.pins.gpio3.into_push_pull_output();
112+
delay.delay_ms(500u32);
113+
info!("About to initialize the SPI LED driver");
114+
115+
let spi = Spi::new(
116+
peripherals.SPI3,
117+
lcd_sclk,
118+
lcd_mosi,
119+
lcd_miso,
120+
lcd_cs,
121+
20u32.MHz(),
122+
SpiMode::Mode0,
123+
&clocks,
124+
) .with_dma(dma_channel.configure(
125+
false,
126+
&mut descriptors,
127+
&mut rx_descriptors,
128+
DmaPriority::Priority0,
129+
));
130+
131+
delay.delay_ms(500u32);
132+
// backlight.set_high().unwrap();
133+
134+
//https://github.com/m5stack/M5CoreS3/blob/main/src/utility/Config.h#L8
135+
let di = spi_dma_displayinterface::new_no_cs(LCD_MEMORY_SIZE, spi, lcd_dc);
136+
137+
let mut display = mipidsi::Builder::ili9342c_rgb565(di)
138+
.with_display_size(320, 240)
139+
.with_color_order(mipidsi::ColorOrder::Bgr)
140+
.with_invert_colors(mipidsi::ColorInversion::Inverted)
141+
.init(&mut delay, Some(lcd_reset))
142+
.unwrap();
143+
delay.delay_ms(500u32);
144+
info!("Initializing...");
145+
Text::new(
146+
"Initializing...",
147+
Point::new(80, 110),
148+
MonoTextStyle::new(&FONT_8X13, RgbColor::WHITE),
149+
)
150+
.draw(&mut display)
151+
.unwrap();
152+
153+
154+
// #[cfg(any(feature = "mpu9250"))]
155+
// let mut icm = Mpu9250::imu_default(bus.acquire_i2c(), &mut delay).unwrap();
156+
157+
// #[cfg(any(feature = "mpu6050"))]
158+
// let mut icm = Mpu6050::new(bus.acquire_i2c());
159+
160+
// let icm_inner = Mpu6886::new(bus.acquire_i2c());
161+
// let icm = Mpu6886Wrapper::new(icm_inner);
162+
// let is_imu_enabled = match icm.init(&mut delay) {
163+
// Ok(_) => true,
164+
// Err(_) => false,
165+
// };
166+
167+
168+
let mut rng = Rng::new(peripherals.RNG);
169+
let mut seed_buffer = [0u8; 32];
170+
rng.read(&mut seed_buffer).unwrap();
171+
172+
let demo_movement_controller = spooky_core::demo_movement_controller::DemoMovementController::new(seed_buffer);
173+
let movement_controller = demo_movement_controller;
174+
175+
info!("Entering main loop");
176+
app_loop(&mut display, seed_buffer, movement_controller);
177+
loop {}
178+
179+
}

m5stack-cores3/wokwi.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
3+
[wokwi]
4+
version = 1
5+
elf = "target/xtensa-esp32-none-elf/release/spooky-m5stack-cores3"
6+
firmware = "target/xtensa-esp32-none-elf/release/spooky-m5stack-cores3"

0 commit comments

Comments
 (0)