pico hid: basic ps/2 mouse support (#147)

This commit is contained in:
No0ne 2023-08-30 22:51:30 +02:00 committed by GitHub
parent 546a8cef8b
commit 19c1c7b933
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 445 additions and 259 deletions

View File

@ -9,6 +9,8 @@ target_sources(${target_name} PRIVATE
ph_usb_mouse.c
ph_ps2.c
ph_ps2_phy.c
ph_ps2_kbd.c
ph_ps2_mouse.c
ph_cmds.c
ph_com.c
ph_com_bridge.c

View File

@ -24,281 +24,38 @@
#include "ph_types.h"
#include "ph_outputs.h"
#include "ph_ps2_phy.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;
ph_ps2_phy ph_ps2_kbd;
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_mod2ps2[] = { 0x14, 0x12, 0x11, 0x1f, 0x14, 0x59, 0x11, 0x27 };
u8 const ph_ps2_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
};
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) {
queue_try_add(&ph_ps2_kbd.qbytes, &byte);
}
void ph_ps2_kbd_maybe_send_e0(u8 byte) {
if (byte == 0x46 ||
(byte >= 0x49 && byte <= 0x52) ||
byte == 0x54 || byte == 0x58 ||
byte == 0x65 || byte == 0x66 ||
byte >= 0x81) {
ph_ps2_kbd_send(0xe0);
}
}
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) {
switch (prev_byte) {
case 0xed: // CMD: Set LEDs
if (byte > 7) byte = 0;
ph_g_ps2_kbd_leds = ph_ps2_led2ps2[byte];
break;
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;
default:
switch (byte) {
case 0xff: // CMD: Reset
ph_ps2_kbd_reset();
break;
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 0xf4: // CMD: Enable scanning
ph_ps2_kbd_scanning = true;
break;
case 0xf5: // CMD: Disable scanning, restore default parameters
case 0xf6: // CMD: Set default parameters
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;
break;
}
break;
}
ph_ps2_kbd_send(0xfa);
}
void ph_ps2_mouse_receive(u8 byte) {
switch (byte) {
}
}
void ph_ps2_init(void) {
if (PH_O_IS_KBD_PS2 || PH_O_IS_MOUSE_PS2) {
gpio_init(13);
gpio_init(13); // GPIO13=LV pull-up voltage
gpio_set_dir(13, GPIO_OUT);
gpio_put(13, 1); // GPIO13=LV pull-up voltage
gpio_put(13, 1);
}
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_kbd_reset();
ph_ps2_kbd_init(11); // keyboard: GPIO11=data, GPIO12=clock
}
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_mouse_init(14); // mouse: GPIO14=data, GPIO15=clock
}
}
void ph_ps2_task(void) {
if (PH_O_IS_KBD_PS2) {
ph_ps2_phy_task(&ph_ps2_kbd);
ph_g_ps2_kbd_online = ph_ps2_kbd_scanning && ph_ps2_kbd.idle;
ph_ps2_kbd_task();
}
if (PH_O_IS_MOUSE_PS2) {
//ph_ps2_phy_task(&ph_ps2_mouse);
ph_ps2_mouse_task();
}
}
void ph_ps2_kbd_send_key(u8 key, bool state) {
if (PH_O_IS_KBD_PS2 && ph_ps2_kbd_scanning) {
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;
if (key > 2 && key != 5) {
ph_ps2_kbd_send(0xe0);
}
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(ph_ps2_mod2ps2[key]);
} else if (key < ph_ps2_maparray) {
if (key == 0x48) {
ph_ps2_kbd_repeat = 0;
if (state) {
if (ph_ps2_is_ctrl) {
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 {
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);
}
}
} else {
ph_ps2_kbd_maybe_send_e0(key);
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(ph_ps2_hid2ps2[key]);
}
}
}
}
void ph_ps2_mouse_send_button(u8 button, bool state) {
// TODO: PS2: Send mouse button
// @button - USB button code
// @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_MOUSE_PS2 is false), the function should do nothing.
(void)button; // Remove this
(void)state; // Remove this
}
void ph_ps2_mouse_send_rel(s8 x, s8 y) {
// TODO: PS2: Send relative move event
// If the PS2 keyboard is not used (PH_O_IS_MOUSE_PS2 is false), the function should do nothing.
(void)x; // Remove this
(void)y; // Remove this
}
void ph_ps2_mouse_send_wheel(s8 h, s8 v) {
(void)h;
// TODO: PS2: Send wheel. As I understand, PS/2 has no horizontal scrolling, so @h just can be ignored.
// @v - vertical scrolling like on USB
// If the PS2 keyboard is not used (PH_O_IS_MOUSE_PS2 is false), the function should do nothing.
(void)v; // Remove this
}
void ph_ps2_send_clear(void) {
// TODO: PS2: Release all pressed buttons and keys.
// If PH_O_IS_KBD_PS2, release all PS/2 buttons

View File

@ -33,8 +33,12 @@ extern bool ph_g_ps2_mouse_online;
void ph_ps2_init(void);
void ph_ps2_task(void);
void ph_ps2_kbd_init(u8 gpio);
void ph_ps2_kbd_task(void);
void ph_ps2_kbd_send_key(u8 key, bool state);
void ph_ps2_mouse_init(u8 gpio);
void ph_ps2_mouse_task(void);
void ph_ps2_mouse_send_button(u8 button, bool state);
void ph_ps2_mouse_send_rel(s8 x, s8 y);
void ph_ps2_mouse_send_wheel(s8 h, s8 v);

222
hid/pico/src/ph_ps2_kbd.c Normal file
View File

@ -0,0 +1,222 @@
#include "ph_outputs.h"
#include "ph_ps2_phy.h"
extern u8 ph_g_ps2_kbd_leds;
extern bool ph_g_ps2_kbd_online;
ph_ps2_phy ph_ps2_kbd;
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_mod2ps2[] = { 0x14, 0x12, 0x11, 0x1f, 0x14, 0x59, 0x11, 0x27 };
u8 const ph_ps2_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
};
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) {
queue_try_add(&ph_ps2_kbd.qbytes, &byte);
}
void ph_ps2_kbd_maybe_send_e0(u8 byte) {
if (byte == 0x46 ||
(byte >= 0x49 && byte <= 0x52) ||
byte == 0x54 || byte == 0x58 ||
byte == 0x65 || byte == 0x66 ||
byte >= 0x81) {
ph_ps2_kbd_send(0xe0);
}
}
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_send_key(u8 key, bool state) {
if (PH_O_IS_KBD_PS2 && ph_ps2_kbd_scanning) {
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;
if (key > 2 && key != 5) {
ph_ps2_kbd_send(0xe0);
}
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(ph_ps2_mod2ps2[key]);
} else if (key < ph_ps2_maparray) {
if (key == 0x48) {
ph_ps2_kbd_repeat = 0;
if (state) {
if (ph_ps2_is_ctrl) {
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 {
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);
}
}
} else {
ph_ps2_kbd_maybe_send_e0(key);
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(ph_ps2_hid2ps2[key]);
}
}
}
}
void ph_ps2_kbd_receive(u8 byte, u8 prev_byte) {
switch (prev_byte) {
case 0xed: // Set LEDs
if (byte > 7) byte = 0;
ph_g_ps2_kbd_leds = ph_ps2_led2ps2[byte];
break;
case 0xf3: // 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;
default:
switch (byte) {
case 0xff: // Reset
ph_ps2_kbd_reset();
break;
case 0xee: // Echo
ph_ps2_kbd_send(0xee);
return;
case 0xf2: // Identify keyboard
ph_ps2_kbd_send(0xfa);
ph_ps2_kbd_send(0xab);
ph_ps2_kbd_send(0x83);
return;
case 0xf4: // Enable scanning
ph_ps2_kbd_scanning = true;
break;
case 0xf5: // Disable scanning, restore default parameters
case 0xf6: // Set default parameters
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;
break;
}
break;
}
ph_ps2_kbd_send(0xfa);
}
void ph_ps2_kbd_task(void) {
ph_ps2_phy_task(&ph_ps2_kbd);
ph_g_ps2_kbd_online = ph_ps2_kbd_scanning && ph_ps2_kbd.idle;
}
void ph_ps2_kbd_init(u8 gpio) {
ph_ps2_phy_init(&ph_ps2_kbd, pio0, gpio, &ph_ps2_kbd_receive);
ph_ps2_kbd_reset();
}

