Skip to content

Commit

Permalink
uart-bridge: code refactor
Browse files Browse the repository at this point in the history
Avoid lines with more than 80 characters.
Fix indentation (tabs = 8 spaces).

Signed-off-by: Álvaro Fernández Rojas <[email protected]>
  • Loading branch information
Noltari committed Feb 3, 2021
1 parent f46d93b commit 8538384
Showing 1 changed file with 41 additions and 37 deletions.
78 changes: 41 additions & 37 deletions uart-bridge.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#define UART_TX_PIN 0
#define UART_RX_PIN 1

#define BUFFER_SIZE 64
#define BUFFER_SIZE 64

#define DEF_BIT_RATE 115200
#define DEF_STOP_BITS 1
Expand Down Expand Up @@ -96,11 +96,11 @@ int update_uart_cfg(void)
}

if ((last_cdc_lc.stop_bits != CDC_LC.stop_bits) ||
(last_cdc_lc.parity != CDC_LC.parity) ||
(last_cdc_lc.data_bits != CDC_LC.data_bits)) {
(last_cdc_lc.parity != CDC_LC.parity) ||
(last_cdc_lc.data_bits != CDC_LC.data_bits)) {
uart_set_format(UART_ID, databits_usb2uart(CDC_LC.data_bits),
stopbits_usb2uart(CDC_LC.stop_bits),
parity_usb2uart(CDC_LC.parity));
stopbits_usb2uart(CDC_LC.stop_bits),
parity_usb2uart(CDC_LC.parity));
updated = 1;
}

Expand All @@ -110,46 +110,49 @@ int update_uart_cfg(void)
return updated;
}

void core1_entry(void)
void usb_cdc_process(void)
{
tusb_init();

while (1) {
tud_task();
uint32_t count;
uint32_t len;

if (tud_cdc_connected()) {
uint32_t count;
tud_cdc_get_line_coding(&CDC_LC);

tud_cdc_get_line_coding(&CDC_LC);
/* Read bytes from USB */
if (tud_cdc_available()) {
mutex_enter_blocking(&USB_MTX);

/* Read bytes from USB */
if (tud_cdc_available()) {
uint32_t len;
len = MIN(tud_cdc_available(), BUFFER_SIZE - USB_POS);
if (len) {
count = tud_cdc_read(USB_BUFFER, len);
USB_POS += count;
}

mutex_enter_blocking(&USB_MTX);
mutex_exit(&USB_MTX);
}

len = MIN(tud_cdc_available(), BUFFER_SIZE - USB_POS);
if (len) {
count = tud_cdc_read(USB_BUFFER, len);
USB_POS += count;
}
/* Write bytes to USB */
if (UART_POS) {
mutex_enter_blocking(&UART_MTX);

mutex_exit(&USB_MTX);
}
count = tud_cdc_write(UART_BUFFER, UART_POS);
if (count) {
UART_POS -= count;
tud_cdc_write_flush();
}

/* Write bytes to USB */
if (UART_POS) {
mutex_enter_blocking(&UART_MTX);
mutex_exit(&UART_MTX);
}
}

count = tud_cdc_write(UART_BUFFER, UART_POS);
if (count) {
UART_POS -= count;
tud_cdc_write_flush();
}
void core1_entry(void)
{
tusb_init();

mutex_exit(&UART_MTX);
}
while (1) {
tud_task();

if (tud_cdc_connected()) {
usb_cdc_process();
gpio_put(LED_PIN, 1);
} else {
gpio_put(LED_PIN, 0);
Expand All @@ -175,8 +178,8 @@ int main(void)

uart_set_hw_flow(UART_ID, false, false);
uart_set_format(UART_ID, databits_usb2uart(CDC_LC.data_bits),
stopbits_usb2uart(CDC_LC.stop_bits),
parity_usb2uart(CDC_LC.parity));
stopbits_usb2uart(CDC_LC.stop_bits),
parity_usb2uart(CDC_LC.parity));

multicore_launch_core1(core1_entry);

Expand All @@ -187,7 +190,8 @@ int main(void)
if (uart_is_readable(UART_ID)) {
mutex_enter_blocking(&UART_MTX);

while (uart_is_readable(UART_ID) && UART_POS < BUFFER_SIZE) {
while (uart_is_readable(UART_ID) &&
UART_POS < BUFFER_SIZE) {
UART_BUFFER[UART_POS] = uart_getc(UART_ID);
UART_POS++;
}
Expand Down

0 comments on commit 8538384

Please sign in to comment.