Skip to content

Commit 2bb6c97

Browse files
committed
Merge branch 'Due'
2 parents 983a7d6 + a0661ef commit 2bb6c97

File tree

5 files changed

+102
-17
lines changed

5 files changed

+102
-17
lines changed

README.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ Documentation for the library can be found at the following link: <http://felis.
5757

5858
By default serial debugging is disabled. To turn it on simply change ```ENABLE_UHS_DEBUGGING``` to 1 in [settings.h](settings.h) like so:
5959

60-
```
60+
```C++
6161
#define ENABLE_UHS_DEBUGGING 1
6262
```
6363
@@ -66,8 +66,10 @@ By default serial debugging is disabled. To turn it on simply change ```ENABLE_U
6666
Currently the following boards are supported by the library:
6767
6868
* All official Arduino AVR boards (Uno, Duemilanove, Mega, Mega 2560, Mega ADK, Leonardo etc.)
69-
* Teensy (Teensy++ 1.0, Teensy 2.0, Teensy++ 2.0, and Teensy 3.0)
70-
* Note if you are using the Teensy 3.0 you should download this SPI library as well: <https://github.com/xxxajk/spi4teensy3>. You should then add ```#include <spi4teensy3.h>``` to your .ino file.
69+
* If you are using the Arduino Due, then you must include the Arduino SPI library like so: ```#include <SPI.h>``` in your .ino file.
70+
* Arduino Due
71+
* Teensy (Teensy++ 1.0, Teensy 2.0, Teensy++ 2.0, and Teensy 3.x)
72+
* Note if you are using the Teensy 3.x you should download this SPI library as well: <https://github.com/xxxajk/spi4teensy3>. You should then add ```#include <spi4teensy3.h>``` to your .ino file.
7173
* Balanduino
7274
* Sanguino
7375
* Black Widdow
@@ -203,15 +205,15 @@ The [Wii](Wii.cpp) library support the Wiimote, but also the Nunchuch and Motion
203205
204206
First you have to pair with the controller, this is done automatically by the library if you create the instance like so:
205207
206-
```
207-
WII Wii(&Btd,PAIR);
208+
```C++
209+
WII Wii(&Btd, PAIR);
208210
```
209211

210212
And then press 1 & 2 at once on the Wiimote or press sync if you are using a Wii U Pro Controller.
211213

212214
After that you can simply create the instance like so:
213215

214-
```
216+
```C++
215217
WII Wii(&Btd);
216218
```
217219

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

824879
#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: 31 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,14 @@ 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+
SPI.setClockDivider(BOARD_SPI_DEFAULT_SS, 4); // Set speed to 84MHz/4=21MHz - the MAX3421E can handle up to 26MHz
48+
}
4449
#else
45-
public:
46-
4750
static void init() {
4851
//uint8_t tmp;
4952
SPI_CLK::SetDirWrite();
@@ -67,8 +70,10 @@ typedef SPi< Pb1, Pb2, Pb3, Pb0 > spi;
6770
typedef SPi< Pb5, Pb3, Pb4, Pb2 > spi;
6871
#elif defined(__AVR_ATmega644__) || defined(__AVR_ATmega644P__) || defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__)
6972
typedef SPi< Pb7, Pb5, Pb6, Pb4 > spi;
70-
#elif defined(__MK20DX128__) || defined(__MK20DX256__)
73+
#elif defined(CORE_TEENSY) && (defined(__MK20DX128__) || defined(__MK20DX256__))
7174
typedef SPi< P13, P11, P12, P10 > spi;
75+
#elif defined(ARDUINO_SAM_DUE) && defined(__SAM3X8E__)
76+
typedef SPi< P76, P75, P74, P10 > spi;
7277
#else
7378
#error "No SPI entry in usbhost.h"
7479
#endif
@@ -130,6 +135,9 @@ void MAX3421e< SPI_SS, INTR >::regWr(uint8_t reg, uint8_t data) {
130135
c[0] = reg | 0x02;
131136
c[1] = data;
132137
spi4teensy3::send(c, 2);
138+
#elif defined(ARDUINO_SAM_DUE) && defined(__SAM3X8E__)
139+
SPI.transfer(reg | 0x02);
140+
SPI.transfer(data);
133141
#else
134142
SPDR = (reg | 0x02);
135143
while(!(SPSR & (1 << SPIF)));
@@ -151,6 +159,13 @@ uint8_t* MAX3421e< SPI_SS, INTR >::bytesWr(uint8_t reg, uint8_t nbytes, uint8_t*
151159
spi4teensy3::send(reg | 0x02);
152160
spi4teensy3::send(data_p, nbytes);
153161
data_p += nbytes;
162+
#elif defined(ARDUINO_SAM_DUE) && defined(__SAM3X8E__)
163+
SPI.transfer(reg | 0x02);
164+
while(nbytes) {
165+
SPI.transfer(*data_p);
166+
nbytes--;
167+
data_p++; // advance data pointer
168+
}
154169
#else
155170
SPDR = (reg | 0x02); //set WR bit and send register number
156171
while(nbytes) {
@@ -186,6 +201,10 @@ uint8_t MAX3421e< SPI_SS, INTR >::regRd(uint8_t reg) {
186201
spi4teensy3::send(reg);
187202
uint8_t rv = spi4teensy3::receive();
188203
SPI_SS::Set();
204+
#elif defined(ARDUINO_SAM_DUE) && defined(__SAM3X8E__)
205+
SPI.transfer(reg);
206+
uint8_t rv = SPI.transfer(0);
207+
SPI_SS::Set();
189208
#else
190209
SPDR = reg;
191210
while(!(SPSR & (1 << SPIF)));
@@ -208,6 +227,12 @@ uint8_t* MAX3421e< SPI_SS, INTR >::bytesRd(uint8_t reg, uint8_t nbytes, uint8_t*
208227
spi4teensy3::send(reg);
209228
spi4teensy3::receive(data_p, nbytes);
210229
data_p += nbytes;
230+
#elif defined(ARDUINO_SAM_DUE) && defined(__SAM3X8E__)
231+
SPI.transfer(reg);
232+
while(nbytes) {
233+
*data_p++ = SPI.transfer(0);
234+
nbytes--;
235+
}
211236
#else
212237
SPDR = reg;
213238
while(!(SPSR & (1 << SPIF))); //wait

0 commit comments

Comments
 (0)