Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

detach uart2 with optional buffer #639

Merged
merged 3 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified docs/mcumap_gen.xlsx
Binary file not shown.
13 changes: 11 additions & 2 deletions uCNC/src/hal/mcus/avr/mcu_avr.c
Original file line number Diff line number Diff line change
Expand Up @@ -418,8 +418,8 @@ ISR(COM_TX_vect, ISR_BLOCK)
DECL_BUFFER(uint8_t, uart2_rx, RX_BUFFER_SIZE);
ISR(COM2_RX_vect, ISR_BLOCK)
{
#if !defined(DETACH_UART2_FROM_MAIN_PROTOCOL)
uint8_t c = COM2_INREG;
#if !defined(DETACH_UART2_FROM_MAIN_PROTOCOL)
if (mcu_com_rx_cb(c))
{
if (BUFFER_FULL(uart2_rx))
Expand All @@ -431,7 +431,16 @@ ISR(COM2_RX_vect, ISR_BLOCK)
BUFFER_STORE(uart2_rx);
}
#else
mcu_uart2_rx_cb(COM2_INREG);
mcu_uart2_rx_cb(c);
#ifndef UART2_DISABLE_BUFFER
if (BUFFER_FULL(uart2_rx))
{
c = OVF;
}

*(BUFFER_NEXT_FREE(uart2_rx)) = c;
BUFFER_STORE(uart2_rx);
#endif
#endif
}

Expand Down
2 changes: 1 addition & 1 deletion uCNC/src/hal/mcus/esp32/esp32_arduino.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,7 @@ static File upload_file;
settings_save(wifi_settings_offset, (uint8_t *)&wifi_settings, sizeof(wifi_settings_t));
}

xTaskCreatePinnedToCore(mcu_wifi_task, "wifiTask", 4069, NULL, 1, NULL, 0);
xTaskCreatePinnedToCore(mcu_wifi_task, "wifiTask", 4069, NULL, 1, NULL, CONFIG_ARDUINO_RUNNING_CORE);
// taskYIELD();

// #ifdef MCU_HAS_WEBSOCKETS
Expand Down
18 changes: 12 additions & 6 deletions uCNC/src/hal/mcus/esp32/mcu_esp32.c
Original file line number Diff line number Diff line change
Expand Up @@ -980,10 +980,10 @@ void mcu_dotasks(void)
#endif
#if defined(MCU_HAS_UART2)
rxlen = uart_read_bytes(UART2_PORT, rxdata, RX_BUFFER_CAPACITY, 0);
#if !defined(DETACH_UART2_FROM_MAIN_PROTOCOL)
for (i = 0; i < rxlen; i++)
{
uint8_t c = (uint8_t)rxdata[i];
#if !defined(DETACH_UART2_FROM_MAIN_PROTOCOL)
if (mcu_com_rx_cb(c))
{
if (BUFFER_FULL(uart2_rx))
Expand All @@ -994,13 +994,19 @@ void mcu_dotasks(void)
*(BUFFER_NEXT_FREE(uart2_rx)) = c;
BUFFER_STORE(uart2_rx);
}
}
#else
for (i = 0; i < rxlen; i++)
{
mcu_uart2_rx_cb((uint8_t)rxdata[i]);
}
mcu_uart2_rx_cb(c);
#ifndef UART2_DISABLE_BUFFER
if (BUFFER_FULL(uart2_rx))
{
c = OVF;
}

*(BUFFER_NEXT_FREE(uart2_rx)) = c;
BUFFER_STORE(uart2_rx);
#endif
#endif
}
#endif

esp32_wifi_bt_process();
Expand Down
9 changes: 9 additions & 0 deletions uCNC/src/hal/mcus/lpc176x/mcu_lpc176x.c
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,15 @@ void MCU_COM2_ISR(void)
}
#else
mcu_uart2_rx_cb(c);
#ifndef UART2_DISABLE_BUFFER
if (BUFFER_FULL(uart2_rx))
{
c = OVF;
}

*(BUFFER_NEXT_FREE(uart2_rx)) = c;
BUFFER_STORE(uart2_rx);
#endif
#endif
}

Expand Down
32 changes: 31 additions & 1 deletion uCNC/src/hal/mcus/mcu.c
Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,6 @@ MCU_RX_CALLBACK bool mcu_com_rx_cb(uint8_t c)
}
break;
}

}
else // extended ascii (plus CMD_CODE_CYCLE_START and DEL)
{
Expand All @@ -769,10 +768,41 @@ MCU_RX_CALLBACK bool mcu_com_rx_cb(uint8_t c)
return true;
}

#ifdef MCU_HAS_UART
#ifdef DETACH_UART_FROM_MAIN_PROTOCOL
MCU_RX_CALLBACK void __attribute__((weak)) mcu_uart_rx_cb(uint8_t c) {}
#endif
#endif

#ifdef MCU_HAS_UART2
#ifdef DETACH_UART2_FROM_MAIN_PROTOCOL
MCU_RX_CALLBACK void __attribute__((weak)) mcu_uart2_rx_cb(uint8_t c){}
#endif
#endif

