From edb0401838e6666740eb5b825f946aee73a5a3f5 Mon Sep 17 00:00:00 2001 From: Astro Date: Thu, 12 Sep 2019 16:12:03 +0200 Subject: [PATCH] add a very small allocator --- firmware/Cargo.lock | 32 ++++++++++++++++++++++++++++ firmware/Cargo.toml | 1 + firmware/src/command_parser.rs | 38 +++++++++++++++++++++++++++++++++- firmware/src/main.rs | 15 +++++++++++++- 4 files changed, 84 insertions(+), 2 deletions(-) diff --git a/firmware/Cargo.lock b/firmware/Cargo.lock index 8d3da42..48bdb0b 100644 --- a/firmware/Cargo.lock +++ b/firmware/Cargo.lock @@ -13,6 +13,15 @@ dependencies = [ "as-slice 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "alloc-cortex-m" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cortex-m 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", + "linked_list_allocator 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "as-slice" version = "0.1.0" @@ -50,6 +59,14 @@ name = "byteorder" version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "cortex-m" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "volatile-register 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "cortex-m" version = "0.5.10" @@ -128,6 +145,7 @@ dependencies = [ name = "ionpak-firmware" version = "1.0.0" dependencies = [ + "alloc-cortex-m 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", "bare-metal 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", "bit_field 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)", "byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)", @@ -158,6 +176,11 @@ name = "libm" version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "linked_list_allocator" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "logos" version = "0.9.7" @@ -326,6 +349,11 @@ name = "void" version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "volatile-register" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "volatile-register" version = "0.2.0" @@ -357,12 +385,14 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [metadata] "checksum aligned 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d39da9b88ae1a81c03c9c082b8db83f1d0e93914126041962af61034ab44c4a5" "checksum aligned 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d3a316c7ea8e1e9ece54862c992def5a7ac14de9f5832b69d71760680efeeefa" +"checksum alloc-cortex-m 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "6d5f7d01bc93ce089de636f946f7f1fdc5e5d751732367e019c9755440e7aef4" "checksum as-slice 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "293dac66b274fab06f95e7efb05ec439a6b70136081ea522d270bc351ae5bb27" "checksum bare-metal 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "5deb64efa5bd81e31fcd1938615a6d98c82eafcbcd787162b6f63b91d6bac5b3" "checksum bit_field 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a165d606cf084741d4ac3a28fb6e9b1eb0bd31f6cd999098cfddb0b2ab381dc0" "checksum bitflags 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3d155346769a6855b86399e9bc3814ab343cd3d62c7e985113d46a0ec3c281fd" "checksum build_const 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "39092a32794787acd8525ee150305ff051b0aa6cc2abaf193924f5ab05425f39" "checksum byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a7c3dd8985a7111efc5c80b44e23ecdd8c007de8ade3b96595387e812b957cf5" +"checksum cortex-m 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "3df5de9a9829f2ccb7defa8945fa020c6614cd2f6ba9b5f33db9241dcc01985e" "checksum cortex-m 0.5.10 (registry+https://github.com/rust-lang/crates.io-index)" = "3c0b159a1e8306949579de3698c841dba58058197b65c60807194e4fa1e7a554" "checksum cortex-m 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)" = "145da2fc379bbd378ed425e75e1748214add9bbd800d4d5b77abb54ca423dbca" "checksum cortex-m-rt 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)" = "17805910e3ecf029bdbfcc42b7384d9e3d9e5626153fa810002c1ef9839338ac" @@ -373,6 +403,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum generic-array 0.12.3 (registry+https://github.com/rust-lang/crates.io-index)" = "c68f0274ae0e023facc3c97b2e00f076be70e254bc851d972503b328db79b2ec" "checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d" "checksum libm 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "7fc7aa29613bd6a620df431842069224d8bc9011086b1db4c0e0cd47fa03ec9a" +"checksum linked_list_allocator 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)" = "47314ec1d29aa869ee7cb5a5be57be9b1055c56567d59c3fb6689926743e0bea" "checksum logos 0.9.7 (registry+https://github.com/rust-lang/crates.io-index)" = "f494e22d293fa05db60b3fd95fb30e9409feb5672b56ce6f250f99d9fbae6b93" "checksum logos-derive 0.9.7 (registry+https://github.com/rust-lang/crates.io-index)" = "13ff1b1068db09ee21d12baf55eccc0900a781a735273e0a606f6f4fbb32a322" "checksum managed 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "43e2737ecabe4ae36a68061398bf27d2bfd0763f4c3c837a398478459494c4b7" @@ -397,6 +428,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum utf8-ranges 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "b4ae116fef2b7fea257ed6440d3cfcff7f190865f170cdad00bb6465bf18ecba" "checksum vcell 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "876e32dcadfe563a4289e994f7cb391197f362b6315dc45e8ba4aa6f564a4b3c" "checksum void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" +"checksum volatile-register 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a470889aa8f2d3ad893bd43cd90c824e63e8ac0ee5fe64c5d81a932d184fd549" "checksum volatile-register 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "0d67cb4616d99b940db1d6bd28844ff97108b498a6ca850e5b6191a532063286" "checksum walkdir 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)" = "bb08f9e670fab86099470b97cd2b252d6527f0b3cc1401acdb595ffc9dd288ff" "checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a" diff --git a/firmware/Cargo.toml b/firmware/Cargo.toml index 3c14a69..bf4702b 100644 --- a/firmware/Cargo.toml +++ b/firmware/Cargo.toml @@ -20,6 +20,7 @@ byteorder = { version = "1.3", default-features = false } bit_field = "0.10" bare-metal = "0.2" logos = { version = "~0.9", default-features = false, features = ["export_derive"] } +alloc-cortex-m = "0.3" [dependencies.smoltcp] git = "https://github.com/m-labs/smoltcp" diff --git a/firmware/src/command_parser.rs b/firmware/src/command_parser.rs index db8b397..f44f305 100644 --- a/firmware/src/command_parser.rs +++ b/firmware/src/command_parser.rs @@ -24,6 +24,10 @@ enum Token { Once, #[token = "continuous"] Continuous, + #[token = "enable"] + Enable, + #[token = "disable"] + Disable, #[regex = "[0-9]+"] Number, @@ -34,6 +38,7 @@ pub enum Error { Parser, UnexpectedEnd, UnexpectedToken(Token), + NoSuchChannel, } #[derive(Debug)] @@ -41,14 +46,24 @@ pub enum ShowCommand { ReportMode, } + +#[derive(Debug)] +pub enum ChannelCommand { + Enable, + Disable, +} + #[derive(Debug)] pub enum Command { Quit, Show(ShowCommand), Report(ReportMode), + Channel(u8, ChannelCommand), } - +const CHANNEL_IDS: &'static [&'static str] = &[ + "0", "1", "2", "3", +]; impl Command { pub fn parse(input: &str) -> Result { @@ -80,6 +95,27 @@ impl Command { ], End => Ok(Command::Report(ReportMode::Once)), ], + Channel => choice![ + Number => { + let channel = CHANNEL_IDS.iter() + .position(|id| *id == lexer.slice()); + match channel { + Some(channel) => { + choice![ + Enable => Ok(Command::Channel( + channel as u8, + ChannelCommand::Enable + )), + Disable => Ok(Command::Channel( + channel as u8, + ChannelCommand::Enable + )), + ] + } + None => Err(Error::NoSuchChannel) + } + }, + ], ] } } diff --git a/firmware/src/main.rs b/firmware/src/main.rs index a1cae4c..2ac9b88 100644 --- a/firmware/src/main.rs +++ b/firmware/src/main.rs @@ -1,14 +1,17 @@ #![feature(const_fn)] +#![feature(alloc_error_handler)] #![no_std] #![no_main] -use cortex_m_rt::entry; +extern crate alloc; +use cortex_m_rt::{entry, heap_start}; use core::fmt::{self, Write}; use smoltcp::time::Instant; use smoltcp::wire::{IpCidr, IpAddress, EthernetAddress}; use smoltcp::iface::{NeighborCache, EthernetInterfaceBuilder}; use smoltcp::socket::{SocketSet, TcpSocket, TcpSocketBuffer}; use cortex_m_semihosting::hio; +use alloc_cortex_m::CortexMHeap; #[macro_export] macro_rules! print { @@ -31,6 +34,15 @@ pub fn panic_fmt(info: &core::panic::PanicInfo) -> ! { loop {} } +#[global_allocator] +static ALLOCATOR: CortexMHeap = CortexMHeap::empty(); +const HEAP_SIZE: usize = 8192; + +#[alloc_error_handler] +fn alloc_error_handler(layout: alloc::alloc::Layout) -> ! { + panic!("Allocation error for: {:?}", layout) +} + mod board; use self::board::{gpio::Gpio, systick::get_time}; mod ethmac; @@ -79,6 +91,7 @@ fn main() -> ! { writeln!(stdout, "ionpak boot").unwrap(); board::init(); writeln!(stdout, "board initialized").unwrap(); + unsafe { ALLOCATOR.init(heap_start() as usize, HEAP_SIZE) }; println!(r#" _ _