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.

spi.cpp 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /***********
  2. * spi.cpp *
  3. ***********/
  4. /****************************************************************************
  5. * Written By Mark Pelletier 2017 - Aleph Objects, Inc. *
  6. * Written By Marcio Teixeira 2018 - Aleph Objects, Inc. *
  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. * To view a copy of the GNU General Public License, go to the following *
  19. * location: <https://www.gnu.org/licenses/>. *
  20. ****************************************************************************/
  21. #include "ftdi_basic.h"
  22. #ifdef FTDI_BASIC
  23. /********************************* SPI Functions *********************************/
  24. namespace FTDI {
  25. #ifndef CLCD_USE_SOFT_SPI
  26. #ifdef CLCD_SPI_BUS
  27. SPIClass EVE_SPI(CLCD_SPI_BUS);
  28. #endif
  29. #ifndef CLCD_HW_SPI_SPEED
  30. #define CLCD_HW_SPI_SPEED 8000000 >> SD_SPI_SPEED
  31. #endif
  32. SPISettings SPI::spi_settings(CLCD_HW_SPI_SPEED, MSBFIRST, SPI_MODE0);
  33. #endif
  34. void SPI::spi_init() {
  35. SET_OUTPUT(CLCD_MOD_RESET); // Module Reset (a.k.a. PD, not SPI)
  36. WRITE(CLCD_MOD_RESET, 0); // start with module in power-down
  37. SET_OUTPUT(CLCD_SPI_CS);
  38. WRITE(CLCD_SPI_CS, 1);
  39. #ifdef CLCD_SPI_EXTRA_CS
  40. SET_OUTPUT(CLCD_SPI_EXTRA_CS);
  41. WRITE(CLCD_SPI_EXTRA_CS, 1);
  42. #endif
  43. #ifdef SPI_FLASH_SS
  44. SET_OUTPUT(SPI_FLASH_SS);
  45. WRITE(SPI_FLASH_SS, 1);
  46. #endif
  47. #ifdef CLCD_USE_SOFT_SPI
  48. SET_OUTPUT(CLCD_SOFT_SPI_MOSI);
  49. WRITE(CLCD_SOFT_SPI_MOSI, 1);
  50. SET_OUTPUT(CLCD_SOFT_SPI_SCLK);
  51. WRITE(CLCD_SOFT_SPI_SCLK, 0);
  52. SET_INPUT_PULLUP(CLCD_SOFT_SPI_MISO);
  53. #else
  54. SPI_OBJ.begin();
  55. #endif
  56. }
  57. #ifdef CLCD_USE_SOFT_SPI
  58. uint8_t SPI::_soft_spi_xfer(uint8_t spiOutByte) {
  59. uint8_t spiIndex = 0x80;
  60. uint8_t spiInByte = 0;
  61. uint8_t k;
  62. noInterrupts();
  63. for (k = 0; k < 8; k++) { // Output and Read each bit of spiOutByte and spiInByte
  64. WRITE(CLCD_SOFT_SPI_MOSI, (spiOutByte & spiIndex) ? 1 : 0); // Output MOSI Bit
  65. WRITE(CLCD_SOFT_SPI_SCLK, 1); // Pulse Clock
  66. if (READ(CLCD_SOFT_SPI_MISO)) spiInByte |= spiIndex; // MISO changes on the falling edge of clock, so sample it before
  67. WRITE(CLCD_SOFT_SPI_SCLK, 0);
  68. spiIndex >>= 1;
  69. }
  70. interrupts();
  71. return spiInByte;
  72. }
  73. #endif
  74. #ifdef CLCD_USE_SOFT_SPI
  75. void SPI::_soft_spi_send(uint8_t spiOutByte) {
  76. uint8_t k, spiIndex = 0x80;
  77. noInterrupts();
  78. for (k = 0; k < 8; k++) { // Output each bit of spiOutByte
  79. WRITE(CLCD_SOFT_SPI_MOSI, (spiOutByte & spiIndex) ? 1 : 0); // Output MOSI Bit
  80. WRITE(CLCD_SOFT_SPI_SCLK, 1); // Pulse Clock
  81. WRITE(CLCD_SOFT_SPI_SCLK, 0);
  82. spiIndex >>= 1;
  83. }
  84. interrupts();
  85. }
  86. #endif
  87. void SPI::spi_read_bulk(void *data, uint16_t len) {
  88. uint8_t *p = (uint8_t *)data;
  89. while (len--) *p++ = spi_recv();
  90. }
  91. bool SPI::spi_verify_bulk(const void *data, uint16_t len) {
  92. const uint8_t *p = (const uint8_t *)data;
  93. while (len--) if (*p++ != spi_recv()) return false;
  94. return true;
  95. }
  96. // CLCD SPI - Chip Select
  97. void SPI::spi_ftdi_select() {
  98. #ifndef CLCD_USE_SOFT_SPI
  99. SPI_OBJ.beginTransaction(spi_settings);
  100. #endif
  101. WRITE(CLCD_SPI_CS, 0);
  102. #ifdef CLCD_SPI_EXTRA_CS
  103. WRITE(CLCD_SPI_EXTRA_CS, 0);
  104. #endif
  105. delayMicroseconds(1);
  106. }
  107. // CLCD SPI - Chip Deselect
  108. void SPI::spi_ftdi_deselect() {
  109. WRITE(CLCD_SPI_CS, 1);
  110. #ifdef CLCD_SPI_EXTRA_CS
  111. WRITE(CLCD_SPI_EXTRA_CS, 1);
  112. #endif
  113. #ifndef CLCD_USE_SOFT_SPI
  114. SPI_OBJ.endTransaction();
  115. #endif
  116. }
  117. #ifdef SPI_FLASH_SS
  118. // Serial SPI Flash SPI - Chip Select
  119. void SPI::spi_flash_select() {
  120. #ifndef CLCD_USE_SOFT_SPI
  121. SPI_OBJ.beginTransaction(spi_settings);
  122. #endif
  123. WRITE(SPI_FLASH_SS, 0);
  124. delayMicroseconds(1);
  125. }
  126. // Serial SPI Flash SPI - Chip Deselect
  127. void SPI::spi_flash_deselect() {
  128. WRITE(SPI_FLASH_SS, 1);
  129. #ifndef CLCD_USE_SOFT_SPI
  130. SPI_OBJ.endTransaction();
  131. #endif
  132. }
  133. #endif
  134. // Not really a SPI signal...
  135. void SPI::ftdi_reset() {
  136. WRITE(CLCD_MOD_RESET, 0);
  137. delay(6); /* minimum time for power-down is 5ms */
  138. WRITE(CLCD_MOD_RESET, 1);
  139. delay(21); /* minimum time to allow from rising PD_N to first access is 20ms */
  140. }
  141. // Not really a SPI signal...
  142. void SPI::test_pulse() {
  143. #ifdef CLCD_AUX_0
  144. WRITE(CLCD_AUX_0, 1);
  145. delayMicroseconds(10);
  146. WRITE(CLCD_AUX_0, 0);
  147. #endif
  148. }
  149. }
  150. #endif // FTDI_BASIC