Skip to content

SPI backport from 1.5 #2375

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
207 changes: 168 additions & 39 deletions libraries/SPI/SPI.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
/*
* Copyright (c) 2010 by Cristian Maglie <c.maglie@bug.st>
* Copyright (c) 2014 by Paul Stoffregen <paul@pjrc.com> (Transaction API)
* Copyright (c) 2014 by Matthijs Kooijman <matthijs@stdin.nl> (SPISettings AVR)
* Copyright (c) 2014 by Andrew J. Kroll <xxxajk@gmail.com> (atomicity fixes)
* SPI Master library for arduino.
*
* This file is free software; you can redistribute it and/or modify
Expand All @@ -8,59 +11,185 @@
* published by the Free Software Foundation.
*/

#include "pins_arduino.h"
#include "SPI.h"

SPIClass SPI;
SPIflags_t SPIClass::modeFlags = {false, false, 0};
uint8_t SPIClass::interruptMask = 0;
uint8_t SPIClass::interruptSave = 0;

void SPIClass::begin() {
uint8_t sreg = SREG;
noInterrupts(); // Protect from a scheduler and prevent transactionBegin
if(!modeFlags.initialized) {
modeFlags.initialized = true;
// Set SS to high so a connected chip will be "deselected" by default
digitalWrite(SS, HIGH);

// Set SS to high so a connected chip will be "deselected" by default
digitalWrite(SS, HIGH);
// When the SS pin is set as OUTPUT, it can be used as
// a general purpose output port (it doesn't influence
// SPI operations).
pinMode(SS, OUTPUT);

// When the SS pin is set as OUTPUT, it can be used as
// a general purpose output port (it doesn't influence
// SPI operations).
pinMode(SS, OUTPUT);
// Warning: if the SS pin ever becomes a LOW INPUT then SPI
// automatically switches to Slave, so the data direction of
// the SS pin MUST be kept as OUTPUT.
SPCR |= _BV(MSTR);
SPCR |= _BV(SPE);

// Warning: if the SS pin ever becomes a LOW INPUT then SPI
// automatically switches to Slave, so the data direction of
// the SS pin MUST be kept as OUTPUT.
SPCR |= _BV(MSTR);
SPCR |= _BV(SPE);

// Set direction register for SCK and MOSI pin.
// MISO pin automatically overrides to INPUT.
// By doing this AFTER enabling SPI, we avoid accidentally
// clocking in a single bit since the lines go directly
// from "input" to SPI control.
// http://code.google.com/p/arduino/issues/detail?id=888
pinMode(SCK, OUTPUT);
pinMode(MOSI, OUTPUT);
// Set direction register for SCK and MOSI pin.
// MISO pin automatically overrides to INPUT.
// By doing this AFTER enabling SPI, we avoid accidentally
// clocking in a single bit since the lines go directly
// from "input" to SPI control.
// http://code.google.com/p/arduino/issues/detail?id=888
pinMode(SCK, OUTPUT);
pinMode(MOSI, OUTPUT);
}
SREG = sreg;
}


void SPIClass::end() {
SPCR &= ~_BV(SPE);
uint8_t sreg = SREG;
noInterrupts(); // Protect from a scheduler and prevent transactionBegin
if(!modeFlags.interruptMode && modeFlags.initialized) {
SPCR &= ~_BV(SPE);
modeFlags.initialized = false;
#ifdef SPI_TRANSACTION_MISMATCH_LED
modeFlags.inTransactionFlag = false;
#endif
}
SREG = sreg;
}

void SPIClass::setBitOrder(uint8_t bitOrder)
{
if(bitOrder == LSBFIRST) {
SPCR |= _BV(DORD);
} else {
SPCR &= ~(_BV(DORD));
}
}
// mapping of interrupt numbers to bits within SPI_AVR_EIMSK
#if defined(__AVR_ATmega32U4__)
#define SPI_INT0_MASK (1<<INT0)
#define SPI_INT1_MASK (1<<INT1)
#define SPI_INT2_MASK (1<<INT2)
#define SPI_INT3_MASK (1<<INT3)
#define SPI_INT4_MASK (1<<INT6)
#elif defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB1286__)
#define SPI_INT0_MASK (1<<INT0)
#define SPI_INT1_MASK (1<<INT1)
#define SPI_INT2_MASK (1<<INT2)
#define SPI_INT3_MASK (1<<INT3)
#define SPI_INT4_MASK (1<<INT4)
#define SPI_INT5_MASK (1<<INT5)
#define SPI_INT6_MASK (1<<INT6)
#define SPI_INT7_MASK (1<<INT7)
#elif defined(EICRA) && defined(EICRB) && defined(EIMSK)
#define SPI_INT0_MASK (1<<INT4)
#define SPI_INT1_MASK (1<<INT5)
#define SPI_INT2_MASK (1<<INT0)
#define SPI_INT3_MASK (1<<INT1)
#define SPI_INT4_MASK (1<<INT2)
#define SPI_INT5_MASK (1<<INT3)
#define SPI_INT6_MASK (1<<INT6)
#define SPI_INT7_MASK (1<<INT7)
#else
#ifdef INT0
#define SPI_INT0_MASK (1<<INT0)
#endif
#ifdef INT1
#define SPI_INT1_MASK (1<<INT1)
#endif
#ifdef INT2
#define SPI_INT2_MASK (1<<INT2)
#endif
#endif

void SPIClass::setDataMode(uint8_t mode)
{
SPCR = (SPCR & ~SPI_MODE_MASK) | mode;
void SPIClass::usingInterrupt(uint8_t interruptNumber) {
uint8_t mask = 0;
uint8_t sreg = SREG;
noInterrupts(); // Protect from a scheduler and prevent transactionBegin
switch(interruptNumber) {
#ifdef SPI_INT0_MASK
case 0: mask = SPI_INT0_MASK;
break;
#endif
#ifdef SPI_INT1_MASK
case 1: mask = SPI_INT1_MASK;
break;
#endif
#ifdef SPI_INT2_MASK
case 2: mask = SPI_INT2_MASK;
break;
#endif
#ifdef SPI_INT3_MASK
case 3: mask = SPI_INT3_MASK;
break;
#endif
#ifdef SPI_INT4_MASK
case 4: mask = SPI_INT4_MASK;
break;
#endif
#ifdef SPI_INT5_MASK
case 5: mask = SPI_INT5_MASK;
break;
#endif
#ifdef SPI_INT6_MASK
case 6: mask = SPI_INT6_MASK;
break;
#endif
#ifdef SPI_INT7_MASK
case 7: mask = SPI_INT7_MASK;
break;
#endif
default:
modeFlags.interruptMode = 2;
break;
}
interruptMask |= mask;
if(!modeFlags.interruptMode) modeFlags.interruptMode = 1;
SREG = sreg;
}

void SPIClass::setClockDivider(uint8_t rate)
{
SPCR = (SPCR & ~SPI_CLOCK_MASK) | (rate & SPI_CLOCK_MASK);
SPSR = (SPSR & ~SPI_2XCLOCK_MASK) | ((rate >> 2) & SPI_2XCLOCK_MASK);
void SPIClass::notUsingInterrupt(uint8_t interruptNumber) {
uint8_t mask = 0;
uint8_t sreg = SREG;
noInterrupts(); // Protect from a scheduler and prevent transactionBegin
switch(interruptNumber) {
#ifdef SPI_INT0_MASK
case 0: mask = SPI_INT0_MASK;
break;
#endif
#ifdef SPI_INT1_MASK
case 1: mask = SPI_INT1_MASK;
break;
#endif
#ifdef SPI_INT2_MASK
case 2: mask = SPI_INT2_MASK;
break;
#endif
#ifdef SPI_INT3_MASK
case 3: mask = SPI_INT3_MASK;
break;
#endif
#ifdef SPI_INT4_MASK
case 4: mask = SPI_INT4_MASK;
break;
#endif
#ifdef SPI_INT5_MASK
case 5: mask = SPI_INT5_MASK;
break;
#endif
#ifdef SPI_INT6_MASK
case 6: mask = SPI_INT6_MASK;
break;
#endif
#ifdef SPI_INT7_MASK
case 7: mask = SPI_INT7_MASK;
break;
#endif
default:
if(interruptMask) {
modeFlags.interruptMode = 1;
} else {
modeFlags.interruptMode = 0;
}
break;
}
interruptMask &= ~mask;
SREG = sreg;
}

Loading