Skip to content

Commit 773902a

Browse files
committed
Found an implementation for pins that allows us to specify the tsc group information in the type. can now read from two seperate pins (3 touch keys)
1 parent 17dd09c commit 773902a

File tree

2 files changed

+50
-36
lines changed

2 files changed

+50
-36
lines changed

examples/touch.rs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,40 +18,48 @@ use hal::prelude::*;
1818
use hal::stm32l4::stm32l4x2;
1919
use hal::tsc::Tsc;
2020
use rt::ExceptionFrame;
21-
use hal::delay::Delay;
2221

2322
entry!(main);
2423

2524
fn main() -> ! {
2625
let p = stm32l4x2::Peripherals::take().unwrap();
27-
let cp = cortex_m::Peripherals::take().unwrap();
26+
// let cp = cortex_m::Peripherals::take().unwrap();
2827

2928
let mut flash = p.FLASH.constrain();
3029
let mut rcc = p.RCC.constrain();
3130
// let mut gpioa = p.GPIOA.split(&mut rcc.ahb2);
3231
let mut gpiob = p.GPIOB.split(&mut rcc.ahb2);
3332

3433
// clock configuration using the default settings (all clocks run at 8 MHz)
35-
let clocks = rcc.cfgr.freeze(&mut flash.acr);
34+
let _clocks = rcc.cfgr.freeze(&mut flash.acr);
3635
// TRY this alternate clock configuration (clocks run at nearly the maximum frequency)
3736
// let clocks = rcc.cfgr.sysclk(64.mhz()).pclk1(32.mhz()).freeze(&mut flash.acr);
3837

3938
// let mut delay = Delay::new(cp.SYST, clocks);
39+
let mut led = gpiob.pb3.into_push_pull_output(&mut gpiob.moder, &mut gpiob.otyper);
4040

4141
let sample_pin = gpiob.pb4.into_touch_sample(&mut gpiob.moder, &mut gpiob.otyper, &mut gpiob.afrl);
42-
let c1 = gpiob.pb5.into_touch_channel(&mut gpiob.moder, &mut gpiob.otyper, &mut gpiob.afrl);
43-
let c2 = gpiob.pb6.into_touch_channel(&mut gpiob.moder, &mut gpiob.otyper, &mut gpiob.afrl);
44-
let c3 = gpiob.pb7.into_touch_channel(&mut gpiob.moder, &mut gpiob.otyper, &mut gpiob.afrl);
42+
let mut c1 = gpiob.pb5.into_touch_channel(&mut gpiob.moder, &mut gpiob.otyper, &mut gpiob.afrl);
43+
let mut c2 = gpiob.pb6.into_touch_channel(&mut gpiob.moder, &mut gpiob.otyper, &mut gpiob.afrl);
44+
// let mut c3 = gpiob.pb7.into_touch_channel(&mut gpiob.moder, &mut gpiob.otyper, &mut gpiob.afrl);
4545

46-
let tsc = Tsc::tsc(p.TSC, sample_pin, (c1, c2, c3) , &mut rcc.ahb1);
46+
// , (c1, c2, c3)
47+
let tsc = Tsc::tsc(p.TSC, sample_pin, &mut rcc.ahb1);
4748

48-
tsc.start();
49+
tsc.start(&mut c1);
4950
let baseline = tsc.wait().unwrap();
50-
51-
tsc.start();
52-
let touched = tsc.wait().unwrap();
53-
54-
loop {}
51+
let threshold = (baseline / 100) * 60;
52+
loop {
53+
tsc.start(&mut c1);
54+
let touched = tsc.wait().unwrap();
55+
tsc.start(&mut c2);
56+
let _touched_c2 = tsc.wait().unwrap();
57+
if touched < threshold {
58+
led.set_high();
59+
} else {
60+
led.set_low();
61+
}
62+
}
5563
}
5664

5765
exception!(HardFault, hard_fault);

