Skip to content
Snippets Groups Projects
Commit 57c2c1e3 authored by Per Lindgren's avatar Per Lindgren
Browse files

eq implemented and tested

parent 032a54c7
No related branches found
No related tags found
No related merge requests found
[{"node":"UART9","expanded":true,"format":0},{"node":"USART2.SR","expanded":true,"format":0}]
\ No newline at end of file
[]
\ No newline at end of file
......@@ -733,5 +733,37 @@
"svdFile": "STM32F413.svd",
"cwd": "${workspaceRoot}"
},
{
"type": "cortex-debug",
"request": "launch",
"servertype": "openocd",
"name": "equivalence (release)",
"preLaunchTask": "cargo build --example equivalence --release",
"executable": "./target/thumbv7em-none-eabihf/release/examples/equivalence",
"configFiles": [
"interface/stlink.cfg",
"target/stm32f4x.cfg"
],
"swoConfig": {
"enabled": true,
"cpuFrequency": 16000000,
"swoFrequency": 2000000,
"source": "probe",
"decoders": [
{
"type": "console",
"label": "ITM0",
"port": 0
},
{
"type": "console",
"label": "ITM1",
"port": 1
}
]
},
"svdFile": "STM32F413.svd",
"cwd": "${workspaceRoot}"
},
]
}
\ No newline at end of file
......@@ -11,7 +11,7 @@
extern crate panic_halt;
use cortex_m::iprintln;
use cortex_m::{asm, iprintln};
use nb::block;
extern crate stm32f4xx_hal as hal;
......@@ -44,6 +44,8 @@ const APP: () = {
let tx = gpioa.pa2.into_alternate_af7();
let rx = gpioa.pa3.into_alternate_af7(); // try comment out
asm::bkpt();
let serial = Serial::usart2(
device.USART2,
(tx, rx),
......@@ -72,6 +74,7 @@ const APP: () = {
match block!(rx.read()) {
Ok(byte) => {
iprintln!(stim, "Ok {:?}", byte);
test(byte);
let _ = tx.write(byte);
}
Err(err) => {
......@@ -82,6 +85,13 @@ const APP: () = {
}
};
#[inline(never)]
fn test(byte: u8) {
unsafe {
core::ptr::read_volatile(&byte);
}
}
// 1. Compile and run the example.
// Verify that it has the same behavior as bare7.
//
......
......@@ -12,17 +12,16 @@ extern crate panic_halt;
use cortex_m::{asm, iprintln};
extern crate stm32f4xx_hal as hal;
// use stm32f4::stm32f413::GPIOA;
use crate::hal::prelude::*;
use crate::hal::serial::{self, config::Config, Rx, Serial, Tx};
use hal::{gpio::Alternate, gpio::AF0, stm32::ITM};
// use heapless::consts::*;
// use heapless::spsc::{Consumer, Producer, Queue};
use hal::{
gpio::{gpioa, Output, PushPull},
stm32::ITM,
};
use nb::block;
use rtfm::{app, Instant};
use rtfm::app;
// Our error type
#[derive(Debug)]
......@@ -32,22 +31,30 @@ pub enum Error {
UsartReceiveOverflow,
}
#[derive(Debug)]
pub enum Event {
Timout,
ChannelA,
ChannelB,
}
#[derive(Debug, Copy, Clone)]
pub struct Data {
a: bool,
b: bool,
event_counter: u32,
out: bool,
}
const TIMEOUT: u32 = 16_000_000;
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum State {
S8000,
S8001,
S8004,
S8014,
S8005,
C001,
C002,
C003,
}
use State::*;
const PERIOD: u32 = 16_000_000;
const DISCREPENCY: u32 = 10;
const T: bool = true;
const F: bool = false;
#[app(device = hal::stm32)]
const APP: () = {
......@@ -55,26 +62,15 @@ const APP: () = {
static mut TX: Tx<hal::stm32::USART2> = ();
static mut RX: Rx<hal::stm32::USART2> = ();
static mut ITM: ITM = ();
static mut LED: hal::gpio::gpioa::PA5<Alternate<AF0>> = ();
// app resources
static mut DATA: Data = Data {
a: false,
b: false,
event_counter: 0,
out: false,
};
static mut LED: gpioa::PA5<Output<PushPull>> = ();
static mut DATA: Data = Data { a: F, b: F };
// init runs in an interrupt free section>
#[init]
#[init(resources = [DATA], schedule = [periodic])]
fn init() {
let stim = &mut core.ITM.stim[0];
iprintln!(stim, "start");
// power on GPIOA, RM0368 6.3.11
device.RCC.ahb1enr.modify(|_, w| w.gpioaen().set_bit());
// configure PA5 as output, RM0368 8.4.1
device.GPIOA.moder.modify(|_, w| w.moder5().bits(1));
iprintln!(stim, "start: {:?}", resources.DATA);
let rcc = device.RCC.constrain();
......@@ -85,7 +81,7 @@ const APP: () = {
let tx = gpioa.pa2.into_alternate_af7();
let rx = gpioa.pa3.into_alternate_af7();
let led = gpioa.pa5.into_alternate_af0();
let led = gpioa.pa5.into_push_pull_output();
let mut serial = Serial::usart2(
device.USART2,
......@@ -99,6 +95,8 @@ const APP: () = {
serial.listen(serial::Event::Rxne);
// Separate out the sender and receiver of the serial port
let (tx, rx) = serial.split();
// Start periodic task
schedule.periodic(start).unwrap();
// pass on late resources
LED = led;
......@@ -132,7 +130,7 @@ const APP: () = {
iprintln!(stim, "{:?}", error);
}
#[interrupt(priority = 3, resources = [RX], spawn = [trace_data, trace_error, echo])]
#[interrupt(priority = 2, resources = [RX], spawn = [trace_data, trace_error, echo])]
fn USART2() {
match resources.RX.read() {
Ok(byte) => {
......@@ -141,13 +139,11 @@ const APP: () = {
spawn.trace_error(Error::RingBufferOverflow).unwrap();
}
}
Err(_err) => {
spawn.trace_error(Error::UsartReceiveOverflow).unwrap()
}
Err(_err) => spawn.trace_error(Error::UsartReceiveOverflow).unwrap(),
}
}
#[task(priority = 2, resources = [TX], spawn = [trace_error, event_a, event_b])]
#[task(priority = 1, resources = [TX, DATA], spawn = [trace_error])]
fn echo(byte: u8) {
let tx = resources.TX;
......@@ -155,61 +151,101 @@ const APP: () = {
spawn.trace_error(Error::UsartSendOverflow).unwrap();
}
let mut data = resources.DATA;
match byte {
b'a' => spawn.event_a(false).unwrap(),
b'b' => spawn.event_b(false).unwrap(),
b'A' => spawn.event_a(true).unwrap(),
b'B' => spawn.event_b(true).unwrap(),
b'a' => data.a = false,
b'b' => data.b = false,
b'A' => data.a = true,
b'B' => data.b = true,
_ => (),
}
}
#[task(priority = 1, resources = [ITM, DATA], schedule = [discrepency])]
fn event_a(val: bool) {
#[task (priority = 1, resources = [LED, ITM, DATA], schedule = [periodic])]
fn periodic() {
// we start directly in the init state S80001
static mut STATE: State = State::S8001;
static mut TIMEOUT: u32 = 0;
let stim = &mut resources.ITM.stim[0];
iprintln!(stim, "event a {}", val);
let data = &mut resources.DATA;
data.a = val;
// evaluate equivalenc.
data.event_counter += 1;
if data.a ^ data.b {
schedule
.discrepency(scheduled + TIMEOUT.cycles(), data.clone())
.unwrap();
data.out = false;
iprintln!(stim, "disc {:?}", data);
let data = resources.DATA;
iprintln!(stim, "Old State {:?}", STATE);
iprintln!(stim, "Timeout {:?}", TIMEOUT);
iprintln!(stim, "{:?}", data);
*STATE = match STATE {
S8000 => match (data.a, data.b) {
(F, F) => S8001,
(F, T) | (T, F) => {
*TIMEOUT = DISCREPENCY;
S8005
}
(T, T) => S8000,
},
S8001 => match (data.a, data.b) {
(F, F) => S8001,
(F, T) => {
*TIMEOUT = DISCREPENCY;
S8014
}
#[task(priority = 1, resources = [ITM, DATA], spawn = [discrepency])]
fn event_b(val: bool) {
let stim = &mut resources.ITM.stim[0];
iprintln!(stim, "event b {}", val);
//evaluate_equivalence(scheduled, &mut resources.DATA);
(T, F) => {
*TIMEOUT = DISCREPENCY;
S8004
}
#[task(priority = 1, resources = [ITM, DATA])]
fn discrepency(data: Data) {
let stim = &mut resources.ITM.stim[0];
iprintln!(stim, "a {} b {}", data.a, data.b);
if data.event_counter == resources.DATA.event_counter {
iprintln!(stim, "timeout");
// data.force_reinit =
(T, T) => S8000,
},
S8004 => {
*TIMEOUT -= 1;
match *TIMEOUT {
0 => C001,
_ => match (data.a, data.b) {
(F, _) => S8001,
(_, T) => S8000,
_ => S8004,
},
}
}
S8014 => {
*TIMEOUT -= 1;
match *TIMEOUT {
0 => C002,
_ => match (data.a, data.b) {
(_, F) => S8001,
(T, _) => S8000,
_ => S8014,
},
}
}
S8005 => {
*TIMEOUT -= 1;
match *TIMEOUT {
0 => C003,
_ => match (data.a, data.b) {
(F, F) => S8001,
_ => S8005,
},
}
}
C001 | C002 => match (data.a, data.b) {
(F, F) => S8001,
_ => C002,
},
C003 => match (data.a, data.b) {
(F, F) => S8001,
_ => C003,
},
};
iprintln!(stim, "New State {:?}\n", STATE);
#[task (priority = 1, resources = [LED])]
fn periodic() {
static mut TOGGLE: bool = false;
if *TOGGLE {
resources.LED.set_low();
} else {
if *STATE == S8000 {
resources.LED.set_high();
} else {
resources.LED.set_low();
}
*TOGGLE = !*TOGGLE;
schedule.periodic(scheduled + PERIOD.cycles()).unwrap();
}
extern "C" {
......
......@@ -16,9 +16,6 @@ use crate::hal::prelude::*;
use crate::hal::serial::{self, config::Config, Rx, Serial, Tx};
use hal::stm32::ITM;
// use heapless::consts::*;
// use heapless::spsc::{Consumer, Producer, Queue};
use nb::block;
use rtfm::{app, Instant};
......@@ -144,9 +141,7 @@ const APP: () = {
spawn.trace_error(Error::RingBufferOverflow).unwrap();
}
}
Err(_err) => {
spawn.trace_error(Error::UsartReceiveOverflow).unwrap()
}
Err(_err) => spawn.trace_error(Error::UsartReceiveOverflow).unwrap(),
}
}
......@@ -172,7 +167,6 @@ const APP: () = {
let stim = &mut resources.ITM.stim[0];
iprintln!(stim, "event a {}", val);
let data = &mut resources.DATA;
data.a = val;
iprintln!(stim, "Start {:?}", data);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment