diff options
| author | Karun <[email protected]> | 2024-06-13 13:36:05 -0400 |
|---|---|---|
| committer | Karun <[email protected]> | 2024-06-13 13:46:10 -0400 |
| commit | 54fa33cbaefa7b934aa600238cbc808ecf0b5f5b (patch) | |
| tree | 0fb4550686bb34010c9a3369b778e637233b3767 /examples/stm32u5/src | |
| parent | 8d79679bb27ce6eca8904e167a8762ad5fc90604 (diff) | |
Add example for touch sensitive controller
Diffstat (limited to 'examples/stm32u5/src')
| -rw-r--r-- | examples/stm32u5/src/bin/tsc.rs | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/examples/stm32u5/src/bin/tsc.rs b/examples/stm32u5/src/bin/tsc.rs new file mode 100644 index 000000000..f5593d1c4 --- /dev/null +++ b/examples/stm32u5/src/bin/tsc.rs | |||
| @@ -0,0 +1,89 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | |||
| 4 | use defmt::*; | ||
| 5 | use embassy_stm32::tsc::{self, *}; | ||
| 6 | use embassy_time::Timer; | ||
| 7 | use {defmt_rtt as _, panic_probe as _}; | ||
| 8 | |||
| 9 | #[cortex_m_rt::exception] | ||
| 10 | unsafe fn HardFault(_: &cortex_m_rt::ExceptionFrame) -> ! { | ||
| 11 | cortex_m::peripheral::SCB::sys_reset(); | ||
| 12 | } | ||
| 13 | |||
| 14 | #[embassy_executor::main] | ||
| 15 | async fn main(_spawner: embassy_executor::Spawner) { | ||
| 16 | let device_config = embassy_stm32::Config::default(); | ||
| 17 | let context = embassy_stm32::init(device_config); | ||
| 18 | |||
| 19 | let config = tsc::Config { | ||
| 20 | ct_pulse_high_length: ChargeTransferPulseCycle::_2, | ||
| 21 | ct_pulse_low_length: ChargeTransferPulseCycle::_2, | ||
| 22 | spread_spectrum: false, | ||
| 23 | spread_spectrum_deviation: SSDeviation::new(2).unwrap(), | ||
| 24 | spread_spectrum_prescaler: false, | ||
| 25 | pulse_generator_prescaler: PGPrescalerDivider::_4, | ||
| 26 | max_count_value: MaxCount::_8191, | ||
| 27 | io_default_mode: false, | ||
| 28 | synchro_pin_polarity: false, | ||
| 29 | acquisition_mode: false, | ||
| 30 | max_count_interrupt: false, | ||
| 31 | channel_ios: TscIOPin::Group2Io2 | TscIOPin::Group7Io3, | ||
| 32 | shield_ios: TscIOPin::Group1Io3.into(), | ||
| 33 | sampling_ios: TscIOPin::Group1Io2 | TscIOPin::Group2Io1 | TscIOPin::Group7Io2, | ||
| 34 | }; | ||
| 35 | |||
| 36 | let mut g1: PinGroup<embassy_stm32::peripherals::TSC, G1> = PinGroup::new(); | ||
| 37 | g1.set_io2(context.PB13, PinType::Sample); | ||
| 38 | g1.set_io3(context.PB14, PinType::Shield); | ||
| 39 | |||
| 40 | let mut g2: PinGroup<embassy_stm32::peripherals::TSC, G2> = PinGroup::new(); | ||
| 41 | g2.set_io1(context.PB4, PinType::Sample); | ||
| 42 | g2.set_io2(context.PB5, PinType::Channel); | ||
| 43 | |||
| 44 | let mut g7: PinGroup<embassy_stm32::peripherals::TSC, G7> = PinGroup::new(); | ||
| 45 | g7.set_io2(context.PE3, PinType::Sample); | ||
| 46 | g7.set_io3(context.PE4, PinType::Channel); | ||
| 47 | |||
| 48 | let mut touch_controller = tsc::Tsc::new( | ||
| 49 | context.TSC, | ||
| 50 | Some(g1), | ||
| 51 | Some(g2), | ||
| 52 | None, | ||
| 53 | None, | ||
| 54 | None, | ||
| 55 | None, | ||
| 56 | Some(g7), | ||
| 57 | None, | ||
| 58 | config, | ||
| 59 | ); | ||
| 60 | |||
| 61 | touch_controller.discharge_io(true); | ||
| 62 | Timer::after_millis(1).await; | ||
| 63 | |||
| 64 | touch_controller.start(); | ||
| 65 | |||
| 66 | let mut group_two_val = 0; | ||
| 67 | let mut group_seven_val = 0; | ||
| 68 | info!("Starting touch_controller interface"); | ||
| 69 | loop { | ||
| 70 | touch_controller.poll_for_acquisition(); | ||
| 71 | touch_controller.discharge_io(true); | ||
| 72 | Timer::after_millis(1).await; | ||
| 73 | |||
| 74 | if touch_controller.group_get_status(Group::Two) == GroupStatus::Complete { | ||
| 75 | group_two_val = touch_controller.group_get_value(Group::Two); | ||
| 76 | } | ||
| 77 | |||
| 78 | if touch_controller.group_get_status(Group::Seven) == GroupStatus::Complete { | ||
| 79 | group_seven_val = touch_controller.group_get_value(Group::Seven); | ||
| 80 | } | ||
| 81 | |||
| 82 | info!( | ||
| 83 | "Group Two value: {}, Group Seven value: {},", | ||
| 84 | group_two_val, group_seven_val | ||
| 85 | ); | ||
| 86 | |||
| 87 | touch_controller.start(); | ||
| 88 | } | ||
| 89 | } | ||
