My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

HAL_spi_SAMD51.cpp 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /**
  2. * Marlin 3D Printer Firmware
  3. *
  4. * Copyright (C) 2019 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 <http://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. #if SD_CONNECTION_IS(ONBOARD)
  42. #endif
  43. #else // !SOFTWARE_SPI
  44. #ifdef ADAFRUIT_GRAND_CENTRAL_M4
  45. #if SD_CONNECTION_IS(ONBOARD)
  46. #define sdSPI SDCARD_SPI
  47. #else
  48. #define sdSPI SPI
  49. #endif
  50. #endif
  51. static SPISettings spiConfig;
  52. // ------------------------
  53. // Hardware SPI
  54. // ------------------------
  55. void spiBegin(void) {
  56. spiInit(SPI_HALF_SPEED);
  57. }
  58. void spiInit(uint8_t spiRate) {
  59. // Use datarates Marlin uses
  60. uint32_t clock;
  61. switch (spiRate) {
  62. case SPI_FULL_SPEED: clock = 8000000; break;
  63. case SPI_HALF_SPEED: clock = 4000000; break;
  64. case SPI_QUARTER_SPEED: clock = 2000000; break;
  65. case SPI_EIGHTH_SPEED: clock = 1000000; break;
  66. case SPI_SIXTEENTH_SPEED: clock = 500000; break;
  67. case SPI_SPEED_5: clock = 250000; break;
  68. case SPI_SPEED_6: clock = 125000; break;
  69. default: clock = 4000000; break; // Default from the SPI library
  70. }
  71. spiConfig = SPISettings(clock, MSBFIRST, SPI_MODE0);
  72. sdSPI.begin();
  73. }
  74. /**
  75. * @brief Receives a single byte from the SPI port.
  76. *
  77. * @return Byte received
  78. *
  79. * @details
  80. */
  81. uint8_t spiRec(void) {
  82. sdSPI.beginTransaction(spiConfig);
  83. uint8_t returnByte = sdSPI.transfer(0xFF);
  84. sdSPI.endTransaction();
  85. return returnByte;
  86. }
  87. /**
  88. * @brief Receives a number of bytes from the SPI port to a buffer
  89. *
  90. * @param buf Pointer to starting address of buffer to write to.
  91. * @param nbyte Number of bytes to receive.
  92. * @return Nothing
  93. */
  94. void spiRead(uint8_t* buf, uint16_t nbyte) {
  95. if (nbyte == 0) return;
  96. memset(buf, 0xFF, nbyte);
  97. sdSPI.beginTransaction(spiConfig);
  98. sdSPI.transfer(buf, nbyte);
  99. sdSPI.endTransaction();
  100. }
  101. /**
  102. * @brief Sends a single byte on SPI port
  103. *
  104. * @param b Byte to send
  105. *
  106. * @details
  107. */
  108. void spiSend(uint8_t b) {
  109. sdSPI.beginTransaction(spiConfig);
  110. sdSPI.transfer(b);
  111. sdSPI.endTransaction();
  112. }
  113. /**
  114. * @brief Write token and then write from 512 byte buffer to SPI (for SD card)
  115. *
  116. * @param buf Pointer with buffer start address
  117. * @return Nothing
  118. *
  119. * @details Uses DMA
  120. */
  121. void spiSendBlock(uint8_t token, const uint8_t* buf) {
  122. sdSPI.beginTransaction(spiConfig);
  123. sdSPI.transfer(token);
  124. sdSPI.transfer((uint8_t*)buf, nullptr, 512);
  125. sdSPI.endTransaction();
  126. }
  127. void spiBeginTransaction(uint32_t spiClock, uint8_t bitOrder, uint8_t dataMode) {
  128. spiConfig = SPISettings(spiClock, (BitOrder)bitOrder, dataMode);
  129. sdSPI.beginTransaction(spiConfig);
  130. }
  131. #endif // !SOFTWARE_SPI
  132. #endif // __SAMD51__