Skip to content

Commit b5deff2

Browse files
authored
Merge pull request #11 from MabezDev/touch-sense-controller
Touch sense controller * Move semantics on the sample pin means only that pin and the other pins in the group * Blocking api with `acquire` * Event driven with `listen` & `start`
2 parents 10396f0 + 96d63d1 commit b5deff2

File tree

11 files changed

+382
-18
lines changed

11 files changed

+382
-18
lines changed

.vscode/launch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"debugger_args": [
1414
"-nx" // dont use the .gdbinit file
1515
],
16-
"executable": "./target/thumbv7em-none-eabi/debug/examples/rtc",
16+
"executable": "./target/thumbv7em-none-eabi/debug/examples/touch",
1717
"remote": true,
1818
"target": ":3333",
1919
"cwd": "${workspaceRoot}",

examples/touch.rs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
//! Test the serial interface
2+
//!
3+
//! This example requires you to short (connect) the TX and RX pins.
4+
#![deny(unsafe_code)]
5+
// #![deny(warnings)]
6+
#![no_main]
7+
#![no_std]
8+
9+
extern crate cortex_m;
10+
#[macro_use(entry, exception)]
11+
extern crate cortex_m_rt as rt;
12+
extern crate panic_semihosting;
13+
14+
extern crate stm32l432xx_hal as hal;
15+
16+
17+
use hal::prelude::*;
18+
use hal::stm32l4::stm32l4x2;
19+
use hal::tsc::Tsc;
20+
use rt::ExceptionFrame;
21+
22+
entry!(main);
23+
24+
fn main() -> ! {
25+
let p = stm32l4x2::Peripherals::take().unwrap();
26+
// let cp = cortex_m::Peripherals::take().unwrap();
27+
28+
let mut flash = p.FLASH.constrain();
29+
let mut rcc = p.RCC.constrain();
30+
// let mut gpioa = p.GPIOA.split(&mut rcc.ahb2);
31+
let mut gpiob = p.GPIOB.split(&mut rcc.ahb2);
32+
33+
// clock configuration using the default settings (all clocks run at 8 MHz)
34+
let _clocks = rcc.cfgr.freeze(&mut flash.acr);
35+
// TRY this alternate clock configuration (clocks run at nearly the maximum frequency)
36+
// let clocks = rcc.cfgr.sysclk(64.mhz()).pclk1(32.mhz()).freeze(&mut flash.acr);
37+
38+
// let mut delay = Delay::new(cp.SYST, clocks);
39+
let mut led = gpiob.pb3.into_push_pull_output(&mut gpiob.moder, &mut gpiob.otyper);
40+
41+
let sample_pin = gpiob.pb4.into_touch_sample(&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);
45+
46+
// , (c1, c2, c3)
47+
let tsc = Tsc::tsc(p.TSC, sample_pin, &mut rcc.ahb1);
48+
49+
let baseline = tsc.acquire(&mut c1).unwrap();
50+
let threshold = (baseline / 100) * 60;
51+
52+
loop {
53+
let touched = tsc.acquire(&mut c1).unwrap();
54+
let _touched_c2 = tsc.acquire(&mut c2).unwrap();
55+
if touched < threshold {
56+
led.set_high();
57+
} else {
58+
led.set_low();
59+
}
60+
}
61+
}
62+
63+
exception!(HardFault, hard_fault);
64+
65+
fn hard_fault(ef: &ExceptionFrame) -> ! {
66+
panic!("{:#?}", ef);
67+
}
68+
69+
exception!(*, default_handler);
70+
71+
fn default_handler(irqn: i16) {
72+
panic!("Unhandled exception (IRQn = {})", irqn);
73+
}

src/datetime.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/// Date and timer units & helper functions
1+
//! Date and timer units & helper functions
22
33
/// Seconds
44
#[derive(Clone, Copy, Debug)]

src/dma.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! Direct Memory Access Engine
2+
13
#![allow(dead_code)]
24

35
use core::marker::PhantomData;

src/gpio.rs

Lines changed: 82 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ pub struct PushPull;
3838
/// Open drain output (type state)
3939
pub struct OpenDrain;
4040

