Skip to content

Commit 201bfc9

Browse files
committed
joybus: Ignore the input bit and reset the loop if console turned off
1 parent c21051b commit 201bfc9

2 files changed

Lines changed: 34 additions & 27 deletions

File tree

include/joybus.pio.h

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,49 +13,51 @@
1313
// ------ //
1414

1515
#define joybus_wrap_target 0
16-
#define joybus_wrap 23
16+
#define joybus_wrap 25
1717
#define joybus_pio_version 1
1818

1919
#define joybus_T1 10
2020
#define joybus_T2 20
2121
#define joybus_T3 10
2222

2323
#define joybus_offset_read 0u
24-
#define joybus_offset_write 6u
24+
#define joybus_offset_write 8u
2525

2626
static const uint16_t joybus_program_instructions[] = {
2727
// .wrap_target
2828
0xe080, // 0: set pindirs, 0
2929
0x20a0, // 1: wait 1 pin, 0
3030
0x3320, // 2: wait 0 pin, 0 [19]
31-
0x4001, // 3: in pins, 1
32-
0x20a0, // 4: wait 1 pin, 0
33-
0x0002, // 5: jmp 2
34-
0xe081, // 6: set pindirs, 1
35-
0xe001, // 7: set pins, 1
36-
0x80e0, // 8: pull ifempty block
37-
0x6021, // 9: out x, 1
38-
0x00ef, // 10: jmp !osre, 15
39-
0x00b4, // 11: jmp x != y, 20
40-
0x80e0, // 12: pull ifempty block
41-
0x6021, // 13: out x, 1
42-
0x0010, // 14: jmp 16
43-
0xa342, // 15: nop [3]
44-
0xa142, // 16: nop [1]
45-
0xe900, // 17: set pins, 0 [9]
46-
0xb201, // 18: mov pins, x [18]
47-
0x0007, // 19: jmp 7
48-
0xa442, // 20: nop [4]
49-
0xe900, // 21: set pins, 0 [9]
50-
0xf201, // 22: set pins, 1 [18]
51-
0x0000, // 23: jmp 0
31+
0xab20, // 3: mov x, pins [11]
32+
0x00c6, // 4: jmp pin, 6
33+
0x0000, // 5: jmp 0
34+
0x4021, // 6: in x, 1
35+
0x0002, // 7: jmp 2
36+
0xe081, // 8: set pindirs, 1
37+
0xe001, // 9: set pins, 1
38+
0x80e0, // 10: pull ifempty block
39+
0x6021, // 11: out x, 1
40+
0x00f1, // 12: jmp !osre, 17
41+
0x00b6, // 13: jmp x != y, 22
42+
0x80e0, // 14: pull ifempty block
43+
0x6021, // 15: out x, 1
44+
0x0012, // 16: jmp 18
45+
0xa342, // 17: nop [3]
46+
0xa142, // 18: nop [1]
47+
0xe900, // 19: set pins, 0 [9]
48+
0xb201, // 20: mov pins, x [18]
49+
0x0009, // 21: jmp 9
50+
0xa442, // 22: nop [4]
51+
0xe900, // 23: set pins, 0 [9]
52+
0xf201, // 24: set pins, 1 [18]
53+
0x0000, // 25: jmp 0
5254
// .wrap
5355
};
5456

5557
#if !PICO_NO_HARDWARE
5658
static const struct pio_program joybus_program = {
5759
.instructions = joybus_program_instructions,
58-
.length = 24,
60+
.length = 26,
5961
.origin = -1,
6062
.pio_version = joybus_pio_version,
6163
#if PICO_PIO_VERSION > 0
@@ -76,6 +78,7 @@ static inline pio_sm_config joybus_program_get_config(PIO pio, uint sm, uint off
7678
sm_config_set_out_pins(&c, pin, 1);
7779
sm_config_set_set_pins(&c, pin, 1);
7880
sm_config_set_in_pins(&c, pin);
81+
sm_config_set_jmp_pin(&c, pin);
7982
// Shift to left, no autopull, 9 bit
8083
sm_config_set_out_shift(&c, false, false, 9);
8184
// Shift to left, autopush, 8 bit

src/joybus.pio

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@
88
; Autopush with 8 bit ISR threshold
99
public read:
1010
set pindirs 0 ; Set pin to input
11-
wait 1 pin 0 ; Make sure the line is high before entering the loop
11+
wait 1 pin 0 ; Done reading, so make sure we wait for the line to go high again before restarting the loop
1212
read_loop:
1313
wait 0 pin 0 [T1 + T2 / 2 - 1] ; Wait for falling edge, then wait until halfway through the 2uS which represents the bit value
14-
in pins, 1 ; Read bit value
15-
wait 1 pin 0 ; Done reading, so make sure we wait for the line to go high again before restarting the loop
14+
mov x, pins [T2 / 2 + 1] ; Read bit value, then wait until the expected rising edge
15+
jmp pin read_next ; Push the bit value if there was a rising edge, if not reset the loop
16+
jmp read
17+
read_next:
18+
in x, 1 ; Push bit value
1619
jmp read_loop
1720

1821

@@ -52,6 +55,7 @@ static inline pio_sm_config joybus_program_get_config(PIO pio, uint sm, uint off
5255
sm_config_set_out_pins(&c, pin, 1);
5356
sm_config_set_set_pins(&c, pin, 1);
5457
sm_config_set_in_pins(&c, pin);
58+
sm_config_set_jmp_pin(&c, pin);
5559
// Shift to left, no autopull, 9 bit
5660
sm_config_set_out_shift(&c, false, false, 9);
5761
// Shift to left, autopush, 8 bit

0 commit comments

Comments
 (0)