diff options
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/nrf/src/bin/usb/cdc_acm.rs | 53 |
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 @@ | |||
| 1 | use core::cell::{Cell, UnsafeCell}; | 1 | use core::cell::Cell; |
| 2 | use core::mem::{self, MaybeUninit}; | 2 | use core::mem::{self, MaybeUninit}; |
| 3 | use core::sync::atomic::{AtomicBool, Ordering}; | 3 | use core::sync::atomic::{AtomicBool, Ordering}; |
| 4 | use defmt::info; | 4 | use defmt::info; |
| @@ -27,14 +27,16 @@ const REQ_SET_LINE_CODING: u8 = 0x20; | |||
| 27 | const REQ_GET_LINE_CODING: u8 = 0x21; | 27 | const REQ_GET_LINE_CODING: u8 = 0x21; |
| 28 | const REQ_SET_CONTROL_LINE_STATE: u8 = 0x22; | 28 | const REQ_SET_CONTROL_LINE_STATE: u8 = 0x22; |
| 29 | 29 | ||
| 30 | pub struct State { | 30 | pub struct State<'a> { |
| 31 | control: MaybeUninit<Control>, | 31 | control: MaybeUninit<Control<'a>>, |
| 32 | shared: ControlShared, | ||
| 32 | } | 33 | } |
| 33 | 34 | ||
| 34 | impl State { | 35 | impl<'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 | ||
| 65 | struct Control { | 67 | struct 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 | ||
| 76 | impl Control { | 78 | impl 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 | |||
| 93 | impl<'a> Control<'a> { | ||
| 94 | fn shared(&mut self) -> &'a ControlShared { | ||
| 95 | self.shared | ||
| 79 | } | 96 | } |
| 80 | } | 97 | } |
| 81 | impl ControlHandler for Control { | 98 | |
| 99 | impl<'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); |
