pico hid: ps/2 keyboard support finished (#146)

This commit is contained in:
No0ne 2023-08-27 04:47:58 +02:00 committed by GitHub
parent d4e5722b24
commit 546a8cef8b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 136 additions and 35 deletions

View File

@ -30,9 +30,18 @@
u8 ph_g_ps2_kbd_leds = 0; u8 ph_g_ps2_kbd_leds = 0;
bool ph_g_ps2_kbd_online = 0; bool ph_g_ps2_kbd_online = 0;
bool ph_g_ps2_mouse_online = 0; bool ph_g_ps2_mouse_online = 0;
ph_ps2_phy ph_ps2_kbd; ph_ps2_phy ph_ps2_kbd;
ph_ps2_phy ph_ps2_mouse; ph_ps2_phy ph_ps2_mouse;
bool ph_ps2_kbd_scanning;
u32 ph_ps2_kbd_repeat_us;
u16 ph_ps2_kbd_delay_ms;
u8 ph_ps2_kbd_repeat = 0;
bool ph_ps2_kbd_repeatmod = false;
alarm_id_t ph_ps2_kbd_repeater;
s8 ph_ps2_is_ctrl = 0;
u8 const ph_ps2_led2ps2[] = { 0, 4, 1, 5, 2, 6, 3, 7 }; u8 const ph_ps2_led2ps2[] = { 0, 4, 1, 5, 2, 6, 3, 7 };
u8 const ph_ps2_mod2ps2[] = { 0x14, 0x12, 0x11, 0x1f, 0x14, 0x59, 0x11, 0x27 }; u8 const ph_ps2_mod2ps2[] = { 0x14, 0x12, 0x11, 0x1f, 0x14, 0x59, 0x11, 0x27 };
u8 const ph_ps2_hid2ps2[] = { u8 const ph_ps2_hid2ps2[] = {
@ -46,6 +55,13 @@ u8 const ph_ps2_hid2ps2[] = {
0x48, 0x50, 0x57, 0x5f 0x48, 0x50, 0x57, 0x5f
}; };
u8 const ph_ps2_maparray = sizeof(ph_ps2_hid2ps2); u8 const ph_ps2_maparray = sizeof(ph_ps2_hid2ps2);
u32 const ph_ps2_repeats[] = {
33333, 37453, 41667, 45872, 48309, 54054, 58480, 62500,
66667, 75188, 83333, 91743, 100000, 108696, 116279, 125000,
133333, 149254, 166667, 181818, 200000, 217391, 232558, 250000,
270270, 303030, 333333, 370370, 400000, 434783, 476190, 500000
};
u16 const ph_ps2_delays[] = { 250, 500, 750, 1000 };
void ph_ps2_kbd_send(u8 byte) { void ph_ps2_kbd_send(u8 byte) {
queue_try_add(&ph_ps2_kbd.qbytes, &byte); queue_try_add(&ph_ps2_kbd.qbytes, &byte);
@ -61,24 +77,59 @@ void ph_ps2_kbd_maybe_send_e0(u8 byte) {
} }
} }
int64_t ph_ps2_repeat_callback() {
if (ph_ps2_kbd_repeat) {
if (ph_ps2_kbd_repeatmod) {
if (ph_ps2_kbd_repeat > 3 && ph_ps2_kbd_repeat != 6) ph_ps2_kbd_send(0xe0);
ph_ps2_kbd_send(ph_ps2_mod2ps2[ph_ps2_kbd_repeat - 1]);
} else {
ph_ps2_kbd_maybe_send_e0(ph_ps2_kbd_repeat);
ph_ps2_kbd_send(ph_ps2_hid2ps2[ph_ps2_kbd_repeat]);
}
return ph_ps2_kbd_repeat_us;
}
ph_ps2_kbd_repeater = 0;
return 0;
}
int64_t ph_ps2_blink_callback() {
ph_g_ps2_kbd_leds = 0;
ph_ps2_kbd_send(0xaa);
return 0;
}
void ph_ps2_kbd_reset() {
ph_ps2_kbd_scanning = true;
ph_ps2_kbd_repeat_us = 91743;
ph_ps2_kbd_delay_ms = 500;
ph_ps2_kbd_repeat = 0;
ph_g_ps2_kbd_leds = 7;
add_alarm_in_ms(500, ph_ps2_blink_callback, NULL, false);
}
void ph_ps2_kbd_receive(u8 byte, u8 prev_byte) { void ph_ps2_kbd_receive(u8 byte, u8 prev_byte) {
switch(prev_byte) { switch (prev_byte) {
case 0xed: // CMD: Set LEDs case 0xed: // CMD: Set LEDs
if (byte > 7) byte = 0;
ph_g_ps2_kbd_leds = ph_ps2_led2ps2[byte]; ph_g_ps2_kbd_leds = ph_ps2_led2ps2[byte];
break; break;
case 0xf3: // CMD: Set typematic rate and delay case 0xf3: // CMD: Set typematic rate and delay
ph_ps2_kbd_repeat_us = ph_ps2_repeats[byte & 0x1f];
ph_ps2_kbd_delay_ms = ph_ps2_delays[(byte & 0x60) >> 5];
break; break;
default: default:
switch(byte) { switch (byte) {
case 0xff: // CMD: Reset case 0xff: // CMD: Reset
ph_g_ps2_kbd_online = true; ph_ps2_kbd_reset();
ph_g_ps2_kbd_leds = 0; break;
ph_ps2_kbd_send(0xfa);
ph_ps2_kbd_send(0xaa);
return;
case 0xee: // CMD: Echo case 0xee: // CMD: Echo
ph_ps2_kbd_send(0xee); ph_ps2_kbd_send(0xee);
@ -91,12 +142,15 @@ void ph_ps2_kbd_receive(u8 byte, u8 prev_byte) {
return; return;
case 0xf4: // CMD: Enable scanning case 0xf4: // CMD: Enable scanning
ph_g_ps2_kbd_online = true; ph_ps2_kbd_scanning = true;
break; break;
case 0xf5: // CMD: Disable scanning, restore default parameters case 0xf5: // CMD: Disable scanning, restore default parameters
case 0xf6: // CMD: Set default parameters case 0xf6: // CMD: Set default parameters
ph_g_ps2_kbd_online = byte == 0xf6; ph_ps2_kbd_scanning = byte == 0xf6;
ph_ps2_kbd_repeat_us = 91743;
ph_ps2_kbd_delay_ms = 500;
ph_ps2_kbd_repeat = 0;
ph_g_ps2_kbd_leds = 0; ph_g_ps2_kbd_leds = 0;
break; break;
} }
@ -107,7 +161,7 @@ void ph_ps2_kbd_receive(u8 byte, u8 prev_byte) {
} }
void ph_ps2_mouse_receive(u8 byte) { void ph_ps2_mouse_receive(u8 byte) {
switch(byte) { switch (byte) {
} }
} }
@ -121,35 +175,61 @@ void ph_ps2_init(void) {
if (PH_O_IS_KBD_PS2) { if (PH_O_IS_KBD_PS2) {
ph_ps2_phy_init(&ph_ps2_kbd, pio0, 11, &ph_ps2_kbd_receive); // keyboard: GPIO11=data, GPIO12=clock ph_ps2_phy_init(&ph_ps2_kbd, pio0, 11, &ph_ps2_kbd_receive); // keyboard: GPIO11=data, GPIO12=clock
ph_ps2_kbd_send(0xaa); ph_ps2_kbd_reset();
ph_g_ps2_kbd_online = true;
} }
if (PH_O_IS_MOUSE_PS2) { if (PH_O_IS_MOUSE_PS2) {
ph_ps2_phy_init(&ph_ps2_mouse, pio1, 14, &ph_ps2_mouse_receive); // mouse: GPIO14=data, GPIO15=clock //ph_ps2_phy_init(&ph_ps2_mouse, pio1, 14, &ph_ps2_mouse_receive); // mouse: GPIO14=data, GPIO15=clock
} }
} }
void ph_ps2_task(void) { void ph_ps2_task(void) {
if (PH_O_IS_KBD_PS2) { if (PH_O_IS_KBD_PS2) {
ph_ps2_phy_task(&ph_ps2_kbd); ph_ps2_phy_task(&ph_ps2_kbd);
ph_g_ps2_kbd_online = ph_ps2_kbd_scanning && ph_ps2_kbd.idle;
} }
if (PH_O_IS_MOUSE_PS2) { if (PH_O_IS_MOUSE_PS2) {
ph_ps2_phy_task(&ph_ps2_mouse); //ph_ps2_phy_task(&ph_ps2_mouse);
} }
} }
void ph_ps2_kbd_send_key(u8 key, bool state) { void ph_ps2_kbd_send_key(u8 key, bool state) {
if (PH_O_IS_KBD_PS2 && ph_g_ps2_kbd_online) { if (PH_O_IS_KBD_PS2 && ph_ps2_kbd_scanning) {
if (key >= 0xe0 && key <= 0xe7) { if (key >= 0xe0 && key <= 0xe7) {
if (key == 0xe0 || key == 0xe4) {
if (state) {
ph_ps2_is_ctrl++;
} else {
ph_ps2_is_ctrl--;
}
if (ph_ps2_is_ctrl < 0 || ph_ps2_is_ctrl > 2) {
ph_ps2_is_ctrl = 0;
}
}
key -= 0xe0; key -= 0xe0;
if (key > 2 && key != 5) { if (key > 2 && key != 5) {
ph_ps2_kbd_send(0xe0); ph_ps2_kbd_send(0xe0);
} }
if (!state) { if (state) {
ph_ps2_kbd_repeat = key + 1;
ph_ps2_kbd_repeatmod = true;
if (ph_ps2_kbd_repeater) {
cancel_alarm(ph_ps2_kbd_repeater);
}
ph_ps2_kbd_repeater = add_alarm_in_ms(ph_ps2_kbd_delay_ms, ph_ps2_repeat_callback, NULL, false);
} else {
if (ph_ps2_kbd_repeat == key + 1 && ph_ps2_kbd_repeatmod) {
ph_ps2_kbd_repeat = 0;
}
ph_ps2_kbd_send(0xf0); ph_ps2_kbd_send(0xf0);
} }
@ -158,20 +238,33 @@ void ph_ps2_kbd_send_key(u8 key, bool state) {
} else if (key < ph_ps2_maparray) { } else if (key < ph_ps2_maparray) {
if (key == 0x48) { if (key == 0x48) {
ph_ps2_kbd_repeat = 0;
if (state) { if (state) {
if (ph_ps2_is_ctrl) {
if (false) { // TODO: is shift
ph_ps2_kbd_send(0xe0); ph_ps2_kbd_send(0x7e); ph_ps2_kbd_send(0xe0); ph_ps2_kbd_send(0xf0); ph_ps2_kbd_send(0x7e); ph_ps2_kbd_send(0xe0); ph_ps2_kbd_send(0x7e); ph_ps2_kbd_send(0xe0); ph_ps2_kbd_send(0xf0); ph_ps2_kbd_send(0x7e);
} else { } else {
ph_ps2_kbd_send(0xe1); ph_ps2_kbd_send(0x14); ph_ps2_kbd_send(0x77); ph_ps2_kbd_send(0xe1); ph_ps2_kbd_send(0xe1); ph_ps2_kbd_send(0x14); ph_ps2_kbd_send(0x77); ph_ps2_kbd_send(0xe1);
ph_ps2_kbd_send(0xf0); ph_ps2_kbd_send(0x14); ph_ps2_kbd_send(0xf0); ph_ps2_kbd_send(0x77); ph_ps2_kbd_send(0xf0); ph_ps2_kbd_send(0x14); ph_ps2_kbd_send(0xf0); ph_ps2_kbd_send(0x77);
} }
} }
} else { } else {
ph_ps2_kbd_maybe_send_e0(key); ph_ps2_kbd_maybe_send_e0(key);
if (!state) { if (state) {
ph_ps2_kbd_repeat = key;
ph_ps2_kbd_repeatmod = false;
if (ph_ps2_kbd_repeater) {
cancel_alarm(ph_ps2_kbd_repeater);
}
ph_ps2_kbd_repeater = add_alarm_in_ms(ph_ps2_kbd_delay_ms, ph_ps2_repeat_callback, NULL, false);
} else {
if (ph_ps2_kbd_repeat == key && !ph_ps2_kbd_repeatmod) {
ph_ps2_kbd_repeat = 0;
}
ph_ps2_kbd_send(0xf0); ph_ps2_kbd_send(0xf0);
} }

View File

@ -1,6 +1,14 @@
#include "ph_ps2_phy.h" #include "ph_ps2_phy.h"
#include "ph_ps2_phy.pio.h" #include "ph_ps2_phy.pio.h"
u32 ph_ps2_phy_frame(u8 byte) {
bool parity = 1;
for (u8 i = 0; i < 8; i++) {
parity = parity ^ (byte >> i & 1);
}
return ((1 << 10) | (parity << 9) | (byte << 1)) ^ 0x7ff;
}
void ph_ps2_phy_init(ph_ps2_phy* this, PIO pio, u8 data_pin, rx_callback rx) { void ph_ps2_phy_init(ph_ps2_phy* this, PIO pio, u8 data_pin, rx_callback rx) {
queue_init(&this->qbytes, sizeof(u8), 9); queue_init(&this->qbytes, sizeof(u8), 9);
queue_init(&this->qpacks, sizeof(u8) * 9, 16); queue_init(&this->qpacks, sizeof(u8) * 9, 16);
@ -13,14 +21,7 @@ void ph_ps2_phy_init(ph_ps2_phy* this, PIO pio, u8 data_pin, rx_callback rx) {
this->rx = rx; this->rx = rx;
this->last_rx = 0; this->last_rx = 0;
this->last_tx = 0; this->last_tx = 0;
} this->idle = true;
u32 ph_ps2_frame(u8 byte) {
u8 parity = 1;
for (u8 i = 0; i < 8; i++) {
parity = parity ^ (byte >> i & 1);
}
return ((1 << 10) | (parity << 9) | (byte << 1)) ^ 0x7ff;
} }
void ph_ps2_phy_task(ph_ps2_phy* this) { void ph_ps2_phy_task(ph_ps2_phy* this) {
@ -38,7 +39,9 @@ void ph_ps2_phy_task(ph_ps2_phy* this) {
queue_try_add(&this->qpacks, &pack); queue_try_add(&this->qpacks, &pack);
} }
if (!queue_is_empty(&this->qpacks) && pio_sm_is_tx_fifo_empty(this->pio, this->sm) && !pio_interrupt_get(this->pio, this->sm * 2 + 0)) { this->idle = !pio_interrupt_get(this->pio, this->sm * 2);
if (!queue_is_empty(&this->qpacks) && pio_sm_is_tx_fifo_empty(this->pio, this->sm) && this->idle) {
if (queue_try_peek(&this->qpacks, &pack)) { if (queue_try_peek(&this->qpacks, &pack)) {
if (this->sent == pack[0]) { if (this->sent == pack[0]) {
this->sent = 0; this->sent = 0;
@ -46,7 +49,7 @@ void ph_ps2_phy_task(ph_ps2_phy* this) {
} else { } else {
this->sent++; this->sent++;
this->last_tx = pack[this->sent]; this->last_tx = pack[this->sent];
pio_sm_put(this->pio, this->sm, ph_ps2_frame(this->last_tx)); pio_sm_put(this->pio, this->sm, ph_ps2_phy_frame(this->last_tx));
} }
} }
} }
@ -58,16 +61,20 @@ void ph_ps2_phy_task(ph_ps2_phy* this) {
} }
if (!pio_sm_is_rx_fifo_empty(this->pio, this->sm)) { if (!pio_sm_is_rx_fifo_empty(this->pio, this->sm)) {
u32 fifo = pio_sm_get(this->pio, this->sm); u32 fifo = pio_sm_get(this->pio, this->sm) >> 23;
fifo = fifo >> 23;
u8 parity = 1; bool parity = 1;
for (i = 0; i < 8; i++) { for (i = 0; i < 8; i++) {
parity = parity ^ (fifo >> i & 1); parity = parity ^ (fifo >> i & 1);
} }
if (parity != fifo >> 8) { if (parity != fifo >> 8) {
pio_sm_put(this->pio, this->sm, ph_ps2_frame(0xfe)); pio_sm_put(this->pio, this->sm, ph_ps2_phy_frame(0xfe));
return;
}
if (fifo == 0xfe) {
pio_sm_put(this->pio, this->sm, ph_ps2_phy_frame(this->last_tx));
return; return;
} }

View File

@ -15,6 +15,7 @@ typedef struct {
rx_callback rx; rx_callback rx;
u8 last_rx; u8 last_rx;
u8 last_tx; u8 last_tx;
bool idle;
} ph_ps2_phy; } ph_ps2_phy;
void ph_ps2_phy_init(ph_ps2_phy* this, PIO pio, u8 data_pin, rx_callback rx); void ph_ps2_phy_init(ph_ps2_phy* this, PIO pio, u8 data_pin, rx_callback rx);