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.cpp 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 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. #include "HAL.h"
  25. #include "../shared/HAL_SPI.h"
  26. #include <pins_arduino.h>
  27. #include "spi_pins.h"
  28. #include <SPI.h>
  29. #include "../../core/macros.h"
  30. // ------------------------
  31. // Public Variables
  32. // ------------------------
  33. static SPISettings spiConfig;
  34. // ------------------------
  35. // Public functions
  36. // ------------------------
  37. #if ENABLED(SOFTWARE_SPI)
  38. // ------------------------
  39. // Software SPI
  40. // ------------------------
  41. #error "Software SPI not supported for ESP32. Use Hardware SPI."
  42. #else
  43. // ------------------------
  44. // Hardware SPI
  45. // ------------------------
  46. void spiBegin() {
  47. #if !PIN_EXISTS(SS)
  48. #error "SS_PIN not defined!"
  49. #endif
  50. OUT_WRITE(SS_PIN, HIGH);
  51. }
  52. void spiInit(uint8_t spiRate) {
  53. uint32_t clock;
  54. switch (spiRate) {
  55. case SPI_FULL_SPEED: clock = 16000000; break;
  56. case SPI_HALF_SPEED: clock = 8000000; break;
  57. case SPI_QUARTER_SPEED: clock = 4000000; break;
  58. case SPI_EIGHTH_SPEED: clock = 2000000; break;
  59. case SPI_SIXTEENTH_SPEED: clock = 1000000; break;
  60. case SPI_SPEED_5: clock = 500000; break;
  61. case SPI_SPEED_6: clock = 250000; break;
  62. default: clock = 1000000; // Default from the SPI library
  63. }
  64. spiConfig = SPISettings(clock, MSBFIRST, SPI_MODE0);
  65. SPI.begin();
  66. }
  67. uint8_t spiRec() {
  68. SPI.beginTransaction(spiConfig);
  69. uint8_t returnByte = SPI.transfer(0xFF);
  70. SPI.endTransaction();
  71. return returnByte;
  72. }
  73. void spiRead(uint8_t* buf, uint16_t nbyte) {
  74. SPI.beginTransaction(spiConfig);
  75. SPI.transferBytes(0, buf, nbyte);
  76. SPI.endTransaction();
  77. }
  78. void spiSend(uint8_t b) {
  79. SPI.beginTransaction(spiConfig);
  80. SPI.transfer(b);
  81. SPI.endTransaction();
  82. }
  83. void spiSendBlock(uint8_t token, const uint8_t* buf) {
  84. SPI.beginTransaction(spiConfig);
  85. SPI.transfer(token);
  86. SPI.writeBytes(const_cast<uint8_t*>(buf), 512);
  87. SPI.endTransaction();
  88. }
  89. void spiBeginTransaction(uint32_t spiClock, uint8_t bitOrder, uint8_t dataMode) {
  90. spiConfig = SPISettings(spiClock, bitOrder, dataMode);
  91. SPI.beginTransaction(spiConfig);
  92. }
  93. #endif // !SOFTWARE_SPI
  94. #endif // ARDUINO_ARCH_ESP32