1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
//! Sharing memory using a `Resource`
//!
//! This builds on top of the `concurrent` example. The `loopback` task now
//! additionally parses the received data as a command. Three commands are
//! available:
//!
//! - `reverse` - reverses the spin direction of the LED roulette
//! - `bounce` - puts the roulette in *bounce* mode where it reverses its spin
//!   direction every time it completes one turn.
//! - `continuous` - puts the roulette in *continuous* mode where it keeps
//!   spinning in the same direction.
//!
//! ```
//! 
//! #![feature(const_fn)]
//! #![feature(used)]
//! #![no_std]
//! 
//! // version = "0.2.2", default-features = false
//! extern crate cast;
//! 
//! // version = "0.2.0"
//! extern crate cortex_m_rt;
//! 
//! // version = "0.1.0"
//! #[macro_use]
//! extern crate cortex_m_rtfm as rtfm;
//! 
//! extern crate f3;
//! 
//! // version = "0.1.0"
//! extern crate heapless;
//! 
//! use core::cell::Cell;
//! 
//! use cast::{u8, usize};
//! use f3::led::{self, LEDS};
//! use f3::serial::Serial;
//! use f3::stm32f30x::interrupt::{Tim7, Usart1Exti25};
//! use f3::stm32f30x;
//! use f3::timer::Timer;
//! use heapless::Vec;
//! use rtfm::{C1, Local, P0, P1, Resource, T0, T1, TMax};
//! 
//! // SUPPORT CODE
//! struct State {
//!     direction: Cell<Direction>,
//!     mode: Cell<Mode>,
//! }
//! 
//! impl State {
//!     const fn new() -> Self {
//!         State {
//!             direction: Cell::new(Direction::Clockwise),
//!             mode: Cell::new(Mode::Continuous),
//!         }
//!     }
//! }
//! 
//! #[derive(Clone, Copy)]
//! enum Direction {
//!     Clockwise,
//!     Counterclockwise,
//! }
//! 
//! impl Direction {
//!     fn reverse(self) -> Self {
//!         match self {
//!             Direction::Clockwise => Direction::Counterclockwise,
//!             Direction::Counterclockwise => Direction::Clockwise,
//!         }
//!     }
//! }
//! 
//! #[derive(Clone, Copy, PartialEq)]
//! enum Mode {
//!     Bounce,
//!     Continuous,
//! }
//! 
//! // CONFIGURATION
//! pub const BAUD_RATE: u32 = 115_200; // bits per second
//! const FREQUENCY: u32 = 4; // Hz
//! 
//! // RESOURCES
//! peripherals!(stm32f30x, {
//!     GPIOA: Peripheral {
//!         register_block: Gpioa,
//!         ceiling: C0,
//!     },
//!     GPIOE: Peripheral {
//!         register_block: Gpioe,
//!         ceiling: C0,
//!     },
//!     RCC: Peripheral {
//!         register_block: Rcc,
//!         ceiling: C0,
//!     },
//!     TIM7: Peripheral {
//!         register_block: Tim7,
//!         ceiling: C1,
//!     },
//!     USART1: Peripheral {
//!         register_block: Usart1,
//!         ceiling: C1,
//!     },
//! });
//! 
//! static SHARED: Resource<State, C1> = Resource::new(State::new());
//! 
//! // INITIALIZATION PHASE
//! fn init(ref priority: P0, threshold: &TMax) {
//!     let gpioa = GPIOA.access(priority, threshold);
//!     let gpioe = GPIOE.access(priority, threshold);
//!     let rcc = RCC.access(priority, threshold);
//!     let tim7 = TIM7.access(priority, threshold);
//!     let timer = Timer(&tim7);
//!     let usart1 = USART1.access(priority, threshold);
//! 
//!     led::init(&gpioe, &rcc);
//!     timer.init(&rcc, FREQUENCY);
//!     Serial(&usart1).init(&gpioa, &rcc, BAUD_RATE);
//! 
//!     timer.resume();
//! }
//! 
//! // IDLE LOOP
//! fn idle(_priority: P0, _threshold: T0) -> ! {
//!     // Sleep
//!     loop {
//!         rtfm::wfi();
//!     }
//! }
//! 
//! // TASKS
//! tasks!(stm32f30x, {
//!     roulette: Task {
//!         interrupt: Tim7,
//!         priority: P1,
//!         enabled: true,
//!     },
//!     receive: Task {
//!         interrupt: Usart1Exti25,
//!         priority: P1,
//!         enabled: true,
//!     },
//! });
//! 
//! fn receive(mut task: Usart1Exti25, ref priority: P1, ref threshold: T1) {
//!     // 16 byte buffer
//!     static BUFFER: Local<Vec<u8, [u8; 16]>, Usart1Exti25> = {
//!         Local::new(Vec::new([0; 16]))
//!     };
//! 
//!     let usart1 = USART1.access(priority, threshold);
//!     let serial = Serial(&usart1);
//! 
//!     if let Ok(byte) = serial.read() {
//!         if serial.write(byte).is_err() {
//!             // As we are echoing the bytes as soon as they arrive, it should
//!             // be impossible to have a TX buffer overrun
//!             #[cfg(debug_assertions)]
//!             unreachable!()
//!         }
//! 
//!         let buffer = BUFFER.borrow_mut(&mut task);
//! 
//!         if byte == b'r' {
//!             // end of command
//! 
//!             let shared = SHARED.access(priority, threshold);
//!             match &**buffer {
//!                 b"bounce" => shared.mode.set(Mode::Bounce),
//!                 b"continuous" => shared.mode.set(Mode::Continuous),
//!                 b"reverse" => {
//!                     shared.direction.set(shared.direction.get().reverse());
//!                 }
//!                 _ => {}
//!             }
//! 
//!             // clear the buffer to prepare for the next command
//!             buffer.clear();
//!         } else {
//!             // push the byte into the buffer
//! 
//!             if buffer.push(byte).is_err() {
//!                 // error: buffer full
//!                 // KISS: we just clear the buffer when it gets full
//!                 buffer.clear();
//!             }
//!         }
//!     } else {
//!         // Only reachable through `rtfm::request(receive)`
//!         #[cfg(debug_assertions)]
//!         unreachable!()
//!     }
//! }
//! 
//! fn roulette(mut task: Tim7, ref priority: P1, ref threshold: T1) {
//!     static STATE: Local<u8, Tim7> = Local::new(0);
//! 
//!     let tim7 = TIM7.access(priority, threshold);
//!     let timer = Timer(&tim7);
//! 
//!     if timer.clear_update_flag().is_ok() {
//!         let state = STATE.borrow_mut(&mut task);
//!         let curr = *state;
//! 
//!         let shared = SHARED.access(priority, threshold);
//!         let mut direction = shared.direction.get();
//! 
//!         if curr == 0 && shared.mode.get() == Mode::Bounce {
//!             direction = direction.reverse();
//!             shared.direction.set(direction);
//!         }
//! 
//!         let n = u8(LEDS.len()).unwrap();
//!         let next = match direction {
//!             Direction::Clockwise => (curr + 1) % n,
//!             Direction::Counterclockwise => curr.checked_sub(1).unwrap_or(n - 1),
//!         };
//! 
//!         LEDS[usize(curr)].off();
//!         LEDS[usize(next)].on();
//! 
//!         *state = next;
//!     } else {
//!         // Only reachable through `rtfm::request(roulette)`
//!         #[cfg(debug_assertion)]
//!         unreachable!()
//!     }
//! }
//! ```
// Auto-generated. Do not modify.