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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /**
  23. * SAMD51 HAL developed by Giuliano Zaro (AKA GMagician)
  24. */
  25. /**
  26. * Hardware and software SPI implementations are included in this file.
  27. *
  28. * Control of the slave select pin(s) is handled by the calling routines and
  29. * SAMD51 let hardware SPI handling to remove SS from its logic.
  30. */
  31. #ifdef __SAMD51__
  32. // --------------------------------------------------------------------------
  33. // Includes
  34. // --------------------------------------------------------------------------
  35. #include "../../inc/MarlinConfig.h"
  36. #include <SPI.h>
  37. // --------------------------------------------------------------------------
  38. // Public functions
  39. // --------------------------------------------------------------------------
  40. #if EITHER(SOFTWARE_SPI, FORCE_SOFT_SPI)
  41. // ------------------------
  42. // Software SPI
  43. // ------------------------
  44. #error "Software SPI not supported for SAMD51. Use Hardware SPI."
  45. #else // !SOFTWARE_SPI
  46. #ifdef ADAFRUIT_GRAND_CENTRAL_M4
  47. #if SD_CONNECTION_IS(ONBOARD)
  48. #define sdSPI SDCARD_SPI
  49. #else
  50. #define sdSPI SPI
  51. #endif
  52. #endif
  53. static SPISettings spiConfig;
  54. // ------------------------
  55. // Hardware SPI
  56. // ------------------------
  57. void spiBegin() {
  58. spiInit(SPI_HALF_SPEED);
  59. }
  60. void spiInit(uint8_t spiRate) {
  61. // Use datarates Marlin uses
  62. uint32_t clock;
  63. switch (spiRate) {
  64. case SPI_FULL_SPEED: clock = 8000000; break;
  65. case SPI_HALF_SPEED: clock = 4000000; break;
  66. case SPI_QUARTER_SPEED: clock = 2000000; break;
  67. case SPI_EIGHTH_SPEED: clock = 1000000; break;
  68. case SPI_SIXTEENTH_SPEED: clock = 500000; break;
  69. case SPI_SPEED_5: clock = 250000; break;
  70. case SPI_SPEED_6: clock = 125000; break;
  71. default: clock = 4000000; break; // Default from the SPI library
  72. }
  73. spiConfig = SPISettings(clock, MSBFIRST, SPI_MODE0);
  74. sdSPI.begin();
  75. }
  76. /**
  77. * @brief Receives a single byte from the SPI port.
  78. *
  79. * @return Byte received
  80. *
  81. * @details
  82. */
  83. uint8_t spiRec() {
  84. sdSPI.beginTransaction(spiConfig);
  85. uint8_t returnByte = sdSPI.transfer(0xFF);
  86. sdSPI.endTransaction();
  87. return returnByte;
  88. }
  89. /**
  90. * @brief Receives a number of bytes from the SPI port to a buffer
  91. *
  92. * @param buf Pointer to starting address of buffer to write to.
  93. * @param nbyte Number of bytes to receive.
  94. * @return Nothing
  95. */
  96. void spiRead(uint8_t *buf, uint16_t nbyte) {
  97. if (nbyte == 0) return;
  98. memset(buf, 0xFF, nbyte);
  99. sdSPI.beginTransaction(spiConfig);
  100. sdSPI.transfer(buf, nbyte);
  101. sdSPI.endTransaction();
  102. }
  103. /**
  104. * @brief Sends a single byte on SPI port
  105. *
  106. * @param b Byte to send
  107. *
  108. * @details
  109. */
  110. void spiSend(uint8_t b) {
  111. sdSPI.beginTransaction(spiConfig);
  112. sdSPI.transfer(b);
  113. sdSPI.endTransaction();
  114. }
  115. /**
  116. * @brief Write token and then write from 512 byte buffer to SPI (for SD card)
  117. *
  118. * @param buf Pointer with buffer start address
  119. * @return Nothing
  120. *
  121. * @details Uses DMA
  122. */
  123. void spiSendBlock(uint8_t token, const uint8_t *buf) {
  124. sdSPI.beginTransaction(spiConfig);
  125. sdSPI.transfer(token);
  126. sdSPI.transfer((uint8_t*)buf, nullptr, 512);
  127. sdSPI.endTransaction();
  128. }
  129. void spiBeginTransaction(uint32_t spiClock, uint8_t bitOrder, uint8_t dataMode) {
  130. spiConfig = SPISettings(spiClock, (BitOrder)bitOrder, dataMode);
  131. sdSPI.beginTransaction(spiConfig);
  132. }
  133. #endif // !SOFTWARE_SPI
  134. #endif // __SAMD51__