Add missing serial files

This commit is contained in:
Michael Droogleever
2018-08-18 01:54:43 +02:00
parent 25c069df83
commit bf5cb69f27
3 changed files with 115 additions and 0 deletions

9
src/serial/Cargo.toml Normal file
View File

@@ -0,0 +1,9 @@
[package]
name = "serial"
version = "0.1.0"
[dependencies]
cortex-m-rt="~0.5"
cortex-m-semihosting=""
panic-semihosting = "~0.3"
microbit="~0.5"

View File

@@ -0,0 +1,53 @@
#![no_std]
#![no_main]
extern crate panic_semihosting;
extern crate cortex_m_rt as rt;
extern crate cortex_m_semihosting as sh;
#[macro_use(entry, exception, block)]
extern crate microbit;
use core::fmt::Write;
use rt::ExceptionFrame;
use sh::hio;
use microbit::hal::prelude::*;
use microbit::hal::delay::Delay;
use microbit::hal::serial;
use microbit::hal::serial::BAUD115200;
exception!(HardFault, hard_fault);
fn hard_fault(ef: &ExceptionFrame) -> ! {
panic!("{:#?}", ef);
}
exception!(*, default_handler);
fn default_handler(irqn: i16) {
panic!("Unhandled exception (IRQn = {})", irqn);
}
entry!(main);
fn main() -> ! {
let mut stdout = hio::hstdout().unwrap();
writeln!(stdout, "Start").unwrap();
if let Some(p) = microbit::Peripherals::take() {
// Split GPIO
let mut gpio = p.GPIO.split();
// Create delay provider
let mut delay = Delay::new(p.TIMER0);
// Configure RX and TX pins accordingly
let tx = gpio.pin24.into_push_pull_output().downgrade();
let rx = gpio.pin25.into_floating_input().downgrade();
// Configure serial communication
let (mut tx, mut rx) = serial::Serial::uart0(p.UART0, tx, rx, BAUD115200).split();
write!(tx, "Start\r\n");
loop {
let val = block!(rx.read()).unwrap();
block!(tx.write(val));
}
}
panic!("End");
}

53
src/serial/src/main.rs Normal file
View File

@@ -0,0 +1,53 @@
#![no_std]
#![no_main]
extern crate panic_semihosting;
extern crate cortex_m_rt as rt;
extern crate cortex_m_semihosting as sh;
#[macro_use(entry, exception, block)]
extern crate microbit;
use core::fmt::Write;
use rt::ExceptionFrame;
use sh::hio;
use microbit::hal::prelude::*;
use microbit::hal::delay::Delay;
use microbit::hal::serial;
use microbit::hal::serial::BAUD115200;
exception!(HardFault, hard_fault);
fn hard_fault(ef: &ExceptionFrame) -> ! {
panic!("{:#?}", ef);
}
exception!(*, default_handler);
fn default_handler(irqn: i16) {
panic!("Unhandled exception (IRQn = {})", irqn);
}
entry!(main);
fn main() -> ! {
let mut stdout = hio::hstdout().unwrap();
writeln!(stdout, "Start").unwrap();
if let Some(p) = microbit::Peripherals::take() {
// Split GPIO
let mut gpio = p.GPIO.split();
// Create delay provider
let mut delay = Delay::new(p.TIMER0);
// Configure RX and TX pins accordingly
let tx = gpio.pin24.into_push_pull_output().downgrade();
let rx = gpio.pin25.into_floating_input().downgrade();
// Configure serial communication
let (mut tx, mut rx) = serial::Serial::uart0(p.UART0, tx, rx, BAUD115200).split();
write!(tx, "Start\r\n");
loop {
let val = block!(rx.read()).unwrap();
block!(tx.write(val));
}
}
panic!("End");
}