Skip to content

Commit 5daab81

Browse files
authored
Merge pull request #17 from Neotron-Compute/feature/add_ps2
Feature: Add PS/2 support
2 parents 4f9a714 + 9fc747a commit 5daab81

File tree

4 files changed

+76
-4
lines changed

4 files changed

+76
-4
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ jobs:
1515
- name: Install flip-link
1616
run: cd / && cargo install flip-link
1717
- name: Build
18-
run: cargo build --verbose --target=thumbv6m-none-eabi
18+
run: cargo build --release --verbose --target=thumbv6m-none-eabi

Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ panic-probe = { version = "0.2.0", features = ["print-defmt"] }
1919
# Use this line if you have an STM32F031K6T6
2020
stm32f0xx-hal = { version = "0.17", features = ["stm32f031", "rt"] }
2121
debouncr = "0.2"
22+
pc-keyboard = "0.5"
2223

2324
[features]
2425
# set logging levels here

src/main.rs

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
use cortex_m::interrupt::free as disable_interrupts;
1212
use rtic::app;
1313
use stm32f0xx_hal::{
14-
gpio::gpioa::{PA10, PA11, PA12, PA2, PA3, PA9},
15-
gpio::gpiob::{PB0, PB1},
14+
gpio::gpioa::{PA10, PA11, PA12, PA15, PA2, PA3, PA9},
15+
gpio::gpiob::{PB0, PB1, PB3, PB4, PB5},
1616
gpio::gpiof::{PF0, PF1},
17-
gpio::{Alternate, Input, Output, PullUp, PushPull, AF1},
17+
gpio::{Alternate, Floating, Input, Output, PullUp, PushPull, AF1},
1818
pac,
1919
prelude::*,
2020
serial,
@@ -72,6 +72,18 @@ const APP: () = {
7272
/// Controls the Reset signal across the main board, putting all the
7373
/// chips (except this BMC!) in reset when pulled low.
7474
pin_sys_reset: PA2<Output<PushPull>>,
75+
/// Clock pin for PS/2 Keyboard port
76+
ps2_clk0: PA15<Input<Floating>>,
77+
/// Clock pin for PS/2 Mouse port
78+
ps2_clk1: PB3<Input<Floating>>,
79+
/// Data pin for PS/2 Keyboard port
80+
ps2_dat0: PB4<Input<Floating>>,
81+
/// Data pin for PS/2 Mouse port
82+
ps2_dat1: PB5<Input<Floating>>,
83+
/// The external interrupt peripheral
84+
exti: pac::EXTI,
85+
/// Our PS/2 keyboard decoder
86+
kb: pc_keyboard::Keyboard<pc_keyboard::layouts::Uk105Key, pc_keyboard::ScancodeSet2>,
7587
}
7688

7789
/// The entry point to our application.
@@ -113,6 +125,10 @@ const APP: () = {
113125
button_reset,
114126
mut pin_dc_on,
115127
mut pin_sys_reset,
128+
ps2_clk0,
129+
ps2_clk1,
130+
ps2_dat0,
131+
ps2_dat1,
116132
) = disable_interrupts(|cs| {
117133
(
118134
gpioa.pa9.into_alternate_af1(cs),
@@ -125,6 +141,10 @@ const APP: () = {
125141
gpiof.pf1.into_pull_up_input(cs),
126142
gpioa.pa3.into_push_pull_output(cs),
127143
gpioa.pa2.into_push_pull_output(cs),
144+
gpioa.pa15.into_floating_input(cs),
145+
gpiob.pb3.into_floating_input(cs),
146+
gpiob.pb4.into_floating_input(cs),
147+
gpiob.pb5.into_floating_input(cs),
128148
)
129149
});
130150

@@ -144,6 +164,14 @@ const APP: () = {
144164

145165
led_power.set_low().unwrap();
146166

167+
// Set EXTI15 to use PORT A (PA15)
168+
dp.SYSCFG.exticr4.write(|w| w.exti15().pa15());
169+
170+
// Enable EXTI15 interrupt as external falling edge
171+
dp.EXTI.imr.modify(|_r, w| w.mr15().set_bit());
172+
dp.EXTI.emr.modify(|_r, w| w.mr15().set_bit());
173+
dp.EXTI.ftsr.modify(|_r, w| w.tr15().set_bit());
174+
147175
defmt::info!("Init complete!");
148176

149177
init::LateResources {
@@ -159,6 +187,16 @@ const APP: () = {
159187
state_dc_power_enabled: DcPowerState::Off,
160188
pin_dc_on,
161189
pin_sys_reset,
190+
ps2_clk0,
191+
ps2_clk1,
192+
ps2_dat0,
193+
ps2_dat1,
194+
exti: dp.EXTI,
195+
kb: pc_keyboard::Keyboard::new(
196+
pc_keyboard::layouts::Uk105Key,
197+
pc_keyboard::ScancodeSet2,
198+
pc_keyboard::HandleControl::MapLettersToUnicode,
199+
),
162200
}
163201
}
164202

@@ -175,6 +213,32 @@ const APP: () = {
175213
}
176214
}
177215

216+
/// This is the PS/2 Keyboard task.
217+
///
218+
/// It fires when there is a falling edge on the PS/2 Keyboard clock pin.
219+
#[task(binds = EXTI4_15, resources=[ps2_clk0, ps2_dat0, exti, kb])]
220+
fn exti4_15_interrupt(ctx: exti4_15_interrupt::Context) {
221+
match ctx
222+
.resources
223+
.kb
224+
.add_bit(ctx.resources.ps2_dat0.is_high().unwrap())
225+
{
226+
Err(e) => {
227+
defmt::warn!("Error decoding kb: {}", e as usize);
228+
}
229+
Ok(None) => {}
230+
Ok(Some(evt)) => {
231+
defmt::info!(
232+
"Got event core={}, state={}",
233+
evt.code as usize,
234+
evt.state as usize
235+
);
236+
}
237+
}
238+
// Clear the pending flag
239+
ctx.resources.exti.pr.write(|w| w.pr15().set_bit());
240+
}
241+
178242
/// This is the USART1 task.
179243
///
180244
/// It fires whenever there is new data received on USART1. We should flag to the host

0 commit comments

Comments
 (0)