191
hid/pico/src/ph_ps2_mouse.c Normal file
View File

@ -0,0 +1,191 @@
#include "ph_outputs.h"
#include "ph_ps2_phy.h"
extern bool ph_g_ps2_mouse_online;
ph_ps2_phy ph_ps2_mouse;
u8 ms_type = 0;
u8 ms_mode = 0;
u8 ms_input_mode = 0;
u8 ms_rate = 100;
u32 ms_magic_seq = 0x00;
u8 buttons = 0;
#define MS_TYPE_STANDARD 0x00
#define MS_TYPE_WHEEL_3 0x03
#define MS_TYPE_WHEEL_5 0x04
#define MS_MODE_IDLE 0
#define MS_MODE_STREAMING 1
#define MS_INPUT_CMD 0
#define MS_INPUT_SET_RATE 1
void ph_ps2_mouse_send(u8 byte) {
queue_try_add(&ph_ps2_mouse.qbytes, &byte);
}
void ph_ps2_mouse_packet(u8 button, u8 x1, u8 y1) {
if(ms_mode == MS_MODE_STREAMING) {
u8 s = (button & 7) + 8;
u8 x = x1 & 0x7f;
u8 y = y1 & 0x7f;
u8 z = 0;
if(x1 >> 7) {
s += 0x10;
x += 0x80;
}
if(y1 >> 7) {
y = 0x80 - y;
} else if(y) {
s += 0x20;
y = 0x100 - y;
}
ph_ps2_mouse_send(s);
ph_ps2_mouse_send(x);
ph_ps2_mouse_send(y);
if (ms_type == MS_TYPE_WHEEL_3 || ms_type == MS_TYPE_WHEEL_5) {
/*if(report[3] >> 7) {
z = 0x8 - z;
} else if(z) {
z = 0x10 - z;
}
if (ms_type == MS_TYPE_WHEEL_5) {
if (report[0] & 0x8) {
z += 0x10;
}
if (report[0] & 0x10) {
z += 0x20;
}
}*/
ph_ps2_mouse_send(z);
}
}
}
void ph_ps2_mouse_send_button(u8 button, bool state) {
// TODO: PS2: Send mouse button
// @button - USB button code
// @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_MOUSE_PS2 is false), the function should do nothing.
(void)button; // Remove this
(void)state; // Remove this
u8 bitval = 1;
button--;
if(state) {
buttons = buttons | (bitval << button);
} else {
buttons = buttons & ~(bitval << button);
}
ph_ps2_mouse_packet(buttons, 0, 0);
}
void ph_ps2_mouse_send_rel(s8 x1, s8 y1) {
// TODO: PS2: Send relative move event
// If the PS2 keyboard is not used (PH_O_IS_MOUSE_PS2 is false), the function should do nothing.
ph_ps2_mouse_packet(buttons, x1, y1);
}
void ph_ps2_mouse_send_wheel(s8 h, s8 v) {
(void)h;
// TODO: PS2: Send wheel. As I understand, PS/2 has no horizontal scrolling, so @h just can be ignored.
// @v - vertical scrolling like on USB
// If the PS2 keyboard is not used (PH_O_IS_MOUSE_PS2 is false), the function should do nothing.
(void)v; // Remove this
}
void ph_ps2_mouse_receive(u8 byte, u8 prev_byte) {
if(ms_input_mode == MS_INPUT_SET_RATE) {
ms_rate = byte;
ms_input_mode = MS_INPUT_CMD;
ph_ps2_mouse_send(0xfa);
ms_magic_seq = (ms_magic_seq << 8) | byte;
if(ms_type == MS_TYPE_STANDARD && ms_magic_seq == 0xc86450) {
ms_type = MS_TYPE_WHEEL_3;
} else if (ms_type == MS_TYPE_WHEEL_3 && ms_magic_seq == 0xc8c850) {
ms_type = MS_TYPE_WHEEL_5;
}
return;
}
if(byte != 0xf3) {
ms_magic_seq = 0x00;
}
switch(byte) {
case 0xff: // Reset
ms_type = MS_TYPE_STANDARD;
ms_mode = MS_MODE_IDLE;
ms_rate = 100;
ph_ps2_mouse_send(0xfa);
ph_ps2_mouse_send(0xaa);
ph_ps2_mouse_send(ms_type);
return;
case 0xf6: // Set Defaults
ms_type = MS_TYPE_STANDARD;
ms_rate = 100;
case 0xf5: // Disable Data Reporting
case 0xea: // Set Stream Mode
ms_mode = MS_MODE_IDLE;
ph_ps2_mouse_send(0xfa);
return;
case 0xf4: // Enable Data Reporting
ms_mode = MS_MODE_STREAMING;
ph_ps2_mouse_send(0xfa);
return;
case 0xf3: // Set Sample Rate
ms_input_mode = MS_INPUT_SET_RATE;
ph_ps2_mouse_send(0xfa);
return;
case 0xf2: // Get Device ID
ph_ps2_mouse_send(0xfa);
ph_ps2_mouse_send(ms_type);
return;
case 0xe9: // Status Request
ph_ps2_mouse_send(0xfa);
ph_ps2_mouse_send(0x00); // Bit6: Mode, Bit 5: Enable, Bit 4: Scaling, Bits[2,1,0] = Buttons[L,M,R]
ph_ps2_mouse_send(0x02); // Resolution
ph_ps2_mouse_send(ms_rate); // Sample Rate
return;
// TODO: Implement (more of) these?
// case 0xf0: // Set Remote Mode
// case 0xee: // Set Wrap Mode
// case 0xec: // Reset Wrap Mode
// case 0xeb: // Read Data
// case 0xe8: // Set Resolution
// case 0xe7: // Set Scaling 2:1
// case 0xe6: // Set Scaling 1:1
}
ph_ps2_mouse_send(0xfa);
}
void ph_ps2_mouse_task(void) {
ph_ps2_phy_task(&ph_ps2_mouse);
}
void ph_ps2_mouse_init(u8 gpio) {
ph_ps2_phy_init(&ph_ps2_mouse, pio0, gpio, &ph_ps2_mouse_receive);
}

