My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

HAL_spi_STM32F7.cpp 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 STM32F7 HAL
  29. */
  30. #ifdef STM32F7
  31. // --------------------------------------------------------------------------
  32. // Includes
  33. // --------------------------------------------------------------------------
  34. #include "../HAL.h"
  35. //#include "../SPI.h"
  36. #include "SPI.h"
  37. //#include <SPI.h>
  38. #include "pins_arduino.h"
  39. #include "spi_pins.h"
  40. #include "../../core/macros.h"
  41. // --------------------------------------------------------------------------
  42. // Public Variables
  43. // --------------------------------------------------------------------------
  44. static SPISettings spiConfig;
  45. // --------------------------------------------------------------------------
  46. // Public functions
  47. // --------------------------------------------------------------------------
  48. #if ENABLED(SOFTWARE_SPI)
  49. // --------------------------------------------------------------------------
  50. // Software SPI
  51. // --------------------------------------------------------------------------
  52. #error "Software SPI not supported for STM32F7. Use hardware SPI."
  53. #else
  54. // --------------------------------------------------------------------------
  55. // Hardware SPI
  56. // --------------------------------------------------------------------------
  57. /**
  58. * VGPV SPI speed start and F_CPU/2, by default 72/2 = 36Mhz
  59. */
  60. /**
  61. * @brief Begin SPI port setup
  62. *
  63. * @return Nothing
  64. *
  65. * @details Only configures SS pin since libmaple creates and initialize the SPI object
  66. */
  67. void spiBegin(void) {
  68. #if !PIN_EXISTS(SS)
  69. #error SS_PIN not defined!
  70. #endif
  71. SET_OUTPUT(SS_PIN);
  72. WRITE(SS_PIN, HIGH);
  73. }
  74. /** Configure SPI for specified SPI speed */
  75. void spiInit(uint8_t spiRate) {
  76. // Use datarates Marlin uses
  77. uint32_t clock;
  78. switch (spiRate) {
  79. case SPI_FULL_SPEED: clock = 20000000; break; //13.9mhz=20000000 6.75mhz=10000000 3.38mhz=5000000 .833mhz=1000000
  80. case SPI_HALF_SPEED: clock = 5000000; break;
  81. case SPI_QUARTER_SPEED: clock = 2500000; break;
  82. case SPI_EIGHTH_SPEED: clock = 1250000; break;
  83. case SPI_SPEED_5: clock = 625000; break;
  84. case SPI_SPEED_6: clock = 300000; break;
  85. default:
  86. clock = 4000000; // Default from the SPI libarary
  87. }
  88. spiConfig = SPISettings(clock, MSBFIRST, SPI_MODE0);
  89. SPI.begin();
  90. }
  91. /**
  92. * @brief Receives a single byte from the SPI port.
  93. *
  94. * @return Byte received
  95. *
  96. * @details
  97. */
  98. uint8_t spiRec(void) {
  99. SPI.beginTransaction(spiConfig);
  100. uint8_t returnByte = SPI.transfer(0xFF);
  101. SPI.endTransaction();
  102. return returnByte;
  103. }
  104. /**
  105. * @brief Receives 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.beginTransaction(spiConfig);
  115. SPI.dmaTransfer(0, const_cast<uint8_t*>(buf), nbyte);
  116. SPI.endTransaction();
  117. }
  118. /**
  119. * @brief Sends 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.beginTransaction(spiConfig);
  127. SPI.transfer(b);
  128. SPI.endTransaction();
  129. }
  130. /**
  131. * @brief Write token and then write from 512 byte buffer to SPI (for SD card)
  132. *
  133. * @param buf Pointer with buffer start address
  134. * @return Nothing
  135. *
  136. * @details Use DMA
  137. */
  138. void spiSendBlock(uint8_t token, const uint8_t* buf) {
  139. SPI.beginTransaction(spiConfig);
  140. SPI.transfer(token);
  141. SPI.dmaSend(const_cast<uint8_t*>(buf), 512);
  142. SPI.endTransaction();
  143. }
  144. #endif // SOFTWARE_SPI
  145. #endif // STM32F7