Skip to content

Commit

Permalink
add a 4th uart. big usb buffers to reduce risk of overflow, 63 byte u…
Browse files Browse the repository at this point in the history
…sb buffer to fix bug, no blocking on pio uart tx
  • Loading branch information
harrywalsh authored and JoeSc committed Sep 22, 2022
1 parent dc053f6 commit 17fb2c5
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 14 deletions.
6 changes: 3 additions & 3 deletions tusb_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@

#define CFG_TUSB_RHPORT0_MODE OPT_MODE_DEVICE

#define CFG_TUD_CDC 3
#define CFG_TUD_CDC_RX_BUFSIZE 256
#define CFG_TUD_CDC_TX_BUFSIZE 256
#define CFG_TUD_CDC 4
#define CFG_TUD_CDC_RX_BUFSIZE 10240
#define CFG_TUD_CDC_TX_BUFSIZE 10240

#endif /* _TUSB_CONFIG_H_ */
45 changes: 42 additions & 3 deletions uart-bridge.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

#define LED_PIN 25

#define BUFFER_SIZE 64
#define BUFFER_SIZE 63

#define DEF_BIT_RATE 115200
#define DEF_STOP_BITS 1
Expand All @@ -45,6 +45,7 @@ typedef struct {
mutex_t uart_mtx;
uint8_t usb_buffer[BUFFER_SIZE];
uint32_t usb_pos;
uint32_t usb_snd;
mutex_t usb_mtx;
} uart_data_t;

Expand All @@ -62,6 +63,11 @@ uart_id_t UART_ID[CFG_TUD_CDC] = {
.tx_pin = 8,
.rx_pin = 9,
.sm = 0,
},{
.inst = 0,
.tx_pin = 12,
.rx_pin = 13,
.sm = 1,
}
};

Expand Down Expand Up @@ -245,7 +251,7 @@ void uart_read_bytes(uint8_t itf) {

}

void uart_write_bytes(uint8_t itf) {
void old_uart_write_bytes(uint8_t itf) {
uart_data_t *ud = &UART_DATA[itf];

if (ud->usb_pos) {
Expand All @@ -269,6 +275,39 @@ void uart_write_bytes(uint8_t itf) {
}
}

void uart_write_bytes(uint8_t itf) {
uart_data_t *ud = &UART_DATA[itf];

if (ud->usb_pos) {
const uart_id_t *ui = &UART_ID[itf];

if (ui->inst != 0){
mutex_enter_blocking(&ud->usb_mtx);

uart_write_blocking(ui->inst, ud->usb_buffer, ud->usb_pos);
ud->usb_pos = 0;

mutex_exit(&ud->usb_mtx);
} else {
mutex_enter_blocking(&ud->usb_mtx);
if (ud->usb_snd < ud->usb_pos) {
size_t bufspace=7-pio_sm_get_tx_fifo_level(pio1,ui->sm);
size_t tosend=ud->usb_pos-ud->usb_snd;
tosend = MIN(tosend,bufspace);
for (size_t i = 0; i<tosend; ++i) {
uart_tx_program_putc(pio1, ui->sm, ud->usb_buffer[ud->usb_snd+i]);
}
ud->usb_snd+=tosend;
if (ud->usb_snd == ud->usb_pos) {
ud->usb_pos = 0;
ud->usb_snd = 0;
}
}
mutex_exit(&ud->usb_mtx);
}
}
}

void init_uart_data(uint8_t itf) {
uart_id_t *ui = &UART_ID[itf];
uart_data_t *ud = &UART_DATA[itf];
Expand All @@ -294,6 +333,7 @@ void init_uart_data(uint8_t itf) {
/* Buffer */
ud->uart_pos = 0;
ud->usb_pos = 0;
ud->usb_snd = 0;

/* Mutex */
mutex_init(&ud->lc_mtx);
Expand All @@ -309,7 +349,6 @@ void init_uart_data(uint8_t itf) {
parity_usb2uart(ud->usb_lc.parity));
} else {
// Set up the state machine we're going to use to for rx/tx
ui->sm = 0;
ui->roffset = pio_add_program(pio0, &uart_rx_program);
uart_rx_program_init(pio0, ui->sm, ui->roffset, ui->rx_pin, ud->uart_lc.bit_rate);
ui->toffset = pio_add_program(pio1, &uart_tx_program);
Expand Down
24 changes: 16 additions & 8 deletions usb-descriptors.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,26 @@
#define USBD_PID 0x000A /* Raspberry Pi Pico SDK CDC */

#define USBD_DESC_LEN (TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN * CFG_TUD_CDC)
#define USBD_MAX_POWER_MA 250
#define USBD_MAX_POWER_MA 500

#define USBD_ITF_CDC_0 0
#define USBD_ITF_CDC_1 2
#define USBD_ITF_CDC_2 4
#define USBD_ITF_MAX 6
#define USBD_ITF_CDC_3 6
#define USBD_ITF_MAX 8

#define USBD_CDC_0_EP_CMD 0x81
#define USBD_CDC_1_EP_CMD 0x84
#define USBD_CDC_0_EP_CMD 0x83
#define USBD_CDC_1_EP_CMD 0x85
#define USBD_CDC_2_EP_CMD 0x87
#define USBD_CDC_3_EP_CMD 0x89
#define USBD_CDC_0_EP_OUT 0x02
#define USBD_CDC_1_EP_OUT 0x05
#define USBD_CDC_2_EP_OUT 0x08
#define USBD_CDC_1_EP_OUT 0x04
#define USBD_CDC_2_EP_OUT 0x06
#define USBD_CDC_3_EP_OUT 0x08
#define USBD_CDC_0_EP_IN 0x82
#define USBD_CDC_1_EP_IN 0x85
#define USBD_CDC_2_EP_IN 0x88
#define USBD_CDC_1_EP_IN 0x84
#define USBD_CDC_2_EP_IN 0x86
#define USBD_CDC_3_EP_IN 0x88
#define USBD_CDC_CMD_MAX_SIZE 8
#define USBD_CDC_IN_OUT_MAX_SIZE 64

Expand Down Expand Up @@ -74,6 +78,10 @@ static const uint8_t usbd_desc_cfg[USBD_DESC_LEN] = {
TUD_CDC_DESCRIPTOR(USBD_ITF_CDC_2, USBD_STR_CDC, USBD_CDC_2_EP_CMD,
USBD_CDC_CMD_MAX_SIZE, USBD_CDC_2_EP_OUT, USBD_CDC_2_EP_IN,
USBD_CDC_IN_OUT_MAX_SIZE),

TUD_CDC_DESCRIPTOR(USBD_ITF_CDC_3, USBD_STR_CDC, USBD_CDC_3_EP_CMD,
USBD_CDC_CMD_MAX_SIZE, USBD_CDC_3_EP_OUT, USBD_CDC_3_EP_IN,
USBD_CDC_IN_OUT_MAX_SIZE),
};

static const char *const usbd_desc_str[] = {
Expand Down

0 comments on commit 17fb2c5

Please sign in to comment.