Skip to content

Commit

Permalink
Merge pull request #197 from exzombie/master
Browse files Browse the repository at this point in the history
tinySPI: apply default settings in begin() for compatibility.
  • Loading branch information
SpenceKonde authored Mar 7, 2018
2 parents f86dadd + d83131e commit c132ee4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
25 changes: 14 additions & 11 deletions avr/libraries/SPI/SPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,6 @@ tinySPI::tinySPI()
USI_impl::ClockOut tinySPI::clockoutfn = 0;
uint8_t tinySPI::delay = 0;
uint8_t tinySPI::msb1st = MSBFIRST;
uint8_t tinySPI::initialized = 0;

uint8_t tinySPI::interruptMode = 0;
uint8_t tinySPI::interruptMask = 0;
Expand All @@ -264,7 +263,7 @@ void tinySPI::begin(void)
USI_SCK_PORT |= _BV(USCK_DD_PIN); //set the USCK pin as output
USI_DDR_PORT |= _BV(DO_DD_PIN); //set the DO pin as output
USI_DDR_PORT &= ~_BV(DI_DD_PIN); //set the DI pin as input
initialized++;
applySettings(SPISettings());
}

void tinySPI::setDataMode(uint8_t spiDataMode)
Expand Down Expand Up @@ -443,6 +442,18 @@ void tinySPI::transfer(void* _buf, size_t count) {
}
}

void tinySPI::applySettings(SPISettings settings) {
USICR = settings.usicr;
msb1st = settings.msb1st ;
delay = settings.delay;
clockoutfn = settings.clockoutfn;
if (settings.cpol) {
digitalWrite(SCK, HIGH);
} else {
digitalWrite(SCK, LOW);
}
}

void tinySPI::beginTransaction(SPISettings settings) {
if (interruptMode > 0) {
uint8_t sreg = SREG;
Expand All @@ -459,15 +470,7 @@ void tinySPI::beginTransaction(SPISettings settings) {
interruptSave = sreg;
}
}
USICR = settings.usicr;
msb1st = settings.msb1st ;
delay = settings.delay;
clockoutfn = settings.clockoutfn;
if (settings.cpol) {
digitalWrite(SCK, HIGH);
} else {
digitalWrite(SCK, LOW);
}
applySettings(settings);
}

void tinySPI::endTransaction(void) {
Expand Down
3 changes: 2 additions & 1 deletion avr/libraries/SPI/SPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,8 @@ class tinySPI
static void notUsingInterrupt(uint8_t interruptNumber);

private:
static uint8_t initialized;
static void applySettings(SPISettings settings);

static uint8_t msb1st;
static uint8_t delay;
static USI_impl::ClockOut clockoutfn;
Expand Down

0 comments on commit c132ee4

Please sign in to comment.