-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from MabezDev/pll-config
RCC/PLL:
- Loading branch information
Showing
2 changed files
with
138 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
//! Test the serial interface | ||
//! | ||
//! This example requires you to short (connect) the TX and RX pins. | ||
#![deny(unsafe_code)] | ||
#![deny(warnings)] | ||
#![no_main] | ||
#![no_std] | ||
|
||
extern crate cortex_m; | ||
#[macro_use(entry, exception)] | ||
extern crate cortex_m_rt as rt; | ||
#[macro_use(block)] | ||
extern crate nb; | ||
extern crate panic_semihosting; | ||
|
||
extern crate stm32l432xx_hal as hal; | ||
// #[macro_use(block)] | ||
// extern crate nb; | ||
|
||
use cortex_m::asm; | ||
use hal::prelude::*; | ||
use hal::serial::Serial; | ||
use hal::stm32l4::stm32l4x2; | ||
use hal::rcc::PllConfig; | ||
use rt::ExceptionFrame; | ||
|
||
entry!(main); | ||
|
||
fn main() -> ! { | ||
let p = stm32l4x2::Peripherals::take().unwrap(); | ||
|
||
let mut flash = p.FLASH.constrain(); | ||
let mut rcc = p.RCC.constrain(); | ||
let mut gpioa = p.GPIOA.split(&mut rcc.ahb2); | ||
// let mut gpiob = p.GPIOB.split(&mut rcc.ahb2); | ||
|
||
// clock configuration using the default settings (all clocks run at 8 MHz) | ||
// let clocks = rcc.cfgr.freeze(&mut flash.acr); | ||
// TRY this alternate clock configuration (clocks run at nearly the maximum frequency) | ||
// let clocks = rcc.cfgr.sysclk(80.mhz()).pclk1(80.mhz()).pclk2(80.mhz()).freeze(&mut flash.acr); | ||
let plls = PllConfig { | ||
m: 0b001, // / 2 | ||
n: 0b1000, // * 8 | ||
r: 0b11 // /8 | ||
}; | ||
// NOTE: it is up to the user to make sure the pll config matches the given sysclk | ||
let clocks = rcc.cfgr.sysclk_with_pll(8.mhz(), plls).pclk1(8.mhz()).pclk2(8.mhz()).freeze(&mut flash.acr); | ||
|
||
// The Serial API is highly generic | ||
// TRY the commented out, different pin configurations | ||
let tx = gpioa.pa9.into_af7(&mut gpioa.moder, &mut gpioa.afrh); | ||
// let tx = gpiob.pb6.into_af7(&mut gpiob.moder, &mut gpiob.afrl); | ||
|
||
let rx = gpioa.pa10.into_af7(&mut gpioa.moder, &mut gpioa.afrh); | ||
// let rx = gpiob.pb7.into_af7(&mut gpiob.moder, &mut gpiob.afrl); | ||
|
||
// TRY using a different USART peripheral here | ||
let serial = Serial::usart1(p.USART1, (tx, rx), 9_600.bps(), clocks, &mut rcc.apb2); | ||
let (mut tx, mut rx) = serial.split(); | ||
|
||
let sent = b'X'; | ||
|
||
// The `block!` macro makes an operation block until it finishes | ||
// NOTE the error type is `!` | ||
|
||
block!(tx.write(sent)).ok(); | ||
|
||
let received = block!(rx.read()).unwrap(); | ||
|
||
assert_eq!(received, sent); | ||
|
||
// if all goes well you should reach this breakpoint | ||
asm::bkpt(); | ||
|
||
loop {} | ||
} | ||
|
||
exception!(HardFault, hard_fault); | ||
|
||
fn hard_fault(ef: &ExceptionFrame) -> ! { | ||
panic!("{:#?}", ef); | ||
} | ||
|
||
exception!(*, default_handler); | ||
|
||
fn default_handler(irqn: i16) { | ||
panic!("Unhandled exception (IRQn = {})", irqn); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters