Skip to content

Commit 2a1cea2

Browse files
author
Jim Lindblom
committedApr 19, 2016
Updating libraries to match Arduino 1.6.5 samd definitions.
1 parent 2ccd441 commit 2a1cea2

File tree

19 files changed

+243
-72
lines changed

19 files changed

+243
-72
lines changed
 

‎sparkfun/samd/libraries/SPI/SPI.cpp

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,22 @@ byte SPIClass::transfer(uint8_t data)
194194
return _p_sercom->readDataSPI() & 0xFF;
195195
}
196196

197+
uint16_t SPIClass::transfer16(uint16_t data) {
198+
union { uint16_t val; struct { uint8_t lsb; uint8_t msb; }; } t;
199+
200+
t.val = data;
201+
202+
if (_p_sercom->getDataOrderSPI() == LSB_FIRST) {
203+
t.lsb = transfer(t.lsb);
204+
t.msb = transfer(t.msb);
205+
} else {
206+
t.msb = transfer(t.msb);
207+
t.lsb = transfer(t.lsb);
208+
}
209+
210+
return t.val;
211+
}
212+
197213
void SPIClass::attachInterrupt() {
198214
// Should be enableInterrupt()
199215
}
@@ -203,22 +219,35 @@ void SPIClass::detachInterrupt() {
203219
}
204220

205221
#if SPI_INTERFACES_COUNT > 0
222+
/* In case new variant doesn't define these macros,
223+
* we put here the ones for Arduino Zero.
224+
*
225+
* These values should be different on some variants!
226+
*
227+
* The SPI PAD values can be found in cores/arduino/SERCOM.h:
228+
* - SercomSpiTXPad
229+
* - SercomRXPad
230+
*/
231+
#ifndef PERIPH_SPI
232+
#define PERIPH_SPI sercom4
233+
#define PAD_SPI_TX SPI_PAD_2_SCK_3
234+
#define PAD_SPI_RX SERCOM_RX_PAD_0
235+
#endif // PERIPH_SPI
236+
SPIClass SPI (&PERIPH_SPI, PIN_SPI_MISO, PIN_SPI_SCK, PIN_SPI_MOSI, PAD_SPI_TX, PAD_SPI_RX);
237+
#endif
238+
#if SPI_INTERFACES_COUNT > 1
239+
SPIClass SPI1(&PERIPH_SPI1, PIN_SPI1_MISO, PIN_SPI1_SCK, PIN_SPI1_MOSI, PAD_SPI1_TX, PAD_SPI1_RX);
240+
#endif
241+
#if SPI_INTERFACES_COUNT > 2
242+
SPIClass SPI2(&PERIPH_SPI2, PIN_SPI2_MISO, PIN_SPI2_SCK, PIN_SPI2_MOSI, PAD_SPI2_TX, PAD_SPI2_RX);
243+
#endif
244+
#if SPI_INTERFACES_COUNT > 3
245+
SPIClass SPI3(&PERIPH_SPI3, PIN_SPI3_MISO, PIN_SPI3_SCK, PIN_SPI3_MOSI, PAD_SPI3_TX, PAD_SPI3_RX);
246+
#endif
247+
#if SPI_INTERFACES_COUNT > 4
248+
SPIClass SPI4(&PERIPH_SPI4, PIN_SPI4_MISO, PIN_SPI4_SCK, PIN_SPI4_MOSI, PAD_SPI4_TX, PAD_SPI4_RX);
249+
#endif
250+
#if SPI_INTERFACES_COUNT > 5
251+
SPIClass SPI5(&PERIPH_SPI5, PIN_SPI5_MISO, PIN_SPI5_SCK, PIN_SPI5_MOSI, PAD_SPI5_TX, PAD_SPI5_RX);
252+
#endif
206253

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

