|
| 1 | +//! OTG USB 2.0 FS serial port example using polling in a busy loop. |
| 2 | +//! |
| 3 | +//! Note: Must build with features "stm32l4x5 otg_fs" or "stm32l4x6 otg_fs". |
| 4 | +#![no_main] |
| 5 | +#![no_std] |
| 6 | + |
| 7 | +extern crate panic_semihosting; |
| 8 | + |
| 9 | +use cortex_m_rt::entry; |
| 10 | +use stm32l4xx_hal::gpio::Speed; |
| 11 | +use stm32l4xx_hal::otg_fs::{UsbBus, USB}; |
| 12 | +use stm32l4xx_hal::prelude::*; |
| 13 | +use stm32l4xx_hal::rcc::{ |
| 14 | + ClockSecuritySystem, CrystalBypass, MsiFreq, PllConfig, PllDivider, PllSource, |
| 15 | +}; |
| 16 | +use stm32l4xx_hal::stm32::{Peripherals, CRS, PWR, RCC}; |
| 17 | +use usb_device::prelude::*; |
| 18 | + |
| 19 | +/// Enable CRS (Clock Recovery System) |
| 20 | +fn enable_crs() { |
| 21 | + let rcc = unsafe { &(*RCC::ptr()) }; |
| 22 | + rcc.apb1enr1.modify(|_, w| w.crsen().set_bit()); |
| 23 | + let crs = unsafe { &(*CRS::ptr()) }; |
| 24 | + // Initialize clock recovery |
| 25 | + // Set autotrim enabled. |
| 26 | + crs.cr.modify(|_, w| w.autotrimen().set_bit()); |
| 27 | + // Enable CR |
| 28 | + crs.cr.modify(|_, w| w.cen().set_bit()); |
| 29 | +} |
| 30 | + |
| 31 | +/// Enables VddUSB power supply |
| 32 | +fn enable_usb_pwr() { |
| 33 | + // Enable PWR peripheral |
| 34 | + let rcc = unsafe { &(*RCC::ptr()) }; |
| 35 | + rcc.apb1enr1.modify(|_, w| w.pwren().set_bit()); |
| 36 | + |
| 37 | + // Enable VddUSB |
| 38 | + let pwr = unsafe { &*PWR::ptr() }; |
| 39 | + pwr.cr2.modify(|_, w| w.usv().set_bit()); |
| 40 | +} |
| 41 | + |
| 42 | +/// Reset peripherals to known state. |
| 43 | +unsafe fn reset_peripherals(dp: &Peripherals) { |
| 44 | + dp.RCC.cr.modify(|_, w| w.msion().set_bit()); |
| 45 | + dp.RCC.cfgr.modify(|_, w| { |
| 46 | + w.sw().bits(0); |
| 47 | + w.hpre().bits(0); |
| 48 | + w.ppre1().bits(0); |
| 49 | + w.ppre2().bits(0); |
| 50 | + w.mcosel().bits(0); |
| 51 | + w |
| 52 | + }); |
| 53 | + dp.RCC.cr.modify(|_, w| { |
| 54 | + w.pllsai2on().clear_bit(); |
| 55 | + w.pllsai1on().clear_bit(); |
| 56 | + w.pllon().clear_bit(); |
| 57 | + w.hsion().clear_bit(); |
| 58 | + w.csson().clear_bit(); |
| 59 | + w.hseon().clear_bit(); |
| 60 | + w |
| 61 | + }); |
| 62 | + dp.RCC.pllcfgr.modify(|_, w| { |
| 63 | + w.pllpdiv().bits(0); |
| 64 | + w.pllr().bits(0); |
| 65 | + w.pllren().clear_bit(); |
| 66 | + w.pllq().bits(0); |
| 67 | + w.pllqen().clear_bit(); |
| 68 | + w.pllp().clear_bit(); |
| 69 | + w.pllpen().clear_bit(); |
| 70 | + w.plln().bits(1 << 4); |
| 71 | + w.pllm().bits(0); |
| 72 | + w.pllsrc().bits(0); |
| 73 | + w |
| 74 | + }); |
| 75 | + |
| 76 | + dp.RCC.crrcr.modify(|_, w| w.hsi48on().clear_bit()); |
| 77 | + dp.RCC.cr.modify(|_, w| w.hsebyp().clear_bit()); |
| 78 | + |
| 79 | + dp.RCC.pllcfgr.modify(|_, w| { |
| 80 | + w.pllsrc().bits(0); |
| 81 | + w.pllpdiv().bits(0); |
| 82 | + w |
| 83 | + }); |
| 84 | + |
| 85 | + dp.RCC.cier.reset(); |
| 86 | + |
| 87 | + dp.FLASH.acr.modify(|_, w| w.bits(4)); |
| 88 | +} |
| 89 | + |
| 90 | +static mut EP_MEMORY: [u32; 1024] = [0; 1024]; |
| 91 | + |
| 92 | +#[entry] |
| 93 | +unsafe fn main() -> ! { |
| 94 | + let dp = Peripherals::take().unwrap(); |
| 95 | + |
| 96 | + //reset_peripherals(&dp); |
| 97 | + |
| 98 | + let mut flash = dp.FLASH.constrain(); |
| 99 | + let mut rcc = dp.RCC.constrain(); |
| 100 | + let mut pwr = dp.PWR.constrain(&mut rcc.apb1r1); |
| 101 | + |
| 102 | + // Set to true if external 16 MHz high-speed resonator/crystal is used. |
| 103 | + const USE_HSE_CLK: bool = true; |
| 104 | + |
| 105 | + let clocks = { |
| 106 | + if !USE_HSE_CLK { |
| 107 | + // 48 MHz / 6 * 40 / 4 = 80 MHz |
| 108 | + let pll_cfg = PllConfig::new(6, 40, PllDivider::Div4); |
| 109 | + |
| 110 | + // Note: If program needs low-speed clocks, adjust this. |
| 111 | + rcc.cfgr |
| 112 | + .msi(MsiFreq::RANGE48M) // Set the MSI (multi-speed internal) clock to 48 MHz |
| 113 | + .pll_source(PllSource::MSI) |
| 114 | + .sysclk_with_pll(80.mhz(), pll_cfg) |
| 115 | + .pclk1(24.mhz()) |
| 116 | + .pclk2(24.mhz()) |
| 117 | + .freeze(&mut flash.acr, &mut pwr) |
| 118 | + } else { |
| 119 | + // Note: If program needs low-speed clocks, adjust this. |
| 120 | + // Tested using a 16 MHz resonator. |
| 121 | + rcc.cfgr |
| 122 | + .msi(MsiFreq::RANGE48M) |
| 123 | + .hse( |
| 124 | + 16.mhz(), |
| 125 | + CrystalBypass::Disable, // Bypass enabled when clock signals instead of crystals/resonators are used. |
| 126 | + ClockSecuritySystem::Disable, // We have not set up interrupt routines handling clock drifts/errors. |
| 127 | + ) |
| 128 | + .pll_source(PllSource::HSE) |
| 129 | + .sysclk(80.mhz()) |
| 130 | + .freeze(&mut flash.acr, &mut pwr) |
| 131 | + } |
| 132 | + }; |
| 133 | + |
| 134 | + // Enable clock recovery system. |
| 135 | + enable_crs(); |
| 136 | + // Enable USB power (and disable VddUSB power isolation). |
| 137 | + enable_usb_pwr(); |
| 138 | + |
| 139 | + let mut gpioa = dp.GPIOA.split(&mut rcc.ahb2); |
| 140 | + |
| 141 | + let usb = USB { |
| 142 | + usb_global: dp.OTG_FS_GLOBAL, |
| 143 | + usb_device: dp.OTG_FS_DEVICE, |
| 144 | + usb_pwrclk: dp.OTG_FS_PWRCLK, |
| 145 | + hclk: clocks.hclk(), |
| 146 | + pin_dm: gpioa |
| 147 | + .pa11 |
| 148 | + .into_af10(&mut gpioa.moder, &mut gpioa.afrh) |
| 149 | + .set_speed(Speed::VeryHigh), |
| 150 | + pin_dp: gpioa |
| 151 | + .pa12 |
| 152 | + .into_af10(&mut gpioa.moder, &mut gpioa.afrh) |
| 153 | + .set_speed(Speed::VeryHigh), |
| 154 | + }; |
| 155 | + |
| 156 | + let usb_bus = UsbBus::new(usb, &mut EP_MEMORY); |
| 157 | + |
| 158 | + let mut usb_serial = usbd_serial::SerialPort::new(&usb_bus); |
| 159 | + |
| 160 | + let mut usb_dev = UsbDeviceBuilder::new(&usb_bus, UsbVidPid(0x16c0, 0x27dd)) |
| 161 | + .manufacturer("Fake Company") |
| 162 | + .product("Serial port") |
| 163 | + .serial_number("TEST") |
| 164 | + .device_class(usbd_serial::USB_CLASS_CDC) |
| 165 | + .build(); |
| 166 | + |
| 167 | + #[cfg(feature = "semihosting")] |
| 168 | + hprintln!("Polling!").ok(); |
| 169 | + |
| 170 | + loop { |
| 171 | + if !usb_dev.poll(&mut [&mut usb_serial]) { |
| 172 | + continue; |
| 173 | + } |
| 174 | + |
| 175 | + let mut buf = [0u8; 64]; |
| 176 | + |
| 177 | + match usb_serial.read(&mut buf) { |
| 178 | + Ok(count) if count > 0 => { |
| 179 | + // Echo back in upper case |
| 180 | + for c in buf[0..count].iter_mut() { |
| 181 | + if 0x61 <= *c && *c <= 0x7a { |
| 182 | + *c &= !0x20; |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + let mut write_offset = 0; |
| 187 | + while write_offset < count { |
| 188 | + match usb_serial.write(&buf[write_offset..count]) { |
| 189 | + Ok(len) if len > 0 => { |
| 190 | + write_offset += len; |
| 191 | + } |
| 192 | + _ => {} |
| 193 | + } |
| 194 | + } |
| 195 | + } |
| 196 | + _ => {} |
| 197 | + } |
| 198 | + } |
| 199 | +} |
0 commit comments