41+
/// Alternate mode (type state)
42+
pub struct Alternate<AF, MODE>
43+
{
44+
_af: PhantomData<AF>,
45+
_mode: PhantomData<MODE>,
46+
}
47+
4148
/// Alternate function 0 (type state)
4249
pub struct AF0;
4350

@@ -99,7 +106,7 @@ macro_rules! gpio {
99106

100107
use rcc::AHB2;
101108
use super::{
102-
AF4, AF5, AF6, AF7, Floating, GpioExt, Input, OpenDrain, Output,
109+
Alternate, AF4, AF5, AF6, AF7, AF8, AF9, Floating, GpioExt, Input, OpenDrain, Output,
103110
PullDown, PullUp, PushPull,
104111
};
105112

@@ -229,7 +236,7 @@ macro_rules! gpio {
229236
self,
230237
moder: &mut MODER,
231238
afr: &mut $AFR,
232-
) -> $PXi<AF4> {
239+
) -> $PXi<Alternate<AF4, MODE>> {
233240
let offset = 2 * $i;
234241

235242
// alternate function mode
@@ -252,7 +259,7 @@ macro_rules! gpio {
252259
self,
253260
moder: &mut MODER,
254261
afr: &mut $AFR,
255-
) -> $PXi<AF5> {
262+
) -> $PXi<Alternate<AF5, MODE>> {
256263
let offset = 2 * $i;
257264

258265
// alternate function mode
@@ -275,7 +282,7 @@ macro_rules! gpio {
275282
self,
276283
moder: &mut MODER,
277284
afr: &mut $AFR,
278-
) -> $PXi<AF6> {
285+
) -> $PXi<Alternate<AF6, MODE>> {
279286
let offset = 2 * $i;
280287

281288
// alternate function mode
@@ -298,7 +305,7 @@ macro_rules! gpio {
298305
self,
299306
moder: &mut MODER,
300307
afr: &mut $AFR,
301-
) -> $PXi<AF7> {
308+
) -> $PXi<Alternate<AF7, MODE>> {
302309
let offset = 2 * $i;
303310

304311
// alternate function mode
@@ -317,6 +324,54 @@ macro_rules! gpio {
317324
$PXi { _mode: PhantomData }
318325
}
319326

327+
/// Configures the pin to serve as alternate function 8 (AF8)
328+
pub fn into_af8(
329+
self,
330+
moder: &mut MODER,
331+
afr: &mut $AFR,
332+
) -> $PXi<Alternate<AF8, MODE>> {
333+
let offset = 2 * $i;
334+
335+
// alternate function mode
336+
let mode = 0b10;
337+
moder.moder().modify(|r, w| unsafe {
338+
w.bits((r.bits() & !(0b11 << offset)) | (mode << offset))
339+
});
340+
341+
let af = 8;
342+
let offset = 4 * ($i % 8);
343+
344+
afr.afr().modify(|r, w| unsafe {
345+
w.bits((r.bits() & !(0b1111 << offset)) | (af << offset))
346+
});
347+
348+
$PXi { _mode: PhantomData }
349+
}
350+
351+
/// Configures the pin to serve as alternate function 9 (AF9)
352+
pub fn into_af9(
353+
self,
354+
moder: &mut MODER,
355+
afr: &mut $AFR,
356+
) -> $PXi<Alternate<AF9, MODE>> {
357+
let offset = 2 * $i;
358+
359+
// alternate function mode
360+
let mode = 0b10;
361+
moder.moder().modify(|r, w| unsafe {
362+
w.bits((r.bits() & !(0b11 << offset)) | (mode << offset))
363+
});
364+
365+
let af = 9;
366+
let offset = 4 * ($i % 8);
367+
368+
afr.afr().modify(|r, w| unsafe {
369+
w.bits((r.bits() & !(0b1111 << offset)) | (af << offset))
370+
});
371+
372+
$PXi { _mode: PhantomData }
373+
}
374+
320375
/// Configures the pin to operate as a floating input pin
321376
pub fn into_floating_input(
322377
self,
@@ -423,6 +478,28 @@ macro_rules! gpio {
423478

424479
$PXi { _mode: PhantomData }
425480
}
481+
482+
/// Configures the pin to operate as an touch sample
483+
pub fn into_touch_sample(
484+
self,
485+
moder: &mut MODER,
486+
otyper: &mut OTYPER,
487+
afr: &mut $AFR,
488+
) -> $PXi<Alternate<AF9, Output<OpenDrain>>> {
489+
let od = self.into_open_drain_output(moder, otyper);
490+
od.into_af9(moder, afr)
491+
}
492+
493+
/// Configures the pin to operate as an touch channel
494+
pub fn into_touch_channel(
495+
self,
496+
moder: &mut MODER,
497+
otyper: &mut OTYPER,
498+
afr: &mut $AFR,
499+
) -> $PXi<Alternate<AF9, Output<PushPull>>> {
500+
let od = self.into_push_pull_output(moder, otyper);
501+
od.into_af9(moder, afr)
502+
}
426503
}
427504

428505
impl $PXi<Output<OpenDrain>> {

src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
//! STM32l432xx Hardware abstraction layer
2+
13
#![no_std]
24

3-
// currently these are the only 2 unstable features, never_typoe can replaced with void, nto sure what unsize can be repalced with
5+
// TODO, remove this feature (currently required in dma.rs)
46
#![feature(unsize)]
5-
#![feature(never_type)]
67

78
extern crate cortex_m;
89
extern crate cast;
@@ -25,6 +26,7 @@ pub mod spi;
2526
pub mod rtc;
2627
pub mod pwr;
2728
pub mod datetime;
29+
pub mod tsc;
2830

2931

3032
#[cfg(test)]

src/pwr.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! Power management
2+
13
use rcc::{APB1R1};
24
use stm32l4::stm32l4x2::{pwr, PWR};
35

src/rtc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/// RTC peripheral abstraction
1+
//! RTC peripheral abstraction
22
33
use datetime::*;
44
use rcc::{BDCR, APB1R1};

src/serial.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use void::Void;
1111

1212
use gpio::gpioa::{PA10, PA2, PA3, PA9};
1313
use gpio::gpiob::{PB6, PB7};
14-
use gpio::AF7;
14+
use gpio::{AF7, Alternate, Input, Floating};
1515
use rcc::{APB1R1, APB2, Clocks};
1616
use time::Bps;
1717
use dma::{dma1, CircBuffer};
@@ -45,15 +45,15 @@ pub trait Pins<USART> {
4545
const REMAP: u8;
4646
}
4747

48-
impl Pins<USART1> for (PA9<AF7>, PA10<AF7>) {
48+
impl Pins<USART1> for (PA9<Alternate<AF7, Input<Floating>>>, PA10<Alternate<AF7, Input<Floating>>>) {
4949
const REMAP: u8 = 0;
5050
}
5151

52-
impl Pins<USART1> for (PB6<AF7>, PB7<AF7>) {
52+
impl Pins<USART1> for (PB6<Alternate<AF7, Input<Floating>>>, PB7<Alternate<AF7, Input<Floating>>>) {
5353
const REMAP: u8 = 1;
5454
}
5555

56-
impl Pins<USART2> for (PA2<AF7>, PA3<AF7>) {
56+
impl Pins<USART2> for (PA2<Alternate<AF7, Input<Floating>>>, PA3<Alternate<AF7, Input<Floating>>>) {
5757
const REMAP: u8 = 0;
5858
}
5959

src/spi.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use nb;
77
use stm32l4::stm32l4x2::{SPI1, /* TODO SPI2, */ SPI3};
88

99
use gpio::gpioa::{PA5, PA6, PA7};
10-
use gpio::{AF5};
10+
use gpio::{AF5, Input, Floating, Alternate};
1111
use rcc::{APB1R1, APB2, Clocks};
1212
use time::Hertz;
1313

@@ -30,9 +30,9 @@ pub trait Pins<SPI> {
3030

3131
impl Pins<SPI1>
3232
for (
33-
PA5<AF5>,
34-
PA6<AF5>,
35-
PA7<AF5>,
33+
PA5<Alternate<AF5, Input<Floating>>>,
34+
PA6<Alternate<AF5, Input<Floating>>>,
35+
PA7<Alternate<AF5, Input<Floating>>>,
3636
)
3737
{
3838
const REMAP: bool = false; // TODO REMAP

0 commit comments

Comments
 (0)