Skip to content

Commit 8e2cb93

Browse files
committed
jolt: Update to latest zephyr-lang-rust
Feedback on zephyr-lang-rust has resulted in numerous API changes. The good news is that I only have a small number of commits on top of zephyr-lang-rust to support this keyboard firmware, and all currently live in: zephyrproject-rtos/zephyr-lang-rust#22
1 parent fb0ddd2 commit 8e2cb93

File tree

4 files changed

+44
-40
lines changed

4 files changed

+44
-40
lines changed

jolt/src/inter.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use core::mem::replace;
44

55
use bbq_keyboard::{Side, serialize::{Decoder, Packet, EventVec, PacketBuffer}, InterState, Event, KeyEvent};
66

7-
use zephyr::driver::uart::Uart;
7+
use zephyr::device::uart::Uart;
88
use zephyr::sync::channel::Sender;
99
use log::{warn, info};
1010

@@ -111,7 +111,7 @@ impl InterHandler {
111111
// TODO: Buffer this better.
112112
while let Some(ch) = self.xmit_buffer.pop_front() {
113113
let buf = [ch];
114-
match self.uart.fifo_fill(&buf) {
114+
match unsafe { self.uart.fifo_fill(&buf) } {
115115
Ok(1) => (),
116116
Ok(_) => (), // TODO: warn?
117117
Err(_) => (),
@@ -141,7 +141,7 @@ impl InterHandler {
141141
/// TODO: Buffer this better.
142142
fn uart_read(&mut self) -> zephyr::Result<Option<u8>> {
143143
let mut buf = [0u8];
144-
match self.uart.fifo_read(&mut buf) {
144+
match unsafe { self.uart.fifo_read(&mut buf) } {
145145
Ok(1) => Ok(Some(buf[0])),
146146
Ok(0) => Ok(None),
147147
Ok(_) => unreachable!(),

jolt/src/leds.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55

66
use crate::devices::leds::LedRgb;
77
use zephyr::kobj_define;
8-
use zephyr::object::KobjInit;
9-
use zephyr::driver::led_strip::LedStrip;
8+
use zephyr::device::led_strip::LedStrip;
109
use zephyr::sync::{Arc, Condvar, Mutex};
1110

1211
const OFF: LedRgb = LedRgb::new(0, 0, 0);
@@ -247,10 +246,8 @@ struct LedState {
247246
impl LedManager {
248247
pub fn new(leds: LedStrip) -> Self {
249248

250-
LED_MUTEX_STATIC.init();
251-
LED_CONDVAR_STATIC.init();
252-
let sys_mutex = LED_MUTEX_STATIC.get();
253-
let sys_condvar = LED_CONDVAR_STATIC.get();
249+
let sys_mutex = LED_MUTEX_STATIC.init_once(()).unwrap();
250+
let sys_condvar = LED_CONDVAR_STATIC.init_once(()).unwrap();
254251

255252
let condvar = Condvar::new_from(sys_condvar);
256253
let info = LedInfo {
@@ -259,11 +256,11 @@ impl LedManager {
259256
let info = Arc::new((Mutex::new_from(info, sys_mutex), condvar));
260257

261258
let info2 = info.clone();
262-
let thread = LED_THREAD.spawn(LED_STACK.token(), -1, move || {
259+
let mut thread = LED_THREAD.init_once(LED_STACK.init_once(()).unwrap()).unwrap();
260+
thread.set_priority(-1);
261+
thread.spawn(move || {
263262
led_thread(leds, info2);
264263
});
265-
// TODO: set priority.
266-
thread.start();
267264

268265
LedManager {
269266
states: [
@@ -439,7 +436,7 @@ fn led_thread(mut driver: LedStrip, info: Arc<InfoPair>) -> ! {
439436
loop {
440437
let info = get_info(&*info);
441438
let leds = info.each_ref().map(|l| l.0);
442-
driver.update(&leds[0..limit]).unwrap();
439+
unsafe { driver.update(&leds[0..limit]).unwrap(); }
443440

444441
// Also update the LEDs. For now, just check for 1, but count really should be a multiple
445442
// of 3.

jolt/src/lib.rs

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ use log::info;
1818

1919
use matrix::Matrix;
2020
use zephyr::{kobj_define, printkln};
21-
use zephyr::driver::uart::LineControl;
22-
use zephyr::object::KobjInit;
21+
use zephyr::device::uart::LineControl;
2322
use zephyr::sync::channel::{
2423
self,
2524
Sender,
@@ -63,22 +62,21 @@ extern "C" fn rust_main() {
6362
zephyr::set_logger();
6463

6564
// Initialize the main event queue.
66-
EVENT_QUEUE_STATIC.init();
67-
let equeue = EVENT_QUEUE_STATIC.get();
65+
let equeue = EVENT_QUEUE_STATIC.init_once(()).unwrap();
6866
let (equeue_send, equeue_recv) = channel::unbounded_from::<Event>(equeue);
6967

7068
// This is the steno queue.
71-
STENO_QUEUE_STATIC.init();
72-
let stenoq = STENO_QUEUE_STATIC.get();
69+
let stenoq = STENO_QUEUE_STATIC.init_once(()).unwrap();
7370
let (stenoq_send, stenoq_recv) = channel::unbounded_from::<Stroke>(stenoq);
7471

7572
// Spawn the steno thread.
7673
// TODO: This needs to be lower priority.
7774
let sc = equeue_send.clone();
78-
let thread = STENO_THREAD.spawn(STENO_STACK.token(), 5, move || {
75+
let mut thread = STENO_THREAD.init_once(STENO_STACK.init_once(()).unwrap()).unwrap();
76+
thread.set_priority(5);
77+
thread.spawn(move || {
7978
steno_thread(stenoq_recv, sc);
8079
});
81-
thread.start();
8280

8381
unsafe {
8482
// Store a sender for the USB callback.
@@ -110,20 +108,20 @@ extern "C" fn rust_main() {
110108
let cols = zephyr::devicetree::aliases::matrix::get_cols();
111109

112110
// Build a Vec for these.
113-
let rows: Vec<_> = rows.into_iter().collect();
114-
let cols: Vec<_> = cols.into_iter().collect();
111+
let rows: Vec<_> = rows.into_iter().map(|p| p.unwrap()).collect();
112+
let cols: Vec<_> = cols.into_iter().map(|p| p.unwrap()).collect();
115113

116114
let matrix = Matrix::new(rows, cols, side);
117115
let mut scanner = Scanner::new(matrix, equeue_send.clone(), &info);
118116

119117
let mut layout = LayoutManager::new();
120118

121-
let leds = zephyr::devicetree::aliases::led_strip::get_instance();
119+
let leds = zephyr::devicetree::aliases::led_strip::get_instance().unwrap();
122120
let mut leds = LedManager::new(leds);
123121

124122
let mut inter = get_inter(side, equeue_send.clone());
125123

126-
let mut acm = zephyr::devicetree::labels::acm_uart_0::get_instance();
124+
let mut acm = zephyr::devicetree::labels::acm_uart_0::get_instance().unwrap();
127125
let mut acm_active;
128126

129127
let mut eq_send = SendWrap(equeue_send.clone());
@@ -140,7 +138,7 @@ extern "C" fn rust_main() {
140138

141139
loop {
142140
// Update the state of the Gemini indicator.
143-
if let Ok(1) = acm.line_ctrl_get(LineControl::DTR) {
141+
if let Ok(1) = unsafe { acm.line_ctrl_get(LineControl::DTR) } {
144142
leds.set_base(2, &leds::GEMINI_INDICATOR);
145143
acm_active = true;
146144
} else {
@@ -154,6 +152,7 @@ extern "C" fn rust_main() {
154152
match ev {
155153
Event::Tick => is_tick = true,
156154
Event::Matrix(key) => {
155+
// info!("Matrix: {:?}", key);
157156
match state {
158157
InterState::Primary | InterState::Idle => {
159158
layout.handle_event(key, &mut eq_send);
@@ -189,7 +188,7 @@ extern "C" fn rust_main() {
189188
// TODO: Do the tx enable tx disable stuff.
190189
let packet = stroke.to_gemini();
191190
// Deal with errors and such.
192-
match acm.fifo_fill(&packet) {
191+
match unsafe { acm.fifo_fill(&packet) } {
193192
Ok(_) => (),
194193
Err(_) => (),
195194
}
@@ -304,7 +303,7 @@ extern "C" fn rust_main() {
304303
/// Conditionally return the inter-board code.
305304
#[cfg(CONFIG_JOLT_INTER)]
306305
fn get_inter(side: Side, equeue_send: Sender<Event>) -> Option<InterHandler> {
307-
let uart = zephyr::devicetree::chosen::inter_board_uart::get_instance();
306+
let uart = zephyr::devicetree::chosen::inter_board_uart::get_instance().unwrap();
308307
Some(InterHandler::new(side, uart, equeue_send))
309308
}
310309

@@ -458,7 +457,9 @@ extern "C" fn rust_heartbeat() {
458457
});
459458
// Send it, if there was one there to send.
460459
if let Some(boxed) = boxed {
461-
send.send_boxed(boxed).unwrap();
460+
unsafe {
461+
send.send_boxed(boxed).unwrap();
462+
}
462463
}
463464
}
464465

jolt/src/matrix.rs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ extern crate alloc;
77
use alloc::vec::Vec;
88

99
use zephyr::sys::busy_wait;
10-
use zephyr::sys::gpio::GpioPin;
10+
use zephyr::device::gpio::{GpioPin, GpioToken};
1111
use zephyr::raw::{
1212
GPIO_OUTPUT_INACTIVE,
1313
GPIO_INPUT,
@@ -17,6 +17,7 @@ use zephyr::raw::{
1717
use bbq_keyboard::Side;
1818

1919
pub struct Matrix {
20+
token: GpioToken,
2021
rows: Vec<GpioPin>,
2122
cols: Vec<GpioPin>,
2223
state: Vec<Debouncer>,
@@ -26,8 +27,9 @@ pub struct Matrix {
2627
impl Matrix {
2728
pub fn new(rows: Vec<GpioPin>, cols: Vec<GpioPin>, side: Side) -> Matrix {
2829
let state = (0 .. rows.len() * cols.len()).map(|_| Debouncer::new()).collect();
29-
let mut result = Matrix { rows, cols, state, side };
30-
Self::pin_setup(&mut result.cols, &mut result.rows);
30+
let token = unsafe { GpioToken::get_instance().unwrap() };
31+
let mut result = Matrix { token, rows, cols, state, side };
32+
Self::pin_setup(&mut result.token, &mut result.cols, &mut result.rows);
3133
result
3234
}
3335

@@ -38,11 +40,11 @@ impl Matrix {
3840
let bias = if self.side.is_left() { 0 } else { self.state.len() };
3941
let mut states = self.state.iter_mut().enumerate();
4042
for col in &mut self.cols {
41-
col.set(true);
42-
busy_wait(5);
43-
for row in &self.rows {
43+
unsafe { col.set(&mut self.token, true); }
44+
unsafe { busy_wait(5); }
45+
for row in &mut self.rows {
4446
let (code, state) = states.next().unwrap();
45-
match state.react(row.get()) {
47+
match state.react(unsafe { row.get(&mut self.token) }) {
4648
KeyAction::Press => {
4749
act((code + bias) as u8, true);
4850
}
@@ -52,21 +54,25 @@ impl Matrix {
5254
_ => (),
5355
}
5456
}
55-
col.set(false);
57+
unsafe { col.set(&mut self.token, false); }
5658
// busy_wait(5);
5759
}
5860
}
5961

6062
/// Setup the gpios to drive from 'push' and read from 'pull'.
61-
fn pin_setup(push: &mut [GpioPin], pull: &mut [GpioPin]) {
63+
fn pin_setup(token: &mut GpioToken, push: &mut [GpioPin], pull: &mut [GpioPin]) {
6264
// The 'push' values are the outputs.
6365
for col in push {
64-
col.configure(GPIO_OUTPUT_INACTIVE);
66+
unsafe {
67+
col.configure(token, GPIO_OUTPUT_INACTIVE);
68+
}
6569
}
6670

6771
// And 'pull' are the inputs.
6872
for row in pull {
69-
row.configure(GPIO_INPUT | GPIO_PULL_DOWN);
73+
unsafe {
74+
row.configure(token, GPIO_INPUT | GPIO_PULL_DOWN);
75+
}
7076
}
7177
}
7278
}

0 commit comments

Comments
 (0)