From 39b6c3b124f95edbd173d0cb7ef7462aa9be5dbc Mon Sep 17 00:00:00 2001 From: Kristian Lauszus Date: Tue, 25 Feb 2014 11:47:59 +0100 Subject: [PATCH] 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