Skip to content

Added Serial4 (USART2) and updated compiler #133

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

Closed
wants to merge 14 commits into from
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,24 @@
# Arduino Core for SAM3X CPU
# Enhanced Arduino Core for SAM3X CPU

This repository is based from the [Arduio Sam SDK for cortex-m3](https://github.com/arduino/ArduinoCore-sam). The sdk was modified to include the following features which are not supported by the main repository:

- Attachment of a callback function to the IRQ handler of USART/UART peripherals. This comes handy when you need to process data in real-time.
- Addition of USART2 as Serial4.
- Updated GCC to version 11.2.1.
- Using by default C++20 and C17

## Installation

Add the following url to your Arduino package manager

```
https://raw.githubusercontent.com/vChavezB/ArduinoBoards/master/SAM3X/package_vchavezb_sam-enhanced.json
```

---
The rest of this readme has been kept unmodified and is as-is from the original repo.

---

This repository contains the source code and configuration files of the Arduino Core for Atmel's SAM3X processor (used on the [Arduino Due](https://www.arduino.cc/en/Main/ArduinoBoardDue) board).

Expand Down
4 changes: 2 additions & 2 deletions boards.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

arduino_due_x_dbg.name=Arduino Due (Programming Port)
arduino_due_x_dbg.name=Arduino Due Enhanced (Programming Port)
arduino_due_x_dbg.vid.0=0x2341
arduino_due_x_dbg.pid.0=0x003d
arduino_due_x_dbg.vid.1=0x2A03
Expand All @@ -23,7 +23,7 @@ arduino_due_x_dbg.build.variant_system_lib=libsam_sam3x8e_gcc_rel.a
arduino_due_x_dbg.build.vid=0x2341
arduino_due_x_dbg.build.pid=0x003e

arduino_due_x.name=Arduino Due (Native USB Port)
arduino_due_x.name=Arduino Due Enhanced (Native USB Port)
arduino_due_x.vid.0=0x2341
arduino_due_x.pid.0=0x003e
arduino_due_x.vid.1=0x2A03
Expand Down
16 changes: 14 additions & 2 deletions cores/arduino/UARTClass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ void UARTClass::init(const uint32_t dwBaudRate, const uint32_t modeReg)
// Configure interrupts
_pUart->UART_IDR = 0xFFFFFFFF;
_pUart->UART_IER = UART_IER_RXRDY | UART_IER_OVRE | UART_IER_FRAME;

// Enable UART interrupt in NVIC
NVIC_EnableIRQ(_dwIrq);

Expand Down Expand Up @@ -172,7 +172,14 @@ void UARTClass::IrqHandler( void )

// Did we receive data?
if ((status & UART_SR_RXRDY) == UART_SR_RXRDY)
_rx_buffer->store_char(_pUart->UART_RHR);
{
uint8_t uart_rx_frame=_pUart->UART_RHR;
_rx_buffer->store_char(uart_rx_frame);
if(pRx_irq_cb != nullptr)
{
pRx_irq_cb(uart_rx_frame,rx_irq_args);
}
}

// Do we need to keep sending data?
if ((status & UART_SR_TXRDY) == UART_SR_TXRDY)
Expand All @@ -196,3 +203,8 @@ void UARTClass::IrqHandler( void )
}
}

void UARTClass::attachRxIrq(rx_irq_cb cb,void * args)
{
pRx_irq_cb = cb;
rx_irq_args = args;
}
7 changes: 6 additions & 1 deletion cores/arduino/UARTClass.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ class UARTClass : public HardwareSerial
uint32_t getInterruptPriority();

void IrqHandler(void);

typedef void (*rx_irq_cb)(uint8_t frame,void * args);

void attachRxIrq(rx_irq_cb cb,void * args);

operator bool() { return true; }; // UART always active

protected:
Expand All @@ -69,6 +72,8 @@ class UARTClass : public HardwareSerial
RingBuffer *_tx_buffer;

Uart* _pUart;
rx_irq_cb pRx_irq_cb;
void * rx_irq_args;
IRQn_Type _dwIrq;
uint32_t _dwId;

Expand Down
11 changes: 11 additions & 0 deletions cores/arduino/new.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,14 @@ void operator delete[](void * ptr) {
free(ptr);
}

/*
Fix for C++>14
https://forum.arduino.cc/t/undefined-reference-to-operator-delete-void-unsigned-int/620428
*/
void operator delete(void* ptr, std::size_t) _GLIBCXX_USE_NOEXCEPT {
delete ptr;
}

void operator delete[](void* ptr, std::size_t) _GLIBCXX_USE_NOEXCEPT {
delete[] ptr;
}
6 changes: 3 additions & 3 deletions platform.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ compiler.warning_flags.default=
compiler.warning_flags.more=-Wall
compiler.warning_flags.all=-Wall -Wextra