#ifdef MCU_HAS_USB
#ifdef DETACH_USB_FROM_MAIN_PROTOCOL
MCU_RX_CALLBACK void __attribute__((weak)) mcu_usb_rx_cb(uint8_t c) {}
#endif
#endif

#ifdef MCU_HAS_WIFI
#ifdef DETACH_WIFI_FROM_MAIN_PROTOCOL
MCU_RX_CALLBACK void __attribute__((weak)) mcu_wifi_rx_cb(uint8_t c) {}
#endif
#endif

#ifdef MCU_HAS_BLUETOOTH
#ifdef DETACH_BLUETOOTH_FROM_MAIN_PROTOCOL
MCU_RX_CALLBACK void __attribute__((weak)) mcu_bt_rx_cb(uint8_t c) {}
#endif
#endif

#if (defined(MCU_HAS_I2C))
#if defined(MCU_SUPPORTS_I2C_SLAVE) && (I2C_ADDRESS != 0)
void __attribute__((weak)) mcu_i2c_slave_cb(uint8_t *data, uint8_t *datalen)
{
}
#endif

#endif
14 changes: 12 additions & 2 deletions uCNC/src/hal/mcus/rp2040/rp2040_arduino.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1085,8 +1085,9 @@ extern "C"
#ifdef MCU_HAS_UART2
while (COM2_UART.available() > 0)
{
#ifndef DETACH_UART2_FROM_MAIN_PROTOCOL
uint8_t c = (uint8_t)COM2_UART.read();
#ifndef DETACH_UART2_FROM_MAIN_PROTOCOL

if (mcu_com_rx_cb(c))
{
if (BUFFER_FULL(uart2_rx))
Expand All @@ -1099,7 +1100,16 @@ extern "C"
}

#else
mcu_uart2_rx_cb((uint8_t)COM2_UART.read());
mcu_uart2_rx_cb(c);
#ifndef UART2_DISABLE_BUFFER
if (BUFFER_FULL(uart2_rx))
{
c = OVF;
}

*(BUFFER_NEXT_FREE(uart2_rx)) = c;
BUFFER_STORE(uart2_rx);
#endif
#endif
}
#endif
Expand Down
9 changes: 9 additions & 0 deletions uCNC/src/hal/mcus/samd21/mcu_samd21.c
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,15 @@ void mcu_com2_isr()
}
#else
mcu_uart2_rx_cb(c);
#ifndef UART2_DISABLE_BUFFER
if (BUFFER_FULL(uart2_rx))
{
c = OVF;
}

*(BUFFER_NEXT_FREE(uart2_rx)) = c;
BUFFER_STORE(uart2_rx);
#endif
#endif
}
if (COM2_UART->USART.INTFLAG.bit.DRE && COM2_UART->USART.INTENSET.bit.DRE)
Expand Down
11 changes: 10 additions & 1 deletion uCNC/src/hal/mcus/stm32f1x/mcu_stm32f1x.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,15 @@ void MCU_SERIAL2_ISR(void)
}
#else
mcu_uart2_rx_cb(c);
#ifndef UART2_DISABLE_BUFFER
if (BUFFER_FULL(uart2_rx))
{
c = OVF;
}

*(BUFFER_NEXT_FREE(uart2_rx)) = c;
BUFFER_STORE(uart2_rx);
#endif
#endif
}

Expand Down Expand Up @@ -1043,7 +1052,7 @@ static uint8_t mcu_i2c_write(uint8_t data, bool send_start, bool send_stop, uint
return I2C_NOTOK;
}
}

__TIMEOUT_ASSERT__(timeout)
{
stop = true;
Expand Down
17 changes: 13 additions & 4 deletions uCNC/src/hal/mcus/stm32f4x/mcu_stm32f4x.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,15 @@ void MCU_SERIAL2_ISR(void)
}
#else
mcu_uart2_rx_cb(c);
#ifndef UART2_DISABLE_BUFFER
if (BUFFER_FULL(uart2_rx))
{
c = OVF;
}

*(BUFFER_NEXT_FREE(uart2_rx)) = c;
BUFFER_STORE(uart2_rx);
#endif
#endif
}

Expand Down Expand Up @@ -642,9 +651,9 @@ void mcu_init(void)
mcu_config_af(SPI_SDI, SPI_SDI_AFIO);
mcu_config_af(SPI_CLK, SPI_CLK_AFIO);
mcu_config_af(SPI_SDO, SPI_SDO_AFIO);
#if ASSERT_PIN_IO(SPI_CS)
#if ASSERT_PIN_IO(SPI_CS)
mcu_config_af(SPI_CS, SPI_CS_AFIO);
#endif
#endif
// initialize the SPI configuration register
SPI_REG->CR1 = SPI_CR1_SSM // software slave management enabled
| SPI_CR1_SSI // internal slave select
Expand Down Expand Up @@ -1028,7 +1037,7 @@ static uint8_t mcu_i2c_write(uint8_t data, bool send_start, bool send_stop, uint
{
bool stop __attribute__((__cleanup__(mcu_i2c_write_stop))) = send_stop;
int32_t timeout = ms_timeout;

uint32_t status = send_start ? I2C_SR1_ADDR : I2C_SR1_BTF;
I2C_REG->SR1 &= ~I2C_SR1_AF;
if (send_start)
Expand Down Expand Up @@ -1144,7 +1153,7 @@ static uint8_t mcu_i2c_read(uint8_t *data, bool with_ack, bool send_stop, uint32
}
}

stop = true;
stop = true;
return I2C_NOTOK;
}

