My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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