aboutsummaryrefslogtreecommitdiff
path: root/examples/nrf
diff options
context:
space:
mode:
authoralexmoon <[email protected]>2022-03-28 10:49:17 -0400
committerDario Nieuwenhuis <[email protected]>2022-04-06 05:38:11 +0200
commita22639ad920fccfd909602f5c69147be911650eb (patch)
tree75c29a5ce30a5e9c24bacd871e9f3cce72f0ad90 /examples/nrf
parent46bafecb2a543c96a65780571bf4bd541dd68ea3 (diff)
Remove UnsafeCell from cdc_acm::Control
Diffstat (limited to 'examples/nrf')
-rw-r--r--examples/nrf/src/bin/usb/cdc_acm.rs53
1 files changed, 31 insertions, 22 deletions
diff --git a/examples/nrf/src/bin/usb/cdc_acm.rs b/examples/nrf/src/bin/usb/cdc_acm.rs
index 4be35fd3f..4b4925937 100644
--- a/examples/nrf/src/bin/usb/cdc_acm.rs
+++ b/examples/nrf/src/bin/usb/cdc_acm.rs
@@ -1,4 +1,4 @@
1use core::cell::{Cell, UnsafeCell}; 1use core::cell::Cell;
2use core::mem::{self, MaybeUninit}; 2use core::mem::{self, MaybeUninit};
3use core::sync::atomic::{AtomicBool, Ordering}; 3use core::sync::atomic::{AtomicBool, Ordering};
4use defmt::info; 4use defmt::info;
@@ -27,14 +27,16 @@ const REQ_SET_LINE_CODING: u8 = 0x20;
27const REQ_GET_LINE_CODING: u8 = 0x21; 27const REQ_GET_LINE_CODING: u8 = 0x21;
28const REQ_SET_CONTROL_LINE_STATE: u8 = 0x22; 28const REQ_SET_CONTROL_LINE_STATE: u8 = 0x22;
29 29
30pub struct State { 30pub struct State<'a> {
31 control: MaybeUninit<Control>, 31 control: MaybeUninit<Control<'a>>,
32 shared: ControlShared,
32} 33}
33 34
34impl State { 35impl<'a> State<'a> {
35 pub fn new() -> Self { 36 pub fn new() -> Self {
36 Self { 37 Self {
37 control: MaybeUninit::uninit(), 38 control: MaybeUninit::uninit(),
39 shared: Default::default(),
38 } 40 }
39 } 41 }
40} 42}
@@ -62,8 +64,8 @@ pub struct CdcAcmClass<'d, D: Driver<'d>> {
62 control: &'d ControlShared, 64 control: &'d ControlShared,
63} 65}
64 66
65struct Control { 67struct Control<'a> {
66 shared: UnsafeCell<ControlShared>, 68 shared: &'a ControlShared,
67} 69}
68 70
69/// Shared data between Control and CdcAcmClass 71/// Shared data between Control and CdcAcmClass
@@ -73,12 +75,28 @@ struct ControlShared {
73 rts: AtomicBool, 75 rts: AtomicBool,
74} 76}
75 77
76impl Control { 78impl Default for ControlShared {
77 fn shared(&mut self) -> &ControlShared { 79 fn default() -> Self {
78 unsafe { &*(self.shared.get() as *const _) } 80 ControlShared {
81 dtr: AtomicBool::new(false),
82 rts: AtomicBool::new(false),
83 line_coding: CriticalSectionMutex::new(Cell::new(LineCoding {
84 stop_bits: StopBits::One,
85 data_bits: 8,
86 parity_type: ParityType::None,
87 data_rate: 8_000,
88 })),
89 }
90 }
91}
92
93impl<'a> Control<'a> {
94 fn shared(&mut self) -> &'a ControlShared {
95 self.shared
79 } 96 }
80} 97}
81impl ControlHandler for Control { 98
99impl<'a> ControlHandler for Control<'a> {
82 fn reset(&mut self) { 100 fn reset(&mut self) {
83 let shared = self.shared(); 101 let shared = self.shared();
84 shared.line_coding.lock(|x| x.set(LineCoding::default())); 102 shared.line_coding.lock(|x| x.set(LineCoding::default()));
@@ -142,23 +160,14 @@ impl<'d, D: Driver<'d>> CdcAcmClass<'d, D> {
142 /// full-speed devices, max_packet_size has to be one of 8, 16, 32 or 64. 160 /// full-speed devices, max_packet_size has to be one of 8, 16, 32 or 64.
143 pub fn new( 161 pub fn new(
144 builder: &mut UsbDeviceBuilder<'d, D>, 162 builder: &mut UsbDeviceBuilder<'d, D>,
145 state: &'d mut State, 163 state: &'d mut State<'d>,
146 max_packet_size: u16, 164 max_packet_size: u16,
147 ) -> Self { 165 ) -> Self {
148 let control = state.control.write(Control { 166 let control = state.control.write(Control {
149 shared: UnsafeCell::new(ControlShared { 167 shared: &state.shared,
150 dtr: AtomicBool::new(false),
151 rts: AtomicBool::new(false),
152 line_coding: CriticalSectionMutex::new(Cell::new(LineCoding {
153 stop_bits: StopBits::One,
154 data_bits: 8,
155 parity_type: ParityType::None,
156 data_rate: 8_000,
157 })),
158 }),
159 }); 168 });
160 169
161 let control_shared = unsafe { &*(control.shared.get() as *const _) }; 170 let control_shared = &state.shared;
162 171
163 let comm_if = builder.alloc_interface_with_handler(control); 172 let comm_if = builder.alloc_interface_with_handler(control);
164 let comm_ep = builder.alloc_interrupt_endpoint_in(8, 255); 173 let comm_ep = builder.alloc_interrupt_endpoint_in(8, 255);