2018-02-10 19:25:34 -06:00
|
|
|
/**
|
2017-06-17 18:36:10 -05:00
|
|
|
* Marlin 3D Printer Firmware
|
2020-02-03 08:00:57 -06:00
|
|
|
* Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
2017-06-17 18:36:10 -05:00
|
|
|
*
|
|
|
|
* Based on Sprinter and grbl.
|
2019-06-27 23:57:50 -05:00
|
|
|
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
|
2017-06-17 18:36:10 -05:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2020-07-22 22:20:14 -05:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2017-06-17 18:36:10 -05:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2019-06-14 20:16:22 -05:00
|
|
|
* Adapted from Arduino Sd2Card Library
|
2019-06-27 23:57:50 -05:00
|
|
|
* Copyright (c) 2009 by William Greiman
|
2017-06-17 18:36:10 -05:00
|
|
|
*/
|
2017-09-27 04:57:33 -05:00
|
|
|
|
2017-06-17 18:36:10 -05:00
|
|
|
/**
|
2019-06-14 20:16:22 -05:00
|
|
|
* HAL for AVR - SPI functions
|
2017-06-17 18:36:10 -05:00
|
|
|
*/
|
|
|
|
|
2017-09-23 23:25:28 -05:00
|
|
|
#ifdef __AVR__
|
2017-06-17 18:36:10 -05:00
|
|
|
|
2017-09-06 06:28:32 -05:00
|
|
|
#include "../../inc/MarlinConfig.h"
|
2017-06-17 18:36:10 -05:00
|
|
|
|
2019-09-16 20:31:08 -05:00
|
|
|
void spiBegin() {
|
2021-10-04 23:23:02 -05:00
|
|
|
#if PIN_EXISTS(SD_SS)
|
2022-01-30 05:31:56 -06:00
|
|
|
// Do not init HIGH for boards with pin 4 used as Fans or Heaters or otherwise, not likely to have multiple SPI devices anyway.
|
|
|
|
#if defined(__AVR_ATmega644__) || defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644PA__) || defined(__AVR_ATmega1284P__)
|
|
|
|
// SS must be in output mode even it is not chip select
|
|
|
|
SET_OUTPUT(SD_SS_PIN);
|
|
|
|
#else
|
|
|
|
// set SS high - may be chip select for another SPI device
|
|
|
|
OUT_WRITE(SD_SS_PIN, HIGH);
|
|
|
|
#endif
|
2021-10-04 23:23:02 -05:00
|
|
|
#endif
|
2021-01-01 14:31:15 -06:00
|
|
|
SET_OUTPUT(SD_SCK_PIN);
|
|
|
|
SET_INPUT(SD_MISO_PIN);
|
|
|
|
SET_OUTPUT(SD_MOSI_PIN);
|
2017-06-17 18:36:10 -05:00
|
|
|
|
2022-01-30 05:31:56 -06:00
|
|
|
IF_DISABLED(SOFTWARE_SPI, spiInit(SPI_HALF_SPEED));
|
2017-06-17 18:36:10 -05:00
|
|
|
}
|
|
|
|
|
2019-07-05 18:20:15 -05:00
|
|
|
#if NONE(SOFTWARE_SPI, FORCE_SOFT_SPI)
|
2019-02-20 04:00:49 -06:00
|
|
|
|
2019-07-20 14:40:49 -05:00
|
|
|
// ------------------------
|
2019-02-20 04:00:49 -06:00
|
|
|
// Hardware SPI
|
2019-07-20 14:40:49 -05:00
|
|
|
// ------------------------
|
2019-02-20 04:00:49 -06:00
|
|
|
|
2017-06-17 18:36:10 -05:00
|
|
|
// make sure SPCR rate is in expected bits
|
|
|
|
#if (SPR0 != 0 || SPR1 != 1)
|
|
|
|
#error "unexpected SPCR bits"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize hardware SPI
|
|
|
|
* Set SCK rate to F_CPU/pow(2, 1 + spiRate) for spiRate [0,6]
|
|
|
|
*/
|
|
|
|
void spiInit(uint8_t spiRate) {
|
|
|
|
// See avr processor documentation
|
|
|
|
CBI(
|
|
|
|
#ifdef PRR
|
|
|
|
PRR
|
|
|
|
#elif defined(PRR0)
|
|
|
|
PRR0
|
|
|
|
#endif
|
2021-09-23 00:51:35 -05:00
|
|
|
, PRSPI
|
|
|
|
);
|
2017-06-17 18:36:10 -05:00
|
|
|
|
|
|
|
SPCR = _BV(SPE) | _BV(MSTR) | (spiRate >> 1);
|
|
|
|
SPSR = spiRate & 1 || spiRate == 6 ? 0 : _BV(SPI2X);
|
|
|
|
}
|
2018-01-03 05:12:25 -06:00
|
|
|
|
2017-06-17 18:36:10 -05:00
|
|
|
/** SPI receive a byte */
|
2019-09-16 20:31:08 -05:00
|
|
|
uint8_t spiRec() {
|
2017-12-15 15:17:52 -06:00
|
|
|
SPDR = 0xFF;
|
2017-06-17 18:36:10 -05:00
|
|
|
while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ }
|
|
|
|
return SPDR;
|
|
|
|
}
|
2019-02-20 04:00:49 -06:00
|
|
|
|
2017-06-17 18:36:10 -05:00
|
|
|
/** SPI read data */
|
2021-03-29 20:36:37 -05:00
|
|
|
void spiRead(uint8_t *buf, uint16_t nbyte) {
|
2017-06-17 18:36:10 -05:00
|
|
|
if (nbyte-- == 0) return;
|
2017-12-15 15:17:52 -06:00
|
|
|
SPDR = 0xFF;
|
2017-06-17 18:36:10 -05:00
|
|
|
for (uint16_t i = 0; i < nbyte; i++) {
|
|
|
|
while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ }
|
|
|
|
buf[i] = SPDR;
|
2017-12-15 15:17:52 -06:00
|
|
|
SPDR = 0xFF;
|
2017-06-17 18:36:10 -05:00
|
|
|
}
|
|
|
|
while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ }
|
|
|
|
buf[nbyte] = SPDR;
|
|
|
|
}
|
2019-02-20 04:00:49 -06:00
|
|
|
|
2017-06-17 18:36:10 -05:00
|
|
|
/** SPI send a byte */
|
|
|
|
void spiSend(uint8_t b) {
|
|
|
|
SPDR = b;
|
|
|
|
while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ }
|
|
|
|
}
|
2019-02-20 04:00:49 -06:00
|
|
|
|
2017-06-17 18:36:10 -05:00
|
|
|
/** SPI send block */
|
2021-03-29 20:36:37 -05:00
|
|
|
void spiSendBlock(uint8_t token, const uint8_t *buf) {
|
2017-06-17 18:36:10 -05:00
|
|
|
SPDR = token;
|
|
|
|
for (uint16_t i = 0; i < 512; i += 2) {
|
|
|
|
while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ }
|
|
|
|
SPDR = buf[i];
|
|
|
|
while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ }
|
|
|
|
SPDR = buf[i + 1];
|
|
|
|
}
|
|
|
|
while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ }
|
|
|
|
}
|
2018-01-03 05:12:25 -06:00
|
|
|
|
|
|
|
|
|
|
|
/** begin spi transaction */
|
|
|
|
void spiBeginTransaction(uint32_t spiClock, uint8_t bitOrder, uint8_t dataMode) {
|
|
|
|
// Based on Arduino SPI library
|
|
|
|
// Clock settings are defined as follows. Note that this shows SPI2X
|
|
|
|
// inverted, so the bits form increasing numbers. Also note that
|
|
|
|
// fosc/64 appears twice
|
|
|
|
// SPR1 SPR0 ~SPI2X Freq
|
|
|
|
// 0 0 0 fosc/2
|
|
|
|
// 0 0 1 fosc/4
|
|
|
|
// 0 1 0 fosc/8
|
|
|
|
// 0 1 1 fosc/16
|
|
|
|
// 1 0 0 fosc/32
|
|
|
|
// 1 0 1 fosc/64
|
|
|
|
// 1 1 0 fosc/64
|
|
|
|
// 1 1 1 fosc/128
|
|
|
|
|
|
|
|
// We find the fastest clock that is less than or equal to the
|
|
|
|
// given clock rate. The clock divider that results in clock_setting
|
|
|
|
// is 2 ^^ (clock_div + 1). If nothing is slow enough, we'll use the
|
|
|
|
// slowest (128 == 2 ^^ 7, so clock_div = 6).
|
|
|
|
uint8_t clockDiv;
|
2018-01-05 10:10:55 -06:00
|
|
|
|
2018-01-03 05:12:25 -06:00
|
|
|
// When the clock is known at compiletime, use this if-then-else
|
|
|
|
// cascade, which the compiler knows how to completely optimize
|
|
|
|
// away. When clock is not known, use a loop instead, which generates
|
|
|
|
// shorter code.
|
|
|
|
if (__builtin_constant_p(spiClock)) {
|
2018-05-25 23:32:37 -05:00
|
|
|
if (spiClock >= F_CPU / 2) clockDiv = 0;
|
|
|
|
else if (spiClock >= F_CPU / 4) clockDiv = 1;
|
|
|
|
else if (spiClock >= F_CPU / 8) clockDiv = 2;
|
|
|
|
else if (spiClock >= F_CPU / 16) clockDiv = 3;
|
|
|
|
else if (spiClock >= F_CPU / 32) clockDiv = 4;
|
|
|
|
else if (spiClock >= F_CPU / 64) clockDiv = 5;
|
|
|
|
else clockDiv = 6;
|
|
|
|
}
|
|
|
|
else {
|
2018-01-03 05:12:25 -06:00
|
|
|
uint32_t clockSetting = F_CPU / 2;
|
|
|
|
clockDiv = 0;
|
|
|
|
while (clockDiv < 6 && spiClock < clockSetting) {
|
|
|
|
clockSetting /= 2;
|
|
|
|
clockDiv++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compensate for the duplicate fosc/64
|
2018-05-25 23:32:37 -05:00
|
|
|
if (clockDiv == 6) clockDiv = 7;
|
2018-01-03 05:12:25 -06:00
|
|
|
|
|
|
|
// Invert the SPI2X bit
|
|
|
|
clockDiv ^= 0x1;
|
|
|
|
|
2019-07-05 01:59:37 -05:00
|
|
|
SPCR = _BV(SPE) | _BV(MSTR) | ((bitOrder == LSBFIRST) ? _BV(DORD) : 0) |
|
2018-01-03 05:12:25 -06:00
|
|
|
(dataMode << CPHA) | ((clockDiv >> 1) << SPR0);
|
2018-01-05 10:10:55 -06:00
|
|
|
SPSR = clockDiv | 0x01;
|
2018-01-03 05:12:25 -06:00
|
|
|
}
|
|
|
|
|
2018-01-05 10:10:55 -06:00
|
|
|
|
2019-06-14 20:16:22 -05:00
|
|
|
#else // SOFTWARE_SPI || FORCE_SOFT_SPI
|
|
|
|
|
2019-07-20 14:40:49 -05:00
|
|
|
// ------------------------
|
2019-06-14 20:16:22 -05:00
|
|
|
// Software SPI
|
2019-07-20 14:40:49 -05:00
|
|
|
// ------------------------
|
2019-02-20 04:00:49 -06:00
|
|
|
|
2019-06-14 20:16:22 -05:00
|
|
|
// nop to tune soft SPI timing
|
2017-06-17 18:36:10 -05:00
|
|
|
#define nop asm volatile ("\tnop\n")
|
|
|
|
|
2019-09-30 21:44:07 -05:00
|
|
|
void spiInit(uint8_t) { /* do nothing */ }
|
2017-06-17 18:36:10 -05:00
|
|
|
|
2019-06-14 20:16:22 -05:00
|
|
|
// Begin SPI transaction, set clock, bit order, data mode
|
2019-09-30 21:44:07 -05:00
|
|
|
void spiBeginTransaction(uint32_t spiClock, uint8_t bitOrder, uint8_t dataMode) { /* do nothing */ }
|
2018-01-03 05:12:25 -06:00
|
|
|
|
2019-06-14 20:16:22 -05:00
|
|
|
// Soft SPI receive byte
|
2017-06-17 18:36:10 -05:00
|
|
|
uint8_t spiRec() {
|
|
|
|
uint8_t data = 0;
|
2018-03-07 17:53:25 -06:00
|
|
|
// no interrupts during byte receive - about 8µs
|
2017-06-17 18:36:10 -05:00
|
|
|
cli();
|
2017-12-15 15:17:52 -06:00
|
|
|
// output pin high - like sending 0xFF
|
2021-01-01 14:31:15 -06:00
|
|
|
WRITE(SD_MOSI_PIN, HIGH);
|
2017-06-17 18:36:10 -05:00
|
|
|
|
2020-03-13 23:18:16 -05:00
|
|
|
LOOP_L_N(i, 8) {
|
2021-01-01 14:31:15 -06:00
|
|
|
WRITE(SD_SCK_PIN, HIGH);
|
2017-06-17 18:36:10 -05:00
|
|
|
|
2019-02-20 04:00:49 -06:00
|
|
|
nop; // adjust so SCK is nice
|
2017-06-17 18:36:10 -05:00
|
|
|
nop;
|
|
|
|
|
|
|
|
data <<= 1;
|
|
|
|
|
2021-01-01 14:31:15 -06:00
|
|
|
if (READ(SD_MISO_PIN)) data |= 1;
|
2017-06-17 18:36:10 -05:00
|
|
|
|
2021-01-01 14:31:15 -06:00
|
|
|
WRITE(SD_SCK_PIN, LOW);
|
2017-06-17 18:36:10 -05:00
|
|
|
}
|
2019-02-20 04:00:49 -06:00
|
|
|
|
2017-06-17 18:36:10 -05:00
|
|
|
sei();
|
|
|
|
return data;
|
|
|
|
}
|
2019-02-20 04:00:49 -06:00
|
|
|
|
2019-06-14 20:16:22 -05:00
|
|
|
// Soft SPI read data
|
2021-03-29 20:36:37 -05:00
|
|
|
void spiRead(uint8_t *buf, uint16_t nbyte) {
|
2017-06-17 18:36:10 -05:00
|
|
|
for (uint16_t i = 0; i < nbyte; i++)
|
|
|
|
buf[i] = spiRec();
|
|
|
|
}
|
2019-02-20 04:00:49 -06:00
|
|
|
|
2019-06-14 20:16:22 -05:00
|
|
|
// Soft SPI send byte
|
2017-06-17 18:36:10 -05:00
|
|
|
void spiSend(uint8_t data) {
|
2018-03-07 17:53:25 -06:00
|
|
|
// no interrupts during byte send - about 8µs
|
2017-06-17 18:36:10 -05:00
|
|
|
cli();
|
2020-03-13 23:18:16 -05:00
|
|
|
LOOP_L_N(i, 8) {
|
2021-01-01 14:31:15 -06:00
|
|
|
WRITE(SD_SCK_PIN, LOW);
|
|
|
|
WRITE(SD_MOSI_PIN, data & 0x80);
|
2017-06-17 18:36:10 -05:00
|
|
|
data <<= 1;
|
2021-01-01 14:31:15 -06:00
|
|
|
WRITE(SD_SCK_PIN, HIGH);
|
2017-06-17 18:36:10 -05:00
|
|
|
}
|
2019-02-20 04:00:49 -06:00
|
|
|
|
|
|
|
nop; // hold SCK high for a few ns
|
2017-06-17 18:36:10 -05:00
|
|
|
nop;
|
|
|
|
nop;
|
|
|
|
nop;
|
|
|
|
|
2021-01-01 14:31:15 -06:00
|
|
|
WRITE(SD_SCK_PIN, LOW);
|
2019-02-20 04:00:49 -06:00
|
|
|
|
2017-06-17 18:36:10 -05:00
|
|
|
sei();
|
|
|
|
}
|
2019-02-20 04:00:49 -06:00
|
|
|
|
2019-06-14 20:16:22 -05:00
|
|
|
// Soft SPI send block
|
2021-03-29 20:36:37 -05:00
|
|
|
void spiSendBlock(uint8_t token, const uint8_t *buf) {
|
2017-06-17 18:36:10 -05:00
|
|
|
spiSend(token);
|
|
|
|
for (uint16_t i = 0; i < 512; i++)
|
|
|
|
spiSend(buf[i]);
|
2018-01-05 10:10:55 -06:00
|
|
|
}
|
2017-06-17 18:36:10 -05:00
|
|
|
|
2019-06-14 20:16:22 -05:00
|
|
|
#endif // SOFTWARE_SPI || FORCE_SOFT_SPI
|
2017-06-17 18:36:10 -05:00
|
|
|
|
2017-09-23 23:25:28 -05:00
|
|
|
#endif // __AVR__
|