Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

F030 HSI DIV support #14

Merged
merged 3 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ readme = "README.md"
license = "MIT OR Apache-2.0"

[dependencies]
py32-metapac = "0.1.0"
# py32-metapac = "0.1.0"
# py32-metapac = { path = "../py32-data/build/py32-metapac"}
py32-metapac = { git = "https://github.com/py32-rs/py32-metapac.git" }


cortex-m = { version = "0.7.7", features = [
"critical-section-single-core",
Expand Down Expand Up @@ -59,7 +61,8 @@ embassy-embedded-hal = { version = "0.2.0", default-features = false }

[build-dependencies]
# py32-metapac = { path = "../py32-data/build/py32-metapac", default-features = false, features = [
py32-metapac = { version = "0.1.0", default-features = false, features = [
# py32-metapac = { version = "0.1.0", default-features = false, features = [
py32-metapac = { git = "https://github.com/py32-rs/py32-metapac.git", default-features = false, features = [
"metadata",
] }

Expand Down
33 changes: 33 additions & 0 deletions examples/py32f030/src/bin/rcc_2mhz.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#![no_std]
#![no_main]
#![feature(impl_trait_in_assoc_type)]

use defmt::*;
use py32_hal::gpio::{Level, Output, Speed};
use py32_hal::rcc::Hsidiv;
use embassy_executor::Spawner;
use {defmt_rtt as _, panic_halt as _};


#[embassy_executor::main]
async fn main(_spawner: Spawner) {
let mut cfg: py32_hal::Config = Default::default();
// cfg.rcc.hsi = Some(Hertz::mhz(8)); // default
cfg.rcc.hsidiv = Hsidiv::DIV4;
// cfg.rcc.sys = Sysclk::HSI; // default
let p = py32_hal::init(cfg);

info!("Hello World!");

let mut led = Output::new(p.PA6, Level::High, Speed::Low);

loop {
info!("high");
led.set_high();
cortex_m::asm::delay(2_000_000);

info!("low");
led.set_low();
cortex_m::asm::delay(2_000_000);
}
}
9 changes: 5 additions & 4 deletions examples/py32f030/src/bin/rcc_48mhz.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
#![no_std]
#![no_main]
#![feature(impl_trait_in_assoc_type)]

use cortex_m_rt::entry;
use defmt::*;
use py32_hal::gpio::{Level, Output, Speed};
use py32_hal::rcc::{Pll, PllSource, Sysclk};
use py32_hal::time::Hertz;
use embassy_executor::Spawner;
use {defmt_rtt as _, panic_halt as _};

#[entry]
fn main() -> ! {

#[embassy_executor::main]
async fn main(_spawner: Spawner) {
let mut cfg: py32_hal::Config = Default::default();
cfg.rcc.hsi = Some(Hertz::mhz(24));
cfg.rcc.pll = Some(Pll {
Expand All @@ -29,7 +31,6 @@ fn main() -> ! {

info!("low");
led.set_low();

cortex_m::asm::delay(8_000_000);
}
}
16 changes: 14 additions & 2 deletions src/rcc/f030.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// use crate::pac::flash::vals::Latency;
use crate::pac::rcc::vals::Pllsrc;
// pub use crate::pac::rcc::vals::Prediv as PllPreDiv;
pub use crate::pac::rcc::vals::{Hpre as AHBPrescaler, Ppre as APBPrescaler, Sw as Sysclk, HsiFs};
pub use crate::pac::rcc::vals::{Hpre as AHBPrescaler, Ppre as APBPrescaler, Sw as Sysclk, HsiFs, Hsidiv};
use crate::pac::{/* FLASH , */RCC};
use crate::time::Hertz;

Expand All @@ -24,6 +24,14 @@ pub struct Hse {
pub mode: HseMode,
}

#[derive(Clone, Copy, Eq, PartialEq)]
pub struct Hsi {
/// HSE frequency.
pub freq: Hertz,
/// HSE mode.
pub mode: HseMode,
}

#[derive(Clone, Copy, Eq, PartialEq)]
pub enum PllSource {
HSE,
Expand All @@ -48,6 +56,7 @@ pub struct Pll {
#[derive(Clone, Copy)]
pub struct Config {
pub hsi: Option<Hertz>,
pub hsidiv: Hsidiv,
pub hse: Option<Hse>,
pub sys: Sysclk,

Expand All @@ -67,6 +76,7 @@ impl Default for Config {
hsi: Some(Hertz::mhz(8)),
hse: None,
sys: Sysclk::HSI,
hsidiv: Hsidiv::DIV1,
pll: None,
ahb_pre: AHBPrescaler::DIV1,
apb1_pre: APBPrescaler::DIV1,
Expand Down Expand Up @@ -99,6 +109,8 @@ pub(crate) unsafe fn init(config: Config) {
RCC.cfgr().modify(|w| w.set_sw(Sysclk::HSI));
while RCC.cfgr().read().sws() != Sysclk::HSI {}

RCC.cr().modify(|w| w.set_hsidiv(config.hsidiv));

// Configure HSI
let hsi = config.hsi;

Expand Down Expand Up @@ -155,7 +167,7 @@ pub(crate) unsafe fn init(config: Config) {

// Configure sysclk
let sys = match config.sys {
Sysclk::HSI => unwrap!(hsi),
Sysclk::HSI => unwrap!(hsi) / config.hsidiv,
Sysclk::HSE => unwrap!(hse),
Sysclk::PLL => unwrap!(pll),
_ => unreachable!(),
Expand Down
Loading