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_Teensy.cpp 3.6KB

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