compiler.path={runtime.tools.arm-none-eabi-gcc-4.8.3-2014q1.path}/bin/
compiler.path={runtime.tools.arm-none-eabi-gcc-11.2.1-xpack.path}/bin/
compiler.c.cmd=arm-none-eabi-gcc
compiler.c.flags=-c -g -Os {compiler.warning_flags} -std=gnu11 -ffunction-sections -fdata-sections -nostdlib --param max-inline-insns-single=500 -Dprintf=iprintf -MMD
compiler.c.flags=-c -g -Os {compiler.warning_flags} -std=gnu17 -ffunction-sections -fdata-sections -nostdlib --param max-inline-insns-single=500 -Dprintf=iprintf -MMD
compiler.c.elf.cmd=arm-none-eabi-gcc
compiler.c.elf.flags=-Os -Wl,--gc-sections
compiler.S.cmd=arm-none-eabi-gcc
compiler.S.flags=-c -g -x assembler-with-cpp -MMD
compiler.cpp.cmd=arm-none-eabi-g++
compiler.cpp.flags=-c -g -Os {compiler.warning_flags} -std=gnu++11 -ffunction-sections -fdata-sections -nostdlib -fno-threadsafe-statics --param max-inline-insns-single=500 -fno-rtti -fno-exceptions -MMD
compiler.cpp.flags=-c -g -Os {compiler.warning_flags} -std=gnu++20 -ffunction-sections -fdata-sections -nostdlib -fno-threadsafe-statics --param max-inline-insns-single=500 -fno-rtti -fno-exceptions -MMD
compiler.ar.cmd=arm-none-eabi-ar
compiler.ar.flags=rcs
compiler.objcopy.cmd=arm-none-eabi-objcopy
Expand Down
21 changes: 19 additions & 2 deletions variants/arduino_due_x/variant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,12 +286,12 @@ extern const PinDescription g_APinDescription[]=
{ PIOA, PIO_PA1A_CANRX0|PIO_PA0A_CANTX0, ID_PIOA, PIO_PERIPH_A, PIO_DEFAULT, (PIN_ATTR_DIGITAL|PIN_ATTR_COMBO), NO_ADC, NO_ADC, NOT_ON_PWM, NOT_ON_TIMER },
// 91 - CAN1 all pins
{ PIOB, PIO_PB15A_CANRX1|PIO_PB14A_CANTX1, ID_PIOB, PIO_PERIPH_A, PIO_DEFAULT, (PIN_ATTR_DIGITAL|PIN_ATTR_COMBO), NO_ADC, NO_ADC, NOT_ON_PWM, NOT_ON_TIMER },

// 92 - USART2 (Serial4) all pins
{ PIOB, PIO_PB20A_TXD2|PIO_PB21A_RXD2, ID_PIOB, PIO_PERIPH_A, PIO_DEFAULT, (PIN_ATTR_DIGITAL|PIN_ATTR_COMBO), NO_ADC, NO_ADC, NOT_ON_PWM, NOT_ON_TIMER },
// END
{ NULL, 0, 0, PIO_NOT_A_PIN, PIO_DEFAULT, 0, NO_ADC, NO_ADC, NOT_ON_PWM, NOT_ON_TIMER }
} ;


uint8_t g_pinStatus[PINS_COUNT] = {0};

#ifdef __cplusplus
Expand Down Expand Up @@ -321,9 +321,11 @@ void UART_Handler(void)
RingBuffer rx_buffer2;
RingBuffer rx_buffer3;
RingBuffer rx_buffer4;
RingBuffer rx_buffer5;
RingBuffer tx_buffer2;
RingBuffer tx_buffer3;
RingBuffer tx_buffer4;
RingBuffer tx_buffer5;

USARTClass Serial1(USART0, USART0_IRQn, ID_USART0, &rx_buffer2, &tx_buffer2);
void serialEvent1() __attribute__((weak));
Expand All @@ -334,6 +336,9 @@ void serialEvent2() { }
USARTClass Serial3(USART3, USART3_IRQn, ID_USART3, &rx_buffer4, &tx_buffer4);
void serialEvent3() __attribute__((weak));
void serialEvent3() { }
USARTClass Serial4(USART2, USART2_IRQn, ID_USART2, &rx_buffer5, &tx_buffer5);
void serialEvent4() __attribute__((weak));
void serialEvent4() { }

// IT handlers
void USART0_Handler(void)
Expand All @@ -351,6 +356,11 @@ void USART3_Handler(void)
Serial3.IrqHandler();
}

void USART2_Handler(void)
{
Serial4.IrqHandler();
}

// ----------------------------------------------------------------------------

void serialEventRun(void)
Expand All @@ -359,6 +369,7 @@ void serialEventRun(void)
if (Serial1.available()) serialEvent1();
if (Serial2.available()) serialEvent2();
if (Serial3.available()) serialEvent3();
if (Serial4.available()) serialEvent4();
}

// ----------------------------------------------------------------------------
Expand Down Expand Up @@ -415,6 +426,12 @@ void init( void )
g_APinDescription[PINS_USART3].ulPinType,
g_APinDescription[PINS_USART3].ulPin,
g_APinDescription[PINS_USART3].ulPinConfiguration);

PIO_Configure(
g_APinDescription[PINS_USART2].pPort,
g_APinDescription[PINS_USART2].ulPinType,
g_APinDescription[PINS_USART2].ulPin,
g_APinDescription[PINS_USART2].ulPinConfiguration);

// Initialize USB pins
PIO_Configure(
Expand Down
5 changes: 3 additions & 2 deletions variants/arduino_due_x/variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,8 @@ static const uint8_t SCL1 = PIN_WIRE1_SCL;
#define PINS_USART1 (83u)
// Serial3
#define PINS_USART3 (84u)

// Serial4
#define PINS_USART2 (92u)
/*
* USB Interfaces
*/
Expand Down Expand Up @@ -252,7 +253,7 @@ extern UARTClass Serial;
extern USARTClass Serial1;
extern USARTClass Serial2;
extern USARTClass Serial3;

extern USARTClass Serial4;
#endif

// These serial port names are intended to allow libraries and architecture-neutral
Expand Down