Skip to content

Commit

Permalink
keyboard and mouse on the same PIO
Browse files Browse the repository at this point in the history
  • Loading branch information
No0ne committed Aug 27, 2023
1 parent f257b34 commit 358390e
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 9 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ add_compile_definitions(MSDAT=14)
add_compile_definitions(LVPWR=13)

pico_set_program_name(ps2x2pico "ps2x2pico")
pico_set_program_version(ps2x2pico "0.7")
pico_set_program_version(ps2x2pico "0.8")

pico_enable_stdio_uart(ps2x2pico 1)
pico_enable_stdio_usb(ps2x2pico 0)
Expand Down
2 changes: 1 addition & 1 deletion ps2ms.c
Original file line number Diff line number Diff line change
Expand Up @@ -186,5 +186,5 @@ void ms_task() {
}

void ms_init() {
ps2phy_init(&ms_phy, pio1, MSDAT, &ms_receive);
ps2phy_init(&ms_phy, pio0, MSDAT, &ms_receive);
}
18 changes: 12 additions & 6 deletions ps2phy.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
#include "ps2phy.h"
#include "ps2phy.pio.h"

uint prog = 0;

u32 ps2phy_frame(u8 byte) {
bool parity = 1;
for (u8 i = 0; i < 8; i++) {
Expand All @@ -36,13 +38,17 @@ u32 ps2phy_frame(u8 byte) {
}

void ps2phy_init(ps2phy* this, PIO pio, u8 data_pin, rx_callback rx) {
if(!prog) {
prog = pio_add_program(pio, &ps2phy_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);
ps2phy_program_init(pio, this->sm, prog, data_pin);

this->pio = pio;
this->sent = 0;
this->rx = rx;
this->last_rx = 0;
Expand All @@ -65,7 +71,7 @@ void ps2phy_task(ps2phy* 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)) {
Expand All @@ -80,10 +86,10 @@ void ps2phy_task(ps2phy* 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)) {
Expand Down
2 changes: 1 addition & 1 deletion ps2phy.pio
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,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
Expand Down

0 comments on commit 358390e

Please sign in to comment.