Firmware2/Marlin/src/HAL/ESP32/HAL_SPI.cpp

116 lines
3.0 KiB
C++
Raw Normal View History

2017-10-08 18:38: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-10-08 18:38: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
* Copyright (c) 2017 Victor Perez
2017-10-08 18:38: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
2020-07-23 05:20:14 +02:00
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2017-10-08 18:38:10 +02:00
*
*/
#ifdef ARDUINO_ARCH_ESP32
#include "../../inc/MarlinConfig.h"
#include "../shared/HAL_SPI.h"
2019-06-23 10:43:36 +02:00
#include <pins_arduino.h>
2017-10-08 18:38:10 +02:00
#include <SPI.h>
2019-07-10 05:30:06 +02:00
// ------------------------
2017-10-08 18:38:10 +02:00
// Public Variables
2019-07-10 05:30:06 +02:00
// ------------------------
2017-10-08 18:38:10 +02:00
static SPISettings spiConfig;
2019-07-10 05:30:06 +02:00
// ------------------------
2017-10-08 18:38:10 +02:00
// Public functions
2019-07-10 05:30:06 +02:00
// ------------------------
2017-10-08 18:38:10 +02:00
#if ENABLED(SOFTWARE_SPI)
2019-07-10 05:30:06 +02:00
// ------------------------
// Software SPI
2019-07-10 05:30:06 +02:00
// ------------------------
#error "Software SPI not supported for ESP32. Use Hardware SPI."
#else
2019-07-10 05:30:06 +02:00
// ------------------------
2017-10-08 18:38:10 +02:00
// Hardware SPI
2019-07-10 05:30:06 +02:00
// ------------------------
2017-10-08 18:38:10 +02:00
void spiBegin() {
#if !PIN_EXISTS(SD_SS)
#error "SD_SS_PIN not defined!"
2017-10-08 18:38:10 +02:00
#endif
OUT_WRITE(SD_SS_PIN, HIGH);
2017-10-08 18:38:10 +02:00
}
void spiInit(uint8_t spiRate) {
uint32_t clock;
switch (spiRate) {
case SPI_FULL_SPEED: clock = 16000000; break;
case SPI_HALF_SPEED: clock = 8000000; break;
case SPI_QUARTER_SPEED: clock = 4000000; break;
case SPI_EIGHTH_SPEED: clock = 2000000; break;
case SPI_SIXTEENTH_SPEED: clock = 1000000; break;
case SPI_SPEED_5: clock = 500000; break;
case SPI_SPEED_6: clock = 250000; break;
default: clock = 1000000; // Default from the SPI library
2017-10-08 18:38:10 +02:00
}
spiConfig = SPISettings(clock, MSBFIRST, SPI_MODE0);
SPI.begin();
}
2019-09-17 03:31:08 +02:00
uint8_t spiRec() {
2017-10-08 18:38:10 +02:00
SPI.beginTransaction(spiConfig);
uint8_t returnByte = SPI.transfer(0xFF);
SPI.endTransaction();
return returnByte;
}
2021-03-30 03:36:37 +02:00
void spiRead(uint8_t *buf, uint16_t nbyte) {
2017-10-08 18:38:10 +02:00
SPI.beginTransaction(spiConfig);
SPI.transferBytes(0, buf, nbyte);
SPI.endTransaction();
}
void spiSend(uint8_t b) {
SPI.beginTransaction(spiConfig);
SPI.transfer(b);
SPI.endTransaction();
}
2021-03-30 03:36:37 +02:00
void spiSendBlock(uint8_t token, const uint8_t *buf) {
2017-10-08 18:38:10 +02:00
SPI.beginTransaction(spiConfig);
SPI.transfer(token);
SPI.writeBytes(const_cast<uint8_t*>(buf), 512);
SPI.endTransaction();
}
void spiBeginTransaction(uint32_t spiClock, uint8_t bitOrder, uint8_t dataMode) {
spiConfig = SPISettings(spiClock, bitOrder, dataMode);
SPI.beginTransaction(spiConfig);
}
#endif // !SOFTWARE_SPI
2017-10-08 18:38:10 +02:00
#endif // ARDUINO_ARCH_ESP32