Skip to content

Commit b7bacde

Browse files
committed
HRTIM: Refactor adc trigger
1 parent 27075e1 commit b7bacde

File tree

1 file changed

+82
-63
lines changed

1 file changed

+82
-63
lines changed

src/hrtim/control.rs

Lines changed: 82 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,17 @@ impl HrTimCalibrated {
193193
fault_4: FltMonitor4 { _x: PhantomData },
194194
fault_5: FltMonitor5 { _x: PhantomData },
195195
fault_6: FltMonitor6 { _x: PhantomData },
196+
197+
adc_trigger1: Adc1Trigger { _x: PhantomData },
198+
adc_trigger2: Adc2Trigger { _x: PhantomData },
199+
adc_trigger3: Adc3Trigger { _x: PhantomData },
200+
adc_trigger4: Adc4Trigger { _x: PhantomData },
201+
adc_trigger5: Adc5Trigger { _x: PhantomData },
202+
adc_trigger6: Adc6Trigger { _x: PhantomData },
203+
adc_trigger7: Adc7Trigger { _x: PhantomData },
204+
adc_trigger8: Adc8Trigger { _x: PhantomData },
205+
adc_trigger9: Adc9Trigger { _x: PhantomData },
206+
adc_trigger10: Adc10Trigger { _x: PhantomData },
196207
}
197208
}
198209
}
@@ -207,82 +218,90 @@ pub struct HrPwmControl {
207218
pub fault_4: FltMonitor4,
208219
pub fault_5: FltMonitor5,
209220
pub fault_6: FltMonitor6,
210-
}
211221

212-
use super::adc_trigger::{Adc13Trigger, Adc24Trigger, Adc579Trigger, Adc6810Trigger};
222+
pub adc_trigger1: Adc1Trigger,
223+
pub adc_trigger2: Adc2Trigger,
224+
pub adc_trigger3: Adc3Trigger,
225+
pub adc_trigger4: Adc4Trigger,
226+
227+
pub adc_trigger5: Adc5Trigger,
228+
pub adc_trigger6: Adc6Trigger,
229+
pub adc_trigger7: Adc7Trigger,
230+
pub adc_trigger8: Adc8Trigger,
231+
pub adc_trigger9: Adc9Trigger,
232+
pub adc_trigger10: Adc10Trigger,
233+
}
213234

