mpfs-hal 0.3.0

Hardware Abstraction Layer for PolarFire SoC
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
use core::future::poll_fn;
use core::task::{Poll, Waker};
use embassy_usb_driver::{
    Direction, Endpoint, EndpointAddress, EndpointAllocError, EndpointError, EndpointInfo,
    EndpointType, Event,
};

use crate::{pac, Peripheral};

// This should not be raised unless related constants in Endpoint Common are ammended
const NUM_ENDPOINTS: usize = 16;

#[cfg(feature = "alloc")]
mod ep {
    extern crate alloc;

    use super::{default_ep, EndpointState, MAX_FIFO_SIZE};
    use crate::pac;
    use aligned::{Aligned, A4};
    use alloc::boxed::Box;
    use core::task::Waker;

    #[derive(Debug)]
    pub struct EndpointController {
        pub ep: pac::mss_usb_ep_t,
        pub waker: Option<Waker>,
        pub state: EndpointState,
        pub buffer: Option<Box<Aligned<A4, [u8; MAX_FIFO_SIZE as usize]>>>,
    }

    impl EndpointController {
        pub fn default() -> Self {
            EndpointController {
                ep: default_ep(),
                waker: None,
                state: EndpointState::Disabled,
                buffer: None,
            }
        }
        pub fn buffer_addr(&mut self) -> &mut [u8] {
            if self.buffer.is_none() {
                self.buffer = Some(Box::new(Aligned::<A4, _>([0; MAX_FIFO_SIZE as usize])));
            }
            self.buffer.as_mut().unwrap().as_mut_slice()
        }
    }
}

#[cfg(not(feature = "alloc"))]
mod ep {
    use super::{default_ep, EndpointState};
    use crate::pac;
    use core::task::Waker;

    #[derive(Debug)]
    pub struct EndpointController {
        pub ep: pac::mss_usb_ep_t,
        pub waker: Option<Waker>,
        pub state: EndpointState,
    }

    impl EndpointController {
        pub fn default() -> Self {
            EndpointController {
                ep: default_ep(),
                waker: None,
                state: EndpointState::Disabled,
            }
        }
        pub fn buffer_addr(&mut self) -> &mut [u8] {
            panic!("USB buffers that aren't 4-byte aligned are not supported without alloc");
        }
    }
}

use ep::*;

#[derive(Debug, Copy, Clone, PartialEq)]
enum EndpointState {
    Idle,
    Rx,
    Tx,
    TxLast,
    TxReadyForNext,
    Setup,
    RxComplete(usize),
    TxComplete,
    Disabled,
}

impl Default for EndpointState {
    fn default() -> Self {
        EndpointState::Disabled
    }
}

// Endpoints are named relative to the host, so In is a device TX and Out is a device RX
// +1 for the control endpoint at index 0
static mut EP_IN_CONTROLLER: [Option<EndpointController>; NUM_ENDPOINTS + 1] =
    [const { None }; NUM_ENDPOINTS + 1];
static mut EP_OUT_CONTROLLER: [Option<EndpointController>; NUM_ENDPOINTS + 1] =
    [const { None }; NUM_ENDPOINTS + 1];

#[derive(Debug, Clone, Copy, PartialEq)]
enum UsbSpeed {
    Full, // 12Mbps
    High, // 480Mbps
}

impl UsbSpeed {
    fn value(&self) -> pac::mss_usb_device_speed_t {
        match self {
            UsbSpeed::Full => pac::mss_usb_device_speed_t_MSS_USB_DEVICE_FS,
            UsbSpeed::High => pac::mss_usb_device_speed_t_MSS_USB_DEVICE_HS,
        }
    }
}

impl Default for UsbSpeed {
    fn default() -> Self {
        UsbSpeed::High
    }
}

//------------------------------------------------------
// Driver

#[derive(Default)]
struct EndpointDetails {
    used: bool,
    fifo_addr: u16,
    fifo_size: u16,
}

#[derive(Default)]
pub struct UsbDriver<'a> {
    speed: UsbSpeed,
    phantom: core::marker::PhantomData<&'a ()>,
    // Endpoint number 1 maps to index 0
    in_endpoints_allocated: [EndpointDetails; NUM_ENDPOINTS],
    out_endpoints_allocated: [EndpointDetails; NUM_ENDPOINTS],
}

static mut DRIVER_TAKEN: bool = false;

impl<'a> Peripheral for UsbDriver<'a> {
    fn take() -> Option<Self> {
        critical_section::with(|_| unsafe {
            if DRIVER_TAKEN {
                None
            } else {
                DRIVER_TAKEN = true;
                Some(Self::default())
            }
        })
    }

    unsafe fn steal() -> Self {
        Self::default()
    }
}

impl<'a> UsbDriver<'a> {
    pub fn set_full_speed(&mut self) {
        self.speed = UsbSpeed::Full;
    }

    fn init(&self, control_packet_size: u16) {
        unsafe {
            // Unlike the other peripherals, the USB peripheral needs to be explicitly
            // turned off and on, or else DMA won't work.
            pac::mss_config_clk_rst(
                pac::mss_peripherals__MSS_PERIPH_USB,
                pac::MPFS_HAL_FIRST_HART as u8,
                pac::PERIPH_RESET_STATE__PERIPHERAL_OFF,
            );
            pac::mss_config_clk_rst(
                pac::mss_peripherals__MSS_PERIPH_USB,
                pac::MPFS_HAL_FIRST_HART as u8,
                pac::PERIPH_RESET_STATE__PERIPHERAL_ON,
            );
            pac::init_usb_dma_upper_address();
            pac::PLIC_SetPriority(pac::PLIC_IRQn_Type_PLIC_USB_DMA_INT_OFFSET, 2);
            pac::PLIC_SetPriority(pac::PLIC_IRQn_Type_PLIC_USB_MC_INT_OFFSET, 2);
            pac::MSS_USBD_CIF_init(self.speed.value());
            pac::MSS_USBD_CIF_cep_configure();
            EP_IN_CONTROLLER[0] = Some({
                let mut ep = EndpointController::default();
                ep.ep.max_pkt_size = control_packet_size;
                ep.state = EndpointState::Idle;
                ep
            });
            EP_OUT_CONTROLLER[0] = Some({
                let mut ep = EndpointController::default();
                ep.ep.max_pkt_size = control_packet_size;
                ep.state = EndpointState::Idle;
                ep
            });
            // MSS_USBD_CIF_dev_connect
            (*pac::USB).POWER |= pac::POWER_REG_SOFT_CONN_MASK as u8;
        }
    }
}

