Skip to content

Commit

Permalink
Merge pull request #11 from MabezDev/touch-sense-controller
Browse files Browse the repository at this point in the history
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`
  • Loading branch information
MabezDev authored Aug 21, 2018
2 parents 10396f0 + 96d63d1 commit b5deff2
Show file tree
Hide file tree
Showing 11 changed files with 382 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"debugger_args": [
"-nx" // dont use the .gdbinit file
],
"executable": "./target/thumbv7em-none-eabi/debug/examples/rtc",
"executable": "./target/thumbv7em-none-eabi/debug/examples/touch",
"remote": true,
"target": ":3333",
"cwd": "${workspaceRoot}",
Expand Down
73 changes: 73 additions & 0 deletions examples/touch.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
//! Test the serial interface
//!
//! This example requires you to short (connect) the TX and RX pins.
#![deny(unsafe_code)]
// #![deny(warnings)]
#![no_main]
#![no_std]

extern crate cortex_m;
#[macro_use(entry, exception)]
extern crate cortex_m_rt as rt;
extern crate panic_semihosting;

extern crate stm32l432xx_hal as hal;


use hal::prelude::*;
use hal::stm32l4::stm32l4x2;
use hal::tsc::Tsc;
use rt::ExceptionFrame;

entry!(main);

fn main() -> ! {
let p = stm32l4x2::Peripherals::take().unwrap();
// let cp = cortex_m::Peripherals::take().unwrap();

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

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

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

let sample_pin = gpiob.pb4.into_touch_sample(&mut gpiob.moder, &mut gpiob.otyper, &mut gpiob.afrl);
let mut c1 = gpiob.pb5.into_touch_channel(&mut gpiob.moder, &mut gpiob.otyper, &mut gpiob.afrl);
let mut c2 = gpiob.pb6.into_touch_channel(&mut gpiob.moder, &mut gpiob.otyper, &mut gpiob.afrl);
// let mut c3 = gpiob.pb7.into_touch_channel(&mut gpiob.moder, &mut gpiob.otyper, &mut gpiob.afrl);

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

let baseline = tsc.acquire(&mut c1).unwrap();
let threshold = (baseline / 100) * 60;

loop {
let touched = tsc.acquire(&mut c1).unwrap();
let _touched_c2 = tsc.acquire(&mut c2).unwrap();
if touched < threshold {
led.set_high();
} else {
led.set_low();
}
}
}

exception!(HardFault, hard_fault);

fn hard_fault(ef: &ExceptionFrame) -> ! {
panic!("{:#?}", ef);
}

exception!(*, default_handler);

fn default_handler(irqn: i16) {
panic!("Unhandled exception (IRQn = {})", irqn);
}
2 changes: 1 addition & 1 deletion src/datetime.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/// Date and timer units & helper functions
//! Date and timer units & helper functions
/// Seconds
#[derive(Clone, Copy, Debug)]
Expand Down
2 changes: 2 additions & 0 deletions src/dma.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! Direct Memory Access Engine
#![allow(dead_code)]

use core::marker::PhantomData;
Expand Down
87 changes: 82 additions & 5 deletions src/gpio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ pub struct PushPull;
/// Open drain output (type state)
pub struct OpenDrain;

/// Alternate mode (type state)
pub struct Alternate<AF, MODE>
{
_af: PhantomData<AF>,
_mode: PhantomData<MODE>,
}

/// Alternate function 0 (type state)
pub struct AF0;

Expand Down Expand Up @@ -99,7 +106,7 @@ macro_rules! gpio {

use rcc::AHB2;
use super::{
AF4, AF5, AF6, AF7, Floating, GpioExt, Input, OpenDrain, Output,
Alternate, AF4, AF5, AF6, AF7, AF8, AF9, Floating, GpioExt, Input, OpenDrain, Output,
PullDown, PullUp, PushPull,
};

Expand Down Expand Up @@ -229,7 +236,7 @@ macro_rules! gpio {
self,
moder: &mut MODER,
afr: &mut $AFR,
) -> $PXi<AF4> {
) -> $PXi<Alternate<AF4, MODE>> {
let offset = 2 * $i;

// alternate function mode
Expand All @@ -252,7 +259,7 @@ macro_rules! gpio {
self,
moder: &mut MODER,
afr: &mut $AFR,
) -> $PXi<AF5> {
) -> $PXi<Alternate<AF5, MODE>> {
let offset = 2 * $i;

// alternate function mode
Expand All @@ -275,7 +282,7 @@ macro_rules! gpio {
self,
moder: &mut MODER,
afr: &mut $AFR,
) -> $PXi<AF6> {
) -> $PXi<Alternate<AF6, MODE>> {
let offset = 2 * $i;

// alternate function mode
Expand All @@ -298,7 +305,7 @@ macro_rules! gpio {
self,
moder: &mut MODER,
afr: &mut $AFR,
) -> $PXi<AF7> {
) -> $PXi<Alternate<AF7, MODE>> {
let offset = 2 * $i;

// alternate function mode
Expand All @@ -317,6 +324,54 @@ macro_rules! gpio {
$PXi { _mode: PhantomData }
}

/// Configures the pin to serve as alternate function 8 (AF8)
pub fn into_af8(
self,
moder: &mut MODER,
afr: &mut $AFR,
) -> $PXi<Alternate<AF8, MODE>> {
let offset = 2 * $i;

// alternate function mode
let mode = 0b10;
moder.moder().modify(|r, w| unsafe {
w.bits((r.bits() & !(0b11 << offset)) | (mode << offset))
});

let af = 8;
let offset = 4 * ($i % 8);

afr.afr().modify(|r, w| unsafe {
w.bits((r.bits() & !(0b1111 << offset)) | (af << offset))
});

$PXi { _mode: PhantomData }
}

/// Configures the pin to serve as alternate function 9 (AF9)
pub fn into_af9(
self,
moder: &mut MODER,
afr: &mut $AFR,
) -> $PXi<Alternate<AF9, MODE>> {
let offset = 2 * $i;

// alternate function mode
let mode = 0b10;
moder.moder().modify(|r, w| unsafe {
w.bits((r.bits() & !(0b11 << offset)) | (mode << offset))
});

let af = 9;
let offset = 4 * ($i % 8);

afr.afr().modify(|r, w| unsafe {
w.bits((r.bits() & !(0b1111 << offset)) | (af << offset))
});

$PXi { _mode: PhantomData }
}

/// Configures the pin to operate as a floating input pin
pub fn into_floating_input(
self,
Expand Down Expand Up @@ -423,6 +478,28 @@ macro_rules! gpio {

$PXi { _mode: PhantomData }
}

/// Configures the pin to operate as an touch sample
pub fn into_touch_sample(
self,
moder: &mut MODER,
otyper: &mut OTYPER,
afr: &mut $AFR,
) -> $PXi<Alternate<AF9, Output<OpenDrain>>> {
let od = self.into_open_drain_output(moder, otyper);
od.into_af9(moder, afr)
}

/// Configures the pin to operate as an touch channel
pub fn into_touch_channel(
self,
moder: &mut MODER,
otyper: &mut OTYPER,
afr: &mut $AFR,
) -> $PXi<Alternate<AF9, Output<PushPull>>> {
let od = self.into_push_pull_output(moder, otyper);
od.into_af9(moder, afr)
}
}

impl $PXi<Output<OpenDrain>> {
Expand Down
6 changes: 4 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
//! STM32l432xx Hardware abstraction layer
#![no_std]

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

extern crate cortex_m;
extern crate cast;
Expand All @@ -25,6 +26,7 @@ pub mod spi;
pub mod rtc;
pub mod pwr;
pub mod datetime;
pub mod tsc;


#[cfg(test)]
Expand Down
2 changes: 2 additions & 0 deletions src/pwr.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! Power management
use rcc::{APB1R1};
use stm32l4::stm32l4x2::{pwr, PWR};

Expand Down
2 changes: 1 addition & 1 deletion src/rtc.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/// RTC peripheral abstraction
//! RTC peripheral abstraction
use datetime::*;
use rcc::{BDCR, APB1R1};
Expand Down
8 changes: 4 additions & 4 deletions src/serial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use void::Void;

use gpio::gpioa::{PA10, PA2, PA3, PA9};
use gpio::gpiob::{PB6, PB7};
use gpio::AF7;
use gpio::{AF7, Alternate, Input, Floating};
use rcc::{APB1R1, APB2, Clocks};
use time::Bps;
use dma::{dma1, CircBuffer};
Expand Down Expand Up @@ -45,15 +45,15 @@ pub trait Pins<USART> {
const REMAP: u8;
}

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

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

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

Expand Down
8 changes: 4 additions & 4 deletions src/spi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use nb;
use stm32l4::stm32l4x2::{SPI1, /* TODO SPI2, */ SPI3};

use gpio::gpioa::{PA5, PA6, PA7};
use gpio::{AF5};
use gpio::{AF5, Input, Floating, Alternate};
use rcc::{APB1R1, APB2, Clocks};
use time::Hertz;

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

impl Pins<SPI1>
for (
PA5<AF5>,
PA6<AF5>,
PA7<AF5>,
PA5<Alternate<AF5, Input<Floating>>>,
PA6<Alternate<AF5, Input<Floating>>>,
PA7<Alternate<AF5, Input<Floating>>>,
)
{
const REMAP: bool = false; // TODO REMAP
Expand Down
Loading

0 comments on commit b5deff2

Please sign in to comment.