Skip to content

Commit 9b4385d

Browse files
authored
Merge pull request #31 from stm32-rs/tsc-charge-time-config
TSC CTPH/CTPL fixes
2 parents 4297410 + 995517e commit 9b4385d

File tree

1 file changed

+33
-5
lines changed

1 file changed

+33
-5
lines changed

src/tsc.rs

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ use crate::stm32::{TSC};
1212
use crate::gpio::gpiob::{PB4, PB5, PB6, PB7};
1313
use crate::gpio::{AF9, Alternate, Output, OpenDrain, PushPull};
1414

15-
#[derive(Debug)]
15+
#[derive(Debug, Copy, Clone)]
1616
pub enum Event {
1717
/// Max count error
1818
MaxCountError,
1919
/// End of acquisition
2020
EndOfAcquisition
2121
}
2222

23-
#[derive(Debug)]
23+
#[derive(Debug, Copy, Clone)]
2424
pub enum Error {
2525
/// Max count error
2626
MaxCountError,
@@ -76,11 +76,15 @@ pub struct Tsc<SPIN> {
7676
tsc: TSC
7777
}
7878

79+
#[derive(Debug, Copy, Clone)]
7980
pub struct Config {
8081
pub clock_prescale: Option<ClockPrescaler>,
8182
pub max_count_error: Option<MaxCountError>,
83+
pub charge_transfer_high: Option<ChargeDischargeTime>,
84+
pub charge_transfer_low: Option<ChargeDischargeTime>,
8285
}
8386

87+
#[derive(Debug, Copy, Clone)]
8488
pub enum ClockPrescaler {
8589
Hclk = 0b000,
8690
HclkDiv2 = 0b001,
@@ -92,6 +96,7 @@ pub enum ClockPrescaler {
9296
HclkDiv128 = 0b111,
9397
}
9498

99+
#[derive(Debug, Copy, Clone)]
95100
pub enum MaxCountError {
96101
/// 000: 255
97102
U255 = 000,
@@ -109,6 +114,27 @@ pub enum MaxCountError {
109114
U16383 = 110
110115
}
111116

117+
#[derive(Debug, Copy, Clone)]
118+
/// How many tsc cycles are spent charging / discharging
119+
pub enum ChargeDischargeTime {
120+
C1 = 0b0000,
121+
C2 = 0b0001,
122+
C3 = 0b0010,
123+
C4 = 0b0011,
124+
C5 = 0b0100,
125+
C6 = 0b0101,
126+
C7 = 0b0110,
127+
C8 = 0b0111,
128+
C9 = 0b1000,
129+
C10 = 0b1001,
130+
C11 = 0b1010,
131+
C12 = 0b1011,
132+
C13 = 0b1100,
133+
C14 = 0b1101,
134+
C15 = 0b1110,
135+
C16 = 0b1111,
136+
}
137+
112138
impl<SPIN> Tsc<SPIN> {
113139
pub fn tsc(tsc: TSC, sample_pin: SPIN, ahb: &mut AHB1, cfg: Option<Config>) -> Self
114140
where SPIN: SamplePin<TSC>
@@ -120,14 +146,16 @@ impl<SPIN> Tsc<SPIN> {
120146

121147
let config = cfg.unwrap_or(Config {
122148
clock_prescale: None,
123-
max_count_error: None
149+
max_count_error: None,
150+
charge_transfer_high: None,
151+
charge_transfer_low: None
124152
});
125153

126154
tsc.cr.write(|w| unsafe {
127155
w.ctph()
128-
.bits((1 << 28) as u8)
156+
.bits(config.charge_transfer_high.unwrap_or(ChargeDischargeTime::C2) as u8)
129157
.ctpl()
130-
.bits((1 << 24) as u8)
158+
.bits(config.charge_transfer_low.unwrap_or(ChargeDischargeTime::C2) as u8)
131159
// TODO configure sse?
132160
.sse()
133161
.set_bit()

0 commit comments

Comments
 (0)