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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. // --------------------------------------------------------------------------
  41. // Hardware SPI
  42. // --------------------------------------------------------------------------
  43. void spiBegin() {
  44. #if !PIN_EXISTS(SS)
  45. #error "SS_PIN not defined!"
  46. #endif
  47. WRITE(SS_PIN, HIGH);
  48. SET_OUTPUT(SS_PIN);
  49. }
  50. void spiInit(uint8_t spiRate) {
  51. uint32_t clock;
  52. switch (spiRate) {
  53. case SPI_FULL_SPEED: clock = SPI_CLOCK_DIV2; break;
  54. case SPI_HALF_SPEED: clock = SPI_CLOCK_DIV4; break;
  55. case SPI_QUARTER_SPEED: clock = SPI_CLOCK_DIV8; break;
  56. case SPI_EIGHTH_SPEED: clock = SPI_CLOCK_DIV16; break;
  57. case SPI_SPEED_5: clock = SPI_CLOCK_DIV32; break;
  58. case SPI_SPEED_6: clock = SPI_CLOCK_DIV64; break;
  59. default: clock = SPI_CLOCK_DIV2; // Default from the SPI library
  60. }
  61. spiConfig = SPISettings(clock, MSBFIRST, SPI_MODE0);
  62. SPI.begin();
  63. }
  64. uint8_t spiRec(void) {
  65. SPI.beginTransaction(spiConfig);
  66. uint8_t returnByte = SPI.transfer(0xFF);
  67. SPI.endTransaction();
  68. return returnByte;
  69. }
  70. void spiRead(uint8_t* buf, uint16_t nbyte) {
  71. SPI.beginTransaction(spiConfig);
  72. SPI.transferBytes(0, buf, nbyte);
  73. SPI.endTransaction();
  74. }
  75. void spiSend(uint8_t b) {
  76. SPI.beginTransaction(spiConfig);
  77. SPI.transfer(b);
  78. SPI.endTransaction();
  79. }
  80. void spiSendBlock(uint8_t token, const uint8_t* buf) {
  81. SPI.beginTransaction(spiConfig);
  82. SPI.transfer(token);
  83. SPI.writeBytes(const_cast<uint8_t*>(buf), 512);
  84. SPI.endTransaction();
  85. }
  86. void spiBeginTransaction(uint32_t spiClock, uint8_t bitOrder, uint8_t dataMode) {
  87. spiConfig = SPISettings(spiClock, bitOrder, dataMode);
  88. SPI.beginTransaction(spiConfig);
  89. }
  90. #endif // ARDUINO_ARCH_ESP32