impl<'a> embassy_usb_driver::Driver<'a> for UsbDriver<'a> {
    type EndpointOut = EndpointOut<'a>;
    type EndpointIn = EndpointIn<'a>;
    type ControlPipe = ControlPipe<'a>;
    type Bus = UsbBus<'a>;

    fn alloc_endpoint_out(
        &mut self,
        endpoint_type: EndpointType,
        max_packet_size: u16,
        interval_ms: u8,
    ) -> Result<Self::EndpointOut, EndpointAllocError> {
        log::trace!(
            "UsbDriver::alloc_endpoint_out: {:?}, {:?}",
            endpoint_type,
            max_packet_size
        );
        if max_packet_size > MAX_FIFO_SIZE {
            return Err(EndpointAllocError);
        }
        // FIFO size must be a power of 2
        let mut fifo_size = max_packet_size.next_power_of_two();
        if fifo_size < 1024 {
            // If we're using a small enough packet size, we increase the FIFO size
            // to allow for double packet buffering
            fifo_size = (fifo_size + 1).next_power_of_two();
        }
        if let Some(i) = self.out_endpoints_allocated.iter().position(|x| !x.used) {
            let fifo_addr = if i == 0 {
                OUT_ENDPOINTS_START_ADDR
            } else {
                self.out_endpoints_allocated[i - 1].fifo_addr
                    + self.out_endpoints_allocated[i - 1].fifo_size
            };
            if fifo_addr + fifo_size > IN_ENDPOINTS_START_ADDR {
                log::error!(
                    "FIFO address is out of bounds for out endpoint {:?} with size {:?}",
                    i,
                    max_packet_size
                );
                return Err(EndpointAllocError);
            }
            self.out_endpoints_allocated[i] = EndpointDetails {
                used: true,
                fifo_addr,
                fifo_size,
            };
            configure_endpoint_controller(
                Direction::Out,
                i + 1,
                endpoint_type,
                max_packet_size,
                fifo_addr,
                fifo_size,
                self.speed,
            );
            Ok(EndpointOut {
                phantom: core::marker::PhantomData,
                info: EndpointInfo {
                    addr: EndpointAddress::from_parts(i + 1, Direction::Out),
                    max_packet_size: max_packet_size,
                    interval_ms: interval_ms,
                    ep_type: endpoint_type,
                },
            })
        } else {
            Err(EndpointAllocError)
        }
    }

    fn alloc_endpoint_in(
        &mut self,
        endpoint_type: EndpointType,
        max_packet_size: u16,
        interval_ms: u8,
    ) -> Result<Self::EndpointIn, EndpointAllocError> {
        log::trace!(
            "UsbDriver::alloc_endpoint_in: {:?}, {:?}",
            endpoint_type,
            max_packet_size
        );
        if max_packet_size > MAX_FIFO_SIZE {
            log::error!("Max packet size is greater than the allowed max packet size for this transfer type {} expected for {:?}, got {}", MAX_FIFO_SIZE, endpoint_type, max_packet_size);
            return Err(EndpointAllocError);
        }
        // FIFO size must be a power of 2
        let mut fifo_size = max_packet_size.next_power_of_two();
        if fifo_size < 1024 {
            // If we're using a small enough packet size, we increase the FIFO size
            // to allow for double packet buffering
            fifo_size = (fifo_size + 1).next_power_of_two();
        }
        if let Some(i) = self.in_endpoints_allocated.iter().position(|x| !x.used) {
            let fifo_addr = if i == 0 {
                IN_ENDPOINTS_START_ADDR
            } else {
                self.in_endpoints_allocated[i - 1].fifo_addr
                    + self.in_endpoints_allocated[i - 1].fifo_size
            };
            if fifo_addr.checked_add(fifo_size).is_none() {
                log::error!(
                    "FIFO address is out of bounds for in endpoint {:?} with size {:?}",
                    i,
                    max_packet_size
                );
                return Err(EndpointAllocError);
            }
            self.in_endpoints_allocated[i] = EndpointDetails {
                used: true,
                fifo_addr: fifo_addr,
                fifo_size: fifo_size,
            };
            configure_endpoint_controller(
                Direction::In,
                i + 1,
                endpoint_type,
                max_packet_size,
                fifo_addr,
                fifo_size,
                self.speed,
            );
            Ok(EndpointIn {
                phantom: core::marker::PhantomData,
                info: EndpointInfo {
                    addr: EndpointAddress::from_parts(i + 1, Direction::In),
                    max_packet_size: max_packet_size,
                    interval_ms: interval_ms,
                    ep_type: endpoint_type,
                },
            })
        } else {
            Err(EndpointAllocError)
        }
    }

    fn start(self, control_packet_size: u16) -> (Self::Bus, Self::ControlPipe) {
        self.init(control_packet_size);
        for (i, ep) in self.in_endpoints_allocated.iter().enumerate() {
            if ep.used {
                configure_endpoint(Direction::In, i + 1);
            } else {
                break;
            }
        }
        for (i, ep) in self.out_endpoints_allocated.iter().enumerate() {
            if ep.used {
                configure_endpoint(Direction::Out, i + 1);
            } else {
                break;
            }
        }
        log::debug!("USB initialized");
        (
            UsbBus {
                phantom: core::marker::PhantomData,
            },
            ControlPipe {
                phantom: core::marker::PhantomData,
                max_packet_size: control_packet_size as usize,
            },
        )
    }
}

