@@ -31,8 +31,72 @@ pub trait CaptureEvent<TIM, PSCL> {
3131 const BITS : u32 ;
3232}
3333
34+ /// Trait for capture channels used for capturing edges
35+ ///
36+ /// ```
37+ /// let capture: HrCapt<_, _, _> = todo!();
38+ /// if capture.is_pending() {
39+ /// let (value, dir) = capture.get_last();
40+ /// capture.clear_interrupt();
41+ /// defmt::info!("Edge captured at counter value: {}, with: {}", value, dir);
42+ /// }
43+ /// ```
44+ ///
45+ /// or alternatively
46+ ///
47+ /// ```
48+ /// let capture: HrCapt<_, _, _> = todo!();
49+ /// if let Some((value, dir)) = capture.get() {
50+ /// defmt::info!("Edge captured at counter value: {}, with: {}", value, dir);
51+ /// }
52+ /// ```
3453pub trait HrCapture {
35- fn get ( & self ) -> ( u16 , CountingDirection ) ;
54+ /// Try to get the capture value
55+ ///
56+ /// Returns none if edge has been captured since last time
57+ ///
58+ /// NOTE: This function will use [`Self::is_pending`] to chech if there is a value available and
59+ /// [`Self::clear_interrupt`] to clear it.
60+ fn get ( & mut self ) -> Option < ( u16 , CountingDirection ) > {
61+ if self . is_pending ( ) {
62+ let value = self . get_last ( ) ;
63+ self . clear_interrupt ( ) ;
64+ Some ( value)
65+ } else {
66+ None
67+ }
68+ }
69+
70+ /// Get number of ticks relative to beginning of upcounting
71+ ///
72+ /// where captures during down counting count as negative (before the upcount)
73+ ///
74+ /// ```txt
75+ /// Counter
76+ /// ---------------------------------- <--- period
77+ /// \ ^ /
78+ /// \ | /
79+ /// \ | /
80+ /// \ | /
81+ /// Down count \ | / Up count
82+ /// \|/
83+ /// <-------------- 0 --------------> t
84+ /// Negative result | positive result
85+ /// ```
86+ ///
87+ /// NOTE: This function will use [`Self::is_pending`] to chech if there is a value available and
88+ /// [`Self::clear_interrupt`] to clear it.
89+ fn get_signed ( & mut self , period : u16 ) -> Option < i32 > {
90+ if self . is_pending ( ) {
91+ let value = self . get_last_signed ( period) ;
92+ self . clear_interrupt ( ) ;
93+ Some ( value)
94+ } else {
95+ None
96+ }
97+ }
98+
99+ fn get_last ( & self ) -> ( u16 , CountingDirection ) ;
36100
37101 /// Get number of ticks relative to beginning of upcounting
38102 ///
@@ -50,12 +114,13 @@ pub trait HrCapture {
50114 /// <-------------- 0 --------------> t
51115 /// Negative result | positive result
52116 /// ````
53- fn get_signed ( & self , period : u16 ) -> i32 {
54- let ( value, dir) = self . get ( ) ;
117+ fn get_last_signed ( & self , period : u16 ) -> i32 {
118+ let ( value, dir) = self . get_last ( ) ;
55119
120+ // The capture counter always counts up and restarts at period
56121 match dir {
57122 CountingDirection :: Up => i32:: from ( value) ,
58- CountingDirection :: Down => i32:: from ( period ) - i32:: from ( value ) ,
123+ CountingDirection :: Down => i32:: from ( value ) - i32:: from ( period ) ,
59124 }
60125 }
61126
@@ -75,9 +140,10 @@ pub fn dma_value_to_dir_and_value(x: u32) -> (u16, CountingDirection) {
75140pub fn dma_value_to_signed ( x : u32 , period : u16 ) -> i32 {
76141 let ( value, dir) = dma_value_to_dir_and_value ( x) ;
77142
143+ // The capture counter always counts up and restarts at period
78144 match dir {
79145 CountingDirection :: Up => i32:: from ( value) ,
80- CountingDirection :: Down => i32:: from ( period ) - i32:: from ( value ) ,
146+ CountingDirection :: Down => i32:: from ( value ) - i32:: from ( period ) ,
81147 }
82148}
83149
@@ -135,7 +201,7 @@ macro_rules! impl_capture {
135201 }
136202
137203 impl <PSCL , DMA > HrCapture for HrCapt <$TIMX, PSCL , $CH, DMA > {
138- fn get ( & self ) -> ( u16 , CountingDirection ) {
204+ fn get_last ( & self ) -> ( u16 , CountingDirection ) {
139205 let tim = unsafe { & * $TIMX:: ptr( ) } ;
140206 let data = tim. $cptXYr. read( ) ;
141207
0 commit comments