2018-02-11 02:25:34 +01:00
|
|
|
/**
|
2017-06-18 01:36:10 +02:00
|
|
|
* Marlin 3D Printer Firmware
|
2020-02-03 15:00:57 +01:00
|
|
|
* Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
2017-06-18 01:36:10 +02:00
|
|
|
*
|
|
|
|
* Based on Sprinter and grbl.
|
2019-06-28 06:57:50 +02:00
|
|
|
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
|
2017-06-18 01:36:10 +02: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
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2019-06-15 03:16:22 +02:00
|
|
|
* Adapted from Arduino Sd2Card Library
|
2019-06-28 06:57:50 +02:00
|
|
|
* Copyright (c) 2009 by William Greiman
|
2017-06-18 01:36:10 +02:00
|
|
|
*/
|
2017-09-27 11:57:33 +02:00
|
|
|
|
2017-06-18 01:36:10 +02:00
|
|
|
/**
|
2019-06-15 03:16:22 +02:00
|
|
|
* HAL for AVR - SPI functions
|
2017-06-18 01:36:10 +02:00
|
|
|
*/
|
|
|
|
|
2017-09-24 06:25:28 +02:00
|
|
|
#ifdef __AVR__
|
2017-06-18 01:36:10 +02:00
|
|
|
|
2017-09-06 13:28:32 +02:00
|
|
|
#include "../../inc/MarlinConfig.h"
|
2017-06-18 01:36:10 +02:00
|
|
|
|
2019-09-17 03:31:08 +02:00
|
|
|
void spiBegin() {
|
2019-06-15 03:16:22 +02:00
|
|
|
OUT_WRITE(SS_PIN, HIGH);
|
2017-06-18 01:36:10 +02:00
|
|
|
SET_OUTPUT(SCK_PIN);
|
|
|
|
SET_INPUT(MISO_PIN);
|
|
|
|
SET_OUTPUT(MOSI_PIN);
|
|
|
|
|
|
|
|
#if DISABLED(SOFTWARE_SPI)
|
|
|
|
// SS must be in output mode even it is not chip select
|
2019-06-15 03:16:22 +02:00
|
|
|
//SET_OUTPUT(SS_PIN);
|
2017-06-18 01:36:10 +02:00
|
|
|
// set SS high - may be chip select for another SPI device
|
2019-06-15 03:16:22 +02:00
|
|
|
//#if SET_SPI_SS_HIGH
|
|
|
|
//WRITE(SS_PIN, HIGH);
|
|
|
|
//#endif
|
2017-06-18 01:36:10 +02:00
|
|
|
// set a default rate
|
|
|
|
spiInit(1);
|
2019-06-15 03:16:22 +02:00
|
|
|
#endif
|
2017-06-18 01:36:10 +02:00
|
|
|
}
|
|
|
|
|
2019-07-06 01:20:15 +02:00
|
|
|
#if NONE(SOFTWARE_SPI, FORCE_SOFT_SPI)
|
2019-02-20 11:00:49 +01:00
|
|
|
|
2019-07-20 21:40:49 +02:00
|
|
|
// ------------------------
|
2019-02-20 11:00:49 +01:00
|
|
|
// Hardware SPI
|
2019-07-20 21:40:49 +02:00
|
|
|
// ------------------------
|
2019-02-20 11:00:49 +01:00
|
|
|
|
2017-06-18 01:36:10 +02: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
|
|
|
|
, PRSPI);
|
|
|
|
|
|
|
|
SPCR = _BV(SPE) | _BV(MSTR) | (spiRate >> 1);
|
|
|
|
SPSR = spiRate & 1 || spiRate == 6 ? 0 : _BV(SPI2X);
|
|
|
|
}
|
2018-01-03 12:12:25 +01:00
|
|
|
|
2017-06-18 01:36:10 +02:00
|
|
|
/** SPI receive a byte */
|
2019-09-17 03:31:08 +02:00
|
|
|
uint8_t spiRec() {
|
2017-12-15 22:17:52 +01:00
|
|
|
SPDR = 0xFF;
|
2017-06-18 01:36:10 +02:00
|
|
|
while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ }
|
|
|
|
return SPDR;
|
|
|
|
}
|
2019-02-20 11:00:49 +01:00
|
|
|
|
2017-06-18 01:36:10 +02:00
|
|
|
/** SPI read data */
|
|
|
|
void spiRead(uint8_t* buf, uint16_t nbyte) {
|
|
|
|
if (nbyte-- == 0) return;
|
2017-12-15 22:17:52 +01:00
|
|
|
SPDR = 0xFF;
|
2017-06-18 01:36:10 +02:00
|
|
|
for (uint16_t i = 0; i < nbyte; i++) {
|
|
|
|
while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ }
|
|
|
|
buf[i] = SPDR;
|
2017-12-15 22:17:52 +01:00
|
|
|
SPDR = 0xFF;
|
2017-06-18 01:36:10 +02:00
|
|
|
}
|
|
|
|
while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ }
|
|
|
|
buf[nbyte] = SPDR;
|
|
|
|
}
|
2019-02-20 11:00:49 +01:00
|
|
|
|
2017-06-18 01:36:10 +02:00
|
|
|
/** SPI send a byte */
|
|
|
|
void spiSend(uint8_t b) {
|
|
|
|
SPDR = b;
|
|
|
|
while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ }
|
|
|
|
}
|
2019-02-20 11:00:49 +01:00
|
|
|
|
2017-06-18 01:36:10 +02:00
|
|
|
/** SPI send block */
|
|
|
|
void spiSendBlock(uint8_t token, const uint8_t* buf) {
|
|
|
|
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 12:12:25 +01: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 17:10:55 +01:00
|
|
|
|
2018-01-03 12:12:25 +01: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-26 06:32:37 +02: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 12:12:25 +01: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-26 06:32:37 +02:00
|
|
|
if (clockDiv == 6) clockDiv = 7;
|
2018-01-03 12:12:25 +01:00
|
|
|
|
|
|
|
// Invert the SPI2X bit
|
|
|
|
clockDiv ^= 0x1;
|
|
|
|
|
2019-07-05 08:59:37 +02:00
|
|
|
SPCR = _BV(SPE) | _BV(MSTR) | ((bitOrder == LSBFIRST) ? _BV(DORD) : 0) |
|
2018-01-03 12:12:25 +01:00
|
|
|
(dataMode << CPHA) | ((clockDiv >> 1) << SPR0);
|
2018-01-05 17:10:55 +01:00
|
|
|
SPSR = clockDiv | 0x01;
|
2018-01-03 12:12:25 +01:00
|
|
|
}
|
|
|
|
|
2018-01-05 17:10:55 +01:00
|
|
|
|
2019-06-15 03:16:22 +02:00
|
|
|
#else // SOFTWARE_SPI || FORCE_SOFT_SPI
|
|
|
|
|
2019-07-20 21:40:49 +02:00
|
|
|
// ------------------------
|
2019-06-15 03:16:22 +02:00
|
|
|
// Software SPI
|
2019-07-20 21:40:49 +02:00
|
|
|
// ------------------------
|
2019-02-20 11:00:49 +01:00
|
|
|
|
2019-06-15 03:16:22 +02:00
|
|
|
// nop to tune soft SPI timing
|
2017-06-18 01:36:10 +02:00
|
|
|
#define nop asm volatile ("\tnop\n")
|
|
|
|
|
2019-10-01 04:44:07 +02:00
|
|
|
void spiInit(uint8_t) { /* do nothing */ }
|
2017-06-18 01:36:10 +02:00
|
|
|
|
2019-06-15 03:16:22 +02:00
|
|
|
// Begin SPI transaction, set clock, bit order, data mode
|
2019-10-01 04:44:07 +02:00
|
|
|
void spiBeginTransaction(uint32_t spiClock, uint8_t bitOrder, uint8_t dataMode) { /* do nothing */ }
|
2018-01-03 12:12:25 +01:00
|
|
|
|
2019-06-15 03:16:22 +02:00
|
|
|
// Soft SPI receive byte
|
2017-06-18 01:36:10 +02:00
|
|
|
uint8_t spiRec() {
|
|
|
|
uint8_t data = 0;
|
2018-03-08 00:53:25 +01:00
|
|
|
// no interrupts during byte receive - about 8µs
|
2017-06-18 01:36:10 +02:00
|
|
|
cli();
|
2017-12-15 22:17:52 +01:00
|
|
|
// output pin high - like sending 0xFF
|
2017-06-18 01:36:10 +02:00
|
|
|
WRITE(MOSI_PIN, HIGH);
|
|
|
|
|
2020-03-14 05:18:16 +01:00
|
|
|
LOOP_L_N(i, 8) {
|
2017-06-18 01:36:10 +02:00
|
|
|
WRITE(SCK_PIN, HIGH);
|
|
|
|
|
2019-02-20 11:00:49 +01:00
|
|
|
nop; // adjust so SCK is nice
|
2017-06-18 01:36:10 +02:00
|
|
|
nop;
|
|
|
|
|
|
|
|
data <<= 1;
|
|
|
|
|
|
|
|
if (READ(MISO_PIN)) data |= 1;
|
|
|
|
|
|
|
|
WRITE(SCK_PIN, LOW);
|
|
|
|
}
|
2019-02-20 11:00:49 +01:00
|
|
|
|
2017-06-18 01:36:10 +02:00
|
|
|
sei();
|
|
|
|
return data;
|
|
|
|
}
|
2019-02-20 11:00:49 +01:00
|
|
|
|
2019-06-15 03:16:22 +02:00
|
|
|
// Soft SPI read data
|
2017-06-18 01:36:10 +02:00
|
|
|
void spiRead(uint8_t* buf, uint16_t nbyte) {
|
|
|
|
for (uint16_t i = 0; i < nbyte; i++)
|
|
|
|
buf[i] = spiRec();
|
|
|
|
}
|
2019-02-20 11:00:49 +01:00
|
|
|
|
2019-06-15 03:16:22 +02:00
|
|
|
// Soft SPI send byte
|
2017-06-18 01:36:10 +02:00
|
|
|
void spiSend(uint8_t data) {
|
2018-03-08 00:53:25 +01:00
|
|
|
// no interrupts during byte send - about 8µs
|
2017-06-18 01:36:10 +02:00
|
|
|
cli();
|
2020-03-14 05:18:16 +01:00
|
|
|
LOOP_L_N(i, 8) {
|
2017-06-18 01:36:10 +02:00
|
|
|
WRITE(SCK_PIN, LOW);
|
2017-12-15 22:17:52 +01:00
|
|
|
WRITE(MOSI_PIN, data & 0x80);
|
2017-06-18 01:36:10 +02:00
|
|
|
data <<= 1;
|
|
|
|
WRITE(SCK_PIN, HIGH);
|
|
|
|
}
|
2019-02-20 11:00:49 +01:00
|
|
|
|
|
|
|
nop; // hold SCK high for a few ns
|
2017-06-18 01:36:10 +02:00
|
|
|
nop;
|
|
|
|
nop;
|
|
|
|
nop;
|
|
|
|
|
|
|
|
WRITE(SCK_PIN, LOW);
|
2019-02-20 11:00:49 +01:00
|
|
|
|
2017-06-18 01:36:10 +02:00
|
|
|
sei();
|
|
|
|
}
|
2019-02-20 11:00:49 +01:00
|
|
|
|
2019-06-15 03:16:22 +02:00
|
|
|
// Soft SPI send block
|
2017-06-18 01:36:10 +02:00
|
|
|
void spiSendBlock(uint8_t token, const uint8_t* buf) {
|
|
|
|
spiSend(token);
|
|
|
|
for (uint16_t i = 0; i < 512; i++)
|
|
|
|
spiSend(buf[i]);
|
2018-01-05 17:10:55 +01:00
|
|
|
}
|
2017-06-18 01:36:10 +02:00
|
|
|
|
2019-06-15 03:16:22 +02:00
|
|
|
#endif // SOFTWARE_SPI || FORCE_SOFT_SPI
|
2017-06-18 01:36:10 +02:00
|
|
|
|
2017-09-24 06:25:28 +02:00
|
|
|
#endif // __AVR__
|