Skip to content

Commit 6820f77

Browse files
author
Jim Lindblom
committed
Updating Arduino cores to 1.6.8 SAMD release.
1 parent fb9a78d commit 6820f77

22 files changed

+616
-392
lines changed

sparkfun/samd/cores/arduino/Arduino.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ typedef uint16_t word;
3535
//
3636
#include "avr/pgmspace.h"
3737
#include "avr/interrupt.h"
38+
#include "avr/io.h"
3839

3940
#include "binary.h"
4041
#include "itoa.h"
@@ -114,6 +115,11 @@ void loop( void ) ;
114115

115116
#define bit(b) (1UL << (b))
116117

118+
#if (ARDUINO_SAMD_VARIANT_COMPLIANCE >= 10606)
119+
// Interrupts
120+
#define digitalPinToInterrupt(P) ( P )
121+
#endif
122+
117123
// USB Device
118124
#include "USB/USBDesc.h"
119125
#include "USB/USBCore.h"

sparkfun/samd/cores/arduino/Print.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ size_t Print::printFloat(double number, uint8_t digits)
245245
while (digits-- > 0)
246246
{
247247
remainder *= 10.0;
248-
int toPrint = int(remainder);
248+
unsigned int toPrint = (unsigned int)remainder;
249249
n += print(toPrint);
250250
remainder -= toPrint;
251251
}

sparkfun/samd/cores/arduino/SERCOM.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ void SERCOM::enableUART()
104104

105105
void SERCOM::flushUART()
106106
{
107+
// Skip checking transmission completion if data register is empty
108+
if(isDataRegisterEmptyUART())
109+
return;
110+
107111
// Wait for transmission to complete
108112
while(!sercom->USART.INTFLAG.bit.TXC);
109113
}
@@ -553,11 +557,8 @@ bool SERCOM::sendDataSlaveWIRE(uint8_t data)
553557
//Send data
554558
sercom->I2CS.DATA.bit.DATA = data;
555559

556-
//Wait data transmission successful
557-
while(!sercom->I2CS.INTFLAG.bit.DRDY);
558-
559560
//Problems on line? nack received?
560-
if(sercom->I2CS.STATUS.bit.RXNACK)
561+
if(!sercom->I2CS.INTFLAG.bit.DRDY || sercom->I2CS.STATUS.bit.RXNACK)
561562
return false;
562563
else
563564
return true;

sparkfun/samd/cores/arduino/Stream.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ class Stream : public Print
6666
// parsing methods
6767

6868
void setTimeout(unsigned long timeout); // sets maximum milliseconds to wait for stream data, default is 1 second
69-
69+
unsigned long getTimeout(void) { return _timeout; }
70+
7071
bool find(char *target); // reads data from the stream until the target string is found
7172
bool find(uint8_t *target) { return find ((char *)target); }
7273
// returns true if target string is found, false if timed out (see setTimeout)

sparkfun/samd/cores/arduino/USB/CDC.cpp

Lines changed: 35 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,6 @@
3333

3434
#define CDC_LINESTATE_READY (CDC_LINESTATE_RTS | CDC_LINESTATE_DTR)
3535

36-
struct ring_buffer {
37-
uint8_t buffer[CDC_SERIAL_BUFFER_SIZE];
38-
volatile uint32_t head;
39-
volatile uint32_t tail;
40-
volatile bool full;
41-
};
42-
ring_buffer cdc_rx_buffer = {{0}, 0, 0, false};
43-
4436
typedef struct {
4537
uint32_t dwDTERate;
4638
uint8_t bCharFormat;
@@ -146,7 +138,6 @@ bool CDC_Setup(USBSetup& setup)
146138
return false;
147139
}
148140