214-
impl HrPwmControl {
215-
pub fn enable_adc_trigger1_source<T: Adc13Trigger>(&mut self, _trigger: &T) {
216-
let common = unsafe { &*HRTIM_COMMON::ptr() };
217-
unsafe {
218-
common.adc1r.modify(|r, w| w.bits(r.bits() | T::BITS));
235+
macro_rules! impl_adc1234_trigger {
236+
($($t:ident: [$trait_:ident, $adcXr:ident, $variant345:ident $(, $variant12:ident)*]),*) => {$(
237+
pub struct $t {
238+
_x: PhantomData<()>,
219239
}
220-
}
221240

222-
pub fn enable_adc_trigger2_source<T: Adc24Trigger>(&mut self, _trigger: &T) {
223-
let common = unsafe { &*HRTIM_COMMON::ptr() };
224-
unsafe {
225-
common.adc2r.modify(|r, w| w.bits(r.bits() | T::BITS));
241+
impl $t {
242+
pub fn enable_source<T: $trait_>(&mut self, _trigger: &T) {
243+
let common = unsafe { &*HRTIM_COMMON::ptr() };
244+
unsafe {
245+
common.$adcXr.modify(|r, w| w.bits(r.bits() | T::BITS));
246+
}
247+
}
248+
249+
$(
250+
pub fn as_adc12_trigger(&self) -> crate::adc::config::ExternalTrigger12 {
251+
crate::adc::config::ExternalTrigger12::$variant12
252+
}
253+
)*
254+
255+
pub fn as_adc345_trigger(&self) -> crate::adc::config::ExternalTrigger345 {
256+
crate::adc::config::ExternalTrigger345::$variant345
257+
}
226258
}
227-
}
259+
)*}
260+
}
228261

229-
pub fn enable_adc_trigger3_source<T: Adc13Trigger>(&mut self, _trigger: &T) {
230-
let common = unsafe { &*HRTIM_COMMON::ptr() };
231-
unsafe {
232-
common.adc3r.modify(|r, w| w.bits(r.bits() | T::BITS));
262+
macro_rules! impl_adc5678910_trigger {
263+
($($t:ident: [$trait_:ident, $adcXtrg:ident, $variant345:ident, $variant12:ident]),*) => {$(
264+
pub struct $t {
265+
_x: PhantomData<()>,
233266
}
234-
}
235267

236-
pub fn enable_adc_trigger4_source<T: Adc24Trigger>(&mut self, _trigger: &T) {
237-
let common = unsafe { &*HRTIM_COMMON::ptr() };
238-
unsafe {
239-
common.adc4r.modify(|r, w| w.bits(r.bits() | T::BITS));
268+
impl $t {
269+
pub fn enable_source<T: $trait_>(&mut self, _trigger: &T) {
270+
let common = unsafe { &*HRTIM_COMMON::ptr() };
271+
common
272+
.adcer
273+
.modify(|_r, w| w.$adcXtrg().variant(T::BITS as u8));
274+
}
275+
276+
pub fn as_adc12_trigger(&self) -> crate::adc::config::ExternalTrigger12 {
277+
crate::adc::config::ExternalTrigger12::$variant12
278+
}
279+
280+
pub fn as_adc345_trigger(&self) -> crate::adc::config::ExternalTrigger345 {
281+
crate::adc::config::ExternalTrigger345::$variant345
282+
}
240283
}
241-
}
242-
243-
pub fn enable_adc_trigger5_source<T: Adc579Trigger>(&mut self, _trigger: &T) {
244-
let common = unsafe { &*HRTIM_COMMON::ptr() };
245-
common
246-
.adcer
247-
.modify(|_r, w| w.adc5trg().variant(T::BITS as u8));
248-
}
249-
250-
pub fn enable_adc_trigger6_source<T: Adc6810Trigger>(&mut self, _trigger: &T) {
251-
let common = unsafe { &*HRTIM_COMMON::ptr() };
252-
common
253-
.adcer
254-
.modify(|_r, w| w.adc6trg().variant(T::BITS as u8));
255-
}
256-
257-
pub fn enable_adc_trigger7_source<T: Adc579Trigger>(&mut self, _trigger: &T) {
258-
let common = unsafe { &*HRTIM_COMMON::ptr() };
259-
common
260-
.adcer
261-
.modify(|_r, w| w.adc7trg().variant(T::BITS as u8));
262-
}
263-
264-
pub fn enable_adc_trigger8_source<T: Adc6810Trigger>(&mut self, _trigger: &T) {
265-
let common = unsafe { &*HRTIM_COMMON::ptr() };
266-
common
267-
.adcer
268-
.modify(|_r, w| w.adc8trg().variant(T::BITS as u8));
269-
}
284+
)*}
285+
}
270286

271-
pub fn enable_adc_trigger9_source<T: Adc579Trigger>(&mut self, _trigger: &T) {
272-
let common = unsafe { &*HRTIM_COMMON::ptr() };
273-
common
274-
.adcer
275-
.modify(|_r, w| w.adc9trg().variant(T::BITS as u8));
276-
}
287+
impl_adc1234_trigger! {// reg adc345, adc12
288+
Adc1Trigger: [Adc13Trigger, adc1r, Hrtim_adc_trg_1, Hrtim_adc_trg_1],
289+
Adc2Trigger: [Adc24Trigger, adc2r, Hrtim_adc_trg_2],
290+
Adc3Trigger: [Adc13Trigger, adc3r, Hrtim_adc_trg_3, Hrtim_adc_trg_3],
291+
Adc4Trigger: [Adc24Trigger, adc4r, Hrtim_adc_trg_4]
292+
}
277293

278-
pub fn enable_adc_trigger10_source<T: Adc6810Trigger>(&mut self, _trigger: &T) {
279-
let common = unsafe { &*HRTIM_COMMON::ptr() };
280-
common
281-
.adcer
282-
.modify(|_r, w| w.adc10trg().variant(T::BITS as u8));
283-
}
294+
impl_adc5678910_trigger! {
295+
Adc5Trigger: [Adc579Trigger, adc5trg, Hrtim_adc_trg_5, Hrtim_adc_trg_5],
296+
Adc6Trigger: [Adc6810Trigger, adc6trg, Hrtim_adc_trg_6, Hrtim_adc_trg_6],
297+
Adc7Trigger: [Adc579Trigger, adc7trg, Hrtim_adc_trg_7, Hrtim_adc_trg_7],
298+
Adc8Trigger: [Adc6810Trigger, adc8trg, Hrtim_adc_trg_8, Hrtim_adc_trg_8],
299+
Adc9Trigger: [Adc579Trigger, adc9trg, Hrtim_adc_trg_9, Hrtim_adc_trg_9],
300+
Adc10Trigger: [Adc6810Trigger, adc10trg, Hrtim_adc_trg_10, Hrtim_adc_trg_10]
284301
}
285302

303+
use super::adc_trigger::{Adc13Trigger, Adc24Trigger, Adc579Trigger, Adc6810Trigger};
304+
286305
pub enum AdcTriggerPostscaler {
287306
None = 0,
288307
Div2 = 1,

0 commit comments

Comments
 (0)