Skip to content

Commit

Permalink
πŸ§‘β€πŸ’» Common serial ports header
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkyhead committed Jan 22, 2025
1 parent 88829bd commit af05f23
Show file tree
Hide file tree
Showing 22 changed files with 334 additions and 609 deletions.
64 changes: 3 additions & 61 deletions Marlin/src/HAL/DUE/HAL.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,67 +35,9 @@

#include <stdint.h>

#include "../../core/serial_hook.h"

// ------------------------
// Serial ports
// ------------------------

typedef ForwardSerial1Class< decltype(Serial) > DefaultSerial1;
typedef ForwardSerial1Class< decltype(Serial1) > DefaultSerial2;
typedef ForwardSerial1Class< decltype(Serial2) > DefaultSerial3;
typedef ForwardSerial1Class< decltype(Serial3) > DefaultSerial4;
extern DefaultSerial1 MSerial0;
extern DefaultSerial2 MSerial1;
extern DefaultSerial3 MSerial2;
extern DefaultSerial4 MSerial3;

#define _MSERIAL(X) MSerial##X
#define MSERIAL(X) _MSERIAL(X)

#if SERIAL_PORT == -1 || ENABLED(EMERGENCY_PARSER)
#define MYSERIAL1 customizedSerial1
#elif WITHIN(SERIAL_PORT, 0, 3)
#define MYSERIAL1 MSERIAL(SERIAL_PORT)
#else
#error "The required SERIAL_PORT must be from 0 to 3, or -1 for USB Serial."
#endif

#ifdef SERIAL_PORT_2
#if SERIAL_PORT_2 == -1 || ENABLED(EMERGENCY_PARSER)
#define MYSERIAL2 customizedSerial2
#elif WITHIN(SERIAL_PORT_2, 0, 3)
#define MYSERIAL2 MSERIAL(SERIAL_PORT_2)
#else
#error "SERIAL_PORT_2 must be from 0 to 3, or -1 for USB Serial."
#endif
#endif

#ifdef SERIAL_PORT_3
#if SERIAL_PORT_3 == -1 || ENABLED(EMERGENCY_PARSER)
#define MYSERIAL3 customizedSerial3
#elif WITHIN(SERIAL_PORT_3, 0, 3)
#define MYSERIAL3 MSERIAL(SERIAL_PORT_3)
#else
#error "SERIAL_PORT_3 must be from 0 to 3, or -1 for USB Serial."
#endif
#endif

#ifdef MMU_SERIAL_PORT
#if WITHIN(MMU_SERIAL_PORT, 0, 3)
#define MMU_SERIAL MSERIAL(MMU_SERIAL_PORT)
#else
#error "MMU_SERIAL_PORT must be from 0 to 3."
#endif
#endif

#ifdef LCD_SERIAL_PORT
#if WITHIN(LCD_SERIAL_PORT, 0, 3)
#define LCD_SERIAL MSERIAL(LCD_SERIAL_PORT)
#else
#error "LCD_SERIAL_PORT must be from 0 to 3."
#endif
#endif
//
// Serial Ports
//

#include "MarlinSerial.h"
#include "MarlinSerialUSB.h"
Expand Down
15 changes: 15 additions & 0 deletions Marlin/src/HAL/DUE/MarlinSerial.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,21 @@
#include "../../core/types.h"
#include "../../core/serial_hook.h"

typedef ForwardSerial1Class< decltype(Serial) > DefaultSerial1;
typedef ForwardSerial1Class< decltype(Serial1) > DefaultSerial2;
typedef ForwardSerial1Class< decltype(Serial2) > DefaultSerial3;
typedef ForwardSerial1Class< decltype(Serial3) > DefaultSerial4;
extern DefaultSerial1 MSerial0;
extern DefaultSerial2 MSerial1;
extern DefaultSerial3 MSerial2;
extern DefaultSerial4 MSerial3;

#define SERIAL_INDEX_MIN 0
#define SERIAL_INDEX_MAX 3
#define EP_SERIAL_PORT(N) customizedSerial##N
#define USB_SERIAL_PORT(N) customizedSerial##N
#include "../shared/serial_ports.h"

// Define constants and variables for buffering incoming serial data. We're
// using a ring buffer (I think), in which rx_buffer_head is the index of the
// location to which to write the next incoming character and rx_buffer_tail
Expand Down
50 changes: 1 addition & 49 deletions Marlin/src/HAL/HC32/HAL.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,60 +36,12 @@

#include "fastio.h"
#include "timers.h"
#include "MarlinSerial.h"

//
// Serial Ports
//
#define _MSERIAL(X) MSerial##X
#define MSERIAL(X) _MSERIAL(X)
#define NUM_UARTS 4

