From 367f8a86c1a4435d6e2c78eda264c314c0a8b27e Mon Sep 17 00:00:00 2001 From: TheTrainHouse Date: Fri, 30 May 2025 08:34:14 +1200 Subject: [PATCH] Fix for SPI1 on ATmega328PB --- avr/libraries/SPI1/src/SPI1.cpp | 4 ++-- avr/libraries/SPI1/src/SPI1.h | 22 +++++++++++----------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/avr/libraries/SPI1/src/SPI1.cpp b/avr/libraries/SPI1/src/SPI1.cpp index e76487fac..3411fecdd 100755 --- a/avr/libraries/SPI1/src/SPI1.cpp +++ b/avr/libraries/SPI1/src/SPI1.cpp @@ -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. diff --git a/avr/libraries/SPI1/src/SPI1.h b/avr/libraries/SPI1/src/SPI1.h index 1d69e9ffb..deaba3ddc 100755 --- a/avr/libraries/SPI1/src/SPI1.h +++ b/avr/libraries/SPI1/src/SPI1.h @@ -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; } @@ -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; @@ -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 @@ -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;