Skip to content

Commit 2dfcbc4

Browse files
authored
Merge pull request #15 from MabezDev/tsc-configuration
RCC/TSC:
2 parents 6adbc41 + 7e8a476 commit 2dfcbc4

File tree

6 files changed

+54
-20
lines changed

6 files changed

+54
-20
lines changed

.vscode/launch.json

Lines changed: 2 additions & 2 deletions
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/touch",
16+
"executable": "./target/thumbv7em-none-eabi/debug/examples/serial",
1717
"remote": true,
1818
"target": ":3333",
1919
"cwd": "${workspaceRoot}",
@@ -34,7 +34,7 @@
3434
"debugger_args": [
3535
"-nx" // dont use the .gdbinit file
3636
],
37-
"executable": "./target/thumbv7em-none-eabi/release/examples/touch",
37+
"executable": "./target/thumbv7em-none-eabi/release/examples/serial",
3838
"remote": true,
3939
"target": ":3333",
4040
"cwd": "${workspaceRoot}",

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ exclude = [
1717
[dependencies]
1818
cortex-m = "0.5.2"
1919
nb = "0.1.1"
20-
cortex-m-rt = "0.5.1"
2120

2221
[dependencies.cast]
2322
version = "0.2.2"
@@ -38,6 +37,7 @@ features = ["stm32l4x2", "rt"]
3837
[dev-dependencies]
3938
panic-semihosting = "0.3.0"
4039
cortex-m-semihosting = "0.3.0"
40+
cortex-m-rt = "0.5.1"
4141

4242
[profile.dev]
4343
incremental = false

examples/serial.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ fn main() -> ! {
3434
// let mut gpiob = p.GPIOB.split(&mut rcc.ahb2);
3535

3636
// clock configuration using the default settings (all clocks run at 8 MHz)
37-
let clocks = rcc.cfgr.freeze(&mut flash.acr);
37+
// let clocks = rcc.cfgr.freeze(&mut flash.acr);
3838
// TRY this alternate clock configuration (clocks run at nearly the maximum frequency)
39-
// let clocks = rcc.cfgr.sysclk(64.mhz()).pclk1(32.mhz()).freeze(&mut flash.acr);
39+
let clocks = rcc.cfgr.sysclk(80.mhz()).pclk1(80.mhz()).pclk2(80.mhz()).freeze(&mut flash.acr);
4040

4141
// The Serial API is highly generic
4242
// TRY the commented out, different pin configurations

examples/touch.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ fn main() -> ! {
4444
// let mut c3 = gpiob.pb7.into_touch_channel(&mut gpiob.moder, &mut gpiob.otyper, &mut gpiob.afrl);
4545

4646
// , (c1, c2, c3)
47-
let tsc = Tsc::tsc(p.TSC, sample_pin, &mut rcc.ahb1);
47+
let tsc = Tsc::tsc(p.TSC, sample_pin, &mut rcc.ahb1, None);
4848

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

src/rcc.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -351,10 +351,10 @@ impl CFGR {
351351
.modify(|_, w| unsafe {
352352
w.pllsrc()
353353
.bits(pllsrc_bits)
354-
.pllm().bits(pllmul_bits)
354+
.pllm().bits(0b0) // no division, how to calculate?
355+
.pllr().bits(0b0) // no division, how to calculate?
356+
.plln().bits(pllmul_bits)
355357
});
356-
// .plln().bits(n) // TODO?
357-
// .pllr().bits(r)
358358

359359
rcc.cr.modify(|_, w| w.pllon().set_bit());
360360

src/tsc.rs

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -76,33 +76,67 @@ pub struct Tsc<SPIN> {
7676
tsc: TSC
7777
}
7878

79+
pub struct Config {
80+
pub clock_prescale: Option<ClockPrescaler>,
81+
pub max_count_error: Option<MaxCountError>,
82+
}
83+
84+
pub enum ClockPrescaler {
85+
Hclk = 0b000,
86+
HclkDiv2 = 0b001,
87+
HclkDiv4 = 0b010,
88+
HclkDiv8 = 0b011,
89+
HclkDiv16 = 0b100,
90+
HclkDiv32 = 0b101,
91+
HclkDiv64 = 0b110,
92+
HclkDiv128 = 0b111,
93+
}
94+
95+
pub enum MaxCountError {
96+
/// 000: 255
97+
U255 = 000,
98+
/// 001: 511
99+
U511 = 001,
100+
/// 010: 1023
101+
U1023 = 010,
102+
/// 011: 2047
103+
U2047 = 011,
104+
/// 100: 4095
105+
U4095 = 100,
106+
/// 101: 8191
107+
U8191 = 101,
108+
/// 110: 16383
109+
U16383 = 110
110+
}
111+
79112
impl<SPIN> Tsc<SPIN> {
80-
pub fn tsc(tsc: TSC, sample_pin: SPIN, ahb: &mut AHB1) -> Self
113+
pub fn tsc(tsc: TSC, sample_pin: SPIN, ahb: &mut AHB1, cfg: Option<Config>) -> Self
81114
where SPIN: SamplePin<TSC>
82115
{
83116
/* Enable the peripheral clock */
84117
ahb.enr().modify(|_, w| w.tscen().set_bit());
85118
ahb.rstr().modify(|_, w| w.tscrst().set_bit());
86119
ahb.rstr().modify(|_, w| w.tscrst().clear_bit());
87120

121+
let config = cfg.unwrap_or(Config {
122+
clock_prescale: None,
123+
max_count_error: None
124+
});
125+
88126
tsc.cr.write(|w| unsafe {
89127
w.ctph()
90128
.bits((1 << 28) as u8)
91129
.ctpl()
92130
.bits((1 << 24) as u8)
131+
// TODO configure sse?
93132
.sse()
94-
.clear_bit()
133+
.set_bit()
134+
.ssd()
135+
.bits(16)
95136
.pgpsc()
96-
.bits((2 << 12) as u8)
137+
.bits(config.clock_prescale.unwrap_or(ClockPrescaler::Hclk) as u8)
97138
.mcv()
98-
// 000: 255
99-
// 001: 511
100-
// 010: 1023
101-
// 011: 2047
102-
// 100: 4095
103-
// 101: 8191
104-
// 110: 16383
105-
.bits(0b101) // TODO make this value configurable
139+
.bits(config.max_count_error.unwrap_or(MaxCountError::U8191) as u8)
106140
.tsce()
107141
.set_bit()
108142
});

0 commit comments

Comments
 (0)