Expand Down
10 changes: 5 additions & 5 deletions uCNC/src/interface/serial.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,19 @@ static stream_clear_cb stream_clear;
serial_stream_t *default_stream;
static serial_stream_t *current_stream;

#if defined(MCU_HAS_UART)
#if defined(MCU_HAS_UART) && !defined(DETACH_UART_FROM_MAIN_PROTOCOL)
DECL_SERIAL_STREAM(uart_serial_stream, mcu_uart_getc, mcu_uart_available, mcu_uart_clear, mcu_uart_putc, mcu_uart_flush);
#endif
#if defined(MCU_HAS_UART2)
#if defined(MCU_HAS_UART2) && !defined(DETACH_UART2_FROM_MAIN_PROTOCOL)
DECL_SERIAL_STREAM(uart2_serial_stream, mcu_uart2_getc, mcu_uart2_available, mcu_uart2_clear, mcu_uart2_putc, mcu_uart2_flush);
#endif
#if defined(MCU_HAS_USB)
#if defined(MCU_HAS_USB) && !defined(DETACH_USB_FROM_MAIN_PROTOCOL)
DECL_SERIAL_STREAM(usb_serial_stream, mcu_usb_getc, mcu_usb_available, mcu_usb_clear, mcu_usb_putc, mcu_usb_flush);
#endif
#if defined(MCU_HAS_WIFI)
#if defined(MCU_HAS_WIFI) && !defined(DETACH_WIFI_FROM_MAIN_PROTOCOL)
DECL_SERIAL_STREAM(wifi_serial_stream, mcu_wifi_getc, mcu_wifi_available, mcu_wifi_clear, mcu_wifi_putc, mcu_wifi_flush);
#endif
#if defined(MCU_HAS_BLUETOOTH)
#if defined(MCU_HAS_BLUETOOTH) && !defined(DETACH_BLUETOOTH_FROM_MAIN_PROTOCOL)
DECL_SERIAL_STREAM(bt_serial_stream, mcu_bt_getc, mcu_bt_available, mcu_bt_clear, mcu_bt_putc, mcu_bt_flush);
#endif
#endif
Expand Down
28 changes: 21 additions & 7 deletions uCNC/src/modules/softuart.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ void softuart_putc(softuart_port_t *port, char c)
{
#if (defined(MCU_HAS_UART2) && defined(DETACH_UART2_FROM_MAIN_PROTOCOL))
mcu_uart2_putc(c);
mcu_uart2_flush();
#endif
}
else
Expand Down Expand Up @@ -54,8 +55,21 @@ int16_t softuart_getc(softuart_port_t *port, uint32_t ms_timeout)

if (!port)
{
#if (defined(MCU_HAS_UART2) && defined(DETACH_UART2_FROM_MAIN_PROTOCOL))
return mcu_uart2_getc();
#if (defined(MCU_HAS_UART2) && defined(DETACH_UART2_FROM_MAIN_PROTOCOL) && !defined(UART2_DISABLE_BUFFER))
__TIMEOUT_MS__(ms_timeout)
{
if (mcu_uart2_available())
{
break;
}
}

__TIMEOUT_ASSERT__(ms_timeout)
{
return 0;
}

val = mcu_uart2_getc();
#endif
}
else
Expand All @@ -68,6 +82,11 @@ int16_t softuart_getc(softuart_port_t *port, uint32_t ms_timeout)
}
}

__TIMEOUT_ASSERT__(ms_timeout)
{
return 0;
}

port->waithalf();

uint8_t bits = 8;
Expand All @@ -85,10 +104,5 @@ int16_t softuart_getc(softuart_port_t *port, uint32_t ms_timeout)
port->waithalf();
}

__TIMEOUT_ASSERT__(ms_timeout)
{
return 0;
}

return (int16_t)val;
}
2 changes: 1 addition & 1 deletion uCNC/src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ extern "C"

#define __TIMEOUT_US__(timeout) for (int32_t elap_us_##timeout, curr_us_##timeout = mcu_free_micros(); ((int32_t)timeout) >= 0; elap_us_##timeout = mcu_free_micros() - curr_us_##timeout, timeout -= ABS(elap_us_##timeout), curr_us_##timeout = mcu_free_micros())
#define __TIMEOUT_MS__(timeout) timeout*=1000; __TIMEOUT_US__(timeout)
#define __TIMEOUT_ASSERT__(timeout) if(timeout < 0)
#define __TIMEOUT_ASSERT__(timeout) if(((int32_t)timeout) < 0)

#if defined(__GNUC__) && __GNUC__ >= 7
#define __FALL_THROUGH__ __attribute__ ((fallthrough));
Expand Down
Loading