Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(esp32c3): add a working USB-Serial driver #205

Merged
merged 6 commits into from
Jul 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,12 @@ build-c3 board:
--bin {{ board }}

# flash an ESP32-C3 with the MnemOS WiFi Buddy firmware
flash-c3 board: (_get-cargo-command "espflash" "cargo-espflash") (build-c3 board)
flash-c3 board *espflash-args: (_get-cargo-command "espflash" "cargo-espflash") (build-c3 board)
cd {{ _espbuddy_dir }} && \
{{ _cargo }} espflash flash \
--release \
--bin {{ board }} \
--monitor
{{ espflash-args }}

# run crowtty (a host serial multiplexer, log viewer, and pseudo-keyboard)
crowtty *FLAGS:
Expand Down
1 change: 1 addition & 0 deletions platforms/esp32c3-buddy/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions platforms/esp32c3-buddy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ version = "0.1.37"
features = ["attributes"]
default-features = false

[dependencies.futures]
version = "0.3.21"
default-features = false
features = ["async-await"]

### patches ###

[patch.crates-io.maitake]
Expand Down
47 changes: 6 additions & 41 deletions platforms/esp32c3-buddy/src/bin/qtpy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ extern crate alloc;

use esp32c3_hal::{
clock::ClockControl, peripherals::Peripherals, prelude::*, systimer::SystemTimer,
timer::TimerGroup, Rtc, IO,
timer::TimerGroup, Rtc,
};
use esp_backtrace as _;
use esp_println::println;
use mnemos_esp32c3_buddy::drivers;

#[entry]
fn main() -> ! {
Expand Down Expand Up @@ -39,50 +37,17 @@ fn main() -> ! {
rtc.rwdt.disable();
wdt0.disable();
wdt1.disable();
println!("Hello world!");

let k = mnemos_esp32c3_buddy::init();

let io = IO::new(peripherals.GPIO, peripherals.IO_MUX);

// initialize SimpleSerial driver
k.initialize({
use esp32c3_hal::uart::{
config::{Config, DataBits, Parity, StopBits},
TxRxPins, Uart,
};

let config = Config {
baudrate: 115200,
data_bits: DataBits::DataBits8,
parity: Parity::ParityNone,
stop_bits: StopBits::STOP1,
};

let pins = TxRxPins::new_tx_rx(
io.pins.gpio1.into_push_pull_output(),
io.pins.gpio2.into_floating_input(),
);

let uart0 = Uart::new_with_config(
peripherals.UART0,
Some(config),
Some(pins),
&clocks,
&mut system.peripheral_clock_control,
);

drivers::uart::C3Uart::uart0(uart0).register(k, 4096, 4096)
})
.unwrap();

mnemos_esp32c3_buddy::spawn_serial(
&k,
peripherals.USB_DEVICE,
&mut system.peripheral_clock_control,
);
mnemos_esp32c3_buddy::spawn_daemons(k);

// configure system timer
let syst = SystemTimer::new(peripherals.SYSTIMER);

println!("SYSTIMER Current value = {}", SystemTimer::now());

// Alarm 1 will be used to generate "sleep until" interrupts.
let alarm1 = syst.alarm1;

Expand Down
47 changes: 6 additions & 41 deletions platforms/esp32c3-buddy/src/bin/xiao.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ extern crate alloc;

use esp32c3_hal::{
clock::ClockControl, peripherals::Peripherals, prelude::*, systimer::SystemTimer,
timer::TimerGroup, Rtc, IO,
timer::TimerGroup, Rtc,
};
use esp_backtrace as _;
use esp_println::println;
use mnemos_esp32c3_buddy::drivers;

#[entry]
fn main() -> ! {
Expand Down Expand Up @@ -39,50 +37,17 @@ fn main() -> ! {
rtc.rwdt.disable();
wdt0.disable();
wdt1.disable();
println!("Hello world!");

let k = mnemos_esp32c3_buddy::init();

let io = IO::new(peripherals.GPIO, peripherals.IO_MUX);

// initialize SimpleSerial driver
k.initialize({
use esp32c3_hal::uart::{
config::{Config, DataBits, Parity, StopBits},
TxRxPins, Uart,
};

let config = Config {
baudrate: 115200,
data_bits: DataBits::DataBits8,
parity: Parity::ParityNone,
stop_bits: StopBits::STOP1,
};

let pins = TxRxPins::new_tx_rx(
io.pins.gpio1.into_push_pull_output(),
io.pins.gpio2.into_floating_input(),
);

let uart0 = Uart::new_with_config(
peripherals.UART0,
Some(config),
Some(pins),
&clocks,
&mut system.peripheral_clock_control,
);

drivers::uart::C3Uart::uart0(uart0).register(k, 4096, 4096)
})
.unwrap();

mnemos_esp32c3_buddy::spawn_daemons(k);
mnemos_esp32c3_buddy::spawn_serial(
&k,
peripherals.USB_DEVICE,
&mut system.peripheral_clock_control,
);

// configure system timer
let syst = SystemTimer::new(peripherals.SYSTIMER);

println!("SYSTIMER Current value = {}", SystemTimer::now());

// Alarm 1 will be used to generate "sleep until" interrupts.
let alarm1 = syst.alarm1;

Expand Down
1 change: 1 addition & 0 deletions platforms/esp32c3-buddy/src/drivers/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod uart;
pub mod usb_serial;
20 changes: 14 additions & 6 deletions platforms/esp32c3-buddy/src/drivers/uart.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
use core::{
ptr::{null_mut},
ptr::null_mut,
sync::atomic::{AtomicPtr, Ordering},
};

use esp32c3_hal::{uart::{Uart, Instance}, prelude::interrupt, peripherals::UART0};
use esp32c3_hal::{
peripherals::UART0,
prelude::interrupt,
uart::{Instance, Uart},
};

use kernel::{
comms::{
Expand Down Expand Up @@ -47,7 +51,10 @@ pub struct C3Uart<T: 'static> {

impl C3Uart<UART0> {
pub fn uart0(uart: Uart<'static, UART0>) -> Self {
Self { uart, tx_done: &UART0_TX_DONE }
Self {
uart,
tx_done: &UART0_TX_DONE,
}
}

pub fn handle_uart0_int() {
Expand Down Expand Up @@ -89,7 +96,9 @@ impl<T: Instance> C3Uart<T> {

self.uart.listen_tx_done();
// TODO(eliza): what should we do if this errors?
self.uart.write_bytes(&rx).expect("UART write bytes should succeed?");
self.uart
.write_bytes(&rx)
.expect("UART write bytes should succeed?");

// wait for the write to complete
wait.await.expect("UART TX_DONE WaitCell is never closed!");
Expand All @@ -104,7 +113,6 @@ impl<T: Instance> C3Uart<T> {
cap_in: usize,
cap_out: usize,
) -> Result<(), registry::RegistrationError> {

let (kprod, kcons) = KChannel::<Message<SimpleSerialService>>::new_async(4)
.await
.split();
Expand Down Expand Up @@ -137,4 +145,4 @@ fn UART0() {
} else {
panic!("unexpected UART interrupt!");
}
}
}
Loading