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

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