//------------------------------------------------------
// EndpointOut

// Host -> Device
pub struct EndpointOut<'a> {
    phantom: core::marker::PhantomData<&'a ()>,
    info: EndpointInfo,
}

impl<'a> embassy_usb_driver::EndpointOut for EndpointOut<'a> {
    async fn read(&mut self, data: &mut [u8]) -> Result<usize, EndpointError> {
        let index = self.info.addr.index();
        log::trace!("USB EndpointOut::read {}", index);
        self.wait_enabled().await;
        let mut aligned_buffer = data.as_ptr();

        unsafe {
            if aligned_buffer.align_offset(4) != 0 {
                let e = EP_OUT_CONTROLLER[index].as_mut().unwrap();
                log::warn!("EndpointIn:{} data should be 32-bit aligned", index);
                aligned_buffer = e.buffer_addr()[..data.len()].as_ptr();
            }
        }

        unsafe {
            let e = EP_OUT_CONTROLLER[index].as_mut().unwrap();
            critical_section::with(|_| {
                e.state = EndpointState::Rx;
                e.ep.buf_addr = aligned_buffer as *mut u8;
                e.ep.xfr_length = data.len() as u32;
            });
            pac::MSS_USBD_CIF_rx_ep_read_prepare(&mut e.ep);
        }

        let read_size = poll_fn(|cx| {
            critical_section::with(|_| unsafe {
                if let EndpointState::RxComplete(i) =
                    EP_OUT_CONTROLLER[index].as_ref().unwrap().state
                {
                    Poll::Ready(i)
                } else {
                    EP_OUT_CONTROLLER[index].as_mut().unwrap().waker = Some(cx.waker().clone());
                    Poll::Pending
                }
            })
        })
        .await;

        if data.as_ptr() != aligned_buffer {
            unsafe {
                let e = EP_OUT_CONTROLLER[index].as_mut().unwrap();
                data.copy_from_slice(&e.buffer_addr()[..data.len()]);
            }
        }

        Ok(read_size)
    }
}

impl<'a> embassy_usb_driver::Endpoint for EndpointOut<'a> {
    fn info(&self) -> &EndpointInfo {
        &self.info
    }

    async fn wait_enabled(&mut self) {
        log::trace!(
            "USB EndpointOut::wait_enabled {} WAITING",
            self.info.addr.index()
        );
        let index = self.info.addr.index();
        poll_fn(|cx| {
            critical_section::with(|_| unsafe {
                if EP_OUT_CONTROLLER[index].as_ref().unwrap().state != EndpointState::Disabled {
                    Poll::Ready(())
                } else {
                    EP_OUT_CONTROLLER[index].as_mut().unwrap().waker = Some(cx.waker().clone());
                    Poll::Pending
                }
            })
        })
        .await;
        log::trace!(
            "USB EndpointOut::wait_enabled {} OK",
            self.info.addr.index()
        );
    }
}

//------------------------------------------------------
// EndpointIn

// Device -> Host
pub struct EndpointIn<'a> {
    phantom: core::marker::PhantomData<&'a ()>,
    info: EndpointInfo,
}

impl<'a> embassy_usb_driver::EndpointIn for EndpointIn<'a> {
    async fn write(&mut self, data: &[u8]) -> Result<(), EndpointError> {
        let index = self.info.addr.index();
        log::trace!("USB EndpointIn::write {} : {:?}", index, data);
        self.wait_enabled().await;
        let mut aligned_buffer = data;

        #[allow(static_mut_refs)]
        unsafe {
            // We are reading values that won't be changed, so we can do this without a lock

            if data.as_ptr().align_offset(4) != 0 {
                let e = EP_IN_CONTROLLER[index].as_mut().unwrap();
                log::warn!("EndpointIn:{} data should be 32-bit aligned", index);
                e.buffer_addr()[..data.len()].copy_from_slice(data);
                aligned_buffer = &e.buffer_addr()[..data.len()];
            }
            critical_section::with(|_| {
                EP_IN_CONTROLLER[index].as_mut().unwrap().state = EndpointState::Tx;
            });
            let e = EP_IN_CONTROLLER[index].as_mut().unwrap();
            pac::MSS_USB_CIF_ep_write_pkt(
                e.ep.num,
                aligned_buffer.as_ptr() as *mut u8,
                e.ep.dma_enable,
                e.ep.dma_channel,
                e.ep.xfr_type,
                aligned_buffer.len() as u32,
                aligned_buffer.len() as u32,
            );
        }

        poll_fn(move |cx| {
            critical_section::with(|_| unsafe {
                if EP_IN_CONTROLLER[index].as_mut().unwrap().state == EndpointState::TxComplete {
                    EP_IN_CONTROLLER[index].as_mut().unwrap().state = EndpointState::Idle;
                    Poll::Ready(())
                } else {
                    EP_IN_CONTROLLER[index].as_mut().unwrap().waker = Some(cx.waker().clone());
                    Poll::Pending
                }
            })
        })
        .await;
        log::trace!("USB EndpointIn::write {} OK", index);
        Ok(())
    }
}

impl<'a> embassy_usb_driver::Endpoint for EndpointIn<'a> {
    fn info(&self) -> &EndpointInfo {
        &self.info
    }

    async fn wait_enabled(&mut self) {
        log::trace!(
            "USB EndpointIn::wait_enabled {} WAITING",
            self.info.addr.index()
        );
        let index = self.info.addr.index();
        poll_fn(|cx| {
            critical_section::with(|_| unsafe {
                if EP_IN_CONTROLLER[index].as_ref().unwrap().state != EndpointState::Disabled {
                    Poll::Ready(())
                } else {
                    EP_IN_CONTROLLER[index].as_mut().unwrap().waker = Some(cx.waker().clone());
                    Poll::Pending
                }
            })
        })
        .await;
        log::trace!("USB EndpointIn::wait_enabled {} OK", self.info.addr.index());
    }
}

