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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. #if defined(STM32GENERIC) && (defined(STM32F4) || defined(STM32F7))
  24. /**
  25. * Software SPI functions originally from Arduino Sd2Card Library
  26. * Copyright (c) 2009 by William Greiman
  27. */
  28. /**
  29. * Adapted to the Marlin STM32F4/7 HAL
  30. */
  31. #include "../../inc/MarlinConfig.h"
  32. #include <SPI.h>
  33. #include <pins_arduino.h>
  34. #include "../shared/HAL_SPI.h"
  35. #include "spi_pins.h"
  36. // ------------------------
  37. // Public Variables
  38. // ------------------------
  39. static SPISettings spiConfig;
  40. // ------------------------
  41. // Public functions
  42. // ------------------------
  43. #if ENABLED(SOFTWARE_SPI)
  44. // ------------------------
  45. // Software SPI
  46. // ------------------------
  47. #error "Software SPI not supported for STM32F4/7. Use Hardware SPI."
  48. #else
  49. // ------------------------
  50. // Hardware SPI
  51. // ------------------------
  52. /**
  53. * VGPV SPI speed start and F_CPU/2, by default 72/2 = 36Mhz
  54. */
  55. /**
  56. * @brief Begin SPI port setup
  57. *
  58. * @return Nothing
  59. *
  60. * @details Only configures SS pin since libmaple creates and initialize the SPI object
  61. */
  62. void spiBegin() {
  63. #if !defined(SS_PIN) || SS_PIN < 0
  64. #error "SS_PIN not defined!"
  65. #endif
  66. OUT_WRITE(SS_PIN, HIGH);
  67. }
  68. /** Configure SPI for specified SPI speed */
  69. void spiInit(uint8_t spiRate) {
  70. // Use datarates Marlin uses
  71. uint32_t clock;
  72. switch (spiRate) {
  73. case SPI_FULL_SPEED: clock = 20000000; break; // 13.9mhz=20000000 6.75mhz=10000000 3.38mhz=5000000 .833mhz=1000000
  74. case SPI_HALF_SPEED: clock = 5000000; break;
  75. case SPI_QUARTER_SPEED: clock = 2500000; break;
  76. case SPI_EIGHTH_SPEED: clock = 1250000; break;
  77. case SPI_SPEED_5: clock = 625000; break;
  78. case SPI_SPEED_6: clock = 300000; break;
  79. default: clock = 4000000; // Default from the SPI libarary
  80. }
  81. spiConfig = SPISettings(clock, MSBFIRST, SPI_MODE0);
  82. SPI.begin();
  83. }
  84. /**
  85. * @brief Receives a single byte from the SPI port.
  86. *
  87. * @return Byte received
  88. *
  89. * @details
  90. */
  91. uint8_t spiRec() {
  92. SPI.beginTransaction(spiConfig);
  93. uint8_t returnByte = SPI.transfer(0xFF);
  94. SPI.endTransaction();
  95. return returnByte;
  96. }
  97. /**
  98. * @brief Receives a number of bytes from the SPI port to a buffer
  99. *
  100. * @param buf Pointer to starting address of buffer to write to.
  101. * @param nbyte Number of bytes to receive.
  102. * @return Nothing
  103. *
  104. * @details Uses DMA
  105. */
  106. void spiRead(uint8_t* buf, uint16_t nbyte) {
  107. SPI.beginTransaction(spiConfig);
  108. #ifndef STM32GENERIC
  109. SPI.dmaTransfer(0, const_cast<uint8_t*>(buf), nbyte);
  110. #else
  111. SPI.transfer((uint8_t*)buf, nbyte);
  112. #endif
  113. SPI.endTransaction();
  114. }
  115. /**
  116. * @brief Sends a single byte on SPI port
  117. *
  118. * @param b Byte to send
  119. *
  120. * @details
  121. */
  122. void spiSend(uint8_t b) {
  123. SPI.beginTransaction(spiConfig);
  124. SPI.transfer(b);
  125. SPI.endTransaction();
  126. }
  127. /**
  128. * @brief Write token and then write from 512 byte buffer to SPI (for SD card)
  129. *
  130. * @param buf Pointer with buffer start address
  131. * @return Nothing
  132. *
  133. * @details Use DMA
  134. */
  135. void spiSendBlock(uint8_t token, const uint8_t* buf) {
  136. SPI.beginTransaction(spiConfig);
  137. SPI.transfer(token);
  138. #ifndef STM32GENERIC
  139. SPI.dmaSend(const_cast<uint8_t*>(buf), 512);
  140. #else
  141. SPI.transfer((uint8_t*)buf, nullptr, 512);
  142. #endif
  143. SPI.endTransaction();
  144. }
  145. #endif // SOFTWARE_SPI
  146. #endif // STM32GENERIC && (STM32F4 || STM32F7)