2222
2323mod channel;
2424
25- use core:: sync:: atomic:: { AtomicBool , AtomicU8 , AtomicUsize , Ordering } ;
25+ use core:: sync:: atomic:: { AtomicBool , AtomicUsize , Ordering } ;
2626
2727use crate :: channel:: Channel ;
2828
@@ -37,13 +37,13 @@ struct Logger;
3737
3838/// Global logger lock.
3939static 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 ( ) ;
4141static mut ENCODER : defmt:: Encoder = defmt:: Encoder :: new ( ) ;
4242
4343unsafe impl defmt:: Logger for Logger {
4444 fn acquire ( ) {
4545 // 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 ( ) } ;
4747
4848 if TAKEN . load ( Ordering :: Relaxed ) {
4949 panic ! ( "defmt logger taken reentrantly" )
@@ -52,9 +52,10 @@ unsafe impl defmt::Logger for Logger {
5252 // no need for CAS because interrupts are disabled
5353 TAKEN . store ( true , Ordering :: Relaxed ) ;
5454
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 } ;
5657
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 .
5859 unsafe { ENCODER . start_frame ( do_write) }
5960 }
6061
@@ -64,16 +65,20 @@ unsafe impl defmt::Logger for Logger {
6465 }
6566
6667 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 .
6869 ENCODER . end_frame ( do_write) ;
6970
7071 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+
7176 // 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 ) ;
7378 }
7479
7580 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 .
7782 ENCODER . write ( bytes, do_write) ;
7883 }
7984}
0 commit comments