My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

HAL_SPI.cpp 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. * HAL SPI for Teensy 4.0 (IMXRT1062DVL6A) / 4.1 (IMXRT1062DVJ6A)
  24. */
  25. #ifdef __IMXRT1062__
  26. #include "HAL.h"
  27. #include <SPI.h>
  28. #include <pins_arduino.h>
  29. #include "spi_pins.h"
  30. #include "../../core/macros.h"
  31. static SPISettings spiConfig;
  32. // ------------------------
  33. // Public functions
  34. // ------------------------
  35. #if ENABLED(SOFTWARE_SPI)
  36. // ------------------------
  37. // Software SPI
  38. // ------------------------
  39. #error "Software SPI not supported for Teensy 4. Use Hardware SPI."
  40. #else
  41. // ------------------------
  42. // Hardware SPI
  43. // ------------------------
  44. void spiBegin() {
  45. #ifndef SS_PIN
  46. #error "SS_PIN is not defined!"
  47. #endif
  48. OUT_WRITE(SS_PIN, HIGH);
  49. //SET_OUTPUT(SCK_PIN);
  50. //SET_INPUT(MISO_PIN);
  51. //SET_OUTPUT(MOSI_PIN);
  52. #if 0 && DISABLED(SOFTWARE_SPI)
  53. // set SS high - may be chip select for another SPI device
  54. #if SET_SPI_SS_HIGH
  55. WRITE(SS_PIN, HIGH);
  56. #endif
  57. // set a default rate
  58. spiInit(SPI_HALF_SPEED); // 1
  59. #endif
  60. }
  61. void spiInit(uint8_t spiRate) {
  62. // Use Marlin data-rates
  63. uint32_t clock;
  64. switch (spiRate) {
  65. case SPI_FULL_SPEED: clock = 10000000; break;
  66. case SPI_HALF_SPEED: clock = 5000000; break;
  67. case SPI_QUARTER_SPEED: clock = 2500000; break;
  68. case SPI_EIGHTH_SPEED: clock = 1250000; break;
  69. case SPI_SPEED_5: clock = 625000; break;
  70. case SPI_SPEED_6: clock = 312500; break;
  71. default:
  72. clock = 4000000; // Default from the SPI libarary
  73. }
  74. spiConfig = SPISettings(clock, MSBFIRST, SPI_MODE0);
  75. SPI.begin();
  76. }
  77. uint8_t spiRec() {
  78. SPI.beginTransaction(spiConfig);
  79. uint8_t returnByte = SPI.transfer(0xFF);
  80. SPI.endTransaction();
  81. return returnByte;
  82. //SPDR = 0xFF;
  83. //while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ }
  84. //return SPDR;
  85. }
  86. void spiRead(uint8_t* buf, uint16_t nbyte) {
  87. SPI.beginTransaction(spiConfig);
  88. SPI.transfer(buf, nbyte);
  89. SPI.endTransaction();
  90. //if (nbyte-- == 0) return;
  91. // SPDR = 0xFF;
  92. //for (uint16_t i = 0; i < nbyte; i++) {
  93. // while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ }
  94. // buf[i] = SPDR;
  95. // SPDR = 0xFF;
  96. //}
  97. //while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ }
  98. //buf[nbyte] = SPDR;
  99. }
  100. void spiSend(uint8_t b) {
  101. SPI.beginTransaction(spiConfig);
  102. SPI.transfer(b);
  103. SPI.endTransaction();
  104. //SPDR = b;
  105. //while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ }
  106. }
  107. void spiSendBlock(uint8_t token, const uint8_t* buf) {
  108. SPI.beginTransaction(spiConfig);
  109. SPDR = token;
  110. for (uint16_t i = 0; i < 512; i += 2) {
  111. while (!TEST(SPSR, SPIF)) { /* nada */ };
  112. SPDR = buf[i];
  113. while (!TEST(SPSR, SPIF)) { /* nada */ };
  114. SPDR = buf[i + 1];
  115. }
  116. while (!TEST(SPSR, SPIF)) { /* nada */ };
  117. SPI.endTransaction();
  118. }
  119. // Begin SPI transaction, set clock, bit order, data mode
  120. void spiBeginTransaction(uint32_t spiClock, uint8_t bitOrder, uint8_t dataMode) {
  121. spiConfig = SPISettings(spiClock, bitOrder, dataMode);
  122. SPI.beginTransaction(spiConfig);
  123. }
  124. #endif // SOFTWARE_SPI
  125. #endif // __IMXRT1062__