Skip to content

UART support with Midi example #39

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,4 @@ embedded-sdmmc = "0.4.0"
usbd-midi = { git = "https://github.com/btrepp/usbd-midi/" }
num_enum = { version = "0.5.1", default-features = false }
usb-device = "0.2.8"
midi-port = { version = "0.1.0"}
59 changes: 59 additions & 0 deletions examples/uart_midi.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//! examples/uart_midi.rs
#![no_main]
#![no_std]

#[rtic::app(
device = stm32h7xx_hal::stm32,
peripherals = true
)]
mod app {
use libdaisy::logger;
use log::info;

use stm32h7xx_hal::prelude::*;
use stm32h7xx_hal::serial::Serial;
use stm32h7xx_hal::stm32::USART1;

use libdaisy::system;
use libdaisy::uart;

use midi_port::*;

#[shared]
struct Shared {}

#[local]
struct Local {
midi: midi_port::MidiInPort<Serial<USART1>>,
}

#[init]
fn init(ctx: init::Context) -> (Shared, Local, init::Monotonics) {
logger::init();
let mut system = system::System::init(ctx.core, ctx.device);
info!("Startup done!");

let midi = MidiInPort::new(uart::serial(
system.gpio.daisy13.take().unwrap().into_alternate(),
system.gpio.daisy14.take().unwrap().into_alternate(),
stm32h7xx_hal::serial::config::Config::default()
.baudrate(31_250.bps())
.parity_none(),
system.uart.usart1.take().unwrap(),
&system.clocks,
));

(Shared {}, Local { midi }, init::Monotonics())
}

#[idle(local=[midi])]
fn idle(ctx: idle::Context) -> ! {
loop {
ctx.local.midi.poll_uart();

if let Some(message) = ctx.local.midi.get_message() {
info!("{:?}", message);
}
}
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub mod prelude;
pub mod sdmmc;
pub mod sdram;
pub mod system;
pub mod uart;

// Delay for ms, note if interrupts are active delay time will extend
pub fn delay_ms(ms: u32) {
Expand Down
12 changes: 12 additions & 0 deletions src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#![allow(dead_code)]
// #![allow(unused_variables)]

use hal::rcc::CoreClocks;
use log::info;

use stm32h7xx_hal::{
Expand Down Expand Up @@ -44,6 +45,8 @@ pub struct System {
pub timer2: Timer<TIM2>,
pub sdram: &'static mut [f32],
pub flash: crate::flash::Flash,
pub clocks: CoreClocks,
pub uart: crate::uart::UART,
}

impl System {
Expand Down Expand Up @@ -269,6 +272,13 @@ impl System {
gpiog.pg6,
);

let uart = crate::uart::UART {
usart1: Some((device.USART1, ccdr.peripheral.USART1)),
usart3: Some((device.USART3, ccdr.peripheral.USART3)),
uart4: Some((device.UART4, ccdr.peripheral.UART4)),
uart5: Some((device.UART5, ccdr.peripheral.UART5)),
};

System {
gpio,
audio,
Expand All @@ -279,6 +289,8 @@ impl System {
timer2,
sdram,
flash,
uart,
clocks: ccdr.clocks,
}
}
}
Expand Down
25 changes: 25 additions & 0 deletions src/uart.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use stm32h7xx_hal::{
self as hal, rcc, serial::config::Config, serial::Serial, serial::SerialExt, stm32,
};

pub struct UART {
pub usart1: Option<(stm32::USART1, rcc::rec::Usart1)>,
pub usart3: Option<(stm32::USART3, rcc::rec::Usart3)>,
pub uart4: Option<(stm32::UART4, rcc::rec::Uart4)>,
pub uart5: Option<(stm32::UART5, rcc::rec::Uart5)>,
}

pub fn serial<
UART,
UARTSERIAL: SerialExt<UART>,
TX: stm32h7xx_hal::serial::PinTx<UART>,
RX: stm32h7xx_hal::serial::PinRx<UART>,
>(
tx: TX,
rx: RX,
config: Config,
uart: (UARTSERIAL, UARTSERIAL::Rec),
clocks: &hal::rcc::CoreClocks,
) -> Serial<UART> {
uart.0.serial((tx, rx), config, uart.1, clocks).unwrap()
}