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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. /**
  24. * Software SPI functions originally from Arduino Sd2Card Library
  25. * Copyright (c) 2009 by William Greiman
  26. * Adapted to the STM32F1 HAL
  27. */
  28. #ifdef __STM32F1__
  29. #include "../../inc/MarlinConfig.h"
  30. #include <SPI.h>
  31. // ------------------------
  32. // Public functions
  33. // ------------------------
  34. #if ENABLED(SOFTWARE_SPI)
  35. // ------------------------
  36. // Software SPI
  37. // ------------------------
  38. #error "Software SPI not supported for STM32F1. Use hardware SPI."
  39. #else
  40. // ------------------------
  41. // Hardware SPI
  42. // ------------------------
  43. /**
  44. * VGPV SPI speed start and F_CPU/2, by default 72/2 = 36Mhz
  45. */
  46. /**
  47. * @brief Begin SPI port setup
  48. *
  49. * @return Nothing
  50. *
  51. * @details Only configures SS pin since libmaple creates and initialize the SPI object
  52. */
  53. void spiBegin() {
  54. #if PIN_EXISTS(SD_SS)
  55. OUT_WRITE(SD_SS_PIN, HIGH);
  56. #endif
  57. }
  58. /**
  59. * @brief Initialize SPI port to required speed rate and transfer mode (MSB, SPI MODE 0)
  60. *
  61. * @param spiRate Rate as declared in HAL.h (speed do not match AVR)
  62. * @return Nothing
  63. *
  64. * @details
  65. */
  66. void spiInit(uint8_t spiRate) {
  67. /**
  68. * STM32F1 APB2 = 72MHz, APB1 = 36MHz, max SPI speed of this MCU if 18Mhz
  69. * STM32F1 has 3 SPI ports, SPI1 in APB2, SPI2/SPI3 in APB1
  70. * so the minimum prescale of SPI1 is DIV4, SPI2/SPI3 is DIV2
  71. */
  72. #if SPI_DEVICE == 1
  73. #define SPI_CLOCK_MAX SPI_CLOCK_DIV4
  74. #else
  75. #define SPI_CLOCK_MAX SPI_CLOCK_DIV2
  76. #endif
  77. uint8_t clock;
  78. switch (spiRate) {
  79. case SPI_FULL_SPEED: clock = SPI_CLOCK_MAX ; break;
  80. case SPI_HALF_SPEED: clock = SPI_CLOCK_DIV4 ; break;
  81. case SPI_QUARTER_SPEED: clock = SPI_CLOCK_DIV8 ; break;
  82. case SPI_EIGHTH_SPEED: clock = SPI_CLOCK_DIV16; break;
  83. case SPI_SPEED_5: clock = SPI_CLOCK_DIV32; break;
  84. case SPI_SPEED_6: clock = SPI_CLOCK_DIV64; break;
  85. default: clock = SPI_CLOCK_DIV2; // Default from the SPI library
  86. }
  87. SPI.setModule(SPI_DEVICE);
  88. SPI.begin();
  89. SPI.setClockDivider(clock);
  90. SPI.setBitOrder(MSBFIRST);
  91. SPI.setDataMode(SPI_MODE0);
  92. }
  93. /**
  94. * @brief Receive a single byte from the SPI port.
  95. *
  96. * @return Byte received
  97. *
  98. * @details
  99. */
  100. uint8_t spiRec() {
  101. uint8_t returnByte = SPI.transfer(0xFF);
  102. return returnByte;
  103. }
  104. /**
  105. * @brief Receive a number of bytes from the SPI port to a buffer
  106. *
  107. * @param buf Pointer to starting address of buffer to write to.
  108. * @param nbyte Number of bytes to receive.
  109. * @return Nothing
  110. *
  111. * @details Uses DMA
  112. */
  113. void spiRead(uint8_t *buf, uint16_t nbyte) {
  114. SPI.dmaTransfer(0, const_cast<uint8_t*>(buf), nbyte);
  115. }
  116. /**
  117. * @brief Send a single byte on SPI port
  118. *
  119. * @param b Byte to send
  120. *
  121. * @details
  122. */
  123. void spiSend(uint8_t b) {
  124. SPI.send(b);
  125. }
  126. /**
  127. * @brief Write token and then write from 512 byte buffer to SPI (for SD card)
  128. *
  129. * @param buf Pointer with buffer start address
  130. * @return Nothing
  131. *
  132. * @details Use DMA
  133. */
  134. void spiSendBlock(uint8_t token, const uint8_t *buf) {
  135. SPI.send(token);
  136. SPI.dmaSend(const_cast<uint8_t*>(buf), 512);
  137. }
  138. #if ENABLED(SPI_EEPROM)
  139. // Read single byte from specified SPI channel
  140. uint8_t spiRec(uint32_t chan) { return SPI.transfer(0xFF); }
  141. // Write single byte to specified SPI channel
  142. void spiSend(uint32_t chan, byte b) { SPI.send(b); }
  143. // Write buffer to specified SPI channel
  144. void spiSend(uint32_t chan, const uint8_t *buf, size_t n) {
  145. for (size_t p = 0; p < n; p++) spiSend(chan, buf[p]);
  146. }
  147. #endif // SPI_EEPROM
  148. #endif // SOFTWARE_SPI
  149. #endif // __STM32F1__