Skip to content

Commit 7e23808

Browse files
committed
Updating core to match Arduino samd release 1.6.19.
1 parent c123ec4 commit 7e23808

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+9244
-260
lines changed

sparkfun/samd/cores/arduino/Print.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,14 +238,14 @@ size_t Print::printFloat(double number, uint8_t digits)
238238

239239
// Print the decimal point, but only if there are digits beyond
240240
if (digits > 0) {
241-
n += print(".");
241+
n += print('.');
242242
}
243243

244244
// Extract digits from the remainder one at a time
245245
while (digits-- > 0)
246246
{
247247
remainder *= 10.0;
248-
unsigned int toPrint = (unsigned int)remainder;
248+
unsigned int toPrint = (unsigned int)(remainder);
249249
n += print(toPrint);
250250
remainder -= toPrint;
251251
}

sparkfun/samd/cores/arduino/Print.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
#define DEC 10
2929
#define HEX 16
3030
#define OCT 8
31+
#ifdef BIN // Prevent warnings if BIN is previously defined in "iotnx4.h" or similar
32+
#undef BIN
33+
#endif
3134
#define BIN 2
3235

3336
class Print
@@ -54,6 +57,10 @@ class Print
5457
return write((const uint8_t *)buffer, size);
5558
}
5659

60+
// default to zero, meaning "a single write may block"
61+
// should be overriden by subclasses with buffering
62+
virtual int availableForWrite() { return 0; }
63+
5764
size_t print(const __FlashStringHelper *);
5865
size_t print(const String &);
5966
size_t print(const char[]);
@@ -78,6 +85,8 @@ class Print
7885
size_t println(double, int = 2);
7986
size_t println(const Printable&);
8087
size_t println(void);
88+
89+
virtual void flush() { /* Empty implementation for backward compatibility */ }
8190
};
8291

8392
#endif

sparkfun/samd/cores/arduino/RingBuffer.cpp

Lines changed: 0 additions & 86 deletions
This file was deleted.

sparkfun/samd/cores/arduino/RingBuffer.h

Lines changed: 104 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1717
*/
1818

19+
#ifdef __cplusplus
20+
1921
#ifndef _RING_BUFFER_
2022
#define _RING_BUFFER_
2123

@@ -27,24 +29,114 @@
2729
// location from which to read.
2830
#define SERIAL_BUFFER_SIZE 64
2931

30-
class RingBuffer
32+
template <int N>
33+
class RingBufferN
3134
{
3235
public:
33-
uint8_t _aucBuffer[SERIAL_BUFFER_SIZE] ;
34-
int _iHead ;
35-
int _iTail ;
36+
uint8_t _aucBuffer[N] ;
37+
volatile int _iHead ;
38+
volatile int _iTail ;
3639

3740
public:
38-
RingBuffer( void ) ;
41+
RingBufferN( void ) ;
3942
void store_char( uint8_t c ) ;
40-
void clear();
41-
int read_char();
42-
int available();
43-
int peek();
44-
bool isFull();
43+
void clear();
44+
int read_char();
45+
int available();
46+
int availableForStore();
47+
int peek();
48+
bool isFull();
4549

4650
private:
47-
int nextIndex(int index);
48-
} ;
51+
int nextIndex(int index);
52+
};
53+
54+
typedef RingBufferN<SERIAL_BUFFER_SIZE> RingBuffer;
55+
56+
57+
template <int N>
58+
RingBufferN<N>::RingBufferN( void )
59+
{
60+
memset( _aucBuffer, 0, N ) ;
61+
clear();
62+
}
63+
64+
template <int N>
65+
void RingBufferN<N>::store_char( uint8_t c )
66+
{
67+
int i = nextIndex(_iHead);
68+
69+
// if we should be storing the received character into the location
70+
// just before the tail (meaning that the head would advance to the
71+
// current location of the tail), we're about to overflow the buffer
72+
// and so we don't write the character or advance the head.
73+
if ( i != _iTail )
74+
{
75+
_aucBuffer[_iHead] = c ;
76+
_iHead = i ;
77+
}
78+
}
79+
80+
template <int N>
81+
void RingBufferN<N>::clear()
82+
{
83+
_iHead = 0;
84+
_iTail = 0;
85+
}
86+
87+
template <int N>
88+
int RingBufferN<N>::read_char()
89+
{
90+
if(_iTail == _iHead)
91+
return -1;
92+
93+
uint8_t value = _aucBuffer[_iTail];
94+
_iTail = nextIndex(_iTail);
95+
96+
return value;
97+
}
98+
99+
template <int N>
100+
int RingBufferN<N>::available()
101+
{
102+
int delta = _iHead - _iTail;
103+
104+
if(delta < 0)
105+
return N + delta;
106+
else
107+
return delta;
108+
}
109+
110+
template <int N>
111+
int RingBufferN<N>::availableForStore()
112+
{
113+
if (_iHead >= _iTail)
114+
return N - 1 - _iHead + _iTail;
115+
else
116+
return _iTail - _iHead - 1;
117+
}
118+
119+
template <int N>
120+
int RingBufferN<N>::peek()
121+
{
122+
if(_iTail == _iHead)
123+
return -1;
124+
125+
return _aucBuffer[_iTail];
126+
}
127+
128+
template <int N>
129+
int RingBufferN<N>::nextIndex(int index)
130+
{
131+
return (uint32_t)(index + 1) % N;
132+
}
133+
134+
template <int N>
135+
bool RingBufferN<N>::isFull()
136+
{
137+
return (nextIndex(_iHead) == _iTail);
138+
}
49139

