mirror of
https://github.com/Noltari/pico-uart-bridge.git
synced 2025-05-14 08:12:20 +00:00
uart-bridge: code refactor
Avoid lines with more than 80 characters. Fix indentation (tabs = 8 spaces). Signed-off-by: Álvaro Fernández Rojas <noltari@gmail.com>
This commit is contained in:
parent
f46d93bb54
commit
85383842d5
1 changed files with 44 additions and 40 deletions
|
@ -22,7 +22,7 @@
|
||||||
#define UART_TX_PIN 0
|
#define UART_TX_PIN 0
|
||||||
#define UART_RX_PIN 1
|
#define UART_RX_PIN 1
|
||||||
|
|
||||||
#define BUFFER_SIZE 64
|
#define BUFFER_SIZE 64
|
||||||
|
|
||||||
#define DEF_BIT_RATE 115200
|
#define DEF_BIT_RATE 115200
|
||||||
#define DEF_STOP_BITS 1
|
#define DEF_STOP_BITS 1
|
||||||
|
@ -96,11 +96,11 @@ int update_uart_cfg(void)
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((last_cdc_lc.stop_bits != CDC_LC.stop_bits) ||
|
if ((last_cdc_lc.stop_bits != CDC_LC.stop_bits) ||
|
||||||
(last_cdc_lc.parity != CDC_LC.parity) ||
|
(last_cdc_lc.parity != CDC_LC.parity) ||
|
||||||
(last_cdc_lc.data_bits != CDC_LC.data_bits)) {
|
(last_cdc_lc.data_bits != CDC_LC.data_bits)) {
|
||||||
uart_set_format(UART_ID, databits_usb2uart(CDC_LC.data_bits),
|
uart_set_format(UART_ID, databits_usb2uart(CDC_LC.data_bits),
|
||||||
stopbits_usb2uart(CDC_LC.stop_bits),
|
stopbits_usb2uart(CDC_LC.stop_bits),
|
||||||
parity_usb2uart(CDC_LC.parity));
|
parity_usb2uart(CDC_LC.parity));
|
||||||
updated = 1;
|
updated = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -110,6 +110,40 @@ int update_uart_cfg(void)
|
||||||
return updated;
|
return updated;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void usb_cdc_process(void)
|
||||||
|
{
|
||||||
|
uint32_t count;
|
||||||
|
uint32_t len;
|
||||||
|
|
||||||
|
tud_cdc_get_line_coding(&CDC_LC);
|
||||||
|
|
||||||
|
/* Read bytes from USB */
|
||||||
|
if (tud_cdc_available()) {
|
||||||
|
mutex_enter_blocking(&USB_MTX);
|
||||||
|
|
||||||
|
len = MIN(tud_cdc_available(), BUFFER_SIZE - USB_POS);
|
||||||
|
if (len) {
|
||||||
|
count = tud_cdc_read(USB_BUFFER, len);
|
||||||
|
USB_POS += count;
|
||||||
|
}
|
||||||
|
|
||||||
|
mutex_exit(&USB_MTX);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Write bytes to USB */
|
||||||
|
if (UART_POS) {
|
||||||
|
mutex_enter_blocking(&UART_MTX);
|
||||||
|
|
||||||
|
count = tud_cdc_write(UART_BUFFER, UART_POS);
|
||||||
|
if (count) {
|
||||||
|
UART_POS -= count;
|
||||||
|
tud_cdc_write_flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
mutex_exit(&UART_MTX);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void core1_entry(void)
|
void core1_entry(void)
|
||||||
{
|
{
|
||||||
tusb_init();
|
tusb_init();
|
||||||
|
@ -118,38 +152,7 @@ void core1_entry(void)
|
||||||
tud_task();
|
tud_task();
|
||||||
|
|
||||||
if (tud_cdc_connected()) {
|
if (tud_cdc_connected()) {
|
||||||
uint32_t count;
|
usb_cdc_process();
|
||||||
|
|
||||||
tud_cdc_get_line_coding(&CDC_LC);
|
|
||||||
|
|
||||||
/* Read bytes from USB */
|
|
||||||
if (tud_cdc_available()) {
|
|
||||||
uint32_t len;
|
|
||||||
|
|
||||||
mutex_enter_blocking(&USB_MTX);
|
|
||||||
|
|
||||||
len = MIN(tud_cdc_available(), BUFFER_SIZE - USB_POS);
|
|
||||||
if (len) {
|
|
||||||
count = tud_cdc_read(USB_BUFFER, len);
|
|
||||||
USB_POS += count;
|
|
||||||
}
|
|
||||||
|
|
||||||
mutex_exit(&USB_MTX);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Write bytes to USB */
|
|
||||||
if (UART_POS) {
|
|
||||||
mutex_enter_blocking(&UART_MTX);
|
|
||||||
|
|
||||||
count = tud_cdc_write(UART_BUFFER, UART_POS);
|
|
||||||
if (count) {
|
|
||||||
UART_POS -= count;
|
|
||||||
tud_cdc_write_flush();
|
|
||||||
}
|
|
||||||
|
|
||||||
mutex_exit(&UART_MTX);
|
|
||||||
}
|
|
||||||
|
|
||||||
gpio_put(LED_PIN, 1);
|
gpio_put(LED_PIN, 1);
|
||||||
} else {
|
} else {
|
||||||
gpio_put(LED_PIN, 0);
|
gpio_put(LED_PIN, 0);
|
||||||
|
@ -175,8 +178,8 @@ int main(void)
|
||||||
|
|
||||||
uart_set_hw_flow(UART_ID, false, false);
|
uart_set_hw_flow(UART_ID, false, false);
|
||||||
uart_set_format(UART_ID, databits_usb2uart(CDC_LC.data_bits),
|
uart_set_format(UART_ID, databits_usb2uart(CDC_LC.data_bits),
|
||||||
stopbits_usb2uart(CDC_LC.stop_bits),
|
stopbits_usb2uart(CDC_LC.stop_bits),
|
||||||
parity_usb2uart(CDC_LC.parity));
|
parity_usb2uart(CDC_LC.parity));
|
||||||
|
|
||||||
multicore_launch_core1(core1_entry);
|
multicore_launch_core1(core1_entry);
|
||||||
|
|
||||||
|
@ -187,7 +190,8 @@ int main(void)
|
||||||
if (uart_is_readable(UART_ID)) {
|
if (uart_is_readable(UART_ID)) {
|
||||||
mutex_enter_blocking(&UART_MTX);
|
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_BUFFER[UART_POS] = uart_getc(UART_ID);
|
||||||
UART_POS++;
|
UART_POS++;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue