Browse Source

Unified SPIClass for Teensy LC (thanks KurtE)

main
PaulStoffregen 7 years ago
parent
commit
70dc198a04
2 changed files with 192 additions and 302 deletions
  1. +97
    -38
      SPI.cpp
  2. +95
    -264
      SPI.h

+ 97
- 38
SPI.cpp View File



full_mask = (hardware().queue_size-1) << 12; full_mask = (hardware().queue_size-1) << 12;
while (count > 0) { while (count > 0) {
// Push out the next byte;
// Push out the next byte
uint16_t w = (*p_write++) << 8; uint16_t w = (*p_write++) << 8;
w |= *p_write++; w |= *p_write++;
if (lsbfirst) w = __builtin_bswap16(w); if (lsbfirst) w = __builtin_bswap16(w);


#elif defined(__arm__) && defined(TEENSYDUINO) && defined(KINETISL) #elif defined(__arm__) && defined(TEENSYDUINO) && defined(KINETISL)


SPIClass SPI;
SPI1Class SPI1;
void _spi_dma_rxISR0(void) {/*SPI.dma_rxisr();*/}
const SPIClass::SPI_Hardware_t SPIClass::spi0_hardware = {
SIM_SCGC4, SIM_SCGC4_SPI0,
0, // BR index 0
DMAMUX_SOURCE_SPI0_TX, DMAMUX_SOURCE_SPI0_RX, _spi_dma_rxISR0,
12, 8,
2, 2,
11, 7,
2, 2,
13, 14,
2, 2,
10, 2,
2, 2,
0x1, 0x1
};
SPIClass SPI((uintptr_t)&KINETISL_SPI0, (uintptr_t)&SPIClass::spi0_hardware);

void _spi_dma_rxISR1(void) {/*SPI1.dma_rxisr();*/}
const SPIClass::SPI_Hardware_t SPIClass::spi1_hardware = {
SIM_SCGC4, SIM_SCGC4_SPI1,
1, // BR index 1 in SPI Settings
DMAMUX_SOURCE_SPI1_TX, DMAMUX_SOURCE_SPI1_RX, _spi_dma_rxISR1,
1, 5,
2, 2,
0, 21,
2, 2,
20, 255,
2, 0,
6, 255,
2, 0,
0x1, 0
};
SPIClass SPI1((uintptr_t)&KINETISL_SPI1, (uintptr_t)&SPIClass::spi1_hardware);


uint32_t SPIClass::interruptMask = 0;
uint32_t SPIClass::interruptSave = 0;
uint32_t SPI1Class::interruptMask = 0;
uint32_t SPI1Class::interruptSave = 0;
#ifdef SPI_TRANSACTION_MISMATCH_LED
uint8_t SPIClass::inTransactionFlag = 0;
uint8_t SPI1Class::inTransactionFlag = 0;
#endif


void SPIClass::begin() void SPIClass::begin()
{ {
SIM_SCGC4 |= SIM_SCGC4_SPI0;
SPI0_C1 = SPI_C1_SPE | SPI_C1_MSTR;
SPI0_C2 = 0;
uint8_t tmp __attribute__((unused)) = SPI0_S;
SPCR.enable_pins(); // pins managed by SPCRemulation in avr_emulation.h
volatile uint32_t *reg;

hardware().clock_gate_register |= hardware().clock_gate_mask;
port().C1 = SPI_C1_SPE | SPI_C1_MSTR;
port().C2 = 0;
uint8_t tmp __attribute__((unused)) = port().S;
reg = portConfigRegister(hardware().mosi_pin[mosi_pin_index]);
*reg = PORT_PCR_MUX(hardware().mosi_mux[mosi_pin_index]);
reg = portConfigRegister(hardware().miso_pin[miso_pin_index]);
*reg = PORT_PCR_MUX(hardware().miso_mux[miso_pin_index]);
reg = portConfigRegister(hardware().sck_pin[sck_pin_index]);
*reg = PORT_PCR_MUX(hardware().sck_mux[sck_pin_index]);
} }


void SPIClass::end() { void SPIClass::end() {
SPCR.disable_pins();
SPI0_C1 = 0;
volatile uint32_t *reg;

reg = portConfigRegister(hardware().mosi_pin[mosi_pin_index]);
*reg = 0;
reg = portConfigRegister(hardware().miso_pin[miso_pin_index]);
*reg = 0;
reg = portConfigRegister(hardware().sck_pin[sck_pin_index]);
*reg = 0;
port().C1 = 0;
} }


const uint16_t SPISettings::br_div_table[30] = { const uint16_t SPISettings::br_div_table[30] = {
SPI_BR_SPPR(5) | SPI_BR_SPR(6) SPI_BR_SPPR(5) | SPI_BR_SPR(6)
}; };


// setCS() is not intended for use from normal Arduino programs/sketches.
uint8_t SPIClass::setCS(uint8_t pin)
void SPIClass::setMOSI(uint8_t pin)
{ {
switch (pin) {
case 10: CORE_PIN10_CONFIG = PORT_PCR_MUX(2); return 0x01; // PTC4
case 2: CORE_PIN2_CONFIG = PORT_PCR_MUX(2); return 0x01; // PTD0
if (pin != hardware().mosi_pin[mosi_pin_index]) {
for (unsigned int i = 0; i < sizeof(hardware().mosi_pin); i++) {
if (pin == hardware().mosi_pin[i] ) {
mosi_pin_index = i;
return;
}
}
} }
return 0;
} }


void SPI1Class::begin()
void SPIClass::setMISO(uint8_t pin)
{ {
SIM_SCGC4 |= SIM_SCGC4_SPI1;
SPI1_C1 = SPI_C1_SPE | SPI_C1_MSTR;
SPI1_C2 = 0;
uint8_t tmp __attribute__((unused)) = SPI1_S;
SPCR1.enable_pins(); // pins managed by SPCRemulation in avr_emulation.h
if (pin != hardware().miso_pin[miso_pin_index]) {
for (unsigned int i = 0; i < sizeof(hardware().miso_pin); i++) {
if (pin == hardware().miso_pin[i] ) {
miso_pin_index = i;
return;
}
}
}
}

void SPIClass::setSCK(uint8_t pin)
{
if (pin != hardware().sck_pin[sck_pin_index]) {
for (unsigned int i = 0; i < sizeof(hardware().sck_pin); i++) {
if (pin == hardware().sck_pin[i] ) {
sck_pin_index = i;
return;
}
}
}
} }


void SPI1Class::end() {
SPCR1.disable_pins();
SPI1_C1 = 0;
bool SPIClass::pinIsChipSelect(uint8_t pin)
{
for (unsigned int i = 0; i < sizeof(hardware().cs_pin); i++) {
if (pin == hardware().cs_pin[i]) return hardware().cs_mask[i];
}
return 0;
} }


// setCS() is not intended for use from normal Arduino programs/sketches. // setCS() is not intended for use from normal Arduino programs/sketches.
uint8_t SPI1Class::setCS(uint8_t pin)
uint8_t SPIClass::setCS(uint8_t pin)
{ {
switch (pin) {
case 6: CORE_PIN6_CONFIG = PORT_PCR_MUX(2); return 0x01; // PTD4
for (unsigned int i = 0; i < sizeof(hardware().cs_pin); i++) {
if (pin == hardware().cs_pin[i]) {
volatile uint32_t *reg = portConfigRegister(pin);
*reg = PORT_PCR_MUX(hardware().cs_mux[i]);
return hardware().cs_mask[i];
}
} }
return 0; return 0;
} }







#endif #endif




+ 95
- 264
SPI.h View File

static const uint32_t ctar_clock_table[23]; static const uint32_t ctar_clock_table[23];
uint32_t ctar; uint32_t ctar;
friend class SPIClass; friend class SPIClass;
#if defined(__MK64FX512__) || defined(__MK66FX1M0__)
friend class SPI1Class;
friend class SPI2Class;
#endif
}; };




