1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
|
#include "hardware/regs/io_bank0.h"
#include "hardware/structs/padsbank0.h"
#include "uart_rx.pio.h"
#include "uart_tx.pio.h"
#include "split.h"
#include "util.h"
#include "matrix.h"
#include "hardware/pio.h"
#include "hardware/irq.h"
#include "hardware/gpio.h"
#include "hardware/address_mapped.h"
#include "hardware/regs/intctrl.h"
#include "hardware/regs/pads_bank0.h"
#include "hardware/uart.h"
#include "hardware/timer.h"
#include "hardware/clocks.h"
#include "pico/time.h"
#include "bsp/board.h"
#include "class/cdc/cdc_device.h"
#include "tusb.h"
#include <stdint.h>
#define UART_TIMEOUT 20
#define UART_BAUD 9600
#define UART_PIN 1
enum {
CMD_SCAN_MATRIX_REQ = 0xA0,
CMD_SCAN_MATRIX_RESP,
CMD_STDIO_PUTS
};
enum { SLAVE, MASTER };
enum { LEFT, RIGHT };
static void uart_tx_init(void);
static void uart_rx_init(void);
static void uart_full_init(void);
static void uart_leave_rx(void);
static void uart_enter_rx(void);
static bool uart_await_rx(uint32_t timeout_ms);
static bool uart_await_tx(uint32_t timeout_ms);
static uint8_t uart_rx_byte(void);
static void uart_tx_byte(uint8_t c);
static uint uart_recv(uint8_t *data, uint len);
static uint uart_send(const uint8_t *data, uint len);
static void handle_cmd(uint8_t cmd);
static void irq_rx(void);
static uint uart_tx_sm;
static uint uart_tx_sm_offset;
static uint uart_rx_sm;
static uint uart_rx_sm_offset;
static bool scan_pending = false;
void
uart_tx_init(void)
{
pio_sm_config config;
uart_tx_sm = CLAIM_UNUSED_SM(pio0);
uart_tx_sm_offset = pio_add_program(pio0, &uart_tx_program);
config = uart_tx_program_get_default_config(uart_tx_sm_offset);
sm_config_set_out_shift(&config, true, false, 32);
sm_config_set_out_pins(&config, UART_PIN, 1);
sm_config_set_sideset_pins(&config, UART_PIN);
sm_config_set_fifo_join(&config, PIO_FIFO_JOIN_TX);
sm_config_set_clkdiv(&config,
((float) clock_get_hz(clk_sys)) / (8 * UART_BAUD));
pio_sm_init(pio0, uart_tx_sm, uart_tx_sm_offset, &config);
pio_sm_set_enabled(pio0, uart_tx_sm, false);
}
void
uart_rx_init(void)
{
pio_sm_config config;
uart_rx_sm = CLAIM_UNUSED_SM(pio0);
uart_rx_sm_offset = pio_add_program(pio0, &uart_rx_program);
config = uart_rx_program_get_default_config(uart_rx_sm_offset);
sm_config_set_in_pins(&config, UART_PIN);
sm_config_set_jmp_pin(&config, UART_PIN);
sm_config_set_in_shift(&config, true, false, 32);
sm_config_set_fifo_join(&config, PIO_FIFO_JOIN_RX);
sm_config_set_clkdiv(&config,
((float) clock_get_hz(clk_sys)) / (8 * UART_BAUD));
pio_sm_init(pio0, uart_rx_sm, uart_rx_sm_offset, &config);
pio_sm_set_enabled(pio0, uart_rx_sm, false);
}
void
uart_full_init(void)
{
pio_sm_set_pins_with_mask(pio0, uart_tx_sm, 0U, 1U << UART_PIN);
pio_sm_set_consecutive_pindirs(pio0, uart_tx_sm, UART_PIN, 1, false);
pio_gpio_init(pio0, UART_PIN);
gpio_pull_up(UART_PIN);
gpio_set_slew_rate(UART_PIN, GPIO_SLEW_RATE_FAST);
/* 1 => set INPUT and pull line HIGH from pullup
* 0 => set OUTPUT and pull line LOW from signal */
gpio_set_oeover(UART_PIN, GPIO_OVERRIDE_INVERT);
gpio_set_drive_strength(UART_PIN, GPIO_DRIVE_STRENGTH_12MA);
uart_rx_init();
uart_tx_init();
pio_set_irq0_source_enabled(pio0,
pis_sm0_rx_fifo_not_empty + uart_rx_sm, false);
pio_set_irq0_source_enabled(pio0,
pis_sm0_tx_fifo_not_full + uart_tx_sm, false);
pio_set_irq0_source_enabled(pio0, pis_interrupt0, true);
pio_set_irq0_source_enabled(pio0, pis_interrupt1, true);
irq_set_priority(PIO0_IRQ_0, PICO_HIGHEST_IRQ_PRIORITY);
irq_set_exclusive_handler(PIO0_IRQ_0, irq_rx);
irq_set_enabled(PIO0_IRQ_0, true);
pio_sm_set_enabled(pio0, uart_tx_sm, true);
uart_enter_rx();
}
void
uart_leave_rx(void)
{
irq_set_enabled(USBCTRL_IRQ, false);
pio_sm_set_enabled(pio0, uart_rx_sm, false);
/* because of OE override pindir true = INPUT (!) */
//pio_sm_set_consecutive_pindirs(pio0, uart_tx_sm, UART_PIN, 1, true);
/* drive LOW with high drive-current for steep falling edges */
//pio_sm_set_pins_with_mask(pio0, uart_tx_sm, 0U, 1 << UART_PIN);
//gpio_set_drive_strength(UART_PIN, GPIO_DRIVE_STRENGTH_12MA);
/* input */
pio_sm_set_consecutive_pindirs(pio0, uart_tx_sm, UART_PIN, 1, true);
pio_sm_restart(pio0, uart_tx_sm);
pio_sm_set_enabled(pio0, uart_tx_sm, true);
}
void
uart_enter_rx(void)
{
/* wait for tx fifo to empty and final byte to transmit
* + extra 1 start + 8 data + 1 stop bit time*/
while (!pio_sm_is_tx_fifo_empty(pio0, uart_tx_sm));
sleep_us(1000000U * 10 / UART_BAUD);
pio_sm_set_enabled(pio0, uart_tx_sm, false);
/* pull HIGH with low drive-current for steeper rising edge */
//gpio_set_drive_strength(UART_PIN, GPIO_DRIVE_STRENGTH_2MA);
//pio_sm_set_pins_with_mask(pio0, uart_tx_sm, ~0U, 1 << UART_PIN);
/* because of OE override pindir false = OUTPUT (!) */
//pio_sm_set_consecutive_pindirs(pio0, uart_tx_sm, UART_PIN, 1, true);
/* input */
pio_sm_set_consecutive_pindirs(pio0, uart_tx_sm, UART_PIN, 1, true);
pio_sm_set_enabled(pio0, uart_rx_sm, true);
irq_set_enabled(USBCTRL_IRQ, true);
}
bool
uart_await_rx(uint32_t timeout_ms)
{
uint32_t start_ms;
bool empty;
start_ms = board_millis();
do {
empty = pio_sm_is_rx_fifo_empty(pio0, uart_rx_sm);
if (!empty) break;
tud_task();
} while (board_millis() < start_ms + timeout_ms);
return !empty;
}
bool
uart_await_tx(uint32_t timeout_ms)
{
uint32_t start_ms;
bool full;
start_ms = board_millis();
do {
full = pio_sm_is_tx_fifo_full(pio0, uart_tx_sm);
if (!full) break;
tud_task();
} while (board_millis() < start_ms + timeout_ms);
return !full;
}
uint8_t
uart_rx_byte(void)
{
return *(uint8_t*)((uintptr_t)&pio0->rxf[uart_rx_sm] + 3);
}
void
uart_tx_byte(uint8_t c)
{
pio_sm_put(pio0, uart_tx_sm, c);
}
uint
uart_recv(uint8_t *data, uint len)
{
uint recv;
for (recv = 0; recv < len; recv++) {
if (pio_sm_is_rx_fifo_empty(pio0, uart_rx_sm)) {
if (!uart_await_rx(UART_TIMEOUT))
break;
}
*data++ = uart_rx_byte();
}
return recv;
}
uint
uart_send(const uint8_t *data, uint len)
{
uint sent;
uart_leave_rx();
for (sent = 0; sent < len; sent++) {
if (pio_sm_is_tx_fifo_full(pio0, uart_tx_sm)) {
if (!uart_await_tx(UART_TIMEOUT))
break;
}
uart_tx_byte(*data++);
}
uart_enter_rx();
return sent;
}
void
handle_cmd(uint8_t cmd)
{
uint8_t buf[128];
DEBUG("Got command %i", cmd);
switch (cmd) {
case CMD_SCAN_MATRIX_REQ:
if (SPLIT_ROLE != SLAVE) {
WARN("Got SCAN_MATRIX_REQ as master");
break;
}
scan_pending = true;
return;
case CMD_SCAN_MATRIX_RESP:
if (SPLIT_ROLE != MASTER) {
WARN("Got SCAN_MATRIX_RESP as slave");
break;
}
scan_pending = false;
return;
case CMD_STDIO_PUTS:
if (SPLIT_ROLE != MASTER) {
WARN("Got STDIO_PUTS as slave");
break;
}
memset(buf, 0, sizeof(buf));
uart_recv(buf, sizeof(buf)-1);
printf("SLAVE: %s\n", buf);
return;
}
WARN("Unexpected uart cmd: %i", cmd);
}
void
irq_rx(void)
{
if (pio_interrupt_get(pio0, 0)) {
DEBUG("UART RX ERR");
pio_interrupt_clear(pio0, 0);
}
}
void
split_init(void)
{
uart_full_init();
}
void
split_task(void)
{
uint32_t start_ms;
uint8_t cmd;
// if (!uart_await_tx(20))
// return;
// DEBUG("Sending");
// uart_leave_rx();
// uart_tx_byte(CMD_SCAN_MATRIX_RESP);
// uart_enter_rx();
// return;
if (SPLIT_ROLE == MASTER) {
scan_pending = true;
cmd = CMD_SCAN_MATRIX_REQ;
ASSERT(uart_send(&cmd, 1) == 1);
//scan_matrix(); /* scan our side in parallel */
start_ms = board_millis();
while (scan_pending && board_millis() < start_ms + 1000) {
if (!pio_sm_is_rx_fifo_empty(pio0, uart_rx_sm))
handle_cmd(uart_rx_byte());
tud_task();
}
if (scan_pending) WARN("Slave matrix scan timeout");
else DEBUG("Slave matrix scan success");
scan_pending = false;
} else {
start_ms = board_millis();
while (!scan_pending && board_millis() < start_ms + 1000) {
if (!pio_sm_is_rx_fifo_empty(pio0, uart_rx_sm))
handle_cmd(uart_rx_byte());
tud_task();
}
if (scan_pending) {
sleep_ms(8); /* ensure tranmitter is ready */
//scan_matrix();
cmd = CMD_SCAN_MATRIX_RESP;
DEBUG("Sending SCAN_MATRIX_RESP %i", cmd);
ASSERT(uart_send(&cmd, 1) == 1);
scan_pending = false;
}
}
}
|