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.

tft_spi.cpp 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. #include "../../../inc/MarlinConfig.h"
  23. #if HAS_SPI_TFT
  24. #include "tft_spi.h"
  25. SPIClass TFT_SPI::SPIx(1);
  26. void TFT_SPI::Init() {
  27. #if PIN_EXISTS(TFT_RESET)
  28. OUT_WRITE(TFT_RESET_PIN, HIGH);
  29. delay(100);
  30. #endif
  31. #if PIN_EXISTS(TFT_BACKLIGHT)
  32. OUT_WRITE(TFT_BACKLIGHT_PIN, HIGH);
  33. #endif
  34. OUT_WRITE(TFT_DC_PIN, HIGH);
  35. OUT_WRITE(TFT_CS_PIN, HIGH);
  36. /**
  37. * STM32F1 APB2 = 72MHz, APB1 = 36MHz, max SPI speed of this MCU if 18Mhz
  38. * STM32F1 has 3 SPI ports, SPI1 in APB2, SPI2/SPI3 in APB1
  39. * so the minimum prescale of SPI1 is DIV4, SPI2/SPI3 is DIV2
  40. */
  41. #if SPI_DEVICE == 1
  42. #define SPI_CLOCK_MAX SPI_CLOCK_DIV4
  43. #else
  44. #define SPI_CLOCK_MAX SPI_CLOCK_DIV2
  45. #endif
  46. uint8_t clock;
  47. uint8_t spiRate = SPI_FULL_SPEED;
  48. switch (spiRate) {
  49. case SPI_FULL_SPEED: clock = SPI_CLOCK_MAX ; break;
  50. case SPI_HALF_SPEED: clock = SPI_CLOCK_DIV4 ; break;
  51. case SPI_QUARTER_SPEED: clock = SPI_CLOCK_DIV8 ; break;
  52. case SPI_EIGHTH_SPEED: clock = SPI_CLOCK_DIV16; break;
  53. case SPI_SPEED_5: clock = SPI_CLOCK_DIV32; break;
  54. case SPI_SPEED_6: clock = SPI_CLOCK_DIV64; break;
  55. default: clock = SPI_CLOCK_DIV2; // Default from the SPI library
  56. }
  57. SPIx.setModule(1);
  58. SPIx.setClockDivider(clock);
  59. SPIx.setBitOrder(MSBFIRST);
  60. SPIx.setDataMode(SPI_MODE0);
  61. }
  62. void TFT_SPI::DataTransferBegin(uint16_t DataSize) {
  63. SPIx.setDataSize(DataSize);
  64. SPIx.begin();
  65. OUT_WRITE(TFT_CS_PIN, LOW);
  66. }
  67. #ifdef TFT_DEFAULT_DRIVER
  68. #include "../../../lcd/tft_io/tft_ids.h"
  69. #endif
  70. uint32_t TFT_SPI::GetID() {
  71. uint32_t id;
  72. id = ReadID(LCD_READ_ID);
  73. if ((id & 0xFFFF) == 0 || (id & 0xFFFF) == 0xFFFF) {
  74. id = ReadID(LCD_READ_ID4);
  75. #ifdef TFT_DEFAULT_DRIVER
  76. if ((id & 0xFFFF) == 0 || (id & 0xFFFF) == 0xFFFF)
  77. id = TFT_DEFAULT_DRIVER;
  78. #endif
  79. }
  80. return id;
  81. }
  82. uint32_t TFT_SPI::ReadID(uint16_t Reg) {
  83. #if !PIN_EXISTS(TFT_MISO)
  84. return 0;
  85. #else
  86. uint8_t d = 0;
  87. uint32_t data = 0;
  88. SPIx.setClockDivider(SPI_CLOCK_DIV16);
  89. DataTransferBegin(DATASIZE_8BIT);
  90. WriteReg(Reg);
  91. LOOP_L_N(i, 4) {
  92. SPIx.read((uint8_t*)&d, 1);
  93. data = (data << 8) | d;
  94. }
  95. DataTransferEnd();
  96. SPIx.setClockDivider(SPI_CLOCK_MAX);
  97. return data >> 7;
  98. #endif
  99. }
  100. bool TFT_SPI::isBusy() { return false; }
  101. void TFT_SPI::Abort() { DataTransferEnd(); }
  102. void TFT_SPI::Transmit(uint16_t Data) { SPIx.send(Data); }
  103. void TFT_SPI::TransmitDMA(uint32_t MemoryIncrease, uint16_t *Data, uint16_t Count) {
  104. DataTransferBegin();
  105. OUT_WRITE(TFT_DC_PIN, HIGH);
  106. SPIx.dmaSend(Data, Count, MemoryIncrease == DMA_MINC_ENABLE);
  107. DataTransferEnd();
  108. }
  109. #endif // HAS_SPI_TFT