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

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