//------------------------------------------------------
// Endpoint Common
//
// Endpoints need to have a FIFO assigned to an address in the MSS USB internal RAM.
// The RAM is 65k (0xFFFF) bytes, so using 32 endpoints (16 in + 16 out)
// allows us to give 2048 bytes to each endpoint if we devide it equally. We will allow
// the user to configure the endpoint with a max packet size of up to 4096 bytes, however
// which means that not all endpoints will be able to have the maximum packet size.
// Further, we will devide the FIFO into 2 parts, with 0x0000 being the start address for
// the out endpoints and 0x8000 being the start address for the in endpoints.
//
// Additionally, we have 4 DMA channels available, and they can each only be mapped to
// one endpoint. Because of this we will configure channels 1 and 2 (for both in and out)
// to use DMA. DMA channel 1, 2 will be used for Out endpoints and DMA channel 3, 4 will
// be used for In endpoints.
//
// Reference: mss_usb_device.h

const MAX_FIFO_SIZE: u16 = 4096;
const OUT_ENDPOINTS_START_ADDR: u16 = 0x0000;
const IN_ENDPOINTS_START_ADDR: u16 = 0x8000;

fn endpoint_to_dma_channel(endpoint: usize, direction: Direction) -> pac::mss_usb_dma_channel_t {
    match (endpoint, direction) {
        (1, Direction::In) => pac::mss_usb_dma_channel_t_MSS_USB_DMA_CHANNEL1,
        (2, Direction::In) => pac::mss_usb_dma_channel_t_MSS_USB_DMA_CHANNEL2,
        (1, Direction::Out) => pac::mss_usb_dma_channel_t_MSS_USB_DMA_CHANNEL3,
        (2, Direction::Out) => pac::mss_usb_dma_channel_t_MSS_USB_DMA_CHANNEL4,
        _ => pac::mss_usb_dma_channel_t_MSS_USB_DMA_CHANNEL_NA,
    }
}

fn configure_endpoint_controller(
    direction: Direction,
    endpoint: usize,
    endpoint_type: EndpointType,
    max_packet_size: u16,
    fifo_addr: u16,
    fifo_size: u16,
    speed: UsbSpeed,
) {
    // Get the standard max packet size based on speed and transfer type
    let std_max_pkt_sz = if speed == UsbSpeed::High {
        match endpoint_type {
            EndpointType::Bulk => pac::USB_HS_BULK_MAX_PKT_SIZE,
            EndpointType::Interrupt => pac::USB_HS_INTERRUPT_MAX_PKT_SIZE,
            EndpointType::Isochronous => pac::USB_HS_ISO_MAX_PKT_SIZE,
            EndpointType::Control => pac::USB_HS_BULK_MAX_PKT_SIZE, // Control uses same as bulk
        }
    } else {
        match endpoint_type {
            EndpointType::Bulk => pac::USB_FS_BULK_MAX_PKT_SIZE,
            EndpointType::Interrupt => pac::USB_FS_INTERRUPT_MAX_PKT_SIZE,
            EndpointType::Isochronous => pac::USB_FS_ISO_MAX_PKT_SIZE,
            EndpointType::Control => pac::USB_FS_BULK_MAX_PKT_SIZE,
        }
    };

    if max_packet_size > std_max_pkt_sz as u16 {
        panic!("Max packet size is greater than the allowed max packet size for this transfer type {} expected for {:?}, got {}", std_max_pkt_sz, endpoint_type, max_packet_size);
    }

    let dma_channel = endpoint_to_dma_channel(endpoint, direction);
    let dma_enable = dma_channel != pac::mss_usb_dma_channel_t_MSS_USB_DMA_CHANNEL_NA;

    // Configure double packet buffering if there's enough FIFO space reserved
    let dpb_enable = max_packet_size * 2 <= fifo_size;

    let ep = pac::mss_usb_ep_t {
        num: endpoint as u32,
        xfr_type: transfer_type(endpoint_type),
        max_pkt_size: max_packet_size,
        fifo_size,
        fifo_addr,
        dma_enable: dma_enable as u8,
        dpb_enable: dpb_enable as u8,
        dma_channel,
        ..default_ep()
    };

    unsafe {
        // We know nothing else will be using this endpoint yet (it hasn't been allocated), so we can safely do this without a lock
        if direction == Direction::Out {
            EP_OUT_CONTROLLER[endpoint] = Some(EndpointController {
                ep,
                ..EndpointController::default()
            });
        } else {
            EP_IN_CONTROLLER[endpoint] = Some(EndpointController {
                ep,
                ..EndpointController::default()
            });
        }
    }
}

fn configure_endpoint(direction: Direction, endpoint: usize) {
    critical_section::with(|_| unsafe {
        if direction == Direction::Out {
            if let Some(ep) = EP_OUT_CONTROLLER[endpoint].as_mut() {
                // log::debug!("Configuring OUT endpoint {:?}", ep);
                pac::MSS_USBD_CIF_rx_ep_configure(&mut ep.ep);
                ep.state = EndpointState::Disabled;
            }
        } else {
            if let Some(ep) = EP_IN_CONTROLLER[endpoint].as_mut() {
                // log::debug!("Configuring IN endpoint {:?}", ep);
                pac::MSS_USBD_CIF_tx_ep_configure(&mut ep.ep);
                ep.state = EndpointState::Disabled;
            }
        }
    });
}

fn transfer_type(endpoint_type: EndpointType) -> pac::mss_usb_xfr_type_t {
    match endpoint_type {
        EndpointType::Bulk => pac::mss_usb_xfr_type_t_MSS_USB_XFR_BULK,
        EndpointType::Interrupt => pac::mss_usb_xfr_type_t_MSS_USB_XFR_INTERRUPT,
        EndpointType::Isochronous => pac::mss_usb_xfr_type_t_MSS_USB_XFR_ISO,
        EndpointType::Control => pac::mss_usb_xfr_type_t_MSS_USB_XFR_CONTROL,
    }
}

