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

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