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

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