//------------------------------------------------------
// ControlPipe
pub struct ControlPipe<'a> {
    phantom: core::marker::PhantomData<&'a ()>,
    max_packet_size: usize,
}

impl<'a> embassy_usb_driver::ControlPipe for ControlPipe<'a> {
    fn max_packet_size(&self) -> usize {
        self.max_packet_size
    }

    async fn setup(&mut self) -> [u8; 8] {
        log::trace!("USB ControlPipe::setup");
        loop {
            poll_fn(move |cx| {
                critical_section::with(|_| unsafe {
                    if EP_OUT_CONTROLLER[0].as_mut().unwrap().state == EndpointState::Setup {
                        EP_OUT_CONTROLLER[0].as_mut().unwrap().state = EndpointState::Idle;
                        Poll::Ready(())
                    } else {
                        EP_OUT_CONTROLLER[0].as_mut().unwrap().waker = Some(cx.waker().clone());
                        Poll::Pending
                    }
                })
            })
            .await;

            let mut setup_packet = [0; 8];
            read_control_packet(&mut setup_packet);
            // For some reason, sometimes the packet wasn't actually ready.
            // If we loop here and wait for the next interrupt, we seem to get the right packet.
            if setup_packet != [0; 8] {
                log::trace!("USB ControlPipe::setup packet: {:x?}", setup_packet);
                return setup_packet;
            } else {
                log::warn!("Got empty setup packet");
            }
        }
    }

    // Host -> Device
    // If a setup packet is received while this is waiting, this must return `EndpointError::Disabled`, but this will never be able to happen
    async fn data_out(
        &mut self,
        buf: &mut [u8],
        _first: bool,
        _last: bool,
    ) -> Result<usize, EndpointError> {
        // It does not seem like we need to do anything special when last is true

        critical_section::with(|_| unsafe {
            EP_OUT_CONTROLLER[0].as_mut().unwrap().state = EndpointState::Rx;
        });
        read_control_packet(buf);

        let read_size = poll_fn(move |cx| {
            critical_section::with(|_| unsafe {
                if let EndpointState::RxComplete(i) = EP_OUT_CONTROLLER[0].as_mut().unwrap().state {
                    EP_OUT_CONTROLLER[0].as_mut().unwrap().state = EndpointState::Idle;
                    Poll::Ready(i)
                } else {
                    EP_OUT_CONTROLLER[0].as_mut().unwrap().waker = Some(cx.waker().clone());
                    Poll::Pending
                }
            })
        })
        .await;
        Ok(read_size)
    }

    // Device -> Host
    // If a setup packet is received while this is waiting, this must return `EndpointError::Disabled`
    async fn data_in(&mut self, data: &[u8], first: bool, last: bool) -> Result<(), EndpointError> {
        log::trace!(
            "USB ControlPipe::data_in: {:x?}, {:?}, {:?}",
            data,
            first,
            last
        );

        let mut aligned_buffer = data;

        #[allow(static_mut_refs)]
        unsafe {
            if data.as_ptr().align_offset(4) != 0 {
                log::warn!("Control endpoint data should be 32-bit aligned");
                let e = EP_IN_CONTROLLER[0].as_mut().unwrap();
                e.buffer_addr()[..data.len()].copy_from_slice(data);
                aligned_buffer = &e.buffer_addr()[..data.len()];
            }
            let ep = EP_IN_CONTROLLER[0].as_mut().unwrap();
            ep.ep.buf_addr = aligned_buffer.as_ptr() as *mut u8;
            ep.ep.txn_length = aligned_buffer.len() as u32;
            // When last is true, we send a zero length packet after the transfer
            // This means that xfr_count + txn_length >= xfr_length
            // Otherwise, xfr_count + txn_length < xfr_length
            // The specifics of these numbers are otherwise not important
            ep.ep.xfr_count = 0;
            ep.ep.xfr_length = if last {
                0
            } else {
                1 + aligned_buffer.len() as u32
            };
            ep.state = if last {
                EndpointState::TxLast
            } else {
                EndpointState::Tx
            };
            pac::MSS_USBD_CIF_cep_write_pkt(&mut ep.ep);
        }
        poll_fn(move |cx| {
            critical_section::with(|_| unsafe {
                let state = &mut EP_IN_CONTROLLER[0].as_mut().unwrap().state;
                if *state == EndpointState::TxComplete {
                    *state = EndpointState::Idle;
                    Poll::Ready(Ok(()))
                } else if *state == EndpointState::TxReadyForNext {
                    Poll::Ready(Ok(()))
                } else if *state == EndpointState::Setup {
                    log::warn!(
                        "Control endpoint setup packet received while waiting for tx complete"
                    );
                    Poll::Ready(Err(EndpointError::Disabled))
                } else {
                    EP_IN_CONTROLLER[0].as_mut().unwrap().waker = Some(cx.waker().clone());
                    Poll::Pending
                }
            })
        })
        .await?;

        Ok(())
    }

    async fn accept(&mut self) {
        unsafe {
            // MSS_USBD_CIF_cep_end_zdr()
            (*pac::USB).INDEXED_CSR.DEVICE_EP0.CSR0 =
                (pac::CSR0L_DEV_SERVICED_RX_PKT_RDY_MASK | pac::CSR0L_DEV_DATA_END_MASK) as u16;
        }
        log::trace!("USB ControlPipe::accept");
    }

    async fn reject(&mut self) {
        unsafe {
            (*pac::USB).INDEXED_CSR.DEVICE_EP0.CSR0 =
                (pac::CSR0L_DEV_SEND_STALL_MASK | pac::CSR0L_DEV_SERVICED_RX_PKT_RDY_MASK) as u16;
        }
        log::trace!("USB ControlPipe::reject");
    }