constexpr SPIClass(uintptr_t myport, uintptr_t myhardware) constexpr SPIClass(uintptr_t myport, uintptr_t myhardware)
: port_addr(myport), hardware_addr(myhardware) { : port_addr(myport), hardware_addr(myhardware) {
} }
/*constexpr SPIClass() : port_addr((uintptr_t)&KINETISK_SPI0), hardware(spi0_hardware) {
}*/
// Initialize the SPI library // Initialize the SPI library
void begin(); void begin();


void updateCTAR(uint32_t ctar); void updateCTAR(uint32_t ctar);
uintptr_t port_addr; uintptr_t port_addr;
uintptr_t hardware_addr; uintptr_t hardware_addr;
//const SPI_Hardware_t &hardware;
uint8_t miso_pin_index = 0; uint8_t miso_pin_index = 0;
uint8_t mosi_pin_index = 0; uint8_t mosi_pin_index = 0;
uint8_t sck_pin_index = 0; uint8_t sck_pin_index = 0;
if (clock >= F_BUS / br_div_table[i]) break; if (clock >= F_BUS / br_div_table[i]) break;
} }
} }
br0 = c;
br[0] = c;
if (__builtin_constant_p(clock)) { if (__builtin_constant_p(clock)) {
if (clock >= (F_PLL/2) / 2) { c = SPI_BR_SPPR(0) | SPI_BR_SPR(0); if (clock >= (F_PLL/2) / 2) { c = SPI_BR_SPPR(0) | SPI_BR_SPR(0);
} else if (clock >= (F_PLL/2) / 4) { c = SPI_BR_SPPR(1) | SPI_BR_SPR(0); } else if (clock >= (F_PLL/2) / 4) { c = SPI_BR_SPPR(1) | SPI_BR_SPR(0);
if (clock >= (F_PLL/2) / br_div_table[i]) break; if (clock >= (F_PLL/2) / br_div_table[i]) break;
} }
} }
br1 = c;
br[1] = c;
} }
static const uint8_t br_clock_table[30]; static const uint8_t br_clock_table[30];
static const uint16_t br_div_table[30]; static const uint16_t br_div_table[30];
uint8_t c1, br0, br1;
uint8_t c1, br[2];
friend class SPIClass; friend class SPIClass;
friend class SPI1Class;
}; };




