22
22
23
23
mod channel;
24
24
25
- use core:: sync:: atomic:: { AtomicBool , AtomicU8 , AtomicUsize , Ordering } ;
25
+ use core:: sync:: atomic:: { AtomicBool , AtomicUsize , Ordering } ;
26
26
27
27
use crate :: channel:: Channel ;
28
28
@@ -37,13 +37,13 @@ struct Logger;
37
37
38
38
/// Global logger lock.
39
39
static TAKEN : AtomicBool = AtomicBool :: new ( false ) ;
40
- static INTERRUPTS_ACTIVE : AtomicU8 = AtomicU8 :: new ( 0 ) ;
40
+ static mut CS_RESTORE : critical_section :: RestoreState = critical_section :: RestoreState :: invalid ( ) ;
41
41
static mut ENCODER : defmt:: Encoder = defmt:: Encoder :: new ( ) ;
42
42
43
43
unsafe impl defmt:: Logger for Logger {
44
44
fn acquire ( ) {
45
45
// safety: Must be paired with corresponding call to release(), see below
46
- let token = unsafe { critical_section:: acquire ( ) } ;
46
+ let restore = unsafe { critical_section:: acquire ( ) } ;
47
47
48
48
if TAKEN . load ( Ordering :: Relaxed ) {
49
49
panic ! ( "defmt logger taken reentrantly" )
@@ -52,9 +52,10 @@ unsafe impl defmt::Logger for Logger {
52
52
// no need for CAS because interrupts are disabled
53
53
TAKEN . store ( true , Ordering :: Relaxed ) ;
54
54
55
- INTERRUPTS_ACTIVE . store ( token, Ordering :: Relaxed ) ;
55
+ // safety: accessing the `static mut` is OK because we have acquired a critical section.
56
+ unsafe { CS_RESTORE = restore } ;
56
57
57
- // safety: accessing the `static mut` is OK because we have disabled interrupts .
58
+ // safety: accessing the `static mut` is OK because we have acquired a critical section .
58
59
unsafe { ENCODER . start_frame ( do_write) }
59
60
}
60
61
@@ -64,16 +65,20 @@ unsafe impl defmt::Logger for Logger {
64
65
}
65
66
66
67
unsafe fn release ( ) {
67
- // safety: accessing the `static mut` is OK because we have disabled interrupts .
68
+ // safety: accessing the `static mut` is OK because we have acquired a critical section .
68
69
ENCODER . end_frame ( do_write) ;
69
70
70
71
TAKEN . store ( false , Ordering :: Relaxed ) ;
72
+
73
+ // safety: accessing the `static mut` is OK because we have acquired a critical section.
74
+ let restore = CS_RESTORE ;
75
+
71
76
// safety: Must be paired with corresponding call to acquire(), see above
72
- critical_section:: release ( INTERRUPTS_ACTIVE . load ( Ordering :: Relaxed ) ) ;
77
+ critical_section:: release ( restore ) ;
73
78
}
74
79
75
80
unsafe fn write ( bytes : & [ u8 ] ) {
76
- // safety: accessing the `static mut` is OK because we have disabled interrupts .
81
+ // safety: accessing the `static mut` is OK because we have acquired a critical section .
77
82
ENCODER . write ( bytes, do_write) ;
78
83
}
79
84
}
0 commit comments