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 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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: <http://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. SPISettings SPI::spi_settings(SPI_FREQUENCY, MSBFIRST, SPI_MODE0);
  27. #endif
  28. void SPI::spi_init() {
  29. SET_OUTPUT(CLCD_MOD_RESET); // Module Reset (a.k.a. PD, not SPI)
  30. WRITE(CLCD_MOD_RESET, 0); // start with module in power-down
  31. SET_OUTPUT(CLCD_SPI_CS);
  32. WRITE(CLCD_SPI_CS, 1);
  33. #ifdef SPI_FLASH_SS
  34. SET_OUTPUT(SPI_FLASH_SS);
  35. WRITE(SPI_FLASH_SS, 1);
  36. #endif
  37. #ifdef CLCD_USE_SOFT_SPI
  38. SET_OUTPUT(CLCD_SOFT_SPI_MOSI);
  39. WRITE(CLCD_SOFT_SPI_MOSI, 1);
  40. SET_OUTPUT(CLCD_SOFT_SPI_SCLK);
  41. WRITE(CLCD_SOFT_SPI_SCLK, 0);
  42. SET_INPUT_PULLUP(CLCD_SOFT_SPI_MISO);
  43. #else
  44. ::SPI.begin();
  45. #endif
  46. }
  47. #ifdef CLCD_USE_SOFT_SPI
  48. uint8_t SPI::_soft_spi_xfer (uint8_t spiOutByte) {
  49. uint8_t spiIndex = 0x80;
  50. uint8_t spiInByte = 0;
  51. uint8_t k;
  52. noInterrupts();
  53. for (k = 0; k < 8; k++) { // Output and Read each bit of spiOutByte and spiInByte
  54. WRITE(CLCD_SOFT_SPI_MOSI, (spiOutByte & spiIndex) ? 1 : 0); // Output MOSI Bit
  55. WRITE(CLCD_SOFT_SPI_SCLK, 1); // Pulse Clock
  56. WRITE(CLCD_SOFT_SPI_SCLK, 0);
  57. if (READ(CLCD_SOFT_SPI_MISO)) spiInByte |= spiIndex;
  58. spiIndex >>= 1;
  59. }
  60. interrupts();
  61. return spiInByte;
  62. }
  63. #endif
  64. #ifdef CLCD_USE_SOFT_SPI
  65. void SPI::_soft_spi_send (uint8_t spiOutByte) {
  66. uint8_t k, spiIndex = 0x80;
  67. noInterrupts();
  68. for (k = 0; k < 8; k++) { // Output each bit of spiOutByte
  69. WRITE(CLCD_SOFT_SPI_MOSI, (spiOutByte & spiIndex) ? 1 : 0); // Output MOSI Bit
  70. WRITE(CLCD_SOFT_SPI_SCLK, 1); // Pulse Clock
  71. WRITE(CLCD_SOFT_SPI_SCLK, 0);
  72. spiIndex >>= 1;
  73. }
  74. interrupts();
  75. }
  76. #endif
  77. void SPI::spi_read_bulk (void *data, uint16_t len) {
  78. uint8_t* p = (uint8_t *)data;
  79. #ifndef CLCD_USE_SOFT_SPI
  80. ::SPI.transfer(p, len);
  81. #else
  82. while (len--) *p++ = spi_recv();
  83. #endif
  84. }
  85. bool SPI::spi_verify_bulk (const void *data, uint16_t len) {
  86. const uint8_t* p = (const uint8_t *)data;
  87. while (len--) if (*p++ != spi_recv()) return false;
  88. return true;
  89. }
  90. // CLCD SPI - Chip Select
  91. void SPI::spi_ftdi_select() {
  92. #ifndef CLCD_USE_SOFT_SPI
  93. ::SPI.beginTransaction(spi_settings);
  94. #endif
  95. WRITE(CLCD_SPI_CS, 0);
  96. delayMicroseconds(1);
  97. }
  98. // CLCD SPI - Chip Deselect
  99. void SPI::spi_ftdi_deselect() {
  100. WRITE(CLCD_SPI_CS, 1);
  101. #ifndef CLCD_USE_SOFT_SPI
  102. ::SPI.endTransaction();
  103. #endif
  104. }
  105. #ifdef SPI_FLASH_SS
  106. // Serial SPI Flash SPI - Chip Select
  107. void SPI::spi_flash_select () {
  108. #ifndef CLCD_USE_SOFT_SPI
  109. ::SPI.beginTransaction(spi_settings);
  110. #endif
  111. WRITE(SPI_FLASH_SS, 0);
  112. delayMicroseconds(1);
  113. }
  114. // Serial SPI Flash SPI - Chip Deselect
  115. void SPI::spi_flash_deselect () {
  116. WRITE(SPI_FLASH_SS, 1);
  117. #ifndef CLCD_USE_SOFT_SPI
  118. ::SPI.endTransaction();
  119. #endif
  120. }
  121. #endif
  122. // Not really a SPI signal...
  123. void SPI::ftdi_reset() {
  124. WRITE(CLCD_MOD_RESET, 0);
  125. delay(6); /* minimum time for power-down is 5ms */
  126. WRITE(CLCD_MOD_RESET, 1);
  127. delay(21); /* minimum time to allow from rising PD_N to first access is 20ms */
  128. }
  129. // Not really a SPI signal...
  130. void SPI::test_pulse() {
  131. #ifdef CLCD_AUX_0
  132. WRITE(CLCD_AUX_0, 1);
  133. delayMicroseconds(10);
  134. WRITE(CLCD_AUX_0, 0);
  135. #endif
  136. }
  137. }
  138. #endif // FTDI_BASIC