Skip to content

Commit e8f90bc

Browse files
committed
support Serial2
1 parent f67f7b7 commit e8f90bc

File tree

4 files changed

+82
-45
lines changed

4 files changed

+82
-45
lines changed

cores/nRF5/HardwareSerial.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ class HardwareSerial : public Stream
7575
virtual int read(void) = 0;
7676
virtual void flush(void) = 0;
7777
virtual size_t write(uint8_t) = 0;
78-
using Print::write; // pull in write(str) and write(buf, size) from Print
78+
virtual size_t write(const uint8_t *buffer, size_t size) = 0;
79+
using Print::write; // pull in write(str) from Print
7980
virtual operator bool() = 0;
8081
};
8182

cores/nRF5/Uart.cpp

Lines changed: 68 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ void serialEventRun(void)
2727
if (serialEvent && Serial.available() ) serialEvent();
2828
}
2929

30-
Uart::Uart(NRF_UART_Type *_nrfUart, IRQn_Type _IRQn, uint8_t _pinRX, uint8_t _pinTX)
30+
Uart::Uart(NRF_UARTE_Type *_nrfUart, IRQn_Type _IRQn, uint8_t _pinRX, uint8_t _pinTX)
3131
{
3232
nrfUart = _nrfUart;
3333
IRQn = _IRQn;
@@ -39,7 +39,7 @@ Uart::Uart(NRF_UART_Type *_nrfUart, IRQn_Type _IRQn, uint8_t _pinRX, uint8_t _pi
3939
_begun = false;
4040
}
4141

42-
Uart::Uart(NRF_UART_Type *_nrfUart, IRQn_Type _IRQn, uint8_t _pinRX, uint8_t _pinTX, uint8_t _pinCTS, uint8_t _pinRTS)
42+
Uart::Uart(NRF_UARTE_Type *_nrfUart, IRQn_Type _IRQn, uint8_t _pinRX, uint8_t _pinTX, uint8_t _pinCTS, uint8_t _pinRTS)
4343
{
4444
nrfUart = _nrfUart;
4545
IRQn = _IRQn;
@@ -55,8 +55,8 @@ Uart::Uart(NRF_UART_Type *_nrfUart, IRQn_Type _IRQn, uint8_t _pinRX, uint8_t _pi
5555

5656
void Uart::setPins(uint8_t pin_rx, uint8_t pin_tx)
5757
{
58-
uc_pinRX = pin_rx;
59-
uc_pinTX = pin_tx;
58+
uc_pinRX = g_ADigitalPinMap[pin_rx];
59+
uc_pinTX = g_ADigitalPinMap[pin_tx];
6060
}
6161

6262
void Uart::begin(unsigned long baudrate)
@@ -69,15 +69,15 @@ void Uart::begin(unsigned long baudrate, uint16_t /*config*/)
6969
// skip if already begun
7070
if ( _begun ) return;
7171

72-
nrfUart->PSELTXD = uc_pinTX;
73-
nrfUart->PSELRXD = uc_pinRX;
72+
nrfUart->PSEL.TXD = uc_pinTX;
73+
nrfUart->PSEL.RXD = uc_pinRX;
7474

7575
if (uc_hwFlow == 1) {
76-
nrfUart->PSELCTS = uc_pinCTS;
77-
nrfUart->PSELRTS = uc_pinRTS;
78-
nrfUart->CONFIG = (UART_CONFIG_PARITY_Excluded << UART_CONFIG_PARITY_Pos) | UART_CONFIG_HWFC_Enabled;
76+
nrfUart->PSEL.CTS = uc_pinCTS;
77+
nrfUart->PSEL.RTS = uc_pinRTS;
78+
nrfUart->CONFIG = (UARTE_CONFIG_PARITY_Excluded << UARTE_CONFIG_PARITY_Pos) | UARTE_CONFIG_HWFC_Enabled;
7979
} else {
80-
nrfUart->CONFIG = (UART_CONFIG_PARITY_Excluded << UART_CONFIG_PARITY_Pos) | UART_CONFIG_HWFC_Disabled;
80+
nrfUart->CONFIG = (UARTE_CONFIG_PARITY_Excluded << UARTE_CONFIG_PARITY_Pos) | UARTE_CONFIG_HWFC_Disabled;
8181
}
8282

8383
uint32_t nrfBaudRate;
@@ -118,15 +118,16 @@ void Uart::begin(unsigned long baudrate, uint16_t /*config*/)
118118

119119
nrfUart->BAUDRATE = nrfBaudRate;
120120

121-
nrfUart->ENABLE = UART_ENABLE_ENABLE_Enabled;
121+
nrfUart->ENABLE = UARTE_ENABLE_ENABLE_Enabled;
122122

123-
nrfUart->EVENTS_RXDRDY = 0x0UL;
124-
nrfUart->EVENTS_TXDRDY = 0x0UL;
123+
nrfUart->TXD.PTR = (uint32_t)txBuffer;
124+
nrfUart->EVENTS_ENDTX = 0x0UL;
125125

126+
nrfUart->RXD.PTR = (uint32_t)&rxRcv;
127+
nrfUart->RXD.MAXCNT = 1;
126128
nrfUart->TASKS_STARTRX = 0x1UL;
127-
nrfUart->TASKS_STARTTX = 0x1UL;
128129

129-
nrfUart->INTENSET = UART_INTENSET_RXDRDY_Msk;
130+
nrfUart->INTENSET = UARTE_INTENSET_ENDRX_Msk | UARTE_INTENSET_ENDTX_Msk;
130131

131132
NVIC_ClearPendingIRQ(IRQn);
132133
NVIC_SetPriority(IRQn, 3);
@@ -140,18 +141,18 @@ void Uart::end()
140141
{
141142
NVIC_DisableIRQ(IRQn);
142143

143-
nrfUart->INTENCLR = UART_INTENCLR_RXDRDY_Msk;
144+
nrfUart->INTENCLR = UARTE_INTENSET_ENDRX_Msk | UARTE_INTENSET_ENDTX_Msk;
144145

145146
nrfUart->TASKS_STOPRX = 0x1UL;
146147
nrfUart->TASKS_STOPTX = 0x1UL;
147148

148-
nrfUart->ENABLE = UART_ENABLE_ENABLE_Disabled;
149+
nrfUart->ENABLE = UARTE_ENABLE_ENABLE_Disabled;
149150

150-
nrfUart->PSELTXD = 0xFFFFFFFF;
151-
nrfUart->PSELRXD = 0xFFFFFFFF;
151+
nrfUart->PSEL.TXD = 0xFFFFFFFF;
152+
nrfUart->PSEL.RXD = 0xFFFFFFFF;
152153

153-
nrfUart->PSELRTS = 0xFFFFFFFF;
154-
nrfUart->PSELCTS = 0xFFFFFFFF;
154+
nrfUart->PSEL.RTS = 0xFFFFFFFF;
155+
nrfUart->PSEL.CTS = 0xFFFFFFFF;
155156

156157
rxBuffer.clear();
157158

@@ -162,15 +163,28 @@ void Uart::end()
162163

163164
void Uart::flush()
164165
{
165-
rxBuffer.clear();
166+
if ( _begun ) {
167+
xSemaphoreTake(_mutex, portMAX_DELAY);
168+
xSemaphoreGive(_mutex);
169+
}
166170
}
167171

168172
void Uart::IrqHandler()
169173
{
170-
if (nrfUart->EVENTS_RXDRDY)
174+
if (nrfUart->EVENTS_ENDRX)
175+
{
176+
nrfUart->EVENTS_ENDRX = 0x0UL;
177+
if (nrfUart->RXD.AMOUNT)
178+
{
179+
rxBuffer.store_char(rxRcv);
180+
}
181+
nrfUart->TASKS_STARTRX = 0x1UL;
182+
}
183+
184+
if (nrfUart->EVENTS_ENDTX)
171185
{
172-
nrfUart->EVENTS_RXDRDY = 0x0UL;
173-
rxBuffer.store_char(nrfUart->RXD);
186+
nrfUart->EVENTS_ENDTX = 0x0UL;
187+
xSemaphoreGiveFromISR(_mutex, NULL);
174188
}
175189
}
176190

@@ -189,25 +203,38 @@ int Uart::read()
189203
return rxBuffer.read_char();
190204
}
191205

192-
size_t Uart::write(const uint8_t data)
206+
size_t Uart::write(uint8_t data)
193207
{
194-
xSemaphoreTake(_mutex, portMAX_DELAY);
208+
return write(&data, 1);
209+
}
210+
211+
size_t Uart::write(const uint8_t *buffer, size_t size)
212+
{
213+
if(size == 0) return 0;
195214

196-
nrfUart->TXD = data;
215+
size_t sent = 0;
197216

198-
while(!nrfUart->EVENTS_TXDRDY);
217+
do
218+
{
219+
size_t remaining = size - sent;
220+
size_t txSize = min(remaining, SERIAL_BUFFER_SIZE);
199221

200-
nrfUart->EVENTS_TXDRDY = 0x0UL;
222+
xSemaphoreTake(_mutex, portMAX_DELAY);
201223

202-
xSemaphoreGive(_mutex);
224+
memcpy(txBuffer, buffer + sent, txSize);
203225

204-
return 1;
226+
nrfUart->TXD.MAXCNT = txSize;
227+
nrfUart->TASKS_STARTTX = 0x1UL;
228+
sent += txSize;
229+
230+
} while (sent < size);
231+
232+
return sent;
205233
}
206234

207-
Uart SERIAL_PORT_HARDWARE( NRF_UART0, UARTE0_UART0_IRQn, PIN_SERIAL_RX, PIN_SERIAL_TX );
235+
Uart SERIAL_PORT_HARDWARE( NRF_UARTE0, UARTE0_UART0_IRQn, PIN_SERIAL_RX, PIN_SERIAL_TX );
208236

209237
#ifdef HAVE_HWSERIAL2
210-
// TODO UART1 is UARTE only, need update class Uart to work
211238
Uart Serial2( NRF_UARTE1, UARTE1_IRQn, PIN_SERIAL2_RX, PIN_SERIAL2_TX );
212239
#endif
213240

@@ -217,4 +244,11 @@ extern "C"
217244
{
218245
SERIAL_PORT_HARDWARE.IrqHandler();
219246
}
247+
248+
#ifdef HAVE_HWSERIAL2
249+
void UARTE1_IRQHandler()
250+
{
251+
Serial2.IrqHandler();
252+
}
253+
#endif
220254
}

cores/nRF5/Uart.h

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
class Uart : public HardwareSerial
3131
{
3232
public:
33-
Uart(NRF_UART_Type *_nrfUart, IRQn_Type _IRQn, uint8_t _pinRX, uint8_t _pinTX);
34-
Uart(NRF_UART_Type *_nrfUart, IRQn_Type _IRQn, uint8_t _pinRX, uint8_t _pinTX, uint8_t _pinCTS, uint8_t _pinRTS );
33+
Uart(NRF_UARTE_Type *_nrfUart, IRQn_Type _IRQn, uint8_t _pinRX, uint8_t _pinTX);
34+
Uart(NRF_UARTE_Type *_nrfUart, IRQn_Type _IRQn, uint8_t _pinRX, uint8_t _pinTX, uint8_t _pinCTS, uint8_t _pinRTS);
3535

3636
void setPins(uint8_t pin_rx, uint8_t pin_tx);
3737
void begin(unsigned long baudRate);
@@ -41,8 +41,9 @@ class Uart : public HardwareSerial
4141
int peek();
4242
int read();
4343
void flush();
44-
size_t write(const uint8_t data);
45-
using Print::write; // pull in write(str) and write(buf, size) from Print
44+
size_t write(uint8_t data);
45+
size_t write(const uint8_t *buffer, size_t size);
46+
using Print::write; // pull in write(str) from Print
4647

4748
void IrqHandler();
4849

@@ -52,8 +53,10 @@ class Uart : public HardwareSerial
5253
}
5354

5455
private:
55-
NRF_UART_Type *nrfUart;
56+
NRF_UARTE_Type *nrfUart;
5657
RingBuffer rxBuffer;
58+
uint8_t rxRcv;
59+
uint8_t txBuffer[SERIAL_BUFFER_SIZE];
5760

5861
IRQn_Type IRQn;
5962

@@ -93,9 +96,8 @@ class Uart : public HardwareSerial
9396
#define SERIAL_PORT_HARDWARE Serial1
9497
#define SERIAL_PORT_HARDWARE_OPEN Serial1
9598

96-
// TODO need to update class Uart to work with UARTE
97-
//extern Uart Serial2;
98-
//#define HAVE_HWSERIAL2
99+
extern Uart Serial2;
100+
#define HAVE_HWSERIAL2
99101

100102
#else
101103

variants/pca10056/variant.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ static const uint8_t AREF = PIN_AREF;
8989
#define PIN_SERIAL_RX (33)
9090
#define PIN_SERIAL_TX (34)
9191

92-
//#define PIN_SERIAL2_RX (8)
93-
//#define PIN_SERIAL2_TX (6)
92+
#define PIN_SERIAL2_RX (8)
93+
#define PIN_SERIAL2_TX (6)
9494

9595
/*
9696
* SPI Interfaces

0 commit comments

Comments
 (0)