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

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