My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

HAL_spi_ESP32.cpp 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
  7. * Copyright (c) 2017 Victor Perez
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. #ifdef ARDUINO_ARCH_ESP32
  24. // --------------------------------------------------------------------------
  25. // Includes
  26. // --------------------------------------------------------------------------
  27. #include "HAL.h"
  28. #include "../shared/HAL_SPI.h"
  29. #include <pins_arduino.h>
  30. #include "spi_pins.h"
  31. #include "../../core/macros.h"
  32. #include <SPI.h>
  33. // --------------------------------------------------------------------------
  34. // Public Variables
  35. // --------------------------------------------------------------------------
  36. static SPISettings spiConfig;
  37. // --------------------------------------------------------------------------
  38. // Public functions
  39. // --------------------------------------------------------------------------
  40. #if ENABLED(SOFTWARE_SPI)
  41. // --------------------------------------------------------------------------
  42. // Software SPI
  43. // --------------------------------------------------------------------------
  44. #error "Software SPI not supported for ESP32. Use Hardware SPI."
  45. #else
  46. // --------------------------------------------------------------------------
  47. // Hardware SPI
  48. // --------------------------------------------------------------------------
  49. void spiBegin() {
  50. #if !PIN_EXISTS(SS)
  51. #error "SS_PIN not defined!"
  52. #endif
  53. OUT_WRITE(SS_PIN, HIGH);
  54. }
  55. void spiInit(uint8_t spiRate) {
  56. uint32_t clock;
  57. switch (spiRate) {
  58. case SPI_FULL_SPEED: clock = 16000000; break;
  59. case SPI_HALF_SPEED: clock = 8000000; break;
  60. case SPI_QUARTER_SPEED: clock = 4000000; break;
  61. case SPI_EIGHTH_SPEED: clock = 2000000; break;
  62. case SPI_SIXTEENTH_SPEED: clock = 1000000; break;
  63. case SPI_SPEED_5: clock = 500000; break;
  64. case SPI_SPEED_6: clock = 250000; break;
  65. default: clock = 1000000; // Default from the SPI library
  66. }
  67. spiConfig = SPISettings(clock, MSBFIRST, SPI_MODE0);
  68. SPI.begin();
  69. }
  70. uint8_t spiRec(void) {
  71. SPI.beginTransaction(spiConfig);
  72. uint8_t returnByte = SPI.transfer(0xFF);
  73. SPI.endTransaction();
  74. return returnByte;
  75. }
  76. void spiRead(uint8_t* buf, uint16_t nbyte) {
  77. SPI.beginTransaction(spiConfig);
  78. SPI.transferBytes(0, buf, nbyte);
  79. SPI.endTransaction();
  80. }
  81. void spiSend(uint8_t b) {
  82. SPI.beginTransaction(spiConfig);
  83. SPI.transfer(b);
  84. SPI.endTransaction();
  85. }
  86. void spiSendBlock(uint8_t token, const uint8_t* buf) {
  87. SPI.beginTransaction(spiConfig);
  88. SPI.transfer(token);
  89. SPI.writeBytes(const_cast<uint8_t*>(buf), 512);
  90. SPI.endTransaction();
  91. }
  92. void spiBeginTransaction(uint32_t spiClock, uint8_t bitOrder, uint8_t dataMode) {
  93. spiConfig = SPISettings(spiClock, bitOrder, dataMode);
  94. SPI.beginTransaction(spiConfig);
  95. }
  96. #endif // !SOFTWARE_SPI
  97. #endif // ARDUINO_ARCH_ESP32