From 2fb3bbfababc4929475a2b37b9ed151bfc937cd3 Mon Sep 17 00:00:00 2001 From: Paciente8159 Date: Tue, 18 Oct 2022 22:28:04 +0100 Subject: [PATCH 1/8] USB and UART working on STM32F1 --- uCNC/src/cnc_hal_config_helper.h | 4 + uCNC/src/hal/boards/stm32/boardmap_bluepill.h | 17 +--- uCNC/src/hal/mcus/stm32f1x/mcu_stm32f1x.c | 87 +++++++++---------- uCNC/src/hal/mcus/stm32f1x/mcumap_stm32f1x.h | 55 ++++-------- 4 files changed, 62 insertions(+), 101 deletions(-) diff --git a/uCNC/src/cnc_hal_config_helper.h b/uCNC/src/cnc_hal_config_helper.h index 909e0d3c9..6da90b7f8 100644 --- a/uCNC/src/cnc_hal_config_helper.h +++ b/uCNC/src/cnc_hal_config_helper.h @@ -2130,6 +2130,10 @@ typedef uint16_t step_t; #error "Invalid config option STATUS_AUTOMATIC_REPORT_INTERVAL must be set between 0 and 1000" #endif +#if (defined(MCU_HAS_USB) || defined(MCU_HAS_WIFI) || defined(MCU_HAS_BLUETOOTH)) +#define ENABLE_SYNC_TX +#endif + #ifdef __cplusplus } #endif diff --git a/uCNC/src/hal/boards/stm32/boardmap_bluepill.h b/uCNC/src/hal/boards/stm32/boardmap_bluepill.h index f79a2697b..d0dfc1ec9 100644 --- a/uCNC/src/hal/boards/stm32/boardmap_bluepill.h +++ b/uCNC/src/hal/boards/stm32/boardmap_bluepill.h @@ -95,31 +95,16 @@ extern "C" #define CS_RES_ISR #define SAFETY_DOOR_ISR -#if (INTERFACE == INTERFACE_USART) #define UART_PORT 1 #define TX_BIT 9 #define TX_PORT A #define RX_BIT 10 #define RX_PORT A -#elif (INTERFACE == INTERFACE_USB) + #define USB_DM_BIT 11 #define USB_DM_PORT A #define USB_DP_BIT 12 #define USB_DP_PORT A -// #define STEP4_BIT 3 // assigns STEP4 pin -// #define STEP4_PORT A // assigns STEP4 port -// #define DIR4_BIT 7 // assigns DIR4 pin -// #define DIR4_PORT A // assigns DIR4 port -// #define LIMIT_B_BIT 11 // assigns LIMIT_A pin -// #define LIMIT_B_PORT B // assigns LIMIT_A port -// #define LIMIT_B_ISR - -#define DOUT27_BIT 9 -#define DOUT27_PORT A -#define DIN27_BIT 10 -#define DIN27_PORT A -#define DIN27_PULLUP -#endif // Setup PWM #define PWM0_BIT 8 // assigns PWM0 pin diff --git a/uCNC/src/hal/mcus/stm32f1x/mcu_stm32f1x.c b/uCNC/src/hal/mcus/stm32f1x/mcu_stm32f1x.c index 16a020d57..e4170d676 100644 --- a/uCNC/src/hal/mcus/stm32f1x/mcu_stm32f1x.c +++ b/uCNC/src/hal/mcus/stm32f1x/mcu_stm32f1x.c @@ -24,7 +24,7 @@ #include "mcumap_stm32f1x.h" #include -#if (INTERFACE == INTERFACE_USB) +#ifdef MCU_HAS_USB #include "../../../tinyusb/tusb_config.h" #include "../../../tinyusb/src/tusb.h" #endif @@ -63,7 +63,7 @@ volatile bool stm32_global_isr_enabled; * The isr functions * The respective IRQHandler will execute these functions **/ -#if (INTERFACE == INTERFACE_UART) +#ifdef MCU_HAS_UART void MCU_SERIAL_ISR(void) { mcu_disable_global_isr(); @@ -84,7 +84,9 @@ void MCU_SERIAL_ISR(void) #endif mcu_enable_global_isr(); } -#elif (INTERFACE == INTERFACE_USB) +#endif + +#ifdef MCU_HAS_USB void USB_HP_CAN1_TX_IRQHandler(void) { mcu_disable_global_isr(); @@ -105,7 +107,6 @@ void USBWakeUp_IRQHandler(void) tud_int_handler(0); mcu_enable_global_isr(); } - #endif // define the mcu internal servo variables @@ -403,9 +404,33 @@ void mcu_clocks_init() } } +#include +#include +#include void mcu_usart_init(void) { -#if (INTERFACE == INTERFACE_UART) +#ifdef MCU_HAS_USB + // configure USB as Virtual COM port + RCC->APB1ENR &= ~RCC_APB1ENR_USBEN; + mcu_config_input(USB_DM); + mcu_config_input(USB_DP); + NVIC_SetPriority(USB_HP_CAN1_TX_IRQn, 10); + NVIC_ClearPendingIRQ(USB_HP_CAN1_TX_IRQn); + NVIC_EnableIRQ(USB_HP_CAN1_TX_IRQn); + NVIC_SetPriority(USB_LP_CAN1_RX0_IRQn, 10); + NVIC_ClearPendingIRQ(USB_LP_CAN1_RX0_IRQn); + NVIC_EnableIRQ(USB_LP_CAN1_RX0_IRQn); + NVIC_SetPriority(USBWakeUp_IRQn, 10); + NVIC_ClearPendingIRQ(USBWakeUp_IRQn); + NVIC_EnableIRQ(USBWakeUp_IRQn); + + // Enable USB interrupts and enable usb + USB->CNTR |= (USB_CNTR_WKUPM | USB_CNTR_SOFM | USB_CNTR_ESOFM | USB_CNTR_CTRM); + RCC->APB1ENR |= RCC_APB1ENR_USBEN; + tusb_init(); +#endif + +#ifdef MCU_HAS_UART /*enables RCC clocks and GPIO*/ mcu_config_output_af(TX, GPIO_OUTALT_OD_50MHZ); mcu_config_input_af(RX); @@ -425,40 +450,17 @@ void mcu_usart_init(void) brr <<= 4; brr += (uint16_t)roundf(16.0f * baudrate); COM_USART->BRR = brr; -#ifndef ENABLE_SYNC_RX COM_USART->CR1 |= USART_CR1_RXNEIE; // enable RXNEIE -#endif -#if (!defined(ENABLE_SYNC_TX) || !defined(ENABLE_SYNC_RX)) NVIC_SetPriority(COM_IRQ, 3); NVIC_ClearPendingIRQ(COM_IRQ); NVIC_EnableIRQ(COM_IRQ); -#endif COM_USART->CR1 |= (USART_CR1_RE | USART_CR1_TE | USART_CR1_UE); // enable TE, RE and UART -#elif (INTERFACE == INTERFACE_USB) - // configure USB as Virtual COM port - RCC->APB1ENR &= ~RCC_APB1ENR_USBEN; - mcu_config_input(USB_DM); - mcu_config_input(USB_DP); - NVIC_SetPriority(USB_HP_CAN1_TX_IRQn, 10); - NVIC_ClearPendingIRQ(USB_HP_CAN1_TX_IRQn); - NVIC_EnableIRQ(USB_HP_CAN1_TX_IRQn); - NVIC_SetPriority(USB_LP_CAN1_RX0_IRQn, 10); - NVIC_ClearPendingIRQ(USB_LP_CAN1_RX0_IRQn); - NVIC_EnableIRQ(USB_LP_CAN1_RX0_IRQn); - NVIC_SetPriority(USBWakeUp_IRQn, 10); - NVIC_ClearPendingIRQ(USBWakeUp_IRQn); - NVIC_EnableIRQ(USBWakeUp_IRQn); - - // Enable USB interrupts and enable usb - USB->CNTR |= (USB_CNTR_WKUPM | USB_CNTR_SOFM | USB_CNTR_ESOFM | USB_CNTR_CTRM); - RCC->APB1ENR |= RCC_APB1ENR_USBEN; - tusb_init(); #endif } void mcu_putc(char c) { -#if (INTERFACE == INTERFACE_UART) +#ifdef MCU_HAS_UART #ifdef ENABLE_SYNC_TX while (!(COM_USART->SR & USART_SR_TC)) ; @@ -467,7 +469,8 @@ void mcu_putc(char c) #ifndef ENABLE_SYNC_TX COM_USART->CR1 |= (USART_CR1_TXEIE); #endif -#elif (INTERFACE == INTERFACE_USB) +#endif +#ifdef MCU_HAS_USB if (c != 0) { tud_cdc_write_char(c); @@ -580,19 +583,10 @@ uint8_t mcu_get_servo(uint8_t servo) char mcu_getc(void) { -#if (INTERFACE == INTERFACE_UART) -#ifdef ENABLE_SYNC_RX - while (!(COM_USART->SR & USART_SR_RXNE)) - ; -#endif +#ifdef MCU_HAS_UART return COM_INREG; -#elif (INTERFACE == INTERFACE_USB) - while (!tud_cdc_available()) - { - tud_task(); - } - - return (unsigned char)tud_cdc_read_char(); +#else + return 0; #endif } @@ -687,14 +681,13 @@ void mcu_rtc_init() void mcu_dotasks() { -#if (INTERFACE == INTERFACE_USB) +#ifdef MCU_HAS_USB tud_cdc_write_flush(); tud_task(); // tinyusb device task -#endif -#ifdef ENABLE_SYNC_RX - while (mcu_rx_ready()) + + while (tud_cdc_available()) { - unsigned char c = mcu_getc(); + unsigned char c = (unsigned char)tud_cdc_read_char(); mcu_com_rx_cb(c); } #endif diff --git a/uCNC/src/hal/mcus/stm32f1x/mcumap_stm32f1x.h b/uCNC/src/hal/mcus/stm32f1x/mcumap_stm32f1x.h index 09c1e20a0..14007e1a1 100644 --- a/uCNC/src/hal/mcus/stm32f1x/mcumap_stm32f1x.h +++ b/uCNC/src/hal/mcus/stm32f1x/mcumap_stm32f1x.h @@ -72,28 +72,11 @@ extern "C" ; \ } - // needed by software delays - // #ifndef MCU_CLOCKS_PER_CYCLE - // #define MCU_CLOCKS_PER_CYCLE 1 - // #endif - // #ifndef MCU_CYCLES_PER_LOOP - // #define MCU_CYCLES_PER_LOOP 7 - // #endif - // #ifndef MCU_CYCLES_PER_LOOP_OVERHEAD - // #define MCU_CYCLES_PER_LOOP_OVERHEAD 6 - // #endif - -#if (INTERFACE == INTERFACE_USB) -// if USB VCP is used force RX sync also -#define ENABLE_SYNC_TX -#define ENABLE_SYNC_RX -#endif - // APB1 cannot exceed 36MHz #if (F_CPU > 36000000UL) #define APB1_PRESC RCC_CFGR_PPRE1_DIV2 #define APB2_PRESC RCC_CFGR_PPRE2_DIV2 -#define PERIPH_CLOCK (F_CPU>>1) +#define PERIPH_CLOCK (F_CPU >> 1) #else #define APB1_PRESC RCC_CFGR_PPRE1_DIV1 #define APB2_PRESC RCC_CFGR_PPRE2_DIV2 @@ -2976,6 +2959,13 @@ extern "C" #define DIO209_CROFF I2C_SDA_CROFF #endif +#if (defined(TX) && defined(RX)) +#define MCU_HAS_UART +#endif +#if (defined(USB_DP) && defined(USB_DM)) +#define MCU_HAS_USB +#endif + /********************************************** * ISR on change inputs **********************************************/ @@ -4200,7 +4190,7 @@ extern "C" #endif // COM registers -#if (INTERFACE == INTERFACE_UART) +#ifdef MCU_HAS_UART #ifndef UART_PORT #define UART_PORT 1 #endif @@ -4210,9 +4200,7 @@ extern "C" #if (UART_PORT < 4) #define COM_USART __usart__(UART_PORT) #define COM_IRQ __helper__(USART, UART_PORT, _IRQn) -#if (!defined(ENABLE_SYNC_TX) || !defined(ENABLE_SYNC_RX)) #define MCU_SERIAL_ISR __helper__(USART, UART_PORT, _IRQHandler) -#endif #define COM_OUTREG (COM_USART)->DR #define COM_INREG (COM_USART)->DR #if (UART_PORT == 1) @@ -4225,9 +4213,7 @@ extern "C" #else #define COM_USART __uart__(UART_PORT) #define COM_IRQ __helper__(UART, UART_PORT, _IRQn) -#if (!defined(ENABLE_SYNC_TX) || !defined(ENABLE_SYNC_RX)) #define MCU_SERIAL_ISR __helper__(UART, UART_PORT, _IRQHandler) -#endif #define COM_APB APB1ENR #define COM_APBEN __helper__(RCC_APB1ENR_, COM_UART, EN) #define COM_OUTREG (COM_USART)->DR @@ -4621,7 +4607,7 @@ extern "C" #endif #endif -extern volatile bool stm32_global_isr_enabled; + extern volatile bool stm32_global_isr_enabled; #define mcu_enable_global_isr() \ { \ stm32_global_isr_enabled = true; \ @@ -4634,22 +4620,15 @@ extern volatile bool stm32_global_isr_enabled; } #define mcu_get_global_isr() stm32_global_isr_enabled -// #ifdef UART_PORT -// #ifndef ENABLE_SYNC_TX -// #define mcu_enable_tx_isr() (COM_USART->CR1 |= (USART_CR1_TXEIE)) -// #define mcu_disable_tx_isr() (COM_USART->CR1 &= ~(USART_CR1_TXEIE)) -// #else -// #define mcu_enable_tx_isr() -// #define mcu_disable_tx_isr() -// #endif -// #else -// #define mcu_enable_tx_isr() -// #define mcu_disable_tx_isr() -// #endif -#if (INTERFACE == INTERFACE_UART) +#if (defined(MCU_HAS_UART) && defined(MCU_HAS_USB)) + extern uint32_t tud_cdc_n_write_available(uint8_t itf); + extern uint32_t tud_cdc_n_available(uint8_t itf); +#define mcu_rx_ready() ((COM_USART->SR & USART_SR_RXNE) || tud_cdc_n_available(0)) +#define mcu_tx_ready() (COM_USART->SR & USART_SR_TXE) +#elif defined(MCU_HAS_UART) #define mcu_rx_ready() (COM_USART->SR & USART_SR_RXNE) #define mcu_tx_ready() (COM_USART->SR & USART_SR_TXE) -#elif (INTERFACE == INTERFACE_USB) +#elif defined(MCU_HAS_USB) extern uint32_t tud_cdc_n_write_available(uint8_t itf); extern uint32_t tud_cdc_n_available(uint8_t itf); #define mcu_rx_ready() tud_cdc_n_available(0) From 3ace85f17f98c2302a5d92a1abb691060cbd28f7 Mon Sep 17 00:00:00 2001 From: Paciente8159 Date: Tue, 18 Oct 2022 23:05:43 +0100 Subject: [PATCH 2/8] working on STM32F4 --- .../src/hal/boards/stm32/boardmap_blackpill.h | 11 +- uCNC/src/hal/boards/stm32/stm32.ini | 6 +- uCNC/src/hal/mcus/stm32f1x/mcu_stm32f1x.c | 24 ++-- uCNC/src/hal/mcus/stm32f1x/mcumap_stm32f1x.h | 20 +-- uCNC/src/hal/mcus/stm32f4x/mcu_stm32f4x.c | 122 +++++++----------- uCNC/src/hal/mcus/stm32f4x/mcumap_stm32f4x.h | 69 ++++------ 6 files changed, 99 insertions(+), 153 deletions(-) diff --git a/uCNC/src/hal/boards/stm32/boardmap_blackpill.h b/uCNC/src/hal/boards/stm32/boardmap_blackpill.h index 701586261..5f9875c8a 100644 --- a/uCNC/src/hal/boards/stm32/boardmap_blackpill.h +++ b/uCNC/src/hal/boards/stm32/boardmap_blackpill.h @@ -95,26 +95,17 @@ extern "C" #define CS_RES_ISR #define SAFETY_DOOR_ISR -#if (INTERFACE == INTERFACE_USART) #define UART_PORT 1 #define TX_BIT 9 #define TX_PORT A #define RX_BIT 10 #define RX_PORT A -#elif (INTERFACE == INTERFACE_USB) + // PIN A10 is also used because of the USB ID (USB OTG) #define USB_DM_BIT 11 #define USB_DM_PORT A #define USB_DP_BIT 12 #define USB_DP_PORT A -#define STEP4_BIT 3 // assigns STEP4 pin -#define STEP4_PORT A // assigns STEP4 port -#define DIR4_BIT 7 // assigns DIR4 pin -#define DIR4_PORT A // assigns DIR4 port -#define LIMIT_B_BIT 11 // assigns LIMIT_A pin -#define LIMIT_B_PORT B // assigns LIMIT_A port -#define LIMIT_B_ISR -#endif // Setup PWM #define PWM0_BIT 8 // assigns PWM0 pin diff --git a/uCNC/src/hal/boards/stm32/stm32.ini b/uCNC/src/hal/boards/stm32/stm32.ini index b841dca10..e14f2c8fa 100644 --- a/uCNC/src/hal/boards/stm32/stm32.ini +++ b/uCNC/src/hal/boards/stm32/stm32.ini @@ -23,7 +23,7 @@ debug_server = -f $PLATFORMIO_SRC_DIR/scripts/board/stm32f103c8_blue_pill.cfg board = bluepill_f103c8 -build_flags = ${common_stm32.build_flags} -D BOARD=BOARD_BLUEPILL -D INTERFACE=1 +build_flags = ${common_stm32.build_flags} -D BOARD=BOARD_BLUEPILL [env:bluepill_f103c8_clone] extends = common_stm32 @@ -34,7 +34,7 @@ debug_server = -f $PLATFORMIO_SRC_DIR/scripts/board/stm32f103c8_blue_pill.cfg board = bluepill_f103c8 -build_flags = ${common_stm32.build_flags} -D BOARD=BOARD_BLUEPILL -D INTERFACE=1 +build_flags = ${common_stm32.build_flags} -D BOARD=BOARD_BLUEPILL ; uncomment if it's a bluepill clone upload_flags = -c set CPUTAPID 0x2ba01477 @@ -49,7 +49,7 @@ board = genericSTM32F103VE board_build.offset = 0x7000 board_upload.offset_address = 0x08007000 board_build.f_cpu = 72000000L -build_flags = ${common_stm32.build_flags} -D BOARD=BOARD_MKS_ROBIN_NANO_V1_2 -D INTERFACE=0 +build_flags = ${common_stm32.build_flags} -D BOARD=BOARD_MKS_ROBIN_NANO_V1_2 [env:skr_pro_v1_2] extends = common_stm32 diff --git a/uCNC/src/hal/mcus/stm32f1x/mcu_stm32f1x.c b/uCNC/src/hal/mcus/stm32f1x/mcu_stm32f1x.c index e4170d676..2beeb03ac 100644 --- a/uCNC/src/hal/mcus/stm32f1x/mcu_stm32f1x.c +++ b/uCNC/src/hal/mcus/stm32f1x/mcu_stm32f1x.c @@ -68,7 +68,7 @@ void MCU_SERIAL_ISR(void) { mcu_disable_global_isr(); #ifndef ENABLE_SYNC_RX - if (COM_USART->SR & USART_SR_RXNE) + if (COM_UART->SR & USART_SR_RXNE) { unsigned char c = COM_INREG; mcu_com_rx_cb(c); @@ -76,9 +76,9 @@ void MCU_SERIAL_ISR(void) #endif #ifndef ENABLE_SYNC_TX - if ((COM_USART->SR & USART_SR_TXE) && (COM_USART->CR1 & USART_CR1_TXEIE)) + if ((COM_UART->SR & USART_SR_TXE) && (COM_UART->CR1 & USART_CR1_TXEIE)) { - COM_USART->CR1 &= ~(USART_CR1_TXEIE); + COM_UART->CR1 &= ~(USART_CR1_TXEIE); mcu_com_tx_cb(); } #endif @@ -439,22 +439,22 @@ void mcu_usart_init(void) #endif RCC->COM_APB |= (COM_APBEN); /*setup UART*/ - COM_USART->CR1 = 0; // 8 bits No parity M=0 PCE=0 - COM_USART->CR2 = 0; // 1 stop bit STOP=00 - COM_USART->CR3 = 0; - COM_USART->SR = 0; + COM_UART->CR1 = 0; // 8 bits No parity M=0 PCE=0 + COM_UART->CR2 = 0; // 1 stop bit STOP=00 + COM_UART->CR3 = 0; + COM_UART->SR = 0; // //115200 baudrate float baudrate = ((float)(PERIPH_CLOCK >> 4) / ((float)BAUDRATE)); uint16_t brr = (uint16_t)baudrate; baudrate -= brr; brr <<= 4; brr += (uint16_t)roundf(16.0f * baudrate); - COM_USART->BRR = brr; - COM_USART->CR1 |= USART_CR1_RXNEIE; // enable RXNEIE + COM_UART->BRR = brr; + COM_UART->CR1 |= USART_CR1_RXNEIE; // enable RXNEIE NVIC_SetPriority(COM_IRQ, 3); NVIC_ClearPendingIRQ(COM_IRQ); NVIC_EnableIRQ(COM_IRQ); - COM_USART->CR1 |= (USART_CR1_RE | USART_CR1_TE | USART_CR1_UE); // enable TE, RE and UART + COM_UART->CR1 |= (USART_CR1_RE | USART_CR1_TE | USART_CR1_UE); // enable TE, RE and UART #endif } @@ -462,12 +462,12 @@ void mcu_putc(char c) { #ifdef MCU_HAS_UART #ifdef ENABLE_SYNC_TX - while (!(COM_USART->SR & USART_SR_TC)) + while (!(COM_UART->SR & USART_SR_TC)) ; #endif COM_OUTREG = c; #ifndef ENABLE_SYNC_TX - COM_USART->CR1 |= (USART_CR1_TXEIE); + COM_UART->CR1 |= (USART_CR1_TXEIE); #endif #endif #ifdef MCU_HAS_USB diff --git a/uCNC/src/hal/mcus/stm32f1x/mcumap_stm32f1x.h b/uCNC/src/hal/mcus/stm32f1x/mcumap_stm32f1x.h index 14007e1a1..1e6e2bdc1 100644 --- a/uCNC/src/hal/mcus/stm32f1x/mcumap_stm32f1x.h +++ b/uCNC/src/hal/mcus/stm32f1x/mcumap_stm32f1x.h @@ -4198,11 +4198,11 @@ extern "C" // this forces the sync TX method to fix communication // #define ENABLE_SYNC_TX #if (UART_PORT < 4) -#define COM_USART __usart__(UART_PORT) +#define COM_UART __usart__(UART_PORT) #define COM_IRQ __helper__(USART, UART_PORT, _IRQn) #define MCU_SERIAL_ISR __helper__(USART, UART_PORT, _IRQHandler) -#define COM_OUTREG (COM_USART)->DR -#define COM_INREG (COM_USART)->DR +#define COM_OUTREG (COM_UART)->DR +#define COM_INREG (COM_UART)->DR #if (UART_PORT == 1) #define COM_APB APB2ENR #define COM_APBEN __helper__(RCC_APB2ENR_USART, UART_PORT, EN) @@ -4211,13 +4211,13 @@ extern "C" #define COM_APBEN __helper__(RCC_APB1ENR_USART, UART_PORT, EN) #endif #else -#define COM_USART __uart__(UART_PORT) +#define COM_UART __uart__(UART_PORT) #define COM_IRQ __helper__(UART, UART_PORT, _IRQn) #define MCU_SERIAL_ISR __helper__(UART, UART_PORT, _IRQHandler) #define COM_APB APB1ENR #define COM_APBEN __helper__(RCC_APB1ENR_, COM_UART, EN) -#define COM_OUTREG (COM_USART)->DR -#define COM_INREG (COM_USART)->DR +#define COM_OUTREG (COM_UART)->DR +#define COM_INREG (COM_UART)->DR #endif #define UART_TX_PIN __iopin__(TX_PORT, TX_BIT) @@ -4623,11 +4623,11 @@ extern "C" #if (defined(MCU_HAS_UART) && defined(MCU_HAS_USB)) extern uint32_t tud_cdc_n_write_available(uint8_t itf); extern uint32_t tud_cdc_n_available(uint8_t itf); -#define mcu_rx_ready() ((COM_USART->SR & USART_SR_RXNE) || tud_cdc_n_available(0)) -#define mcu_tx_ready() (COM_USART->SR & USART_SR_TXE) +#define mcu_rx_ready() ((COM_UART->SR & USART_SR_RXNE) || tud_cdc_n_available(0)) +#define mcu_tx_ready() (COM_UART->SR & USART_SR_TXE) #elif defined(MCU_HAS_UART) -#define mcu_rx_ready() (COM_USART->SR & USART_SR_RXNE) -#define mcu_tx_ready() (COM_USART->SR & USART_SR_TXE) +#define mcu_rx_ready() (COM_UART->SR & USART_SR_RXNE) +#define mcu_tx_ready() (COM_UART->SR & USART_SR_TXE) #elif defined(MCU_HAS_USB) extern uint32_t tud_cdc_n_write_available(uint8_t itf); extern uint32_t tud_cdc_n_available(uint8_t itf); diff --git a/uCNC/src/hal/mcus/stm32f4x/mcu_stm32f4x.c b/uCNC/src/hal/mcus/stm32f4x/mcu_stm32f4x.c index cd5a50d5b..6e340f7e1 100644 --- a/uCNC/src/hal/mcus/stm32f4x/mcu_stm32f4x.c +++ b/uCNC/src/hal/mcus/stm32f4x/mcu_stm32f4x.c @@ -25,7 +25,7 @@ #include "mcumap_stm32f4x.h" #include -#if (INTERFACE == INTERFACE_USB) +#ifdef MCU_HAS_USB #include "../../../tinyusb/tusb_config.h" #include "../../../tinyusb/src/tusb.h" #endif @@ -64,12 +64,12 @@ volatile bool stm32_global_isr_enabled; * The isr functions * The respective IRQHandler will execute these functions **/ -#if (INTERFACE == INTERFACE_UART) +#ifdef MCU_HAS_UART void MCU_SERIAL_ISR(void) { mcu_disable_global_isr(); #ifndef ENABLE_SYNC_RX - if (COM_USART->SR & USART_SR_RXNE) + if (COM_UART->SR & USART_SR_RXNE) { unsigned char c = COM_INREG; mcu_com_rx_cb(c); @@ -77,15 +77,17 @@ void MCU_SERIAL_ISR(void) #endif #ifndef ENABLE_SYNC_TX - if ((COM_USART->SR & USART_SR_TXE) && (COM_USART->CR1 & USART_CR1_TXEIE)) + if ((COM_UART->SR & USART_SR_TXE) && (COM_UART->CR1 & USART_CR1_TXEIE)) { - COM_USART->CR1 &= ~(USART_CR1_TXEIE); + COM_UART->CR1 &= ~(USART_CR1_TXEIE); mcu_com_tx_cb(); } #endif mcu_enable_global_isr(); } -#elif (INTERFACE == INTERFACE_USB) +#endif + +#ifdef MCU_HAS_USB void OTG_FS_IRQHandler(void) { mcu_disable_global_isr(); @@ -411,64 +413,21 @@ void mcu_clocks_init() void mcu_usart_init(void) { -#if (INTERFACE == INTERFACE_UART) - /*enables RCC clocks and GPIO*/ - RCC->COM_APB |= (COM_APBEN); - mcu_config_af(TX, GPIO_AF_USART); - mcu_config_af(RX, GPIO_AF_USART); - /*setup UART*/ - COM_USART->CR1 = 0; // 8 bits No parity M=0 PCE=0 - COM_USART->CR2 = 0; // 1 stop bit STOP=00 - COM_USART->CR3 = 0; - COM_USART->SR = 0; - // //115200 baudrate - float baudrate = ((float)(PERIPH_CLOCK >> 4) / ((float)(BAUDRATE))); - uint16_t brr = (uint16_t)baudrate; - baudrate -= brr; - brr <<= 4; - brr += (uint16_t)roundf(16.0f * baudrate); - COM_USART->BRR = brr; -#ifndef ENABLE_SYNC_RX - COM_USART->CR1 |= USART_CR1_RXNEIE; // enable RXNEIE -#endif -#if (!defined(ENABLE_SYNC_TX) || !defined(ENABLE_SYNC_RX)) - NVIC_SetPriority(COM_IRQ, 3); - NVIC_ClearPendingIRQ(COM_IRQ); - NVIC_EnableIRQ(COM_IRQ); -#endif - COM_USART->CR1 |= (USART_CR1_RE | USART_CR1_TE | USART_CR1_UE); // enable TE, RE and UART -#elif (INTERFACE == INTERFACE_USB) +#ifdef MCU_HAS_USB // configure USB as Virtual COM port mcu_config_input(USB_DM); mcu_config_input(USB_DP); mcu_config_af(USB_DP, GPIO_OTG_FS); mcu_config_af(USB_DM, GPIO_OTG_FS); RCC->AHB2ENR |= RCC_AHB2ENR_OTGFSEN; - - // configure ID pin - GPIOA->MODER &= ~(GPIO_RESET << (10 << 1)); /*USB ID*/ - GPIOA->MODER |= (GPIO_AF << (10 << 1)); /*af mode*/ - GPIOA->PUPDR &= ~(GPIO_RESET << (10 << 1)); // pullup - GPIOA->PUPDR |= (GPIO_IN_PULLUP << (10 << 1)); - GPIOA->OTYPER |= (1 << 10); // open drain - GPIOA->OSPEEDR &= ~(GPIO_RESET << (10 << 1)); - GPIOA->OSPEEDR |= (0x02 << (10 << 1)); // high-speed - GPIOA->AFR[1] |= (GPIO_AFRH_AFSEL10_1 | GPIO_AFRH_AFSEL10_3); // GPIO_OTG_FS - - // GPIOA->MODER &= ~(GPIO_RESET << (9 << 1)); /*USB VBUS*/ - /* Disable all interrupts. */ USB_OTG_FS->GINTMSK = 0U; + /* Operate as device only mode */ + USB_OTG_FS->GUSBCFG |= USB_OTG_GUSBCFG_FDMOD; + // /* Clear any pending interrupts */ USB_OTG_FS->GINTSTS = 0xBFFFFFFFU; - // USB_OTG_FS->GINTMSK |= USB_OTG_GINTMSK_RXFLVLM; - /* Enable interrupts matching to the Device mode ONLY */ - // USB_OTG_FS->GINTMSK |= USB_OTG_GINTMSK_USBSUSPM | USB_OTG_GINTMSK_USBRST | - // USB_OTG_GINTMSK_ENUMDNEM | USB_OTG_GINTMSK_IEPINT | - // USB_OTG_GINTMSK_OEPINT | USB_OTG_GINTMSK_IISOIXFRM | - // USB_OTG_GINTMSK_PXFRM_IISOOXFRM | USB_OTG_GINTMSK_WUIM; - USB_OTG_FS->GINTMSK |= USB_OTG_GINTMSK_OTGINT; NVIC_SetPriority(OTG_FS_IRQn, 10); @@ -481,20 +440,45 @@ void mcu_usart_init(void) tusb_init(); #endif + +#ifdef MCU_HAS_UART + /*enables RCC clocks and GPIO*/ + RCC->COM_APB |= (COM_APBEN); + mcu_config_af(TX, GPIO_AF_USART); + mcu_config_af(RX, GPIO_AF_USART); + /*setup UART*/ + COM_UART->CR1 = 0; // 8 bits No parity M=0 PCE=0 + COM_UART->CR2 = 0; // 1 stop bit STOP=00 + COM_UART->CR3 = 0; + COM_UART->SR = 0; + // //115200 baudrate + float baudrate = ((float)(PERIPH_CLOCK >> 4) / ((float)(BAUDRATE))); + uint16_t brr = (uint16_t)baudrate; + baudrate -= brr; + brr <<= 4; + brr += (uint16_t)roundf(16.0f * baudrate); + COM_UART->BRR = brr; + COM_UART->CR1 |= USART_CR1_RXNEIE; // enable RXNEIE + NVIC_SetPriority(COM_IRQ, 3); + NVIC_ClearPendingIRQ(COM_IRQ); + NVIC_EnableIRQ(COM_IRQ); + COM_UART->CR1 |= (USART_CR1_RE | USART_CR1_TE | USART_CR1_UE); // enable TE, RE and UART +#endif } void mcu_putc(char c) { -#if (INTERFACE == INTERFACE_UART) +#ifdef MCU_HAS_UART #ifdef ENABLE_SYNC_TX - while (!(COM_USART->SR & USART_SR_TC)) + while (!(COM_UART->SR & USART_SR_TC)) ; #endif COM_OUTREG = c; #ifndef ENABLE_SYNC_TX - COM_USART->CR1 |= (USART_CR1_TXEIE); + COM_UART->CR1 |= (USART_CR1_TXEIE); +#endif #endif -#elif (INTERFACE == INTERFACE_USB) +#ifdef MCU_HAS_USB if (c != 0) { tud_cdc_write_char(c); @@ -645,19 +629,10 @@ uint8_t mcu_get_pwm(uint8_t pwm) char mcu_getc(void) { -#if (INTERFACE == INTERFACE_UART) -#ifdef ENABLE_SYNC_RX - while (!(COM_USART->SR & USART_SR_RXNE)) - ; -#endif +#ifdef MCU_HAS_UART return COM_INREG; -#elif (INTERFACE == INTERFACE_USB) - while (!tud_cdc_available()) - { - tud_task(); - } - - return (unsigned char)tud_cdc_read_char(); +#else + return 0; #endif } @@ -743,14 +718,13 @@ void mcu_rtc_init() void mcu_dotasks() { -#if (INTERFACE == INTERFACE_USB) +#ifdef MCU_HAS_USB tud_cdc_write_flush(); tud_task(); // tinyusb device task -#endif -#ifdef ENABLE_SYNC_RX - while (mcu_rx_ready()) + + while (tud_cdc_available()) { - unsigned char c = mcu_getc(); + unsigned char c = (unsigned char)tud_cdc_read_char(); mcu_com_rx_cb(c); } #endif diff --git a/uCNC/src/hal/mcus/stm32f4x/mcumap_stm32f4x.h b/uCNC/src/hal/mcus/stm32f4x/mcumap_stm32f4x.h index 05ff0609a..feb4a6aba 100644 --- a/uCNC/src/hal/mcus/stm32f4x/mcumap_stm32f4x.h +++ b/uCNC/src/hal/mcus/stm32f4x/mcumap_stm32f4x.h @@ -74,22 +74,6 @@ extern "C" ; \ } -// #ifndef MCU_CLOCKS_PER_CYCLE -// #define MCU_CLOCKS_PER_CYCLE 1 -// #endif -// #ifndef MCU_CYCLES_PER_LOOP -// #define MCU_CYCLES_PER_LOOP 5 -// #endif -// #ifndef MCU_CYCLES_PER_LOOP_OVERHEAD -// #define MCU_CYCLES_PER_LOOP_OVERHEAD 13 -// #endif - -#if (INTERFACE == INTERFACE_USB) -// if USB VCP is used force RX sync also -#define ENABLE_SYNC_TX -#define ENABLE_SYNC_RX -#endif - // APB1 cannot exceed 36MHz #if (F_CPU > 90000000UL) #define APB1_PRESC RCC_CFGR_PPRE1_DIV4 @@ -1630,6 +1614,13 @@ extern "C" #define DIO209_GPIO I2C_SDA_GPIO #endif +#if (defined(TX) && defined(RX)) +#define MCU_HAS_UART +#endif +#if (defined(USB_DP) && defined(USB_DM)) +#define MCU_HAS_USB +#endif + /********************************************** * ISR on change inputs **********************************************/ @@ -2990,18 +2981,16 @@ extern "C" #endif // COM registers -#if (INTERFACE == INTERFACE_UART) +#ifdef MCU_HAS_UART // this MCU does not work well with both TX and RX interrupt // this forces the sync TX method to fix communication // #define ENABLE_SYNC_TX #if (UART_PORT < 4 || UART_PORT == 6) -#define COM_USART __usart__(UART_PORT) +#define COM_UART __usart__(UART_PORT) #define COM_IRQ __helper__(USART, UART_PORT, _IRQn) -#if (!defined(ENABLE_SYNC_TX) || !defined(ENABLE_SYNC_RX)) #define MCU_SERIAL_ISR __helper__(USART, UART_PORT, _IRQHandler) -#endif -#define COM_OUTREG (COM_USART)->DR -#define COM_INREG (COM_USART)->DR +#define COM_OUTREG (COM_UART)->DR +#define COM_INREG (COM_UART)->DR #if (UART_PORT == 1 || UART_PORT == 6) #define COM_APB APB2ENR #define COM_APBEN __helper__(RCC_APB2ENR_USART, UART_PORT, EN) @@ -3010,15 +2999,13 @@ extern "C" #define COM_APBEN __helper__(RCC_APB1ENR_USART, UART_PORT, EN) #endif #else -#define COM_USART __uart__(UART_PORT) +#define COM_UART __uart__(UART_PORT) #define COM_IRQ __helper__(UART, UART_PORT, _IRQn) -#if (!defined(ENABLE_SYNC_TX) || !defined(ENABLE_SYNC_RX)) #define MCU_SERIAL_ISR __helper__(UART, UART_PORT, _IRQHandler) -#endif #define COM_APB APB1ENR #define COM_APBEN __helper__(RCC_APB1ENR_, COM_UART, EN) -#define COM_OUTREG (COM_USART)->DR -#define COM_INREG (COM_USART)->DR +#define COM_OUTREG (COM_UART)->DR +#define COM_INREG (COM_UART)->DR #endif // remmaping and pin checking @@ -3037,7 +3024,8 @@ extern "C" #else #define GPIO_AF_USART 0x08 #endif -#else +#endif +#ifdef MCU_HAS_USB #define GPIO_OTG_FS 0x0A #endif @@ -3377,22 +3365,15 @@ extern "C" } #define mcu_get_global_isr() stm32_global_isr_enabled -// #ifdef UART_PORT -// #ifndef ENABLE_SYNC_TX -// #define mcu_enable_tx_isr() (COM_USART->CR1 |= (USART_CR1_TXEIE)) -// #define mcu_disable_tx_isr() (COM_USART->CR1 &= ~(USART_CR1_TXEIE)) -// #else -// #define mcu_enable_tx_isr() -// #define mcu_disable_tx_isr() -// #endif -// #else -// #define mcu_enable_tx_isr() -// #define mcu_disable_tx_isr() -// #endif -#if (INTERFACE == INTERFACE_UART) -#define mcu_rx_ready() (COM_USART->SR & USART_SR_RXNE) -#define mcu_tx_ready() (COM_USART->SR & USART_SR_TXE) -#elif (INTERFACE == INTERFACE_USB) +#if (defined(MCU_HAS_UART) && defined(MCU_HAS_USB)) + extern uint32_t tud_cdc_n_write_available(uint8_t itf); + extern uint32_t tud_cdc_n_available(uint8_t itf); +#define mcu_rx_ready() ((COM_UART->SR & USART_SR_RXNE) || tud_cdc_n_available(0)) +#define mcu_tx_ready() (COM_UART->SR & USART_SR_TXE) +#elif defined(MCU_HAS_UART) +#define mcu_rx_ready() (COM_UART->SR & USART_SR_RXNE) +#define mcu_tx_ready() (COM_UART->SR & USART_SR_TXE) +#elif defined(MCU_HAS_USB) extern uint32_t tud_cdc_n_write_available(uint8_t itf); extern uint32_t tud_cdc_n_available(uint8_t itf); #define mcu_rx_ready() tud_cdc_n_available(0) From 29fe13d51048eb5400dd29f92ad2a80fa680204b Mon Sep 17 00:00:00 2001 From: Paciente8159 Date: Tue, 18 Oct 2022 23:21:02 +0100 Subject: [PATCH 3/8] working on LPC --- uCNC/src/hal/boards/lpc176x/boardmap_re_arm.h | 5 +- uCNC/src/hal/mcus/lpc176x/mcu_lpc176x.c | 101 +++++++----------- uCNC/src/hal/mcus/lpc176x/mcumap_lpc176x.h | 30 ++++-- 3 files changed, 59 insertions(+), 77 deletions(-) diff --git a/uCNC/src/hal/boards/lpc176x/boardmap_re_arm.h b/uCNC/src/hal/boards/lpc176x/boardmap_re_arm.h index cd595a1b7..e2cfa077c 100644 --- a/uCNC/src/hal/boards/lpc176x/boardmap_re_arm.h +++ b/uCNC/src/hal/boards/lpc176x/boardmap_re_arm.h @@ -80,20 +80,17 @@ extern "C" // #define PROBE_ISR -3 // Setup com pins -#if (INTERFACE == INTERFACE_UART) #define RX_BIT 3 #define TX_BIT 2 #define RX_PORT 0 #define TX_PORT 0 // only uncomment this if other port other then 0 is used //#define UART_PORT 0 -#elif (INTERFACE == INTERFACE_USB) -// PIN A10 is also used because of the USB ID (USB OTG) + #define USB_DM_BIT 30 #define USB_DM_PORT 0 #define USB_DP_BIT 29 #define USB_DP_PORT 0 -#endif // // Setup PWM #define PWM0_BIT 5 // assigns PWM0 pin diff --git a/uCNC/src/hal/mcus/lpc176x/mcu_lpc176x.c b/uCNC/src/hal/mcus/lpc176x/mcu_lpc176x.c index ed64e9a33..b2d75fe8f 100644 --- a/uCNC/src/hal/mcus/lpc176x/mcu_lpc176x.c +++ b/uCNC/src/hal/mcus/lpc176x/mcu_lpc176x.c @@ -22,7 +22,7 @@ #if (MCU == MCU_LPC176X) #include "system_LPC17xx.h" -#if (INTERFACE == INTERFACE_USB) +#ifdef MCU_HAS_USB #include "../../../tinyusb/tusb_config.h" #include "../../../tinyusb/src/tusb.h" #endif @@ -220,17 +220,17 @@ void mcu_clocks_init(void) * The isr functions * The respective IRQHandler will execute these functions // **/ -#if (INTERFACE == INTERFACE_USART) +#ifdef MCU_HAS_UART void MCU_COM_ISR(void) { mcu_disable_global_isr(); - uint32_t irqstatus = UART_GetIntId(COM_USART); + uint32_t irqstatus = UART_GetIntId(COM_UART); irqstatus &= UART_IIR_INTID_MASK; // Receive Line Status if (irqstatus == UART_IIR_INTID_RLS) { - uint32_t linestatus = UART_GetLineStatus(COM_USART); + uint32_t linestatus = UART_GetLineStatus(COM_UART); // Receive Line Status if (linestatus & (UART_LSR_OE | UART_LSR_PE | UART_LSR_FE | UART_LSR_RXFE | UART_LSR_BI)) @@ -242,25 +242,16 @@ void MCU_COM_ISR(void) } } -#ifndef ENABLE_SYNC_RX if (irqstatus == UART_IIR_INTID_RDA) { unsigned char c = (unsigned char)(COM_INREG & UART_RBR_MASKBIT); mcu_com_rx_cb(c); } -#endif -#ifndef ENABLE_SYNC_TX - if (irqstatus == UART_IIR_INTID_THRE) - { - // UART_IntConfig(COM_USART, UART_INTCFG_THRE, DISABLE); - COM_USART->IER &= ~UART_IER_THREINT_EN; - mcu_com_tx_cb(); - } -#endif mcu_enable_global_isr(); } -#elif (INTERFACE == INTERFACE_USB) +#endif +#ifdef MCU_HAS_USB void USB_IRQHandler(void) { mcu_disable_global_isr(); @@ -271,33 +262,33 @@ void USB_IRQHandler(void) void mcu_usart_init(void) { -#if (INTERFACE == INTERFACE_UART) +#ifdef MCU_HAS_UART /*mcu_config_af(TX, UART_ALT_FUNC); mcu_config_af(RX, UART_ALT_FUNC); LPC_SC->PCONP |= UART_PCONP; LPC_SC->UART_PCLKSEL_REG &= ~UART_PCLKSEL_MASK; // div clock by 4 - COM_USART->FCR = UART_FCR_FIFO_EN | UART_FCR_RX_RS | UART_FCR_TX_RS; // Enable FIFO and reset Rx/Tx FIFO buffers - COM_USART->IER = 0; - COM_USART->ACR = 0; - COM_USART->LCR = 0; - COM_USART->TER = 0; - // COM_USART->FCR = 0; + COM_UART->FCR = UART_FCR_FIFO_EN | UART_FCR_RX_RS | UART_FCR_TX_RS; // Enable FIFO and reset Rx/Tx FIFO buffers + COM_UART->IER = 0; + COM_UART->ACR = 0; + COM_UART->LCR = 0; + COM_UART->TER = 0; + // COM_UART->FCR = 0; - COM_USART->LCR = UART_LCR_WLEN8 | UART_LCR_DLAB_EN; + COM_UART->LCR = UART_LCR_WLEN8 | UART_LCR_DLAB_EN; uint32_t uartspeed = ((F_CPU >> 2) / (16 * BAUDRATE)); - COM_USART->DLL = uartspeed & 0xFF; - COM_USART->DLM = (uartspeed >> 0x08) & 0xFF; - while ((COM_USART->LCR & UART_LCR_DLAB_EN)) + COM_UART->DLL = uartspeed & 0xFF; + COM_UART->DLM = (uartspeed >> 0x08) & 0xFF; + while ((COM_UART->LCR & UART_LCR_DLAB_EN)) ; - COM_USART->IER |= UART_IER_RLSINT_EN; + COM_UART->IER |= UART_IER_RLSINT_EN; #ifndef ENABLE_SYNC_RX - COM_USART->IER |= UART_IER_RBRINT_EN; + COM_UART->IER |= UART_IER_RBRINT_EN; #endif - COM_USART->TER |= UART_TER_TXEN; + COM_UART->TER |= UART_TER_TXEN; */ PINSEL_CFG_Type tx = {TX_PORT, TX_BIT, UART_ALT_FUNC, PINSEL_PINMODE_PULLUP, PINSEL_PINMODE_NORMAL}; @@ -308,24 +299,21 @@ void mcu_usart_init(void) CLKPWR_SetPCLKDiv(COM_PCLK, CLKPWR_PCLKSEL_CCLK_DIV_4); UART_CFG_Type conf = {BAUDRATE, UART_PARITY_NONE, UART_DATABIT_8, UART_STOPBIT_1}; - UART_Init(COM_USART, &conf); + UART_Init(COM_UART, &conf); // Enable UART Transmit - UART_TxCmd(COM_USART, ENABLE); + UART_TxCmd(COM_UART, ENABLE); // Configure Interrupts - UART_IntConfig(COM_USART, UART_INTCFG_RLS, ENABLE); -#ifndef ENABLE_SYNC_RX - UART_IntConfig(COM_USART, UART_INTCFG_RBR, ENABLE); -#endif + UART_IntConfig(COM_UART, UART_INTCFG_RLS, ENABLE); + UART_IntConfig(COM_UART, UART_INTCFG_RBR, ENABLE); -#if (!defined(ENABLE_SYNC_TX) || !defined(ENABLE_SYNC_RX)) NVIC_SetPriority(COM_IRQ, 3); NVIC_ClearPendingIRQ(COM_IRQ); NVIC_EnableIRQ(COM_IRQ); #endif -#elif (INTERFACE == INTERFACE_USB) +#ifdef MCU_HAS_USB // // configure USB as Virtual COM port LPC_PINCON->PINSEL1 &= ~((3 << 26) | (3 << 28)); /* P0.29 D+, P0.30 D- */ LPC_PINCON->PINSEL1 |= ((1 << 26) | (1 << 28)); /* PINSEL1 26.27, 28.29 = 01 */ @@ -525,17 +513,17 @@ bool mcu_rx_ready(void) #ifndef mcu_putc void mcu_putc(char c) { -#if (INTERFACE == INTERFACE_UART) +#ifdef MCU_HAS_UART #ifdef ENABLE_SYNC_TX while (!mcu_tx_ready()) ; #endif COM_OUTREG = c; #ifndef ENABLE_SYNC_TX - // UART_IntConfig(COM_USART, UART_INTCFG_THRE, ENABLE); - COM_USART->IER |= UART_IER_THREINT_EN; + COM_UART->IER |= UART_IER_THREINT_EN; #endif -#elif (INTERFACE == INTERFACE_USB) +#endif +#ifdef MCU_HAS_USB if (c != 0) { tud_cdc_write_char(c); @@ -555,19 +543,10 @@ void mcu_putc(char c) #ifndef mcu_getc char mcu_getc(void) { -#if (INTERFACE == INTERFACE_UART) -#ifdef ENABLE_SYNC_RX - while (!mcu_rx_ready()) - ; -#endif - return (COM_INREG & UART_RBR_MASKBIT); -#elif (INTERFACE == INTERFACE_USB) - while (!tud_cdc_available()) - { - tud_task(); - } - - return (unsigned char)tud_cdc_read_char(); +#ifdef MCU_HAS_UART + return (COM_INREG & UART_RBR_MASKBIT); +#else + return 0; #endif } #endif @@ -707,19 +686,15 @@ void mcu_delay_us(uint16_t delay) * - if ENABLE_SYNC_RX is enabled check if there are any chars in the rx transmitter (or the tinyUSB buffer) and read them to the mcu_com_rx_cb * - if ENABLE_SYNC_TX is enabled check if com_tx_empty is false and run mcu_com_tx_cb * */ -void mcu_dotasks(void) +void mcu_dotasks() { -#if (INTERFACE == INTERFACE_USB) +#ifdef MCU_HAS_USB tud_cdc_write_flush(); tud_task(); // tinyusb device task -#endif -#if (defined(ENABLE_SYNC_TX) || defined(ENABLE_SYNC_RX)) - // lpc176x_uart_flush(); -#endif -#ifdef ENABLE_SYNC_RX - while (mcu_rx_ready()) + + while (tud_cdc_available()) { - unsigned char c = mcu_getc(); + unsigned char c = (unsigned char)tud_cdc_read_char(); mcu_com_rx_cb(c); } #endif diff --git a/uCNC/src/hal/mcus/lpc176x/mcumap_lpc176x.h b/uCNC/src/hal/mcus/lpc176x/mcumap_lpc176x.h index 7223534cc..d46f254df 100644 --- a/uCNC/src/hal/mcus/lpc176x/mcumap_lpc176x.h +++ b/uCNC/src/hal/mcus/lpc176x/mcumap_lpc176x.h @@ -2846,6 +2846,13 @@ typedef struct #define DIO209_PINCON I2C_SDA_PINCON #endif +#if (defined(TX) && defined(RX)) +#define MCU_HAS_UART +#endif +#if (defined(USB_DP) && defined(USB_DM)) +#define MCU_HAS_USB +#endif + /********************************************** * ISR on change inputs **********************************************/ @@ -3563,7 +3570,7 @@ typedef struct #endif // COM registers -#if (INTERFACE == INTERFACE_UART) +#ifdef MCU_HAS_UART #ifndef UART_PORT #define UART_PORT 0 #endif @@ -3591,15 +3598,13 @@ typedef struct // this MCU does not work well with both TX and RX interrupt // this forces the sync TX method to fix communication -#define COM_USART __helper__(LPC_UART, UART_PORT, ) +#define COM_UART __helper__(LPC_UART, UART_PORT, ) #define COM_IRQ __helper__(UART, UART_PORT, _IRQn) #define COM_PCLK __helper__(CLKPWR_PCLKSEL_UART, UART_PORT, ) -#if (!defined(ENABLE_SYNC_TX) || !defined(ENABLE_SYNC_RX)) #define MCU_COM_ISR __helper__(UART, UART_PORT, _IRQHandler) -#endif -#define COM_OUTREG (COM_USART)->THR -#define COM_INREG (COM_USART)->RBR +#define COM_OUTREG (COM_UART)->THR +#define COM_INREG (COM_UART)->RBR #endif @@ -3823,10 +3828,15 @@ typedef struct } #define mcu_get_global_isr() lpc_global_isr_enabled -#if (INTERFACE == INTERFACE_UART) -#define mcu_rx_ready() (CHECKBIT(COM_USART->LSR, 0)) -#define mcu_tx_ready() (CHECKBIT(COM_USART->LSR, 5)) -#elif (INTERFACE == INTERFACE_USB) +#if (defined(MCU_HAS_UART) && defined(MCU_HAS_USB)) + extern uint32_t tud_cdc_n_write_available(uint8_t itf); + extern uint32_t tud_cdc_n_available(uint8_t itf); +#define mcu_rx_ready() (CHECKBIT(COM_UART->LSR, 0) || tud_cdc_n_available(0)) +#define mcu_tx_ready() (CHECKBIT(COM_UART->LSR, 5)) +#elif defined(MCU_HAS_UART) +#define mcu_rx_ready() (CHECKBIT(COM_UART->LSR, 0)) +#define mcu_tx_ready() (CHECKBIT(COM_UART->LSR, 5)) +#elif defined(MCU_HAS_USB) extern uint32_t tud_cdc_n_write_available(uint8_t itf); extern uint32_t tud_cdc_n_available(uint8_t itf); #define mcu_rx_ready() tud_cdc_n_available(0) From 1bdf4d16bcd389f7e51ba6c5ce75cb7b88a64a41 Mon Sep 17 00:00:00 2001 From: Paciente8159 Date: Tue, 18 Oct 2022 23:46:36 +0100 Subject: [PATCH 4/8] several boards changed --- uCNC/cnc_config.h | 2 +- .../hal/boards/lpc176x/boardmap_mks_base13.h | 16 ++++++------ .../boards/lpc176x/boardmap_skr_v14_turbo.h | 17 ++++++------- uCNC/src/hal/boards/lpc176x/lpc176x.ini | 6 ++--- uCNC/src/hal/boards/samd21/samd21.ini | 4 +-- .../stm32/boardmap_mks_robin_nano_v1_2.h | 4 --- .../hal/boards/stm32/boardmap_srk_pro_v1_2.h | 15 ++++++----- uCNC/src/hal/boards/stm32/stm32.ini | 4 +-- uCNC/src/hal/mcus/stm32f1x/mcu_stm32f1x.c | 25 ++++++++----------- 9 files changed, 39 insertions(+), 54 deletions(-) diff --git a/uCNC/cnc_config.h b/uCNC/cnc_config.h index 0ffff6e82..cf903c34b 100644 --- a/uCNC/cnc_config.h +++ b/uCNC/cnc_config.h @@ -368,7 +368,7 @@ extern "C" * helps to reduce code size if features are not needed * */ #ifndef DISABLE_ALL_CONTROLS -// #define DISABLE_ALL_CONTROLS +#define DISABLE_ALL_CONTROLS #endif #ifndef DISABLE_ALL_LIMITS // #define DISABLE_ALL_LIMITS diff --git a/uCNC/src/hal/boards/lpc176x/boardmap_mks_base13.h b/uCNC/src/hal/boards/lpc176x/boardmap_mks_base13.h index 37d2e5515..09a80abb9 100644 --- a/uCNC/src/hal/boards/lpc176x/boardmap_mks_base13.h +++ b/uCNC/src/hal/boards/lpc176x/boardmap_mks_base13.h @@ -92,20 +92,18 @@ extern "C" // #define PROBE_ISR -3 // Setup com pins -#if (INTERFACE == INTERFACE_UART) -#define RX_BIT 3 -#define TX_BIT 2 -#define RX_PORT 0 -#define TX_PORT 0 -// only uncomment this if other port other then 0 is used -//#define UART_PORT 0 -#elif (INTERFACE == INTERFACE_USB) +// #define RX_BIT 3 +// #define TX_BIT 2 +// #define RX_PORT 0 +// #define TX_PORT 0 +// // only uncomment this if other port other then 0 is used +// //#define UART_PORT 0 + // PIN A10 is also used because of the USB ID (USB OTG) #define USB_DM_BIT 30 #define USB_DM_PORT 0 #define USB_DP_BIT 29 #define USB_DP_PORT 0 -#endif // // Setup PWM #define PWM0_BIT 5 // assigns PWM0 pin diff --git a/uCNC/src/hal/boards/lpc176x/boardmap_skr_v14_turbo.h b/uCNC/src/hal/boards/lpc176x/boardmap_skr_v14_turbo.h index 775182abc..a6006868a 100644 --- a/uCNC/src/hal/boards/lpc176x/boardmap_skr_v14_turbo.h +++ b/uCNC/src/hal/boards/lpc176x/boardmap_skr_v14_turbo.h @@ -92,20 +92,17 @@ extern "C" // #define PROBE_ISR -3 // Setup com pins -#if (INTERFACE == INTERFACE_UART) -#define RX_BIT 3 -#define TX_BIT 2 -#define RX_PORT 0 -#define TX_PORT 0 -// only uncomment this if other port other then 0 is used -//#define UART_PORT 0 -#elif (INTERFACE == INTERFACE_USB) -// PIN A10 is also used because of the USB ID (USB OTG) +// #define RX_BIT 3 +// #define TX_BIT 2 +// #define RX_PORT 0 +// #define TX_PORT 0 +// // only uncomment this if other port other then 0 is used +// //#define UART_PORT 0 + #define USB_DM_BIT 30 #define USB_DM_PORT 0 #define USB_DP_BIT 29 #define USB_DP_PORT 0 -#endif // // Setup PWM #define PWM0_BIT 5 // assigns PWM0 pin diff --git a/uCNC/src/hal/boards/lpc176x/lpc176x.ini b/uCNC/src/hal/boards/lpc176x/lpc176x.ini index 5d8efd91f..e1404558a 100644 --- a/uCNC/src/hal/boards/lpc176x/lpc176x.ini +++ b/uCNC/src/hal/boards/lpc176x/lpc176x.ini @@ -12,14 +12,14 @@ build_flags = ${common.build_flags} -D NXP_LPC17xx [env:re_arm] extends = common_lpc176x board = nxp_lpc1768 -build_flags = ${common_lpc176x.build_flags} -D BOARD=BOARD_RE_ARM -D INTERFACE=1 +build_flags = ${common_lpc176x.build_flags} -D BOARD=BOARD_RE_ARM [env:mks_base_v13] extends = common_lpc176x board = nxp_lpc1768 -build_flags = ${common_lpc176x.build_flags} -D BOARD=BOARD_MKS_BASE13 -D INTERFACE=1 +build_flags = ${common_lpc176x.build_flags} -D BOARD=BOARD_MKS_BASE13 [env:skr_v14_turbo] extends = common_lpc176x board = nxp_lpc1768 -build_flags = ${common_lpc176x.build_flags} -D BOARD=BOARD_SKR_V14_TURBO -D INTERFACE=1 +build_flags = ${common_lpc176x.build_flags} -D BOARD=BOARD_SKR_V14_TURBO diff --git a/uCNC/src/hal/boards/samd21/samd21.ini b/uCNC/src/hal/boards/samd21/samd21.ini index 43988eed3..bcf29c945 100644 --- a/uCNC/src/hal/boards/samd21/samd21.ini +++ b/uCNC/src/hal/boards/samd21/samd21.ini @@ -20,8 +20,8 @@ build_flags = ${common.build_flags} -nostdlib -fno-exceptions [env:arduinoM0] extends = common_samd21 -build_flags = ${common_samd21.build_flags} -D BOARD=BOARD_MZERO -D INTERFACE=1 +build_flags = ${common_samd21.build_flags} -D BOARD=BOARD_MZERO [env:arduinoZero] extends = common_samd21 -build_flags = ${common_samd21.build_flags} -D BOARD=BOARD_ZERO -D INTERFACE=1 +build_flags = ${common_samd21.build_flags} -D BOARD=BOARD_ZERO diff --git a/uCNC/src/hal/boards/stm32/boardmap_mks_robin_nano_v1_2.h b/uCNC/src/hal/boards/stm32/boardmap_mks_robin_nano_v1_2.h index 1ff7e023f..3d1bbc2f4 100644 --- a/uCNC/src/hal/boards/stm32/boardmap_mks_robin_nano_v1_2.h +++ b/uCNC/src/hal/boards/stm32/boardmap_mks_robin_nano_v1_2.h @@ -109,15 +109,11 @@ extern "C" #define PROBE_PORT A #define PROBE_ISR -#if (INTERFACE == INTERFACE_USART) #define UART_PORT 3 #define TX_BIT 10 #define TX_PORT B #define RX_BIT 11 #define RX_PORT B -#else -#error "This board uses an external USB<->Serial converter" -#endif // Setup PWM #define PWM0_BIT 0 // assigns PWM0 pin diff --git a/uCNC/src/hal/boards/stm32/boardmap_srk_pro_v1_2.h b/uCNC/src/hal/boards/stm32/boardmap_srk_pro_v1_2.h index c91df2f1c..896396e7a 100644 --- a/uCNC/src/hal/boards/stm32/boardmap_srk_pro_v1_2.h +++ b/uCNC/src/hal/boards/stm32/boardmap_srk_pro_v1_2.h @@ -116,18 +116,17 @@ extern "C" #define PROBE_PORT G #define PROBE_ISR -#if (INTERFACE == INTERFACE_USART) -#define UART_PORT 3 -#define TX_BIT 10 -#define TX_PORT B -#define RX_BIT 11 -#define RX_PORT B -#elif (INTERFACE == INTERFACE_USB) +// #define UART_PORT 3 +// #define TX_BIT 10 +// #define TX_PORT B +// #define RX_BIT 11 +// #define RX_PORT B + #define USB_DM_BIT 11 #define USB_DM_PORT A #define USB_DP_BIT 12 #define USB_DP_PORT A -#endif + // Setup PWM #define PWM0_BIT 1 // assigns PWM0 pin diff --git a/uCNC/src/hal/boards/stm32/stm32.ini b/uCNC/src/hal/boards/stm32/stm32.ini index e14f2c8fa..f8353375e 100644 --- a/uCNC/src/hal/boards/stm32/stm32.ini +++ b/uCNC/src/hal/boards/stm32/stm32.ini @@ -41,7 +41,7 @@ upload_flags = -c set CPUTAPID 0x2ba01477 [env:blackpill_f401cc] extends = common_stm32 board = blackpill_f401cc -build_flags = ${common_stm32.build_flags} -D BOARD=BOARD_BLACKPILL -D INTERFACE=1 +build_flags = ${common_stm32.build_flags} -D BOARD=BOARD_BLACKPILL [env:mks_robin_nano_v1_2] extends = common_stm32 @@ -57,4 +57,4 @@ board = black_f407zg board_build.offset = 0x8000 board_upload.offset_address = 0x08008000 board_build.f_cpu = 168000000L -build_flags = ${common_stm32.build_flags} -D BOARD=BOARD_SKR_PRO_V1_2 -D INTERFACE=1 +build_flags = ${common_stm32.build_flags} -D BOARD=BOARD_SKR_PRO_V1_2 diff --git a/uCNC/src/hal/mcus/stm32f1x/mcu_stm32f1x.c b/uCNC/src/hal/mcus/stm32f1x/mcu_stm32f1x.c index 2beeb03ac..3aa89ccb0 100644 --- a/uCNC/src/hal/mcus/stm32f1x/mcu_stm32f1x.c +++ b/uCNC/src/hal/mcus/stm32f1x/mcu_stm32f1x.c @@ -461,14 +461,9 @@ void mcu_usart_init(void) void mcu_putc(char c) { #ifdef MCU_HAS_UART -#ifdef ENABLE_SYNC_TX - while (!(COM_UART->SR & USART_SR_TC)) + while (!mcu_tx_ready()) ; -#endif COM_OUTREG = c; -#ifndef ENABLE_SYNC_TX - COM_UART->CR1 |= (USART_CR1_TXEIE); -#endif #endif #ifdef MCU_HAS_USB if (c != 0) @@ -482,6 +477,15 @@ void mcu_putc(char c) #endif } +char mcu_getc(void) +{ +#ifdef MCU_HAS_UART + return COM_INREG; +#else + return 0; +#endif +} + void mcu_init(void) { mcu_clocks_init(); @@ -581,15 +585,6 @@ uint8_t mcu_get_servo(uint8_t servo) return 0; } -char mcu_getc(void) -{ -#ifdef MCU_HAS_UART - return COM_INREG; -#else - return 0; -#endif -} - // ISR // enables all interrupts on the mcu. Must be called to enable all IRS functions #ifndef mcu_enable_global_isr From 732fb19bfdeb8ef6aebb97b687f333cee7e9cbb7 Mon Sep 17 00:00:00 2001 From: Paciente8159 Date: Wed, 19 Oct 2022 00:07:07 +0100 Subject: [PATCH 5/8] working for SAMD21 --- uCNC/cnc_config.h | 2 +- uCNC/src/hal/boards/samd21/boardmap_mzero.h | 4 +- uCNC/src/hal/mcus/lpc176x/mcu_lpc176x.c | 20 +++- uCNC/src/hal/mcus/samd21/mcu_samd21.c | 118 ++++++++------------ uCNC/src/hal/mcus/samd21/mcumap_samd21.h | 46 ++++---- uCNC/src/hal/mcus/stm32f1x/mcu_stm32f1x.c | 4 + 6 files changed, 92 insertions(+), 102 deletions(-) diff --git a/uCNC/cnc_config.h b/uCNC/cnc_config.h index cf903c34b..0ffff6e82 100644 --- a/uCNC/cnc_config.h +++ b/uCNC/cnc_config.h @@ -368,7 +368,7 @@ extern "C" * helps to reduce code size if features are not needed * */ #ifndef DISABLE_ALL_CONTROLS -#define DISABLE_ALL_CONTROLS +// #define DISABLE_ALL_CONTROLS #endif #ifndef DISABLE_ALL_LIMITS // #define DISABLE_ALL_LIMITS diff --git a/uCNC/src/hal/boards/samd21/boardmap_mzero.h b/uCNC/src/hal/boards/samd21/boardmap_mzero.h index 8d199a29b..c82d6a292 100644 --- a/uCNC/src/hal/boards/samd21/boardmap_mzero.h +++ b/uCNC/src/hal/boards/samd21/boardmap_mzero.h @@ -84,21 +84,19 @@ extern "C" // #define CS_RES_ISR // On the STM32 always use sync TX UART (async doesn't work well) -#if (INTERFACE == INTERFACE_USART) #define TX_BIT 10 #define TX_PORT A #define RX_BIT 11 #define RX_PORT A // set COM number. By default SERCOM2 if not set. Arduino IDE already uses SERCOM0 and SERCOM1 // #define UART_PORT 2 -#elif (INTERFACE == INTERFACE_USB) + #define USB_DM_BIT 24 #define USB_DM_PORT A #define USB_DM_MUX G #define USB_DP_BIT 25 #define USB_DP_PORT A #define USB_DP_MUX G -#endif // Setup PWM #define PWM0_BIT 16 // assigns PWM0 pin diff --git a/uCNC/src/hal/mcus/lpc176x/mcu_lpc176x.c b/uCNC/src/hal/mcus/lpc176x/mcu_lpc176x.c index b2d75fe8f..def953183 100644 --- a/uCNC/src/hal/mcus/lpc176x/mcu_lpc176x.c +++ b/uCNC/src/hal/mcus/lpc176x/mcu_lpc176x.c @@ -131,9 +131,9 @@ void servo_timer_init(void) SERVO_TIMER_REG->PR = ((F_CPU >> 2) / 127500) - 1; // for 1us SERVO_TIMER_REG->IR = 0xFFFFFFFF; - SERVO_TIMER_REG->MR1 = SERVO_MIN; // minimum value for servo setup - SERVO_TIMER_REG->MR0 = 425; // reset @ every 3.333ms * 6 servos = 20ms->50Hz - SERVO_TIMER_REG->MCR = 0x0B; // Interrupt on MC0 and MC1 and reset on MC0 + SERVO_TIMER_REG->MR1 = SERVO_MIN; // minimum value for servo setup + SERVO_TIMER_REG->MR0 = 425; // reset @ every 3.333ms * 6 servos = 20ms->50Hz + SERVO_TIMER_REG->MCR = 0x0B; // Interrupt on MC0 and MC1 and reset on MC0 NVIC_SetPriority(SERVO_TIMER_IRQ, 10); NVIC_ClearPendingIRQ(SERVO_TIMER_IRQ); @@ -248,6 +248,15 @@ void MCU_COM_ISR(void) mcu_com_rx_cb(c); } +#ifndef ENABLE_SYNC_TX + if (irqstatus == UART_IIR_INTID_THRE) + { + // UART_IntConfig(COM_USART, UART_INTCFG_THRE, DISABLE); + COM_UART->IER &= ~UART_IER_THREINT_EN; + mcu_com_tx_cb(); + } +#endif + mcu_enable_global_isr(); } #endif @@ -544,7 +553,7 @@ void mcu_putc(char c) char mcu_getc(void) { #ifdef MCU_HAS_UART - return (COM_INREG & UART_RBR_MASKBIT); + return (COM_INREG & UART_RBR_MASKBIT); #else return 0; #endif @@ -841,7 +850,8 @@ uint8_t mcu_i2c_read(bool with_ack, bool send_stop) void MCU_ONESHOT_ISR(void) { - if(mcu_timeout_cb){ + if (mcu_timeout_cb) + { mcu_timeout_cb(); } diff --git a/uCNC/src/hal/mcus/samd21/mcu_samd21.c b/uCNC/src/hal/mcus/samd21/mcu_samd21.c index 7a9a98569..f9df521f4 100644 --- a/uCNC/src/hal/mcus/samd21/mcu_samd21.c +++ b/uCNC/src/hal/mcus/samd21/mcu_samd21.c @@ -38,7 +38,7 @@ #define NVM_EEPROM_BASE (FLASH_ADDR + NVMCTRL_FLASH_SIZE - (NVM_EEPROM_ROWS * NVMCTRL_ROW_SIZE)) #define NVM_MEMORY ((volatile uint16_t *)FLASH_ADDR) -#if (INTERFACE == INTERFACE_USB) +#ifdef MCU_HAS_USB #include "../../../tinyusb/tusb_config.h" #include "../../../tinyusb/src/tusb.h" #endif @@ -207,22 +207,20 @@ void MCU_ITP_ISR(void) mcu_enable_global_isr(); } -#if (INTERFACE == INTERFACE_UART) +#ifdef MCU_HAS_UART void mcu_com_isr() { mcu_disable_global_isr(); -#ifndef ENABLE_SYNC_RX - if (COM->USART.INTFLAG.bit.RXC && COM->USART.INTENSET.bit.RXC) + if (COM_UART->USART.INTFLAG.bit.RXC && COM_UART->USART.INTENSET.bit.RXC) { - COM->USART.INTFLAG.bit.RXC = 1; + COM_UART->USART.INTFLAG.bit.RXC = 1; unsigned char c = (0xff & COM_INREG); mcu_com_rx_cb(c); } -#endif #ifndef ENABLE_SYNC_TX - if (COM->USART.INTFLAG.bit.DRE && COM->USART.INTENSET.bit.DRE) + if (COM_UART->USART.INTFLAG.bit.DRE && COM_UART->USART.INTENSET.bit.DRE) { - COM->USART.INTENCLR.reg = SERCOM_USART_INTENCLR_DRE; + COM_UART->USART.INTENCLR.reg = SERCOM_USART_INTENCLR_DRE; mcu_com_tx_cb(); } #endif @@ -232,7 +230,7 @@ void mcu_com_isr() void mcu_usart_init(void) { -#if (INTERFACE == INTERFACE_UART) +#ifdef MCU_HAS_UART PM->APBCMASK.reg |= PM_APBCMASK_COM; /* Setup GCLK SERCOMx to use GENCLK0 */ @@ -241,49 +239,44 @@ void mcu_usart_init(void) ; // Start the Software Reset - COM->USART.CTRLA.bit.SWRST = 1; + COM_UART->USART.CTRLA.bit.SWRST = 1; - while (COM->USART.SYNCBUSY.bit.SWRST) + while (COM_UART->USART.SYNCBUSY.bit.SWRST) ; - COM->USART.CTRLA.bit.MODE = 1; - COM->USART.CTRLA.bit.SAMPR = 0; // 16x sample rate - COM->USART.CTRLA.bit.FORM = 0; // no parity - COM->USART.CTRLA.bit.DORD = 1; // LSB first - COM->USART.CTRLA.bit.RXPO = COM_RX_PAD; // RX on PAD3 - COM->USART.CTRLA.bit.TXPO = COM_TX_PAD; // TX on PAD2 - COM->USART.CTRLB.bit.SBMODE = 0; // one stop bit - COM->USART.CTRLB.bit.CHSIZE = 0; // 8 bits - COM->USART.CTRLB.bit.RXEN = 1; // enable receiver - COM->USART.CTRLB.bit.TXEN = 1; // enable transmitter + COM_UART->USART.CTRLA.bit.MODE = 1; + COM_UART->USART.CTRLA.bit.SAMPR = 0; // 16x sample rate + COM_UART->USART.CTRLA.bit.FORM = 0; // no parity + COM_UART->USART.CTRLA.bit.DORD = 1; // LSB first + COM_UART->USART.CTRLA.bit.RXPO = COM_RX_PAD; // RX on PAD3 + COM_UART->USART.CTRLA.bit.TXPO = COM_TX_PAD; // TX on PAD2 + COM_UART->USART.CTRLB.bit.SBMODE = 0; // one stop bit + COM_UART->USART.CTRLB.bit.CHSIZE = 0; // 8 bits + COM_UART->USART.CTRLB.bit.RXEN = 1; // enable receiver + COM_UART->USART.CTRLB.bit.TXEN = 1; // enable transmitter - while (COM->USART.SYNCBUSY.bit.CTRLB) + while (COM_UART->USART.SYNCBUSY.bit.CTRLB) ; uint16_t baud = (uint16_t)(65536.0f * (1.0f - (((float)BAUDRATE) / (F_CPU >> 4)))); - COM->USART.BAUD.reg = baud; + COM_UART->USART.BAUD.reg = baud; mcu_config_altfunc(TX); mcu_config_altfunc(RX); + COM_UART->USART.INTENSET.bit.RXC = 1; // enable recieved interrupt + COM_UART->USART.INTENSET.bit.ERROR = 1; -#ifndef ENABLE_SYNC_RX - COM->USART.INTENSET.bit.RXC = 1; // enable recieved interrupt - COM->USART.INTENSET.bit.ERROR = 1; -#endif -#ifndef ENABLE_SYNC_TX - COM->USART.INTENCLR.reg = SERCOM_USART_INTENCLR_DRE; -#endif NVIC_ClearPendingIRQ(COM_IRQ); NVIC_EnableIRQ(COM_IRQ); NVIC_SetPriority(COM_IRQ, 0); - // enable COM - COM->USART.CTRLA.bit.ENABLE = 1; - while (COM->USART.SYNCBUSY.bit.ENABLE) + // enable COM_UART + COM_UART->USART.CTRLA.bit.ENABLE = 1; + while (COM_UART->USART.SYNCBUSY.bit.ENABLE) ; #endif -#if (INTERFACE == INTERFACE_USB) +#ifdef MCU_HAS_USB PM->AHBMASK.reg |= PM_AHBMASK_USB; mcu_config_input(USB_DM); @@ -310,7 +303,7 @@ void mcu_usart_init(void) #endif } -#if (INTERFACE == INTERFACE_USB) +#ifdef MCU_HAS_USB void USB_Handler(void) { mcu_disable_global_isr(); @@ -703,58 +696,46 @@ bool mcu_rx_ready(void) #endif /** - * sends a char either via uart (hardware, software or USB virtual COM port) + * sends a char either via uart (hardware, software or USB virtual COM_UART port) * can be defined either as a function or a macro call * */ #ifndef mcu_putc void mcu_putc(char c) { -#if (INTERFACE == INTERFACE_USB) - if (c != 0) - { - tud_cdc_write_char(c); - } - if (c == '\r' || c == 0) - { - tud_cdc_write_flush(); - } -#else -#if (INTERFACE == INTERFACE_UART) +#ifdef MCU_HAS_UART #ifdef ENABLE_SYNC_TX while (!mcu_tx_ready()) ; #endif COM_OUTREG = c; #ifndef ENABLE_SYNC_TX - COM->USART.INTENSET.bit.DRE = 1; // enable recieved interrupt + COM_UART->USART.INTENSET.bit.DRE = 1; // enable recieved interrupt #endif #endif +#ifdef MCU_HAS_USB + if (c != 0) + { + tud_cdc_write_char(c); + } + if (c == '\r' || c == 0) + { + tud_cdc_write_flush(); + } #endif } #endif /** - * gets a char either via uart (hardware, software or USB virtual COM port) + * gets a char either via uart (hardware, software or USB virtual COM_UART port) * can be defined either as a function or a macro call * */ #ifndef mcu_getc char mcu_getc(void) { -#if (INTERFACE == INTERFACE_USB) - while (!tud_cdc_available()) - { - tud_task(); - } - - return (unsigned char)tud_cdc_read_char(); -#else -#if (INTERFACE == INTERFACE_UART) -#ifdef ENABLE_SYNC_RX - while (!mcu_rx_ready()) - ; -#endif +#ifdef MCU_HAS_UART return (char)(0xff & COM_INREG); -#endif +#else + return 0; #endif } #endif @@ -955,14 +936,13 @@ void mcu_delay_us(uint16_t delay) * */ void mcu_dotasks(void) { -#if (INTERFACE == INTERFACE_USB) +#ifdef MCU_HAS_USB tud_cdc_write_flush(); tud_task(); // tinyusb device task -#endif -#ifdef ENABLE_SYNC_RX - while (mcu_rx_ready()) + + while (tud_cdc_available()) { - unsigned char c = mcu_getc(); + unsigned char c = (unsigned char)tud_cdc_read_char(); mcu_com_rx_cb(c); } #endif @@ -1266,7 +1246,7 @@ void mcu_config_timeout(mcu_timeout_delgate fp, uint32_t timeout) { mcu_timeout_cb = fp; uint16_t ticks = (uint16_t)(timeout - 1); - uint16_t prescaller = 3; //div by 8 giving one tick per us + uint16_t prescaller = 3; // div by 8 giving one tick per us #if (ONESHOT_TIMER < 3) // reset timer diff --git a/uCNC/src/hal/mcus/samd21/mcumap_samd21.h b/uCNC/src/hal/mcus/samd21/mcumap_samd21.h index 67308961c..b4c038a96 100644 --- a/uCNC/src/hal/mcus/samd21/mcumap_samd21.h +++ b/uCNC/src/hal/mcus/samd21/mcumap_samd21.h @@ -71,12 +71,6 @@ extern "C" #define rom_memcpy memcpy #define rom_read_byte * -#if (INTERFACE == INTERFACE_USB) -// if USB VCP is used force RX sync also -#define ENABLE_SYNC_TX -#define ENABLE_SYNC_RX -#endif - // Helper macros #define __helper_ex__(left, mid, right) left##mid##right #define __helper__(left, mid, right) __helper_ex__(left, mid, right) @@ -1296,6 +1290,13 @@ extern "C" #define DIO209_GPIO I2C_SDA_GPIO #endif +#if (defined(TX) && defined(RX)) +#define MCU_HAS_UART +#endif +#if (defined(USB_DP) && defined(USB_DM)) +#define MCU_HAS_USB +#endif + #define __pinmuxevenodd0 PMUXE #define __pinmuxevenodd2 PMUXE #define __pinmuxevenodd4 PMUXE @@ -1494,44 +1495,37 @@ extern "C" #define _sercommux_pin(X, Y, Z) (SERCOMMUX_##X##_##Y##Z) #define sercommux_pin(X, Y, Z) (_sercommux_pin(X, Y, Z)) -#if (INTERFACE == INTERFACE_USB) -#ifdef USB_DM +#ifdef MCU_HAS_USB #define USB_DM_PMUX (pinmux(USB_DM_PORT, USB_DM_BIT)) #define USB_DM_PMUXVAL (pinmuxval(USB_DM_MUX)) #define DIO202_PMUX USB_DM_PMUX #define DIO202_PMUXVAL USB_DM_PMUXVAL -#endif -#ifdef USB_DP #define USB_DP_PMUX (pinmux(USB_DP_PORT, USB_DP_BIT)) #define USB_DP_PMUXVAL (pinmuxval(USB_DP_MUX)) #define DIO203_PMUX USB_DP_PMUX #define DIO203_PMUXVAL USB_DP_PMUXVAL #endif -#else + +#ifdef MCU_HAS_UART // Arduino already uses SERCOM0 and SERCOM 1 #ifndef UART_PORT #define UART_PORT 2 #endif - -#ifdef TX #define TX_PMUX (pinmux(TX_PORT, TX_BIT)) #define TX_PMUXVAL (sercommux_pin(UART_PORT, TX_PORT, TX_BIT)) #define DIO200_PMUX TX_PMUX #define DIO200_PMUXVAL TX_PMUXVAL -#endif -#ifdef RX #define RX_PMUX (pinmux(RX_PORT, RX_BIT)) #define RX_PMUXVAL (sercommux_pin(UART_PORT, RX_PORT, RX_BIT)) #define DIO201_PMUX RX_PMUX #define DIO201_PMUXVAL RX_PMUXVAL -#endif -#define COM __helper__(SERCOM, UART_PORT, ) +#define COM_UART __helper__(SERCOM, UART_PORT, ) #define PM_APBCMASK_COM __helper__(PM_APBCMASK_SERCOM, UART_PORT, ) #define GCLK_CLKCTRL_ID_COM __helper__(GCLK_CLKCTRL_ID_SERCOM, UART_PORT, _CORE) #define COM_IRQ __helper__(SERCOM, UART_PORT, _IRQn) #define mcu_com_isr __helper__(SERCOM, UART_PORT, _Handler) -#define COM_OUTREG (COM->USART.DATA.reg) -#define COM_INREG (COM->USART.DATA.reg) +#define COM_OUTREG (COM_UART->USART.DATA.reg) +#define COM_INREG (COM_UART->USART.DATA.reg) #define COM_TX_PAD sercompad(TX_, UART_PORT, TX_PORT, TX_BIT) #define COM_RX_PAD sercompad(RX_, UART_PORT, RX_PORT, RX_BIT) #endif @@ -3053,11 +3047,15 @@ extern "C" } #define mcu_get_global_isr() samd21_global_isr_enabled -#if (INTERFACE == INTERFACE_UART) -#define mcu_rx_ready() (COM->USART.INTFLAG.bit.RXC) -#define mcu_tx_ready() (COM->USART.INTFLAG.bit.DRE) -#elif (INTERFACE == INTERFACE_USB) -#define CFG_TUSB_MCU OPT_MCU_SAMD21 +#if (defined(MCU_HAS_UART) && defined(MCU_HAS_USB)) + extern uint32_t tud_cdc_n_write_available(uint8_t itf); + extern uint32_t tud_cdc_n_available(uint8_t itf); +#define mcu_rx_ready() ((COM_UART->USART.INTFLAG.bit.RXC) || tud_cdc_n_available(0)) +#define mcu_tx_ready() (COM_UART->USART.INTFLAG.bit.DRE) +#elif defined(MCU_HAS_UART) +#define mcu_rx_ready() (COM_UART->USART.INTFLAG.bit.RXC) +#define mcu_tx_ready() (COM_UART->USART.INTFLAG.bit.DRE) +#elif defined(MCU_HAS_USB) extern uint32_t tud_cdc_n_write_available(uint8_t itf); extern uint32_t tud_cdc_n_available(uint8_t itf); #define mcu_rx_ready() tud_cdc_n_available(0) diff --git a/uCNC/src/hal/mcus/stm32f1x/mcu_stm32f1x.c b/uCNC/src/hal/mcus/stm32f1x/mcu_stm32f1x.c index 3aa89ccb0..8d2c91249 100644 --- a/uCNC/src/hal/mcus/stm32f1x/mcu_stm32f1x.c +++ b/uCNC/src/hal/mcus/stm32f1x/mcu_stm32f1x.c @@ -464,7 +464,11 @@ void mcu_putc(char c) while (!mcu_tx_ready()) ; COM_OUTREG = c; +#ifndef ENABLE_SYNC_TX + COM_UART->CR1 |= (USART_CR1_TXEIE); +#endif #endif + #ifdef MCU_HAS_USB if (c != 0) { From 0f1f8d75d43b66ce7588ed8d0745e89f7985a9e0 Mon Sep 17 00:00:00 2001 From: Paciente8159 Date: Wed, 19 Oct 2022 00:13:04 +0100 Subject: [PATCH 6/8] code cleanup --- makefiles/samd21/Makefile | 5 +---- makefiles/samd21/samd21.c | 4 ++-- uCNC/cnc_config.h | 7 ------- uCNC/src/cnc_hal_config_helper.h | 4 ---- uCNC/src/hal/boards/esp32/boardmap_wemos_d1_r32.h | 2 -- uCNC/src/hal/boards/esp8266/boardmap_wemos_d1.h | 2 -- uCNC/src/hal/mcus/avr/mcumap_avr.h | 2 -- uCNC/src/hal/mcus/esp32/mcumap_esp32.h | 2 -- uCNC/src/hal/mcus/esp8266/mcumap_esp8266.h | 2 -- uCNC/src/hal/mcus/lpc176x/mcumap_lpc176x.h | 10 ---------- 10 files changed, 3 insertions(+), 37 deletions(-) diff --git a/makefiles/samd21/Makefile b/makefiles/samd21/Makefile index ee2a40232..b71c04a36 100644 --- a/makefiles/samd21/Makefile +++ b/makefiles/samd21/Makefile @@ -90,8 +90,6 @@ BOARD ?= MZERO #interface 0-USART 1-USB -INTERFACE ?= 1 - # mcu MCU = $(CPU) -mthumb $(FPU) $(FLOAT-ABI) @@ -105,8 +103,7 @@ BUILD_OPTIONS ?= C_DEFS = $(BUILD_OPTIONS)\ -DSAMD21 \ -D__SAMD21E18A__ \ --DBOARD=BOARD_$(BOARD) \ --DINTERFACE=$(INTERFACE) +-DBOARD=BOARD_$(BOARD) # AS includes AS_INCLUDES = diff --git a/makefiles/samd21/samd21.c b/makefiles/samd21/samd21.c index 7ded4ae0d..d6fa03672 100644 --- a/makefiles/samd21/samd21.c +++ b/makefiles/samd21/samd21.c @@ -54,7 +54,7 @@ void SystemInit(void) /* Set the correct number of wait states for 48 MHz @ 3.3v */ NVMCTRL->CTRLB.bit.RWS = 1; -#if (INTERFACE != INTERFACE_USB) +#ifndef MCU_HAS_USB /* Crystal oscillators can take a long time to startup. This waits the maximum amount of time (4 seconds). This can be reduced depending on your crystal oscillator. */ @@ -188,7 +188,7 @@ void SystemInit(void) { }; -#if (INTERFACE == INTERFACE_USB) +#ifdef MCU_HAS_USB USB->DEVICE.QOSCTRL.bit.CQOS = 2; USB->DEVICE.QOSCTRL.bit.DQOS = 2; #endif diff --git a/uCNC/cnc_config.h b/uCNC/cnc_config.h index 0ffff6e82..ee8218293 100644 --- a/uCNC/cnc_config.h +++ b/uCNC/cnc_config.h @@ -25,9 +25,6 @@ extern "C" { #endif -#define INTERFACE_UART 0 -#define INTERFACE_USB 1 - /** * Serial COM * Defines the serial COM baud rate @@ -38,10 +35,6 @@ extern "C" #define BAUDRATE 115200 #endif -#ifndef INTERFACE -#define INTERFACE INTERFACE_UART -#endif - #ifndef ENABLE_WIFI // #define ENABLE_WIFI #endif diff --git a/uCNC/src/cnc_hal_config_helper.h b/uCNC/src/cnc_hal_config_helper.h index 6da90b7f8..442a33017 100644 --- a/uCNC/src/cnc_hal_config_helper.h +++ b/uCNC/src/cnc_hal_config_helper.h @@ -1993,10 +1993,6 @@ extern "C" #define SERVOS_MASK (SERVO0_MASK | SERVO1_MASK | SERVO2_MASK | SERVO3_MASK | SERVO4_MASK | SERVO5_MASK) -#if (INTERFACE < 0 || INTERFACE > 1) -#error "undefined COM interface" -#endif - #ifdef BRESENHAM_16BIT #if (DSS_MAX_OVERSAMPLING < 0 || DSS_MAX_OVERSAMPLING > 3) #error DSS_MAX_OVERSAMPLING invalid value! Should be set between 0 and 3 diff --git a/uCNC/src/hal/boards/esp32/boardmap_wemos_d1_r32.h b/uCNC/src/hal/boards/esp32/boardmap_wemos_d1_r32.h index e54a364bd..9de5b58fb 100644 --- a/uCNC/src/hal/boards/esp32/boardmap_wemos_d1_r32.h +++ b/uCNC/src/hal/boards/esp32/boardmap_wemos_d1_r32.h @@ -63,12 +63,10 @@ extern "C" // #define PROBE_ISR // Setup com pins -#if (INTERFACE == INTERFACE_UART) #define RX_BIT 3 #define TX_BIT 1 // only uncomment this if other port other then 0 is used // #define COM_PORT 0 -#endif // Setup PWM #define PWM0_BIT 23 // assigns PWM0 pin diff --git a/uCNC/src/hal/boards/esp8266/boardmap_wemos_d1.h b/uCNC/src/hal/boards/esp8266/boardmap_wemos_d1.h index 7fa91566a..507b0f0c4 100644 --- a/uCNC/src/hal/boards/esp8266/boardmap_wemos_d1.h +++ b/uCNC/src/hal/boards/esp8266/boardmap_wemos_d1.h @@ -51,14 +51,12 @@ extern "C" //#define ESTOP_ISR // Setup com pins -#if (INTERFACE == INTERFACE_UART) #define RX_BIT 3 #define TX_BIT 1 #define RX_PORT D #define TX_PORT D // only uncomment this if other port other then 0 is used // #define COM_PORT 0 -#endif // Setup PWM #define PWM0_BIT 2 // assigns PWM0 pin diff --git a/uCNC/src/hal/mcus/avr/mcumap_avr.h b/uCNC/src/hal/mcus/avr/mcumap_avr.h index 0bf7aae2d..c41b3cc8f 100644 --- a/uCNC/src/hal/mcus/avr/mcumap_avr.h +++ b/uCNC/src/hal/mcus/avr/mcumap_avr.h @@ -4301,7 +4301,6 @@ extern "C" #endif // COM registers -#if (INTERFACE == INTERFACE_USART) #ifndef UART_PORT #define COM_RX_vect USART_RX_vect #define COM_TX_vect USART_UDRE_vect @@ -4323,7 +4322,6 @@ extern "C" #define RXCIE __rxciereg__(UART_PORT) #define UDRE __udrereg__(UART_PORT) #define RXC __rxcreg__(UART_PORT) -#endif // SPI #if (defined(SPI_CLK) && defined(SPI_SDI) && defined(SPI_SDO)) diff --git a/uCNC/src/hal/mcus/esp32/mcumap_esp32.h b/uCNC/src/hal/mcus/esp32/mcumap_esp32.h index acf81a99d..e27fe9f50 100644 --- a/uCNC/src/hal/mcus/esp32/mcumap_esp32.h +++ b/uCNC/src/hal/mcus/esp32/mcumap_esp32.h @@ -979,11 +979,9 @@ extern "C" #define DIO89_ISRCALLBACK __indirect__(X, ISRCALLBACK) #endif -#if (INTERFACE == INTERFACE_UART) #ifndef COM_PORT #define COM_PORT 0 #endif -#endif #define ENABLE_SYNC_RX #define ENABLE_SYNC_TX diff --git a/uCNC/src/hal/mcus/esp8266/mcumap_esp8266.h b/uCNC/src/hal/mcus/esp8266/mcumap_esp8266.h index d51c806f5..14cbf9fbc 100644 --- a/uCNC/src/hal/mcus/esp8266/mcumap_esp8266.h +++ b/uCNC/src/hal/mcus/esp8266/mcumap_esp8266.h @@ -978,11 +978,9 @@ extern "C" #define DIO89_ISRCALLBACK __indirect__(X, ISRCALLBACK) #endif -#if (INTERFACE == INTERFACE_UART) #ifndef COM_PORT #define COM_PORT 0 #endif -#endif #define ENABLE_SYNC_RX #define ENABLE_SYNC_TX diff --git a/uCNC/src/hal/mcus/lpc176x/mcumap_lpc176x.h b/uCNC/src/hal/mcus/lpc176x/mcumap_lpc176x.h index d46f254df..93bf20448 100644 --- a/uCNC/src/hal/mcus/lpc176x/mcumap_lpc176x.h +++ b/uCNC/src/hal/mcus/lpc176x/mcumap_lpc176x.h @@ -112,16 +112,6 @@ typedef struct ; \ } -#if (INTERFACE == INTERFACE_USB) -// if USB VCP is used force RX sync also -#ifndef ENABLE_SYNC_TX -#define ENABLE_SYNC_TX -#endif -#ifndef ENABLE_SYNC_RX -#define ENABLE_SYNC_RX -#endif -#endif - // Helper macros #define __helper_ex__(left, mid, right) left##mid##right #define __helper__(left, mid, right) __helper_ex__(left, mid, right) From dcf7d28e8c960ed04659b721f9e2293c31688048 Mon Sep 17 00:00:00 2001 From: Paciente8159 Date: Wed, 19 Oct 2022 11:13:50 +0100 Subject: [PATCH 7/8] code cleanup --- uCNC/cnc_config.h | 10 ---------- uCNC/src/hal/boards/stm32/stm32.ini | 15 ++------------- uCNC/src/hal/mcus/avr/mcu_avr.c | 10 ---------- uCNC/src/hal/mcus/esp32/mcu_esp32.c | 8 ++------ uCNC/src/hal/mcus/esp32/mcumap_esp32.h | 3 ++- uCNC/src/hal/mcus/esp8266/mcu_esp8266.c | 5 ----- uCNC/src/hal/mcus/esp8266/mcumap_esp8266.h | 3 ++- uCNC/src/hal/mcus/lpc176x/mcu_lpc176x.c | 2 -- uCNC/src/hal/mcus/mcu.h | 2 -- uCNC/src/hal/mcus/samd21/mcu_samd21.c | 2 -- uCNC/src/hal/mcus/stm32f1x/mcu_stm32f1x.c | 2 -- uCNC/src/hal/mcus/stm32f4x/mcu_stm32f4x.c | 2 -- uCNC/src/hal/mcus/virtual/mcu_virtual.c | 7 ------- 13 files changed, 8 insertions(+), 63 deletions(-) diff --git a/uCNC/cnc_config.h b/uCNC/cnc_config.h index ee8218293..03e1c169e 100644 --- a/uCNC/cnc_config.h +++ b/uCNC/cnc_config.h @@ -39,16 +39,6 @@ extern "C" // #define ENABLE_WIFI #endif - /** - * uncomment to force enable synchronized TX/RX (used in USB VCP) - * enable these options to perform communications in the mcu tasks function - * call instead of being interrupt driven (sync RX may cause problems with - * hardware USART) - * */ - - // #define ENABLE_SYNC_TX - // #define ENABLE_SYNC_RX - /** * Choose the board * Check boards.h for list of available/supported boards diff --git a/uCNC/src/hal/boards/stm32/stm32.ini b/uCNC/src/hal/boards/stm32/stm32.ini index f8353375e..2eafb0ac5 100644 --- a/uCNC/src/hal/boards/stm32/stm32.ini +++ b/uCNC/src/hal/boards/stm32/stm32.ini @@ -5,7 +5,8 @@ [common_stm32] platform = ststm32 ; debug with st-link -; upload_protocol = cmsis-dap +upload_protocol = cmsis-dap +debug_tool = cmsis-dap platform_packages = platformio/tool-openocd debug_build_flags = -Og -g3 -ggdb3 -gdwarf-2 debug_init_cmds = @@ -16,23 +17,11 @@ build_flags = ${common.build_flags} -D HAL_TIM_MODULE_DISABLED -D HAL_EXTI_MODUL [env:bluepill_f103c8] extends = common_stm32 -debug_server = - $PLATFORMIO_CORE_DIR/packages/tool-openocd/bin/openocd - -f - $PLATFORMIO_SRC_DIR/scripts/interface/stlink-v2.cfg - -f - $PLATFORMIO_SRC_DIR/scripts/board/stm32f103c8_blue_pill.cfg board = bluepill_f103c8 build_flags = ${common_stm32.build_flags} -D BOARD=BOARD_BLUEPILL [env:bluepill_f103c8_clone] extends = common_stm32 -debug_server = - $PLATFORMIO_CORE_DIR/packages/tool-openocd/bin/openocd - -f - $PLATFORMIO_SRC_DIR/scripts/interface/stlink-v2.cfg - -f - $PLATFORMIO_SRC_DIR/scripts/board/stm32f103c8_blue_pill.cfg board = bluepill_f103c8 build_flags = ${common_stm32.build_flags} -D BOARD=BOARD_BLUEPILL ; uncomment if it's a bluepill clone diff --git a/uCNC/src/hal/mcus/avr/mcu_avr.c b/uCNC/src/hal/mcus/avr/mcu_avr.c index 1a2285bee..53aac9d28 100644 --- a/uCNC/src/hal/mcus/avr/mcu_avr.c +++ b/uCNC/src/hal/mcus/avr/mcu_avr.c @@ -524,9 +524,7 @@ void mcu_putc(char c) char mcu_getc(void) { -#ifdef ENABLE_SYNC_RX loop_until_bit_is_set(UCSRA, RXC); -#endif return COM_INREG; } @@ -699,14 +697,6 @@ void mcu_start_rtc() void mcu_dotasks() { -#ifdef ENABLE_SYNC_RX - // read any char that is received - while (CHECKBIT(UCSRA, RXC)) - { - unsigned char c = mcu_getc(); - mcu_com_rx_cb(c); - } -#endif } // This was copied from grbl diff --git a/uCNC/src/hal/mcus/esp32/mcu_esp32.c b/uCNC/src/hal/mcus/esp32/mcu_esp32.c index e93e3166b..9c568ec6f 100644 --- a/uCNC/src/hal/mcus/esp32/mcu_esp32.c +++ b/uCNC/src/hal/mcus/esp32/mcu_esp32.c @@ -481,11 +481,8 @@ void mcu_putc(char c) #ifndef mcu_getc char mcu_getc(void) { -#ifdef ENABLE_SYNC_RX while (!mcu_rx_ready()) ; -#endif - return esp32_uart_read(); } #endif @@ -613,8 +610,6 @@ void esp32_delay_us(uint16_t delay) * runs all internal tasks of the MCU. * for the moment these are: * - if USB is enabled and MCU uses tinyUSB framework run tinyUSB tud_task - * - if ENABLE_SYNC_RX is enabled check if there are any chars in the rx transmitter (or the tinyUSB buffer) and read them to the serial_rx_isr - * - if ENABLE_SYNC_TX is enabled check if serial_tx_empty is false and run serial_tx_isr * */ void mcu_dotasks(void) { @@ -744,7 +739,8 @@ IRAM_ATTR void mcu_oneshot_isr(void *arg) timer_pause(ONESHOT_TIMER_TG, ONESHOT_TIMER_IDX); timer_group_clr_intr_status_in_isr(ITP_TIMER_TG, ITP_TIMER_IDX); - if(mcu_timeout_cb){ + if (mcu_timeout_cb) + { mcu_timeout_cb(); } } diff --git a/uCNC/src/hal/mcus/esp32/mcumap_esp32.h b/uCNC/src/hal/mcus/esp32/mcumap_esp32.h index e27fe9f50..a7d1f57cc 100644 --- a/uCNC/src/hal/mcus/esp32/mcumap_esp32.h +++ b/uCNC/src/hal/mcus/esp32/mcumap_esp32.h @@ -983,8 +983,9 @@ extern "C" #define COM_PORT 0 #endif -#define ENABLE_SYNC_RX +#ifndef ENABLE_SYNC_TX #define ENABLE_SYNC_TX +#endif #ifndef RTC_TIMER #define RTC_TIMER 0 diff --git a/uCNC/src/hal/mcus/esp8266/mcu_esp8266.c b/uCNC/src/hal/mcus/esp8266/mcu_esp8266.c index 279710fbc..ca0a9c4f9 100644 --- a/uCNC/src/hal/mcus/esp8266/mcu_esp8266.c +++ b/uCNC/src/hal/mcus/esp8266/mcu_esp8266.c @@ -529,11 +529,8 @@ void mcu_putc(char c) #ifndef mcu_getc char mcu_getc(void) { -#ifdef ENABLE_SYNC_RX while (!mcu_rx_ready()) ; -#endif - return esp8266_uart_read(); } #endif @@ -638,8 +635,6 @@ void esp8266_delay_us(uint16_t delay) * runs all internal tasks of the MCU. * for the moment these are: * - if USB is enabled and MCU uses tinyUSB framework run tinyUSB tud_task - * - if ENABLE_SYNC_RX is enabled check if there are any chars in the rx transmitter (or the tinyUSB buffer) and read them to the serial_rx_isr - * - if ENABLE_SYNC_TX is enabled check if serial_tx_empty is false and run serial_tx_isr * */ void mcu_dotasks(void) { diff --git a/uCNC/src/hal/mcus/esp8266/mcumap_esp8266.h b/uCNC/src/hal/mcus/esp8266/mcumap_esp8266.h index 14cbf9fbc..5768d92d6 100644 --- a/uCNC/src/hal/mcus/esp8266/mcumap_esp8266.h +++ b/uCNC/src/hal/mcus/esp8266/mcumap_esp8266.h @@ -982,8 +982,9 @@ extern "C" #define COM_PORT 0 #endif -#define ENABLE_SYNC_RX +#ifndef ENABLE_SYNC_TX #define ENABLE_SYNC_TX +#endif #define MCU_HAS_ONESHOT_TIMER diff --git a/uCNC/src/hal/mcus/lpc176x/mcu_lpc176x.c b/uCNC/src/hal/mcus/lpc176x/mcu_lpc176x.c index def953183..f3879f712 100644 --- a/uCNC/src/hal/mcus/lpc176x/mcu_lpc176x.c +++ b/uCNC/src/hal/mcus/lpc176x/mcu_lpc176x.c @@ -692,8 +692,6 @@ void mcu_delay_us(uint16_t delay) * runs all internal tasks of the MCU. * for the moment these are: * - if USB is enabled and MCU uses tinyUSB framework run tinyUSB tud_task - * - if ENABLE_SYNC_RX is enabled check if there are any chars in the rx transmitter (or the tinyUSB buffer) and read them to the mcu_com_rx_cb - * - if ENABLE_SYNC_TX is enabled check if com_tx_empty is false and run mcu_com_tx_cb * */ void mcu_dotasks() { diff --git a/uCNC/src/hal/mcus/mcu.h b/uCNC/src/hal/mcus/mcu.h index cdecfa818..bebd7434c 100644 --- a/uCNC/src/hal/mcus/mcu.h +++ b/uCNC/src/hal/mcus/mcu.h @@ -436,8 +436,6 @@ extern "C" * runs all internal tasks of the MCU. * for the moment these are: * - if USB is enabled and MCU uses tinyUSB framework run tinyUSB tud_task - * - if ENABLE_SYNC_RX is enabled check if there are any chars in the rx transmitter (or the tinyUSB buffer) and read them to the mcu_com_rx_cb - * - if ENABLE_SYNC_TX is enabled check if serial_tx_empty is false and run mcu_com_tx_cb * */ void mcu_dotasks(void); diff --git a/uCNC/src/hal/mcus/samd21/mcu_samd21.c b/uCNC/src/hal/mcus/samd21/mcu_samd21.c index f9df521f4..911d6ac56 100644 --- a/uCNC/src/hal/mcus/samd21/mcu_samd21.c +++ b/uCNC/src/hal/mcus/samd21/mcu_samd21.c @@ -931,8 +931,6 @@ void mcu_delay_us(uint16_t delay) * runs all internal tasks of the MCU. * for the moment these are: * - if USB is enabled and MCU uses tinyUSB framework run tinyUSB tud_task - * - if ENABLE_SYNC_RX is enabled check if there are any chars in the rx transmitter (or the tinyUSB buffer) and read them to the mcu_com_rx_cb - * - if ENABLE_SYNC_TX is enabled check if serial_tx_empty is false and run mcu_com_tx_cb * */ void mcu_dotasks(void) { diff --git a/uCNC/src/hal/mcus/stm32f1x/mcu_stm32f1x.c b/uCNC/src/hal/mcus/stm32f1x/mcu_stm32f1x.c index 8d2c91249..051be9c7d 100644 --- a/uCNC/src/hal/mcus/stm32f1x/mcu_stm32f1x.c +++ b/uCNC/src/hal/mcus/stm32f1x/mcu_stm32f1x.c @@ -67,13 +67,11 @@ volatile bool stm32_global_isr_enabled; void MCU_SERIAL_ISR(void) { mcu_disable_global_isr(); -#ifndef ENABLE_SYNC_RX if (COM_UART->SR & USART_SR_RXNE) { unsigned char c = COM_INREG; mcu_com_rx_cb(c); } -#endif #ifndef ENABLE_SYNC_TX if ((COM_UART->SR & USART_SR_TXE) && (COM_UART->CR1 & USART_CR1_TXEIE)) diff --git a/uCNC/src/hal/mcus/stm32f4x/mcu_stm32f4x.c b/uCNC/src/hal/mcus/stm32f4x/mcu_stm32f4x.c index 6e340f7e1..f4f82d9c3 100644 --- a/uCNC/src/hal/mcus/stm32f4x/mcu_stm32f4x.c +++ b/uCNC/src/hal/mcus/stm32f4x/mcu_stm32f4x.c @@ -68,13 +68,11 @@ volatile bool stm32_global_isr_enabled; void MCU_SERIAL_ISR(void) { mcu_disable_global_isr(); -#ifndef ENABLE_SYNC_RX if (COM_UART->SR & USART_SR_RXNE) { unsigned char c = COM_INREG; mcu_com_rx_cb(c); } -#endif #ifndef ENABLE_SYNC_TX if ((COM_UART->SR & USART_SR_TXE) && (COM_UART->CR1 & USART_CR1_TXEIE)) diff --git a/uCNC/src/hal/mcus/virtual/mcu_virtual.c b/uCNC/src/hal/mcus/virtual/mcu_virtual.c index 77fe08bb7..1ae52cc9c 100644 --- a/uCNC/src/hal/mcus/virtual/mcu_virtual.c +++ b/uCNC/src/hal/mcus/virtual/mcu_virtual.c @@ -1572,13 +1572,6 @@ uint16_t mcu_stopPerfCounter(void) void mcu_dotasks(void) { -#ifdef ENABLE_SYNC_RX - while (mcu_read_available()) - { - unsigned char c = mcu_getc(); - serial_rx_isr(c); - } -#endif } #endif From 571271908cd3535843e1bb901fd1723db18f6a82 Mon Sep 17 00:00:00 2001 From: Paciente8159 Date: Wed, 19 Oct 2022 15:33:24 +0100 Subject: [PATCH 8/8] Update stm32.ini --- uCNC/src/hal/boards/stm32/stm32.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/uCNC/src/hal/boards/stm32/stm32.ini b/uCNC/src/hal/boards/stm32/stm32.ini index 2eafb0ac5..9d5b452e2 100644 --- a/uCNC/src/hal/boards/stm32/stm32.ini +++ b/uCNC/src/hal/boards/stm32/stm32.ini @@ -5,7 +5,7 @@ [common_stm32] platform = ststm32 ; debug with st-link -upload_protocol = cmsis-dap +; upload_protocol = cmsis-dap debug_tool = cmsis-dap platform_packages = platformio/tool-openocd debug_build_flags = -Og -g3 -ggdb3 -gdwarf-2