149-
uint32_t _serialPeek = -1;
150141
void Serial_::begin(uint32_t /* baud_count */)
151142
{
152143
// uart config is ignored in USB-CDC
@@ -161,85 +152,50 @@ void Serial_::end(void)
161152
{
162153
}
163154

164-
void Serial_::accept(void)
155+
int Serial_::available(void)
165156
{
166-
uint8_t buffer[CDC_SERIAL_BUFFER_SIZE];
167-
uint32_t len = usb.recv(CDC_ENDPOINT_OUT, &buffer, CDC_SERIAL_BUFFER_SIZE);
168-
169-
uint8_t enableInterrupts = ((__get_PRIMASK() & 0x1) == 0);
170-
__disable_irq();
171-
172-
ring_buffer *ringBuffer = &cdc_rx_buffer;
173-
uint32_t i = ringBuffer->head;
174-
175-
// if we should be storing the received character into the location
176-
// just before the tail (meaning that the head would advance to the
177-
// current location of the tail), we're about to overflow the buffer
178-
// and so we don't write the character or advance the head.
179-
uint32_t k = 0;
180-
while (len > 0 && !ringBuffer->full) {
181-
len--;
182-
ringBuffer->buffer[i++] = buffer[k++];
183-
i %= CDC_SERIAL_BUFFER_SIZE;
184-
if (i == ringBuffer->tail)
185-
ringBuffer->full = true;
186-
}
187-
ringBuffer->head = i;
188-
if (enableInterrupts) {
189-
__enable_irq();
190-
}
157+
return usb.available(CDC_ENDPOINT_OUT);
191158
}
192159

193-
int Serial_::available(void)
160+
int Serial_::availableForWrite(void)
194161
{
195-
ring_buffer *buffer = &cdc_rx_buffer;
196-
if (buffer->full) {
197-
return CDC_SERIAL_BUFFER_SIZE;
198-
}
199-
if (buffer->head == buffer->tail) {
200-
USB->DEVICE.DeviceEndpoint[CDC_ENDPOINT_OUT].EPINTENSET.reg = USB_DEVICE_EPINTENCLR_TRCPT(1);
201-
}
202-
return (uint32_t)(CDC_SERIAL_BUFFER_SIZE + buffer->head - buffer->tail) % CDC_SERIAL_BUFFER_SIZE;
162+
// return the number of bytes left in the current bank,
163+
// always EP size - 1, because bank is flushed on every write
164+
return (EPX_SIZE - 1);
203165
}
204166

167+
int _serialPeek = -1;
168+
205169
int Serial_::peek(void)
206170
{
207-
ring_buffer *buffer = &cdc_rx_buffer;
208-
if (buffer->head == buffer->tail && !buffer->full) {
209-
return -1;
210-
} else {
211-
return buffer->buffer[buffer->tail];
212-
}
171+
if (_serialPeek != -1)
172+
return _serialPeek;
173+
_serialPeek = read();
174+
return _serialPeek;
213175
}
214176

215-
216-
// if the ringBuffer is empty: try to fill it
217-
// if it's still empty: return -1
218-
// else return the last char
219-
// so the buffer is filled only when needed
220177
int Serial_::read(void)
221178
{
222-
ring_buffer *buffer = &cdc_rx_buffer;
223-
224-
// if the head isn't ahead of the tail, we don't have any characters
225-
if (buffer->head == buffer->tail && !buffer->full)
226-
{
227-
if (usb.available(CDC_ENDPOINT_OUT))
228-
accept();
229-
}
230-
if (buffer->head == buffer->tail && !buffer->full)
231-
{
232-
return -1;
179+
if (_serialPeek != -1) {
180+
int res = _serialPeek;
181+
_serialPeek = -1;
182+
return res;
233183
}
234-
else
184+
return usb.recv(CDC_ENDPOINT_OUT);
185+
}
186+
187+
size_t Serial_::readBytes(char *buffer, size_t length)
188+
{
189+
size_t count = 0;
190+
_startMillis = millis();
191+
while (count < length)
235192
{
236-
unsigned char c = buffer->buffer[buffer->tail];
237-
buffer->tail = (uint32_t)(buffer->tail + 1) % CDC_SERIAL_BUFFER_SIZE;
238-
buffer->full = false;
239-
// if (usb.available(CDC_ENDPOINT_OUT))
240-
// accept();
241-
return c;
193+
uint32_t n = usb.recv(CDC_ENDPOINT_OUT, buffer+count, length-count);
194+
if (n == 0 && (millis() - _startMillis) >= _timeout)
195+
break;
196+
count += n;
242197
}
198+
return count;
243199
}
244200

245201
void Serial_::flush(void)
@@ -249,28 +205,14 @@ void Serial_::flush(void)
249205

250206
size_t Serial_::write(const uint8_t *buffer, size_t size)
251207
{
252-
/* only try to send bytes if the high-level CDC connection itself
253-
is open (not just the pipe) - the OS should set lineState when the port
254-
is opened and clear lineState when the port is closed.
255-
bytes sent before the user opens the connection or after
256-
the connection is closed are lost - just like with a UART. */
257-
258-
// TODO - ZE - check behavior on different OSes and test what happens if an
259-
// open connection isn't broken cleanly (cable is yanked out, host dies
260-
// or locks up, or host virtual serial port hangs)
261-
if (_usbLineInfo.lineState > 0) // Problem with Windows(R)
262-
{
263-
uint32_t r = usb.send(CDC_ENDPOINT_IN, buffer, size);
208+
uint32_t r = usb.send(CDC_ENDPOINT_IN, buffer, size);
264209

265-
if (r == 0) {
266-
return r;
267-
} else {
268-
setWriteError();
269-
return 0;
270-
}
210+
if (r > 0) {
211+
return r;
212+
} else {
213+
setWriteError();
214+
return 0;
271215
}
272-
setWriteError();
273-
return 0;
274216
}
275217

276218
size_t Serial_::write(uint8_t c) {

0 commit comments

Comments
 (0)