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_AVR.cpp 7.1KB

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