My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

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 <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #ifdef __MK20DX256__
  23. #include "../../inc/MarlinConfig.h"
  24. #include "HAL.h"
  25. #include <SPI.h>
  26. #include <pins_arduino.h>
  27. #include "spi_pins.h"
  28. static SPISettings spiConfig;
  29. /**
  30. * Standard SPI functions
  31. */
  32. // Initialize SPI bus
  33. void spiBegin() {
  34. #if PIN_EXISTS(SD_SS)
  35. OUT_WRITE(SD_SS_PIN, HIGH);
  36. #endif
  37. SET_OUTPUT(SD_SCK_PIN);
  38. SET_INPUT(SD_MISO_PIN);
  39. SET_OUTPUT(SD_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(SD_SS_PIN, HIGH);
  44. #endif
  45. // set a default rate
  46. spiInit(SPI_HALF_SPEED); // 1
  47. #endif
  48. }
  49. // Configure SPI for specified SPI speed
  50. void spiInit(uint8_t spiRate) {
  51. // Use data rates Marlin uses
  52. uint32_t clock;
  53. switch (spiRate) {
  54. case SPI_FULL_SPEED: clock = 10000000; break;
  55. case SPI_HALF_SPEED: clock = 5000000; break;
  56. case SPI_QUARTER_SPEED: clock = 2500000; break;
  57. case SPI_EIGHTH_SPEED: clock = 1250000; break;
  58. case SPI_SPEED_5: clock = 625000; break;
  59. case SPI_SPEED_6: clock = 312500; break;
  60. default: clock = 4000000; // Default from the SPI library
  61. }
  62. spiConfig = SPISettings(clock, MSBFIRST, SPI_MODE0);
  63. SPI.begin();
  64. }
  65. // SPI receive a byte
  66. uint8_t spiRec() {
  67. SPI.beginTransaction(spiConfig);
  68. const uint8_t returnByte = SPI.transfer(0xFF);
  69. SPI.endTransaction();
  70. return returnByte;
  71. //SPDR = 0xFF;
  72. //while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ }
  73. //return SPDR;
  74. }
  75. // SPI read data
  76. void spiRead(uint8_t *buf, uint16_t nbyte) {
  77. SPI.beginTransaction(spiConfig);
  78. SPI.transfer(buf, nbyte);
  79. SPI.endTransaction();
  80. //if (nbyte-- == 0) return;
  81. // SPDR = 0xFF;
  82. //for (uint16_t i = 0; i < nbyte; i++) {
  83. // while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ }
  84. // buf[i] = SPDR;
  85. // SPDR = 0xFF;
  86. //}
  87. //while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ }
  88. //buf[nbyte] = SPDR;
  89. }
  90. // SPI send a byte
  91. void spiSend(uint8_t b) {
  92. SPI.beginTransaction(spiConfig);
  93. SPI.transfer(b);
  94. SPI.endTransaction();
  95. //SPDR = b;
  96. //while (!TEST(SPSR, SPIF)) { /* nada */ }
  97. }
  98. // SPI send block
  99. void spiSendBlock(uint8_t token, const uint8_t *buf) {
  100. SPI.beginTransaction(spiConfig);
  101. SPDR = token;
  102. for (uint16_t i = 0; i < 512; i += 2) {
  103. while (!TEST(SPSR, SPIF)) { /* nada */ };
  104. SPDR = buf[i];
  105. while (!TEST(SPSR, SPIF)) { /* nada */ };
  106. SPDR = buf[i + 1];
  107. }
  108. while (!TEST(SPSR, SPIF)) { /* nada */ };
  109. SPI.endTransaction();
  110. }
  111. // Begin SPI transaction, set clock, bit order, data mode
  112. void spiBeginTransaction(uint32_t spiClock, uint8_t bitOrder, uint8_t dataMode) {
  113. spiConfig = SPISettings(spiClock, bitOrder, dataMode);
  114. SPI.beginTransaction(spiConfig);
  115. }
  116. #endif // __MK20DX256__