Skip to content

Commit bf1f185

Browse files
committed
Merge branch 'sercom_defs_in_variant' of https://github.com/aethaniel/ArduinoCore-samd
2 parents 9a5422d + 9888e64 commit bf1f185

File tree

7 files changed

+77
-100
lines changed

7 files changed

+77
-100
lines changed

libraries/SPI/SPI.cpp

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,20 @@
2828

2929
const SPISettings DEFAULT_SPI_SETTINGS = SPISettings();
3030

31-
SPIClass::SPIClass(SERCOM *p_sercom, uint8_t uc_pinMISO, uint8_t uc_pinSCK, uint8_t uc_pinMOSI)
31+
SPIClass::SPIClass(SERCOM *p_sercom, uint8_t uc_pinMISO, uint8_t uc_pinSCK, uint8_t uc_pinMOSI, SercomSpiTXPad PadTx, SercomRXPad PadRx)
3232
{
3333
initialized = false;
3434
assert(p_sercom != NULL);
3535
_p_sercom = p_sercom;
3636

37+
// pins
3738
_uc_pinMiso = uc_pinMISO;
3839
_uc_pinSCK = uc_pinSCK;
3940
_uc_pinMosi = uc_pinMOSI;
41+
42+
// SERCOM pads
43+
_padTx=PadTx;
44+
_padRx=PadRx;
4045
}
4146

