Use new Arduino SPI library

See: https://github.com/arduino/Arduino/pull/2223
This commit is contained in:
Kristian Lauszus 2014-08-02 15:06:46 -07:00
parent f349d61dfc
commit 45df70641b
2 changed files with 64 additions and 15 deletions

View file

@ -137,8 +137,8 @@ e-mail : support@circuitsathome.com
#define USING_SPI4TEENSY3 0
#endif
#if defined(ARDUINO_SAM_DUE) && defined(__SAM3X8E__)
#include <SPI.h> // Use the Arduino SPI library for the Arduino Due
#if (defined(ARDUINO_SAM_DUE) && defined(__SAM3X8E__)) || ARDUINO >= 158
#include <SPI.h> // Use the Arduino SPI library for the Arduino Due or if the SPI library with transaction is available
#endif
#endif /* SETTINGS_H */

View file

@ -30,7 +30,11 @@ e-mail : support@circuitsathome.com
/* SPI initialization */
template< typename SPI_CLK, typename SPI_MOSI, typename SPI_MISO, typename SPI_SS > class SPi {
public:
#if USING_SPI4TEENSY3
#if SPI_HAS_TRANSACTION
static void init() {
SPI.begin(); // The SPI library with transaction will take care of setting up the pins - settings is set in beginTransaction()
}
#elif USING_SPI4TEENSY3
static void init() {
// spi4teensy3 inits everything for us, except /SS
// CLK, MOSI and MISO are hard coded for now.
@ -129,8 +133,17 @@ MAX3421e< SPI_SS, INTR >::MAX3421e() {
template< typename SPI_SS, typename INTR >
void MAX3421e< SPI_SS, INTR >::regWr(uint8_t reg, uint8_t data) {
XMEM_ACQUIRE_SPI();
#if SPI_HAS_TRANSACTION
SPI.beginTransaction(SPISettings(26000000, MSBFIRST, SPI_MODE0)); // The MAX3421E can handle up to 26MHz, use MSB First and SPI mode 0
#endif
SPI_SS::Clear();
#if USING_SPI4TEENSY3
#if SPI_HAS_TRANSACTION
uint8_t c[2];
c[0] = reg | 0x02;
c[1] = data;
SPI.transfer(c, 2);
#elif USING_SPI4TEENSY3
uint8_t c[2];
c[0] = reg | 0x02;
c[1] = data;
@ -144,7 +157,11 @@ void MAX3421e< SPI_SS, INTR >::regWr(uint8_t reg, uint8_t data) {
SPDR = data;
while(!(SPSR & (1 << SPIF)));
#endif
SPI_SS::Set();
#if SPI_HAS_TRANSACTION
SPI.endTransaction();
#endif
XMEM_RELEASE_SPI();
return;
};
@ -154,8 +171,16 @@ void MAX3421e< SPI_SS, INTR >::regWr(uint8_t reg, uint8_t data) {
template< typename SPI_SS, typename INTR >
uint8_t* MAX3421e< SPI_SS, INTR >::bytesWr(uint8_t reg, uint8_t nbytes, uint8_t* data_p) {
XMEM_ACQUIRE_SPI();
#if SPI_HAS_TRANSACTION
SPI.beginTransaction(SPISettings(26000000, MSBFIRST, SPI_MODE0)); // The MAX3421E can handle up to 26MHz, use MSB First and SPI mode 0
#endif
SPI_SS::Clear();
#if USING_SPI4TEENSY3
#if SPI_HAS_TRANSACTION
SPI.transfer(reg | 0x02);
SPI.transfer(data_p, nbytes);
data_p += nbytes;
#elif USING_SPI4TEENSY3
spi4teensy3::send(reg | 0x02);
spi4teensy3::send(data_p, nbytes);
data_p += nbytes;
@ -176,7 +201,11 @@ uint8_t* MAX3421e< SPI_SS, INTR >::bytesWr(uint8_t reg, uint8_t nbytes, uint8_t*
}
while(!(SPSR & (1 << SPIF)));
#endif
SPI_SS::Set();
#if SPI_HAS_TRANSACTION
SPI.endTransaction();
#endif
XMEM_RELEASE_SPI();
return ( data_p);
}
@ -196,23 +225,31 @@ void MAX3421e< SPI_SS, INTR >::gpioWr(uint8_t data) {
template< typename SPI_SS, typename INTR >
uint8_t MAX3421e< SPI_SS, INTR >::regRd(uint8_t reg) {
XMEM_ACQUIRE_SPI();
#if SPI_HAS_TRANSACTION
SPI.beginTransaction(SPISettings(26000000, MSBFIRST, SPI_MODE0)); // The MAX3421E can handle up to 26MHz, use MSB First and SPI mode 0
#endif
SPI_SS::Clear();
#if USING_SPI4TEENSY3
spi4teensy3::send(reg);
uint8_t rv = spi4teensy3::receive();
SPI_SS::Set();
#elif defined(ARDUINO_SAM_DUE) && defined(__SAM3X8E__)
#if (defined(ARDUINO_SAM_DUE) && defined(__SAM3X8E__)) || SPI_HAS_TRANSACTION
SPI.transfer(reg);
uint8_t rv = SPI.transfer(0);
uint8_t rv = SPI.transfer(0); // Send empty byte
SPI_SS::Set();
#elif USING_SPI4TEENSY3
spi4teensy3::send(reg);
uint8_t rv = spi4teensy3::receive(); // Send empty byte
SPI_SS::Set();
#else
SPDR = reg;
while(!(SPSR & (1 << SPIF)));
SPDR = 0; //send empty byte
SPDR = 0; // Send empty byte
while(!(SPSR & (1 << SPIF)));
SPI_SS::Set();
uint8_t rv = SPDR;
#endif
#if SPI_HAS_TRANSACTION
SPI.endTransaction();
#endif
XMEM_RELEASE_SPI();
return (rv);
}
@ -222,8 +259,16 @@ uint8_t MAX3421e< SPI_SS, INTR >::regRd(uint8_t reg) {
template< typename SPI_SS, typename INTR >
uint8_t* MAX3421e< SPI_SS, INTR >::bytesRd(uint8_t reg, uint8_t nbytes, uint8_t* data_p) {
XMEM_ACQUIRE_SPI();
#if SPI_HAS_TRANSACTION
SPI.beginTransaction(SPISettings(26000000, MSBFIRST, SPI_MODE0)); // The MAX3421E can handle up to 26MHz, use MSB First and SPI mode 0
#endif
SPI_SS::Clear();
#if USING_SPI4TEENSY3
#if SPI_HAS_TRANSACTION
SPI.transfer(reg);
SPI.transfer(data_p, nbytes);
data_p += nbytes;
#elif USING_SPI4TEENSY3
spi4teensy3::send(reg);
spi4teensy3::receive(data_p, nbytes);
data_p += nbytes;
@ -253,7 +298,11 @@ uint8_t* MAX3421e< SPI_SS, INTR >::bytesRd(uint8_t reg, uint8_t nbytes, uint8_t*
}
#endif
#endif
SPI_SS::Set();
#if SPI_HAS_TRANSACTION
SPI.endTransaction();
#endif
XMEM_RELEASE_SPI();
return ( data_p);
}