My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

HAL_spi_AVR.cpp 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. *
  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 ARDUINO_ARCH_AVR
  30. */
  31. #ifdef ARDUINO_ARCH_AVR
  32. // --------------------------------------------------------------------------
  33. // Includes
  34. // --------------------------------------------------------------------------
  35. #include "../../../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. //------------------------------------------------------------------------------
  122. #else // SOFTWARE_SPI
  123. //------------------------------------------------------------------------------
  124. /** nop to tune soft SPI timing */
  125. #define nop asm volatile ("\tnop\n")
  126. /** Set SPI rate */
  127. void spiInit(uint8_t spiRate) {
  128. // nothing to do
  129. UNUSED(spiRate);
  130. }
  131. //------------------------------------------------------------------------------
  132. /** Soft SPI receive byte */
  133. uint8_t spiRec() {
  134. uint8_t data = 0;
  135. // no interrupts during byte receive - about 8 us
  136. cli();
  137. // output pin high - like sending 0XFF
  138. WRITE(MOSI_PIN, HIGH);
  139. for (uint8_t i = 0; i < 8; i++) {
  140. WRITE(SCK_PIN, HIGH);
  141. // adjust so SCK is nice
  142. nop;
  143. nop;
  144. data <<= 1;
  145. if (READ(MISO_PIN)) data |= 1;
  146. WRITE(SCK_PIN, LOW);
  147. }
  148. // enable interrupts
  149. sei();
  150. return data;
  151. }
  152. //------------------------------------------------------------------------------
  153. /** Soft SPI read data */
  154. void spiRead(uint8_t* buf, uint16_t nbyte) {
  155. for (uint16_t i = 0; i < nbyte; i++)
  156. buf[i] = spiRec();
  157. }
  158. //------------------------------------------------------------------------------
  159. /** Soft SPI send byte */
  160. void spiSend(uint8_t data) {
  161. // no interrupts during byte send - about 8 us
  162. cli();
  163. for (uint8_t i = 0; i < 8; i++) {
  164. WRITE(SCK_PIN, LOW);
  165. WRITE(MOSI_PIN, data & 0X80);
  166. data <<= 1;
  167. WRITE(SCK_PIN, HIGH);
  168. }
  169. // hold SCK high for a few ns
  170. nop;
  171. nop;
  172. nop;
  173. nop;
  174. WRITE(SCK_PIN, LOW);
  175. // enable interrupts
  176. sei();
  177. }
  178. //------------------------------------------------------------------------------
  179. /** Soft SPI send block */
  180. void spiSendBlock(uint8_t token, const uint8_t* buf) {
  181. spiSend(token);
  182. for (uint16_t i = 0; i < 512; i++)
  183. spiSend(buf[i]);
  184. }
  185. #endif // SOFTWARE_SPI
  186. #endif // ARDUINO_ARCH_AVR