From da6a4ed330f922e6396b10cb8135719f144a223d Mon Sep 17 00:00:00 2001 From: Paciente8159 Date: Tue, 10 Oct 2023 15:52:29 +0100 Subject: [PATCH] Modified LPC176x --- uCNC/src/hal/mcus/lpc176x/lpc176x_arduino.cpp | 46 ++++++-- uCNC/src/hal/mcus/lpc176x/mcu_lpc176x.c | 101 +++++++++++++----- 2 files changed, 108 insertions(+), 39 deletions(-) diff --git a/uCNC/src/hal/mcus/lpc176x/lpc176x_arduino.cpp b/uCNC/src/hal/mcus/lpc176x/lpc176x_arduino.cpp index 428e00fdc..01f552088 100644 --- a/uCNC/src/hal/mcus/lpc176x/lpc176x_arduino.cpp +++ b/uCNC/src/hal/mcus/lpc176x/lpc176x_arduino.cpp @@ -32,9 +32,30 @@ extern "C" { #include "../../../cnc.h" #ifdef USE_ARDUINO_CDC + DECL_BUFFER(uint8_t, usb_rx, RX_BUFFER_SIZE); + void mcu_usb_dotasks(void) { MSC_RunDeferredCommands(); + while (UsbSerial.available() > 0) + { +#ifndef DETACH_USB_FROM_MAIN_PROTOCOL + uint8_t c = (uint8_t)UsbSerial.read(); + if (mcu_com_rx_cb(c)) + { + if (BUFFER_FULL(usb_rx)) + { + c = OVF; + } + + *(BUFFER_NEXT_FREE(usb_rx)) = c; + BUFFER_STORE(usb_rx); + } + +#else + mcu_usb_rx_cb((uint8_t)UsbSerial.read()); +#endif + } } void mcu_usb_init(void) @@ -57,18 +78,19 @@ extern "C" #ifndef USB_TX_BUFFER_SIZE #define USB_TX_BUFFER_SIZE 64 #endif - DECL_BUFFER(uint8_t, usb, USB_TX_BUFFER_SIZE); + DECL_BUFFER(uint8_t, usb_tx, USB_TX_BUFFER_SIZE); + void mcu_usb_flush(void) { - while (!BUFFER_EMPTY(usb)) + while (!BUFFER_EMPTY(usb_tx)) { uint8_t tmp[USB_TX_BUFFER_SIZE]; uint8_t r; - BUFFER_READ(usb, tmp, USB_TX_BUFFER_SIZE, r); + BUFFER_READ(usb_tx, tmp, USB_TX_BUFFER_SIZE, r); #ifdef MCU_HAS_USB - UsbSerial.write(tmp, r); + UsbSerial.write((char *)tmp, r); UsbSerial.flushTX(); #endif } @@ -76,28 +98,30 @@ extern "C" void mcu_usb_putc(uint8_t c) { - while (BUFFER_FULL(usb)) + while (BUFFER_FULL(usb_tx)) { mcu_usb_flush(); } - BUFFER_ENQUEUE(usb, &c); + BUFFER_ENQUEUE(usb_tx, &c); } uint8_t mcu_usb_getc(void) { - int16_t c = UsbSerial.read(); - return (uint8_t)((c >= 0) ? c : 0); + uint8_t c = 0; + BUFFER_DEQUEUE(usb_rx, &c); + return c; } uint8_t mcu_usb_available(void) { - return UsbSerial.available(); + return BUFFER_READ_AVAILABLE(usb_rx); } - uint8_t mcu_usb_tx_available(void) + void mcu_usb_clear(void) { - return (UsbSerial.availableForWrite() | (UsbSerial.host_connected ? 0 : 1)); + BUFFER_CLEAR(usb_rx); } + #endif } #endif diff --git a/uCNC/src/hal/mcus/lpc176x/mcu_lpc176x.c b/uCNC/src/hal/mcus/lpc176x/mcu_lpc176x.c index febf0394d..2ea85907f 100644 --- a/uCNC/src/hal/mcus/lpc176x/mcu_lpc176x.c +++ b/uCNC/src/hal/mcus/lpc176x/mcu_lpc176x.c @@ -26,9 +26,6 @@ #ifdef USE_ARDUINO_CDC extern void mcu_usb_dotasks(void); extern void mcu_usb_init(void); -extern uint8_t mcu_usb_getc(void); -extern uint8_t mcu_usb_available(void); -extern uint8_t mcu_usb_tx_available(void); #else #include #endif @@ -231,7 +228,9 @@ void mcu_clocks_init(void) #ifndef UART_TX_BUFFER_SIZE #define UART_TX_BUFFER_SIZE 64 #endif -DECL_BUFFER(uint8_t, uart, UART_TX_BUFFER_SIZE); +DECL_BUFFER(uint8_t, uart_tx, UART_TX_BUFFER_SIZE); +DECL_BUFFER(uint8_t, uart_rx, RX_BUFFER_SIZE); + void MCU_COM_ISR(void) { __ATOMIC_FORCEON__ @@ -258,7 +257,17 @@ void MCU_COM_ISR(void) { uint8_t c = (uint8_t)(COM_INREG & UART_RBR_MASKBIT); #if !defined(DETACH_UART_FROM_MAIN_PROTOCOL) - mcu_com_rx_cb(c); + if (mcu_com_rx_cb(c)) + { + if (BUFFER_FULL(uart_rx)) + { + c = OVF; + } + + *(BUFFER_NEXT_FREE(uart_rx)) = c; + BUFFER_STORE(uart_rx); + } + #else mcu_uart_rx_cb(c); #endif @@ -269,13 +278,13 @@ void MCU_COM_ISR(void) // UART_IntConfig(COM_USART, UART_INTCFG_THRE, DISABLE); mcu_enable_global_isr(); - if (BUFFER_EMPTY(uart)) + if (BUFFER_EMPTY(uart_tx)) { COM_UART->IER &= ~UART_IER_THREINT_EN; return; } uint8_t c = 0; - BUFFER_DEQUEUE(uart, &c); + BUFFER_DEQUEUE(uart_tx, &c); COM_OUTREG = c; } } @@ -286,7 +295,8 @@ void MCU_COM_ISR(void) #ifndef UART2_TX_BUFFER_SIZE #define UART2_TX_BUFFER_SIZE 64 #endif -DECL_BUFFER(uint8_t, uart2, UART2_TX_BUFFER_SIZE); +DECL_BUFFER(uint8_t, uart2_rx, RX_BUFFER_SIZE); +DECL_BUFFER(uint8_t, uart2_tx, UART2_TX_BUFFER_SIZE); void MCU_COM2_ISR(void) { __ATOMIC_FORCEON__ @@ -313,7 +323,16 @@ void MCU_COM2_ISR(void) { uint8_t c = (uint8_t)(COM2_INREG & UART_RBR_MASKBIT); #if !defined(DETACH_UART2_FROM_MAIN_PROTOCOL) - mcu_com_rx_cb(c); + if (mcu_com_rx_cb(c)) + { + if (BUFFER_FULL(uart2_rx)) + { + c = OVF; + } + + *(BUFFER_NEXT_FREE(uart2_rx)) = c; + BUFFER_STORE(uart2_rx); + } #else mcu_uart2_rx_cb(c); #endif @@ -322,13 +341,13 @@ void MCU_COM2_ISR(void) if (irqstatus == UART_IIR_INTID_THRE) { mcu_enable_global_isr(); - if (BUFFER_EMPTY(uart2)) + if (BUFFER_EMPTY(uart2_tx)) { COM2_UART->IER &= ~UART_IER_THREINT_EN; return; } uint8_t c; - BUFFER_DEQUEUE(uart2, &c); + BUFFER_DEQUEUE(uart2_tx, &c); COM2_OUTREG = c; } } @@ -573,25 +592,43 @@ uint8_t mcu_get_servo(uint8_t servo) * can be defined either as a function or a macro call * */ #ifdef MCU_HAS_UART + +uint8_t mcu_uart_getc(void) +{ + uint8_t c = 0; + BUFFER_DEQUEUE(uart_rx, &c); + return c; +} + +uint8_t mcu_uart_available(void) +{ + return BUFFER_READ_AVAILABLE(uart_rx); +} + +void mcu_uart_clear(void) +{ + BUFFER_CLEAR(uart_rx); +} + void mcu_uart_putc(uint8_t c) { - while (BUFFER_FULL(uart)) + while (BUFFER_FULL(uart_tx)) { mcu_uart_flush(); } - BUFFER_ENQUEUE(uart, &c); + BUFFER_ENQUEUE(uart_tx, &c); } void mcu_uart_flush(void) { if (!(COM_UART->IER & UART_IER_THREINT_EN)) // not ready start flushing { - if (BUFFER_EMPTY(uart)) + if (BUFFER_EMPTY(uart_tx)) { return; } uint8_t c = 0; - BUFFER_DEQUEUE(uart, &c); + BUFFER_DEQUEUE(uart_tx, &c); while (!CHECKBIT(COM_UART->LSR, 5)) ; COM_OUTREG = c; @@ -604,25 +641,42 @@ void mcu_uart_flush(void) #endif #ifdef MCU_HAS_UART2 +uint8_t mcu_uart2_getc(void) +{ + uint8_t c = 0; + BUFFER_DEQUEUE(uart2_rx, &c); + return c; +} + +uint8_t mcu_uart2_available(void) +{ + return BUFFER_READ_AVAILABLE(uart2_rx); +} + +void mcu_uart2_clear(void) +{ + BUFFER_CLEAR(uart2_rx); +} + void mcu_uart2_putc(uint8_t c) { - while (BUFFER_FULL(uart2)) + while (BUFFER_FULL(uart2_tx)) { mcu_uart2_flush(); } - BUFFER_ENQUEUE(uart2, &c); + BUFFER_ENQUEUE(uart2_tx, &c); } void mcu_uart2_flush(void) { if (!(COM2_UART->IER & UART_IER_THREINT_EN)) // not ready start flushing { - if (BUFFER_EMPTY(uart2)) + if (BUFFER_EMPTY(uart2_tx)) { return; } uint8_t c = 0; - BUFFER_DEQUEUE(uart2, &c); + BUFFER_DEQUEUE(uart2_tx, &c); while (!CHECKBIT(COM2_UART->LSR, 5)) ; COM2_OUTREG = c; @@ -809,15 +863,6 @@ void mcu_dotasks() #ifdef USE_ARDUINO_CDC mcu_usb_flush(); mcu_usb_dotasks(); - while (mcu_usb_available()) - { - uint8_t c = (uint8_t)mcu_usb_getc(); -#ifndef DETACH_USB_FROM_MAIN_PROTOCOL - mcu_com_rx_cb(c); -#else - mcu_usb_rx_cb(c); -#endif - } #else tusb_cdc_task(); // tinyusb device task