Skip to content

Fix for SPI1 on ATmega328PB #353

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions avr/libraries/SPI1/src/SPI1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ void SPI1Class::begin()
// 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.
SPCR1 |= _BV(MSTR);
SPCR1 |= _BV(SPE);
SPCR1 |= _BV(MSTR1);
SPCR1 |= _BV(SPE1);

// Set direction register for SCK and MOSI pin.
// MISO pin automatically overrides to INPUT.
Expand Down
22 changes: 11 additions & 11 deletions avr/libraries/SPI1/src/SPI1.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ class SPI1Settings {
clockDiv ^= 0x1;

// Pack into the SPI1Settings class
spcr1 = _BV(SPE) | _BV(MSTR) | ((bitOrder == LSBFIRST) ? _BV(DORD) : 0) |
spcr1 = _BV(SPE1) | _BV(MSTR) | ((bitOrder == LSBFIRST) ? _BV(DORD1) : 0) |
(dataMode & SPI_MODE_MASK) | ((clockDiv >> 1) & SPI_CLOCK_MASK);
spsr1 = clockDiv & SPI_2XCLOCK_MASK;
}
Expand Down Expand Up @@ -214,29 +214,29 @@ class SPI1Class {
* speeds it is unnoticed.
*/
asm volatile("nop");
while (!(SPSR1 & _BV(SPIF))) ; // wait
while (!(SPSR1 & _BV(SPIF1))) ; // wait
return SPDR1;
}
inline static uint16_t transfer16(uint16_t data) {
union { uint16_t val; struct { uint8_t lsb; uint8_t msb; }; } in, out;
in.val = data;
if (!(SPCR1 & _BV(DORD))) {
if (!(SPCR1 & _BV(DORD1))) {
SPDR1 = in.msb;
asm volatile("nop"); // See transfer(uint8_t) function
while (!(SPSR1 & _BV(SPIF))) ;
while (!(SPSR1 & _BV(SPIF1))) ;
out.msb = SPDR1;
SPDR1 = in.lsb;
asm volatile("nop");
while (!(SPSR1 & _BV(SPIF))) ;
while (!(SPSR1 & _BV(SPIF1))) ;
out.lsb = SPDR1;
} else {
SPDR1 = in.lsb;
asm volatile("nop");
while (!(SPSR1 & _BV(SPIF))) ;
while (!(SPSR1 & _BV(SPIF1))) ;
out.lsb = SPDR1;
SPDR1 = in.msb;
asm volatile("nop");
while (!(SPSR1 & _BV(SPIF))) ;
while (!(SPSR1 & _BV(SPIF1))) ;
out.msb = SPDR1;
}
return out.val;
Expand All @@ -247,12 +247,12 @@ class SPI1Class {
SPDR1 = *p;
while (--count > 0) {
uint8_t out = *(p + 1);
while (!(SPSR1 & _BV(SPIF))) ;
while (!(SPSR1 & _BV(SPIF1))) ;
uint8_t in = SPDR1;
SPDR1 = out;
*p++ = in;
}
while (!(SPSR1 & _BV(SPIF))) ;
while (!(SPSR1 & _BV(SPIF1))) ;
*p = SPDR1;
}
// After performing a group of transfers and releasing the chip select
Expand Down Expand Up @@ -306,8 +306,8 @@ class SPI1Class {
// 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() { SPCR1 |= _BV(SPIE); }
inline static void detachInterrupt() { SPCR1 &= ~_BV(SPIE); }
inline static void attachInterrupt() { SPCR1 |= _BV(SPIE1); }
inline static void detachInterrupt() { SPCR1 &= ~_BV(SPIE1); }

private:
static uint8_t initialized;
Expand Down