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 bsp:: pin_alias;
35+ use hal:: clock:: v2:: clock_system_at_reset;
36+ use hal:: pac:: { interrupt, CorePeripherals , Peripherals } ;
37+ use hal:: prelude:: * ;
38+ use hal:: usb:: UsbBus ;
4039
4140#[ entry]
4241fn main ( ) -> ! {
4342 let mut peripherals = Peripherals :: take ( ) . unwrap ( ) ;
4443 let mut core = CorePeripherals :: take ( ) . unwrap ( ) ;
45- let mut clocks = GenericClockController :: with_internal_32kosc (
44+ let ( _buses, clocks, tokens) = clock_system_at_reset (
45+ peripherals. oscctrl ,
46+ peripherals. osc32kctrl ,
4647 peripherals. gclk ,
47- & mut peripherals. mclk ,
48- & mut peripherals. osc32kctrl ,
49- & mut peripherals. oscctrl ,
48+ peripherals. mclk ,
5049 & mut peripherals. nvmctrl ,
5150 ) ;
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 ( ) ) ;
5651
5752 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 ( ) ) ) ;
53+ let mut red_led: bsp:: RedLed = pin_alias ! ( pins. red_led) . into ( ) ;
6154
6255 let bus_allocator = unsafe {
6356 USB_ALLOCATOR = Some ( bsp:: usb_allocator (
6457 peripherals. usb ,
65- & mut clocks,
66- & mut peripherals. mclk ,
58+ clocks. dfll ,
59+ tokens. gclks . gclk1 ,
60+ tokens. pclks . usb ,
61+ clocks. ahbs . usb ,
62+ tokens. apbs . usb ,
6763 pins. usb_dm ,
6864 pins. usb_dp ,
6965 ) ) ;
7066 USB_ALLOCATOR . as_ref ( ) . unwrap ( )
7167 } ;
7268
7369 unsafe {
74- USB_SERIAL = Some ( SerialPort :: new ( & bus_allocator) ) ;
70+ USB_SERIAL = Some ( SerialPort :: new ( bus_allocator) ) ;
7571 USB_BUS = Some (
76- UsbDeviceBuilder :: new ( & bus_allocator, UsbVidPid ( 0x16c0 , 0x27dd ) )
77- . strings ( & [ StringDescriptors :: new ( LangID :: EN_US )
72+ UsbDeviceBuilder :: new ( bus_allocator, UsbVidPid ( 0x2222 , 0x3333 ) )
73+ . strings ( & [ StringDescriptors :: new ( LangID :: EN )
7874 . manufacturer ( "Fake company" )
7975 . product ( "Serial port" )
8076 . serial_number ( "TEST" ) ] )
@@ -85,66 +81,66 @@ fn main() -> ! {
8581 }
8682
8783 unsafe {
88- core. NVIC . set_priority ( interrupt:: USB_OTHER , 1 ) ;
8984 core. NVIC . set_priority ( interrupt:: USB_TRCPT0 , 1 ) ;
90- core. NVIC . set_priority ( interrupt:: USB_TRCPT1 , 1 ) ;
91- NVIC :: unmask ( interrupt:: USB_OTHER ) ;
9285 NVIC :: unmask ( interrupt:: USB_TRCPT0 ) ;
86+ core. NVIC . set_priority ( interrupt:: USB_TRCPT1 , 1 ) ;
9387 NVIC :: unmask ( interrupt:: USB_TRCPT1 ) ;
88+ core. NVIC . set_priority ( interrupt:: USB_SOF_HSOF , 1 ) ;
89+ NVIC :: unmask ( interrupt:: USB_SOF_HSOF ) ;
90+ core. NVIC . set_priority ( interrupt:: USB_OTHER , 1 ) ;
91+ NVIC :: unmask ( interrupt:: USB_OTHER ) ;
9492 }
9593
94+ // Flash the LED in a spin loop to demonstrate that USB is
95+ // entirely interrupt driven.
9696 loop {
97- let pending = disable_interrupts ( |_| unsafe {
98- let pending = PENDING_COLOR ;
99- PENDING_COLOR = None ;
100- pending
97+ cycle_delay ( 5 * 1024 * 1024 ) ;
98+ red_led. toggle ( ) . unwrap ( ) ;
99+ // Turn off interrupts so we don't fight with the interrupt
100+ cortex_m:: interrupt:: free ( |_| unsafe {
101+ if USB_BUS . as_mut ( ) . is_some ( ) {
102+ if let Some ( serial) = USB_SERIAL . as_mut ( ) {
103+ let _ = serial. write ( "Hello USB\n " . as_bytes ( ) ) ;
104+ }
105+ }
101106 } ) ;
102- if let Some ( color) = pending {
103- let _ = neopixel. write ( ( 0 ..5 ) . map ( |_| color) ) ;
104- }
105107 }
106108}
107109
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 ;
110+ static mut USB_ALLOCATOR : Option < UsbBusAllocator < UsbBus < Gclk1Id > > > = None ;
111+ static mut USB_BUS : Option < UsbDevice < UsbBus < Gclk1Id > > > = None ;
112+ static mut USB_SERIAL : Option < SerialPort < UsbBus < Gclk1Id > > > = None ;
112113
113114fn poll_usb ( ) {
114115 unsafe {
115- USB_BUS . as_mut ( ) . map ( |usb_dev| {
116- USB_SERIAL . as_mut ( ) . map ( |serial| {
116+ if let Some ( usb_dev ) = USB_BUS . as_mut ( ) {
117+ if let Some ( serial ) = USB_SERIAL . as_mut ( ) {
117118 usb_dev. poll ( & mut [ serial] ) ;
118119
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- } ) ;
120+ // Make the other side happy
121+ let mut buf = [ 0u8 ; 16 ] ;
122+ let _ = serial. read ( & mut buf) ;
123+ } ;
124+ }
134125 } ;
135126}
136127
137128#[ interrupt]
138- fn USB_OTHER ( ) {
129+ fn USB_TRCPT0 ( ) {
139130 poll_usb ( ) ;
140131}
141132
142133#[ interrupt]
143- fn USB_TRCPT0 ( ) {
134+ fn USB_TRCPT1 ( ) {
144135 poll_usb ( ) ;
145136}
146137
147138#[ interrupt]
148- fn USB_TRCPT1 ( ) {
139+ fn USB_SOF_HSOF ( ) {
140+ poll_usb ( ) ;
141+ }
142+
143+ #[ interrupt]
144+ fn USB_OTHER ( ) {
149145 poll_usb ( ) ;
150146}
0 commit comments