2021-02-03 11:49:34 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
/*
|
|
|
|
* Copyright 2021 Álvaro Fernández Rojas <noltari@gmail.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <hardware/irq.h>
|
|
|
|
#include <hardware/structs/sio.h>
|
|
|
|
#include <hardware/uart.h>
|
|
|
|
#include <pico/multicore.h>
|
|
|
|
#include <pico/stdlib.h>
|
2021-02-06 11:05:47 +00:00
|
|
|
#include <string.h>
|
2021-02-03 11:49:34 +00:00
|
|
|
#include <tusb.h>
|
|
|
|
|
|
|
|
#if !defined(MIN)
|
|
|
|
#define MIN(a, b) ((a > b) ? b : a)
|
|
|
|
#endif /* MIN */
|
|
|
|
|
|
|
|
#define LED_PIN 25
|
|
|
|
|
2022-11-04 09:59:54 +00:00
|
|
|
#define BUFFER_SIZE 2560
|
2021-02-03 11:49:34 +00:00
|
|
|
|
|
|
|
#define DEF_BIT_RATE 115200
|
|
|
|
#define DEF_STOP_BITS 1
|
|
|
|
#define DEF_PARITY 0
|
|
|
|
#define DEF_DATA_BITS 8
|
|
|
|
|
2021-02-04 08:02:27 +00:00
|
|
|
typedef struct {
|
|
|
|
uart_inst_t *const inst;
|
2022-11-04 10:00:31 +00:00
|
|
|
uint irq;
|
|
|
|
void *irq_fn;
|
2021-02-04 08:02:27 +00:00
|
|
|
uint8_t tx_pin;
|
|
|
|
uint8_t rx_pin;
|
|
|
|
} uart_id_t;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
cdc_line_coding_t usb_lc;
|
|
|
|
cdc_line_coding_t uart_lc;
|
2021-02-06 10:05:12 +00:00
|
|
|
mutex_t lc_mtx;
|
2021-02-04 08:02:27 +00:00
|
|
|
uint8_t uart_buffer[BUFFER_SIZE];
|
|
|
|
uint32_t uart_pos;
|
|
|
|
mutex_t uart_mtx;
|
|
|
|
uint8_t usb_buffer[BUFFER_SIZE];
|
|
|
|
uint32_t usb_pos;
|
|
|
|
mutex_t usb_mtx;
|
|
|
|
} uart_data_t;
|
|
|
|
|
2022-11-04 10:00:31 +00:00
|
|
|
void uart0_irq_fn(void);
|
|
|
|
void uart1_irq_fn(void);
|
|
|
|
|
2021-02-04 08:02:27 +00:00
|
|
|
const uart_id_t UART_ID[CFG_TUD_CDC] = {
|
|
|
|
{
|
|
|
|
.inst = uart0,
|
2022-11-04 10:00:31 +00:00
|
|
|
.irq = UART0_IRQ,
|
|
|
|
.irq_fn = &uart0_irq_fn,
|
2022-11-04 10:08:49 +00:00
|
|
|
.tx_pin = 16,
|
|
|
|
.rx_pin = 17,
|
2021-02-04 08:02:27 +00:00
|
|
|
}, {
|
|
|
|
.inst = uart1,
|
2022-11-04 10:00:31 +00:00
|
|
|
.irq = UART1_IRQ,
|
|
|
|
.irq_fn = &uart1_irq_fn,
|
2021-02-04 08:02:27 +00:00
|
|
|
.tx_pin = 4,
|
|
|
|
.rx_pin = 5,
|
|
|
|
}
|
2021-02-03 11:49:34 +00:00
|
|
|
};
|
|
|
|
|
2021-02-04 08:02:27 +00:00
|
|
|
uart_data_t UART_DATA[CFG_TUD_CDC];
|
2021-02-03 11:49:34 +00:00
|
|
|
|
2021-02-03 14:33:45 +00:00
|
|
|
static inline uint databits_usb2uart(uint8_t data_bits)
|
2021-02-03 11:49:34 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-03 14:33:45 +00:00
|
|
|
static inline uint stopbits_usb2uart(uint8_t stop_bits)
|
2021-02-03 11:49:34 +00:00
|
|
|
{
|
|
|
|
switch (stop_bits) {
|
|
|
|
case 2:
|
|
|
|
return 2;
|
|
|
|
default:
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-04 08:02:27 +00:00
|
|
|
void update_uart_cfg(uint8_t itf)
|
2021-02-03 11:49:34 +00:00
|
|
|
{
|
2021-02-04 08:02:27 +00:00
|
|
|
const uart_id_t *ui = &UART_ID[itf];
|
|
|
|
uart_data_t *ud = &UART_DATA[itf];
|
2021-02-03 11:49:34 +00:00
|
|
|
|
2021-02-06 10:05:12 +00:00
|
|
|
mutex_enter_blocking(&ud->lc_mtx);
|
|
|
|
|
2021-02-04 08:02:27 +00:00
|
|
|
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;
|
2021-02-03 11:49:34 +00:00
|
|
|
}
|
|
|
|
|
2021-02-04 08:02:27 +00:00
|
|
|
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;
|
|
|
|
}
|
2021-02-06 10:05:12 +00:00
|
|
|
|
|
|
|
mutex_exit(&ud->lc_mtx);
|
2021-02-03 11:49:34 +00:00
|
|
|
}
|
|
|
|
|
2022-11-04 10:00:31 +00:00
|
|
|
void usb_read_bytes(uint8_t itf)
|
|
|
|
{
|
|
|
|
uart_data_t *ud = &UART_DATA[itf];
|
2021-02-04 08:02:27 +00:00
|
|
|
uint32_t len = tud_cdc_n_available(itf);
|
2021-02-03 11:49:34 +00:00
|
|
|
|
2022-11-04 10:00:31 +00:00
|
|
|
if (len &&
|
|
|
|
mutex_try_enter(&ud->usb_mtx, NULL)) {
|
2021-02-04 08:02:27 +00:00
|
|
|
len = MIN(len, BUFFER_SIZE - ud->usb_pos);
|
2021-02-03 14:45:04 +00:00
|
|
|
if (len) {
|
2021-02-04 08:02:27 +00:00
|
|
|
uint32_t count;
|
|
|
|
|
2021-10-25 16:42:30 +00:00
|
|
|
count = tud_cdc_n_read(itf, &ud->usb_buffer[ud->usb_pos], len);
|
2021-02-04 08:02:27 +00:00
|
|
|
ud->usb_pos += count;
|
2021-02-03 14:45:04 +00:00
|
|
|
}
|
2021-02-03 11:49:34 +00:00
|
|
|
|
2021-02-04 08:02:27 +00:00
|
|
|
mutex_exit(&ud->usb_mtx);
|
2021-02-03 14:45:04 +00:00
|
|
|
}
|
2021-02-04 08:02:27 +00:00
|
|
|
}
|
2021-02-03 11:49:34 +00:00
|
|
|
|
2022-11-04 10:00:31 +00:00
|
|
|
void usb_write_bytes(uint8_t itf)
|
|
|
|
{
|
2021-02-04 08:02:27 +00:00
|
|
|
uart_data_t *ud = &UART_DATA[itf];
|
2021-02-03 11:49:34 +00:00
|
|
|
|
2022-11-04 10:00:31 +00:00
|
|
|
if (ud->uart_pos &&
|
|
|
|
mutex_try_enter(&ud->uart_mtx, NULL)) {
|
2021-02-04 08:02:27 +00:00
|
|
|
uint32_t count;
|
|
|
|
|
|
|
|
count = tud_cdc_n_write(itf, ud->uart_buffer, ud->uart_pos);
|
2021-02-06 11:05:47 +00:00
|
|
|
if (count < ud->uart_pos)
|
2023-01-29 18:49:11 +00:00
|
|
|
memmove(ud->uart_buffer, &ud->uart_buffer[count],
|
2021-02-06 11:05:47 +00:00
|
|
|
ud->uart_pos - count);
|
|
|
|
ud->uart_pos -= count;
|
2021-02-03 11:49:34 +00:00
|
|
|
|
2021-02-04 08:02:27 +00:00
|
|
|
mutex_exit(&ud->uart_mtx);
|
2021-02-06 11:05:47 +00:00
|
|
|
|
|
|
|
if (count)
|
|
|
|
tud_cdc_n_write_flush(itf);
|
2021-02-03 14:45:04 +00:00
|
|
|
}
|
|
|
|
}
|
2021-02-03 11:49:34 +00:00
|
|
|
|
2021-02-04 08:02:27 +00:00
|
|
|
void usb_cdc_process(uint8_t itf)
|
|
|
|
{
|
|
|
|
uart_data_t *ud = &UART_DATA[itf];
|
|
|
|
|
2021-02-06 10:05:12 +00:00
|
|
|
mutex_enter_blocking(&ud->lc_mtx);
|
2021-02-04 08:02:27 +00:00
|
|
|
tud_cdc_n_get_line_coding(itf, &ud->usb_lc);
|
2021-02-06 10:05:12 +00:00
|
|
|
mutex_exit(&ud->lc_mtx);
|
|
|
|
|
2021-02-04 08:02:27 +00:00
|
|
|
usb_read_bytes(itf);
|
|
|
|
usb_write_bytes(itf);
|
|
|
|
}
|
|
|
|
|
2021-02-03 14:45:04 +00:00
|
|
|
void core1_entry(void)
|
|
|
|
{
|
|
|
|
tusb_init();
|
2021-02-03 11:49:34 +00:00
|
|
|
|
2021-02-03 14:45:04 +00:00
|
|
|
while (1) {
|
2021-02-04 08:02:27 +00:00
|
|
|
int itf;
|
|
|
|
int con = 0;
|
2021-02-03 11:49:34 +00:00
|
|
|
|
2021-02-04 08:02:27 +00:00
|
|
|
tud_task();
|
2022-11-04 09:58:51 +00:00
|
|
|
|
2021-02-04 08:02:27 +00:00
|
|
|
for (itf = 0; itf < CFG_TUD_CDC; itf++) {
|
|
|
|
if (tud_cdc_n_connected(itf)) {
|
|
|
|
con = 1;
|
|
|
|
usb_cdc_process(itf);
|
|
|
|
}
|
2021-02-03 11:49:34 +00:00
|
|
|
}
|
2021-02-04 08:02:27 +00:00
|
|
|
|
|
|
|
gpio_put(LED_PIN, con);
|
2021-02-03 14:29:50 +00:00
|
|
|
}
|
2021-02-03 11:49:34 +00:00
|
|
|
}
|
|
|
|
|
2022-11-04 10:00:31 +00:00
|
|
|
static inline void uart_read_bytes(uint8_t itf)
|
|
|
|
{
|
|
|
|
uart_data_t *ud = &UART_DATA[itf];
|
2021-02-04 08:02:27 +00:00
|
|
|
const uart_id_t *ui = &UART_ID[itf];
|
2021-02-03 11:49:34 +00:00
|
|
|
|
2021-02-04 08:02:27 +00:00
|
|
|
if (uart_is_readable(ui->inst)) {
|
|
|
|
mutex_enter_blocking(&ud->uart_mtx);
|
2021-02-03 11:49:34 +00:00
|
|
|
|
2021-02-04 08:02:27 +00:00
|
|
|
while (uart_is_readable(ui->inst) &&
|
2022-11-04 10:00:31 +00:00
|
|
|
(ud->uart_pos < BUFFER_SIZE)) {
|
2021-02-04 08:02:27 +00:00
|
|
|
ud->uart_buffer[ud->uart_pos] = uart_getc(ui->inst);
|
|
|
|
ud->uart_pos++;
|
|
|
|
}
|
2021-02-03 11:49:34 +00:00
|
|
|
|
2021-02-04 08:02:27 +00:00
|
|
|
mutex_exit(&ud->uart_mtx);
|
|
|
|
}
|
|
|
|
}
|
2021-02-03 11:49:34 +00:00
|
|
|
|
2022-11-04 10:00:31 +00:00
|
|
|
void uart0_irq_fn(void)
|
|
|
|
{
|
|
|
|
uart_read_bytes(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void uart1_irq_fn(void)
|
|
|
|
{
|
|
|
|
uart_read_bytes(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void uart_write_bytes(uint8_t itf)
|
|
|
|
{
|
2021-02-04 08:02:27 +00:00
|
|
|
uart_data_t *ud = &UART_DATA[itf];
|
2021-02-03 11:49:34 +00:00
|
|
|
|
2022-11-04 10:00:31 +00:00
|
|
|
if (ud->usb_pos &&
|
|
|
|
mutex_try_enter(&ud->usb_mtx, NULL)) {
|
2021-02-04 08:02:27 +00:00
|
|
|
const uart_id_t *ui = &UART_ID[itf];
|
2022-11-04 10:00:31 +00:00
|
|
|
uint32_t count = 0;
|
2021-02-03 11:49:34 +00:00
|
|
|
|
2022-11-04 10:00:31 +00:00
|
|
|
while (uart_is_writable(ui->inst) &&
|
|
|
|
count < ud->usb_pos) {
|
2022-11-04 10:13:57 +00:00
|
|
|
uart_putc_raw(ui->inst, ud->usb_buffer[count]);
|
2022-11-04 10:00:31 +00:00
|
|
|
count++;
|
|
|
|
}
|
2021-02-03 11:49:34 +00:00
|
|
|
|
2022-11-04 10:00:31 +00:00
|
|
|
if (count < ud->usb_pos)
|
2023-01-29 18:49:11 +00:00
|
|
|
memmove(ud->usb_buffer, &ud->usb_buffer[count],
|
2022-11-04 10:00:31 +00:00
|
|
|
ud->usb_pos - count);
|
|
|
|
ud->usb_pos -= count;
|
2021-02-03 11:49:34 +00:00
|
|
|
|
2021-02-04 08:02:27 +00:00
|
|
|
mutex_exit(&ud->usb_mtx);
|
|
|
|
}
|
|
|
|
}
|
2021-02-03 11:49:34 +00:00
|
|
|
|
2022-11-04 10:00:31 +00:00
|
|
|
void init_uart_data(uint8_t itf)
|
|
|
|
{
|
2021-02-04 08:02:27 +00:00
|
|
|
const uart_id_t *ui = &UART_ID[itf];
|
|
|
|
uart_data_t *ud = &UART_DATA[itf];
|
|
|
|
|
|
|
|
/* 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_pos = 0;
|
|
|
|
ud->usb_pos = 0;
|
|
|
|
|
|
|
|
/* Mutex */
|
2021-02-06 10:05:12 +00:00
|
|
|
mutex_init(&ud->lc_mtx);
|
2021-02-04 08:02:27 +00:00
|
|
|
mutex_init(&ud->uart_mtx);
|
|
|
|
mutex_init(&ud->usb_mtx);
|
|
|
|
|
|
|
|
/* 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));
|
2022-11-04 10:00:31 +00:00
|
|
|
uart_set_fifo_enabled(ui->inst, false);
|
|
|
|
|
|
|
|
/* UART RX Interrupt */
|
|
|
|
irq_set_exclusive_handler(ui->irq, ui->irq_fn);
|
|
|
|
irq_set_enabled(ui->irq, true);
|
|
|
|
uart_set_irq_enables(ui->inst, true, false);
|
2021-02-04 08:02:27 +00:00
|
|
|
}
|
2021-02-03 11:49:34 +00:00
|
|
|
|
2021-02-04 08:02:27 +00:00
|
|
|
int main(void)
|
|
|
|
{
|
|
|
|
int itf;
|
2021-02-03 11:49:34 +00:00
|
|
|
|
2022-05-03 17:59:18 +00:00
|
|
|
set_sys_clock_khz(250000, false);
|
|
|
|
|
2022-11-03 17:12:56 +00:00
|
|
|
usbd_serial_init();
|
|
|
|
|
2021-02-04 08:02:27 +00:00
|
|
|
for (itf = 0; itf < CFG_TUD_CDC; itf++)
|
|
|
|
init_uart_data(itf);
|
2021-02-03 11:49:34 +00:00
|
|
|
|
2021-02-04 08:02:27 +00:00
|
|
|
gpio_init(LED_PIN);
|
|
|
|
gpio_set_dir(LED_PIN, GPIO_OUT);
|
|
|
|
|
|
|
|
multicore_launch_core1(core1_entry);
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
for (itf = 0; itf < CFG_TUD_CDC; itf++) {
|
|
|
|
update_uart_cfg(itf);
|
|
|
|
uart_write_bytes(itf);
|
2021-02-03 11:49:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|