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.

u8g_com_stm32duino_swspi.cpp 4.5KB

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