summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNo0ne <[email protected]>2023-08-14 08:34:31 +0200
committerMaxim Devaev <[email protected]>2023-08-25 12:41:47 +0300
commit94041d45fc789b1ed498c010486b1f4439b5a458 (patch)
treeb016887afe2dd924f0481bf8a950998c2f0f8ffe
parent36df38972c4b6c39f026ed6633982295a79076bf (diff)
pico hid: ps/2 keyboard support getting started (#139)
-rw-r--r--hid/pico/src/CMakeLists.txt3
-rw-r--r--hid/pico/src/ph_ps2.c154
-rw-r--r--hid/pico/src/ph_ps2.pio86
3 files changed, 230 insertions, 13 deletions
diff --git a/hid/pico/src/CMakeLists.txt b/hid/pico/src/CMakeLists.txt
index cb1c7feb..0426110c 100644
--- a/hid/pico/src/CMakeLists.txt
+++ b/hid/pico/src/CMakeLists.txt
@@ -20,9 +20,12 @@ target_link_options(${target_name} PRIVATE -Xlinker --print-memory-usage)
target_compile_options(${target_name} PRIVATE -Wall -Wextra)
target_include_directories(${target_name} PRIVATE ${CMAKE_CURRENT_LIST_DIR})
+pico_generate_pio_header(${target_name} ${CMAKE_CURRENT_LIST_DIR}/ph_ps2.pio)
+
target_link_libraries(${target_name} PRIVATE
pico_stdlib
pico_unique_id
+ hardware_pio
hardware_spi
hardware_watchdog
tinyusb_device
diff --git a/hid/pico/src/ph_ps2.c b/hid/pico/src/ph_ps2.c
index 60723a1c..caddc174 100644
--- a/hid/pico/src/ph_ps2.c
+++ b/hid/pico/src/ph_ps2.c
@@ -24,25 +24,136 @@
#include "ph_types.h"
#include "ph_outputs.h"
-
+#include "ph_ps2.pio.h"
+#include "hardware/gpio.h"
u8 ph_g_ps2_kbd_leds = 0;
bool ph_g_ps2_kbd_online = 0;
bool ph_g_ps2_mouse_online = 0;
+uint8_t const mod2ps2[] = { 0x14, 0x12, 0x11, 0x1f, 0x14, 0x59, 0x11, 0x27 };
+uint8_t const hid2ps2[] = {
+ 0x00, 0x00, 0xfc, 0x00, 0x1c, 0x32, 0x21, 0x23, 0x24, 0x2b, 0x34, 0x33, 0x43, 0x3b, 0x42, 0x4b,
+ 0x3a, 0x31, 0x44, 0x4d, 0x15, 0x2d, 0x1b, 0x2c, 0x3c, 0x2a, 0x1d, 0x22, 0x35, 0x1a, 0x16, 0x1e,
+ 0x26, 0x25, 0x2e, 0x36, 0x3d, 0x3e, 0x46, 0x45, 0x5a, 0x76, 0x66, 0x0d, 0x29, 0x4e, 0x55, 0x54,
+ 0x5b, 0x5d, 0x5d, 0x4c, 0x52, 0x0e, 0x41, 0x49, 0x4a, 0x58, 0x05, 0x06, 0x04, 0x0c, 0x03, 0x0b,
+ 0x83, 0x0a, 0x01, 0x09, 0x78, 0x07, 0x7c, 0x7e, 0x7e, 0x70, 0x6c, 0x7d, 0x71, 0x69, 0x7a, 0x74,
+ 0x6b, 0x72, 0x75, 0x77, 0x4a, 0x7c, 0x7b, 0x79, 0x5a, 0x69, 0x72, 0x7a, 0x6b, 0x73, 0x74, 0x6c,
+ 0x75, 0x7d, 0x70, 0x71, 0x61, 0x2f, 0x37, 0x0f, 0x08, 0x10, 0x18, 0x20, 0x28, 0x30, 0x38, 0x40,
+ 0x48, 0x50, 0x57, 0x5f
+};
+uint8_t const maparray = sizeof(hid2ps2) / sizeof(uint8_t);
+
+PIO pio = pio0;
+uint sm;
+uint offset;
+
+uint16_t ph_ps2_frame(uint8_t data) {
+ uint8_t parity = 1;
+ for (uint8_t i = 0; i < 8; i++) {
+ parity = parity ^ (data >> i & 1);
+ }
+
+ return ((1 << 10) | (parity << 9) | (data << 1)) ^ 0x7ff;
+}
+
+void ph_ps2_kbd_send(uint8_t data) {
+ pio_sm_put(pio, sm, ph_ps2_frame(data));
+}
+
+void ph_ps2_kbd_maybe_send_e0(uint8_t data) {
+ if (data == 0x46 ||
+ (data >= 0x49 && data <= 0x52) ||
+ data == 0x54 || data == 0x58 ||
+ data == 0x65 || data == 0x66 ||
+ data >= 0x81) {
+ ph_ps2_kbd_send(0xe0);
+ }
+}
void ph_ps2_init(void) {
- // TODO: PS2: Initialize PS/2 stuff here IF you have at least one PS/2 device, check ph_usb.c for the example
- // Use macro PH_O_IS_KBD_PS2 and PH_O_IS_MOUSE_PS2
if (PH_O_IS_KBD_PS2 || PH_O_IS_MOUSE_PS2) {
- // ...
+ gpio_init(13);
+ gpio_set_dir(13, GPIO_OUT);
+ gpio_put(13, 1); // LV pull-up voltage
+
+ sm = pio_claim_unused_sm(pio, true);
+ offset = pio_add_program(pio, &ps2device_program);
+ ps2device_program_init(pio, sm, offset, 14);
}
}
void ph_ps2_task(void) {
- // TODO: PS2: Perform periodic stuff here IF you have at least one PS/2 device, check ph_usb.c
if (PH_O_IS_KBD_PS2 || PH_O_IS_MOUSE_PS2) {
- // ...
+
+ if (!pio_sm_is_rx_fifo_empty(pio, sm)) {
+ uint32_t fifo = pio_sm_get(pio, sm);
+ fifo = fifo >> 23;
+
+ uint8_t parity = 1;
+ for(uint8_t i = 0; i < 8; i++) {
+ parity = parity ^ (fifo >> i & 1);
+ }
+
+ if(parity != fifo >> 8) {
+ ph_ps2_kbd_send(0xfe);
+ return;
+ }
+
+ uint8_t data = fifo;
+
+ /*switch() {
+ case 0xed: // CMD: Set LEDs
+
+ break;
+
+ case 0xf3: // CMD: Set typematic rate and delay
+
+ break;
+
+ default:*/
+ switch(data) {
+ case 0xff: // CMD: Reset
+ pio_sm_clear_fifos(pio, sm);
+ pio_sm_drain_tx_fifo(pio, sm);
+ ph_ps2_kbd_send(0xfa);
+ ph_ps2_kbd_send(0xaa);
+ return;
+
+ case 0xfe: // CMD: Resend
+
+ return;
+
+ case 0xee: // CMD: Echo
+ ph_ps2_kbd_send(0xee);
+ return;
+
+ case 0xf2: // CMD: Identify keyboard
+ ph_ps2_kbd_send(0xfa);
+ ph_ps2_kbd_send(0xab);
+ ph_ps2_kbd_send(0x83);
+ return;
+
+ case 0xf3: // CMD: Set typematic rate and delay
+ case 0xed: // CMD: Set LEDs
+
+ break;
+
+ case 0xf4: // CMD: Enable scanning
+
+ break;
+
+ case 0xf5: // CMD: Disable scanning, restore default parameters
+ case 0xf6: // CMD: Set default parameters
+
+ break;
+ }
+ /* break;
+ }*/
+
+ ph_ps2_kbd_send(0xfa);
+ }
+
}
// Here you should update some values:
// - ph_g_ps2_kbd_leds - keyboard LEDs mask like on USB
@@ -53,13 +164,30 @@ void ph_ps2_task(void) {
}
void ph_ps2_kbd_send_key(u8 key, bool state) {
- // TODO: PS2: Send keyboard key
- // @key - is a USB keycode, modifier keys has range 0xE0...0xE7, check ph_usb_kbd_send_key()
- // @state - true if pressed, false if released
- // The function should take care not to send duplicate events (if needed for PS/2)
- // If the PS2 keyboard is not used (PH_O_IS_KBD_PS2 is false), the function should do nothing.
- (void)key; // Remove this
- (void)state; // Remove this
+ if (PH_O_IS_KBD_PS2) {
+ if (key >= 0xe0 && key <= 0xe7) {
+ key -= 0xe0;
+ if(key > 2 && key != 5) {
+ ph_ps2_kbd_send(0xe0);
+ }
+
+ if(!state) {
+ ph_ps2_kbd_send(0xf0);
+ }
+
+ ph_ps2_kbd_send(mod2ps2[key]);
+
+ } else if (key < maparray) {
+ ph_ps2_kbd_maybe_send_e0(key);
+
+ if(!state) {
+ ph_ps2_kbd_send(0xf0);
+ }
+
+ ph_ps2_kbd_send(hid2ps2[key]);
+
+ }
+ }
}
void ph_ps2_mouse_send_button(u8 button, bool state) {
diff --git a/hid/pico/src/ph_ps2.pio b/hid/pico/src/ph_ps2.pio
new file mode 100644
index 00000000..2313419d
--- /dev/null
+++ b/hid/pico/src/ph_ps2.pio
@@ -0,0 +1,86 @@
+;
+; Copyright (c) 2022 No0ne (https://github.com/No0ne)
+; (c) 2023 Dustin Hoffman
+;
+; SPDX-License-Identifier: MIT
+;
+
+.program ps2device
+.side_set 1 opt pindirs
+
+ //mov y ! null
+start:
+.wrap_target
+ set pindirs 0 side 0 // Clock and data to input mode.
+ jmp pin sendcheck // If clock is high, see if we have data to send.
+ // Clock is being pulled low.
+ wait 1 pin 1 // Wait for clock to be pulled high.
+
+ // We are not sending, look for a start bit (Clock high, data low)
+ in pins 1 // Read in from data.
+ mov x isr // Move what we read to x.
+ mov isr null // Clear ISR.
+ jmp !x receive // If x is low, start the receive process.
+ jmp start // Not receiving, restart.
+
+receive:
+ set pindirs, 1 [6] // Clock low.
+ set x, 8 // Set loop counter.
+
+receiveloop:
+ set pindirs, 0 [6]// Clock high
+ in pins, 1 [4]// Read a bit into ISR.
+ set pindirs, 1 [6]// Clock low
+ jmp x-- receiveloop [4]// Iterate
+ set pindirs, 0 [6] // Clock high
+ nop side 1 [6]// Data low
+ set pindirs, 1 [7] // Clock low
+ //in null 1
+.wrap
+
+sendcheck:
+ jmp !osre wait_to_write // See if we have data to send.
+ jmp start // No data to send, restart.
+
+wait_to_write:
+ set x 10 // Number of bits to write out.
+
+ sendloop:
+ set pindirs, 0 [6] // Clock set to input (high)
+ jmp pin sendcontinue // If clock is high, host is still receiving data.
+ // Pin was low, host wants to send data.
+ // Notify of failure to send data..
+ //in null 8
+ //in y 2
+ jmp start
+sendcontinue:
+ out pindirs, 1 [6] // Write out data.
+ set pindirs, 1 [6] // Set clock low.
+ jmp x-- sendloop [6]
+ //in y 10
+ jmp start
+
+% c-sdk {
+
+ void ps2device_program_init(PIO pio, uint sm, uint offset, uint dat) {
+ pio_sm_config c = ps2device_program_get_default_config(offset);
+
+ uint clk = dat + 1;
+ pio_gpio_init(pio, clk);
+ pio_gpio_init(pio, dat);
+
+ // Use a frequency high enough to effectivly sample clock and data.
+ sm_config_set_clkdiv(&c, 427); // 2560 is 20 us, 640 is 5 us, 427 is 3.3333 us
+ sm_config_set_jmp_pin(&c, clk);
+ sm_config_set_set_pins(&c, clk, 1);
+ sm_config_set_sideset_pins(&c, dat);
+ sm_config_set_out_pins(&c, dat, 1);
+ sm_config_set_out_shift(&c, true, true, 11);
+ sm_config_set_in_pins(&c, dat);
+ sm_config_set_in_shift(&c, true, true, 9);
+
+ pio_sm_init(pio, sm, offset, &c);
+ pio_sm_set_enabled(pio, sm, true);
+ }
+
+%} \ No newline at end of file