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 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 "../../inc/MarlinConfig.h"
  27. #include "HAL.h"
  28. #include <SPI.h>
  29. #include <pins_arduino.h>
  30. #include "spi_pins.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. #if PIN_EXISTS(SD_SS)
  46. OUT_WRITE(SD_SS_PIN, HIGH);
  47. #endif
  48. //SET_OUTPUT(SD_SCK_PIN);
  49. //SET_INPUT(SD_MISO_PIN);
  50. //SET_OUTPUT(SD_MOSI_PIN);
  51. #if 0 && DISABLED(SOFTWARE_SPI)
  52. // set SS high - may be chip select for another SPI device
  53. #if SET_SPI_SS_HIGH
  54. WRITE(SD_SS_PIN, HIGH);
  55. #endif
  56. // set a default rate
  57. spiInit(SPI_HALF_SPEED); // 1
  58. #endif
  59. }
  60. void spiInit(uint8_t spiRate) {
  61. // Use Marlin data-rates
  62. uint32_t clock;
  63. switch (spiRate) {
  64. case SPI_FULL_SPEED: clock = 10000000; break;
  65. case SPI_HALF_SPEED: clock = 5000000; break;
  66. case SPI_QUARTER_SPEED: clock = 2500000; break;
  67. case SPI_EIGHTH_SPEED: clock = 1250000; break;
  68. case SPI_SPEED_5: clock = 625000; break;
  69. case SPI_SPEED_6: clock = 312500; break;
  70. default:
  71. clock = 4000000; // Default from the SPI library
  72. }
  73. spiConfig = SPISettings(clock, MSBFIRST, SPI_MODE0);
  74. SPI.begin();
  75. }
  76. uint8_t spiRec() {
  77. SPI.beginTransaction(spiConfig);
  78. uint8_t returnByte = SPI.transfer(0xFF);
  79. SPI.endTransaction();
  80. return returnByte;
  81. //SPDR = 0xFF;
  82. //while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ }
  83. //return SPDR;
  84. }
  85. void spiRead(uint8_t *buf, uint16_t nbyte) {
  86. SPI.beginTransaction(spiConfig);
  87. SPI.transfer(buf, nbyte);
  88. SPI.endTransaction();
  89. //if (nbyte-- == 0) return;
  90. // SPDR = 0xFF;
  91. //for (uint16_t i = 0; i < nbyte; i++) {
  92. // while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ }
  93. // buf[i] = SPDR;
  94. // SPDR = 0xFF;
  95. //}
  96. //while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ }
  97. //buf[nbyte] = SPDR;
  98. }
  99. void spiSend(uint8_t b) {
  100. SPI.beginTransaction(spiConfig);
  101. SPI.transfer(b);
  102. SPI.endTransaction();
  103. //SPDR = b;
  104. //while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ }
  105. }
  106. void spiSendBlock(uint8_t token, const uint8_t *buf) {
  107. SPI.beginTransaction(spiConfig);
  108. SPDR = token;
  109. for (uint16_t i = 0; i < 512; i += 2) {
  110. while (!TEST(SPSR, SPIF)) { /* nada */ };
  111. SPDR = buf[i];
  112. while (!TEST(SPSR, SPIF)) { /* nada */ };
  113. SPDR = buf[i + 1];
  114. }
  115. while (!TEST(SPSR, SPIF)) { /* nada */ };
  116. SPI.endTransaction();
  117. }
  118. // Begin SPI transaction, set clock, bit order, data mode
  119. void spiBeginTransaction(uint32_t spiClock, uint8_t bitOrder, uint8_t dataMode) {
  120. spiConfig = SPISettings(spiClock, bitOrder, dataMode);
  121. SPI.beginTransaction(spiConfig);
  122. }
  123. #endif // SOFTWARE_SPI
  124. #endif // __IMXRT1062__