Skip to content

Commit 30e39c7

Browse files
committed
Removed uneedded operations on channel gpio (schmit trigger) added the read & read_unchecked apis, read verifies that the user is trying to read the pin that the acq started on, read_unchecked returns the result of the cnt register
1 parent 8b06ef8 commit 30e39c7

File tree

3 files changed

+57
-16
lines changed

3 files changed

+57
-16
lines changed

.vscode/launch.json

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
{
88
"type": "gdb",
99
"request": "attach",
10-
"name": "STM32F1",
10+
"name": "Debug",
1111
// "gdbpath" : "/home/mabez/programs/gcc-arm-none-eabi-5_4-2016q3/bin/arm-none-eabi-gdb",
1212
"gdbpath" : "/home/mabez/programs/gcc-arm-none-eabi-7-2017-q4-major/bin/arm-none-eabi-gdb",
1313
"debugger_args": [
@@ -25,5 +25,26 @@
2525
"step"
2626
]
2727
},
28+
{
29+
"type": "gdb",
30+
"request": "attach",
31+
"name": "Release",
32+
// "gdbpath" : "/home/mabez/programs/gcc-arm-none-eabi-5_4-2016q3/bin/arm-none-eabi-gdb",
33+
"gdbpath" : "/home/mabez/programs/gcc-arm-none-eabi-7-2017-q4-major/bin/arm-none-eabi-gdb",
34+
"debugger_args": [
35+
"-nx" // dont use the .gdbinit file
36+
],
37+
"executable": "./target/thumbv7em-none-eabi/release/examples/touch",
38+
"remote": true,
39+
"target": ":3333",
40+
"cwd": "${workspaceRoot}",
41+
"autorun": [
42+
"set print asm-demangle on",
43+
"monitor arm semihosting enable",
44+
"load",
45+
// "break main",
46+
"step"
47+
]
48+
},
2849
]
2950
}

examples/touch.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ fn main() -> ! {
5252
loop {
5353
let touched = tsc.acquire(&mut c1).unwrap();
5454
let _touched_c2 = tsc.acquire(&mut c2).unwrap();
55+
// try and pass c1, it will detect an error!
56+
let _touched_c2_again = tsc.read(&mut c2).unwrap();
5557
if touched < threshold {
5658
led.set_high();
5759
} else {

src/tsc.rs

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -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
1725
pub 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

Comments
 (0)