1414//! Note leds may appear white during debug. Either build for release or add
1515//! opt-level = 2 to profile.dev in Cargo.toml
1616
17- use bsp:: hal;
1817use grand_central_m4 as bsp;
1918
19+ use bsp:: hal;
20+
2021#[ cfg( not( feature = "use_semihosting" ) ) ]
2122use panic_halt as _;
2223#[ cfg( feature = "use_semihosting" ) ]
2324use panic_semihosting as _;
2425
25- use bsp:: entry;
26- use cortex_m:: interrupt:: free as disable_interrupts;
26+ use cortex_m:: asm:: delay as cycle_delay;
2727use cortex_m:: peripheral:: NVIC ;
28- use hal:: clock:: GenericClockController ;
29- use hal:: pac:: { interrupt, CorePeripherals , Peripherals } ;
30- use hal:: prelude:: * ;
31- use hal:: time:: Hertz ;
32- use hal:: timer:: TimerCounter ;
33- use hal:: timer_traits:: InterruptDrivenTimer ;
34- use hal:: usb:: UsbBus ;
35- use smart_leds:: { colors, hsv:: RGB8 , SmartLedsWrite } ;
3628use usb_device:: bus:: UsbBusAllocator ;
3729use usb_device:: prelude:: * ;
3830use usbd_serial:: { SerialPort , USB_CLASS_CDC } ;
39- use ws2812_timer_delay as ws2812;
31+
32+ use bsp:: clock:: gclk:: Gclk1Id ;
33+ use bsp:: entry;
34+ use hal:: clock:: v2:: clock_system_at_reset;
35+ use hal:: pac:: { interrupt, CorePeripherals , Peripherals } ;
36+ use hal:: prelude:: * ;
37+ use hal:: usb:: UsbBus ;
4038
4139#[ entry]
4240fn main ( ) -> ! {
4341 let mut peripherals = Peripherals :: take ( ) . unwrap ( ) ;
4442 let mut core = CorePeripherals :: take ( ) . unwrap ( ) ;
45- let mut clocks = GenericClockController :: with_internal_32kosc (
43+ let ( _buses, clocks, tokens) = clock_system_at_reset (
44+ peripherals. oscctrl ,
45+ peripherals. osc32kctrl ,
4646 peripherals. gclk ,
47- & mut peripherals. mclk ,
48- & mut peripherals. osc32kctrl ,
49- & mut peripherals. oscctrl ,
47+ peripherals. mclk ,
5048 & mut peripherals. nvmctrl ,
5149 ) ;
52- let gclk0 = clocks. gclk0 ( ) ;
53- let tc2_3 = clocks. tc2_tc3 ( & gclk0) . unwrap ( ) ;
54- let mut timer = TimerCounter :: tc3_ ( & tc2_3, peripherals. tc3 , & mut peripherals. mclk ) ;
55- InterruptDrivenTimer :: start ( & mut timer, Hertz :: MHz ( 3 ) . into_duration ( ) ) ;
5650
5751 let pins = bsp:: Pins :: new ( peripherals. port ) ;
58- let neopixel_pin = pins. neopixel . into_push_pull_output ( ) ;
59- let mut neopixel = ws2812:: Ws2812 :: new ( timer, neopixel_pin) ;
60- let _ = neopixel. write ( ( 0 ..5 ) . map ( |_| RGB8 :: default ( ) ) ) ;
52+ let mut red_led: bsp:: RedLed = pins. d13 . into ( ) ;
6153
6254 let bus_allocator = unsafe {
6355 USB_ALLOCATOR = Some ( bsp:: usb_allocator (
6456 peripherals. usb ,
65- & mut clocks,
66- & mut peripherals. mclk ,
57+ clocks. dfll ,
58+ tokens. gclks . gclk1 ,
59+ tokens. pclks . usb ,
60+ clocks. ahbs . usb ,
61+ tokens. apbs . usb ,
6762 pins. usb_dm ,
6863 pins. usb_dp ,
6964 ) ) ;
7065 USB_ALLOCATOR . as_ref ( ) . unwrap ( )
7166 } ;
7267
7368 unsafe {
74- USB_SERIAL = Some ( SerialPort :: new ( & bus_allocator) ) ;
69+ USB_SERIAL = Some ( SerialPort :: new ( bus_allocator) ) ;
7570 USB_BUS = Some (
76- UsbDeviceBuilder :: new ( & bus_allocator, UsbVidPid ( 0x16c0 , 0x27dd ) )
77- . strings ( & [ StringDescriptors :: new ( LangID :: EN_US )
71+ UsbDeviceBuilder :: new ( bus_allocator, UsbVidPid ( 0x2222 , 0x3333 ) )
72+ . strings ( & [ StringDescriptors :: new ( LangID :: EN )
7873 . manufacturer ( "Fake company" )
7974 . product ( "Serial port" )
8075 . serial_number ( "TEST" ) ] )
@@ -85,66 +80,66 @@ fn main() -> ! {
8580 }
8681
8782 unsafe {
88- core. NVIC . set_priority ( interrupt:: USB_OTHER , 1 ) ;
8983 core. NVIC . set_priority ( interrupt:: USB_TRCPT0 , 1 ) ;
90- core. NVIC . set_priority ( interrupt:: USB_TRCPT1 , 1 ) ;
91- NVIC :: unmask ( interrupt:: USB_OTHER ) ;
9284 NVIC :: unmask ( interrupt:: USB_TRCPT0 ) ;
85+ core. NVIC . set_priority ( interrupt:: USB_TRCPT1 , 1 ) ;
9386 NVIC :: unmask ( interrupt:: USB_TRCPT1 ) ;
87+ core. NVIC . set_priority ( interrupt:: USB_SOF_HSOF , 1 ) ;
88+ NVIC :: unmask ( interrupt:: USB_SOF_HSOF ) ;
89+ core. NVIC . set_priority ( interrupt:: USB_OTHER , 1 ) ;
90+ NVIC :: unmask ( interrupt:: USB_OTHER ) ;
9491 }
9592
93+ // Flash the LED in a spin loop to demonstrate that USB is
94+ // entirely interrupt driven.
9695 loop {
97- let pending = disable_interrupts ( |_| unsafe {
98- let pending = PENDING_COLOR ;
99- PENDING_COLOR = None ;
100- pending
96+ cycle_delay ( 5 * 1024 * 1024 ) ;
97+ red_led. toggle ( ) . unwrap ( ) ;
98+ // Turn off interrupts so we don't fight with the interrupt
99+ cortex_m:: interrupt:: free ( |_| unsafe {
100+ if USB_BUS . as_mut ( ) . is_some ( ) {
101+ if let Some ( serial) = USB_SERIAL . as_mut ( ) {
102+ let _ = serial. write ( "Hello USB\n " . as_bytes ( ) ) ;
103+ }
104+ }
101105 } ) ;
102- if let Some ( color) = pending {
103- let _ = neopixel. write ( ( 0 ..5 ) . map ( |_| color) ) ;
104- }
105106 }
106107}
107108
108- static mut USB_ALLOCATOR : Option < UsbBusAllocator < UsbBus > > = None ;
109- static mut USB_BUS : Option < UsbDevice < UsbBus > > = None ;
110- static mut USB_SERIAL : Option < SerialPort < UsbBus > > = None ;
111- static mut PENDING_COLOR : Option < RGB8 > = None ;
109+ static mut USB_ALLOCATOR : Option < UsbBusAllocator < UsbBus < Gclk1Id > > > = None ;
110+ static mut USB_BUS : Option < UsbDevice < UsbBus < Gclk1Id > > > = None ;
111+ static mut USB_SERIAL : Option < SerialPort < UsbBus < Gclk1Id > > > = None ;
112112
113113fn poll_usb ( ) {
114114 unsafe {
115- USB_BUS . as_mut ( ) . map ( |usb_dev| {
116- USB_SERIAL . as_mut ( ) . map ( |serial| {
115+ if let Some ( usb_dev ) = USB_BUS . as_mut ( ) {
116+ if let Some ( serial ) = USB_SERIAL . as_mut ( ) {
117117 usb_dev. poll ( & mut [ serial] ) ;
118118
119- let mut buf = [ 0u8 ; 64 ] ;
120-
121- if let Ok ( count) = serial. read ( & mut buf) {
122- let last = buf[ count - 1 ] as char ;
123- let color = match last {
124- 'R' => colors:: RED ,
125- 'G' => colors:: GREEN ,
126- 'O' => colors:: ORANGE ,
127- _ => RGB8 :: default ( ) ,
128- } ;
129-
130- PENDING_COLOR = Some ( color) ;
131- } ;
132- } ) ;
133- } ) ;
119+ // Make the other side happy
120+ let mut buf = [ 0u8 ; 16 ] ;
121+ let _ = serial. read ( & mut buf) ;
122+ } ;
123+ }
134124 } ;
135125}
136126
137127#[ interrupt]
138- fn USB_OTHER ( ) {
128+ fn USB_TRCPT0 ( ) {
139129 poll_usb ( ) ;
140130}
141131
142132#[ interrupt]
143- fn USB_TRCPT0 ( ) {
133+ fn USB_TRCPT1 ( ) {
144134 poll_usb ( ) ;
145135}
146136
147137#[ interrupt]
148- fn USB_TRCPT1 ( ) {
138+ fn USB_SOF_HSOF ( ) {
139+ poll_usb ( ) ;
140+ }
141+
142+ #[ interrupt]
143+ fn USB_OTHER ( ) {
149144 poll_usb ( ) ;
150145}
0 commit comments