View File

@ -1,6 +1,11 @@
// Source: https://github.com/No0ne/ps2x2pico/blob/main/ps2phy.c
// replace ps2phy with ph_ps2_phy
#include "ph_ps2_phy.h"
#include "ph_ps2_phy.pio.h"
uint prog = 0;
u32 ph_ps2_phy_frame(u8 byte) {
bool parity = 1;
for (u8 i = 0; i < 8; i++) {
@ -10,13 +15,17 @@ u32 ph_ps2_phy_frame(u8 byte) {
}
void ph_ps2_phy_init(ph_ps2_phy* this, PIO pio, u8 data_pin, rx_callback rx) {
if(!prog) {
prog = pio_add_program(pio, &ph_ps2_phy_program);
}
queue_init(&this->qbytes, sizeof(u8), 9);
queue_init(&this->qpacks, sizeof(u8) * 9, 16);
this->pio = pio;
this->sm = pio_claim_unused_sm(this->pio, true);
ps2phy_program_init(this->pio, this->sm, pio_add_program(this->pio, &ps2phy_program), data_pin);
this->sm = pio_claim_unused_sm(pio, true);
ph_ps2_phy_program_init(pio, this->sm, prog, data_pin);
this->pio = pio;
this->sent = 0;
this->rx = rx;
this->last_rx = 0;
@ -39,7 +48,7 @@ void ph_ps2_phy_task(ph_ps2_phy* this) {
queue_try_add(&this->qpacks, &pack);
}
this->idle = !pio_interrupt_get(this->pio, this->sm * 2);
this->idle = !pio_interrupt_get(this->pio, this->sm);
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)) {
@ -54,10 +63,10 @@ void ph_ps2_phy_task(ph_ps2_phy* this) {
}
}
if (pio_interrupt_get(this->pio, this->sm * 2 + 1)) {
if (pio_interrupt_get(this->pio, this->sm + 4)) {
this->sent = 0;
pio_sm_drain_tx_fifo(this->pio, this->sm);
pio_interrupt_clear(this->pio, this->sm * 2 + 1);
pio_interrupt_clear(this->pio, this->sm + 4);
}
if (!pio_sm_is_rx_fifo_empty(this->pio, this->sm)) {

View File

@ -4,8 +4,9 @@
;
; SPDX-License-Identifier: MIT
;
; Source: https://github.com/No0ne/ps2x2pico/blob/main/ps2phy.pio
.program ps2phy
.program ph_ps2_phy
.side_set 1 opt pindirs
restart:
@ -49,7 +50,7 @@ wait_to_write:
sendloop:
set pindirs, 0 [6] // clock set to input (high)
jmp pin sendcontinue // if clock is high, host is still receiving data
irq wait 1 rel // clock was low, host wants to send data, notify of failure to send data
irq wait 4 rel // clock was low, host wants to send data, notify of failure to send data
jmp restart // and wait for restart
sendcontinue:
out pindirs, 1 [6] // write out data
@ -57,8 +58,8 @@ sendcontinue:
jmp x-- sendloop [6]
% c-sdk {
void ps2phy_program_init(PIO pio, uint sm, uint offset, uint dat) {
pio_sm_config c = ps2phy_program_get_default_config(offset);
void ph_ps2_phy_program_init(PIO pio, uint sm, uint offset, uint dat) {
pio_sm_config c = ph_ps2_phy_program_get_default_config(offset);
u8 clk = dat + 1;
pio_gpio_init(pio, clk);