forked from 8086net/pico-sexa-uart-bridge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuart-i2c-bridge.c
More file actions
577 lines (494 loc) · 15.8 KB
/
uart-i2c-bridge.c
File metadata and controls
577 lines (494 loc) · 15.8 KB
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
// SPDX-License-Identifier: MIT
/*
* Copyright 2021 Álvaro Fernández Rojas <noltari@gmail.com>
* Copyright (c) 2022 Nicolai Electronics
* Copyright (c) 2023 Chris Burton
*/
#include "bsp/board.h"
#include <hardware/irq.h>
#include <hardware/structs/sio.h>
#include <hardware/uart.h>
#include "hardware/i2c.h"
#include <hardware/structs/pio.h>
#include <pico/multicore.h>
#include <pico/stdlib.h>
#include <string.h>
#include <tusb.h>
#include <uart_rx.pio.h>
#include <uart_tx.pio.h>
#include <hardware/flash.h>
#include "serial.h"
#include "kernel_i2c_flags.h"
#if !defined(MIN)
#define MIN(a, b) ((a > b) ? b : a)
#endif /* MIN */
// might as well use our RAM
#define BUFFER_SIZE 2560
// activity LED on duration
#define LED_TICKER_COUNT 500
#define DEF_BIT_RATE 9600
#define DEF_STOP_BITS 1
#define DEF_PARITY 0
#define DEF_DATA_BITS 8
#define I2C_INST i2c0
#define I2C_SDA 0
#define I2C_SCL 1
#define POWER_LED 22
/* commands from USB, must e.g. match command ids in kernel driver */
#define CMD_ECHO 0
#define CMD_GET_FUNC 1
#define CMD_SET_DELAY 2
#define CMD_GET_STATUS 3
#define CMD_I2C_IO 4
#define CMD_I2C_BEGIN 1 // flag fo I2C_IO
#define CMD_I2C_END 2 // flag fo I2C_IO
const unsigned long i2c_func = I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
#define STATUS_IDLE 0
#define STATUS_ADDRESS_ACK 1
#define STATUS_ADDRESS_NAK 2
static uint8_t i2c_state = STATUS_IDLE;
uint8_t i2c_data[1024] = {0};
uint8_t led_i2c_pin = 14;
uint32_t led_i2c_ticker;
typedef struct {
uart_inst_t *const inst;
uint8_t tx_pin;
uint8_t rx_pin;
uint sm;
uint8_t led_act_pin;
} uart_id_t;
typedef struct {
cdc_line_coding_t usb_lc;
cdc_line_coding_t uart_lc;
mutex_t lc_mtx;
uint8_t uart_rx_buffer[BUFFER_SIZE];
uint32_t uart_rx_pos;
uint8_t uart_to_usb_buffer[BUFFER_SIZE];
uint32_t uart_to_usb_pos;
mutex_t uart_mtx;
uint8_t usb_to_uart_buffer[BUFFER_SIZE];
uint32_t usb_to_uart_pos;
uint32_t usb_to_uart_snd;
mutex_t usb_mtx;
uint32_t led_act_ticker;
} uart_data_t;
uart_id_t UART_ID[CFG_TUD_CDC] = {
{
.inst = uart0,
.tx_pin = 16,
.rx_pin = 17,
.led_act_pin = 18,
}};
// ,{
// .inst = uart1,
// .tx_pin = 4,
// .rx_pin = 5,
// .led_act_pin = 3
// },{
// .inst = 0,
// .tx_pin = 8,
// .rx_pin = 9,
// .sm = 0,
// .led_act_pin = 6,
// },{
// .inst = 0,
// .tx_pin = 12,
// .rx_pin = 13,
// .sm = 1,
// .led_act_pin = 7,
// },{
// .inst = 0,
// .tx_pin = 16,
// .rx_pin = 17,
// .sm = 2,
// .led_act_pin = 10,
// },{
// .inst = 0,
// .tx_pin = 20,
// .rx_pin = 21,
// .sm = 3,
// .led_act_pin = 11,
// }
// };
uart_data_t UART_DATA[CFG_TUD_CDC];
uint rx_offset=0;
uint rxp_offset=0;
uint tx_offset=0;
uint txp_offset=0;
static inline uint databits_usb2uart(uint8_t data_bits)
{
switch (data_bits) {
case 5:
return 5;
case 6:
return 6;
case 7:
return 7;
default:
return 8;
}
}
static inline uart_parity_t parity_usb2uart(uint8_t usb_parity)
{
switch (usb_parity) {
case 1:
return UART_PARITY_ODD;
case 2:
return UART_PARITY_EVEN;
default:
return UART_PARITY_NONE;
}
}
static inline uint stopbits_usb2uart(uint8_t stop_bits)
{
switch (stop_bits) {
case 2:
return 2;
default:
return 1;
}
}
void update_uart_cfg(uint8_t itf)
{
uart_id_t *ui = &UART_ID[itf];
uart_data_t *ud = &UART_DATA[itf];
if (mutex_try_enter(&ud->lc_mtx, NULL)) {
if (ui->inst != 0) { //regular uart
if (ud->usb_lc.bit_rate != ud->uart_lc.bit_rate) {
uart_set_baudrate(ui->inst, ud->usb_lc.bit_rate);
ud->uart_lc.bit_rate = ud->usb_lc.bit_rate;
}
if ((ud->usb_lc.stop_bits != ud->uart_lc.stop_bits) ||
(ud->usb_lc.parity != ud->uart_lc.parity) ||
(ud->usb_lc.data_bits != ud->uart_lc.data_bits)) {
uart_set_format(ui->inst,
databits_usb2uart(ud->usb_lc.data_bits),
stopbits_usb2uart(ud->usb_lc.stop_bits),
parity_usb2uart(ud->usb_lc.parity));
ud->uart_lc.data_bits = ud->usb_lc.data_bits;
ud->uart_lc.parity = ud->usb_lc.parity;
ud->uart_lc.stop_bits = ud->usb_lc.stop_bits;
}
} else {
if (ud->usb_lc.bit_rate != ud->uart_lc.bit_rate) {
uart_baud(pio0,ui->sm,ud->usb_lc.bit_rate);
uart_baud(pio1,ui->sm,ud->usb_lc.bit_rate);
ud->uart_lc.bit_rate = ud->usb_lc.bit_rate;
}
if (ud->usb_lc.parity != ud->uart_lc.parity) {
ud->uart_lc.parity = ud->usb_lc.parity;
if (ud->usb_lc.parity == UART_PARITY_NONE) {
uart_rx_program_init(pio0, ui->sm, rx_offset, ui->rx_pin, ud->uart_lc.bit_rate);
uart_tx_program_init(pio1, ui->sm, tx_offset, ui->tx_pin, ud->uart_lc.bit_rate);
} else {
uart_rx_program_init(pio0, ui->sm, rxp_offset, ui->rx_pin, ud->uart_lc.bit_rate);
uart_tx_program_init(pio1, ui->sm, txp_offset, ui->tx_pin, ud->uart_lc.bit_rate);
}
}
}
mutex_exit(&ud->lc_mtx);
}
}
void usb_read_bytes(uint8_t itf) {
uint32_t len = tud_cdc_n_available(itf);
if (len) {
uart_data_t *ud = &UART_DATA[itf];
mutex_enter_blocking(&ud->usb_mtx);
len = MIN(len, BUFFER_SIZE - ud->usb_to_uart_pos);
if (len) {
uint32_t count;
count = tud_cdc_n_read(itf, &ud->usb_to_uart_buffer[ud->usb_to_uart_pos], len);
ud->usb_to_uart_pos += count;
}
mutex_exit(&ud->usb_mtx);
}
}
void usb_write_bytes(uint8_t itf) {
uart_data_t *ud = &UART_DATA[itf];
if (ud->uart_to_usb_pos && mutex_try_enter(&ud->uart_mtx, NULL)) {
uint32_t count;
count = tud_cdc_n_write(itf, ud->uart_to_usb_buffer, ud->uart_to_usb_pos);
if (count < ud->uart_to_usb_pos)
memcpy(ud->uart_to_usb_buffer, &ud->uart_to_usb_buffer[count],
ud->uart_to_usb_pos - count);
ud->uart_to_usb_pos -= count;
mutex_exit(&ud->uart_mtx);
if (count)
tud_cdc_n_write_flush(itf);
}
}
void usb_cdc_process(uint8_t itf)
{
uart_data_t *ud = &UART_DATA[itf];
mutex_enter_blocking(&ud->lc_mtx);
tud_cdc_n_get_line_coding(itf, &ud->usb_lc);
mutex_exit(&ud->lc_mtx);
usb_read_bytes(itf);
usb_write_bytes(itf);
}
void core1_entry(void)
{
tusb_init();
while (1) {
int itf;
tud_task();
for (itf = 0; itf < CFG_TUD_CDC; itf++) {
if (tud_cdc_n_connected(itf)) {
usb_cdc_process(itf);
}
}
if (led_i2c_ticker) {
gpio_put(led_i2c_pin, 1);
led_i2c_ticker--;
} else {
gpio_put(led_i2c_pin, 0);
}
}
}
void uart_read_bytes(uint8_t itf)
{
const uart_id_t *ui = &UART_ID[itf];
uart_data_t *ud = &UART_DATA[itf];
if (ui->inst != 0) {
if (uart_is_readable(ui->inst)) {
while (uart_is_readable(ui->inst) &&
ud->uart_rx_pos < BUFFER_SIZE) {
ud->uart_rx_buffer[ud->uart_rx_pos] = uart_getc(ui->inst);
ud->uart_rx_pos++;
ud->led_act_ticker = LED_TICKER_COUNT;
}
}
} else {
if (!pio_sm_is_rx_fifo_empty(pio0, ui->sm)) {
while (!pio_sm_is_rx_fifo_empty(pio0, ui->sm) &&
ud->uart_rx_pos < BUFFER_SIZE) {
ud->uart_rx_buffer[ud->uart_rx_pos] = uart_rx_program_getc(pio0, ui->sm);
ud->uart_rx_pos++;
ud->led_act_ticker = LED_TICKER_COUNT;
}
}
}
// If we can get the uart mutex then copy the UART data to the uart USB sender, otherwise we'll get it next time around
if (mutex_try_enter(&ud->uart_mtx, NULL)) {
// Ensure we don't overflow the uart_to_usb_buffer
uint32_t len = MIN(ud->uart_rx_pos, BUFFER_SIZE - ud->uart_to_usb_pos);
memcpy(&ud->uart_to_usb_buffer[ud->uart_to_usb_pos], ud->uart_rx_buffer, len);
ud->uart_to_usb_pos += len;
ud->uart_rx_pos = 0;
mutex_exit(&ud->uart_mtx);
}
if (ud->led_act_ticker) {
gpio_put(ui->led_act_pin, 1);
ud->led_act_ticker--;
} else {
gpio_put(ui->led_act_pin, 0);
}
}
void uart_write_bytes(uint8_t itf) {
const uart_id_t *ui = &UART_ID[itf];
uart_data_t *ud = &UART_DATA[itf];
// Try to get the usb_mutex and don't block if we cannot get it, we'll TX the data next passs
if ((ud->usb_to_uart_pos) && (ud->usb_to_uart_snd < ud->usb_to_uart_pos) &&
mutex_try_enter(&ud->usb_mtx, NULL)) {
const uart_id_t *ui = &UART_ID[itf];
ud->led_act_ticker = LED_TICKER_COUNT;
if (ui->inst != 0){
while (uart_is_writable(ui->inst)&&(ud->usb_to_uart_snd < ud->usb_to_uart_pos)) {
uart_putc(ui->inst, ud->usb_to_uart_buffer[ud->usb_to_uart_snd++]);
}
} else {
size_t bufspace=7-pio_sm_get_tx_fifo_level(pio1,ui->sm);
size_t tosend=ud->usb_to_uart_pos-ud->usb_to_uart_snd;
tosend = MIN(tosend,bufspace);
for (size_t i = 0; i<tosend; ++i) {
uart_tx_program_putc(pio1, ui->sm, ud->usb_to_uart_buffer[ud->usb_to_uart_snd+i],ud->usb_lc.parity);
}
ud->usb_to_uart_snd+=tosend;
}
// only reset buffers if we've sent everything
if (ud->usb_to_uart_snd == ud->usb_to_uart_pos) {
ud->usb_to_uart_pos = 0;
ud->usb_to_uart_snd = 0;
}
mutex_exit(&ud->usb_mtx);
}
if (ud->led_act_ticker) {
gpio_put(ui->led_act_pin, 1);
ud->led_act_ticker--;
} else {
gpio_put(ui->led_act_pin, 0);
}
}
static inline void init_usb_cdc_serial_num() {
uint8_t id[8];
flash_get_unique_id(id);
for (int i = 0; i < 8; ++i) {
sprintf(serial + 2 * i, "%X", id[i]);
}
serial[16] = '\0';
}
void init_uart_data(uint8_t itf) {
uart_id_t *ui = &UART_ID[itf];
uart_data_t *ud = &UART_DATA[itf];
if (ui->inst != 0) {
/* Pinmux */
gpio_set_function(ui->tx_pin, GPIO_FUNC_UART);
gpio_set_function(ui->rx_pin, GPIO_FUNC_UART);
}
/* USB CDC LC */
ud->usb_lc.bit_rate = DEF_BIT_RATE;
ud->usb_lc.data_bits = DEF_DATA_BITS;
ud->usb_lc.parity = DEF_PARITY;
ud->usb_lc.stop_bits = DEF_STOP_BITS;
/* UART LC */
ud->uart_lc.bit_rate = DEF_BIT_RATE;
ud->uart_lc.data_bits = DEF_DATA_BITS;
ud->uart_lc.parity = DEF_PARITY;
ud->uart_lc.stop_bits = DEF_STOP_BITS;
/* Buffer */
ud->uart_rx_pos = 0;
ud->uart_to_usb_pos = 0;
ud->usb_to_uart_pos = 0;
ud->usb_to_uart_snd = 0;
/* Mutex */
mutex_init(&ud->lc_mtx);
mutex_init(&ud->uart_mtx);
mutex_init(&ud->usb_mtx);
/* Activity LED */
gpio_init(ui->led_act_pin);
gpio_set_dir(ui->led_act_pin, GPIO_OUT);
gpio_put(ui->led_act_pin, 0);
ud->led_act_ticker = 0;
if (ui->inst != 0){
/* UART start */
uart_init(ui->inst, ud->usb_lc.bit_rate);
uart_set_hw_flow(ui->inst, false, false);
uart_set_format(ui->inst, databits_usb2uart(ud->usb_lc.data_bits),
stopbits_usb2uart(ud->usb_lc.stop_bits),
parity_usb2uart(ud->usb_lc.parity));
} else {
// Set up the state machine we're going to use to for rx/tx
uart_rx_program_init(pio0, ui->sm, rx_offset, ui->rx_pin, ud->uart_lc.bit_rate);
uart_tx_program_init(pio1, ui->sm, tx_offset, ui->tx_pin, ud->uart_lc.bit_rate);
}
}
bool tud_vendor_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t const* request) {
if (request->bmRequestType_bit.type == TUSB_REQ_TYPE_VENDOR) {
led_i2c_ticker = LED_TICKER_COUNT;
switch (request->bRequest) {
case CMD_ECHO:
if (stage != CONTROL_STAGE_SETUP) return true;
return tud_control_xfer(rhport, request, (void*) &request->wValue, sizeof(request->wValue));
case CMD_GET_FUNC:
if (stage != CONTROL_STAGE_SETUP) return true;
return tud_control_xfer(rhport, request, (void*) &i2c_func, sizeof(i2c_func));
break;
case CMD_SET_DELAY:
if (stage != CONTROL_STAGE_SETUP) return true;
if (request->wValue == 0) {
i2c_set_baudrate(I2C_INST, 100000); // Use default: 100kHz
} else {
int baudrate = 1000000 / request->wValue;
if (baudrate > 400000) baudrate = 400000; // Limit to 400kHz
i2c_set_baudrate(I2C_INST, baudrate);
}
return tud_control_status(rhport, request);
case CMD_GET_STATUS:
if (stage != CONTROL_STAGE_SETUP) return true;
return tud_control_xfer(rhport, request, (void*) &i2c_state, sizeof(i2c_state));
case CMD_I2C_IO:
case CMD_I2C_IO + CMD_I2C_BEGIN:
case CMD_I2C_IO + CMD_I2C_END:
case CMD_I2C_IO + CMD_I2C_BEGIN + CMD_I2C_END:
{
if (stage != CONTROL_STAGE_SETUP && stage != CONTROL_STAGE_DATA) return true;
bool nostop = !(request->bRequest & CMD_I2C_END);
//sprintf(buffer, "%s i2c %s at 0x%02x, len = %d, nostop = %d\r\n", (stage != CONTROL_STAGE_SETUP) ? "[D]" : "[S]", (request->wValue & I2C_M_RD)?"rd":"wr", request->wIndex, request->wLength, nostop);
//debug_print(buffer);
if (request->wLength > sizeof(i2c_data)) {
return false; // Prevent buffer overflow in case host sends us an impossible request
}
if (stage == CONTROL_STAGE_SETUP) { // Before transfering data
if (request->wValue & I2C_M_RD) {
// Reading from I2C device
int res = i2c_read_blocking(I2C_INST, request->wIndex, i2c_data, request->wLength, nostop);
if (res == PICO_ERROR_GENERIC) {
i2c_state = STATUS_ADDRESS_NAK;
} else {
i2c_state = STATUS_ADDRESS_ACK;
}
} else if (request->wLength == 0) { // Writing with length of 0, this is used for bus scanning, do dummy read
uint8_t dummy = 0x00;
int res = i2c_read_blocking(I2C_INST, request->wIndex, (void*) &dummy, 1, nostop);
if (res == PICO_ERROR_GENERIC) {
i2c_state = STATUS_ADDRESS_NAK;
} else {
i2c_state = STATUS_ADDRESS_ACK;
}
}
tud_control_xfer(rhport, request, (void*) i2c_data, request->wLength);
}
if (stage == CONTROL_STAGE_DATA) { // After transfering data
if (!(request->wValue & I2C_M_RD)) { // I2C write operation
int res = i2c_write_blocking(I2C_INST, request->wIndex, i2c_data, request->wLength, nostop);
if (res == PICO_ERROR_GENERIC) {
i2c_state = STATUS_ADDRESS_NAK;
} else {
i2c_state = STATUS_ADDRESS_ACK;
}
}
}
return true;
}
default:
if (stage != CONTROL_STAGE_SETUP) return true;
break;
}
} else {
if (stage != CONTROL_STAGE_SETUP) return true;
}
return false; // stall unknown request
}
bool tud_vendor_control_complete_cb(uint8_t rhport, tusb_control_request_t const* request) {
(void) rhport;
(void) request;
return true;
}
int main(void)
{
int itf;
// store our PIO programs in tbe instruction registers
// we'll use pio0 for RX and pio1 for tx so only one copy of each is needed
// however we'll use a different program to send/receive with parity
// rx_offset = pio_add_program(pio0, &uart_rx_program);
// tx_offset = pio_add_program(pio1, &uart_tx_program);
// rxp_offset = pio_add_program(pio0, &uart_rxp_program);
// txp_offset = pio_add_program(pio1, &uart_txp_program);
board_init();
// gpio_init(POWER_LED);
// gpio_set_dir(POWER_LED, GPIO_OUT);
// gpio_put(POWER_LED, 1);
gpio_init(I2C_SDA);
gpio_set_function(I2C_SDA, GPIO_FUNC_I2C);
gpio_pull_up(I2C_SDA);
gpio_init(I2C_SCL);
gpio_set_function(I2C_SCL, GPIO_FUNC_I2C);
gpio_pull_up(I2C_SCL);
i2c_init(I2C_INST, 100000);
gpio_init(led_i2c_pin);
gpio_set_dir(led_i2c_pin, GPIO_OUT);
gpio_put(led_i2c_pin, 0);
led_i2c_ticker = 0;
init_usb_cdc_serial_num();
// for (itf = 0; itf < CFG_TUD_CDC; itf++)
// init_uart_data(itf);
multicore_launch_core1(core1_entry);
while (1) {
for (itf = 0; itf < CFG_TUD_CDC; itf++) {
update_uart_cfg(itf);
uart_read_bytes(itf);
uart_write_bytes(itf);
}
}
return 0;
}