50140
#endif /* _RING_BUFFER_ */
141+
142+
#endif /* __cplusplus */

sparkfun/samd/cores/arduino/SERCOM.cpp

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@
1919
#include "SERCOM.h"
2020
#include "variant.h"
2121

22+
#ifndef WIRE_RISE_TIME_NANOSECONDS
23+
// Default rise time in nanoseconds, based on 4.7K ohm pull up resistors
24+
// you can override this value in your variant if needed
25+
#define WIRE_RISE_TIME_NANOSECONDS 125
26+
#endif
27+
2228
SERCOM::SERCOM(Sercom* s)
2329
{
2430
sercom = s;
@@ -173,6 +179,16 @@ int SERCOM::writeDataUART(uint8_t data)
173179
return 1;
174180
}
175181

182+
void SERCOM::enableDataRegisterEmptyInterruptUART()
183+
{
184+
sercom->USART.INTENSET.reg = SERCOM_USART_INTENSET_DRE;
185+
}
186+
187+
void SERCOM::disableDataRegisterEmptyInterruptUART()
188+
{
189+
sercom->USART.INTENCLR.reg = SERCOM_USART_INTENCLR_DRE;
190+
}
191+
176192
/* =========================
177193
* ===== Sercom SPI
178194
* =========================
@@ -240,11 +256,11 @@ void SERCOM::enableSPI()
240256

241257
void SERCOM::disableSPI()
242258
{
243-
244259
while(sercom->SPI.SYNCBUSY.bit.ENABLE)
245260
{
246261
//Waiting then enable bit from SYNCBUSY is equal to 0;
247262
}
263+
248264
//Setting the enable bit to 0
249265
sercom->SPI.CTRLA.bit.ENABLE = 0;
250266
}
@@ -360,7 +376,7 @@ void SERCOM::enableWIRE()
360376
{
361377
// I2C Master and Slave modes share the ENABLE bit function.
362378

363-
// Enable the I²C master mode
379+
// Enable the I2C master mode
364380
sercom->I2CM.CTRLA.bit.ENABLE = 1 ;
365381

366382
while ( sercom->I2CM.SYNCBUSY.bit.ENABLE != 0 )
@@ -381,7 +397,7 @@ void SERCOM::disableWIRE()
381397
{
382398
// I2C Master and Slave modes share the ENABLE bit function.
383399

384-
// Enable the I²C master mode
400+
// Enable the I2C master mode
385401
sercom->I2CM.CTRLA.bit.ENABLE = 0 ;
386402

387403
while ( sercom->I2CM.SYNCBUSY.bit.ENABLE != 0 )
@@ -390,17 +406,20 @@ void SERCOM::disableWIRE()
390406
}
391407
}
392408

393-
void SERCOM::initSlaveWIRE( uint8_t ucAddress )
409+
void SERCOM::initSlaveWIRE( uint8_t ucAddress, bool enableGeneralCall )
394410
{
395411
// Initialize the peripheral clock and interruption
396412
initClockNVIC() ;
397413
resetWIRE() ;
398414

399415
// Set slave mode
400-
sercom->I2CS.CTRLA.bit.MODE = I2C_SLAVE_OPERATION ;
416+
sercom->I2CS.CTRLA.bit.MODE = I2C_SLAVE_OPERATION;
401417

402418
sercom->I2CS.ADDR.reg = SERCOM_I2CS_ADDR_ADDR( ucAddress & 0x7Ful ) | // 0x7F, select only 7 bits
403-
SERCOM_I2CS_ADDR_ADDRMASK( 0x00ul ) ; // 0x00, only match exact address
419+
SERCOM_I2CS_ADDR_ADDRMASK( 0x00ul ); // 0x00, only match exact address
420+
if (enableGeneralCall) {
421+
sercom->I2CS.ADDR.reg |= SERCOM_I2CS_ADDR_GENCEN; // enable general call (address 0x00)
422+
}
404423

405424
// Set the interrupt register
406425
sercom->I2CS.INTENSET.reg = SERCOM_I2CS_INTENSET_PREC | // Stop
@@ -432,7 +451,7 @@ void SERCOM::initMasterWIRE( uint32_t baudrate )
432451
// sercom->I2CM.INTENSET.reg = SERCOM_I2CM_INTENSET_MB | SERCOM_I2CM_INTENSET_SB | SERCOM_I2CM_INTENSET_ERROR ;
433452

434453
// Synchronous arithmetic baudrate
435-
sercom->I2CM.BAUD.bit.BAUD = SystemCoreClock / ( 2 * baudrate) - 1 ;
454+
sercom->I2CM.BAUD.bit.BAUD = SystemCoreClock / ( 2 * baudrate) - 5 - (((SystemCoreClock / 1000000) * WIRE_RISE_TIME_NANOSECONDS) / (2 * 1000));
436455
}
437456

438457
void SERCOM::prepareNackBitWIRE( void )
@@ -652,16 +671,20 @@ void SERCOM::initClockNVIC( void )
652671
clockId = GCM_SERCOM3_CORE;
653672
IdNvic = SERCOM3_IRQn;
654673
}
674+
#if defined(SERCOM4)
655675
else if(sercom == SERCOM4)
656676
{
657677
clockId = GCM_SERCOM4_CORE;
658678
IdNvic = SERCOM4_IRQn;
659679
}
680+
#endif // SERCOM4
681+
#if defined(SERCOM5)
660682
else if(sercom == SERCOM5)
661683
{
662684
clockId = GCM_SERCOM5_CORE;
663685
IdNvic = SERCOM5_IRQn;
664686
}
687+
#endif // SERCOM5
665688

666689
if ( IdNvic == PendSV_IRQn )
667690
{
@@ -671,7 +694,7 @@ void SERCOM::initClockNVIC( void )
671694

672695
// Setting NVIC
673696
NVIC_EnableIRQ(IdNvic);
674-
NVIC_SetPriority (IdNvic, (1<<__NVIC_PRIO_BITS) - 1); /* set Priority */
697+
NVIC_SetPriority (IdNvic, SERCOM_NVIC_PRIORITY); /* set Priority */
675698

676699
//Setting clock
677700
GCLK->CLKCTRL.reg = GCLK_CLKCTRL_ID( clockId ) | // Generic Clock 0 (SERCOMx)

0 commit comments

Comments
 (0)