From 39b6c3b124f95edbd173d0cb7ef7462aa9be5dbc Mon Sep 17 00:00:00 2001 From: Kristian Lauszus Date: Tue, 25 Feb 2014 11:47:59 +0100 Subject: [PATCH 1/8] Added support for the Arduino Due Untested --- UsbCore.h | 2 +- avrpins.h | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++- settings.h | 9 ++++++--- usbhost.h | 36 +++++++++++++++++++++++++++++------ 4 files changed, 91 insertions(+), 11 deletions(-) diff --git a/UsbCore.h b/UsbCore.h index 9a09d804..444700a6 100644 --- a/UsbCore.h +++ b/UsbCore.h @@ -24,7 +24,7 @@ typedef MAX3421e MAX3421E; // Arduino Mega ADK #elif defined(ARDUINO_AVR_BALANDUINO) typedef MAX3421e MAX3421E; // Balanduino #else -typedef MAX3421e MAX3421E; // Official Arduinos (UNO, Duemilanove, Mega, 2560, Leonardo etc.) or Teensy 2.0 and 3.0 +typedef MAX3421e MAX3421E; // Official Arduinos (UNO, Duemilanove, Mega, 2560, Leonardo, Due etc.) or Teensy 2.0 and 3.0 #endif /* Common setup data constant combinations */ diff --git a/avrpins.h b/avrpins.h index bdc99ba6..2bfcdc10 100644 --- a/avrpins.h +++ b/avrpins.h @@ -749,11 +749,13 @@ public: #endif // __AVR__ -#if defined(__arm__) && defined(CORE_TEENSY) +#if defined(__arm__) // pointers are 32 bits on ARM #define pgm_read_pointer(p) pgm_read_dword(p) +#if defined(CORE_TEENSY) && (defined(__MK20DX128__) || defined(__MK20DX256__)) + #include "core_pins.h" #include "avr_emulation.h" @@ -819,6 +821,57 @@ MAKE_PIN(P33, CORE_PIN33_PORTREG, CORE_PIN33_BIT, CORE_PIN33_CONFIG); #undef MAKE_PIN +#elif defined(ARDUINO_SAM_DUE) && defined(__SAM3X8E__) + +// Disable interrupts +// Enable the pull up resistor +// Set to INPUT +// Enable PIO + +// Disable interrupts +// Disable the pull up resistor +// Set to OUTPUT +// Enable PIO + +#define MAKE_PIN(className, baseReg, pinMask) \ +class className { \ +public: \ + static void Set() { \ + baseReg->PIO_SODR = pinMask; \ + } \ + static void Clear() { \ + baseReg->PIO_CODR = pinMask; \ + } \ + static void SetDirRead() { \ + baseReg->PIO_IDR = pinMask ; \ + baseReg->PIO_PUER = pinMask; \ + baseReg->PIO_ODR = pinMask; \ + baseReg->PIO_PER = pinMask; \ + } \ + static void SetDirWrite() { \ + baseReg->PIO_IDR = pinMask ; \ + baseReg->PIO_PUDR = pinMask; \ + baseReg->PIO_OER = pinMask; \ + baseReg->PIO_PER = pinMask; \ + } \ + static uint8_t IsSet() { \ + return baseReg->PIO_PDSR & pinMask; \ + } \ +}; + +MAKE_PIN(P9, PIOC, PIO_PC21); // INT +MAKE_PIN(P10, PIOC, PIO_PC29); // SS +MAKE_PIN(P74, PIOA, PIO_PA25); // MISO +MAKE_PIN(P75, PIOA, PIO_PA26); // MOSI +MAKE_PIN(P76, PIOA, PIO_PA27); // CLK + +#undef MAKE_PIN + +#else +#error "Please define board in avrpins.h" + +#endif + #endif // __arm__ #endif //_avrpins_h_ diff --git a/settings.h b/settings.h index 424a5bcf..c6d78ad4 100644 --- a/settings.h +++ b/settings.h @@ -68,7 +68,6 @@ // No user serviceable parts below this line. // DO NOT change anything below here unless you are a developer! -// When will we drop support for the older bug-ridden stuff? #if defined(ARDUINO) && ARDUINO >=100 #include #else @@ -79,7 +78,7 @@ #define F(str) (str) #endif -#ifdef __GNUC__ +#if defined(__GNUC__) && defined(__AVR__) #ifndef GCC_VERSION #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) #endif @@ -110,10 +109,14 @@ #define XMEM_RELEASE_SPI() (void(0)) #endif -#if defined(__MK20DX128__) || defined(__MK20DX256__) +#if defined(CORE_TEENSY) && (defined(__MK20DX128__) || defined(__MK20DX256__)) #define USING_SPI4TEENSY3 USE_SPI4TEENSY3 #else #define USING_SPI4TEENSY3 0 #endif +#if defined(ARDUINO_SAM_DUE) && defined(__SAM3X8E__) +#include // Use the Arduino SPI library for the Arduino Due +#endif + #endif /* SETTINGS_H */ diff --git a/usbhost.h b/usbhost.h index adfff592..e5cd4188 100644 --- a/usbhost.h +++ b/usbhost.h @@ -29,9 +29,8 @@ e-mail : support@circuitsathome.com /* SPI initialization */ template< typename SPI_CLK, typename SPI_MOSI, typename SPI_MISO, typename SPI_SS > class SPi { -#if USING_SPI4TEENSY3 public: - +#if USING_SPI4TEENSY3 static void init() { // spi4teensy3 inits everything for us, except /SS // CLK, MOSI and MISO are hard coded for now. @@ -40,10 +39,13 @@ public: SPI_SS::SetDirWrite(); SPI_SS::Set(); } - +#elif defined(ARDUINO_SAM_DUE) && defined(__SAM3X8E__) + static void init() { + SPI_SS::SetDirWrite(); + SPI_SS::Set(); + SPI.begin(); + } #else -public: - static void init() { //uint8_t tmp; SPI_CLK::SetDirWrite(); @@ -67,8 +69,10 @@ typedef SPi< Pb1, Pb2, Pb3, Pb0 > spi; typedef SPi< Pb5, Pb3, Pb4, Pb2 > spi; #elif defined(__AVR_ATmega644__) || defined(__AVR_ATmega644P__) || defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__) typedef SPi< Pb7, Pb5, Pb6, Pb4 > spi; -#elif defined(__MK20DX128__) || defined(__MK20DX256__) +#elif defined(CORE_TEENSY) && (defined(__MK20DX128__) || defined(__MK20DX256__)) typedef SPi< P13, P11, P12, P10 > spi; +#elif defined(ARDUINO_SAM_DUE) && defined(__SAM3X8E__) +typedef SPi< P76, P75, P74, P10 > spi; #else #error "No SPI entry in usbhost.h" #endif @@ -130,6 +134,9 @@ void MAX3421e< SPI_SS, INTR >::regWr(uint8_t reg, uint8_t data) { c[0] = reg | 0x02; c[1] = data; spi4teensy3::send(c, 2); +#elif defined(ARDUINO_SAM_DUE) && defined(__SAM3X8E__) + SPI.transfer(reg | 0x02); + SPI.transfer(data); #else SPDR = (reg | 0x02); while(!(SPSR & (1 << SPIF))); @@ -151,6 +158,13 @@ uint8_t* MAX3421e< SPI_SS, INTR >::bytesWr(uint8_t reg, uint8_t nbytes, uint8_t* spi4teensy3::send(reg | 0x02); spi4teensy3::send(data_p, nbytes); data_p += nbytes; +#elif defined(ARDUINO_SAM_DUE) && defined(__SAM3X8E__) + SPI.transfer(reg | 0x02); + while(nbytes) { + SPI.transfer(*data_p); + nbytes--; + data_p++; // advance data pointer + } #else SPDR = (reg | 0x02); //set WR bit and send register number while(nbytes) { @@ -186,6 +200,10 @@ uint8_t MAX3421e< SPI_SS, INTR >::regRd(uint8_t reg) { spi4teensy3::send(reg); uint8_t rv = spi4teensy3::receive(); SPI_SS::Set(); +#elif defined(ARDUINO_SAM_DUE) && defined(__SAM3X8E__) + SPI.transfer(reg); + uint8_t rv = SPI.transfer(0); + SPI_SS::Set(); #else SPDR = reg; while(!(SPSR & (1 << SPIF))); @@ -208,6 +226,12 @@ uint8_t* MAX3421e< SPI_SS, INTR >::bytesRd(uint8_t reg, uint8_t nbytes, uint8_t* spi4teensy3::send(reg); spi4teensy3::receive(data_p, nbytes); data_p += nbytes; +#elif defined(ARDUINO_SAM_DUE) && defined(__SAM3X8E__) + SPI.transfer(reg); + while(nbytes) { + *data_p++ = SPI.transfer(0); + nbytes--; + } #else SPDR = reg; while(!(SPSR & (1 << SPIF))); //wait From a24ecb57977e8aad850b7aed635c65b8e9397fc1 Mon Sep 17 00:00:00 2001 From: Kristian Lauszus Date: Tue, 25 Feb 2014 19:32:49 +0100 Subject: [PATCH 2/8] Updated readme and some comments --- README.md | 5 +++-- avrpins.h | 18 ++++++++++-------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 56152496..80715680 100644 --- a/README.md +++ b/README.md @@ -66,8 +66,9 @@ By default serial debugging is disabled. To turn it on simply change ```ENABLE_U Currently the following boards are supported by the library: * All official Arduino AVR boards (Uno, Duemilanove, Mega, Mega 2560, Mega ADK, Leonardo etc.) -* Teensy (Teensy++ 1.0, Teensy 2.0, Teensy++ 2.0, and Teensy 3.0) - * Note if you are using the Teensy 3.0 you should download this SPI library as well: . You should then add ```#include ``` to your .ino file. +* Arduino Due +* Teensy (Teensy++ 1.0, Teensy 2.0, Teensy++ 2.0, and Teensy 3.x) + * Note if you are using the Teensy 3.x you should download this SPI library as well: . You should then add ```#include ``` to your .ino file. * Balanduino * Sanguino * Black Widdow diff --git a/avrpins.h b/avrpins.h index 2bfcdc10..e75042b2 100644 --- a/avrpins.h +++ b/avrpins.h @@ -823,15 +823,17 @@ MAKE_PIN(P33, CORE_PIN33_PORTREG, CORE_PIN33_BIT, CORE_PIN33_CONFIG); #elif defined(ARDUINO_SAM_DUE) && defined(__SAM3X8E__) -// Disable interrupts -// Enable the pull up resistor -// Set to INPUT -// Enable PIO +// SetDirRead: +// Disable interrupts +// Enable the pull up resistor +// Set to INPUT +// Enable PIO -// Disable interrupts -// Disable the pull up resistor -// Set to OUTPUT -// Enable PIO +// SetDirWrite: +// Disable interrupts +// Disable the pull up resistor +// Set to OUTPUT +// Enable PIO #define MAKE_PIN(className, baseReg, pinMask) \ class className { \ From ce81146f2b58fded9e08371fc2b3cbbcd8c57c5f Mon Sep 17 00:00:00 2001 From: Kristian Lauszus Date: Wed, 26 Feb 2014 23:43:40 +0100 Subject: [PATCH 3/8] Disable pull up resistor when using input on the Due --- avrpins.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/avrpins.h b/avrpins.h index e75042b2..d013851a 100644 --- a/avrpins.h +++ b/avrpins.h @@ -825,7 +825,7 @@ MAKE_PIN(P33, CORE_PIN33_PORTREG, CORE_PIN33_BIT, CORE_PIN33_CONFIG); // SetDirRead: // Disable interrupts -// Enable the pull up resistor +// Disable the pull up resistor // Set to INPUT // Enable PIO @@ -846,7 +846,7 @@ public: \ } \ static void SetDirRead() { \ baseReg->PIO_IDR = pinMask ; \ - baseReg->PIO_PUER = pinMask; \ + baseReg->PIO_PUDR = pinMask; \ baseReg->PIO_ODR = pinMask; \ baseReg->PIO_PER = pinMask; \ } \ From f8379baffea82490374c44cea7c8363704f28a5b Mon Sep 17 00:00:00 2001 From: Kristian Lauszus Date: Wed, 26 Feb 2014 23:46:21 +0100 Subject: [PATCH 4/8] Changed variable name from baseReg to pio --- avrpins.h | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/avrpins.h b/avrpins.h index d013851a..6e074b65 100644 --- a/avrpins.h +++ b/avrpins.h @@ -835,29 +835,29 @@ MAKE_PIN(P33, CORE_PIN33_PORTREG, CORE_PIN33_BIT, CORE_PIN33_CONFIG); // Set to OUTPUT // Enable PIO -#define MAKE_PIN(className, baseReg, pinMask) \ +#define MAKE_PIN(className, pio, pinMask) \ class className { \ public: \ static void Set() { \ - baseReg->PIO_SODR = pinMask; \ + pio->PIO_SODR = pinMask; \ } \ static void Clear() { \ - baseReg->PIO_CODR = pinMask; \ + pio->PIO_CODR = pinMask; \ } \ static void SetDirRead() { \ - baseReg->PIO_IDR = pinMask ; \ - baseReg->PIO_PUDR = pinMask; \ - baseReg->PIO_ODR = pinMask; \ - baseReg->PIO_PER = pinMask; \ + pio->PIO_IDR = pinMask ; \ + pio->PIO_PUDR = pinMask; \ + pio->PIO_ODR = pinMask; \ + pio->PIO_PER = pinMask; \ } \ static void SetDirWrite() { \ - baseReg->PIO_IDR = pinMask ; \ - baseReg->PIO_PUDR = pinMask; \ - baseReg->PIO_OER = pinMask; \ - baseReg->PIO_PER = pinMask; \ + pio->PIO_IDR = pinMask ; \ + pio->PIO_PUDR = pinMask; \ + pio->PIO_OER = pinMask; \ + pio->PIO_PER = pinMask; \ } \ static uint8_t IsSet() { \ - return baseReg->PIO_PDSR & pinMask; \ + return pio->PIO_PDSR & pinMask; \ } \ }; From d8cadc5744e4483ce2161f8fd0c163483e12a997 Mon Sep 17 00:00:00 2001 From: Kristian Lauszus Date: Sun, 2 Mar 2014 16:35:16 +0100 Subject: [PATCH 5/8] Increase SPI speed on the Due to 21MHz --- usbhost.h | 1 + 1 file changed, 1 insertion(+) diff --git a/usbhost.h b/usbhost.h index e5cd4188..172742e5 100644 --- a/usbhost.h +++ b/usbhost.h @@ -44,6 +44,7 @@ public: SPI_SS::SetDirWrite(); SPI_SS::Set(); SPI.begin(); + SPI.setClockDivider(BOARD_SPI_DEFAULT_SS, 4); // Set speed to 84MHz/4=21MHz - the MAX3421E can handle up to 26MHz } #else static void init() { From 76b1c3fb323c5ccbf00199fa8e71aa84e3a6da15 Mon Sep 17 00:00:00 2001 From: Kristian Lauszus Date: Mon, 3 Mar 2014 02:59:09 +0100 Subject: [PATCH 6/8] Added some syntax highlighting in the readme --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 80715680..b964c548 100644 --- a/README.md +++ b/README.md @@ -57,7 +57,7 @@ Documentation for the library can be found at the following link: Date: Thu, 6 Mar 2014 19:25:35 +0100 Subject: [PATCH 7/8] Added information about the need to include the Arduino SPI library when using the Due --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index b964c548..85ef87c7 100644 --- a/README.md +++ b/README.md @@ -66,6 +66,7 @@ By default serial debugging is disabled. To turn it on simply change ```ENABLE_U Currently the following boards are supported by the library: * All official Arduino AVR boards (Uno, Duemilanove, Mega, Mega 2560, Mega ADK, Leonardo etc.) + * If you are using the Arduino Due, then you must include the Arduino SPI library as well like so: ```#include ``` to your .ino file. * Arduino Due * Teensy (Teensy++ 1.0, Teensy 2.0, Teensy++ 2.0, and Teensy 3.x) * Note if you are using the Teensy 3.x you should download this SPI library as well: . You should then add ```#include ``` to your .ino file. From a0661ef244d5d5fae8946236ba806de370a7aeb1 Mon Sep 17 00:00:00 2001 From: Kristian Lauszus Date: Thu, 6 Mar 2014 19:32:17 +0100 Subject: [PATCH 8/8] Typo correction --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 85ef87c7..647a69a9 100644 --- a/README.md +++ b/README.md @@ -66,7 +66,7 @@ By default serial debugging is disabled. To turn it on simply change ```ENABLE_U Currently the following boards are supported by the library: * All official Arduino AVR boards (Uno, Duemilanove, Mega, Mega 2560, Mega ADK, Leonardo etc.) - * If you are using the Arduino Due, then you must include the Arduino SPI library as well like so: ```#include ``` to your .ino file. + * If you are using the Arduino Due, then you must include the Arduino SPI library like so: ```#include ``` in your .ino file. * Arduino Due * Teensy (Teensy++ 1.0, Teensy 2.0, Teensy++ 2.0, and Teensy 3.x) * Note if you are using the Teensy 3.x you should download this SPI library as well: . You should then add ```#include ``` to your .ino file.