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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. OUT_WRITE(SS_PIN, HIGH);
  33. SET_OUTPUT(SCK_PIN);
  34. SET_INPUT(MISO_PIN);
  35. SET_OUTPUT(MOSI_PIN);
  36. #if DISABLED(SOFTWARE_SPI)
  37. // SS must be in output mode even it is not chip select
  38. //SET_OUTPUT(SS_PIN);
  39. // set SS high - may be chip select for another SPI device
  40. //#if SET_SPI_SS_HIGH
  41. //WRITE(SS_PIN, HIGH);
  42. //#endif
  43. // set a default rate
  44. spiInit(1);
  45. #endif
  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. SPCR = _BV(SPE) | _BV(MSTR) | (spiRate >> 1);
  69. SPSR = spiRate & 1 || spiRate == 6 ? 0 : _BV(SPI2X);
  70. }
  71. /** SPI receive a byte */
  72. uint8_t spiRec() {
  73. SPDR = 0xFF;
  74. while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ }
  75. return SPDR;
  76. }
  77. /** SPI read data */
  78. void spiRead(uint8_t* buf, uint16_t nbyte) {
  79. if (nbyte-- == 0) return;
  80. SPDR = 0xFF;
  81. for (uint16_t i = 0; i < nbyte; i++) {
  82. while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ }
  83. buf[i] = SPDR;
  84. SPDR = 0xFF;
  85. }
  86. while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ }
  87. buf[nbyte] = SPDR;
  88. }
  89. /** SPI send a byte */
  90. void spiSend(uint8_t b) {
  91. SPDR = b;
  92. while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ }
  93. }
  94. /** SPI send block */
  95. void spiSendBlock(uint8_t token, const uint8_t* buf) {
  96. SPDR = token;
  97. for (uint16_t i = 0; i < 512; i += 2) {
  98. while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ }
  99. SPDR = buf[i];
  100. while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ }
  101. SPDR = buf[i + 1];
  102. }
  103. while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ }
  104. }
  105. /** begin spi transaction */
  106. void spiBeginTransaction(uint32_t spiClock, uint8_t bitOrder, uint8_t dataMode) {
  107. // Based on Arduino SPI library
  108. // Clock settings are defined as follows. Note that this shows SPI2X
  109. // inverted, so the bits form increasing numbers. Also note that
  110. // fosc/64 appears twice
  111. // SPR1 SPR0 ~SPI2X Freq
  112. // 0 0 0 fosc/2
  113. // 0 0 1 fosc/4
  114. // 0 1 0 fosc/8
  115. // 0 1 1 fosc/16
  116. // 1 0 0 fosc/32
  117. // 1 0 1 fosc/64
  118. // 1 1 0 fosc/64
  119. // 1 1 1 fosc/128
  120. // We find the fastest clock that is less than or equal to the
  121. // given clock rate. The clock divider that results in clock_setting
  122. // is 2 ^^ (clock_div + 1). If nothing is slow enough, we'll use the
  123. // slowest (128 == 2 ^^ 7, so clock_div = 6).
  124. uint8_t clockDiv;
  125. // When the clock is known at compiletime, use this if-then-else
  126. // cascade, which the compiler knows how to completely optimize
  127. // away. When clock is not known, use a loop instead, which generates
  128. // shorter code.
  129. if (__builtin_constant_p(spiClock)) {
  130. if (spiClock >= F_CPU / 2) clockDiv = 0;
  131. else if (spiClock >= F_CPU / 4) clockDiv = 1;
  132. else if (spiClock >= F_CPU / 8) clockDiv = 2;
  133. else if (spiClock >= F_CPU / 16) clockDiv = 3;
  134. else if (spiClock >= F_CPU / 32) clockDiv = 4;
  135. else if (spiClock >= F_CPU / 64) clockDiv = 5;
  136. else clockDiv = 6;
  137. }
  138. else {
  139. uint32_t clockSetting = F_CPU / 2;
  140. clockDiv = 0;
  141. while (clockDiv < 6 && spiClock < clockSetting) {
  142. clockSetting /= 2;
  143. clockDiv++;
  144. }
  145. }
  146. // Compensate for the duplicate fosc/64
  147. if (clockDiv == 6) clockDiv = 7;
  148. // Invert the SPI2X bit
  149. clockDiv ^= 0x1;
  150. SPCR = _BV(SPE) | _BV(MSTR) | ((bitOrder == LSBFIRST) ? _BV(DORD) : 0) |
  151. (dataMode << CPHA) | ((clockDiv >> 1) << SPR0);
  152. SPSR = clockDiv | 0x01;
  153. }
  154. #else // SOFTWARE_SPI || FORCE_SOFT_SPI
  155. // ------------------------
  156. // Software SPI
  157. // ------------------------
  158. // nop to tune soft SPI timing
  159. #define nop asm volatile ("\tnop\n")
  160. void spiInit(uint8_t) { /* do nothing */ }
  161. // Begin SPI transaction, set clock, bit order, data mode
  162. void spiBeginTransaction(uint32_t spiClock, uint8_t bitOrder, uint8_t dataMode) { /* do nothing */ }
  163. // Soft SPI receive byte
  164. uint8_t spiRec() {
  165. uint8_t data = 0;
  166. // no interrupts during byte receive - about 8µs
  167. cli();
  168. // output pin high - like sending 0xFF
  169. WRITE(MOSI_PIN, HIGH);
  170. LOOP_L_N(i, 8) {
  171. WRITE(SCK_PIN, HIGH);
  172. nop; // adjust so SCK is nice
  173. nop;
  174. data <<= 1;
  175. if (READ(MISO_PIN)) data |= 1;
  176. WRITE(SCK_PIN, LOW);
  177. }
  178. sei();
  179. return data;
  180. }
  181. // Soft SPI read data
  182. void spiRead(uint8_t* buf, uint16_t nbyte) {
  183. for (uint16_t i = 0; i < nbyte; i++)
  184. buf[i] = spiRec();
  185. }
  186. // Soft SPI send byte
  187. void spiSend(uint8_t data) {
  188. // no interrupts during byte send - about 8µs
  189. cli();
  190. LOOP_L_N(i, 8) {
  191. WRITE(SCK_PIN, LOW);
  192. WRITE(MOSI_PIN, data & 0x80);
  193. data <<= 1;
  194. WRITE(SCK_PIN, HIGH);
  195. }
  196. nop; // hold SCK high for a few ns
  197. nop;
  198. nop;
  199. nop;
  200. WRITE(SCK_PIN, LOW);
  201. sei();
  202. }
  203. // Soft SPI send block
  204. void spiSendBlock(uint8_t token, const uint8_t* buf) {
  205. spiSend(token);
  206. for (uint16_t i = 0; i < 512; i++)
  207. spiSend(buf[i]);
  208. }
  209. #endif // SOFTWARE_SPI || FORCE_SOFT_SPI
  210. #endif // __AVR__