My Marlin configs for Fabrikator Mini and CTC i3 Pro B
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

HAL_spi_STM32.cpp 4.2KB

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