1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
|
use core::marker::PhantomData;
use core::ops::BitOr;
use super::errors::GroupError;
use super::io_pin::*;
use super::Instance;
use crate::gpio::{AfType, AnyPin, OutputType, Speed};
use crate::Peri;
/// Pin type definition to control IO parameters
#[derive(PartialEq, Clone, Copy)]
pub enum PinType {
/// Sensing channel pin connected to an electrode
Channel,
/// Sampling capacitor pin, one required for every pin group
Sample,
/// Shield pin connected to capacitive sensing shield
Shield,
}
/// Pin struct that maintains usage
#[allow(missing_docs)]
pub struct Pin<'d, T, Group> {
_pin: Peri<'d, AnyPin>,
role: PinType,
tsc_io_pin: IOPin,
phantom: PhantomData<(T, Group)>,
}
impl<'d, T, Group> Pin<'d, T, Group> {
/// Returns the role of this TSC pin.
///
/// The role indicates whether this pin is configured as a channel,
/// sampling capacitor, or shield in the TSC group.
///
/// # Returns
/// The `PinType` representing the role of this pin.
pub fn role(&self) -> PinType {
self.role
}
/// Returns the TSC IO pin associated with this pin.
///
/// This method provides access to the specific TSC IO pin configuration,
/// which includes information about the pin's group and position within that group.
///
/// # Returns
/// The `IOPin` representing this pin's TSC-specific configuration.
pub fn tsc_io_pin(&self) -> IOPin {
self.tsc_io_pin
}
}
/// Represents a group of TSC (Touch Sensing Controller) pins.
///
/// In the TSC peripheral, pins are organized into groups of four IOs. Each group
/// must have exactly one sampling capacitor pin and can have multiple channel pins
/// or a single shield pin. This structure encapsulates these pin configurations
/// for a single TSC group.
///
/// # Pin Roles
/// - Sampling Capacitor: One required per group, used for charge transfer.
/// - Channel: Sensing pins connected to electrodes for touch detection.
/// - Shield: Optional, used for active shielding to improve sensitivity.
///
/// # Constraints
/// - Each group must have exactly one sampling capacitor pin.
/// - A group can have either channel pins or a shield pin, but not both.
/// - No more than one shield pin is allowed across all groups.
#[allow(missing_docs)]
pub struct PinGroup<'d, T, Group> {
pin1: Option<Pin<'d, T, Group>>,
pin2: Option<Pin<'d, T, Group>>,
pin3: Option<Pin<'d, T, Group>>,
pin4: Option<Pin<'d, T, Group>>,
}
impl<'d, T, G> Default for PinGroup<'d, T, G> {
fn default() -> Self {
Self {
pin1: None,
pin2: None,
pin3: None,
pin4: None,
}
}
}
/// Defines roles and traits for TSC (Touch Sensing Controller) pins.
///
/// This module contains marker types and traits that represent different roles
/// a TSC pin can have, such as channel, sample, or shield.
pub mod pin_roles {
use super::{OutputType, PinType};
/// Marker type for a TSC channel pin.
#[derive(PartialEq, Clone, Copy, Debug)]
pub struct Channel;
/// Marker type for a TSC sampling pin.
#[derive(PartialEq, Clone, Copy, Debug)]
pub struct Sample;
/// Marker type for a TSC shield pin.
#[derive(PartialEq, Clone, Copy, Debug)]
pub struct Shield;
/// Trait for TSC pin roles.
///
/// This trait defines the behavior and properties of different TSC pin roles.
/// It is implemented by the marker types `Channel`, `Sample`, and `Shield`.
pub trait Role {
/// Returns the `PinType` associated with this role.
fn pin_type() -> PinType;
/// Returns the `OutputType` associated with this role.
fn output_type() -> OutputType;
}
impl Role for Channel {
fn pin_type() -> PinType {
PinType::Channel
}
fn output_type() -> OutputType {
OutputType::PushPull
}
}
impl Role for Sample {
fn pin_type() -> PinType {
PinType::Sample
}
fn output_type() -> OutputType {
OutputType::OpenDrain
}
}
impl Role for Shield {
fn pin_type() -> PinType {
PinType::Shield
}
fn output_type() -> OutputType {
OutputType::PushPull
}
}
}
/// Represents a group of TSC pins with their associated roles.
///
/// This struct allows for type-safe configuration of TSC pin groups,
/// ensuring that pins are assigned appropriate roles within their group.
/// This type is essentially just a wrapper type around a `PinGroup` value.
///
/// # Type Parameters
/// - `'d`: Lifetime of the pin group.
/// - `T`: The TSC instance type.
/// - `G`: The group identifier.
/// - `R1`, `R2`, `R3`, `R4`: Role types for each pin in the group, defaulting to `Channel`.
pub struct PinGroupWithRoles<
'd,
T: Instance,
G,
R1 = pin_roles::Channel,
R2 = pin_roles::Channel,
R3 = pin_roles::Channel,
R4 = pin_roles::Channel,
> {
/// The underlying pin group without role information.
pub pin_group: PinGroup<'d, T, G>,
_phantom: PhantomData<(R1, R2, R3, R4)>,
}
impl<'d, T: Instance, G, R1, R2, R3, R4> Default for PinGroupWithRoles<'d, T, G, R1, R2, R3, R4> {
fn default() -> Self {
Self {
pin_group: PinGroup::default(),
_phantom: PhantomData,
}
}
}
impl<'d, T: Instance, G> PinGroup<'d, T, G> {
fn contains_exactly_one_shield_pin(&self) -> bool {
let shield_count = self.shield_pins().count();
shield_count == 1
}
fn check_group(&self) -> Result<(), GroupError> {
let mut channel_count = 0;
let mut shield_count = 0;
let mut sample_count = 0;
for pin in self.pins().into_iter().flatten() {
match pin.role {
PinType::Channel => {
channel_count += 1;
}
PinType::Shield => {
shield_count += 1;
}
PinType::Sample => {
sample_count += 1;
}
}
}
// Every group requires exactly one sampling capacitor
if sample_count != 1 {
return Err(GroupError::NoSamplingCapacitor);
}
// Each group must have at least one shield or channel IO
if shield_count == 0 && channel_count == 0 {
return Err(GroupError::NoChannelOrShield);
}
// Any group can either contain channel ios or a shield IO.
// (An active shield requires its own sampling capacitor)
if shield_count != 0 && channel_count != 0 {
return Err(GroupError::MixedChannelAndShield);
}
// No more than one shield IO is allow per group and amongst all groups
if shield_count > 1 {
return Err(GroupError::MultipleShields);
}
Ok(())
}
/// Returns a reference to the first pin in the group, if configured.
pub fn pin1(&self) -> Option<&Pin<'d, T, G>> {
self.pin1.as_ref()
}
/// Returns a reference to the second pin in the group, if configured.
pub fn pin2(&self) -> Option<&Pin<'d, T, G>> {
self.pin2.as_ref()
}
/// Returns a reference to the third pin in the group, if configured.
pub fn pin3(&self) -> Option<&Pin<'d, T, G>> {
self.pin3.as_ref()
}
/// Returns a reference to the fourth pin in the group, if configured.
pub fn pin4(&self) -> Option<&Pin<'d, T, G>> {
self.pin4.as_ref()
}
fn sample_pins(&self) -> impl Iterator<Item = IOPin> + '_ {
self.pins_filtered(PinType::Sample)
}
fn shield_pins(&self) -> impl Iterator<Item = IOPin> + '_ {
self.pins_filtered(PinType::Shield)
}
fn channel_pins(&self) -> impl Iterator<Item = IOPin> + '_ {
self.pins_filtered(PinType::Channel)
}
fn pins_filtered(&self, pin_type: PinType) -> impl Iterator<Item = IOPin> + '_ {
self.pins().into_iter().filter_map(move |pin| {
pin.as_ref()
.and_then(|p| if p.role == pin_type { Some(p.tsc_io_pin) } else { None })
})
}
fn make_channel_ios_mask(&self) -> u32 {
self.channel_pins().fold(0, u32::bitor)
}
fn make_shield_ios_mask(&self) -> u32 {
self.shield_pins().fold(0, u32::bitor)
}
fn make_sample_ios_mask(&self) -> u32 {
self.sample_pins().fold(0, u32::bitor)
}
fn pins(&self) -> [&Option<Pin<'d, T, G>>; 4] {
[&self.pin1, &self.pin2, &self.pin3, &self.pin4]
}
fn pins_mut(&mut self) -> [&mut Option<Pin<'d, T, G>>; 4] {
[&mut self.pin1, &mut self.pin2, &mut self.pin3, &mut self.pin4]
}
}
#[cfg(any(tsc_v2, tsc_v3))]
macro_rules! TSC_V2_V3_GUARD {
($e:expr) => {{
#[cfg(any(tsc_v2, tsc_v3))]
{
$e
}
#[cfg(not(any(tsc_v2, tsc_v3)))]
{
compile_error!("Group 7 is not supported in this TSC version")
}
}};
}
#[cfg(tsc_v3)]
macro_rules! TSC_V3_GUARD {
($e:expr) => {{
#[cfg(tsc_v3)]
{
$e
}
#[cfg(not(tsc_v3))]
{
compile_error!("Group 8 is not supported in this TSC version")
}
}};
}
macro_rules! trait_to_io_pin {
(G1IO1Pin) => {
IOPin::Group1Io1
};
(G1IO2Pin) => {
IOPin::Group1Io2
};
(G1IO3Pin) => {
IOPin::Group1Io3
};
(G1IO4Pin) => {
IOPin::Group1Io4
};
(G2IO1Pin) => {
IOPin::Group2Io1
};
(G2IO2Pin) => {
IOPin::Group2Io2
};
(G2IO3Pin) => {
IOPin::Group2Io3
};
(G2IO4Pin) => {
IOPin::Group2Io4
};
(G3IO1Pin) => {
IOPin::Group3Io1
};
(G3IO2Pin) => {
IOPin::Group3Io2
};
(G3IO3Pin) => {
IOPin::Group3Io3
};
(G3IO4Pin) => {
IOPin::Group3Io4
};
(G4IO1Pin) => {
IOPin::Group4Io1
};
(G4IO2Pin) => {
IOPin::Group4Io2
};
(G4IO3Pin) => {
IOPin::Group4Io3
};
(G4IO4Pin) => {
IOPin::Group4Io4
};
(G5IO1Pin) => {
IOPin::Group5Io1
};
(G5IO2Pin) => {
IOPin::Group5Io2
};
(G5IO3Pin) => {
IOPin::Group5Io3
};
(G5IO4Pin) => {
IOPin::Group5Io4
};
(G6IO1Pin) => {
IOPin::Group6Io1
};
(G6IO2Pin) => {
IOPin::Group6Io2
};
(G6IO3Pin) => {
IOPin::Group6Io3
};
(G6IO4Pin) => {
IOPin::Group6Io4
};
(G7IO1Pin) => {
TSC_V2_V3_GUARD!(IOPin::Group7Io1)
};
(G7IO2Pin) => {
TSC_V2_V3_GUARD!(IOPin::Group7Io2)
};
(G7IO3Pin) => {
TSC_V2_V3_GUARD!(IOPin::Group7Io3)
};
(G7IO4Pin) => {
TSC_V2_V3_GUARD!(IOPin::Group7Io4)
};
(G8IO1Pin) => {
TSC_V3_GUARD!(IOPin::Group8Io1)
};
(G8IO2Pin) => {
TSC_V3_GUARD!(IOPin::Group8Io2)
};
(G8IO3Pin) => {
TSC_V3_GUARD!(IOPin::Group8Io3)
};
(G8IO4Pin) => {
TSC_V3_GUARD!(IOPin::Group8Io4)
};
}
macro_rules! impl_set_io {
($method:ident, $group:ident, $trait:ident, $index:expr) => {
#[doc = concat!("Create a new pin1 for ", stringify!($group), " TSC group instance.")]
pub fn $method<Role: pin_roles::Role>(&mut self, pin: Peri<'d, impl $trait<T>>) -> IOPinWithRole<$group, Role> {
critical_section::with(|_| {
pin.set_low();
set_as_af!(pin, AfType::output(Role::output_type(), Speed::VeryHigh));
let tsc_io_pin = trait_to_io_pin!($trait);
let new_pin = Pin {
_pin: pin.into(),
role: Role::pin_type(),
tsc_io_pin,
phantom: PhantomData,
};
*self.pin_group.pins_mut()[$index] = Some(new_pin);
IOPinWithRole {
pin: tsc_io_pin,
phantom: PhantomData,
}
})
}
};
}
macro_rules! group_impl {
($group:ident, $trait1:ident, $trait2:ident, $trait3:ident, $trait4:ident) => {
impl<'d, T: Instance, R1: pin_roles::Role, R2: pin_roles::Role, R3: pin_roles::Role, R4: pin_roles::Role>
PinGroupWithRoles<'d, T, $group, R1, R2, R3, R4>
{
impl_set_io!(set_io1, $group, $trait1, 0);
impl_set_io!(set_io2, $group, $trait2, 1);
impl_set_io!(set_io3, $group, $trait3, 2);
impl_set_io!(set_io4, $group, $trait4, 3);
}
};
}
group_impl!(G1, G1IO1Pin, G1IO2Pin, G1IO3Pin, G1IO4Pin);
group_impl!(G2, G2IO1Pin, G2IO2Pin, G2IO3Pin, G2IO4Pin);
group_impl!(G3, G3IO1Pin, G3IO2Pin, G3IO3Pin, G3IO4Pin);
group_impl!(G4, G4IO1Pin, G4IO2Pin, G4IO3Pin, G4IO4Pin);
group_impl!(G5, G5IO1Pin, G5IO2Pin, G5IO3Pin, G5IO4Pin);
group_impl!(G6, G6IO1Pin, G6IO2Pin, G6IO3Pin, G6IO4Pin);
#[cfg(any(tsc_v2, tsc_v3))]
group_impl!(G7, G7IO1Pin, G7IO2Pin, G7IO3Pin, G7IO4Pin);
#[cfg(tsc_v3)]
group_impl!(G8, G8IO1Pin, G8IO2Pin, G8IO3Pin, G8IO4Pin);
/// Group 1 marker type.
#[derive(Clone, Copy, Debug)]
pub enum G1 {}
/// Group 2 marker type.
#[derive(Clone, Copy, Debug)]
pub enum G2 {}
/// Group 3 marker type.
#[derive(Clone, Copy, Debug)]
pub enum G3 {}
/// Group 4 marker type.
#[derive(Clone, Copy, Debug)]
pub enum G4 {}
/// Group 5 marker type.
#[derive(Clone, Copy, Debug)]
pub enum G5 {}
/// Group 6 marker type.
#[derive(Clone, Copy, Debug)]
pub enum G6 {}
/// Group 7 marker type.
#[derive(Clone, Copy, Debug)]
pub enum G7 {}
/// Group 8 marker type.
#[derive(Clone, Copy, Debug)]
pub enum G8 {}
/// Represents the collection of pin groups for the Touch Sensing Controller (TSC).
///
/// Each field corresponds to a specific group of TSC pins:
#[allow(missing_docs)]
pub struct PinGroups<'d, T: Instance> {
pub g1: Option<PinGroup<'d, T, G1>>,
pub g2: Option<PinGroup<'d, T, G2>>,
pub g3: Option<PinGroup<'d, T, G3>>,
pub g4: Option<PinGroup<'d, T, G4>>,
pub g5: Option<PinGroup<'d, T, G5>>,
pub g6: Option<PinGroup<'d, T, G6>>,
#[cfg(any(tsc_v2, tsc_v3))]
pub g7: Option<PinGroup<'d, T, G7>>,
#[cfg(tsc_v3)]
pub g8: Option<PinGroup<'d, T, G8>>,
}
impl<'d, T: Instance> PinGroups<'d, T> {
pub(super) fn check(&self) -> Result<(), GroupError> {
let mut shield_count = 0;
// Helper function to check a single group
fn check_group<C, T: Instance>(
group: &Option<PinGroup<'_, T, C>>,
shield_count: &mut u32,
) -> Result<(), GroupError> {
if let Some(group) = group {
group.check_group()?;
if group.contains_exactly_one_shield_pin() {
*shield_count += 1;
if *shield_count > 1 {
return Err(GroupError::MultipleShields);
}
}
}
Ok(())
}
// Check each group
check_group(&self.g1, &mut shield_count)?;
check_group(&self.g2, &mut shield_count)?;
check_group(&self.g3, &mut shield_count)?;
check_group(&self.g4, &mut shield_count)?;
check_group(&self.g5, &mut shield_count)?;
check_group(&self.g6, &mut shield_count)?;
#[cfg(any(tsc_v2, tsc_v3))]
check_group(&self.g7, &mut shield_count)?;
#[cfg(tsc_v3)]
check_group(&self.g8, &mut shield_count)?;
Ok(())
}
pub(super) fn make_channel_ios_mask(&self) -> u32 {
#[allow(unused_mut)]
let mut mask = self.g1.as_ref().map_or(0, |g| g.make_channel_ios_mask())
| self.g2.as_ref().map_or(0, |g| g.make_channel_ios_mask())
| self.g3.as_ref().map_or(0, |g| g.make_channel_ios_mask())
| self.g4.as_ref().map_or(0, |g| g.make_channel_ios_mask())
| self.g5.as_ref().map_or(0, |g| g.make_channel_ios_mask())
| self.g6.as_ref().map_or(0, |g| g.make_channel_ios_mask());
#[cfg(any(tsc_v2, tsc_v3))]
{
mask |= self.g7.as_ref().map_or(0, |g| g.make_channel_ios_mask());
}
#[cfg(tsc_v3)]
{
mask |= self.g8.as_ref().map_or(0, |g| g.make_channel_ios_mask());
}
mask
}
pub(super) fn make_shield_ios_mask(&self) -> u32 {
#[allow(unused_mut)]
let mut mask = self.g1.as_ref().map_or(0, |g| g.make_shield_ios_mask())
| self.g2.as_ref().map_or(0, |g| g.make_shield_ios_mask())
| self.g3.as_ref().map_or(0, |g| g.make_shield_ios_mask())
| self.g4.as_ref().map_or(0, |g| g.make_shield_ios_mask())
| self.g5.as_ref().map_or(0, |g| g.make_shield_ios_mask())
| self.g6.as_ref().map_or(0, |g| g.make_shield_ios_mask());
#[cfg(any(tsc_v2, tsc_v3))]
{
mask |= self.g7.as_ref().map_or(0, |g| g.make_shield_ios_mask());
}
#[cfg(tsc_v3)]
{
mask |= self.g8.as_ref().map_or(0, |g| g.make_shield_ios_mask());
}
mask
}
pub(super) fn make_sample_ios_mask(&self) -> u32 {
#[allow(unused_mut)]
let mut mask = self.g1.as_ref().map_or(0, |g| g.make_sample_ios_mask())
| self.g2.as_ref().map_or(0, |g| g.make_sample_ios_mask())
| self.g3.as_ref().map_or(0, |g| g.make_sample_ios_mask())
| self.g4.as_ref().map_or(0, |g| g.make_sample_ios_mask())
| self.g5.as_ref().map_or(0, |g| g.make_sample_ios_mask())
| self.g6.as_ref().map_or(0, |g| g.make_sample_ios_mask());
#[cfg(any(tsc_v2, tsc_v3))]
{
mask |= self.g7.as_ref().map_or(0, |g| g.make_sample_ios_mask());
}
#[cfg(tsc_v3)]
{
mask |= self.g8.as_ref().map_or(0, |g| g.make_sample_ios_mask());
}
mask
}
}
impl<'d, T: Instance> Default for PinGroups<'d, T> {
fn default() -> Self {
Self {
g1: None,
g2: None,
g3: None,
g4: None,
g5: None,
g6: None,
#[cfg(any(tsc_v2, tsc_v3))]
g7: None,
#[cfg(tsc_v3)]
g8: None,
}
}
}
pin_trait!(G1IO1Pin, Instance);
pin_trait!(G1IO2Pin, Instance);
pin_trait!(G1IO3Pin, Instance);
pin_trait!(G1IO4Pin, Instance);
pin_trait!(G2IO1Pin, Instance);
pin_trait!(G2IO2Pin, Instance);
pin_trait!(G2IO3Pin, Instance);
pin_trait!(G2IO4Pin, Instance);
pin_trait!(G3IO1Pin, Instance);
pin_trait!(G3IO2Pin, Instance);
pin_trait!(G3IO3Pin, Instance);
pin_trait!(G3IO4Pin, Instance);
pin_trait!(G4IO1Pin, Instance);
pin_trait!(G4IO2Pin, Instance);
pin_trait!(G4IO3Pin, Instance);
pin_trait!(G4IO4Pin, Instance);
pin_trait!(G5IO1Pin, Instance);
pin_trait!(G5IO2Pin, Instance);
pin_trait!(G5IO3Pin, Instance);
pin_trait!(G5IO4Pin, Instance);
pin_trait!(G6IO1Pin, Instance);
pin_trait!(G6IO2Pin, Instance);
pin_trait!(G6IO3Pin, Instance);
pin_trait!(G6IO4Pin, Instance);
pin_trait!(G7IO1Pin, Instance);
pin_trait!(G7IO2Pin, Instance);
pin_trait!(G7IO3Pin, Instance);
pin_trait!(G7IO4Pin, Instance);
pin_trait!(G8IO1Pin, Instance);
pin_trait!(G8IO2Pin, Instance);
pin_trait!(G8IO3Pin, Instance);
pin_trait!(G8IO4Pin, Instance);
|