    async fn accept_set_address(&mut self, addr: u8) {
        self.accept().await;
        // Unless we wait here, the zero length packet is not sent
        // See: mss_usb_device.c:mss_usbd_cep_setup_cb
        for _ in 0..5000 {
            core::hint::spin_loop();
        }

        unsafe {
            ((*pac::USB).FADDR) = addr;
        }
        log::trace!("USB ControlPipe::accept_set_address: {}", addr);
    }
}

fn read_control_packet(buf: &mut [u8]) {
    unsafe {
        let ep = EP_OUT_CONTROLLER[0].as_mut().unwrap();
        ep.ep.buf_addr = buf.as_mut_ptr() as *mut u8;
        ep.ep.txn_length = buf.len() as u32;
        ep.ep.xfr_length = buf.len() as u32;
        pac::MSS_USBD_CIF_cep_read_pkt(&mut ep.ep);
    }
}

const fn default_ep() -> pac::mss_usb_ep_t {
    pac::mss_usb_ep_t {
        // Important for configuration
        num: 0,
        xfr_type: pac::mss_usb_xfr_type_t_MSS_USB_XFR_CONTROL,
        max_pkt_size: 0,
        fifo_size: 0,
        fifo_addr: 0,
        dma_enable: 0,
        dpb_enable: 0,
        dma_channel: pac::mss_usb_dma_channel_t_MSS_USB_DMA_CHANNEL_NA,

        // Used for transfer
        buf_addr: core::ptr::null_mut(),
        txn_length: 0,
        xfr_length: 0,
        txn_count: 0,
        xfr_count: 0,
        state: pac::mss_usb_ep_state_t_MSS_USB_EP_VALID,

        // Used for configuration by the high-level driver, but not by this
        add_zlp: 0,     // Used by the high-level driver that we don't use
        num_usb_pkt: 1, // This must always be 1, according to mss_usb_device.h

        // Unused?
        cep_cmd_addr: core::ptr::null_mut(),
        cep_data_dir: 0,
        disable_ping: 0,
        interval: 0,
        stall: 0,
        req_pkt_n: 0,
        tdev_idx: 0,
    }
}

//------------------------------------------------------
// Bus

struct BusEvent {
    reset: bool,
    suspend: bool,
    resume: bool,
    disconnect: bool,
}

static mut BUS_EVENT_WAKER: Option<Waker> = None;
static mut BUS_DISCONNECT_WAKER: Option<Waker> = None;

static mut BUS_EVENT: BusEvent = BusEvent {
    reset: false,
    suspend: false,
    resume: false,
    disconnect: false,
};

pub struct UsbBus<'a> {
    phantom: core::marker::PhantomData<&'a ()>,
}

impl<'a> UsbBus<'a> {
    pub async fn await_disconnect(&self) {
        poll_fn(move |cx| {
            critical_section::with(|_| unsafe {
                BUS_DISCONNECT_WAKER = Some(cx.waker().clone());
                if BUS_EVENT.disconnect {
                    BUS_EVENT.disconnect = false;
                    Poll::Ready(())
                } else {
                    Poll::Pending
                }
            })
        })
        .await
    }
}

impl<'a> embassy_usb_driver::Bus for UsbBus<'a> {
    async fn poll(&mut self) -> Event {
        let ret = poll_fn(move |cx| {
            critical_section::with(|_| unsafe {
                BUS_EVENT_WAKER = Some(cx.waker().clone());
                // The order here matters since a host may send a suspend and reset
                // quick succession, and we want to handle the reset first
                if BUS_EVENT.suspend {
                    BUS_EVENT.suspend = false;
                    Poll::Ready(Event::Suspend)
                } else if BUS_EVENT.reset {
                    BUS_EVENT.reset = false;
                    Poll::Ready(Event::Reset)
                } else if BUS_EVENT.resume {
                    BUS_EVENT.resume = false;
                    Poll::Ready(Event::Resume)
                } else {
                    Poll::Pending
                }
            })
        })
        .await;
        log::debug!("USB poll: {:?}", ret);
        if ret == Event::Reset {
            unsafe {
                pac::MSS_USBD_CIF_cep_configure();
                critical_section::with(|_| {
                    EP_IN_CONTROLLER[0].as_mut().unwrap().state = EndpointState::Idle;
                    EP_OUT_CONTROLLER[0].as_mut().unwrap().state = EndpointState::Idle;
                });
                for ep in 1..=NUM_ENDPOINTS {
                    configure_endpoint(Direction::In, ep);
                }
                for ep in 1..=NUM_ENDPOINTS {
                    configure_endpoint(Direction::Out, ep);
                }
            }
        }
        ret
    }

    fn endpoint_set_enabled(&mut self, ep_addr: EndpointAddress, enabled: bool) {
        log::trace!(
            "USB Bus::endpoint_set_enabled: {:?}{}, {:?}",
            ep_addr.direction(),
            ep_addr.index(),
            enabled
        );
        let state = if enabled {
            EndpointState::Idle
        } else {
            EndpointState::Disabled
        };
        critical_section::with(|_| unsafe {
            if ep_addr.direction() == Direction::Out {
                EP_OUT_CONTROLLER[ep_addr.index()].as_mut().unwrap().state = state;
            } else {
                EP_IN_CONTROLLER[ep_addr.index()].as_mut().unwrap().state = state;
            }
        });
    }

    fn endpoint_set_stalled(&mut self, ep_addr: EndpointAddress, stalled: bool) {
        log::trace!(
            "USB Bus::endpoint_set_stalled: {:?}, {:?}",
            ep_addr,
            stalled
        );
        unsafe {
            if ep_addr.direction() == Direction::Out {
                let reg = &mut (*pac::USB).ENDPOINT[ep_addr.index()].RX_CSR;
                if stalled {
                    *reg |= (pac::RxCSRL_REG_EPN_SEND_STALL_MASK
                        | pac::RxCSRL_REG_EPN_RX_PKT_RDY_MASK) as u16;
                } else {
                    let mut reg_val = *reg;
                    reg_val &= !(pac::RxCSRL_REG_EPN_SEND_STALL_MASK as u16);
                    reg_val |= pac::RxCSRL_REG_EPN_RX_PKT_RDY_MASK as u16;
                    *reg = reg_val;
                }
            } else {
                let reg = &mut (*pac::USB).ENDPOINT[ep_addr.index()].TX_CSR;
                if stalled {
                    *reg |= pac::TxCSRL_REG_EPN_SEND_STALL_MASK as u16;
                } else {
                    *reg &= !(pac::TxCSRL_REG_EPN_SEND_STALL_MASK as u16);
                }
            }
        }
    }

