@@ -13,6 +13,14 @@ pub enum Event {
1313 EndOfAcquisition
1414}
1515
16+ #[ derive( Debug ) ]
17+ pub enum Error {
18+ /// Max count error
19+ MaxCountError ,
20+ /// Wrong GPIO for reading
21+ InvalidPin
22+ }
23+
1624// TODO macro to impl all possible channel/sample pin combinations
1725pub trait SamplePin < TSC > {
1826 const GROUP : u32 ;
@@ -148,35 +156,45 @@ impl<SPIN> Tsc<SPIN> {
148156 }
149157
150158 /// Blocks waiting for a acquisition to complete or for a Max Count Error
151- pub fn acquire < PIN > ( & self , input : & mut PIN ) -> Result < u16 , Event >
159+ pub fn acquire < PIN > ( & self , input : & mut PIN ) -> Result < u16 , Error >
152160 where PIN : ChannelPin < TSC >
153161 {
154- let bit_pos = PIN :: OFFSET + ( 4 * ( PIN :: GROUP - 1 ) ) ;
155-
156- // disable Schmitt trigger hysteresis
157- self . tsc . iohcr . write ( |w| unsafe {
158- w. bits ( 1 << bit_pos)
159- } ) ;
160-
162+ // start the acq
161163 self . start ( input) ;
162164
163165 let result = loop {
164166 let isr = self . tsc . isr . read ( ) ;
165167 if isr. eoaf ( ) . bit_is_set ( ) {
166- break Ok ( self . tsc . iog2cr . read ( ) . cnt ( ) . bits ( ) )
168+ break Ok ( self . read_unchecked ( ) )
167169 } else if isr. mcef ( ) . bit_is_set ( ) {
168- break Err ( Event :: MaxCountError )
170+ break Err ( Error :: MaxCountError )
169171 }
170172 } ;
171173
172- // re-enable Schmitt trigger hysteresis
173- self . tsc . iohcr . write ( |w| unsafe {
174- w. bits ( 0 << bit_pos)
175- } ) ;
176-
177174 result
178175 }
179176
177+ /// Reads the tsc group 2 count register
178+ pub fn read < PIN > ( & self , _input : & mut PIN ) -> Result < u16 , Error >
179+ where PIN : ChannelPin < TSC >
180+ {
181+ let bit_pos = PIN :: OFFSET + ( 4 * ( PIN :: GROUP - 1 ) ) ;
182+ // Read the current channel config
183+ let channel = self . tsc . ioccr . read ( ) . bits ( ) ;
184+ // if they are equal we have the right pin
185+ if channel == ( 1 << bit_pos) {
186+ Ok ( self . read_unchecked ( ) )
187+ } else {
188+ Err ( Error :: InvalidPin )
189+ }
190+ }
191+
192+ /// Reads the tsc group 2 count register
193+ /// WARNING, just returns the contents of the register! No validation of the correct pin
194+ pub fn read_unchecked ( & self ) -> u16 {
195+ self . tsc . iog2cr . read ( ) . cnt ( ) . bits ( )
196+ }
197+
180198 /// Enables an interrupt event
181199 pub fn listen ( & mut self , event : Event ) {
182200 match event {
0 commit comments