-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ceef590
commit 8b743e5
Showing
4 changed files
with
123 additions
and
0 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,40 @@ | ||
//! Configure the MCO (Microcontroller Clock Output) to give a 2MHz signal on PA8 and PA9, sourced | ||
//! from the internal HSI16 16MHz oscillator. | ||
#![deny(unsafe_code)] | ||
#![no_main] | ||
#![no_std] | ||
|
||
extern crate panic_halt; | ||
|
||
use cortex_m_rt::entry; | ||
use stm32l0xx_hal::{ | ||
pac::{ | ||
self, | ||
rcc::cfgr::{MCOPRE_A, MCOSEL_A}, | ||
}, | ||
prelude::*, | ||
rcc::Config, | ||
}; | ||
|
||
#[entry] | ||
fn main() -> ! { | ||
let dp = pac::Peripherals::take().unwrap(); | ||
|
||
// Configure the 16MHz internal clock | ||
let mut rcc = dp.RCC.freeze(Config::hsi16()); | ||
|
||
let gpioa = dp.GPIOA.split(&mut rcc); | ||
|
||
// Source MCO from HSI16, configure prescaler to divide by 8 to get 2MHz output. | ||
rcc.configure_mco(MCOSEL_A::HSI16, MCOPRE_A::DIV8, (gpioa.pa8, gpioa.pa9)); | ||
|
||
// Individual pins can also be set by passing them directly: | ||
// rcc.enable_mco(MCOSEL_A::HSI16, MCOPRE_A::DIV8, gpioa.pa8); | ||
|
||
// Or for larger devices, all 3 MCO pins can be configured: | ||
// rcc.configure_mco(MCOSEL_A::HSI16, MCOPRE_A::DIV8, (gpioa.pa8, gpioa.pa9, gpiob.pb13)); | ||
|
||
// Probe PA8 or PA9 to see generated 2MHz MCO signal. | ||
loop {} | ||
} |
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
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,52 @@ | ||
//! MCO (Microcontroller Clock Output) | ||
//! | ||
//! MCO is available on PA8 or PA9. See "Table 16. Alternate function port A" in the datasheet. | ||
use crate::gpio::{gpioa, gpiob, AltMode, Analog}; | ||
|
||
pub trait Pin { | ||
fn into_mco(self); | ||
} | ||
|
||
impl Pin for gpioa::PA8<Analog> { | ||
fn into_mco(self) { | ||
self.set_alt_mode(AltMode::AF0); | ||
} | ||
} | ||
|
||
impl Pin for gpioa::PA9<Analog> { | ||
fn into_mco(self) { | ||
self.set_alt_mode(AltMode::AF0); | ||
} | ||
} | ||
|
||
impl Pin for gpiob::PB13<Analog> { | ||
fn into_mco(self) { | ||
self.set_alt_mode(AltMode::AF2); | ||
} | ||
} | ||
|
||
// Blanket impls to allow configuration of all MCO pins. | ||
impl<P1, P2> Pin for (P1, P2) | ||
where | ||
P1: Pin, | ||
P2: Pin, | ||
{ | ||
fn into_mco(self) { | ||
self.0.into_mco(); | ||
self.1.into_mco(); | ||
} | ||
} | ||
|
||
impl<P1, P2, P3> Pin for (P1, P2, P3) | ||
where | ||
P1: Pin, | ||
P2: Pin, | ||
P3: Pin, | ||
{ | ||
fn into_mco(self) { | ||
self.0.into_mco(); | ||
self.1.into_mco(); | ||
self.2.into_mco(); | ||
} | ||
} |
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