    fn endpoint_is_stalled(&mut self, ep_addr: EndpointAddress) -> bool {
        let val = unsafe {
            if ep_addr.direction() == Direction::Out {
                (*pac::USB).ENDPOINT[ep_addr.index()].RX_CSR
                    & pac::RxCSRL_REG_EPN_STALL_SENT_MASK as u16
                    != 0
            } else {
                (*pac::USB).ENDPOINT[ep_addr.index()].TX_CSR
                    & pac::TxCSRL_REG_EPN_STALL_SENT_MASK as u16
                    != 0
            }
        };
        log::trace!("USB Bus::endpoint_is_stalled: {:?}, {:?}", ep_addr, val);
        val
    }

    async fn enable(&mut self) {}
    async fn disable(&mut self) {}

    async fn remote_wakeup(&mut self) -> Result<(), embassy_usb_driver::Unsupported> {
        // Maybe TODO: Does the MPFS support this? The platform code seems to hint at its existence,
        // yet I don't see any way to actually create a remote wakeup signal.
        Err(embassy_usb_driver::Unsupported)
    }
}

//------------------------------------------------------
// MSS USB CIF Callbacks

#[no_mangle]
#[doc(hidden)]
#[allow(non_upper_case_globals)]
pub static g_mss_usbd_cb: pac::mss_usbd_cb_t = pac::mss_usbd_cb_t {
    usbd_ep_rx: Some(usbd_ep_rx),
    usbd_ep_tx_complete: Some(usbd_ep_tx_complete),
    usbd_cep_setup: Some(usbd_cep_setup),
    usbd_cep_rx: Some(usbd_cep_rx),
    usbd_cep_tx_complete: Some(usbd_cep_tx_complete),
    usbd_sof: Some(usbd_sof),
    usbd_reset: Some(usbd_reset),
    usbd_suspend: Some(usbd_suspend),
    usbd_resume: Some(usbd_resume),
    usbd_disconnect: Some(usbd_disconnect),
    usbd_dma_handler: Some(usbd_dma_handler),
};
extern "C" fn usbd_cep_setup(status: u8) {
    unsafe {
        log::trace!(
            "usbd_cep_setup: {:?} {:?} {:?}",
            status,
            EP_OUT_CONTROLLER[0].as_mut().unwrap().state,
            EP_IN_CONTROLLER[0].as_mut().unwrap().state
        );
    }
    // This is called in three cases:
    // 1. When the control endpoint is stalled
    // 2. When the control endpoint recieves a setup packet before the setup has completed
    // 3. When the control endpoint recieves a normal setup packet
    //
    // The CIF clears the PHY of the stall in the first case already, so there's nothing left to do
    // The other two we can handle the same (I think?)
    critical_section::with(|_| unsafe {
        let read_ready = pac::MSS_USB_CIF_cep_is_rxpktrdy() != 0;
        if EP_OUT_CONTROLLER[0].as_mut().unwrap().state == EndpointState::Rx && read_ready {
            // MSS_USB_CIF_cep_rx_byte_count
            let len = (*pac::USB).INDEXED_CSR.DEVICE_EP0.COUNT0 & pac::COUNT0_REG_MASK as u16;
            EP_OUT_CONTROLLER[0].as_mut().unwrap().state = EndpointState::RxComplete(len as usize);
        } else if EP_IN_CONTROLLER[0].as_mut().unwrap().state == EndpointState::Tx {
            EP_IN_CONTROLLER[0].as_mut().unwrap().state = EndpointState::TxReadyForNext;
        } else if EP_IN_CONTROLLER[0].as_mut().unwrap().state == EndpointState::TxLast {
            EP_IN_CONTROLLER[0].as_mut().unwrap().state = EndpointState::TxComplete;
        } else if read_ready {
            EP_OUT_CONTROLLER[0].as_mut().unwrap().state = EndpointState::Setup;
        }

        #[allow(static_mut_refs)]
        if let Some(waker) = EP_OUT_CONTROLLER[0].as_mut().unwrap().waker.as_mut() {
            waker.wake_by_ref();
        }
        if let Some(waker) = EP_IN_CONTROLLER[0].as_mut().unwrap().waker.as_mut() {
            waker.wake_by_ref();
        }
    });
}

unsafe fn rx_complete(num: usize, received_count: u32) {
    critical_section::with(|_| {
        EP_OUT_CONTROLLER[num as usize].as_mut().unwrap().state =
            EndpointState::RxComplete(received_count as usize);
    });
    if let Some(waker) = EP_OUT_CONTROLLER[num as usize]
        .as_mut()
        .unwrap()
        .waker
        .as_mut()
    {
        waker.wake_by_ref();
    }
}

extern "C" fn usbd_ep_rx(num: pac::mss_usb_ep_num_t, status: u8) {
    unsafe {
        let received_count = (*pac::USB).ENDPOINT[num as usize].RX_COUNT as u32;
        let ep = EP_OUT_CONTROLLER[num as usize].as_ref().unwrap();
        log::trace!(
            "usbd_ep_rx {:?}: {:?}; Received count {:?}",
            num,
            status,
            received_count
        );

        // DMA
        if ep.ep.dma_enable != 0 {
            if ((*pac::USB).ENDPOINT[num as usize].RX_CSR
                & pac::RxCSRL_REG_EPN_DMA_MODE_MASK as u16)
                == 0
            {
                log::trace!("DMA mode 0");

                // MSS_USB_CIF_dma_write_count
                (*pac::USB).DMA_CHANNEL[ep.ep.dma_channel as usize].COUNT = received_count;
                // MSS_USB_CIF_dma_start_xfr
                (*pac::USB).DMA_CHANNEL[ep.ep.dma_channel as usize].CNTL |=
                    pac::DMA_CNTL_REG_START_XFR_MASK;
                return; // A DMA interrupt will be triggered
            } else {
                log::trace!("DMA mode 1");
                // MSS_USB_CIF_dma_stop_xfr
                (*pac::USB).DMA_CHANNEL[ep.ep.dma_channel as usize].CNTL &=
                    !pac::DMA_CNTL_REG_START_XFR_MASK;
                pac::MSS_USB_CIF_rx_ep_clr_autoclr(num);
                // Rest is the same as for non-DMA mode
            }
        }

        if received_count > 0 {
            pac::MSS_USB_CIF_read_rx_fifo(
                num,
                EP_OUT_CONTROLLER[num as usize]
                    .as_mut()
                    .unwrap()
                    .ep
                    .buf_addr as *mut core::ffi::c_void,
                received_count,
            );
        }
        // MSS_USB_CIF_rx_ep_clr_rxpktrdy
        (*pac::USB).ENDPOINT[num as usize].RX_CSR &= !(pac::RxCSRL_REG_EPN_RX_PKT_RDY_MASK as u16);
        rx_complete(num as usize, received_count);
    }
}

fn tx_complete(ep_num: usize) {
    critical_section::with(|_| unsafe {
        EP_IN_CONTROLLER[ep_num].as_mut().unwrap().state = EndpointState::TxComplete;
        if let Some(waker) = EP_IN_CONTROLLER[ep_num].as_mut().unwrap().waker.as_mut() {
            waker.wake_by_ref();
        }
    });
}

extern "C" fn usbd_ep_tx_complete(num: pac::mss_usb_ep_num_t, status: u8) {
    log::trace!("usbd_ep_tx_complete {:?}: {:?}", num, status);
    // unsafe {
    //     let dma_channel = EP_IN_CONTROLLER[num as usize]
    //         .as_mut()
    //         .unwrap()
    //         .ep
    //         .dma_channel;
    //     if dma_channel != pac::mss_usb_dma_channel_t_MSS_USB_DMA_CHANNEL_NA {
    //         log::trace!(
    //             "DMA Tx {} complete on channel {:?}: {:?}",
    //             num,
    //             dma_channel,
    //             &(*pac::USB).DMA_CHANNEL[dma_channel as usize]
    //         );
    //     }
    // }
    tx_complete(num as usize);
}

extern "C" fn usbd_dma_handler(
    ep_num: pac::mss_usb_ep_num_t,
    dma_dir: pac::mss_usb_dma_dir_t,
    status: u8,
    dma_addr_val: u32,
) {
    log::trace!(
        "usbd_dma_handler {:?}: {:?}, {:?}, 0x{:x?}",
        ep_num,
        dma_dir,
        status,
        dma_addr_val
    );
    if status == pac::DMA_XFR_ERROR as u8 {
        log::error!("DMA transfer error");
    } else {
        if dma_dir == pac::mss_usb_dma_dir_t_MSS_USB_DMA_READ {
            // Tx
            unsafe {
                // This triggers a TX complete interrupt
                // MSS_USB_CIF_tx_ep_set_txpktrdy
                (*pac::USB).ENDPOINT[ep_num as usize].TX_CSR |=
                    pac::TxCSRL_REG_EPN_TX_PKT_RDY_MASK as u16;
            }
        } else {
            // Rx
            log::trace!("DMA RX {:?}", ep_num);
            unsafe {
                let received_count = dma_addr_val
                    - EP_OUT_CONTROLLER[ep_num as usize]
                        .as_ref()
                        .unwrap()
                        .ep
                        .buf_addr as u32;
                rx_complete(ep_num as usize, received_count);
            }
        }
    }
}

extern "C" fn usbd_cep_rx(_status: u8) {
    // This will never be called, since we don't use the CIF's cep_state, which is what dictates
    // whether setup, rx, or tx_complete is called
    // Instead we look at the state of the endpoint controller directly in cep_setup
}

extern "C" fn usbd_cep_tx_complete(_status: u8) {
    // This will never be called, since we don't use the CIF's cep_state, which is what dictates
    // whether setup, rx, or tx_complete is called
    // Instead we look at the state of the endpoint controller directly in cep_setup
}

extern "C" fn usbd_sof(_status: u8) {
    // This is not called
}

extern "C" fn usbd_reset() {
    log::trace!("usbd_reset");
    critical_section::with(|_| unsafe {
        BUS_EVENT.reset = true;
        #[allow(static_mut_refs)]
        if let Some(waker) = BUS_EVENT_WAKER.as_mut() {
            waker.wake_by_ref();
        }
    });
}

extern "C" fn usbd_suspend() {
    log::trace!("usbd_suspend");
    critical_section::with(|_| unsafe {
        BUS_EVENT.suspend = true;
        #[allow(static_mut_refs)]
        if let Some(waker) = BUS_EVENT_WAKER.as_mut() {
            waker.wake_by_ref();
        }
    });
}

extern "C" fn usbd_resume() {
    log::trace!("usbd_resume");
    critical_section::with(|_| unsafe {
        BUS_EVENT.resume = true;
        #[allow(static_mut_refs)]
        if let Some(waker) = BUS_EVENT_WAKER.as_mut() {
            waker.wake_by_ref();
        }
    });
}

extern "C" fn usbd_disconnect() {
    log::trace!("usbd_disconnect");
    critical_section::with(|_| unsafe {
        BUS_EVENT.disconnect = true;
        #[allow(static_mut_refs)]
        if let Some(waker) = BUS_DISCONNECT_WAKER.as_mut() {
            waker.wake_by_ref();
        }
    });
}