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_swspi.cpp 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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_MARLINUI_U8GLIB, FORCE_SOFT_SPI)
  22. #include <U8glib.h>
  23. #undef SPI_SPEED
  24. #define SPI_SPEED 0 // Fastest
  25. //#define SPI_SPEED 2 // Slower
  26. static uint8_t SPI_speed = SPI_SPEED;
  27. static inline uint8_t swSpiTransfer_mode_0(uint8_t b, const uint8_t spi_speed, const pin_t miso_pin=-1) {
  28. LOOP_L_N(i, 8) {
  29. if (spi_speed == 0) {
  30. WRITE(DOGLCD_MOSI, !!(b & 0x80));
  31. WRITE(DOGLCD_SCK, HIGH);
  32. b <<= 1;
  33. if (miso_pin >= 0 && READ(miso_pin)) b |= 1;
  34. WRITE(DOGLCD_SCK, LOW);
  35. }
  36. else {
  37. const uint8_t state = (b & 0x80) ? HIGH : LOW;
  38. LOOP_L_N(j, spi_speed)
  39. WRITE(DOGLCD_MOSI, state);
  40. LOOP_L_N(j, spi_speed + (miso_pin >= 0 ? 0 : 1))
  41. WRITE(DOGLCD_SCK, HIGH);
  42. b <<= 1;
  43. if (miso_pin >= 0 && READ(miso_pin)) b |= 1;
  44. LOOP_L_N(j, spi_speed)
  45. WRITE(DOGLCD_SCK, LOW);
  46. }
  47. }
  48. return b;
  49. }
  50. static inline uint8_t swSpiTransfer_mode_3(uint8_t b, const uint8_t spi_speed, const pin_t miso_pin=-1) {
  51. LOOP_L_N(i, 8) {
  52. const uint8_t state = (b & 0x80) ? HIGH : LOW;
  53. if (spi_speed == 0) {
  54. WRITE(DOGLCD_SCK, LOW);
  55. WRITE(DOGLCD_MOSI, state);
  56. WRITE(DOGLCD_MOSI, state); // need some setup time
  57. WRITE(DOGLCD_SCK, HIGH);
  58. }
  59. else {
  60. LOOP_L_N(j, spi_speed + (miso_pin >= 0 ? 0 : 1))
  61. WRITE(DOGLCD_SCK, LOW);
  62. LOOP_L_N(j, spi_speed)
  63. WRITE(DOGLCD_MOSI, state);
  64. LOOP_L_N(j, spi_speed)
  65. WRITE(DOGLCD_SCK, HIGH);
  66. }
  67. b <<= 1;
  68. if (miso_pin >= 0 && READ(miso_pin)) b |= 1;
  69. }
  70. return b;
  71. }
  72. static void u8g_sw_spi_HAL_STM32F1_shift_out(uint8_t val) {
  73. #if ENABLED(FYSETC_MINI_12864)
  74. swSpiTransfer_mode_3(val, SPI_speed);
  75. #else
  76. swSpiTransfer_mode_0(val, SPI_speed);
  77. #endif
  78. }
  79. static uint8_t swSpiInit(const uint8_t spi_speed) {
  80. #if PIN_EXISTS(LCD_RESET)
  81. SET_OUTPUT(LCD_RESET_PIN);
  82. #endif
  83. SET_OUTPUT(DOGLCD_A0);
  84. OUT_WRITE(DOGLCD_SCK, LOW);
  85. OUT_WRITE(DOGLCD_MOSI, LOW);
  86. OUT_WRITE(DOGLCD_CS, HIGH);
  87. return spi_speed;
  88. }
  89. uint8_t u8g_com_HAL_STM32F1_sw_spi_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr) {
  90. switch (msg) {
  91. case U8G_COM_MSG_INIT:
  92. SPI_speed = swSpiInit(SPI_SPEED);
  93. break;
  94. case U8G_COM_MSG_STOP:
  95. break;
  96. case U8G_COM_MSG_RESET:
  97. #if PIN_EXISTS(LCD_RESET)
  98. WRITE(LCD_RESET_PIN, arg_val);
  99. #endif
  100. break;
  101. case U8G_COM_MSG_CHIP_SELECT:
  102. #if ENABLED(FYSETC_MINI_12864) // This LCD SPI is running mode 3 while SD card is running mode 0
  103. if (arg_val) { // SCK idle state needs to be set to the proper idle state before
  104. // the next chip select goes active
  105. WRITE(DOGLCD_SCK, HIGH); // Set SCK to mode 3 idle state before CS goes active
  106. WRITE(DOGLCD_CS, LOW);
  107. }
  108. else {
  109. WRITE(DOGLCD_CS, HIGH);
  110. WRITE(DOGLCD_SCK, LOW); // Set SCK to mode 0 idle state after CS goes inactive
  111. }
  112. #else
  113. WRITE(DOGLCD_CS, !arg_val);
  114. #endif
  115. break;
  116. case U8G_COM_MSG_WRITE_BYTE:
  117. u8g_sw_spi_HAL_STM32F1_shift_out(arg_val);
  118. break;
  119. case U8G_COM_MSG_WRITE_SEQ: {
  120. uint8_t *ptr = (uint8_t *)arg_ptr;
  121. while (arg_val > 0) {
  122. u8g_sw_spi_HAL_STM32F1_shift_out(*ptr++);
  123. arg_val--;
  124. }
  125. } break;
  126. case U8G_COM_MSG_WRITE_SEQ_P: {
  127. uint8_t *ptr = (uint8_t *)arg_ptr;
  128. while (arg_val > 0) {
  129. u8g_sw_spi_HAL_STM32F1_shift_out(u8g_pgm_read(ptr));
  130. ptr++;
  131. arg_val--;
  132. }
  133. } break;
  134. case U8G_COM_MSG_ADDRESS: /* define cmd (arg_val = 0) or data mode (arg_val = 1) */
  135. WRITE(DOGLCD_A0, arg_val);
  136. break;
  137. }
  138. return 1;
  139. }
  140. #endif // HAS_MARLINUI_U8GLIB && FORCE_SOFT_SPI
  141. #endif // STM32F1