diff --git a/.vscode/tasks.json b/.vscode/tasks.json index db1e20156d78c951c233e3b80949689fa3b148f3..91ef4d541d9e9a738b6a348563d41ea7d3ded3a6 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -330,7 +330,7 @@ { "type": "shell", "label": "cargo build --example bare10 --release", - "command": "cargo build --example bare10 --release --features \"hal rtfm\"", + "command": "cargo build --example bare10 --release --features \"hal rtfm rtfm-tq\"", "problemMatcher": [ "$rustc" ], diff --git a/examples/bare10.rs b/examples/bare10.rs index 88532d5a72f100d8f7d4f0ef2ce46cbdd9a20bd2..fab8e4824eee8f578ad6d142dba76fdac592e06e 100644 --- a/examples/bare10.rs +++ b/examples/bare10.rs @@ -42,9 +42,9 @@ pub struct Cmd { impl Cmd { fn parse(&mut self, cmd_byte: [u8; 10], cmd_length: usize) { - if cmd_byte.starts_with(&[b'o', b'n', b' ']) { + if cmd_byte.starts_with(&[b'o', b'n']) { self.cmd_type = CmdType::On; - self.half_period = freq_to_half_period(1); + self.half_period = Cmd::freq_to_half_period(1); } else if cmd_byte.starts_with(&[b's', b'e', b't', b' ']) { let mut tmp = 0; for c in &cmd_byte[4..cmd_length] { @@ -55,16 +55,16 @@ impl Cmd { } } self.cmd_type = CmdType::Set; - self.half_period = freq_to_half_period(tmp); + self.half_period = Cmd::freq_to_half_period(tmp); } else { self.cmd_type = CmdType::Off; - self.half_period = freq_to_half_period(1); + self.half_period = Cmd::freq_to_half_period(1); } } fn freq_to_half_period(freq: u32) -> u32 { if freq == 0 { - return 1; + return Cmd::freq_to_half_period(1); } else { return SYSTEM_CLOCK_FREQ / (2 * freq); } @@ -91,7 +91,7 @@ const APP: () = { static mut CURRENT_CMD: Cmd = (); // init runs in an interrupt free section> - #[init] + #[init(spawn = [off])] fn init() { let stim = &mut core.ITM.stim[0]; iprintln!(stim, "bare10"); @@ -122,6 +122,7 @@ const APP: () = { // Separate out the sender and receiver of the serial port let (tx, rx) = serial.split(); + let _ = spawn.off(); // Our split serial TX = tx; RX = rx; @@ -131,7 +132,7 @@ const APP: () = { IS_LED_ON = false; CURRENT_CMD = Cmd { cmd_type: CmdType::Off, - frequency: 1, + half_period: Cmd::freq_to_half_period(1), }; // CMD Interpretor B = [0u8; 10]; @@ -149,7 +150,7 @@ const APP: () = { } } - #[task(priority = 1, capacity = 10, resources = [IS_LED_ON, B, B_INDEX, CURRENT_CMD, ITM], spawn = [echo, on, off])] + #[task(priority = 1, capacity = 10, resources = [IS_LED_ON, B, B_INDEX, CURRENT_CMD, ITM], spawn = [on, off])] fn cmd_interpretor(byte: u8) { let is_led_on = *resources.IS_LED_ON; let mut b = *resources.B; @@ -185,29 +186,68 @@ const APP: () = { iprintln!(stim, "IS_LED_ON: {:?}", is_led_on); match cmd.cmd_type { - CmdType::Off => iprintln!(stim, "CURRENT_CMD: off, {:?}", cmd.frequency), - CmdType::On => iprintln!(stim, "CURRENT_CMD: on, {:?}", cmd.frequency), - CmdType::Set => iprintln!(stim, "CURRENT_CMD: set, {:?}", cmd.frequency), + CmdType::Off => iprintln!(stim, "CURRENT_CMD: off, {:?}", cmd.half_period), + CmdType::On => iprintln!(stim, "CURRENT_CMD: on, {:?}", cmd.half_period), + CmdType::Set => iprintln!(stim, "CURRENT_CMD: set, {:?}", cmd.half_period), } - - let _ = spawn.on(); *resources.B = b; *resources.B_INDEX = b_i; } - #[task(priority = 1, schedule = [off], resources = [LED, IS_LED_ON])] + #[task(priority = 1, schedule = [off, on], resources = [CURRENT_CMD, LED, IS_LED_ON, ITM], spawn = [off])] fn on() { - if - schedule.off(Instant::now + ) - resources.LED.set_high(); - *resources.IS_LED_ON = true; + let cmd = resources.CURRENT_CMD; + let stim = &mut resources.ITM.stim[0]; + iprintln!(stim, "entered on: {:?}", scheduled); + + + match cmd.cmd_type { + CmdType::On => { + schedule.on(scheduled + (cmd.half_period).cycles()); + if *resources.IS_LED_ON == false { + resources.LED.set_high(); + *resources.IS_LED_ON = true; + } + }, + CmdType::Off => { + let _ = spawn.off(); + }, + CmdType::Set => { + schedule.off(scheduled + (cmd.half_period).cycles()); + if *resources.IS_LED_ON == false { + resources.LED.set_high(); + *resources.IS_LED_ON = true; + } + }, + } } - #[task(priority = 1, resources = [LED, IS_LED_ON])] + #[task(priority = 1, schedule = [off, on], resources = [CURRENT_CMD, LED, IS_LED_ON, ITM], spawn = [on])] fn off() { - resources.LED.set_low(); - *resources.IS_LED_ON = false; + let cmd = resources.CURRENT_CMD; + let stim = &mut resources.ITM.stim[0]; + iprintln!(stim, "entered off: {:?}", scheduled); + + match cmd.cmd_type { + CmdType::On => { + let _ = spawn.on(); + }, + CmdType::Off => { + schedule.off(scheduled + (cmd.half_period).cycles()); + if *resources.IS_LED_ON == true { + resources.LED.set_low(); + *resources.IS_LED_ON = false; + } + }, + CmdType::Set => { + schedule.on(scheduled + (cmd.half_period).cycles()); + if *resources.IS_LED_ON == true { + resources.LED.set_low(); + *resources.IS_LED_ON = false; + } + }, + } } #[task(priority = 1, resources = [ITM])] @@ -216,15 +256,6 @@ const APP: () = { iprintln!(stim, "{:?}", error); } - #[task(priority = 2, resources = [TX], spawn = [trace_error])] - fn echo(byte: u8) { - let tx = resources.TX; - - if block!(tx.write(byte)).is_err() { - let _ = spawn.trace_error(Error::UsartSendOverflow); - } - } - #[interrupt(priority = 3, resources = [RX], spawn = [trace_error, cmd_interpretor])] fn USART2() { let rx = resources.RX;