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

HAL_SPI.cpp 3.5KB

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