stm32: Register access for timers now doesn't require self

This commit is contained in:
Matous Hybl 2022-02-28 16:20:42 +01:00
parent 141e007acf
commit a88c5e716e
4 changed files with 47 additions and 56 deletions

View File

@ -99,7 +99,7 @@ macro_rules! impl_compare_capable_16bit {
mode: OutputCompareMode, mode: OutputCompareMode,
) { ) {
use crate::timer::sealed::GeneralPurpose16bitInstance; use crate::timer::sealed::GeneralPurpose16bitInstance;
let r = self.regs_gp16(); let r = Self::regs_gp16();
let raw_channel: usize = channel.raw(); let raw_channel: usize = channel.raw();
r.ccmr_output(raw_channel / 2) r.ccmr_output(raw_channel / 2)
.modify(|w| w.set_ocm(raw_channel % 2, mode.into())); .modify(|w| w.set_ocm(raw_channel % 2, mode.into()));
@ -107,21 +107,21 @@ macro_rules! impl_compare_capable_16bit {
unsafe fn enable_channel(&mut self, channel: Channel, enable: bool) { unsafe fn enable_channel(&mut self, channel: Channel, enable: bool) {
use crate::timer::sealed::GeneralPurpose16bitInstance; use crate::timer::sealed::GeneralPurpose16bitInstance;
self.regs_gp16() Self::regs_gp16()
.ccer() .ccer()
.modify(|w| w.set_cce(channel.raw(), enable)); .modify(|w| w.set_cce(channel.raw(), enable));
} }
unsafe fn set_compare_value(&mut self, channel: Channel, value: u16) { unsafe fn set_compare_value(&mut self, channel: Channel, value: u16) {
use crate::timer::sealed::GeneralPurpose16bitInstance; use crate::timer::sealed::GeneralPurpose16bitInstance;
self.regs_gp16() Self::regs_gp16()
.ccr(channel.raw()) .ccr(channel.raw())
.modify(|w| w.set_ccr(value)); .modify(|w| w.set_ccr(value));
} }
unsafe fn get_max_compare_value(&self) -> u16 { unsafe fn get_max_compare_value(&self) -> u16 {
use crate::timer::sealed::GeneralPurpose16bitInstance; use crate::timer::sealed::GeneralPurpose16bitInstance;
self.regs_gp16().arr().read().arr() Self::regs_gp16().arr().read().arr()
} }
} }
}; };
@ -136,7 +136,7 @@ foreach_interrupt! {
mode: OutputCompareMode, mode: OutputCompareMode,
) { ) {
use crate::timer::sealed::GeneralPurpose16bitInstance; use crate::timer::sealed::GeneralPurpose16bitInstance;
let r = self.regs_gp16(); let r = Self::regs_gp16();
let raw_channel: usize = channel.raw(); let raw_channel: usize = channel.raw();
r.ccmr_output(raw_channel / 2) r.ccmr_output(raw_channel / 2)
.modify(|w| w.set_ocm(raw_channel % 2, mode.into())); .modify(|w| w.set_ocm(raw_channel % 2, mode.into()));
@ -144,21 +144,21 @@ foreach_interrupt! {
unsafe fn enable_channel(&mut self, channel: Channel, enable: bool) { unsafe fn enable_channel(&mut self, channel: Channel, enable: bool) {
use crate::timer::sealed::GeneralPurpose16bitInstance; use crate::timer::sealed::GeneralPurpose16bitInstance;
self.regs_gp16() Self::regs_gp16()
.ccer() .ccer()
.modify(|w| w.set_cce(channel.raw(), enable)); .modify(|w| w.set_cce(channel.raw(), enable));
} }
unsafe fn set_compare_value(&mut self, channel: Channel, value: u16) { unsafe fn set_compare_value(&mut self, channel: Channel, value: u16) {
use crate::timer::sealed::GeneralPurpose16bitInstance; use crate::timer::sealed::GeneralPurpose16bitInstance;
self.regs_gp16() Self::regs_gp16()
.ccr(channel.raw()) .ccr(channel.raw())
.modify(|w| w.set_ccr(value)); .modify(|w| w.set_ccr(value));
} }
unsafe fn get_max_compare_value(&self) -> u16 { unsafe fn get_max_compare_value(&self) -> u16 {
use crate::timer::sealed::GeneralPurpose16bitInstance; use crate::timer::sealed::GeneralPurpose16bitInstance;
self.regs_gp16().arr().read().arr() Self::regs_gp16().arr().read().arr()
} }
} }
@ -177,22 +177,22 @@ foreach_interrupt! {
) { ) {
use crate::timer::sealed::GeneralPurpose32bitInstance; use crate::timer::sealed::GeneralPurpose32bitInstance;
let raw_channel = channel.raw(); let raw_channel = channel.raw();
self.regs_gp32().ccmr_output(raw_channel / 2).modify(|w| w.set_ocm(raw_channel % 2, mode.into())); Self::regs_gp32().ccmr_output(raw_channel / 2).modify(|w| w.set_ocm(raw_channel % 2, mode.into()));
} }
unsafe fn enable_channel(&mut self, channel: Channel, enable: bool) { unsafe fn enable_channel(&mut self, channel: Channel, enable: bool) {
use crate::timer::sealed::GeneralPurpose32bitInstance; use crate::timer::sealed::GeneralPurpose32bitInstance;
self.regs_gp32().ccer().modify(|w| w.set_cce(channel.raw(), enable)); Self::regs_gp32().ccer().modify(|w| w.set_cce(channel.raw(), enable));
} }
unsafe fn set_compare_value(&mut self, channel: Channel, value: u32) { unsafe fn set_compare_value(&mut self, channel: Channel, value: u32) {
use crate::timer::sealed::GeneralPurpose32bitInstance; use crate::timer::sealed::GeneralPurpose32bitInstance;
self.regs_gp32().ccr(channel.raw()).modify(|w| w.set_ccr(value)); Self::regs_gp32().ccr(channel.raw()).modify(|w| w.set_ccr(value));
} }
unsafe fn get_max_compare_value(&self) -> u32 { unsafe fn get_max_compare_value(&self) -> u32 {
use crate::timer::sealed::GeneralPurpose32bitInstance; use crate::timer::sealed::GeneralPurpose32bitInstance;
self.regs_gp32().arr().read().arr() as u32 Self::regs_gp32().arr().read().arr() as u32
} }
} }
impl CaptureCompare16bitInstance for crate::peripherals::$inst { impl CaptureCompare16bitInstance for crate::peripherals::$inst {
@ -211,7 +211,7 @@ foreach_interrupt! {
mode: OutputCompareMode, mode: OutputCompareMode,
) { ) {
use crate::timer::sealed::AdvancedControlInstance; use crate::timer::sealed::AdvancedControlInstance;
let r = self.regs_advanced(); let r = Self::regs_advanced();
let raw_channel: usize = channel.raw(); let raw_channel: usize = channel.raw();
r.ccmr_output(raw_channel / 2) r.ccmr_output(raw_channel / 2)
.modify(|w| w.set_ocm(raw_channel % 2, mode.into())); .modify(|w| w.set_ocm(raw_channel % 2, mode.into()));
@ -219,21 +219,21 @@ foreach_interrupt! {
unsafe fn enable_channel(&mut self, channel: Channel, enable: bool) { unsafe fn enable_channel(&mut self, channel: Channel, enable: bool) {
use crate::timer::sealed::AdvancedControlInstance; use crate::timer::sealed::AdvancedControlInstance;
self.regs_advanced() Self::regs_advanced()
.ccer() .ccer()
.modify(|w| w.set_cce(channel.raw(), enable)); .modify(|w| w.set_cce(channel.raw(), enable));
} }
unsafe fn set_compare_value(&mut self, channel: Channel, value: u16) { unsafe fn set_compare_value(&mut self, channel: Channel, value: u16) {
use crate::timer::sealed::AdvancedControlInstance; use crate::timer::sealed::AdvancedControlInstance;
self.regs_advanced() Self::regs_advanced()
.ccr(channel.raw()) .ccr(channel.raw())
.modify(|w| w.set_ccr(value)); .modify(|w| w.set_ccr(value));
} }
unsafe fn get_max_compare_value(&self) -> u16 { unsafe fn get_max_compare_value(&self) -> u16 {
use crate::timer::sealed::AdvancedControlInstance; use crate::timer::sealed::AdvancedControlInstance;
self.regs_advanced().arr().read().arr() Self::regs_advanced().arr().read().arr()
} }
} }

View File

@ -103,7 +103,6 @@ impl AlarmState {
} }
struct RtcDriver { struct RtcDriver {
timer: T,
/// Number of 2^15 periods elapsed since boot. /// Number of 2^15 periods elapsed since boot.
period: AtomicU32, period: AtomicU32,
alarm_count: AtomicU8, alarm_count: AtomicU8,
@ -114,7 +113,6 @@ struct RtcDriver {
const ALARM_STATE_NEW: AlarmState = AlarmState::new(); const ALARM_STATE_NEW: AlarmState = AlarmState::new();
embassy::time_driver_impl!(static DRIVER: RtcDriver = RtcDriver { embassy::time_driver_impl!(static DRIVER: RtcDriver = RtcDriver {
timer: unsafe { core::mem::transmute(()) }, // steal is not const
period: AtomicU32::new(0), period: AtomicU32::new(0),
alarm_count: AtomicU8::new(0), alarm_count: AtomicU8::new(0),
alarms: Mutex::const_new(CriticalSectionRawMutex::new(), [ALARM_STATE_NEW; ALARM_COUNT]), alarms: Mutex::const_new(CriticalSectionRawMutex::new(), [ALARM_STATE_NEW; ALARM_COUNT]),
@ -122,7 +120,7 @@ embassy::time_driver_impl!(static DRIVER: RtcDriver = RtcDriver {
impl RtcDriver { impl RtcDriver {
fn init(&'static self) { fn init(&'static self) {
let r = self.timer.regs_gp16(); let r = T::regs_gp16();
<T as RccPeripheral>::enable(); <T as RccPeripheral>::enable();
<T as RccPeripheral>::reset(); <T as RccPeripheral>::reset();
@ -163,7 +161,7 @@ impl RtcDriver {
} }
fn on_interrupt(&self) { fn on_interrupt(&self) {
let r = self.timer.regs_gp16(); let r = T::regs_gp16();
// NOTE(unsafe) Use critical section to access the methods // NOTE(unsafe) Use critical section to access the methods
// XXX: reduce the size of this critical section ? // XXX: reduce the size of this critical section ?
@ -194,7 +192,7 @@ impl RtcDriver {
} }
fn next_period(&self) { fn next_period(&self) {
let r = self.timer.regs_gp16(); let r = T::regs_gp16();
let period = self.period.fetch_add(1, Ordering::Relaxed) + 1; let period = self.period.fetch_add(1, Ordering::Relaxed) + 1;
let t = (period as u64) << 15; let t = (period as u64) << 15;
@ -236,7 +234,7 @@ impl RtcDriver {
impl Driver for RtcDriver { impl Driver for RtcDriver {
fn now(&self) -> u64 { fn now(&self) -> u64 {
let r = self.timer.regs_gp16(); let r = T::regs_gp16();
let period = self.period.load(Ordering::Relaxed); let period = self.period.load(Ordering::Relaxed);
compiler_fence(Ordering::Acquire); compiler_fence(Ordering::Acquire);
@ -273,7 +271,7 @@ impl Driver for RtcDriver {
fn set_alarm(&self, alarm: AlarmHandle, timestamp: u64) { fn set_alarm(&self, alarm: AlarmHandle, timestamp: u64) {
critical_section::with(|cs| { critical_section::with(|cs| {
let r = self.timer.regs_gp16(); let r = T::regs_gp16();
let n = alarm.id() as _; let n = alarm.id() as _;
let alarm = self.get_alarm(cs, alarm); let alarm = self.get_alarm(cs, alarm);

View File

@ -14,7 +14,7 @@ pub(crate) mod sealed {
pub trait Basic16bitInstance: RccPeripheral { pub trait Basic16bitInstance: RccPeripheral {
type Interrupt: Interrupt; type Interrupt: Interrupt;
fn regs(&self) -> crate::pac::timer::TimBasic; fn regs() -> crate::pac::timer::TimBasic;
fn start(&mut self); fn start(&mut self);
@ -30,17 +30,17 @@ pub(crate) mod sealed {
} }
pub trait GeneralPurpose16bitInstance: Basic16bitInstance { pub trait GeneralPurpose16bitInstance: Basic16bitInstance {
fn regs_gp16(&self) -> crate::pac::timer::TimGp16; fn regs_gp16() -> crate::pac::timer::TimGp16;
} }
pub trait GeneralPurpose32bitInstance: GeneralPurpose16bitInstance { pub trait GeneralPurpose32bitInstance: GeneralPurpose16bitInstance {
fn regs_gp32(&self) -> crate::pac::timer::TimGp32; fn regs_gp32() -> crate::pac::timer::TimGp32;
fn set_frequency<F: Into<Hertz>>(&mut self, frequency: F); fn set_frequency<F: Into<Hertz>>(&mut self, frequency: F);
} }
pub trait AdvancedControlInstance: Basic16bitInstance { pub trait AdvancedControlInstance: Basic16bitInstance {
fn regs_advanced(&self) -> crate::pac::timer::TimAdv; fn regs_advanced() -> crate::pac::timer::TimAdv;
} }
} }
@ -58,26 +58,25 @@ macro_rules! impl_basic_16bit_timer {
impl sealed::Basic16bitInstance for crate::peripherals::$inst { impl sealed::Basic16bitInstance for crate::peripherals::$inst {
type Interrupt = crate::interrupt::$irq; type Interrupt = crate::interrupt::$irq;
fn regs(&self) -> crate::pac::timer::TimBasic { fn regs() -> crate::pac::timer::TimBasic {
crate::pac::timer::TimBasic(crate::pac::$inst.0) crate::pac::timer::TimBasic(crate::pac::$inst.0)
} }
fn start(&mut self) { fn start(&mut self) {
unsafe { unsafe {
self.regs().cr1().modify(|r| r.set_cen(true)); Self::regs().cr1().modify(|r| r.set_cen(true));
} }
} }
fn stop(&mut self) { fn stop(&mut self) {
let regs = self.regs();
unsafe { unsafe {
regs.cr1().modify(|r| r.set_cen(false)); Self::regs().cr1().modify(|r| r.set_cen(false));
} }
} }
fn reset(&mut self) { fn reset(&mut self) {
unsafe { unsafe {
self.regs().cnt().write(|r| r.set_cnt(0)); Self::regs().cnt().write(|r| r.set_cnt(0));
} }
} }
@ -90,7 +89,7 @@ macro_rules! impl_basic_16bit_timer {
let arr: u16 = let arr: u16 =
unwrap!((pclk_ticks_per_timer_period / (u32::from(psc) + 1)).try_into()); unwrap!((pclk_ticks_per_timer_period / (u32::from(psc) + 1)).try_into());
let regs = self.regs(); let regs = Self::regs();
unsafe { unsafe {
regs.psc().write(|r| r.set_psc(psc)); regs.psc().write(|r| r.set_psc(psc));
regs.arr().write(|r| r.set_arr(arr)); regs.arr().write(|r| r.set_arr(arr));
@ -102,10 +101,11 @@ macro_rules! impl_basic_16bit_timer {
} }
fn clear_update_interrupt(&mut self) -> bool { fn clear_update_interrupt(&mut self) -> bool {
let regs = Self::regs();
unsafe { unsafe {
let sr = self.regs().sr().read(); let sr = regs.sr().read();
if sr.uif() { if sr.uif() {
self.regs().sr().modify(|r| { regs.sr().modify(|r| {
r.set_uif(false); r.set_uif(false);
}); });
true true
@ -117,7 +117,7 @@ macro_rules! impl_basic_16bit_timer {
fn enable_update_interrupt(&mut self, enable: bool) { fn enable_update_interrupt(&mut self, enable: bool) {
unsafe { unsafe {
self.regs().dier().write(|r| r.set_uie(enable)); Self::regs().dier().write(|r| r.set_uie(enable));
} }
} }
} }
@ -128,7 +128,7 @@ macro_rules! impl_basic_16bit_timer {
macro_rules! impl_32bit_timer { macro_rules! impl_32bit_timer {
($inst:ident) => { ($inst:ident) => {
impl sealed::GeneralPurpose32bitInstance for crate::peripherals::$inst { impl sealed::GeneralPurpose32bitInstance for crate::peripherals::$inst {
fn regs_gp32(&self) -> crate::pac::timer::TimGp32 { fn regs_gp32() -> crate::pac::timer::TimGp32 {
crate::pac::$inst crate::pac::$inst
} }
@ -141,7 +141,7 @@ macro_rules! impl_32bit_timer {
let arr: u32 = let arr: u32 =
unwrap!(((pclk_ticks_per_timer_period / (psc as u64 + 1)).try_into())); unwrap!(((pclk_ticks_per_timer_period / (psc as u64 + 1)).try_into()));
let regs = self.regs_gp32(); let regs = Self::regs_gp32();
unsafe { unsafe {
regs.psc().write(|r| r.set_psc(psc)); regs.psc().write(|r| r.set_psc(psc));
regs.arr().write(|r| r.set_arr(arr)); regs.arr().write(|r| r.set_arr(arr));
@ -169,7 +169,7 @@ foreach_interrupt! {
} }
impl sealed::GeneralPurpose16bitInstance for crate::peripherals::$inst { impl sealed::GeneralPurpose16bitInstance for crate::peripherals::$inst {
fn regs_gp16(&self) -> crate::pac::timer::TimGp16 { fn regs_gp16() -> crate::pac::timer::TimGp16 {
crate::pac::$inst crate::pac::$inst
} }
} }
@ -185,7 +185,7 @@ foreach_interrupt! {
} }
impl sealed::GeneralPurpose16bitInstance for crate::peripherals::$inst { impl sealed::GeneralPurpose16bitInstance for crate::peripherals::$inst {
fn regs_gp16(&self) -> crate::pac::timer::TimGp16 { fn regs_gp16() -> crate::pac::timer::TimGp16 {
crate::pac::timer::TimGp16(crate::pac::$inst.0) crate::pac::timer::TimGp16(crate::pac::$inst.0)
} }
} }
@ -206,7 +206,7 @@ foreach_interrupt! {
} }
impl sealed::AdvancedControlInstance for crate::peripherals::$inst { impl sealed::AdvancedControlInstance for crate::peripherals::$inst {
fn regs_advanced(&self) -> crate::pac::timer::TimAdv { fn regs_advanced() -> crate::pac::timer::TimAdv {
crate::pac::$inst crate::pac::$inst
} }
} }

View File

@ -90,20 +90,16 @@ impl<'d, T: CaptureCompare32bitInstance> SimplePwm32<'d, T> {
this.inner.start(); this.inner.start();
unsafe { unsafe {
this.inner T::regs_gp32()
.regs_gp32()
.ccmr_output(0) .ccmr_output(0)
.modify(|w| w.set_ocm(0, OutputCompareMode::PwmMode1.into())); .modify(|w| w.set_ocm(0, OutputCompareMode::PwmMode1.into()));
this.inner T::regs_gp32()
.regs_gp32()
.ccmr_output(0) .ccmr_output(0)
.modify(|w| w.set_ocm(1, OutputCompareMode::PwmMode1.into())); .modify(|w| w.set_ocm(1, OutputCompareMode::PwmMode1.into()));
this.inner T::regs_gp32()
.regs_gp32()
.ccmr_output(1) .ccmr_output(1)
.modify(|w| w.set_ocm(0, OutputCompareMode::PwmMode1.into())); .modify(|w| w.set_ocm(0, OutputCompareMode::PwmMode1.into()));
this.inner T::regs_gp32()
.regs_gp32()
.ccmr_output(1) .ccmr_output(1)
.modify(|w| w.set_ocm(1, OutputCompareMode::PwmMode1.into())); .modify(|w| w.set_ocm(1, OutputCompareMode::PwmMode1.into()));
} }
@ -112,8 +108,7 @@ impl<'d, T: CaptureCompare32bitInstance> SimplePwm32<'d, T> {
pub fn enable(&mut self, channel: Channel) { pub fn enable(&mut self, channel: Channel) {
unsafe { unsafe {
self.inner T::regs_gp32()
.regs_gp32()
.ccer() .ccer()
.modify(|w| w.set_cce(channel.raw(), true)); .modify(|w| w.set_cce(channel.raw(), true));
} }
@ -121,8 +116,7 @@ impl<'d, T: CaptureCompare32bitInstance> SimplePwm32<'d, T> {
pub fn disable(&mut self, channel: Channel) { pub fn disable(&mut self, channel: Channel) {
unsafe { unsafe {
self.inner T::regs_gp32()
.regs_gp32()
.ccer() .ccer()
.modify(|w| w.set_cce(channel.raw(), false)); .modify(|w| w.set_cce(channel.raw(), false));
} }
@ -136,14 +130,13 @@ impl<'d, T: CaptureCompare32bitInstance> SimplePwm32<'d, T> {
} }
pub fn get_max_duty(&self) -> u32 { pub fn get_max_duty(&self) -> u32 {
unsafe { self.inner.regs_gp32().arr().read().arr() } unsafe { T::regs_gp32().arr().read().arr() }
} }
pub fn set_duty(&mut self, channel: Channel, duty: u32) { pub fn set_duty(&mut self, channel: Channel, duty: u32) {
defmt::assert!(duty < self.get_max_duty()); defmt::assert!(duty < self.get_max_duty());
unsafe { unsafe {
self.inner T::regs_gp32()
.regs_gp32()
.ccr(channel.raw()) .ccr(channel.raw())
.modify(|w| w.set_ccr(duty)) .modify(|w| w.set_ccr(duty))
} }