Skip to content

Commit 516b246

Browse files
committed
HRTIM - Multiple changes
* Allow disabling update of shadow registers * Add `control` member to HrPwmControl * HRTIM now has its own `FaultMonitor` trait
1 parent f0507de commit 516b246

File tree

5 files changed

+116
-30
lines changed

5 files changed

+116
-30
lines changed

src/hrtim/control.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ impl HrTimCalibrated {
186186
pub fn constrain(self) -> HrPwmControl {
187187
HrPwmControl {
188188
_x: PhantomData,
189+
control: HrPwmCtrl { _x: PhantomData },
189190
fault_sys: FltMonitorSys { _x: PhantomData },
190191
fault_1: FltMonitor1 { _x: PhantomData },
191192
fault_2: FltMonitor2 { _x: PhantomData },
@@ -208,9 +209,25 @@ impl HrTimCalibrated {
208209
}
209210
}
210211

212+
impl<'a> Into<&'a mut HrPwmCtrl> for &'a mut HrPwmControl {
213+
fn into(self) -> &'a mut HrPwmCtrl {
214+
&mut self.control
215+
}
216+
}
217+
218+
/// Used as a token to guarantee unique access to resources common to multiple timers
219+
///
220+
/// An instance of this object can be obtained from [`HrPwmControl`].control
221+
pub struct HrPwmCtrl {
222+
_x: PhantomData<()>,
223+
}
224+
225+
/// Used as a token to guarantee unique access to resources common to multiple timers
211226
pub struct HrPwmControl {
212227
_x: PhantomData<()>,
213228

229+
pub control: HrPwmCtrl,
230+
214231
pub fault_sys: FltMonitorSys,
215232
pub fault_1: FltMonitor1,
216233
pub fault_2: FltMonitor2,

src/hrtim/fault.rs

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,24 @@ use crate::gpio::gpiob::{PB0, PB10, PB11};
66
use crate::gpio::gpioc::{PC10, PC7};
77
use crate::gpio::{self, AF13, AF3};
88
use crate::hrtim::control::HrPwmControl;
9-
use crate::pwm::FaultMonitor;
109
use crate::stm32::HRTIM_COMMON;
1110

11+
use super::control::HrPwmCtrl;
12+
13+
/// Allows a FaultMonitor to monitor faults
14+
pub trait FaultMonitor {
15+
fn enable_interrupt(&mut self, hr_control: &mut HrPwmCtrl);
16+
17+
/// Returns true if a fault is preventing PWM output
18+
fn is_fault_active(&self) -> bool;
19+
20+
/// Clear the fault interrupt flag
21+
///
22+
/// This will *NOT* resume normal PWM operation. The affected outputs need to be re-enabled to resume operation;
23+
/// This will do nothing if the fault is still active.
24+
fn clear_fault(&mut self);
25+
}
26+
1227
pub enum FaultAction {
1328
/// Output never enters fault mode
1429
None = 0b00,
@@ -227,12 +242,17 @@ pub enum FaultSamplingFilter {
227242
}
228243

229244
macro_rules! impl_flt_monitor {
230-
($($t:ident: ($fltx:ident, $fltxc:ident),)+) => {$(
245+
($($t:ident: ($fltx:ident, $fltxc:ident, $fltxie:ident),)+) => {$(
231246
pub struct $t {
232247
pub(crate) _x: PhantomData<()>
233248
}
234249

235250
impl FaultMonitor for $t {
251+
fn enable_interrupt(&mut self, _hr_control: &mut HrPwmCtrl) {
252+
let common = unsafe { &*HRTIM_COMMON::ptr() };
253+
common.ier.modify(|_r, w| w.$fltxie().set_bit());
254+
}
255+
236256
fn is_fault_active(&self) -> bool {
237257
let common = unsafe { &*HRTIM_COMMON::ptr() };
238258
common.isr.read().$fltx().bit()
@@ -242,21 +262,16 @@ macro_rules! impl_flt_monitor {
242262
let common = unsafe { &*HRTIM_COMMON::ptr() };
243263
common.icr.write(|w| w.$fltxc().set_bit());
244264
}
245-
246-
// TODO: Should we have our own trait since it does not seem possible to implement this
247-
fn set_fault(&mut self) {
248-
todo!()
249-
}
250265
}
251266
)+};
252267
}
253268

254269
impl_flt_monitor!(
255-
FltMonitorSys: (sysflt, sysfltc),
256-
FltMonitor1: (flt1, flt1c),
257-
FltMonitor2: (flt2, flt2c),
258-
FltMonitor3: (flt3, flt3c),
259-
FltMonitor4: (flt4, flt4c),
260-
FltMonitor5: (flt5, flt5c),
261-
FltMonitor6: (flt6, flt6c),
270+
FltMonitorSys: (sysflt, sysfltc, sysflte),
271+
FltMonitor1: (flt1, flt1c, flt1ie),
272+
FltMonitor2: (flt2, flt2c, flt2ie),
273+
FltMonitor3: (flt3, flt3c, flt3ie),
274+
FltMonitor4: (flt4, flt4c, flt4ie),
275+
FltMonitor5: (flt5, flt5c, flt5ie),
276+
FltMonitor6: (flt6, flt6c, flt6ie),
262277
);

src/hrtim/output.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,20 @@ pub enum State {
121121
Fault,
122122
}
123123

124+
impl State {
125+
pub fn is_idle(self) -> bool {
126+
matches!(self, State::Idle)
127+
}
128+
129+
pub fn is_running(self) -> bool {
130+
matches!(self, State::Running)
131+
}
132+
133+
pub fn is_fault(self) -> bool {
134+
matches!(self, State::Fault)
135+
}
136+
}
137+
124138
pub unsafe trait ToHrOut {
125139
type Out<PSCL>: ToHrOut;
126140
}

src/hrtim/timer.rs

Lines changed: 54 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use core::marker::PhantomData;
55

66
use super::{
77
capture::{self, HrCapt, HrCapture},
8-
control::HrPwmControl,
8+
control::HrPwmCtrl,
99
HrtimPrescaler,
1010
};
1111

@@ -42,13 +42,13 @@ pub trait HrTimer {
4242
fn set_period(&mut self, period: u16);
4343

4444
/// Start timer
45-
fn start(&mut self, _hr_control: &mut HrPwmControl);
45+
fn start(&mut self, _hr_control: &mut HrPwmCtrl);
4646

4747
/// Stop timer
48-
fn stop(&mut self, _hr_control: &mut HrPwmControl);
48+
fn stop(&mut self, _hr_control: &mut HrPwmCtrl);
4949

5050
/// Stop timer and reset counter
51-
fn stop_and_reset(&mut self, _hr_control: &mut HrPwmControl);
51+
fn stop_and_reset(&mut self, _hr_control: &mut HrPwmCtrl);
5252

5353
fn clear_repetition_interrupt(&mut self);
5454

@@ -57,6 +57,21 @@ pub trait HrTimer {
5757

5858
/// Make a handle to this timers period event to use as adc trigger
5959
fn as_period_adc_trigger(&self) -> super::adc_trigger::TimerPeriod<Self::Timer>;
60+
61+
/// Disable register updates
62+
///
63+
/// Calling this function temporarily disables the transfer from preload to active registers,
64+
/// whatever the selected update event. This allows to modify several registers.
65+
/// The regular update event takes place once [`Self::enable_register_updates`] is called.
66+
fn disable_register_updates(&mut self, _hr_control: &mut HrPwmCtrl);
67+
68+
/// Enable register updates
69+
///
70+
/// See [`Self::disable_register_updates`].
71+
///
72+
/// NOTE: Register updates are enabled by default, no need to call this
73+
/// unless [`Self::disable_register_updates`] has been called.
74+
fn enable_register_updates(&mut self, _hr_control: &mut HrPwmCtrl);
6075
}
6176

6277
pub trait HrSlaveTimer: HrTimer {
@@ -106,6 +121,7 @@ macro_rules! hrtim_timer {
106121
$repie:ident,
107122
$icr:ident,
108123
$repc:ident,
124+
$tXudis:ident,
109125
$(($rstXr:ident))*,
110126
)+) => {$(
111127
impl<PSCL: HrtimPrescaler, CPT1, CPT2> HrTimer for HrTim<$TIMX, PSCL, CPT1, CPT2> {
@@ -124,7 +140,7 @@ macro_rules! hrtim_timer {
124140
}
125141

126142
/// Start timer
127-
fn start(&mut self, _hr_control: &mut HrPwmControl) {
143+
fn start(&mut self, _hr_control: &mut HrPwmCtrl) {
128144
// Start timer
129145

130146
// SAFETY: Since we hold _hr_control there is no risk for a race condition
@@ -133,15 +149,15 @@ macro_rules! hrtim_timer {
133149
}
134150

135151
/// Stop timer
136-
fn stop(&mut self, _hr_control: &mut HrPwmControl) {
152+
fn stop(&mut self, _hr_control: &mut HrPwmCtrl) {
137153
// Stop counter
138154
// SAFETY: Since we hold _hr_control there is no risk for a race condition
139155
let master = unsafe { &*HRTIM_MASTER::ptr() };
140156
master.mcr.modify(|_r, w| { w.$tXcen().set_bit() });
141157
}
142158

143159
/// Stop timer and reset counter
144-
fn stop_and_reset(&mut self, _hr_control: &mut HrPwmControl) {
160+
fn stop_and_reset(&mut self, _hr_control: &mut HrPwmCtrl) {
145161
self.stop(_hr_control);
146162

147163
// Reset counter
@@ -164,6 +180,29 @@ macro_rules! hrtim_timer {
164180

165181
tim.$icr.write(|w| w.$repc().set_bit());
166182
}
183+
184+
/// Disable register updates
185+
///
186+
/// Calling this function temporarily disables the transfer from preload to active registers,
187+
/// whatever the selected update event. This allows to modify several registers.
188+
/// The regular update event takes place once [`Self::enable_register_updates`] is called.
189+
fn disable_register_updates(&mut self, _hr_control: &mut HrPwmCtrl) {
190+
use super::HRTIM_COMMON;
191+
let common = unsafe { &*HRTIM_COMMON::ptr() };
192+
common.cr1.modify(|_r, w| w.$tXudis().set_bit());
193+
}
194+
195+
/// Enable register updates
196+
///
197+
/// See [`Self::disable_register_updates`].
198+
///
199+
/// NOTE: Register updates are enabled by default, no need to call this
200+
/// unless [`Self::disable_register_updates`] has been called.
201+
fn enable_register_updates<'a>(&mut self, _hr_control: &mut HrPwmCtrl) {
202+
use super::HRTIM_COMMON;
203+
let common = unsafe { &*HRTIM_COMMON::ptr() };
204+
common.cr1.modify(|_r, w| w.$tXudis().clear_bit());
205+
}
167206
}
168207

169208
impl<PSCL, CPT1, CPT2> HrTim<$TIMX, PSCL, CPT1, CPT2> {
@@ -290,14 +329,14 @@ use super::adc_trigger::Adc579Trigger as Adc579;
290329
use super::adc_trigger::Adc6810Trigger as Adc6810;
291330

292331
hrtim_timer! {
293-
HRTIM_MASTER: mcntr, mcnt, mper, mcen, mper, mrep, mrep, mdier, mrepie, micr, mrepc,,
294-
295-
HRTIM_TIMA: cntar, cntx, perar, tacen, perx, repar, repx, timadier, repie, timaicr, repc, (rstar),
296-
HRTIM_TIMB: cntr, cntx, perbr, tbcen, perx, repbr, repx, timbdier, repie, timbicr, repc, (rstbr),
297-
HRTIM_TIMC: cntcr, cntx, percr, tccen, perx, repcr, repx, timcdier, repie, timcicr, repc, (rstcr),
298-
HRTIM_TIMD: cntdr, cntx, perdr, tdcen, perx, repdr, repx, timddier, repie, timdicr, repc, (rstdr),
299-
HRTIM_TIME: cnter, cntx, perer, tecen, perx, reper, repx, timedier, repie, timeicr, repc, (rster),
300-
HRTIM_TIMF: cntfr, cntx, perfr, tfcen, perx, repfr, repx, timfdier, repie, timficr, repc, (rstfr),
332+
HRTIM_MASTER: mcntr, mcnt, mper, mcen, mper, mrep, mrep, mdier, mrepie, micr, mrepc, mudis,,
333+
334+
HRTIM_TIMA: cntar, cntx, perar, tacen, perx, repar, repx, timadier, repie, timaicr, repc, taudis, (rstar),
335+
HRTIM_TIMB: cntr, cntx, perbr, tbcen, perx, repbr, repx, timbdier, repie, timbicr, repc, tbudis, (rstbr),
336+
HRTIM_TIMC: cntcr, cntx, percr, tccen, perx, repcr, repx, timcdier, repie, timcicr, repc, tcudis, (rstcr),
337+
HRTIM_TIMD: cntdr, cntx, perdr, tdcen, perx, repdr, repx, timddier, repie, timdicr, repc, tdudis, (rstdr),
338+
HRTIM_TIME: cnter, cntx, perer, tecen, perx, reper, repx, timedier, repie, timeicr, repc, teudis, (rster),
339+
HRTIM_TIMF: cntfr, cntx, perfr, tfcen, perx, repfr, repx, timfdier, repie, timficr, repc, tfudis, (rstfr),
301340
}
302341

303342
hrtim_timer_adc_trigger! {

src/serial/usart.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,8 @@ macro_rules! uart_shared {
360360
let config = crate::dma::config::DmaConfig::default()
361361
.transfer_complete_interrupt(false)
362362
.circular_buffer(false)
363-
.memory_increment(true);
363+
.memory_increment(true)
364+
.priority(crate::dma::config::Priority::Low);
364365
stream.apply_config(config);
365366

366367
Tx {

0 commit comments

Comments
 (0)