#if SERIAL_PORT == -1
#error "USB Serial is not supported on HC32F460"
#elif WITHIN(SERIAL_PORT, 1, NUM_UARTS)
#define MYSERIAL1 MSERIAL(SERIAL_PORT)
#else
#define MYSERIAL1 MSERIAL(1) // Dummy port
static_assert(false, "SERIAL_PORT must be from 1 to " STRINGIFY(NUM_UARTS) ".")
#endif

#ifdef SERIAL_PORT_2
#if SERIAL_PORT_2 == -1
#error "USB Serial is not supported on HC32F460"
#elif WITHIN(SERIAL_PORT_2, 1, NUM_UARTS)
#define MYSERIAL2 MSERIAL(SERIAL_PORT_2)
#else
#define MYSERIAL2 MSERIAL(1) // Dummy port
static_assert(false, "SERIAL_PORT_2 must be from 1 to " STRINGIFY(NUM_UARTS) ".")
#endif
#endif

#ifdef SERIAL_PORT_3
#if SERIAL_PORT_3 == -1
#error "USB Serial is not supported on HC32F460"
#elif WITHIN(SERIAL_PORT_3, 1, NUM_UARTS)
#define MYSERIAL3 MSERIAL(SERIAL_PORT_3)
#else
#define MYSERIAL3 MSERIAL(1) // Dummy port
static_assert(false, "SERIAL_PORT_3 must be from 1 to " STRINGIFY(NUM_UARTS) ".")
#endif
#endif

#ifdef LCD_SERIAL_PORT
#if LCD_SERIAL_PORT == -1
#error "USB Serial is not supported on HC32F460"
#elif WITHIN(LCD_SERIAL_PORT, 1, NUM_UARTS)
#define LCD_SERIAL MSERIAL(LCD_SERIAL_PORT)
#else
#define LCD_SERIAL MSERIAL(1) // Dummy port
static_assert(false, "LCD_SERIAL_PORT must be from 1 to " STRINGIFY(NUM_UARTS) ".")
#endif

#if HAS_DGUS_LCD
#define LCD_SERIAL_TX_BUFFER_FREE() LCD_SERIAL.availableForWrite()
#endif
#endif
#include "MarlinSerial.h"

//
// Emergency Parser
Expand Down
8 changes: 8 additions & 0 deletions Marlin/src/HAL/HC32/MarlinSerial.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@
#include "../../core/serial_hook.h"
#include <drivers/usart/Usart.h>

#define SERIAL_INDEX_MIN 1
#define SERIAL_INDEX_MAX 4
#include "../shared/serial_ports.h"

#if defined(LCD_SERIAL_PORT) && ANY(HAS_DGUS_LCD, EXTENSIBLE_UI)
#define LCD_SERIAL_TX_BUFFER_FREE() LCD_SERIAL.availableForWrite()
#endif

// Optionally set uart IRQ priority to reduce overflow errors
//#define UART_RX_IRQ_PRIO 1
//#define UART_TX_IRQ_PRIO 1
Expand Down
2 changes: 0 additions & 2 deletions Marlin/src/HAL/LPC1768/HAL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@
#include <CDCSerial.h>
#include <usb/mscuser.h>

DefaultSerial1 USBSerial(false, UsbSerial);

uint32_t MarlinHAL::adc_result = 0;
pin_t MarlinHAL::adc_pin = 0;

Expand Down
65 changes: 4 additions & 61 deletions Marlin/src/HAL/LPC1768/HAL.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,72 +38,15 @@ extern "C" volatile uint32_t _millis;
#include "../shared/math_32bit.h"
#include "../shared/HAL_SPI.h"
#include "fastio.h"
#include "MarlinSerial.h"

#include <adc.h>
#include <pinmapping.h>
#include <CDCSerial.h>

// ------------------------
// Serial ports
// ------------------------

typedef ForwardSerial1Class< decltype(UsbSerial) > DefaultSerial1;
extern DefaultSerial1 USBSerial;

#define _MSERIAL(X) MSerial##X
#define MSERIAL(X) _MSERIAL(X)

#if SERIAL_PORT == -1
#define MYSERIAL1 USBSerial
#elif WITHIN(SERIAL_PORT, 0, 3)
#define MYSERIAL1 MSERIAL(SERIAL_PORT)
#else
#error "SERIAL_PORT must be from 0 to 3. You can also use -1 if the board supports Native USB."
#endif

#ifdef SERIAL_PORT_2
#if SERIAL_PORT_2 == -1
#define MYSERIAL2 USBSerial
#elif WITHIN(SERIAL_PORT_2, 0, 3)
#define MYSERIAL2 MSERIAL(SERIAL_PORT_2)
#else
#error "SERIAL_PORT_2 must be from 0 to 3. You can also use -1 if the board supports Native USB."
#endif
#endif