class SPIClass { // Teensy-LC class SPIClass { // Teensy-LC
public: public:
// Initialize the SPI library
static void begin();

// If SPI is to used from within an interrupt, this function registers
// that interrupt with the SPI library, so beginTransaction() can
// prevent conflicts. The input interruptNumber is the number used
// with attachInterrupt. If SPI is used from a different interrupt
// (eg, a timer), interruptNumber should be 255.
static void usingInterrupt(uint8_t n) {
if (n == 3 || n == 4) {
usingInterrupt(IRQ_PORTA);
} else if ((n >= 2 && n <= 15) || (n >= 20 && n <= 23)) {
usingInterrupt(IRQ_PORTCD);
}
}
static void usingInterrupt(IRQ_NUMBER_t interruptName) {
uint32_t n = (uint32_t)interruptName;
if (n < NVIC_NUM_INTERRUPTS) interruptMask |= (1 << n);
}
static void notUsingInterrupt(IRQ_NUMBER_t interruptName) {
uint32_t n = (uint32_t)interruptName;
if (n < NVIC_NUM_INTERRUPTS) interruptMask &= ~(1 << n);
}

// Before using SPI.transfer() or asserting chip select pins,
// this function is used to gain exclusive access to the SPI bus
// and configure the correct settings.
inline static void beginTransaction(SPISettings settings) {
if (interruptMask) {
__disable_irq();
interruptSave = NVIC_ICER0 & interruptMask;
NVIC_ICER0 = interruptSave;
__enable_irq();
}
#ifdef SPI_TRANSACTION_MISMATCH_LED
if (inTransactionFlag) {
pinMode(SPI_TRANSACTION_MISMATCH_LED, OUTPUT);
digitalWrite(SPI_TRANSACTION_MISMATCH_LED, HIGH);
}
inTransactionFlag = 1;
#endif
SPI0_C1 = settings.c1;
SPI0_BR = settings.br0;
}

// Write to the SPI bus (MOSI pin) and also receive (MISO pin)
inline static uint8_t transfer(uint8_t data) {
SPI0_DL = data;
while (!(SPI0_S & SPI_S_SPRF)) ; // wait
return SPI0_DL;
}
inline static uint16_t transfer16(uint16_t data) {
SPI0_C2 = SPI_C2_SPIMODE;
SPI0_S;
SPI0_DL = data;
SPI0_DH = data >> 8;
while (!(SPI0_S & SPI_S_SPRF)) ; // wait
uint16_t r = SPI0_DL | (SPI0_DH << 8);
SPI0_C2 = 0;
SPI0_S;
return r;
}
inline static void transfer(void *buf, size_t count) {
if (count == 0) return;
uint8_t *p = (uint8_t *)buf;
while (!(SPI0_S & SPI_S_SPTEF)) ; // wait
SPI0_DL = *p;
while (--count > 0) {
uint8_t out = *(p + 1);
while (!(SPI0_S & SPI_S_SPTEF)) ; // wait
__disable_irq();
SPI0_DL = out;
while (!(SPI0_S & SPI_S_SPRF)) ; // wait
uint8_t in = SPI0_DL;
__enable_irq();
*p++ = in;
}
while (!(SPI0_S & SPI_S_SPRF)) ; // wait
*p = SPDR;
}

// After performing a group of transfers and releasing the chip select
// signal, this function allows others to access the SPI bus
inline static void endTransaction(void) {
#ifdef SPI_TRANSACTION_MISMATCH_LED
if (!inTransactionFlag) {
pinMode(SPI_TRANSACTION_MISMATCH_LED, OUTPUT);
digitalWrite(SPI_TRANSACTION_MISMATCH_LED, HIGH);
}
inTransactionFlag = 0;
#endif
if (interruptMask) {
NVIC_ISER0 = interruptSave;
}
}

// Disable the SPI bus
static void end();

// This function is deprecated. New applications should use
// beginTransaction() to configure SPI settings.
static void setBitOrder(uint8_t bitOrder) {
uint8_t c = SPI0_C1 | SPI_C1_SPE;
if (bitOrder == LSBFIRST) c |= SPI_C1_LSBFE;
else c &= ~SPI_C1_LSBFE;
SPI0_C1 = c;
}

// This function is deprecated. New applications should use
// beginTransaction() to configure SPI settings.
static void setDataMode(uint8_t dataMode) {
uint8_t c = SPI0_C1 | SPI_C1_SPE;
if (dataMode & 0x04) c |= SPI_C1_CPHA;
else c &= ~SPI_C1_CPHA;
if (dataMode & 0x08) c |= SPI_C1_CPOL;
else c &= ~SPI_C1_CPOL;
SPI0_C1 = c;
}

// This function is deprecated. New applications should use
// beginTransaction() to configure SPI settings.
inline static void setClockDivider(uint8_t clockDiv) {
if (clockDiv == SPI_CLOCK_DIV2) {
SPI0_BR = (SPISettings(12000000, MSBFIRST, SPI_MODE0).br0);
} else if (clockDiv == SPI_CLOCK_DIV4) {
SPI0_BR = (SPISettings(4000000, MSBFIRST, SPI_MODE0).br0);
} else if (clockDiv == SPI_CLOCK_DIV8) {
SPI0_BR = (SPISettings(2000000, MSBFIRST, SPI_MODE0).br0);
} else if (clockDiv == SPI_CLOCK_DIV16) {
SPI0_BR = (SPISettings(1000000, MSBFIRST, SPI_MODE0).br0);
} else if (clockDiv == SPI_CLOCK_DIV32) {
SPI0_BR = (SPISettings(500000, MSBFIRST, SPI_MODE0).br0);
} else if (clockDiv == SPI_CLOCK_DIV64) {
SPI0_BR = (SPISettings(250000, MSBFIRST, SPI_MODE0).br0);
} else { /* clockDiv == SPI_CLOCK_DIV128 */
SPI0_BR = (SPISettings(125000, MSBFIRST, SPI_MODE0).br0);
}
}

// These undocumented functions should not be used. SPI.transfer()
// polls the hardware flag which is automatically cleared as the
// AVR responds to SPI's interrupt
inline static void attachInterrupt() { }
inline static void detachInterrupt() { }

// Teensy LC can use alternate pins for these 3 SPI signals.
inline static void setMOSI(uint8_t pin) __attribute__((always_inline)) {
SPCR.setMOSI(pin);
}
inline static void setMISO(uint8_t pin) __attribute__((always_inline)) {
SPCR.setMISO(pin);
}
inline static void setSCK(uint8_t pin) __attribute__((always_inline)) {
SPCR.setSCK(pin);
}
// return true if "pin" has special chip select capability
static bool pinIsChipSelect(uint8_t pin) { return (pin == 10 || pin == 2); }
// return true if both pin1 and pin2 have independent chip select capability
static bool pinIsChipSelect(uint8_t pin1, uint8_t pin2) { return false; }
// configure a pin for chip select and return its SPI_MCR_PCSIS bitmask
// setCS() is a special function, not intended for use from normal Arduino
// programs/sketches. See the ILI3941_t3 library for an example.
static uint8_t setCS(uint8_t pin);

private:
static uint32_t interruptMask;
static uint32_t interruptSave;
#ifdef SPI_TRANSACTION_MISMATCH_LED
static uint8_t inTransactionFlag;
#endif
};



