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_Stm32f1.cpp 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2016 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. // --------------------------------------------------------------------------
  32. // Includes
  33. // --------------------------------------------------------------------------
  34. #include "../HAL.h"
  35. #include "../HAL_SPI.h"
  36. #include "pins_arduino.h"
  37. #include "spi_pins.h"
  38. #include "../../core/macros.h"
  39. // --------------------------------------------------------------------------
  40. // Public Variables
  41. // --------------------------------------------------------------------------
  42. static SPISettings spiConfig;
  43. // --------------------------------------------------------------------------
  44. // Public functions
  45. // --------------------------------------------------------------------------
  46. #if ENABLED(SOFTWARE_SPI)
  47. // --------------------------------------------------------------------------
  48. // Software SPI
  49. // --------------------------------------------------------------------------
  50. #error "Software SPI not supported for STM32F1. Use hardware SPI."
  51. #else
  52. // --------------------------------------------------------------------------
  53. // Hardware SPI
  54. // --------------------------------------------------------------------------
  55. /**
  56. * VGPV SPI speed start and F_CPU/2, by default 72/2 = 36Mhz
  57. */
  58. /**
  59. * @brief Begin SPI port setup
  60. *
  61. * @return Nothing
  62. *
  63. * @details Only configures SS pin since libmaple creates and initialize the SPI object
  64. */
  65. void spiBegin() {
  66. #if !PIN_EXISTS(SS)
  67. #error "SS_PIN not defined!"
  68. #endif
  69. WRITE(SS_PIN, HIGH);
  70. SET_OUTPUT(SS_PIN);
  71. }
  72. /**
  73. * @brief Initializes SPI port to required speed rate and transfer mode (MSB, SPI MODE 0)
  74. *
  75. * @param spiRate Rate as declared in HAL.h (speed do not match AVR)
  76. * @return Nothing
  77. *
  78. * @details
  79. */
  80. void spiInit(uint8_t spiRate) {
  81. uint8_t clock;
  82. switch (spiRate) {
  83. case SPI_FULL_SPEED: clock = SPI_CLOCK_DIV2 ; break;
  84. case SPI_HALF_SPEED: clock = SPI_CLOCK_DIV4 ; break;
  85. case SPI_QUARTER_SPEED: clock = SPI_CLOCK_DIV8 ; break;
  86. case SPI_EIGHTH_SPEED: clock = SPI_CLOCK_DIV16; break;
  87. case SPI_SPEED_5: clock = SPI_CLOCK_DIV32; break;
  88. case SPI_SPEED_6: clock = SPI_CLOCK_DIV64; break;
  89. default:
  90. clock = SPI_CLOCK_DIV2; // Default from the SPI library
  91. }
  92. spiConfig = SPISettings(clock, MSBFIRST, SPI_MODE0);
  93. SPI.begin();
  94. }
  95. /**
  96. * @brief Receives a single byte from the SPI port.
  97. *
  98. * @return Byte received
  99. *
  100. * @details
  101. */
  102. uint8_t spiRec(void) {
  103. SPI.beginTransaction(spiConfig);
  104. uint8_t returnByte = SPI.transfer(0xFF);
  105. SPI.endTransaction();
  106. return returnByte;
  107. }
  108. /**
  109. * @brief Receives a number of bytes from the SPI port to a buffer
  110. *
  111. * @param buf Pointer to starting address of buffer to write to.
  112. * @param nbyte Number of bytes to receive.
  113. * @return Nothing
  114. *
  115. * @details Uses DMA
  116. */
  117. void spiRead(uint8_t* buf, uint16_t nbyte) {
  118. SPI.beginTransaction(spiConfig);
  119. SPI.dmaTransfer(0, const_cast<uint8*>(buf), nbyte);
  120. SPI.endTransaction();
  121. }
  122. /**
  123. * @brief Sends a single byte on SPI port
  124. *
  125. * @param b Byte to send
  126. *
  127. * @details
  128. */
  129. void spiSend(uint8_t b) {
  130. SPI.beginTransaction(spiConfig);
  131. SPI.send(b);
  132. SPI.endTransaction();
  133. }
  134. /**
  135. * @brief Write token and then write from 512 byte buffer to SPI (for SD card)
  136. *
  137. * @param buf Pointer with buffer start address
  138. * @return Nothing
  139. *
  140. * @details Use DMA
  141. */
  142. void spiSendBlock(uint8_t token, const uint8_t* buf) {
  143. SPI.beginTransaction(spiConfig);
  144. SPI.send(token);
  145. SPI.dmaSend(const_cast<uint8*>(buf), 512);
  146. SPI.endTransaction();
  147. }
  148. /** Begin SPI transaction, set clock, bit order, data mode */
  149. void spiBeginTransaction(uint32_t spiClock, uint8_t bitOrder, uint8_t dataMode) {
  150. spiConfig = SPISettings(spiClock, bitOrder, dataMode);
  151. SPI.beginTransaction(spiConfig);
  152. }
  153. #endif // SOFTWARE_SPI
  154. #endif // __STM32F1__