#ifdef SERIAL_PORT_3
#if SERIAL_PORT_3 == -1
#define MYSERIAL3 USBSerial
#elif WITHIN(SERIAL_PORT_3, 0, 3)
#define MYSERIAL3 MSERIAL(SERIAL_PORT_3)
#else
#error "SERIAL_PORT_3 must be from 0 to 3. You can also use -1 if the board supports Native USB."
#endif
#endif

#ifdef MMU_SERIAL_PORT
#if MMU_SERIAL_PORT == -1
#define MMU_SERIAL USBSerial
#elif WITHIN(MMU_SERIAL_PORT, 0, 3)
#define MMU_SERIAL MSERIAL(MMU_SERIAL_PORT)
#else
#error "MMU_SERIAL_PORT must be from 0 to 3. You can also use -1 if the board supports Native USB."
#endif
#endif
//
// Serial Ports
//

#ifdef LCD_SERIAL_PORT
#if LCD_SERIAL_PORT == -1
#define LCD_SERIAL USBSerial
#elif WITHIN(LCD_SERIAL_PORT, 0, 3)
#define LCD_SERIAL MSERIAL(LCD_SERIAL_PORT)
#else
#error "LCD_SERIAL_PORT must be from 0 to 3. You can also use -1 if the board supports Native USB."
#endif
#if ANY(HAS_DGUS_LCD, EXTENSIBLE_UI)
#define LCD_SERIAL_TX_BUFFER_FREE() LCD_SERIAL.available()
#endif
#endif
#include "MarlinSerial.h"

//
// Interrupts
Expand Down
2 changes: 2 additions & 0 deletions Marlin/src/HAL/LPC1768/MarlinSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

#include "../../inc/MarlinConfig.h"

DefaultSerial1 USBSerial(false, UsbSerial);

#if USING_HW_SERIAL0
MarlinSerial _MSerial0(LPC_UART0);
MSerialT MSerial0(true, _MSerial0);
Expand Down
13 changes: 13 additions & 0 deletions Marlin/src/HAL/LPC1768/MarlinSerial.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
*/
#pragma once

#include <CDCSerial.h>
#include <HardwareSerial.h>
#include <WString.h>

Expand All @@ -30,6 +31,18 @@
#endif
#include "../../core/serial_hook.h"

typedef ForwardSerial1Class< decltype(UsbSerial) > DefaultSerial1;
extern DefaultSerial1 USBSerial;

#define SERIAL_INDEX_MIN 0
#define SERIAL_INDEX_MAX 3
#define USB_SERIAL_PORT(...) USBSerial
#include "../shared/serial_ports.h"

#if defined(LCD_SERIAL_PORT) && ANY(HAS_DGUS_LCD, EXTENSIBLE_UI)
#define LCD_SERIAL_TX_BUFFER_FREE() LCD_SERIAL.available()
#endif

class MarlinSerial : public HardwareSerial<RX_BUFFER_SIZE, TX_BUFFER_SIZE> {
public:
MarlinSerial(LPC_UART_TypeDef *UARTx) : HardwareSerial<RX_BUFFER_SIZE, TX_BUFFER_SIZE>(UARTx) { }
Expand Down
33 changes: 3 additions & 30 deletions Marlin/src/HAL/NATIVE_SIM/HAL.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,37 +71,10 @@ extern MSerialT serial_stream_2;
extern MSerialT serial_stream_3;

#define _MSERIAL(X) serial_stream_##X
#define MSERIAL(X) _MSERIAL(X)

#if WITHIN(SERIAL_PORT, 0, 3)
#define MYSERIAL1 MSERIAL(SERIAL_PORT)
#else
#error "SERIAL_PORT must be from 0 to 3. Please update your configuration."
#endif

#ifdef SERIAL_PORT_2
#if WITHIN(SERIAL_PORT_2, 0, 3)
#define MYSERIAL2 MSERIAL(SERIAL_PORT_2)
#else
#error "SERIAL_PORT_2 must be from 0 to 3. Please update your configuration."
#endif
#endif

#ifdef MMU_SERIAL_PORT
#if WITHIN(MMU_SERIAL_PORT, 0, 3)
#define MMU_SERIAL MSERIAL(MMU_SERIAL_PORT)
#else
#error "MMU_SERIAL_PORT must be from 0 to 3. Please update your configuration."
#endif
#endif

#ifdef LCD_SERIAL_PORT
#if WITHIN(LCD_SERIAL_PORT, 0, 3)
#define LCD_SERIAL MSERIAL(LCD_SERIAL_PORT)
#else
#error "LCD_SERIAL_PORT must be from 0 to 3. Please update your configuration."
#endif
#endif
#define SERIAL_INDEX_MIN 0
#define SERIAL_INDEX_MAX 3
#include "../shared/serial_ports.h"

// ------------------------
// Interrupts
Expand Down
Loading

0 comments on commit af05f23

Please sign in to comment.