1
1
//! USB_FS and OTG_FS device mode peripheral driver
2
2
//! Note that this currently only implements device mode
3
- //!
3
+ //!
4
4
//! <div class="warning">
5
5
//! There's a lot of TODOs and panics where things are not implemented
6
6
//! </div>
7
- //!
7
+ //!
8
8
//! List of things that is tested
9
9
//! Untested but expected to work items are noted as well
10
10
//!
11
11
//! Control Pipe:
12
- //! - [x] Recieve `SETUP` packet (Host -> Dev)
12
+ //! - [x] Recieve `SETUP` packet (Host -> Dev)
13
13
//! - [x] Send `IN` packet (Dev -> Host).
14
14
//! - [x] Recieve `OUT` packet (Host -> Dev).
15
- //!
16
- //! Other Endpoints:
15
+ //!
16
+ //! Other Endpoints:
17
17
//! - [x] Interrupt Out
18
18
//! - [x] Interrupt In
19
19
//! - [ ] Bulk Out (Expected to work but not tested)
@@ -31,9 +31,9 @@ use core::task::Poll;
31
31
32
32
use ch32_metapac:: otg:: vals:: { EpRxResponse , EpTxResponse , UsbToken } ;
33
33
use embassy_sync:: waitqueue:: AtomicWaker ;
34
- use embassy_usb_driver:: { self as driver , Direction , EndpointAddress , EndpointInfo , EndpointType , Event } ;
35
- use endpoint:: { ControlPipe , Endpoint , EndpointBufferAllocator , EndpointDataBuffer } ;
36
- use marker :: { Dir , In , Out } ;
34
+ use embassy_usb_driver:: { Direction , EndpointAddress , EndpointInfo , EndpointType , Event } ;
35
+ use endpoint:: { ControlPipe , Endpoint } ;
36
+ use crate :: usb :: { Dir , EndpointBufferAllocator , EndpointDataBuffer , In , Out } ;
37
37
38
38
use crate :: gpio:: { AFType , Speed } ;
39
39
use crate :: interrupt:: typelevel:: Interrupt ;
@@ -151,7 +151,7 @@ where
151
151
ep_type : EndpointType ,
152
152
max_packet_size : u16 ,
153
153
interval_ms : u8 ,
154
- ) -> Result < Endpoint < ' d , T , D > , driver :: EndpointAllocError > {
154
+ ) -> Result < Endpoint < ' d , T , D > , embassy_usb_driver :: EndpointAllocError > {
155
155
let ep_addr = self . alloc_ep_address ( ) ;
156
156
let data = self . allocator . alloc_endpoint ( max_packet_size) ?;
157
157
@@ -168,7 +168,7 @@ where
168
168
}
169
169
}
170
170
171
- impl < ' d , T : Instance , const NR_EP : usize > driver :: Driver < ' d > for Driver < ' d , T , NR_EP > {
171
+ impl < ' d , T : Instance , const NR_EP : usize > embassy_usb_driver :: Driver < ' d > for Driver < ' d , T , NR_EP > {
172
172
type EndpointOut = Endpoint < ' d , T , Out > ;
173
173
174
174
type EndpointIn = Endpoint < ' d , T , In > ;
@@ -179,19 +179,19 @@ impl<'d, T: Instance, const NR_EP: usize> driver::Driver<'d> for Driver<'d, T, N
179
179
180
180
fn alloc_endpoint_out (
181
181
& mut self ,
182
- ep_type : driver :: EndpointType ,
182
+ ep_type : embassy_usb_driver :: EndpointType ,
183
183
max_packet_size : u16 ,
184
184
interval_ms : u8 ,
185
- ) -> Result < Self :: EndpointOut , driver :: EndpointAllocError > {
185
+ ) -> Result < Self :: EndpointOut , embassy_usb_driver :: EndpointAllocError > {
186
186
self . alloc_endpoint :: < Out > ( ep_type, max_packet_size, interval_ms)
187
187
}
188
188
189
189
fn alloc_endpoint_in (
190
190
& mut self ,
191
- ep_type : driver :: EndpointType ,
191
+ ep_type : embassy_usb_driver :: EndpointType ,
192
192
max_packet_size : u16 ,
193
193
interval_ms : u8 ,
194
- ) -> Result < Self :: EndpointIn , driver :: EndpointAllocError > {
194
+ ) -> Result < Self :: EndpointIn , embassy_usb_driver :: EndpointAllocError > {
195
195
self . alloc_endpoint :: < In > ( ep_type, max_packet_size, interval_ms)
196
196
}
197
197
@@ -229,23 +229,11 @@ impl<'d, T: Instance, const NR_EP: usize> driver::Driver<'d> for Driver<'d, T, N
229
229
} ) ;
230
230
231
231
let ep0_buf = self . allocator . alloc_endpoint ( control_max_packet_size) . unwrap ( ) ;
232
-
233
232
regs. uep_dma ( 0 ) . write_value ( ep0_buf. addr ( ) as u32 ) ;
234
233
235
234
regs. uep_rx_ctrl ( 0 ) . write ( |w| w. set_mask_r_res ( EpRxResponse :: ACK ) ) ;
236
235
regs. uep_tx_ctrl ( 0 ) . write ( |w| w. set_mask_t_res ( EpTxResponse :: NAK ) ) ;
237
236
238
- // Hookup the bus on start?
239
- regs. udev_ctrl ( ) . write ( |w| {
240
- // pd is for HOST
241
- w. set_pd_dis ( true ) ;
242
- w. set_port_en ( true ) ;
243
- } ) ;
244
-
245
- // Initialize the bus so that it signals that power is available
246
- // usbd.rs does BUS_WAKER.wake(), but it doesn't seem necessary
247
- BUS_WAKER . wake ( ) ;
248
-
249
237
critical_section:: with ( |_cs| {
250
238
T :: Interrupt :: unpend ( ) ;
251
239
unsafe {
@@ -284,18 +272,31 @@ impl<'d, T: Instance> Bus<'d, T> {
284
272
285
273
// Mark all other EPs as NAK
286
274
for i in 1 ..=7 {
275
+ use embassy_usb_driver:: Bus ;
287
276
regs. uep_rx_ctrl ( i) . write ( |v| v. set_mask_r_res ( EpRxResponse :: NAK ) ) ;
288
277
regs. uep_tx_ctrl ( i) . write ( |v| v. set_mask_t_res ( EpTxResponse :: NAK ) ) ;
278
+ self . endpoint_set_enabled ( EndpointAddress :: from_parts ( i, Direction :: In ) , false ) ;
279
+ self . endpoint_set_enabled ( EndpointAddress :: from_parts ( i, Direction :: Out ) , false ) ;
289
280
}
290
281
}
291
282
}
292
283
293
- impl < ' d , T > driver :: Bus for Bus < ' d , T >
284
+ impl < ' d , T > embassy_usb_driver :: Bus for Bus < ' d , T >
294
285
where
295
286
T : Instance ,
296
287
{
297
288
async fn enable ( & mut self ) {
298
- trace ! ( "enable" )
289
+ // Do a bus reset on "enable"
290
+ let regs = T :: regs ( ) ;
291
+
292
+ // Enable the port
293
+ regs. udev_ctrl ( ) . write ( |w| {
294
+ // Pull Down needs to be disabled because that is for HOST
295
+ w. set_pd_dis ( true ) ;
296
+ w. set_port_en ( true ) ;
297
+ } ) ;
298
+
299
+ self . bus_reset ( ) ;
299
300
}
300
301
301
302
async fn disable ( & mut self ) {
0 commit comments