time: remove dependency on embassy-sync.

This commit is contained in:
Dario Nieuwenhuis 2023-04-13 23:50:41 +02:00
parent 62ecd97350
commit e7ff759f1c
3 changed files with 14 additions and 19 deletions

View File

@ -156,7 +156,6 @@ embedded-hal-1 = { package = "embedded-hal", version = "=1.0.0-alpha.10", option
embedded-hal-async = { version = "=0.2.0-alpha.1", optional = true} embedded-hal-async = { version = "=0.2.0-alpha.1", optional = true}
futures-util = { version = "0.3.17", default-features = false } futures-util = { version = "0.3.17", default-features = false }
embassy-sync = { version = "0.2.0", path = "../embassy-sync" }
atomic-polyfill = "1.0.1" atomic-polyfill = "1.0.1"
critical-section = "1.1" critical-section = "1.1"
cfg-if = "1.0.0" cfg-if = "1.0.0"

View File

@ -5,8 +5,7 @@ use std::time::{Duration as StdDuration, Instant as StdInstant};
use std::{mem, ptr, thread}; use std::{mem, ptr, thread};
use atomic_polyfill::{AtomicU8, Ordering}; use atomic_polyfill::{AtomicU8, Ordering};
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex; use critical_section::Mutex as CsMutex;
use embassy_sync::blocking_mutex::Mutex as EmbassyMutex;
use crate::driver::{AlarmHandle, Driver}; use crate::driver::{AlarmHandle, Driver};
@ -40,7 +39,7 @@ struct TimeDriver {
// The STD Driver implementation requires the alarms' mutex to be reentrant, which the STD Mutex isn't // The STD Driver implementation requires the alarms' mutex to be reentrant, which the STD Mutex isn't
// Fortunately, mutexes based on the `critical-section` crate are reentrant, because the critical sections // Fortunately, mutexes based on the `critical-section` crate are reentrant, because the critical sections
// themselves are reentrant // themselves are reentrant
alarms: UninitCell<EmbassyMutex<CriticalSectionRawMutex, RefCell<[AlarmState; ALARM_COUNT]>>>, alarms: UninitCell<CsMutex<RefCell<[AlarmState; ALARM_COUNT]>>>,
zero_instant: UninitCell<StdInstant>, zero_instant: UninitCell<StdInstant>,
signaler: UninitCell<Signaler>, signaler: UninitCell<Signaler>,
} }
@ -58,8 +57,7 @@ crate::time_driver_impl!(static DRIVER: TimeDriver = TimeDriver {
impl TimeDriver { impl TimeDriver {
fn init(&self) { fn init(&self) {
self.once.call_once(|| unsafe { self.once.call_once(|| unsafe {
self.alarms self.alarms.write(CsMutex::new(RefCell::new([ALARM_NEW; ALARM_COUNT])));
.write(EmbassyMutex::new(RefCell::new([ALARM_NEW; ALARM_COUNT])));
self.zero_instant.write(StdInstant::now()); self.zero_instant.write(StdInstant::now());
self.signaler.write(Signaler::new()); self.signaler.write(Signaler::new());
@ -72,7 +70,8 @@ impl TimeDriver {
loop { loop {
let now = DRIVER.now(); let now = DRIVER.now();
let next_alarm = unsafe { DRIVER.alarms.as_ref() }.lock(|alarms| { let next_alarm = critical_section::with(|cs| {
let alarms = unsafe { DRIVER.alarms.as_ref() }.borrow(cs);
loop { loop {
let pending = alarms let pending = alarms
.borrow_mut() .borrow_mut()
@ -139,8 +138,8 @@ impl Driver for TimeDriver {
fn set_alarm_callback(&self, alarm: AlarmHandle, callback: fn(*mut ()), ctx: *mut ()) { fn set_alarm_callback(&self, alarm: AlarmHandle, callback: fn(*mut ()), ctx: *mut ()) {
self.init(); self.init();
unsafe { self.alarms.as_ref() }.lock(|alarms| { critical_section::with(|cs| {
let mut alarms = alarms.borrow_mut(); let mut alarms = unsafe { self.alarms.as_ref() }.borrow_ref_mut(cs);
let alarm = &mut alarms[alarm.id() as usize]; let alarm = &mut alarms[alarm.id() as usize];
alarm.callback = callback as *const (); alarm.callback = callback as *const ();
alarm.ctx = ctx; alarm.ctx = ctx;
@ -149,9 +148,8 @@ impl Driver for TimeDriver {
fn set_alarm(&self, alarm: AlarmHandle, timestamp: u64) -> bool { fn set_alarm(&self, alarm: AlarmHandle, timestamp: u64) -> bool {
self.init(); self.init();
unsafe { self.alarms.as_ref() }.lock(|alarms| { critical_section::with(|cs| {
let mut alarms = alarms.borrow_mut(); let mut alarms = unsafe { self.alarms.as_ref() }.borrow_ref_mut(cs);
let alarm = &mut alarms[alarm.id() as usize]; let alarm = &mut alarms[alarm.id() as usize];
alarm.timestamp = timestamp; alarm.timestamp = timestamp;
unsafe { self.signaler.as_ref() }.signal(); unsafe { self.signaler.as_ref() }.signal();

View File

@ -2,8 +2,7 @@ use core::cell::RefCell;
use core::cmp::{min, Ordering}; use core::cmp::{min, Ordering};
use core::task::Waker; use core::task::Waker;
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex; use critical_section::Mutex;
use embassy_sync::blocking_mutex::Mutex;
use heapless::Vec; use heapless::Vec;
use crate::driver::{allocate_alarm, set_alarm, set_alarm_callback, AlarmHandle}; use crate::driver::{allocate_alarm, set_alarm, set_alarm_callback, AlarmHandle};
@ -129,7 +128,7 @@ impl InnerQueue {
} }
struct Queue { struct Queue {
inner: Mutex<CriticalSectionRawMutex, RefCell<Option<InnerQueue>>>, inner: Mutex<RefCell<Option<InnerQueue>>>,
} }
impl Queue { impl Queue {
@ -140,8 +139,8 @@ impl Queue {
} }
fn schedule_wake(&'static self, at: Instant, waker: &Waker) { fn schedule_wake(&'static self, at: Instant, waker: &Waker) {
self.inner.lock(|inner| { critical_section::with(|cs| {
let mut inner = inner.borrow_mut(); let mut inner = self.inner.borrow_ref_mut(cs);
if inner.is_none() {} if inner.is_none() {}
@ -159,8 +158,7 @@ impl Queue {
} }
fn handle_alarm(&self) { fn handle_alarm(&self) {
self.inner critical_section::with(|cs| self.inner.borrow_ref_mut(cs).as_mut().unwrap().handle_alarm())
.lock(|inner| inner.borrow_mut().as_mut().unwrap().handle_alarm());
} }
fn handle_alarm_callback(ctx: *mut ()) { fn handle_alarm_callback(ctx: *mut ()) {