‎sparkfun/samd/libraries/SPI/SPI.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ class SPIClass {
9595

9696

9797
byte transfer(uint8_t data);
98+
uint16_t transfer16(uint16_t data);
9899
inline void transfer(void *buf, size_t count);
99100

100101
// Transaction Functions
@@ -142,6 +143,21 @@ void SPIClass::transfer(void *buf, size_t count)
142143
#if SPI_INTERFACES_COUNT > 0
143144
extern SPIClass SPI;
144145
#endif
146+
#if SPI_INTERFACES_COUNT > 1
147+
extern SPIClass SPI1;
148+
#endif
149+
#if SPI_INTERFACES_COUNT > 2
150+
extern SPIClass SPI2;
151+
#endif
152+
#if SPI_INTERFACES_COUNT > 3
153+
extern SPIClass SPI3;
154+
#endif
155+
#if SPI_INTERFACES_COUNT > 4
156+
extern SPIClass SPI4;
157+
#endif
158+
#if SPI_INTERFACES_COUNT > 5
159+
extern SPIClass SPI5;
160+
#endif
145161

146162
// For compatibility with sketches designed for AVR @ 16 MHz
147163
// New programs should use SPI.beginTransaction to set the SPI clock

‎sparkfun/samd/libraries/USBHost/examples/USB_desc/USB_desc.ino

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -340,16 +340,19 @@ void printHIDdescr( uint8_t* descr_ptr )
340340
/* function to print endpoint descriptor */
341341
void printepdescr( uint8_t* descr_ptr )
342342
{
343+
uint8_t transfer_type;
344+
343345
USB_ENDPOINT_DESCRIPTOR* ep_ptr = ( USB_ENDPOINT_DESCRIPTOR* )descr_ptr;
344346
printProgStr(End_Header_str);
345347
printProgStr(End_Address_str);
346348
if( 0x80 & ep_ptr->bEndpointAddress ) printProgStr(PSTR("IN\t\t"));
347349
else printProgStr(PSTR("OUT\t\t"));
348350
print_hex( (ep_ptr->bEndpointAddress & 0xF), 8 );
349351
printProgStr(End_Attr_str);
350-
if( 0x03 & ep_ptr->bmAttributes ) printProgStr(PSTR("INTERRUPT\t"));
351-
else if( 0x02 & ep_ptr->bmAttributes ) printProgStr(PSTR("BULK\t"));
352-
else if( 0x01 & ep_ptr->bmAttributes ) printProgStr(PSTR("ISO\t"));
352+
transfer_type = ep_ptr->bmAttributes & bmUSB_TRANSFER_TYPE;
353+
if( transfer_type == USB_TRANSFER_TYPE_INTERRUPT ) printProgStr(PSTR("INTERRUPT\t"));
354+
else if( transfer_type == USB_TRANSFER_TYPE_BULK ) printProgStr(PSTR("BULK\t"));
355+
else if( transfer_type == USB_TRANSFER_TYPE_ISOCHRONOUS ) printProgStr(PSTR("ISO\t"));
353356
print_hex( ep_ptr->bmAttributes, 8 );
354357
printProgStr(End_Pktsize_str);
355358
print_hex( ep_ptr->wMaxPacketSize, 16 );

‎sparkfun/samd/libraries/USBHost/src/Usb.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ USBHost::USBHost() : bmHubPre(0) {
3636
uint32_t USBHost::Init() {
3737
//devConfigIndex = 0;
3838
// Init host stack
39-
init();
4039
bmHubPre = 0;
4140
UHD_Init();
4241
return 0;
@@ -263,6 +262,7 @@ uint32_t USBHost::InTransfer(EpInfo *pep, uint32_t nak_limit, uint8_t *nbytesptr
263262
continue;
264263
}
265264
if(rcode) {
265+
uhd_freeze_pipe(pep->epAddr);
266266
//printf(">>>>>>>> Problem! dispatchPkt %2.2x\r\n", rcode);
267267
return(rcode);// break; //should be 0, indicating ACK. Else return error code.
268268
}
@@ -305,6 +305,7 @@ uint32_t USBHost::InTransfer(EpInfo *pep, uint32_t nak_limit, uint8_t *nbytesptr
305305
break;
306306
} // if
307307
} //while( 1 )
308+
uhd_freeze_pipe(pep->epAddr);
308309
return ( rcode);
309310
}
310311

@@ -422,6 +423,16 @@ uint32_t USBHost::dispatchPkt(uint32_t token, uint32_t epAddr, uint32_t nak_limi
422423
return 0;
423424
}
424425

426+
//case hrNAK:
427+
if((USB->HOST.HostPipe[epAddr].PINTFLAG.reg & USB_HOST_PINTFLAG_TRFAIL) ) {
428+
USB->HOST.HostPipe[epAddr].PINTFLAG.reg = USB_HOST_PINTFLAG_TRFAIL;
429+
nak_count++;
430+
if(nak_limit && (nak_count == nak_limit)) {
431+
rcode = USB_ERRORFLOW;
432+
return (rcode);
433+
}
434+
}
435+
425436
//case hrNAK:
426437
if( (usb_pipe_table[epAddr].HostDescBank[0].STATUS_BK.reg & USB_ERRORFLOW ) ) {
427438
nak_count++;
@@ -495,7 +506,6 @@ void USBHost::Task(void) //USB state machine
495506

496507
// Init USB stack and driver
497508
UHD_Init();
498-
init();
499509

500510
// Free all USB resources
501511
for (uint32_t i = 0; i < USB_NUMDEVICES; ++i)
@@ -710,7 +720,8 @@ uint32_t USBHost::Configuring(uint32_t parent, uint32_t port, uint32_t lowspeed)
710720

711721
epInfo.epAddr = 0;
712722
epInfo.maxPktSize = 8;
713-
epInfo.epAttribs = 0;
723+
epInfo.bmSndToggle = 0;
724+
epInfo.bmRcvToggle = 0;
714725
epInfo.bmNakPower = USB_NAK_MAX_POWER;
715726

716727
//delay(2000);

‎sparkfun/samd/libraries/USBHost/src/adk.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ ready(false) {
4545
for (uint32_t i = 0; i < ADK_MAX_ENDPOINTS; i++) {
4646
epInfo[i].epAddr = 0;
4747
epInfo[i].maxPktSize = (i) ? 0 : 8;
48-
epInfo[i].epAttribs = 0;
48+
epInfo[i].bmSndToggle = 0;
49+
epInfo[i].bmRcvToggle = 0;
4950
epInfo[i].bmNakPower = (i) ? USB_NAK_NOWAIT : USB_NAK_MAX_POWER;
5051
}//for(uint32_t i=0; i<ADK_MAX_ENDPOINTS; i++...
5152

@@ -364,6 +365,8 @@ void ADK::EndpointXtract(uint32_t conf, uint32_t /* iface */, uint32_t /* alt */
364365
// Fill in the endpoint info structure
365366
epInfo[index].epAddr = (pep->bEndpointAddress & 0x0F);
366367
epInfo[index].maxPktSize = (uint8_t)pep->wMaxPacketSize;
368+
epInfo[index].bmSndToggle = 0;
369+
epInfo[index].bmRcvToggle = 0;
367370

368371
bNumEP++;
369372

‎sparkfun/samd/libraries/USBHost/src/hidboot.cpp

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,23 +181,27 @@ uint8_t KeyboardReportParser::HandleLockingKeys(HID *hid, uint8_t key) {
181181
const uint8_t KeyboardReportParser::numKeys[10] = { '!', '@', '#', '$', '%', '^', '&', '*', '(', ')'};
182182
const uint8_t KeyboardReportParser::symKeysUp[12] = { '_', '+', '{', '}', '|', '~', ':', '"', '~', '<', '>', '?'};
183183
const uint8_t KeyboardReportParser::symKeysLo[12] = { '-', '=', '[', ']', '\\', ' ', ';', '\'', '`', ',', '.', '/'};
184-
const uint8_t KeyboardReportParser::padKeys[5] = { '/', '*', '-', '+', 0x13};
184+
const uint8_t KeyboardReportParser::padKeys[5] = { '/', '*', '-', '+', 0x0d};
185185
#define VALUE_WITHIN(v,l,h) (((v)>=(l)) && ((v)<=(h)))
186186
uint8_t KeyboardReportParser::OemToAscii(uint8_t mod, uint8_t key) {
187187
uint8_t shift = (mod & 0x22);
188+
uint8_t ctrl = (mod & 0x11);
188189

189190
// [a-z]
190191
if (VALUE_WITHIN(key, 0x04, 0x1d)) {
192+
// [^a-^z]
193+
if (ctrl) return (key - 3);
191194
// Upper case letters
192-
if ((kbdLockingKeys.kbdLeds.bmCapsLock == 0 && (mod & 2)) ||
193-
(kbdLockingKeys.kbdLeds.bmCapsLock == 1 && (mod & 2) == 0))
195+
if ((kbdLockingKeys.kbdLeds.bmCapsLock == 0 && shift) ||
196+
(kbdLockingKeys.kbdLeds.bmCapsLock == 1 && shift == 0))
194197
return (key - 4 + 'A');
195198

196199
// Lower case letters
197200
else
198201
return (key - 4 + 'a');
199202
}// Numbers
200203
else if (VALUE_WITHIN(key, 0x1e, 0x27)) {
204+
if (ctrl && (key == 0x23)) return (0x1E); /* RS ^^ */
201205
if (shift)
202206
return ((uint8_t)pgm_read_byte(&getNumKeys()[key - 0x1e]));
203207
else
@@ -206,14 +210,27 @@ uint8_t KeyboardReportParser::OemToAscii(uint8_t mod, uint8_t key) {
206210
else if(VALUE_WITHIN(key, 0x59, 0x61)) {
207211
if(kbdLockingKeys.kbdLeds.bmNumLock == 1)
208212
return (key - 0x59 + '1');
209-
} else if(VALUE_WITHIN(key, 0x2d, 0x38))
213+
} else if(VALUE_WITHIN(key, 0x2d, 0x38)) {
214+
if (ctrl) {
215+
switch (key) {
216+
case 0x2d: return (0x1f); /* US ^_ */
217+
case 0x2f: return (0x1b); /* ESC ^[ */
218+
case 0x30: return (0x1d); /* GS ^] */
219+
case 0x31: return (0x1c); /* FS ^\ */
220+
default: return (0x00);
221+
}
222+
}
210223
return ((shift) ? (uint8_t)pgm_read_byte(&getSymKeysUp()[key - 0x2d]) : (uint8_t)pgm_read_byte(&getSymKeysLo()[key - 0x2d]));
211-
else if(VALUE_WITHIN(key, 0x54, 0x58))
224+
} else if(VALUE_WITHIN(key, 0x54, 0x58))
212225
return (uint8_t)pgm_read_byte(&getPadKeys()[key - 0x54]);
213226
else {
214227
switch(key) {
215228
case UHS_HID_BOOT_KEY_SPACE: return (0x20);
216-
case UHS_HID_BOOT_KEY_ENTER: return (0x13);
229+
case UHS_HID_BOOT_KEY_ENTER: return (0x0d);
230+
case UHS_HID_BOOT_KEY_ESCAPE: return (0x1b);
231+
case UHS_HID_BOOT_KEY_DELETE: return (0x08);
232+
case UHS_HID_BOOT_KEY_DELETE_FORWARD: return (0x7f);
233+
case UHS_HID_BOOT_KEY_TAB: return (0x09);
217234
case UHS_HID_BOOT_KEY_ZERO2: return ((kbdLockingKeys.kbdLeds.bmNumLock == 1) ? '0': 0);
218235
case UHS_HID_BOOT_KEY_PERIOD: return ((kbdLockingKeys.kbdLeds.bmNumLock == 1) ? '.': 0);
219236
}

‎sparkfun/samd/libraries/USBHost/src/hidboot.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ e-mail : support@circuitsathome.com
2525

2626
#define UHS_HID_BOOT_KEY_ZERO 0x27
2727
#define UHS_HID_BOOT_KEY_ENTER 0x28
28+
#define UHS_HID_BOOT_KEY_ESCAPE 0x29
29+
#define UHS_HID_BOOT_KEY_DELETE 0x2a // Backspace
30+
#define UHS_HID_BOOT_KEY_DELETE_FORWARD 0x4C // Delete
31+
#define UHS_HID_BOOT_KEY_TAB 0x2b
2832
#define UHS_HID_BOOT_KEY_SPACE 0x2c
2933
#define UHS_HID_BOOT_KEY_CAPS_LOCK 0x39
3034
#define UHS_HID_BOOT_KEY_SCROLL_LOCK 0x47
@@ -240,7 +244,8 @@ void HIDBoot<BOOT_PROTOCOL>::Initialize() {
240244
for(int i = 0; i < totalEndpoints(BOOT_PROTOCOL); i++) {
241245
epInfo[i].epAddr = 0;
242246
epInfo[i].maxPktSize = (i) ? 0 : 8;
243-
epInfo[i].epAttribs = 0;
247+
epInfo[i].bmSndToggle = 0;
248+
epInfo[i].bmRcvToggle = 0;
244249
epInfo[i].bmNakPower = (i) ? USB_NAK_NOWAIT : USB_NAK_MAX_POWER;
245250
}
246251
bNumEP = 1;
@@ -535,7 +540,8 @@ void HIDBoot<BOOT_PROTOCOL>::EndpointXtract(uint32_t conf, uint32_t iface, uint3
535540
// Fill in the endpoint info structure
536541
epInfo[bNumEP].epAddr = (pep->bEndpointAddress & 0x0F);
537542
epInfo[bNumEP].maxPktSize = (uint8_t)pep->wMaxPacketSize;
538-
epInfo[bNumEP].epAttribs = 0;
543+
epInfo[bNumEP].bmSndToggle = 0;
544+
epInfo[bNumEP].bmRcvToggle = 0;
539545
epInfo[bNumEP].bmNakPower = USB_NAK_NOWAIT;
540546
bNumEP++;
541547

‎sparkfun/samd/libraries/USBHost/src/hiduniversal.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ void HIDUniversal::Initialize() {
6060
for(uint8_t i = 0; i < totalEndpoints; i++) {
6161
epInfo[i].epAddr = 0;
6262
epInfo[i].maxPktSize = (i) ? 0 : 8;
63-
epInfo[i].epAttribs = 0;
63+
epInfo[i].bmSndToggle = 0;
64+
epInfo[i].bmRcvToggle = 0;
6465
epInfo[i].bmNakPower = (i) ? USB_NAK_NOWAIT : USB_NAK_MAX_POWER;
6566
}
6667
bNumEP = 1;
@@ -324,7 +325,8 @@ void HIDUniversal::EndpointXtract(uint32_t conf, uint32_t iface, uint32_t alt, u
324325
// Fill in the endpoint info structure
325326
epInfo[bNumEP].epAddr = (pep->bEndpointAddress & 0x0F);
326327
epInfo[bNumEP].maxPktSize = (uint8_t)pep->wMaxPacketSize;
327-
epInfo[bNumEP].epAttribs = 0;
328+
epInfo[bNumEP].bmSndToggle = 0;
329+
epInfo[bNumEP].bmRcvToggle = 0;
328330
epInfo[bNumEP].bmNakPower = USB_NAK_NOWAIT;
329331

330332
// Fill in the endpoint index list

‎sparkfun/samd/libraries/USBHost/src/printhex.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ e-mail : support@circuitsathome.com
2020
#else
2121
#define __PRINTHEX_H__
2222

23+
#include <Arduino.h>
24+
2325
void E_Notifyc(char c, int lvl);
2426

2527
template <class T>

‎sparkfun/samd/libraries/USBHost/src/usbhub.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,14 @@ qNextPollTime(0),
2828
bPollEnable(false) {
2929
epInfo[0].epAddr = 0;
3030
epInfo[0].maxPktSize = 8;
31-
epInfo[0].epAttribs = 0;
31+
epInfo[0].bmSndToggle = 0;
32+
epInfo[0].bmRcvToggle = 0;
3233
epInfo[0].bmNakPower = USB_NAK_MAX_POWER;
3334

3435
epInfo[1].epAddr = 1;
3536
epInfo[1].maxPktSize = 8; //kludge
36-
epInfo[1].epAttribs = 0;
37+
epInfo[1].bmSndToggle = 0;
38+
epInfo[1].bmRcvToggle = 0;
3739
epInfo[1].bmNakPower = USB_NAK_NOWAIT;
3840

3941
if(pUsb)

‎sparkfun/samd/libraries/Wire/Wire.cpp

Lines changed: 97 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ void TwoWire::begin(uint8_t address) {
4747
//Slave mode
4848
sercom->initSlaveWIRE(address);
4949
sercom->enableWIRE();
50+
51+
pinPeripheral(_uc_pinSDA, g_APinDescription[_uc_pinSDA].ulPinType);
52+
pinPeripheral(_uc_pinSCL, g_APinDescription[_uc_pinSCL].ulPinType);
5053
}
5154

5255
void TwoWire::setClock(uint32_t baudrate) {
@@ -68,6 +71,8 @@ uint8_t TwoWire::requestFrom(uint8_t address, size_t quantity, bool stopBit)
6871

6972
size_t byteRead = 0;
7073

74+
rxBuffer.clear();
75+
7176
if(sercom->startTransmissionWIRE(address, WIRE_READ_FLAG))
7277
{
7378
// Read first data
@@ -82,7 +87,11 @@ uint8_t TwoWire::requestFrom(uint8_t address, size_t quantity, bool stopBit)
8287
}
8388
sercom->prepareNackBitWIRE(); // Prepare NACK to stop slave transmission
8489
//sercom->readDataWIRE(); // Clear data register to send NACK
85-
sercom->prepareCommandBitsWire(WIRE_MASTER_ACT_STOP); // Send Stop
90+
91+
if (stopBit)
92+
{
93+
sercom->prepareCommandBitsWire(WIRE_MASTER_ACT_STOP); // Send Stop
94+
}
8695
}
8796

8897
return byteRead;
@@ -128,7 +137,11 @@ uint8_t TwoWire::endTransmission(bool stopBit)
128137
return 3 ; // Nack or error
129138
}
130139
}
131-
sercom->prepareCommandBitsWire(WIRE_MASTER_ACT_STOP);
140+
141+
if (stopBit)
142+
{
143+
sercom->prepareCommandBitsWire(WIRE_MASTER_ACT_STOP);
144+
}
132145

133146
return 0;
134147
}
@@ -212,56 +225,108 @@ void TwoWire::onService(void)
212225
{
213226
if ( sercom->isSlaveWIRE() )
214227
{
215-
//Received data
216-
if(sercom->isDataReadyWIRE())
228+
if(sercom->isStopDetectedWIRE() ||
229+
(sercom->isAddressMatch() && sercom->isRestartDetectedWIRE() && !sercom->isMasterReadOperationWIRE())) //Stop or Restart detected
217230
{
218-
//Store data
219-
rxBuffer.store_char(sercom->readDataWIRE());
231+
sercom->prepareAckBitWIRE();
232+
sercom->prepareCommandBitsWire(0x03);
220233

221-
//Stop or Restart detected
222-
if(sercom->isStopDetectedWIRE() || sercom->isRestartDetectedWIRE())
234+
//Calling onReceiveCallback, if exists
235+
if(onReceiveCallback)
223236
{
224-
//Calling onReceiveCallback, if exists
225-
if(onReceiveCallback)
226-
{
227-
onReceiveCallback(available());
228-
}
237+
onReceiveCallback(available());
229238
}
239+
240+
rxBuffer.clear();
230241
}
231-
232-
//Address Match
233-
if(sercom->isAddressMatch())
242+
else if(sercom->isAddressMatch()) //Address Match
234243
{
235-
//Is a request ?
236-
if(sercom->isMasterReadOperationWIRE())
244+
sercom->prepareAckBitWIRE();
245+
sercom->prepareCommandBitsWire(0x03);
246+
247+
if(sercom->isMasterReadOperationWIRE()) //Is a request ?
237248
{
249+
// wait for data ready flag,
250+
// before calling request callback
251+
while(!sercom->isDataReadyWIRE());
252+
238253
//Calling onRequestCallback, if exists
239254
if(onRequestCallback)
240255
{
241256
onRequestCallback();
242257
}
243258
}
244259
}
260+
else if(sercom->isDataReadyWIRE()) //Received data
261+
{
262+
if (rxBuffer.isFull()) {
263+
sercom->prepareNackBitWIRE();
264+
} else {
265+
//Store data
266+
rxBuffer.store_char(sercom->readDataWIRE());
267+
268+
sercom->prepareAckBitWIRE();
269+
}
270+
271+
sercom->prepareCommandBitsWire(0x03);
272+
}
245273
}
246274
}
247275

248276
#if WIRE_INTERFACES_COUNT > 0
277+
/* In case new variant doesn't define these macros,
278+
* we put here the ones for Arduino Zero.
279+
*
280+
* These values should be different on some variants!
281+
*/
282+
#ifndef PERIPH_WIRE
283+
#define PERIPH_WIRE sercom3
284+
#define WIRE_IT_HANDLER SERCOM3_Handler
285+
#endif // PERIPH_WIRE
286+
TwoWire Wire(&PERIPH_WIRE, PIN_WIRE_SDA, PIN_WIRE_SCL);
287+
288+
void WIRE_IT_HANDLER(void) {
289+
Wire.onService();
290+
}
291+
#endif
249292

250-
/* In case new variant doesn't define these macros,
251-
* we put here the ones for Arduino Zero.
252-
*
253-
* These values should be different on some variants!
254-
*/
293+
#if WIRE_INTERFACES_COUNT > 1
294+
TwoWire Wire1(&PERIPH_WIRE1, PIN_WIRE1_SDA, PIN_WIRE1_SCL);
255295

256-
#ifndef PERIPH_WIRE
257-
# define PERIPH_WIRE sercom3
258-
# define WIRE_IT_HANDLER SERCOM3_Handler
259-
#endif // PERIPH_WIRE
296+
void WIRE1_IT_HANDLER(void) {
297+
Wire1.onService();
298+
}
299+
#endif
260300

261-
TwoWire Wire(&PERIPH_WIRE, PIN_WIRE_SDA, PIN_WIRE_SCL);
301+
#if WIRE_INTERFACES_COUNT > 2
302+
TwoWire Wire2(&PERIPH_WIRE2, PIN_WIRE2_SDA, PIN_WIRE2_SCL);
262303

263-
void WIRE_IT_HANDLER(void) {
264-
Wire.onService();
265-
}
304+
void WIRE2_IT_HANDLER(void) {
305+
Wire2.onService();
306+
}
307+
#endif
308+
309+
#if WIRE_INTERFACES_COUNT > 3
310+
TwoWire Wire3(&PERIPH_WIRE3, PIN_WIRE3_SDA, PIN_WIRE3_SCL);
311+
312+
void WIRE3_IT_HANDLER(void) {
313+
Wire3.onService();
314+
}
315+
#endif
316+
317+
#if WIRE_INTERFACES_COUNT > 4
318+
TwoWire Wire4(&PERIPH_WIRE4, PIN_WIRE4_SDA, PIN_WIRE4_SCL);
319+
320+
void WIRE4_IT_HANDLER(void) {
321+
Wire4.onService();
322+
}
323+
#endif
324+
325+
#if WIRE_INTERFACES_COUNT > 5
326+
TwoWire Wire5(&PERIPH_WIRE5, PIN_WIRE5_SDA, PIN_WIRE5_SCL);
327+
328+
void WIRE5_IT_HANDLER(void) {
329+
Wire5.onService();
330+
}
331+
#endif
266332

267-
#endif // WIRE_INTERFACES_COUNT > 0

‎sparkfun/samd/libraries/Wire/Wire.h

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,23 @@ class TwoWire : public Stream
106106
//static const uint32_t XMIT_TIMEOUT = 100000;
107107
};
108108

109-
extern TwoWire Wire;
109+
#if WIRE_INTERFACES_COUNT > 0
110+
extern TwoWire Wire;
111+
#endif
112+
#if WIRE_INTERFACES_COUNT > 1
113+
extern TwoWire Wire1;
114+
#endif
115+
#if WIRE_INTERFACES_COUNT > 2
116+
extern TwoWire Wire2;
117+
#endif
118+
#if WIRE_INTERFACES_COUNT > 3
119+
extern TwoWire Wire3;
120+
#endif
121+
#if WIRE_INTERFACES_COUNT > 4
122+
extern TwoWire Wire4;
123+
#endif
124+
#if WIRE_INTERFACES_COUNT > 5
125+
extern TwoWire Wire5;
126+
#endif
110127

111128
#endif

‎sparkfun/samd/libraries/Wire/keywords.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ begin KEYWORD2
1414
beginTransmission KEYWORD2
1515
endTransmission KEYWORD2
1616
requestFrom KEYWORD2
17-
send KEYWORD2
18-
receive KEYWORD2
1917
onReceive KEYWORD2
2018
onRequest KEYWORD2
2119

0 commit comments

Comments
 (0)
Please sign in to comment.