src/tsc.rs

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,30 @@ pub enum Event {
1717
pub trait SamplePin<TSC> {}
1818
impl SamplePin<TSC> for PB4<Alternate<AF9, Output<OpenDrain>>> {}
1919

20-
pub trait ChannelPins<TSC> {}
21-
impl ChannelPins<TSC> for (
22-
PB5<Alternate<AF9, Output<PushPull>>>,
23-
PB6<Alternate<AF9, Output<PushPull>>>,
24-
PB7<Alternate<AF9, Output<PushPull>>>)
25-
{}
20+
pub trait ChannelPin<TSC> {
21+
const OFFSET: u32;
22+
}
23+
impl ChannelPin<TSC> for PB5<Alternate<AF9, Output<PushPull>>> {
24+
const OFFSET: u32 = 1 << 1 + (1 * 4);
25+
}
26+
impl ChannelPin<TSC> for PB6<Alternate<AF9, Output<PushPull>>> {
27+
const OFFSET: u32 = 1 << 2 + (1 * 4);
28+
}
29+
impl ChannelPin<TSC> for PB7<Alternate<AF9, Output<PushPull>>> {
30+
const OFFSET: u32 = 1 << 3 + (1 * 4);
31+
}
32+
2633

2734
// TODO currently requires all the pins even if a user wants one channel, fix
28-
pub struct Tsc<SPIN, PINS> {
35+
pub struct Tsc<SPIN> {
2936
sample_pin: SPIN,
30-
pins: PINS,
37+
// pins: PINS,
3138
tsc: TSC
3239
}
3340

34-
impl<SPIN, PINS> Tsc<SPIN, PINS> {
35-
pub fn tsc(tsc: TSC, sample_pin: SPIN, pins: PINS, ahb: &mut AHB1) -> Self
36-
where PINS: ChannelPins<TSC>,
37-
SPIN: SamplePin<TSC>
41+
impl<SPIN> Tsc<SPIN> {
42+
pub fn tsc(tsc: TSC, sample_pin: SPIN, ahb: &mut AHB1) -> Self
43+
where SPIN: SamplePin<TSC> // PINS: ChannelPins<TSC>,
3844
{
3945
/* Enable the peripheral clock */
4046
ahb.enr().modify(|_, w| w.tscen().set_bit());
@@ -75,13 +81,6 @@ impl<SPIN, PINS> Tsc<SPIN, PINS> {
7581

7682
// Set the sampling pin
7783
tsc.ioscr.write(|w| { w.g2_io1().set_bit() });
78-
79-
// Set the channel pin(s)
80-
tsc.ioccr.write(|w| {
81-
w.g2_io2().set_bit()
82-
.g2_io3().set_bit()
83-
.g2_io4().set_bit()
84-
});
8584

8685
// set the acquisitiuon groups based of the channel pins, stm32l432xx only has group 2
8786
tsc.iogcsr.write(|w| { w.g2e().set_bit() });
@@ -95,12 +94,14 @@ impl<SPIN, PINS> Tsc<SPIN, PINS> {
9594
Tsc {
9695
tsc: tsc,
9796
sample_pin: sample_pin,
98-
pins: pins,
97+
// pins: pins,
9998
}
10099
}
101100

102101
/// Starts a charge acquisition
103-
pub fn start(&self) {
102+
pub fn start<PIN>(&self, _input: &mut PIN)
103+
where PIN: ChannelPin<TSC>
104+
{
104105
// clear interrupt & flags
105106
self.tsc.icr.write(|w| {
106107
w.eoaic().set_bit()
@@ -112,6 +113,11 @@ impl<SPIN, PINS> Tsc<SPIN, PINS> {
112113
w.iodef().clear_bit()
113114
});
114115

116+
// Set the channel pin
117+
self.tsc.ioccr.write(|w| unsafe {
118+
w.bits(PIN::OFFSET)
119+
});
120+
115121
self.tsc.cr.modify(|_, w| { w.start().set_bit() });
116122
}
117123

@@ -152,7 +158,7 @@ impl<SPIN, PINS> Tsc<SPIN, PINS> {
152158
}
153159

154160
/// Releases the TSC peripheral and associated pins
155-
pub fn free(self) -> (TSC, SPIN, PINS) {
156-
(self.tsc, self.sample_pin, self.pins)
161+
pub fn free(self) -> (TSC, SPIN) {
162+
(self.tsc, self.sample_pin)
157163
}
158164
}

0 commit comments

Comments
 (0)