Skip to content

Commit

Permalink
UART bridge
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelBell committed Jul 30, 2023
1 parent 0db6c0f commit 3db290b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 12 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ target_link_libraries(${NAME}
hardware_dma
hardware_pio
hardware_watchdog
hardware_uart
tinyusb_device tinyusb_board
)

Expand Down
49 changes: 37 additions & 12 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

#include <pico/stdlib.h>
#include <hardware/watchdog.h>
#include <hardware/uart.h>

#include "bsp/board.h"
#include "tusb.h"
Expand All @@ -53,6 +54,7 @@ enum {
static uint32_t blink_interval_ms = BLINK_NOT_MOUNTED;

void led_blinking_task(void);
void cdc_init();
void cdc_task(void);

/*------------- MAIN -------------*/
Expand All @@ -69,6 +71,8 @@ int main(void)
for (int i = 0; i < 65536; ++i) emu_ram[i] = i;
}

cdc_init();

// init device stack on configured roothub port
tud_init(BOARD_TUD_RHPORT);

Expand Down Expand Up @@ -118,24 +122,45 @@ void tud_resume_cb(void)
//--------------------------------------------------------------------+
// USB CDC
//--------------------------------------------------------------------+

#define UART_BRIDGE_UART uart1
#define UART_BRIDGE_BAUD 93750
#define UART_BRIDGE_TX 8
#define UART_BRIDGE_RX 9
#define UART_BRIDGE_CTS 10

void cdc_init()
{
uart_init(UART_BRIDGE_UART, UART_BRIDGE_BAUD);

gpio_set_function(UART_BRIDGE_TX, GPIO_FUNC_UART);
gpio_set_function(UART_BRIDGE_RX, GPIO_FUNC_UART);

#ifdef UART_BRIDGE_CTS
gpio_set_function(UART_BRIDGE_CTS, GPIO_FUNC_UART);
uart_set_hw_flow(UART_BRIDGE_UART, true, false);
#endif
}

void cdc_task(void)
{
// connected() check for DTR bit
// Most but not all terminal client set this when making connection
// if ( tud_cdc_connected() )
if ( tud_cdc_connected() )
{
// connected and there are data available
if ( tud_cdc_available() )
while (tud_cdc_available() && uart_is_writable(UART_BRIDGE_UART))
{
uart_get_hw(UART_BRIDGE_UART)->dr = tud_cdc_read_char();
}

uint32_t count;
uint8_t buf[32];
for (count = 0; count < 32 && uart_is_readable(UART_BRIDGE_UART); ++count)
{
// read data
char buf[64];
uint32_t count = tud_cdc_read(buf, sizeof(buf));
(void) count;

// Echo back
// Note: Skip echo by commenting out write() and write_flush()
// for throughput test e.g
// $ dd if=/dev/zero of=/dev/ttyACM0 count=10000
buf[count] = (uint8_t)uart_get_hw(UART_BRIDGE_UART)->dr;
}

if (count > 0) {
tud_cdc_write(buf, count);
tud_cdc_write_flush();
}
Expand Down

0 comments on commit 3db290b

Please sign in to comment.