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 2.9KB

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