My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

HAL_SPI.cpp 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. #if defined(ARDUINO_ARCH_STM32) && !defined(STM32GENERIC)
  24. #include "../../inc/MarlinConfig.h"
  25. #include <SPI.h>
  26. // ------------------------
  27. // Public Variables
  28. // ------------------------
  29. static SPISettings spiConfig;
  30. // ------------------------
  31. // Public functions
  32. // ------------------------
  33. #if ENABLED(SOFTWARE_SPI)
  34. // ------------------------
  35. // Software SPI
  36. // ------------------------
  37. #include "../shared/Delay.h"
  38. void spiBegin(void) {
  39. OUT_WRITE(SS_PIN, HIGH);
  40. OUT_WRITE(SCK_PIN, HIGH);
  41. SET_INPUT(MISO_PIN);
  42. OUT_WRITE(MOSI_PIN, HIGH);
  43. }
  44. static uint16_t delay_STM32_soft_spi;
  45. void spiInit(uint8_t spiRate) {
  46. // Use datarates Marlin uses
  47. switch (spiRate) {
  48. case SPI_FULL_SPEED: delay_STM32_soft_spi = 125; break; // desired: 8,000,000 actual: ~1.1M
  49. case SPI_HALF_SPEED: delay_STM32_soft_spi = 125; break; // desired: 4,000,000 actual: ~1.1M
  50. case SPI_QUARTER_SPEED:delay_STM32_soft_spi = 250; break; // desired: 2,000,000 actual: ~890K
  51. case SPI_EIGHTH_SPEED: delay_STM32_soft_spi = 500; break; // desired: 1,000,000 actual: ~590K
  52. case SPI_SPEED_5: delay_STM32_soft_spi = 1000; break; // desired: 500,000 actual: ~360K
  53. case SPI_SPEED_6: delay_STM32_soft_spi = 2000; break; // desired: 250,000 actual: ~210K
  54. default: delay_STM32_soft_spi = 4000; break; // desired: 125,000 actual: ~123K
  55. }
  56. SPI.begin();
  57. }
  58. // Begin SPI transaction, set clock, bit order, data mode
  59. void spiBeginTransaction(uint32_t spiClock, uint8_t bitOrder, uint8_t dataMode) { /* do nothing */ }
  60. uint8_t HAL_SPI_STM32_SpiTransfer_Mode_3(uint8_t b) { // using Mode 3
  61. for (uint8_t bits = 8; bits--;) {
  62. WRITE(SCK_PIN, LOW);
  63. WRITE(MOSI_PIN, b & 0x80);
  64. DELAY_NS(delay_STM32_soft_spi);
  65. WRITE(SCK_PIN, HIGH);
  66. DELAY_NS(delay_STM32_soft_spi);
  67. b <<= 1; // little setup time
  68. b |= (READ(MISO_PIN) != 0);
  69. }
  70. DELAY_NS(125);
  71. return b;
  72. }
  73. // Soft SPI receive byte
  74. uint8_t spiRec() {
  75. DISABLE_ISRS(); // No interrupts during byte receive
  76. const uint8_t data = HAL_SPI_STM32_SpiTransfer_Mode_3(0xFF);
  77. ENABLE_ISRS(); // Enable interrupts
  78. return data;
  79. }
  80. // Soft SPI read data
  81. void spiRead(uint8_t *buf, uint16_t nbyte) {
  82. for (uint16_t i = 0; i < nbyte; i++)
  83. buf[i] = spiRec();
  84. }
  85. // Soft SPI send byte
  86. void spiSend(uint8_t data) {
  87. DISABLE_ISRS(); // No interrupts during byte send
  88. HAL_SPI_STM32_SpiTransfer_Mode_3(data); // Don't care what is received
  89. ENABLE_ISRS(); // Enable interrupts
  90. }
  91. // Soft SPI send block
  92. void spiSendBlock(uint8_t token, const uint8_t *buf) {
  93. spiSend(token);
  94. for (uint16_t i = 0; i < 512; i++)
  95. spiSend(buf[i]);
  96. }
  97. #else
  98. // ------------------------
  99. // Hardware SPI
  100. // ------------------------
  101. /**
  102. * VGPV SPI speed start and PCLK2/2, by default 108/2 = 54Mhz
  103. */
  104. /**
  105. * @brief Begin SPI port setup
  106. *
  107. * @return Nothing
  108. *
  109. * @details Only configures SS pin since stm32duino creates and initialize the SPI object
  110. */
  111. void spiBegin() {
  112. #if !PIN_EXISTS(SS)
  113. #error "SS_PIN not defined!"
  114. #endif
  115. OUT_WRITE(SS_PIN, HIGH);
  116. }
  117. // Configure SPI for specified SPI speed
  118. void spiInit(uint8_t spiRate) {
  119. // Use datarates Marlin uses
  120. uint32_t clock;
  121. switch (spiRate) {
  122. case SPI_FULL_SPEED: clock = 20000000; break; // 13.9mhz=20000000 6.75mhz=10000000 3.38mhz=5000000 .833mhz=1000000
  123. case SPI_HALF_SPEED: clock = 5000000; break;
  124. case SPI_QUARTER_SPEED: clock = 2500000; break;
  125. case SPI_EIGHTH_SPEED: clock = 1250000; break;
  126. case SPI_SPEED_5: clock = 625000; break;
  127. case SPI_SPEED_6: clock = 300000; break;
  128. default:
  129. clock = 4000000; // Default from the SPI library
  130. }
  131. spiConfig = SPISettings(clock, MSBFIRST, SPI_MODE0);
  132. #if ENABLED(CUSTOM_SPI_PINS)
  133. SPI.setMISO(MISO_PIN);
  134. SPI.setMOSI(MOSI_PIN);
  135. SPI.setSCLK(SCK_PIN);
  136. SPI.setSSEL(SS_PIN);
  137. #endif
  138. SPI.begin();
  139. }
  140. /**
  141. * @brief Receives a single byte from the SPI port.
  142. *
  143. * @return Byte received
  144. *
  145. * @details
  146. */
  147. uint8_t spiRec() {
  148. SPI.beginTransaction(spiConfig);
  149. uint8_t returnByte = SPI.transfer(0xFF);
  150. SPI.endTransaction();
  151. return returnByte;
  152. }
  153. /**
  154. * @brief Receive a number of bytes from the SPI port to a buffer
  155. *
  156. * @param buf Pointer to starting address of buffer to write to.
  157. * @param nbyte Number of bytes to receive.
  158. * @return Nothing
  159. *
  160. * @details Uses DMA
  161. */
  162. void spiRead(uint8_t* buf, uint16_t nbyte) {
  163. if (nbyte == 0) return;
  164. memset(buf, 0xFF, nbyte);
  165. SPI.beginTransaction(spiConfig);
  166. SPI.transfer(buf, nbyte);
  167. SPI.endTransaction();
  168. }
  169. /**
  170. * @brief Send a single byte on SPI port
  171. *
  172. * @param b Byte to send
  173. *
  174. * @details
  175. */
  176. void spiSend(uint8_t b) {
  177. SPI.beginTransaction(spiConfig);
  178. SPI.transfer(b);
  179. SPI.endTransaction();
  180. }
  181. /**
  182. * @brief Write token and then write from 512 byte buffer to SPI (for SD card)
  183. *
  184. * @param buf Pointer with buffer start address
  185. * @return Nothing
  186. *
  187. * @details Use DMA
  188. */
  189. void spiSendBlock(uint8_t token, const uint8_t* buf) {
  190. uint8_t rxBuf[512];
  191. SPI.beginTransaction(spiConfig);
  192. SPI.transfer(token);
  193. SPI.transfer((uint8_t*)buf, &rxBuf, 512);
  194. SPI.endTransaction();
  195. }
  196. #endif // SOFTWARE_SPI
  197. #endif // ARDUINO_ARCH_STM32 && !STM32GENERIC