diff options
| author | Dario Nieuwenhuis <[email protected]> | 2023-06-16 16:06:50 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-06-16 16:06:50 +0000 |
| commit | ec36225f8ab35fab149971e587ef506aa1c9d1ca (patch) | |
| tree | c62292e1420d48be2ab81600e01e866b9a488523 | |
| parent | 0ac43d3e7c2f57e6ea9608449735b3ea79ec75a8 (diff) | |
| parent | 61aa6b5236b68b037db1c5f349e8183a2980ffc5 (diff) | |
Merge pull request #1560 from kevswims/feature/stm32g4-pll-enhancements
Feature/stm32g4 pll enhancements - Add PLL support for the P and Q outputs for G4 series chips
| -rw-r--r-- | embassy-stm32/src/rcc/g4.rs | 207 | ||||
| -rw-r--r-- | examples/stm32g4/Cargo.toml | 1 | ||||
| -rw-r--r-- | examples/stm32g4/src/bin/pll.rs | 15 | ||||
| -rw-r--r-- | examples/stm32g4/src/bin/usb_serial.rs | 110 |
4 files changed, 295 insertions, 38 deletions
diff --git a/embassy-stm32/src/rcc/g4.rs b/embassy-stm32/src/rcc/g4.rs index 2b52416b2..9401af4c3 100644 --- a/embassy-stm32/src/rcc/g4.rs +++ b/embassy-stm32/src/rcc/g4.rs | |||
| @@ -17,7 +17,7 @@ pub const LSI_FREQ: Hertz = Hertz(32_000); | |||
| 17 | pub enum ClockSrc { | 17 | pub enum ClockSrc { |
| 18 | HSE(Hertz), | 18 | HSE(Hertz), |
| 19 | HSI16, | 19 | HSI16, |
| 20 | PLLCLK(PllSrc, PllM, PllN, PllR), | 20 | PLL, |
| 21 | } | 21 | } |
| 22 | 22 | ||
| 23 | /// AHB prescaler | 23 | /// AHB prescaler |
| @@ -60,6 +60,68 @@ impl Into<Pllsrc> for PllSrc { | |||
| 60 | } | 60 | } |
| 61 | } | 61 | } |
| 62 | 62 | ||
| 63 | seq_macro::seq!(P in 2..=31 { | ||
| 64 | /// Output divider for the PLL P output. | ||
| 65 | #[derive(Clone, Copy)] | ||
| 66 | pub enum PllP { | ||
| 67 | // Note: If PLL P is set to 0 the PLLP bit controls the output division. There does not seem to | ||
| 68 | // a good reason to do this so the API does not support it. | ||
| 69 | // Div1 is invalid | ||
| 70 | #( | ||
| 71 | Div~P, | ||
| 72 | )* | ||
| 73 | } | ||
| 74 | |||
| 75 | impl From<PllP> for u8 { | ||
| 76 | /// Returns the register value for the P output divider. | ||
| 77 | fn from(val: PllP) -> u8 { | ||
| 78 | match val { | ||
| 79 | #( | ||
| 80 | PllP::Div~P => P, | ||
| 81 | )* | ||
| 82 | } | ||
| 83 | } | ||
| 84 | } | ||
| 85 | }); | ||
| 86 | |||
| 87 | impl PllP { | ||
| 88 | /// Returns the numeric value of the P output divider. | ||
| 89 | pub fn to_div(self) -> u32 { | ||
| 90 | let val: u8 = self.into(); | ||
| 91 | val as u32 | ||
| 92 | } | ||
| 93 | } | ||
| 94 | |||
| 95 | /// Output divider for the PLL Q output. | ||
| 96 | #[derive(Clone, Copy)] | ||
| 97 | pub enum PllQ { | ||
| 98 | Div2, | ||
| 99 | Div4, | ||
| 100 | Div6, | ||
| 101 | Div8, | ||
| 102 | } | ||
| 103 | |||
| 104 | impl PllQ { | ||
| 105 | /// Returns the numeric value of the Q output divider. | ||
| 106 | pub fn to_div(self) -> u32 { | ||
| 107 | let val: u8 = self.into(); | ||
| 108 | (val as u32 + 1) * 2 | ||
| 109 | } | ||
| 110 | } | ||
| 111 | |||
| 112 | impl From<PllQ> for u8 { | ||
| 113 | /// Returns the register value for the Q output divider. | ||
| 114 | fn from(val: PllQ) -> u8 { | ||
| 115 | match val { | ||
| 116 | PllQ::Div2 => 0b00, | ||
| 117 | PllQ::Div4 => 0b01, | ||
| 118 | PllQ::Div6 => 0b10, | ||
| 119 | PllQ::Div8 => 0b11, | ||
| 120 | } | ||
| 121 | } | ||
| 122 | } | ||
| 123 | |||
| 124 | /// Output divider for the PLL R output. | ||
| 63 | #[derive(Clone, Copy)] | 125 | #[derive(Clone, Copy)] |
| 64 | pub enum PllR { | 126 | pub enum PllR { |
| 65 | Div2, | 127 | Div2, |
| @@ -69,6 +131,7 @@ pub enum PllR { | |||
| 69 | } | 131 | } |
| 70 | 132 | ||
| 71 | impl PllR { | 133 | impl PllR { |
| 134 | /// Returns the numeric value of the R output divider. | ||
| 72 | pub fn to_div(self) -> u32 { | 135 | pub fn to_div(self) -> u32 { |
| 73 | let val: u8 = self.into(); | 136 | let val: u8 = self.into(); |
| 74 | (val as u32 + 1) * 2 | 137 | (val as u32 + 1) * 2 |
| @@ -76,6 +139,7 @@ impl PllR { | |||
| 76 | } | 139 | } |
| 77 | 140 | ||
| 78 | impl From<PllR> for u8 { | 141 | impl From<PllR> for u8 { |
| 142 | /// Returns the register value for the R output divider. | ||
| 79 | fn from(val: PllR) -> u8 { | 143 | fn from(val: PllR) -> u8 { |
| 80 | match val { | 144 | match val { |
| 81 | PllR::Div2 => 0b00, | 145 | PllR::Div2 => 0b00, |
| @@ -87,6 +151,7 @@ impl From<PllR> for u8 { | |||
| 87 | } | 151 | } |
| 88 | 152 | ||
| 89 | seq_macro::seq!(N in 8..=127 { | 153 | seq_macro::seq!(N in 8..=127 { |
| 154 | /// Multiplication factor for the PLL VCO input clock. | ||
| 90 | #[derive(Clone, Copy)] | 155 | #[derive(Clone, Copy)] |
| 91 | pub enum PllN { | 156 | pub enum PllN { |
| 92 | #( | 157 | #( |
| @@ -95,6 +160,7 @@ seq_macro::seq!(N in 8..=127 { | |||
| 95 | } | 160 | } |
| 96 | 161 | ||
| 97 | impl From<PllN> for u8 { | 162 | impl From<PllN> for u8 { |
| 163 | /// Returns the register value for the N multiplication factor. | ||
| 98 | fn from(val: PllN) -> u8 { | 164 | fn from(val: PllN) -> u8 { |
| 99 | match val { | 165 | match val { |
| 100 | #( | 166 | #( |
| @@ -105,6 +171,7 @@ seq_macro::seq!(N in 8..=127 { | |||
| 105 | } | 171 | } |
| 106 | 172 | ||
| 107 | impl PllN { | 173 | impl PllN { |
| 174 | /// Returns the numeric value of the N multiplication factor. | ||
| 108 | pub fn to_mul(self) -> u32 { | 175 | pub fn to_mul(self) -> u32 { |
| 109 | match self { | 176 | match self { |
| 110 | #( | 177 | #( |
| @@ -115,7 +182,7 @@ seq_macro::seq!(N in 8..=127 { | |||
| 115 | } | 182 | } |
| 116 | }); | 183 | }); |
| 117 | 184 | ||
| 118 | // Pre-division | 185 | /// PLL Pre-division. This must be set such that the PLL input is between 2.66 MHz and 16 MHz. |
| 119 | #[derive(Copy, Clone)] | 186 | #[derive(Copy, Clone)] |
| 120 | pub enum PllM { | 187 | pub enum PllM { |
| 121 | Div1, | 188 | Div1, |
| @@ -137,6 +204,7 @@ pub enum PllM { | |||
| 137 | } | 204 | } |
| 138 | 205 | ||
| 139 | impl PllM { | 206 | impl PllM { |
| 207 | /// Returns the numeric value of the M pre-division. | ||
| 140 | pub fn to_div(self) -> u32 { | 208 | pub fn to_div(self) -> u32 { |
| 141 | let val: u8 = self.into(); | 209 | let val: u8 = self.into(); |
| 142 | val as u32 + 1 | 210 | val as u32 + 1 |
| @@ -144,6 +212,7 @@ impl PllM { | |||
| 144 | } | 212 | } |
| 145 | 213 | ||
| 146 | impl From<PllM> for u8 { | 214 | impl From<PllM> for u8 { |
| 215 | /// Returns the register value for the M pre-division. | ||
| 147 | fn from(val: PllM) -> u8 { | 216 | fn from(val: PllM) -> u8 { |
| 148 | match val { | 217 | match val { |
| 149 | PllM::Div1 => 0b0000, | 218 | PllM::Div1 => 0b0000, |
| @@ -166,6 +235,31 @@ impl From<PllM> for u8 { | |||
| 166 | } | 235 | } |
| 167 | } | 236 | } |
| 168 | 237 | ||
| 238 | /// PLL Configuration | ||
| 239 | /// | ||
| 240 | /// Use this struct to configure the PLL source, input frequency, multiplication factor, and output | ||
| 241 | /// dividers. Be sure to keep check the datasheet for your specific part for the appropriate | ||
| 242 | /// frequency ranges for each of these settings. | ||
| 243 | pub struct Pll { | ||
| 244 | /// PLL Source clock selection. | ||
| 245 | pub source: PllSrc, | ||
| 246 | |||
| 247 | /// PLL pre-divider | ||
| 248 | pub prediv_m: PllM, | ||
| 249 | |||
| 250 | /// PLL multiplication factor for VCO | ||
| 251 | pub mul_n: PllN, | ||
| 252 | |||
| 253 | /// PLL division factor for P clock (ADC Clock) | ||
| 254 | pub div_p: Option<PllP>, | ||
| 255 | |||
| 256 | /// PLL division factor for Q clock (USB, I2S23, SAI1, FDCAN, QSPI) | ||
| 257 | pub div_q: Option<PllQ>, | ||
| 258 | |||
| 259 | /// PLL division factor for R clock (SYSCLK) | ||
| 260 | pub div_r: Option<PllR>, | ||
| 261 | } | ||
| 262 | |||
| 169 | impl AHBPrescaler { | 263 | impl AHBPrescaler { |
| 170 | const fn div(self) -> u32 { | 264 | const fn div(self) -> u32 { |
| 171 | match self { | 265 | match self { |
| @@ -229,6 +323,9 @@ pub struct Config { | |||
| 229 | pub apb1_pre: APBPrescaler, | 323 | pub apb1_pre: APBPrescaler, |
| 230 | pub apb2_pre: APBPrescaler, | 324 | pub apb2_pre: APBPrescaler, |
| 231 | pub low_power_run: bool, | 325 | pub low_power_run: bool, |
| 326 | /// Iff PLL is requested as the main clock source in the `mux` field then the PLL configuration | ||
| 327 | /// MUST turn on the PLLR output. | ||
| 328 | pub pll: Option<Pll>, | ||
| 232 | } | 329 | } |
| 233 | 330 | ||
| 234 | impl Default for Config { | 331 | impl Default for Config { |
| @@ -240,11 +337,80 @@ impl Default for Config { | |||
| 240 | apb1_pre: APBPrescaler::NotDivided, | 337 | apb1_pre: APBPrescaler::NotDivided, |
| 241 | apb2_pre: APBPrescaler::NotDivided, | 338 | apb2_pre: APBPrescaler::NotDivided, |
| 242 | low_power_run: false, | 339 | low_power_run: false, |
| 340 | pll: None, | ||
| 243 | } | 341 | } |
| 244 | } | 342 | } |
| 245 | } | 343 | } |
| 246 | 344 | ||
| 345 | pub struct PllFreq { | ||
| 346 | pub pll_p: Option<Hertz>, | ||
| 347 | pub pll_q: Option<Hertz>, | ||
| 348 | pub pll_r: Option<Hertz>, | ||
| 349 | } | ||
| 350 | |||
| 247 | pub(crate) unsafe fn init(config: Config) { | 351 | pub(crate) unsafe fn init(config: Config) { |
| 352 | let pll_freq = config.pll.map(|pll_config| { | ||
| 353 | let src_freq = match pll_config.source { | ||
| 354 | PllSrc::HSI16 => { | ||
| 355 | RCC.cr().write(|w| w.set_hsion(true)); | ||
| 356 | while !RCC.cr().read().hsirdy() {} | ||
| 357 | |||
| 358 | HSI_FREQ.0 | ||
| 359 | } | ||
| 360 | PllSrc::HSE(freq) => { | ||
| 361 | RCC.cr().write(|w| w.set_hseon(true)); | ||
| 362 | while !RCC.cr().read().hserdy() {} | ||
| 363 | freq.0 | ||
| 364 | } | ||
| 365 | }; | ||
| 366 | |||
| 367 | // Disable PLL before configuration | ||
| 368 | RCC.cr().modify(|w| w.set_pllon(false)); | ||
| 369 | while RCC.cr().read().pllrdy() {} | ||
| 370 | |||
| 371 | let internal_freq = src_freq / pll_config.prediv_m.to_div() * pll_config.mul_n.to_mul(); | ||
| 372 | |||
| 373 | RCC.pllcfgr().write(|w| { | ||
| 374 | w.set_plln(pll_config.mul_n.into()); | ||
| 375 | w.set_pllm(pll_config.prediv_m.into()); | ||
| 376 | w.set_pllsrc(pll_config.source.into()); | ||
| 377 | }); | ||
| 378 | |||
| 379 | let pll_p_freq = pll_config.div_p.map(|div_p| { | ||
| 380 | RCC.pllcfgr().modify(|w| { | ||
| 381 | w.set_pllpdiv(div_p.into()); | ||
| 382 | w.set_pllpen(true); | ||
| 383 | }); | ||
| 384 | Hertz(internal_freq / div_p.to_div()) | ||
| 385 | }); | ||
| 386 | |||
| 387 | let pll_q_freq = pll_config.div_q.map(|div_q| { | ||
| 388 | RCC.pllcfgr().modify(|w| { | ||
| 389 | w.set_pllq(div_q.into()); | ||
| 390 | w.set_pllqen(true); | ||
| 391 | }); | ||
| 392 | Hertz(internal_freq / div_q.to_div()) | ||
| 393 | }); | ||
| 394 | |||
| 395 | let pll_r_freq = pll_config.div_r.map(|div_r| { | ||
| 396 | RCC.pllcfgr().modify(|w| { | ||
| 397 | w.set_pllr(div_r.into()); | ||
| 398 | w.set_pllren(true); | ||
| 399 | }); | ||
| 400 | Hertz(internal_freq / div_r.to_div()) | ||
| 401 | }); | ||
| 402 | |||
| 403 | // Enable the PLL | ||
| 404 | RCC.cr().modify(|w| w.set_pllon(true)); | ||
| 405 | while !RCC.cr().read().pllrdy() {} | ||
| 406 | |||
| 407 | PllFreq { | ||
| 408 | pll_p: pll_p_freq, | ||
| 409 | pll_q: pll_q_freq, | ||
| 410 | pll_r: pll_r_freq, | ||
| 411 | } | ||
| 412 | }); | ||
| 413 | |||
| 248 | let (sys_clk, sw) = match config.mux { | 414 | let (sys_clk, sw) = match config.mux { |
| 249 | ClockSrc::HSI16 => { | 415 | ClockSrc::HSI16 => { |
| 250 | // Enable HSI16 | 416 | // Enable HSI16 |
| @@ -260,29 +426,12 @@ pub(crate) unsafe fn init(config: Config) { | |||
| 260 | 426 | ||
| 261 | (freq.0, Sw::HSE) | 427 | (freq.0, Sw::HSE) |
| 262 | } | 428 | } |
| 263 | ClockSrc::PLLCLK(src, prediv, mul, div) => { | 429 | ClockSrc::PLL => { |
| 264 | let src_freq = match src { | 430 | assert!(pll_freq.is_some()); |
| 265 | PllSrc::HSI16 => { | 431 | assert!(pll_freq.as_ref().unwrap().pll_r.is_some()); |
| 266 | // Enable HSI16 as clock source for PLL | ||
| 267 | RCC.cr().write(|w| w.set_hsion(true)); | ||
| 268 | while !RCC.cr().read().hsirdy() {} | ||
| 269 | |||
| 270 | HSI_FREQ.0 | ||
| 271 | } | ||
| 272 | PllSrc::HSE(freq) => { | ||
| 273 | // Enable HSE as clock source for PLL | ||
| 274 | RCC.cr().write(|w| w.set_hseon(true)); | ||
| 275 | while !RCC.cr().read().hserdy() {} | ||
| 276 | |||
| 277 | freq.0 | ||
| 278 | } | ||
| 279 | }; | ||
| 280 | 432 | ||
| 281 | // Make sure PLL is disabled while we configure it | 433 | let freq = pll_freq.unwrap().pll_r.unwrap().0; |
| 282 | RCC.cr().modify(|w| w.set_pllon(false)); | ||
| 283 | while RCC.cr().read().pllrdy() {} | ||
| 284 | 434 | ||
| 285 | let freq = src_freq / prediv.to_div() * mul.to_mul() / div.to_div(); | ||
| 286 | assert!(freq <= 170_000_000); | 435 | assert!(freq <= 170_000_000); |
| 287 | 436 | ||
| 288 | if freq >= 150_000_000 { | 437 | if freq >= 150_000_000 { |
| @@ -316,18 +465,6 @@ pub(crate) unsafe fn init(config: Config) { | |||
| 316 | } | 465 | } |
| 317 | } | 466 | } |
| 318 | 467 | ||
| 319 | RCC.pllcfgr().write(move |w| { | ||
| 320 | w.set_plln(mul.into()); | ||
| 321 | w.set_pllm(prediv.into()); | ||
| 322 | w.set_pllr(div.into()); | ||
| 323 | w.set_pllsrc(src.into()); | ||
| 324 | }); | ||
| 325 | |||
| 326 | // Enable PLL | ||
| 327 | RCC.cr().modify(|w| w.set_pllon(true)); | ||
| 328 | while !RCC.cr().read().pllrdy() {} | ||
| 329 | RCC.pllcfgr().modify(|w| w.set_pllren(true)); | ||
| 330 | |||
| 331 | (freq, Sw::PLLRCLK) | 468 | (freq, Sw::PLLRCLK) |
| 332 | } | 469 | } |
| 333 | }; | 470 | }; |
diff --git a/examples/stm32g4/Cargo.toml b/examples/stm32g4/Cargo.toml index f94df2dd3..fbfbc6408 100644 --- a/examples/stm32g4/Cargo.toml +++ b/examples/stm32g4/Cargo.toml | |||
| @@ -10,6 +10,7 @@ embassy-executor = { version = "0.2.0", path = "../../embassy-executor", feature | |||
| 10 | embassy-time = { version = "0.1.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime", "tick-hz-32_768"] } | 10 | embassy-time = { version = "0.1.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime", "tick-hz-32_768"] } |
| 11 | embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "defmt", "time-driver-any", "stm32g491re", "memory-x", "unstable-pac", "exti"] } | 11 | embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "defmt", "time-driver-any", "stm32g491re", "memory-x", "unstable-pac", "exti"] } |
| 12 | embassy-hal-common = {version = "0.1.0", path = "../../embassy-hal-common" } | 12 | embassy-hal-common = {version = "0.1.0", path = "../../embassy-hal-common" } |
| 13 | embassy-usb = { version = "0.1.0", path = "../../embassy-usb", features = ["defmt"] } | ||
| 13 | 14 | ||
| 14 | defmt = "0.3" | 15 | defmt = "0.3" |
| 15 | defmt-rtt = "0.4" | 16 | defmt-rtt = "0.4" |
diff --git a/examples/stm32g4/src/bin/pll.rs b/examples/stm32g4/src/bin/pll.rs index 8cee41e9b..ef7d4800c 100644 --- a/examples/stm32g4/src/bin/pll.rs +++ b/examples/stm32g4/src/bin/pll.rs | |||
| @@ -4,7 +4,7 @@ | |||
| 4 | 4 | ||
| 5 | use defmt::*; | 5 | use defmt::*; |
| 6 | use embassy_executor::Spawner; | 6 | use embassy_executor::Spawner; |
| 7 | use embassy_stm32::rcc::{ClockSrc, PllM, PllN, PllR, PllSrc}; | 7 | use embassy_stm32::rcc::{ClockSrc, Pll, PllM, PllN, PllR, PllSrc}; |
| 8 | use embassy_stm32::Config; | 8 | use embassy_stm32::Config; |
| 9 | use embassy_time::{Duration, Timer}; | 9 | use embassy_time::{Duration, Timer}; |
| 10 | use {defmt_rtt as _, panic_probe as _}; | 10 | use {defmt_rtt as _, panic_probe as _}; |
| @@ -13,8 +13,17 @@ use {defmt_rtt as _, panic_probe as _}; | |||
| 13 | async fn main(_spawner: Spawner) { | 13 | async fn main(_spawner: Spawner) { |
| 14 | let mut config = Config::default(); | 14 | let mut config = Config::default(); |
| 15 | 15 | ||
| 16 | // Configure PLL to max frequency of 170 MHz | 16 | config.rcc.pll = Some(Pll { |
| 17 | config.rcc.mux = ClockSrc::PLLCLK(PllSrc::HSI16, PllM::Div4, PllN::Mul85, PllR::Div2); | 17 | source: PllSrc::HSI16, |
| 18 | prediv_m: PllM::Div4, | ||
| 19 | mul_n: PllN::Mul85, | ||
| 20 | div_p: None, | ||
| 21 | div_q: None, | ||
| 22 | // Main system clock at 170 MHz | ||
| 23 | div_r: Some(PllR::Div2), | ||
| 24 | }); | ||
| 25 | |||
| 26 | config.rcc.mux = ClockSrc::PLL; | ||
| 18 | 27 | ||
| 19 | let _p = embassy_stm32::init(config); | 28 | let _p = embassy_stm32::init(config); |
| 20 | info!("Hello World!"); | 29 | info!("Hello World!"); |
diff --git a/examples/stm32g4/src/bin/usb_serial.rs b/examples/stm32g4/src/bin/usb_serial.rs new file mode 100644 index 000000000..ecbe3a6e6 --- /dev/null +++ b/examples/stm32g4/src/bin/usb_serial.rs | |||
| @@ -0,0 +1,110 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | |||
| 5 | use defmt::{panic, *}; | ||
| 6 | use embassy_executor::Spawner; | ||
| 7 | use embassy_stm32::rcc::{ClockSrc, Pll, PllM, PllN, PllQ, PllR, PllSrc}; | ||
| 8 | use embassy_stm32::time::Hertz; | ||
| 9 | use embassy_stm32::usb::{self, Driver, Instance}; | ||
| 10 | use embassy_stm32::{bind_interrupts, pac, peripherals, Config}; | ||
| 11 | use embassy_usb::class::cdc_acm::{CdcAcmClass, State}; | ||
| 12 | use embassy_usb::driver::EndpointError; | ||
| 13 | use embassy_usb::Builder; | ||
| 14 | use futures::future::join; | ||
| 15 | use {defmt_rtt as _, panic_probe as _}; | ||
| 16 | |||
| 17 | bind_interrupts!(struct Irqs { | ||
| 18 | USB_LP => usb::InterruptHandler<peripherals::USB>; | ||
| 19 | }); | ||
| 20 | |||
| 21 | #[embassy_executor::main] | ||
| 22 | async fn main(_spawner: Spawner) { | ||
| 23 | let mut config = Config::default(); | ||
| 24 | |||
| 25 | config.rcc.pll = Some(Pll { | ||
| 26 | source: PllSrc::HSE(Hertz(8000000)), | ||
| 27 | prediv_m: PllM::Div2, | ||
| 28 | mul_n: PllN::Mul72, | ||
| 29 | div_p: None, | ||
| 30 | // USB and CAN at 48 MHz | ||
| 31 | div_q: Some(PllQ::Div6), | ||
| 32 | // Main system clock at 144 MHz | ||
| 33 | div_r: Some(PllR::Div2), | ||
| 34 | }); | ||
| 35 | |||
| 36 | config.rcc.mux = ClockSrc::PLL; | ||
| 37 | |||
| 38 | let p = embassy_stm32::init(config); | ||
| 39 | info!("Hello World!"); | ||
| 40 | |||
| 41 | unsafe { | ||
| 42 | pac::RCC.ccipr().write(|w| w.set_clk48sel(0b10)); | ||
| 43 | } | ||
| 44 | |||
| 45 | let driver = Driver::new(p.USB, Irqs, p.PA12, p.PA11); | ||
| 46 | |||
| 47 | let mut config = embassy_usb::Config::new(0xc0de, 0xcafe); | ||
| 48 | config.manufacturer = Some("Embassy"); | ||
| 49 | config.product = Some("USB-Serial Example"); | ||
| 50 | config.serial_number = Some("123456"); | ||
| 51 | |||
| 52 | config.device_class = 0xEF; | ||
| 53 | config.device_sub_class = 0x02; | ||
| 54 | config.device_protocol = 0x01; | ||
| 55 | config.composite_with_iads = true; | ||
| 56 | |||
| 57 | let mut device_descriptor = [0; 256]; | ||
| 58 | let mut config_descriptor = [0; 256]; | ||
| 59 | let mut bos_descriptor = [0; 256]; | ||
| 60 | let mut control_buf = [0; 64]; | ||
| 61 | |||
| 62 | let mut state = State::new(); | ||
| 63 | |||
| 64 | let mut builder = Builder::new( | ||
| 65 | driver, | ||
| 66 | config, | ||
| 67 | &mut device_descriptor, | ||
| 68 | &mut config_descriptor, | ||
| 69 | &mut bos_descriptor, | ||
| 70 | &mut control_buf, | ||
| 71 | ); | ||
| 72 | |||
| 73 | let mut class = CdcAcmClass::new(&mut builder, &mut state, 64); | ||
| 74 | |||
| 75 | let mut usb = builder.build(); | ||
| 76 | |||
| 77 | let usb_fut = usb.run(); | ||
| 78 | |||
| 79 | let echo_fut = async { | ||
| 80 | loop { | ||
| 81 | class.wait_connection().await; | ||
| 82 | info!("Connected"); | ||
| 83 | let _ = echo(&mut class).await; | ||
| 84 | info!("Disconnected"); | ||
| 85 | } | ||
| 86 | }; | ||
| 87 | |||
| 88 | join(usb_fut, echo_fut).await; | ||
| 89 | } | ||
| 90 | |||
| 91 | struct Disconnected {} | ||
| 92 | |||
| 93 | impl From<EndpointError> for Disconnected { | ||
| 94 | fn from(val: EndpointError) -> Self { | ||
| 95 | match val { | ||
| 96 | EndpointError::BufferOverflow => panic!("Buffer overflow"), | ||
| 97 | EndpointError::Disabled => Disconnected {}, | ||
| 98 | } | ||
| 99 | } | ||
| 100 | } | ||
| 101 | |||
| 102 | async fn echo<'d, T: Instance + 'd>(class: &mut CdcAcmClass<'d, Driver<'d, T>>) -> Result<(), Disconnected> { | ||
| 103 | let mut buf = [0; 64]; | ||
| 104 | loop { | ||
| 105 | let n = class.read_packet(&mut buf).await?; | ||
| 106 | let data = &buf[..n]; | ||
| 107 | info!("data: {:x}", data); | ||
| 108 | class.write_packet(data).await?; | ||
| 109 | } | ||
| 110 | } | ||
