My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

u8g_com_stm32duino_hwspi.cpp 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. *
  18. */
  19. #ifdef __STM32F1__
  20. #include "../../../inc/MarlinConfig.h"
  21. #if BOTH(HAS_GRAPHICAL_LCD, SPI_GRAPHICAL_TFT) && DISABLED(FORCE_SOFT_SPI)
  22. #include "../HAL.h"
  23. #include <U8glib.h>
  24. #include <SPI.h>
  25. #define SPI_TFT_CS_H OUT_WRITE(SPI_TFT_CS_PIN, HIGH)
  26. #define SPI_TFT_CS_L OUT_WRITE(SPI_TFT_CS_PIN, LOW)
  27. #define SPI_TFT_DC_H OUT_WRITE(SPI_TFT_DC_PIN, HIGH)
  28. #define SPI_TFT_DC_L OUT_WRITE(SPI_TFT_DC_PIN, LOW)
  29. #define SPI_TFT_RST_H OUT_WRITE(SPI_TFT_RST_PIN, HIGH)
  30. #define SPI_TFT_RST_L OUT_WRITE(SPI_TFT_RST_PIN, LOW)
  31. #define SPI_TFT_BLK_H OUT_WRITE(LCD_BACKLIGHT_PIN, HIGH)
  32. #define SPI_TFT_BLK_L OUT_WRITE(LCD_BACKLIGHT_PIN, LOW)
  33. void LCD_IO_Init(uint8_t cs, uint8_t rs);
  34. void LCD_IO_WriteData(uint16_t RegValue);
  35. void LCD_IO_WriteReg(uint16_t Reg);
  36. uint16_t LCD_IO_ReadData(uint16_t RegValue);
  37. uint32_t LCD_IO_ReadData(uint16_t RegValue, uint8_t ReadSize);
  38. #ifdef LCD_USE_DMA_SPI
  39. void LCD_IO_WriteMultiple(uint16_t data, uint32_t count);
  40. void LCD_IO_WriteSequence(uint16_t *data, uint16_t length);
  41. #endif
  42. void LCD_WR_REG(uint8_t cmd) {
  43. SPI_TFT_CS_L;
  44. SPI_TFT_DC_L;
  45. SPI.send(cmd);
  46. SPI_TFT_CS_H;
  47. }
  48. void LCD_WR_DATA(uint8_t data) {
  49. SPI_TFT_CS_L;
  50. SPI_TFT_DC_H;
  51. SPI.send(data);
  52. SPI_TFT_CS_H;
  53. }
  54. void spi1Init(uint8_t spiRate) {
  55. SPI_TFT_CS_H;
  56. /**
  57. * STM32F1 APB2 = 72MHz, APB1 = 36MHz, max SPI speed of this MCU if 18Mhz
  58. * STM32F1 has 3 SPI ports, SPI1 in APB2, SPI2/SPI3 in APB1
  59. * so the minimum prescale of SPI1 is DIV4, SPI2/SPI3 is DIV2
  60. */
  61. uint8_t clock;
  62. switch (spiRate) {
  63. case SPI_FULL_SPEED: clock = SPI_CLOCK_DIV4; break;
  64. case SPI_HALF_SPEED: clock = SPI_CLOCK_DIV4; break;
  65. case SPI_QUARTER_SPEED: clock = SPI_CLOCK_DIV8; break;
  66. case SPI_EIGHTH_SPEED: clock = SPI_CLOCK_DIV16; break;
  67. case SPI_SPEED_5: clock = SPI_CLOCK_DIV32; break;
  68. case SPI_SPEED_6: clock = SPI_CLOCK_DIV64; break;
  69. default: clock = SPI_CLOCK_DIV2; // Default from the SPI library
  70. }
  71. SPI.setModule(1);
  72. SPI.begin();
  73. SPI.setClockDivider(clock);
  74. SPI.setBitOrder(MSBFIRST);
  75. SPI.setDataMode(SPI_MODE0);
  76. }
  77. void LCD_IO_Init(uint8_t cs, uint8_t rs) {
  78. spi1Init(SPI_FULL_SPEED);
  79. }
  80. void LCD_IO_WriteData(uint16_t RegValue) {
  81. LCD_WR_DATA(RegValue);
  82. }
  83. void LCD_IO_WriteReg(uint16_t Reg) {
  84. LCD_WR_REG(Reg);
  85. }
  86. uint16_t LCD_IO_ReadData(uint16_t RegValue) {
  87. uint16_t d = 0;
  88. SPI_TFT_CS_L;
  89. SPI_TFT_DC_L;
  90. SPI.send(RegValue);
  91. SPI_TFT_DC_H;
  92. SPI.read((uint8_t*)&d, 1); //dummy read
  93. SPI.read((uint8_t*)&d, 1);
  94. SPI_TFT_CS_H;
  95. return d >> 7;
  96. }
  97. uint32_t LCD_IO_ReadData(uint16_t RegValue, uint8_t ReadSize) {
  98. uint32_t data = 0;
  99. uint8_t d = 0;
  100. SPI_TFT_CS_L;
  101. SPI_TFT_DC_L;
  102. SPI.send(RegValue);
  103. SPI_TFT_DC_H;
  104. SPI.read((uint8_t*)&d, 1); //dummy read
  105. SPI.read((uint8_t*)&d, 1);
  106. data = d;
  107. while (--ReadSize) {
  108. data <<= 8;
  109. SPI.read((uint8_t*)&d, 1);
  110. data |= (d & 0xFF);
  111. }
  112. SPI_TFT_CS_H;
  113. return uint32_t(data >> 7);
  114. }
  115. #ifdef LCD_USE_DMA_SPI
  116. void LCD_IO_WriteMultiple(uint16_t data, uint32_t count) {
  117. if (SPI.getDataSize() == DATA_SIZE_8BIT) {
  118. count *= 2;
  119. }
  120. while (count > 0) {
  121. SPI_TFT_CS_L;
  122. SPI_TFT_DC_H;
  123. SPI.dmaSend(&data, 1, true);
  124. SPI_TFT_CS_H;
  125. count--;
  126. }
  127. }
  128. void LCD_IO_WriteSequence(uint16_t *data, uint16_t length) {
  129. if (SPI.getDataSize() == DATA_SIZE_8BIT) {
  130. length *= 2;
  131. }
  132. SPI_TFT_CS_L;
  133. SPI_TFT_DC_H;
  134. SPI.dmaSend(data, length, true);
  135. SPI_TFT_CS_H;
  136. }
  137. void LCD_IO_WriteSequence_Async(uint16_t *data, uint16_t length) {
  138. if (SPI.getDataSize() == DATA_SIZE_8BIT) {
  139. length *= 2;
  140. }
  141. SPI_TFT_CS_L;
  142. SPI_TFT_DC_H;
  143. SPI.dmaSendAsync(data, length, true);
  144. SPI_TFT_CS_H;
  145. }
  146. void LCD_IO_WaitSequence_Async() {
  147. SPI_TFT_CS_L;
  148. SPI_TFT_DC_H;
  149. SPI.dmaSendAsync(NULL, 0, true);
  150. SPI_TFT_CS_H;
  151. }
  152. #endif
  153. static uint8_t msgInitCount = 2; // Ignore all messages until 2nd U8G_COM_MSG_INIT
  154. #ifndef LCD_READ_ID
  155. #define LCD_READ_ID 0x04 // Read display identification information (0xD3 on ILI9341)
  156. #endif
  157. uint8_t u8g_com_stm32duino_spi_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr) {
  158. if (msgInitCount) {
  159. if (msg == U8G_COM_MSG_INIT) msgInitCount--;
  160. if (msgInitCount) return -1;
  161. }
  162. static uint8_t isCommand;
  163. LCD_IO_Init(-1, -1);
  164. switch (msg) {
  165. case U8G_COM_MSG_STOP: break;
  166. case U8G_COM_MSG_INIT:
  167. u8g_SetPIOutput(u8g, U8G_PI_RESET);
  168. u8g_Delay(50);
  169. if (arg_ptr) {
  170. spi1Init(SPI_EIGHTH_SPEED);
  171. *((uint32_t *)arg_ptr) = (LCD_READ_ID << 24) | LCD_IO_ReadData(LCD_READ_ID, 3);
  172. spi1Init(SPI_FULL_SPEED);
  173. }
  174. isCommand = 0;
  175. break;
  176. case U8G_COM_MSG_ADDRESS: // define cmd (arg_val = 0) or data mode (arg_val = 1)
  177. isCommand = arg_val == 0 ? 1 : 0;
  178. break;
  179. case U8G_COM_MSG_RESET:
  180. u8g_SetPILevel(u8g, U8G_PI_RESET, arg_val);
  181. break;
  182. case U8G_COM_MSG_WRITE_BYTE:
  183. if (isCommand)
  184. LCD_IO_WriteReg(arg_val);
  185. else
  186. LCD_IO_WriteData((uint16_t)arg_val);
  187. break;
  188. case U8G_COM_MSG_WRITE_SEQ:
  189. for (uint8_t i = 0; i < arg_val; i += 2)
  190. LCD_IO_WriteData(*(uint16_t *)(((uint32_t)arg_ptr) + i));
  191. break;
  192. }
  193. return 1;
  194. }
  195. #endif // HAS_GRAPHICAL_LCD
  196. #endif // STM32F1