class SPI1Class {
static const uint8_t CNT_MISO_PINS = 2;
static const uint8_t CNT_MMOSI_PINS = 2;
static const uint8_t CNT_SCK_PINS = 2;
static const uint8_t CNT_CS_PINS = 2;
typedef struct {
volatile uint32_t &clock_gate_register;
uint32_t clock_gate_mask;
uint8_t br_index;
uint8_t tx_dma_channel;
uint8_t rx_dma_channel;
void (*dma_isr)();
uint8_t miso_pin[CNT_MISO_PINS];
uint8_t miso_mux[CNT_MISO_PINS];
uint8_t mosi_pin[CNT_MMOSI_PINS];
uint8_t mosi_mux[CNT_MMOSI_PINS];
uint8_t sck_pin[CNT_SCK_PINS];
uint8_t sck_mux[CNT_SCK_PINS];
uint8_t cs_pin[CNT_CS_PINS];
uint8_t cs_mux[CNT_CS_PINS];
uint8_t cs_mask[CNT_CS_PINS];
} SPI_Hardware_t;
static const SPI_Hardware_t spi0_hardware;
static const SPI_Hardware_t spi1_hardware;
public: public:
constexpr SPIClass(uintptr_t myport, uintptr_t myhardware)
: port_addr(myport), hardware_addr(myhardware) {
}
// Initialize the SPI library // Initialize the SPI library
static void begin();
void begin();


// If SPI is to used from within an interrupt, this function registers // If SPI is to used from within an interrupt, this function registers
// that interrupt with the SPI library, so beginTransaction() can // that interrupt with the SPI library, so beginTransaction() can
// prevent conflicts. The input interruptNumber is the number used // prevent conflicts. The input interruptNumber is the number used
// with attachInterrupt. If SPI is used from a different interrupt // with attachInterrupt. If SPI is used from a different interrupt
// (eg, a timer), interruptNumber should be 255. // (eg, a timer), interruptNumber should be 255.
static void usingInterrupt(uint8_t n) {
void usingInterrupt(uint8_t n) {
if (n == 3 || n == 4) { if (n == 3 || n == 4) {
usingInterrupt(IRQ_PORTA); usingInterrupt(IRQ_PORTA);
} else if ((n >= 2 && n <= 15) || (n >= 20 && n <= 23)) { } else if ((n >= 2 && n <= 15) || (n >= 20 && n <= 23)) {
usingInterrupt(IRQ_PORTCD); usingInterrupt(IRQ_PORTCD);
} }
} }
static void usingInterrupt(IRQ_NUMBER_t interruptName) {
void usingInterrupt(IRQ_NUMBER_t interruptName) {
uint32_t n = (uint32_t)interruptName; uint32_t n = (uint32_t)interruptName;
if (n < NVIC_NUM_INTERRUPTS) interruptMask |= (1 << n); if (n < NVIC_NUM_INTERRUPTS) interruptMask |= (1 << n);
} }
static void notUsingInterrupt(IRQ_NUMBER_t interruptName) {
void notUsingInterrupt(IRQ_NUMBER_t interruptName) {
uint32_t n = (uint32_t)interruptName; uint32_t n = (uint32_t)interruptName;
if (n < NVIC_NUM_INTERRUPTS) interruptMask &= ~(1 << n); if (n < NVIC_NUM_INTERRUPTS) interruptMask &= ~(1 << n);
} }
// Before using SPI.transfer() or asserting chip select pins, // Before using SPI.transfer() or asserting chip select pins,
// this function is used to gain exclusive access to the SPI bus // this function is used to gain exclusive access to the SPI bus
// and configure the correct settings. // and configure the correct settings.
inline static void beginTransaction(SPISettings settings) {
void beginTransaction(SPISettings settings) {
if (interruptMask) { if (interruptMask) {
__disable_irq(); __disable_irq();
interruptSave = NVIC_ICER0 & interruptMask; interruptSave = NVIC_ICER0 & interruptMask;
} }
inTransactionFlag = 1; inTransactionFlag = 1;
#endif #endif
SPI1_C1 = settings.c1;
SPI1_BR = settings.br1;
port().C1 = settings.c1;
port().BR = settings.br[hardware().br_index];
} }


// Write to the SPI bus (MOSI pin) and also receive (MISO pin) // Write to the SPI bus (MOSI pin) and also receive (MISO pin)
inline static uint8_t transfer(uint8_t data) {
SPI1_DL = data;
while (!(SPI1_S & SPI_S_SPRF)) ; // wait
return SPI1_DL;
uint8_t transfer(uint8_t data) {
port().DL = data;
while (!(port().S & SPI_S_SPRF)) ; // wait
return port().DL;
} }
inline static uint16_t transfer16(uint16_t data) {
SPI1_C2 = SPI_C2_SPIMODE;
SPI1_S;
SPI1_DL = data;
SPI1_DH = data >> 8;
while (!(SPI1_S & SPI_S_SPRF)) ; // wait
uint16_t r = SPI1_DL | (SPI1_DH << 8);
SPI1_C2 = 0;
SPI1_S;
uint16_t transfer16(uint16_t data) {
port().C2 = SPI_C2_SPIMODE;
port().S;
port().DL = data;
port().DH = data >> 8;
while (!(port().S & SPI_S_SPRF)) ; // wait
uint16_t r = port().DL | (port().DH << 8);
port().C2 = 0;
port().S;
return r; return r;
} }
inline static void transfer(void *buf, size_t count) {
void transfer(void *buf, size_t count) {
if (count == 0) return; if (count == 0) return;
uint8_t *p = (uint8_t *)buf; uint8_t *p = (uint8_t *)buf;
while (!(SPI1_S & SPI_S_SPTEF)) ; // wait
SPI1_DL = *p;
while (!(port().S & SPI_S_SPTEF)) ; // wait
port().DL = *p;
while (--count > 0) { while (--count > 0) {
uint8_t out = *(p + 1); uint8_t out = *(p + 1);
while (!(SPI1_S & SPI_S_SPTEF)) ; // wait
while (!(port().S & SPI_S_SPTEF)) ; // wait
__disable_irq(); __disable_irq();
SPI1_DL = out;
while (!(SPI1_S & SPI_S_SPRF)) ; // wait
uint8_t in = SPI1_DL;
port().DL = out;
while (!(port().S & SPI_S_SPRF)) ; // wait
uint8_t in = port().DL;
__enable_irq(); __enable_irq();
*p++ = in; *p++ = in;
} }
while (!(SPI1_S & SPI_S_SPRF)) ; // wait
*p = SPDR;
while (!(port().S & SPI_S_SPRF)) ; // wait
*p = port().DL;
} }


// After performing a group of transfers and releasing the chip select // After performing a group of transfers and releasing the chip select
// signal, this function allows others to access the SPI bus // signal, this function allows others to access the SPI bus
inline static void endTransaction(void) {
void endTransaction(void) {
#ifdef SPI_TRANSACTION_MISMATCH_LED #ifdef SPI_TRANSACTION_MISMATCH_LED
if (!inTransactionFlag) { if (!inTransactionFlag) {
pinMode(SPI_TRANSACTION_MISMATCH_LED, OUTPUT); pinMode(SPI_TRANSACTION_MISMATCH_LED, OUTPUT);
} }


// Disable the SPI bus // Disable the SPI bus
static void end();
void end();


// This function is deprecated. New applications should use // This function is deprecated. New applications should use
// beginTransaction() to configure SPI settings. // beginTransaction() to configure SPI settings.
static void setBitOrder(uint8_t bitOrder) {
uint8_t c = SPI1_C1 | SPI_C1_SPE;
void setBitOrder(uint8_t bitOrder) {
uint8_t c = port().C1 | SPI_C1_SPE;
if (bitOrder == LSBFIRST) c |= SPI_C1_LSBFE; if (bitOrder == LSBFIRST) c |= SPI_C1_LSBFE;
else c &= ~SPI_C1_LSBFE; else c &= ~SPI_C1_LSBFE;
SPI1_C1 = c;
port().C1 = c;
} }


// This function is deprecated. New applications should use // This function is deprecated. New applications should use
// beginTransaction() to configure SPI settings. // beginTransaction() to configure SPI settings.
static void setDataMode(uint8_t dataMode) {
uint8_t c = SPI1_C1 | SPI_C1_SPE;
void setDataMode(uint8_t dataMode) {
uint8_t c = port().C1 | SPI_C1_SPE;
if (dataMode & 0x04) c |= SPI_C1_CPHA; if (dataMode & 0x04) c |= SPI_C1_CPHA;
else c &= ~SPI_C1_CPHA; else c &= ~SPI_C1_CPHA;
if (dataMode & 0x08) c |= SPI_C1_CPOL; if (dataMode & 0x08) c |= SPI_C1_CPOL;
else c &= ~SPI_C1_CPOL; else c &= ~SPI_C1_CPOL;
SPI1_C1 = c;
port().C1 = c;
} }


// This function is deprecated. New applications should use // This function is deprecated. New applications should use
// beginTransaction() to configure SPI settings. // beginTransaction() to configure SPI settings.
inline static void setClockDivider(uint8_t clockDiv) {
void setClockDivider(uint8_t clockDiv) {
unsigned int i = hardware().br_index;
if (clockDiv == SPI_CLOCK_DIV2) { if (clockDiv == SPI_CLOCK_DIV2) {
SPI1_BR = (SPISettings(12000000, MSBFIRST, SPI_MODE0).br1);
port().BR = (SPISettings(12000000, MSBFIRST, SPI_MODE0).br[i]);
} else if (clockDiv == SPI_CLOCK_DIV4) { } else if (clockDiv == SPI_CLOCK_DIV4) {
SPI1_BR = (SPISettings(4000000, MSBFIRST, SPI_MODE0).br1);
port().BR = (SPISettings(4000000, MSBFIRST, SPI_MODE0).br[i]);
} else if (clockDiv == SPI_CLOCK_DIV8) { } else if (clockDiv == SPI_CLOCK_DIV8) {
SPI1_BR = (SPISettings(2000000, MSBFIRST, SPI_MODE0).br1);
port().BR = (SPISettings(2000000, MSBFIRST, SPI_MODE0).br[i]);
} else if (clockDiv == SPI_CLOCK_DIV16) { } else if (clockDiv == SPI_CLOCK_DIV16) {
SPI1_BR = (SPISettings(1000000, MSBFIRST, SPI_MODE0).br1);
port().BR = (SPISettings(1000000, MSBFIRST, SPI_MODE0).br[i]);
} else if (clockDiv == SPI_CLOCK_DIV32) { } else if (clockDiv == SPI_CLOCK_DIV32) {
SPI1_BR = (SPISettings(500000, MSBFIRST, SPI_MODE0).br1);
port().BR = (SPISettings(500000, MSBFIRST, SPI_MODE0).br[i]);
} else if (clockDiv == SPI_CLOCK_DIV64) { } else if (clockDiv == SPI_CLOCK_DIV64) {
SPI1_BR = (SPISettings(250000, MSBFIRST, SPI_MODE0).br1);
port().BR = (SPISettings(250000, MSBFIRST, SPI_MODE0).br[i]);
} else { /* clockDiv == SPI_CLOCK_DIV128 */ } else { /* clockDiv == SPI_CLOCK_DIV128 */
SPI1_BR = (SPISettings(125000, MSBFIRST, SPI_MODE0).br1);
port().BR = (SPISettings(125000, MSBFIRST, SPI_MODE0).br[i]);
} }
} }


// These undocumented functions should not be used. SPI.transfer() // These undocumented functions should not be used. SPI.transfer()
// polls the hardware flag which is automatically cleared as the // polls the hardware flag which is automatically cleared as the
// AVR responds to SPI's interrupt // AVR responds to SPI's interrupt
inline static void attachInterrupt() { }
inline static void detachInterrupt() { }
void attachInterrupt() { }
void detachInterrupt() { }


// Teensy LC can use alternate pins for these 3 SPI signals. // Teensy LC can use alternate pins for these 3 SPI signals.
inline static void setMOSI(uint8_t pin) __attribute__((always_inline)) {
SPCR1.setMOSI(pin);
}
inline static void setMISO(uint8_t pin) __attribute__((always_inline)) {
SPCR1.setMISO(pin);
}
inline static void setSCK(uint8_t pin) __attribute__((always_inline)) {
SPCR1.setSCK(pin);
}
void setMOSI(uint8_t pin);
void setMISO(uint8_t pin);
void setSCK(uint8_t pin);
// return true if "pin" has special chip select capability // return true if "pin" has special chip select capability
static bool pinIsChipSelect(uint8_t pin) { return (pin == 6); }
bool pinIsChipSelect(uint8_t pin);
// return true if both pin1 and pin2 have independent chip select capability // return true if both pin1 and pin2 have independent chip select capability
static bool pinIsChipSelect(uint8_t pin1, uint8_t pin2) { return false; }
bool pinIsChipSelect(uint8_t pin1, uint8_t pin2) { return false; }
// configure a pin for chip select and return its SPI_MCR_PCSIS bitmask // configure a pin for chip select and return its SPI_MCR_PCSIS bitmask
// setCS() is a special function, not intended for use from normal Arduino // setCS() is a special function, not intended for use from normal Arduino
// programs/sketches. See the ILI3941_t3 library for an example. // programs/sketches. See the ILI3941_t3 library for an example.
static uint8_t setCS(uint8_t pin);
uint8_t setCS(uint8_t pin);


private: private:
static uint32_t interruptMask;
static uint32_t interruptSave;
KINETISL_SPI_t & port() { return *(KINETISL_SPI_t *)port_addr; }
const SPI_Hardware_t & hardware() { return *(const SPI_Hardware_t *)hardware_addr; }
uintptr_t port_addr;
uintptr_t hardware_addr;
uint32_t interruptMask = 0;
uint32_t interruptSave = 0;
uint8_t mosi_pin_index = 0;
uint8_t miso_pin_index = 0;
uint8_t sck_pin_index = 0;
#ifdef SPI_TRANSACTION_MISMATCH_LED #ifdef SPI_TRANSACTION_MISMATCH_LED
static uint8_t inTransactionFlag;
uint8_t inTransactionFlag = 0;
#endif #endif
}; };


















#endif #endif






extern SPIClass SPI; extern SPIClass SPI;
#if defined(__arm__) && defined(TEENSYDUINO) && defined(KINETISL)
extern SPI1Class SPI1;
#if defined(__MKL26Z64__)
extern SPIClass SPI1;
#endif #endif
#if defined(__MK64FX512__) || defined(__MK66FX1M0__) #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
extern SPIClass SPI1; extern SPIClass SPI1;

Loading…
Cancel
Save