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 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /**
  23. * Adapted from Arduino Sd2Card Library
  24. * Copyright (c) 2009 by William Greiman
  25. */
  26. /**
  27. * HAL for AVR - SPI functions
  28. */
  29. #ifdef __AVR__
  30. #include "../../inc/MarlinConfig.h"
  31. void spiBegin() {
  32. #if PIN_EXISTS(SD_SS)
  33. // Do not init HIGH for boards with pin 4 used as Fans or Heaters or otherwise, not likely to have multiple SPI devices anyway.
  34. #if defined(__AVR_ATmega644__) || defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644PA__) || defined(__AVR_ATmega1284P__)
  35. // SS must be in output mode even it is not chip select
  36. SET_OUTPUT(SD_SS_PIN);
  37. #else
  38. // set SS high - may be chip select for another SPI device
  39. OUT_WRITE(SD_SS_PIN, HIGH);
  40. #endif
  41. #endif
  42. SET_OUTPUT(SD_SCK_PIN);
  43. SET_INPUT(SD_MISO_PIN);
  44. SET_OUTPUT(SD_MOSI_PIN);
  45. IF_DISABLED(SOFTWARE_SPI, spiInit(SPI_HALF_SPEED));
  46. }
  47. #if NONE(SOFTWARE_SPI, FORCE_SOFT_SPI)
  48. // ------------------------
  49. // Hardware SPI
  50. // ------------------------
  51. // make sure SPCR rate is in expected bits
  52. #if (SPR0 != 0 || SPR1 != 1)
  53. #error "unexpected SPCR bits"
  54. #endif
  55. /**
  56. * Initialize hardware SPI
  57. * Set SCK rate to F_CPU/pow(2, 1 + spiRate) for spiRate [0,6]
  58. */
  59. void spiInit(uint8_t spiRate) {
  60. // See avr processor documentation
  61. CBI(
  62. #ifdef PRR
  63. PRR
  64. #elif defined(PRR0)
  65. PRR0
  66. #endif
  67. , PRSPI
  68. );
  69. SPCR = _BV(SPE) | _BV(MSTR) | (spiRate >> 1);
  70. SPSR = spiRate & 1 || spiRate == 6 ? 0 : _BV(SPI2X);
  71. }
  72. /** SPI receive a byte */
  73. uint8_t spiRec() {
  74. SPDR = 0xFF;
  75. while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ }
  76. return SPDR;
  77. }
  78. /** SPI read data */
  79. void spiRead(uint8_t *buf, uint16_t nbyte) {
  80. if (nbyte-- == 0) return;
  81. SPDR = 0xFF;
  82. for (uint16_t i = 0; i < nbyte; i++) {
  83. while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ }
  84. buf[i] = SPDR;
  85. SPDR = 0xFF;
  86. }
  87. while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ }
  88. buf[nbyte] = SPDR;
  89. }
  90. /** SPI send a byte */
  91. void spiSend(uint8_t b) {
  92. SPDR = b;
  93. while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ }
  94. }
  95. /** SPI send block */
  96. void spiSendBlock(uint8_t token, const uint8_t *buf) {
  97. SPDR = token;
  98. for (uint16_t i = 0; i < 512; i += 2) {
  99. while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ }
  100. SPDR = buf[i];
  101. while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ }
  102. SPDR = buf[i + 1];
  103. }
  104. while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ }
  105. }
  106. /** begin spi transaction */
  107. void spiBeginTransaction(uint32_t spiClock, uint8_t bitOrder, uint8_t dataMode) {
  108. // Based on Arduino SPI library
  109. // Clock settings are defined as follows. Note that this shows SPI2X
  110. // inverted, so the bits form increasing numbers. Also note that
  111. // fosc/64 appears twice
  112. // SPR1 SPR0 ~SPI2X Freq
  113. // 0 0 0 fosc/2
  114. // 0 0 1 fosc/4
  115. // 0 1 0 fosc/8
  116. // 0 1 1 fosc/16
  117. // 1 0 0 fosc/32
  118. // 1 0 1 fosc/64
  119. // 1 1 0 fosc/64
  120. // 1 1 1 fosc/128
  121. // We find the fastest clock that is less than or equal to the
  122. // given clock rate. The clock divider that results in clock_setting
  123. // is 2 ^^ (clock_div + 1). If nothing is slow enough, we'll use the
  124. // slowest (128 == 2 ^^ 7, so clock_div = 6).
  125. uint8_t clockDiv;
  126. // When the clock is known at compiletime, use this if-then-else
  127. // cascade, which the compiler knows how to completely optimize
  128. // away. When clock is not known, use a loop instead, which generates
  129. // shorter code.
  130. if (__builtin_constant_p(spiClock)) {
  131. if (spiClock >= F_CPU / 2) clockDiv = 0;
  132. else if (spiClock >= F_CPU / 4) clockDiv = 1;
  133. else if (spiClock >= F_CPU / 8) clockDiv = 2;
  134. else if (spiClock >= F_CPU / 16) clockDiv = 3;
  135. else if (spiClock >= F_CPU / 32) clockDiv = 4;
  136. else if (spiClock >= F_CPU / 64) clockDiv = 5;
  137. else clockDiv = 6;
  138. }
  139. else {
  140. uint32_t clockSetting = F_CPU / 2;
  141. clockDiv = 0;
  142. while (clockDiv < 6 && spiClock < clockSetting) {
  143. clockSetting /= 2;
  144. clockDiv++;
  145. }
  146. }
  147. // Compensate for the duplicate fosc/64
  148. if (clockDiv == 6) clockDiv = 7;
  149. // Invert the SPI2X bit
  150. clockDiv ^= 0x1;
  151. SPCR = _BV(SPE) | _BV(MSTR) | ((bitOrder == LSBFIRST) ? _BV(DORD) : 0) |
  152. (dataMode << CPHA) | ((clockDiv >> 1) << SPR0);
  153. SPSR = clockDiv | 0x01;
  154. }
  155. #else // SOFTWARE_SPI || FORCE_SOFT_SPI
  156. // ------------------------
  157. // Software SPI
  158. // ------------------------
  159. // nop to tune soft SPI timing
  160. #define nop asm volatile ("\tnop\n")
  161. void spiInit(uint8_t) { /* do nothing */ }
  162. // Begin SPI transaction, set clock, bit order, data mode
  163. void spiBeginTransaction(uint32_t spiClock, uint8_t bitOrder, uint8_t dataMode) { /* do nothing */ }
  164. // Soft SPI receive byte
  165. uint8_t spiRec() {
  166. uint8_t data = 0;
  167. // no interrupts during byte receive - about 8µs
  168. cli();
  169. // output pin high - like sending 0xFF
  170. WRITE(SD_MOSI_PIN, HIGH);
  171. LOOP_L_N(i, 8) {
  172. WRITE(SD_SCK_PIN, HIGH);
  173. nop; // adjust so SCK is nice
  174. nop;
  175. data <<= 1;
  176. if (READ(SD_MISO_PIN)) data |= 1;
  177. WRITE(SD_SCK_PIN, LOW);
  178. }
  179. sei();
  180. return data;
  181. }
  182. // Soft SPI read data
  183. void spiRead(uint8_t *buf, uint16_t nbyte) {
  184. for (uint16_t i = 0; i < nbyte; i++)
  185. buf[i] = spiRec();
  186. }
  187. // Soft SPI send byte
  188. void spiSend(uint8_t data) {
  189. // no interrupts during byte send - about 8µs
  190. cli();
  191. LOOP_L_N(i, 8) {
  192. WRITE(SD_SCK_PIN, LOW);
  193. WRITE(SD_MOSI_PIN, data & 0x80);
  194. data <<= 1;
  195. WRITE(SD_SCK_PIN, HIGH);
  196. }
  197. nop; // hold SCK high for a few ns
  198. nop;
  199. nop;
  200. nop;
  201. WRITE(SD_SCK_PIN, LOW);
  202. sei();
  203. }
  204. // Soft SPI send block
  205. void spiSendBlock(uint8_t token, const uint8_t *buf) {
  206. spiSend(token);
  207. for (uint16_t i = 0; i < 512; i++)
  208. spiSend(buf[i]);
  209. }
  210. #endif // SOFTWARE_SPI || FORCE_SOFT_SPI
  211. #endif // __AVR__