Skip to content

Commit 39b6c3b

Browse files
committed
Added support for the Arduino Due
Untested
1 parent 37d59a6 commit 39b6c3b

File tree

4 files changed

+91
-11
lines changed

4 files changed

+91
-11
lines changed

UsbCore.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ typedef MAX3421e<P53, P54> MAX3421E; // Arduino Mega ADK
2424
#elif defined(ARDUINO_AVR_BALANDUINO)
2525
typedef MAX3421e<P20, P19> MAX3421E; // Balanduino
2626
#else
27-
typedef MAX3421e<P10, P9> MAX3421E; // Official Arduinos (UNO, Duemilanove, Mega, 2560, Leonardo etc.) or Teensy 2.0 and 3.0
27+
typedef MAX3421e<P10, P9> MAX3421E; // Official Arduinos (UNO, Duemilanove, Mega, 2560, Leonardo, Due etc.) or Teensy 2.0 and 3.0
2828
#endif
2929

3030
/* Common setup data constant combinations */

avrpins.h

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -749,11 +749,13 @@ class Tp_Tc {
749749

750750
#endif // __AVR__
751751

752-
#if defined(__arm__) && defined(CORE_TEENSY)
752+
#if defined(__arm__)
753753

754754
// pointers are 32 bits on ARM
755755
#define pgm_read_pointer(p) pgm_read_dword(p)
756756

757+
#if defined(CORE_TEENSY) && (defined(__MK20DX128__) || defined(__MK20DX256__))
758+
757759
#include "core_pins.h"
758760
#include "avr_emulation.h"
759761

@@ -819,6 +821,57 @@ MAKE_PIN(P33, CORE_PIN33_PORTREG, CORE_PIN33_BIT, CORE_PIN33_CONFIG);
819821

820822
#undef MAKE_PIN
821823

824+
#elif defined(ARDUINO_SAM_DUE) && defined(__SAM3X8E__)
825+
826+
// Disable interrupts
827+
// Enable the pull up resistor
828+
// Set to INPUT
829+
// Enable PIO
830+
831+
// Disable interrupts
832+
// Disable the pull up resistor
833+
// Set to OUTPUT
834+
// Enable PIO
835+
836+
#define MAKE_PIN(className, baseReg, pinMask) \
837+
class className { \
838+
public: \
839+
static void Set() { \
840+
baseReg->PIO_SODR = pinMask; \
841+
} \
842+
static void Clear() { \
843+
baseReg->PIO_CODR = pinMask; \
844+
} \
845+
static void SetDirRead() { \
846+
baseReg->PIO_IDR = pinMask ; \
847+
baseReg->PIO_PUER = pinMask; \
848+
baseReg->PIO_ODR = pinMask; \
849+
baseReg->PIO_PER = pinMask; \
850+
} \
851+
static void SetDirWrite() { \
852+
baseReg->PIO_IDR = pinMask ; \
853+
baseReg->PIO_PUDR = pinMask; \
854+
baseReg->PIO_OER = pinMask; \
855+
baseReg->PIO_PER = pinMask; \
856+
} \
857+
static uint8_t IsSet() { \
858+
return baseReg->PIO_PDSR & pinMask; \
859+
} \
860+
};
861+
862+
MAKE_PIN(P9, PIOC, PIO_PC21); // INT
863+
MAKE_PIN(P10, PIOC, PIO_PC29); // SS
864+
MAKE_PIN(P74, PIOA, PIO_PA25); // MISO
865+
MAKE_PIN(P75, PIOA, PIO_PA26); // MOSI
866+
MAKE_PIN(P76, PIOA, PIO_PA27); // CLK
867+
868+
#undef MAKE_PIN
869+
870+
#else
871+
#error "Please define board in avrpins.h"
872+
873+
#endif
874+
822875
#endif // __arm__
823876

824877
#endif //_avrpins_h_

settings.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@
6868
// No user serviceable parts below this line.
6969
// DO NOT change anything below here unless you are a developer!
7070

71-
// When will we drop support for the older bug-ridden stuff?
7271
#if defined(ARDUINO) && ARDUINO >=100
7372
#include <Arduino.h>
7473
#else
@@ -79,7 +78,7 @@
7978
#define F(str) (str)
8079
#endif
8180

82-
#ifdef __GNUC__
81+
#if defined(__GNUC__) && defined(__AVR__)
8382
#ifndef GCC_VERSION
8483
#define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
8584
#endif
@@ -110,10 +109,14 @@
110109
#define XMEM_RELEASE_SPI() (void(0))
111110
#endif
112111

113-
#if defined(__MK20DX128__) || defined(__MK20DX256__)
112+
#if defined(CORE_TEENSY) && (defined(__MK20DX128__) || defined(__MK20DX256__))
114113
#define USING_SPI4TEENSY3 USE_SPI4TEENSY3
115114
#else
116115
#define USING_SPI4TEENSY3 0
117116
#endif
118117

118+
#if defined(ARDUINO_SAM_DUE) && defined(__SAM3X8E__)
119+
#include <SPI.h> // Use the Arduino SPI library for the Arduino Due
120+
#endif
121+
119122
#endif /* SETTINGS_H */

usbhost.h

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,8 @@ e-mail : [email protected]
2929

3030
/* SPI initialization */
3131
template< typename SPI_CLK, typename SPI_MOSI, typename SPI_MISO, typename SPI_SS > class SPi {
32-
#if USING_SPI4TEENSY3
3332
public:
34-
33+
#if USING_SPI4TEENSY3
3534
static void init() {
3635
// spi4teensy3 inits everything for us, except /SS
3736
// CLK, MOSI and MISO are hard coded for now.
@@ -40,10 +39,13 @@ template< typename SPI_CLK, typename SPI_MOSI, typename SPI_MISO, typename SPI_S
4039
SPI_SS::SetDirWrite();
4140
SPI_SS::Set();
4241
}
43-
42+
#elif defined(ARDUINO_SAM_DUE) && defined(__SAM3X8E__)
43+
static void init() {
44+
SPI_SS::SetDirWrite();
45+
SPI_SS::Set();
46+
SPI.begin();
47+
}
4448
#else
45-
public:
46-
4749
static void init() {
4850
//uint8_t tmp;
4951
SPI_CLK::SetDirWrite();
@@ -67,8 +69,10 @@ typedef SPi< Pb1, Pb2, Pb3, Pb0 > spi;
6769
typedef SPi< Pb5, Pb3, Pb4, Pb2 > spi;
6870
#elif defined(__AVR_ATmega644__) || defined(__AVR_ATmega644P__) || defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__)
6971
typedef SPi< Pb7, Pb5, Pb6, Pb4 > spi;
70-
#elif defined(__MK20DX128__) || defined(__MK20DX256__)
72+
#elif defined(CORE_TEENSY) && (defined(__MK20DX128__) || defined(__MK20DX256__))
7173
typedef SPi< P13, P11, P12, P10 > spi;
74+
#elif defined(ARDUINO_SAM_DUE) && defined(__SAM3X8E__)
75+
typedef SPi< P76, P75, P74, P10 > spi;
7276
#else
7377
#error "No SPI entry in usbhost.h"
7478
#endif
@@ -130,6 +134,9 @@ void MAX3421e< SPI_SS, INTR >::regWr(uint8_t reg, uint8_t data) {
130134
c[0] = reg | 0x02;
131135
c[1] = data;
132136
spi4teensy3::send(c, 2);
137+
#elif defined(ARDUINO_SAM_DUE) && defined(__SAM3X8E__)
138+
SPI.transfer(reg | 0x02);
139+
SPI.transfer(data);
133140
#else
134141
SPDR = (reg | 0x02);
135142
while(!(SPSR & (1 << SPIF)));
@@ -151,6 +158,13 @@ uint8_t* MAX3421e< SPI_SS, INTR >::bytesWr(uint8_t reg, uint8_t nbytes, uint8_t*
151158
spi4teensy3::send(reg | 0x02);
152159
spi4teensy3::send(data_p, nbytes);
153160
data_p += nbytes;
161+
#elif defined(ARDUINO_SAM_DUE) && defined(__SAM3X8E__)
162+
SPI.transfer(reg | 0x02);
163+
while(nbytes) {
164+
SPI.transfer(*data_p);
165+
nbytes--;
166+
data_p++; // advance data pointer
167+
}
154168
#else
155169
SPDR = (reg | 0x02); //set WR bit and send register number
156170
while(nbytes) {
@@ -186,6 +200,10 @@ uint8_t MAX3421e< SPI_SS, INTR >::regRd(uint8_t reg) {
186200
spi4teensy3::send(reg);
187201
uint8_t rv = spi4teensy3::receive();
188202
SPI_SS::Set();
203+
#elif defined(ARDUINO_SAM_DUE) && defined(__SAM3X8E__)
204+
SPI.transfer(reg);
205+
uint8_t rv = SPI.transfer(0);
206+
SPI_SS::Set();
189207
#else
190208
SPDR = reg;
191209
while(!(SPSR & (1 << SPIF)));
@@ -208,6 +226,12 @@ uint8_t* MAX3421e< SPI_SS, INTR >::bytesRd(uint8_t reg, uint8_t nbytes, uint8_t*
208226
spi4teensy3::send(reg);
209227
spi4teensy3::receive(data_p, nbytes);
210228
data_p += nbytes;
229+
#elif defined(ARDUINO_SAM_DUE) && defined(__SAM3X8E__)
230+
SPI.transfer(reg);
231+
while(nbytes) {
232+
*data_p++ = SPI.transfer(0);
233+
nbytes--;
234+
}
211235
#else
212236
SPDR = reg;
213237
while(!(SPSR & (1 << SPIF))); //wait

0 commit comments

Comments
 (0)