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

Rcc improvement #420

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
11 changes: 4 additions & 7 deletions src/adc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use core::sync::atomic::{self, Ordering};
use cortex_m::asm::delay;
use embedded_dma::WriteBuffer;

use crate::pac::{self, RCC};
use crate::pac;

/// Continuous mode
pub struct Continuous;
Expand Down Expand Up @@ -271,18 +271,15 @@ macro_rules! adc_hal {
}

fn reset(&mut self) {
let rcc = unsafe { &(*RCC::ptr()) };
<$ADC>::reset(rcc);
self.rb.reset();
}

fn enable_clock(&mut self) {
let rcc = unsafe { &(*RCC::ptr()) };
<$ADC>::enable(rcc);
self.rb.enable();
}

fn disable_clock(&mut self) {
let rcc = unsafe { &(*RCC::ptr()) };
<$ADC>::disable(rcc);
self.rb.disable();
}

fn calibrate(&mut self) {
Expand Down
7 changes: 3 additions & 4 deletions src/afio.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! # Alternate Function I/Os
use crate::pac::{afio, AFIO, RCC};
use crate::pac::{afio, AFIO};

use crate::rcc::{Enable, Reset};

Expand All @@ -13,9 +13,8 @@ pub trait AfioExt {

impl AfioExt for AFIO {
fn constrain(self) -> Parts {
let rcc = unsafe { &(*RCC::ptr()) };
AFIO::enable(rcc);
AFIO::reset(rcc);
self.enable();
self.reset();

Parts {
evcr: EVCR { _0: () },
Expand Down
8 changes: 3 additions & 5 deletions src/can.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

use crate::afio::MAPR;
use crate::gpio::{self, Alternate, Input};
use crate::pac::{self, RCC};
use crate::pac;

pub trait Pins: crate::Sealed {
type Instance;
Expand Down Expand Up @@ -95,17 +95,15 @@ where
/// prevent accidental shared usage.
#[cfg(not(feature = "connectivity"))]
pub fn new(can: Instance, _usb: pac::USB) -> Can<Instance> {
let rcc = unsafe { &(*RCC::ptr()) };
Instance::enable(rcc);
can.enable();

Can { _peripheral: can }
}

/// Creates a CAN interaface.
#[cfg(feature = "connectivity")]
pub fn new(can: Instance) -> Can<Instance> {
let rcc = unsafe { &(*RCC::ptr()) };
Instance::enable(rcc);
can.enable();

Can { _peripheral: can }
}
Expand Down
5 changes: 2 additions & 3 deletions src/crc.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! CRC

use crate::pac::{CRC, RCC};
use crate::pac::CRC;
use crate::rcc::Enable;

/// Extension trait to constrain the CRC peripheral
Expand All @@ -12,8 +12,7 @@ pub trait CrcExt {

impl CrcExt for CRC {
fn new(self) -> Crc {
let rcc = unsafe { &(*RCC::ptr()) };
CRC::enable(rcc);
self.enable();

Crc { crc: self }
}
Expand Down
5 changes: 2 additions & 3 deletions src/dma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ macro_rules! dma {
pub mod $dmaX {
use core::{sync::atomic::{self, Ordering}, ptr, mem, convert::TryFrom};

use crate::pac::{RCC, $DMAX, dma1};
use crate::pac::{$DMAX, dma1};

use crate::dma::{CircBuffer, DmaExt, Error, Event, Half, Transfer, W, RxDma, TxDma, RxTxDma, TransferPayload};
use crate::rcc::Enable;
Expand Down Expand Up @@ -447,8 +447,7 @@ macro_rules! dma {
type Channels = Channels;

fn split(self) -> Channels {
let rcc = unsafe { &(*RCC::ptr()) };
$DMAX::enable(rcc);
self.enable();

// reset the DMA control registers (stops all on-going transfers)
$(
Expand Down
7 changes: 3 additions & 4 deletions src/gpio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ macro_rules! gpio {
]) => {
/// GPIO
pub mod $gpiox {
use crate::pac::{$GPIOX, RCC};
use crate::pac::$GPIOX;
use crate::rcc::{Enable, Reset};
use super::{Active, Floating, GpioExt, Input, PartiallyErasedPin, ErasedPin, Pin, Cr};
#[allow(unused)]
Expand All @@ -366,9 +366,8 @@ macro_rules! gpio {
type Parts = Parts;

fn split(self) -> Parts {
let rcc = unsafe { &(*RCC::ptr()) };
$GPIOX::enable(rcc);
$GPIOX::reset(rcc);
self.enable();
self.reset();

Parts {
crl: Cr::<$port_id, false>(()),
Expand Down
7 changes: 3 additions & 4 deletions src/i2c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use crate::afio::MAPR;
use crate::gpio::{self, Alternate, OpenDrain};
use crate::hal::blocking::i2c::{Read, Write, WriteRead};
use crate::pac::{DWT, I2C1, I2C2, RCC};
use crate::pac::{DWT, I2C1, I2C2};
use crate::rcc::{BusClock, Clocks, Enable, Reset};
use crate::time::{kHz, Hertz};
use core::ops::Deref;
Expand Down Expand Up @@ -166,9 +166,8 @@ where
/// Configures the I2C peripheral to work in master mode
fn configure<M: Into<Mode>>(i2c: I2C, pins: PINS, mode: M, clocks: Clocks) -> Self {
let mode = mode.into();
let rcc = unsafe { &(*RCC::ptr()) };
I2C::enable(rcc);
I2C::reset(rcc);
i2c.enable();
i2c.reset();

let pclk1 = I2C::clock(&clocks);

Expand Down
19 changes: 5 additions & 14 deletions src/rcc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,6 @@ impl APB1 {
}
}

impl APB1 {
/// Set power interface clock (PWREN) bit in RCC_APB1ENR
pub fn set_pwren() {
let rcc = unsafe { &*RCC::ptr() };
PWR::enable(rcc);
}
}

/// Advanced Peripheral Bus 2 (APB2) registers
pub struct APB2 {
_0: (),
Expand Down Expand Up @@ -309,9 +301,8 @@ impl BKP {
/// Enables write access to the registers in the backup domain
pub fn constrain(self, bkp: crate::pac::BKP, pwr: &mut PWR) -> BackupDomain {
// Enable the backup interface by setting PWREN and BKPEN
let rcc = unsafe { &(*RCC::ptr()) };
crate::pac::BKP::enable(rcc);
crate::pac::PWR::enable(rcc);
bkp.enable();
pwr.enable();

// Enable access to the backup registers
pwr.cr.modify(|_r, w| w.dbp().set_bit());
Expand Down Expand Up @@ -470,12 +461,12 @@ pub trait RccBus: crate::Sealed {

/// Enable/disable peripheral
pub trait Enable: RccBus {
fn enable(rcc: &rcc::RegisterBlock);
fn disable(rcc: &rcc::RegisterBlock);
fn enable(&self);
fn disable(&self);
}
/// Reset peripheral
pub trait Reset: RccBus {
fn reset(rcc: &rcc::RegisterBlock);
fn reset(&self);
}

#[derive(Clone, Copy, Debug, PartialEq)]
Expand Down
15 changes: 10 additions & 5 deletions src/rcc/enable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,24 @@ macro_rules! bus {
}
impl Enable for crate::pac::$PER {
#[inline(always)]
fn enable(rcc: &rcc::RegisterBlock) {
fn enable(&self) {
let rcc = unsafe { &(*RCC::ptr()) };
unsafe {
bb::set(Self::Bus::enr(rcc), $bit);
}
}
#[inline(always)]
fn disable(rcc: &rcc::RegisterBlock) {
fn disable(&self) {
let rcc = unsafe { &(*RCC::ptr()) };
unsafe {
bb::clear(Self::Bus::enr(rcc), $bit);
}
}
}
impl Reset for crate::pac::$PER {
#[inline(always)]
fn reset(rcc: &rcc::RegisterBlock) {
fn reset(&self) {
let rcc = unsafe { &(*RCC::ptr()) };
unsafe {
bb::set(Self::Bus::rstr(rcc), $bit);
bb::clear(Self::Bus::rstr(rcc), $bit);
Expand All @@ -46,13 +49,15 @@ macro_rules! ahb_bus {
}
impl Enable for crate::pac::$PER {
#[inline(always)]
fn enable(rcc: &rcc::RegisterBlock) {
fn enable(&self) {
let rcc = unsafe { &(*RCC::ptr()) };
unsafe {
bb::set(Self::Bus::enr(rcc), $bit);
}
}
#[inline(always)]
fn disable(rcc: &rcc::RegisterBlock) {
fn disable(&self) {
let rcc = unsafe { &(*RCC::ptr()) };
unsafe {
bb::clear(Self::Bus::enr(rcc), $bit);
}
Expand Down
7 changes: 3 additions & 4 deletions src/serial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ use embedded_dma::{ReadBuffer, WriteBuffer};
use crate::afio::MAPR;
use crate::dma::{dma1, CircBuffer, RxDma, Transfer, TxDma, R, W};
use crate::gpio::{self, Alternate, Input};
use crate::pac::{RCC, USART1, USART2, USART3};
use crate::pac::{USART1, USART2, USART3};
use crate::rcc::{BusClock, Clocks, Enable, Reset};
use crate::time::{Bps, U32Ext};

Expand Down Expand Up @@ -292,9 +292,8 @@ impl<USART: Instance, PINS> Serial<USART, PINS> {
PINS: Pins<USART>,
{
// Enable and reset USART
let rcc = unsafe { &(*RCC::ptr()) };
USART::enable(rcc);
USART::reset(rcc);
usart.enable();
usart.reset();

PINS::remap(mapr);

Expand Down
12 changes: 5 additions & 7 deletions src/spi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use core::ops::Deref;
use core::ptr;

pub use crate::hal::spi::{FullDuplex, Mode, Phase, Polarity};
use crate::pac::{self, RCC};
use crate::pac;

use crate::afio::MAPR;
use crate::dma::dma1;
Expand Down Expand Up @@ -468,9 +468,8 @@ where
{
fn configure(spi: SPI, pins: PINS, mode: Mode, freq: Hertz, clocks: Clocks) -> Self {
// enable or reset SPI
let rcc = unsafe { &(*RCC::ptr()) };
SPI::enable(rcc);
SPI::reset(rcc);
spi.enable();
spi.reset();

// disable SS output
spi.cr2.write(|w| w.ssoe().clear_bit());
Expand Down Expand Up @@ -540,9 +539,8 @@ where
{
fn configure(spi: SPI, pins: PINS, mode: Mode) -> Self {
// enable or reset SPI
let rcc = unsafe { &(*RCC::ptr()) };
SPI::enable(rcc);
SPI::reset(rcc);
spi.enable();
spi.reset();

// disable SS output
spi.cr2.write(|w| w.ssoe().clear_bit());
Expand Down
22 changes: 7 additions & 15 deletions src/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
#![allow(non_upper_case_globals)]

use crate::bb;
use crate::pac::{self, DBGMCU as DBG, RCC};
use crate::pac::{self, DBGMCU as DBG};

use crate::rcc::{self, Clocks};
use core::convert::TryFrom;
Expand Down Expand Up @@ -636,13 +636,9 @@ macro_rules! with_pwm {
impl<TIM: Instance> Timer<TIM> {
/// Initialize timer
pub fn new(tim: TIM, clocks: &Clocks) -> Self {
unsafe {
//NOTE(unsafe) this reference will only be used for atomic writes with no side effects
let rcc = &(*RCC::ptr());
// Enable and reset the timer peripheral
TIM::enable(rcc);
TIM::reset(rcc);
}
// Enable and reset the timer peripheral
tim.enable();
tim.reset();

Self {
clk: TIM::timer_clock(clocks),
Expand Down Expand Up @@ -713,13 +709,9 @@ pub type FTimerMs<TIM> = FTimer<TIM, 1_000>;
impl<TIM: Instance, const FREQ: u32> FTimer<TIM, FREQ> {
/// Initialize timer
pub fn new(tim: TIM, clocks: &Clocks) -> Self {
unsafe {
//NOTE(unsafe) this reference will only be used for atomic writes with no side effects
let rcc = &(*RCC::ptr());
// Enable and reset the timer peripheral
TIM::enable(rcc);
TIM::reset(rcc);
}
// Enable and reset the timer peripheral
tim.enable();
tim.reset();

let mut t = Self { tim };
t.configure(clocks);
Expand Down
16 changes: 7 additions & 9 deletions src/usb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//! See https://github.com/stm32-rs/stm32f1xx-hal/tree/master/examples
//! for usage examples.

use crate::pac::{RCC, USB};
use crate::pac::USB;
use crate::rcc::{Enable, Reset};
use stm32_usbd::UsbPeripheral;

Expand All @@ -28,14 +28,12 @@ unsafe impl UsbPeripheral for Peripheral {
const EP_MEMORY_ACCESS_2X16: bool = false;

fn enable() {
unsafe {
let rcc = &*RCC::ptr();

// Enable USB peripheral
USB::enable(rcc);
// Reset USB peripheral
USB::reset(rcc);
}
// TODO: use self.usb, after adding the &self parameter
let usb = unsafe { crate::pac::Peripherals::steal().USB };
// Enable USB peripheral
usb.enable();
// Reset USB peripheral
usb.reset();
}

fn startup_delay() {
Expand Down