4247
void SPIClass::begin()
@@ -65,7 +70,7 @@ void SPIClass::config(SPISettings settings)
6570
{
6671
_p_sercom->disableSPI();
6772

68-
_p_sercom->initSPI(SPI_PAD_2_SCK_3, SERCOM_RX_PAD_0, SPI_CHAR_SIZE_8_BITS, settings.bitOrder);
73+
_p_sercom->initSPI(_padTx, _padRx, SPI_CHAR_SIZE_8_BITS, settings.bitOrder);
6974
_p_sercom->initSPIClock(settings.dataMode, settings.clockFreq);
7075

7176
_p_sercom->enableSPI();
@@ -197,4 +202,23 @@ void SPIClass::detachInterrupt() {
197202
// Should be disableInterrupt()
198203
}
199204

200-
SPIClass SPI( &sercom4, PIN_SPI_MISO, PIN_SPI_SCK, PIN_SPI_MOSI );
205+
#if SPI_INTERFACES_COUNT > 0
206+
207+
/* In case new variant doesn't define these macros,
208+
* we put here the ones for Arduino Zero.
209+
*
210+
* These values should be different on some variants!
211+
*
212+
* The SPI PAD values can be found in cores/arduino/SERCOM.h:
213+
* - SercomSpiTXPad
214+
* - SercomRXPad
215+
*/
216+
#ifndef PERIPH_SPI
217+
#define PERIPH_SPI sercom4
218+
#define PAD_SPI_TX SPI_PAD_2_SCK_3
219+
#define PAD_SPI_RX SERCOM_RX_PAD_0
220+
#endif // PERIPH_SPI
221+
222+
SPIClass SPI( &PERIPH_SPI, PIN_SPI_MISO, PIN_SPI_SCK, PIN_SPI_MOSI, PAD_SPI_TX, PAD_SPI_RX );
223+
224+
#endif // SPI_INTERFACES_COUNT > 0

libraries/SPI/SPI.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ class SPISettings {
9191

9292
class SPIClass {
9393
public:
94-
SPIClass(SERCOM *p_sercom, uint8_t uc_pinMISO, uint8_t uc_pinSCK, uint8_t uc_pinMOSI);
94+
SPIClass(SERCOM *p_sercom, uint8_t uc_pinMISO, uint8_t uc_pinSCK, uint8_t uc_pinMOSI, SercomSpiTXPad, SercomRXPad);
95+
9596

9697
byte transfer(uint8_t data);
9798
inline void transfer(void *buf, size_t count);
@@ -120,6 +121,10 @@ class SPIClass {
120121
uint8_t _uc_pinMiso;
121122
uint8_t _uc_pinMosi;
122123
uint8_t _uc_pinSCK;
124+
125+
SercomSpiTXPad _padTx;
126+
SercomRXPad _padRx;
127+
123128
bool initialized;
124129
uint8_t interruptMode;
125130
char interruptSave;

libraries/SPI/library.properties

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
name=SPI
2+
version=1.0
3+
author=Jonathan BAUDIN, Thibaut VIARD, Arduino
4+
maintainer=Arduino <[email protected]>
5+
sentence=Enables the communication with devices that use the Serial Peripheral Interface (SPI) Bus. Specific implementation for Arduino Zero.
6+
paragraph=
7+
url=http://www.arduino.cc/en/Reference/SPI
8+
architectures=samd

libraries/Wire/Wire.cpp

Lines changed: 18 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@ extern "C" {
2626

2727
#include "Wire.h"
2828

29-
TwoWire::TwoWire(SERCOM * s)
29+
TwoWire::TwoWire(SERCOM * s, uint8_t pinSDA, uint8_t pinSCL)
3030
{
3131
this->sercom = s;
32+
this->_uc_pinSDA=pinSDA;
33+
this->_uc_pinSCL=pinSCL;
3234
transmissionBegun = false;
3335
}
3436

@@ -37,8 +39,8 @@ void TwoWire::begin(void) {
3739
sercom->initMasterWIRE(TWI_CLOCK);
3840
sercom->enableWIRE();
3941

40-
pinPeripheral(PIN_WIRE_SDA, g_APinDescription[PIN_WIRE_SDA].ulPinType);
41-
pinPeripheral(PIN_WIRE_SCL, g_APinDescription[PIN_WIRE_SCL].ulPinType);
42+
pinPeripheral(_uc_pinSDA, g_APinDescription[_uc_pinSDA].ulPinType);
43+
pinPeripheral(_uc_pinSCL, g_APinDescription[_uc_pinSCL].ulPinType);
4244
}
4345

4446
void TwoWire::begin(uint8_t address) {
@@ -254,102 +256,23 @@ void TwoWire::onService(void)
254256
}
255257
}
256258

257-
/*
258-
void TwoWire::onService(void)
259-
{
260-
// Retrieve interrupt status
261-
uint32_t sr = TWI_GetStatus(twi);
262-
263-
if (status == SLAVE_IDLE && TWI_STATUS_SVACC(sr)) {
264-
TWI_DisableIt(twi, TWI_IDR_SVACC);
265-
TWI_EnableIt(twi, TWI_IER_RXRDY | TWI_IER_GACC | TWI_IER_NACK
266-
| TWI_IER_EOSACC | TWI_IER_SCL_WS | TWI_IER_TXCOMP);
267-
268-
srvBufferLength = 0;
269-
srvBufferIndex = 0;
270-
271-
// Detect if we should go into RECV or SEND status
272-
// SVREAD==1 means *master* reading -> SLAVE_SEND
273-
if (!TWI_STATUS_SVREAD(sr)) {
274-
status = SLAVE_RECV;
275-
} else {
276-
status = SLAVE_SEND;
277-
278-
// Alert calling program to generate a response ASAP
279-
if (onRequestCallback)
280-
onRequestCallback();
281-
else
282-
// create a default 1-byte response
283-
write((uint8_t) 0);
284-
}
285-
}
286-
287-
if (status != SLAVE_IDLE) {
288-
if (TWI_STATUS_TXCOMP(sr) && TWI_STATUS_EOSACC(sr)) {
289-
if (status == SLAVE_RECV && onReceiveCallback) {
290-
// Copy data into rxBuffer
291-
// (allows to receive another packet while the
292-
// user program reads actual data)
293-
for (uint8_t i = 0; i < srvBufferLength; ++i)
294-
rxBuffer[i] = srvBuffer[i];
295-
rxBufferIndex = 0;
296-
rxBufferLength = srvBufferLength;
297-
298-
// Alert calling program
299-
onReceiveCallback( rxBufferLength);
300-
}
259+
#if WIRE_INTERFACES_COUNT > 0
301260

302-
// Transfer completed
303-
TWI_EnableIt(twi, TWI_SR_SVACC);
304-
TWI_DisableIt(twi, TWI_IDR_RXRDY | TWI_IDR_GACC | TWI_IDR_NACK
305-
| TWI_IDR_EOSACC | TWI_IDR_SCL_WS | TWI_IER_TXCOMP);
306-
status = SLAVE_IDLE;
307-
}
308-
}
261+
/* In case new variant doesn't define these macros,
262+
* we put here the ones for Arduino Zero.
263+
*
264+
* These values should be different on some variants!
265+
*/
309266

310-
if (status == SLAVE_RECV) {
311-
if (TWI_STATUS_RXRDY(sr)) {
312-
if (srvBufferLength < BUFFER_LENGTH)
313-
srvBuffer[srvBufferLength++] = TWI_ReadByte(twi);
314-
}
315-
}
267+
#ifndef PERIPH_WIRE
268+
# define PERIPH_WIRE sercom3
269+
# define WIRE_IT_HANDLER SERCOM3_Handler
270+
#endif // PERIPH_WIRE
316271

317-
if (status == SLAVE_SEND) {
318-
if (TWI_STATUS_TXRDY(sr) && !TWI_STATUS_NACK(sr)) {
319-
uint8_t c = 'x';
320-
if (srvBufferIndex < srvBufferLength)
321-
c = srvBuffer[srvBufferIndex++];
322-
TWI_WriteByte(twi, c);
323-
}
324-
}
325-
}
326-
*/
272+
TwoWire Wire(&PERIPH_WIRE, PIN_WIRE_SDA, PIN_WIRE_SCL);
327273

328-
#if WIRE_INTERFACES_COUNT > 0
329-
/*static void Wire_Init(void) {
330-
pmc_enable_periph_clk(WIRE_INTERFACE_ID);
331-
PIO_Configure(
332-
g_APinDescription[PIN_WIRE_SDA].pPort,
333-
g_APinDescription[PIN_WIRE_SDA].ulPinType,
334-
g_APinDescription[PIN_WIRE_SDA].ulPin,
335-
g_APinDescription[PIN_WIRE_SDA].ulPinConfiguration);
336-
PIO_Configure(
337-
g_APinDescription[PIN_WIRE_SCL].pPort,
338-
g_APinDescription[PIN_WIRE_SCL].ulPinType,
339-
g_APinDescription[PIN_WIRE_SCL].ulPin,
340-
g_APinDescription[PIN_WIRE_SCL].ulPinConfiguration);
341-
342-
NVIC_DisableIRQ(WIRE_ISR_ID);
343-
NVIC_ClearPendingIRQ(WIRE_ISR_ID);
344-
NVIC_SetPriority(WIRE_ISR_ID, 0);
345-
NVIC_EnableIRQ(WIRE_ISR_ID);
346-
}*/
347-
348-
349-
TwoWire Wire(&sercom3);
350-
351-
void SERCOM3_Handler(void) {
274+
void WIRE_IT_HANDLER(void) {
352275
Wire.onService();
353276
}
354277

355-
#endif
278+
#endif // WIRE_INTERFACES_COUNT > 0

libraries/Wire/Wire.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
class TwoWire : public Stream
3232
{
3333
public:
34-
TwoWire(SERCOM *s);
34+
TwoWire(SERCOM *s, uint8_t pinSDA, uint8_t pinSCL);
3535
void begin();
3636
void begin(uint8_t);
3737
void setClock(uint32_t); // dummy function
@@ -59,6 +59,9 @@ class TwoWire : public Stream
5959

6060
private:
6161
SERCOM * sercom;
62+
uint8_t _uc_pinSDA;
63+
uint8_t _uc_pinSCL;
64+
6265
bool transmissionBegun;
6366

6467
// RX Buffer

libraries/Wire/library.properties

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=Wire
2+
version=1.0
3+
author=Jonathan BAUDIN, Thibaut VIARD, Arduino
4+
maintainer=Arduino <[email protected]>
5+
sentence=Allows the communication between devices or sensors connected via Two Wire Interface Bus. Specific implementation for Arduino Zero.
6+
paragraph=
7+
url=http://www.arduino.cc/en/Reference/Wire
8+
architectures=samd
9+

variants/arduino_zero/variant.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@ static const uint8_t ATN = PIN_ATN;
129129
#define PIN_SPI_MISO (22u)
130130
#define PIN_SPI_MOSI (23u)
131131
#define PIN_SPI_SCK (24u)
132+
#define PERIPH_SPI sercom4
133+
#define PAD_SPI_TX SPI_PAD_2_SCK_3
134+
#define PAD_SPI_RX SERCOM_RX_PAD_0
132135

133136
static const uint8_t SS = PIN_A2 ; // SERCOM4 last PAD is present on A2 but HW SS isn't used. Set here only for reference.
134137
static const uint8_t MOSI = PIN_SPI_MOSI ;
@@ -142,6 +145,8 @@ static const uint8_t SCK = PIN_SPI_SCK ;
142145

143146
#define PIN_WIRE_SDA (20u)
144147
#define PIN_WIRE_SCL (21u)
148+
#define PERIPH_WIRE sercom3
149+
#define WIRE_IT_HANDLER SERCOM3_Handler
145150

146151
/*
147152
* USB

0 commit comments

Comments
 (0)