|
| 1 | +/** |
| 2 | + * Marlin 3D Printer Firmware |
| 3 | + * Copyright (c) 2023 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 | + |
| 23 | +#ifdef ARDUINO_ARCH_HC32 |
| 24 | + |
| 25 | +#include "../../../inc/MarlinConfig.h" |
| 26 | + |
| 27 | +#if U8G_SW_SPI_HC32 |
| 28 | + |
| 29 | +#warning "Software SPI for U8Glib is experimental on HC32F460. Please share your experiences at https://github.com/shadow578/Marlin-H32/issues/35" |
| 30 | + |
| 31 | +#include <U8glib-HAL.h> |
| 32 | +#include "../../shared/HAL_SPI.h" |
| 33 | + |
| 34 | +#ifndef LCD_SPI_SPEED |
| 35 | + #define LCD_SPI_SPEED SPI_FULL_SPEED // Fastest |
| 36 | + //#define LCD_SPI_SPEED SPI_QUARTER_SPEED // Slower |
| 37 | +#endif |
| 38 | + |
| 39 | +static uint8_t SPI_speed = LCD_SPI_SPEED; |
| 40 | + |
| 41 | +static inline uint8_t swSpiTransfer_mode_0(uint8_t b, const uint8_t spi_speed, const pin_t miso_pin=-1) { |
| 42 | + for (i = 0; i < 8; ++i) { |
| 43 | + if (spi_speed == 0) { |
| 44 | + WRITE(DOGLCD_MOSI, !!(b & 0x80)); |
| 45 | + WRITE(DOGLCD_SCK, HIGH); |
| 46 | + b <<= 1; |
| 47 | + if (miso_pin >= 0 && READ(miso_pin)) b |= 1; |
| 48 | + WRITE(DOGLCD_SCK, LOW); |
| 49 | + } |
| 50 | + else { |
| 51 | + const uint8_t state = (b & 0x80) ? HIGH : LOW; |
| 52 | + for (j = 0; j < spi_speed; ++j) WRITE(DOGLCD_MOSI, state); |
| 53 | + for (j = 0; j < spi_speed + (miso_pin >= 0 ? 0 : 1; ++j)) WRITE(DOGLCD_SCK, HIGH); |
| 54 | + |
| 55 | + b <<= 1; |
| 56 | + if (miso_pin >= 0 && READ(miso_pin)) b |= 1; |
| 57 | + |
| 58 | + for (j = 0; j < spi_speed; ++j) WRITE(DOGLCD_SCK, LOW); |
| 59 | + } |
| 60 | + } |
| 61 | + return b; |
| 62 | +} |
| 63 | + |
| 64 | +static inline uint8_t swSpiTransfer_mode_3(uint8_t b, const uint8_t spi_speed, const pin_t miso_pin=-1) { |
| 65 | + for (i = 0; i < 8; ++i) { |
| 66 | + const uint8_t state = (b & 0x80) ? HIGH : LOW; |
| 67 | + if (spi_speed == 0) { |
| 68 | + WRITE(DOGLCD_SCK, LOW); |
| 69 | + WRITE(DOGLCD_MOSI, state); |
| 70 | + WRITE(DOGLCD_MOSI, state); // need some setup time |
| 71 | + WRITE(DOGLCD_SCK, HIGH); |
| 72 | + } |
| 73 | + else { |
| 74 | + for (j = 0; j < spi_speed + (miso_pin >= 0 ? 0 : 1); ++j) WRITE(DOGLCD_SCK, LOW); |
| 75 | + for (j = 0; j < spi_speed; ++j) WRITE(DOGLCD_MOSI, state); |
| 76 | + for (j = 0; j < spi_speed; ++j) WRITE(DOGLCD_SCK, HIGH); |
| 77 | + } |
| 78 | + b <<= 1; |
| 79 | + if (miso_pin >= 0 && READ(miso_pin)) b |= 1; |
| 80 | + } |
| 81 | + return b; |
| 82 | +} |
| 83 | + |
| 84 | +static void u8g_sw_spi_shift_out(uint8_t val) { |
| 85 | + #if ANY(FYSETC_MINI_12864, MKS_MINI_12864) |
| 86 | + swSpiTransfer_mode_3(val, SPI_speed); |
| 87 | + #else |
| 88 | + swSpiTransfer_mode_0(val, SPI_speed); |
| 89 | + #endif |
| 90 | +} |
| 91 | + |
| 92 | +static uint8_t swSpiInit(const uint8_t spi_speed) { |
| 93 | + #if PIN_EXISTS(LCD_RESET) |
| 94 | + SET_OUTPUT(LCD_RESET_PIN); |
| 95 | + #endif |
| 96 | + SET_OUTPUT(DOGLCD_A0); |
| 97 | + OUT_WRITE(DOGLCD_SCK, LOW); |
| 98 | + OUT_WRITE(DOGLCD_MOSI, LOW); |
| 99 | + OUT_WRITE(DOGLCD_CS, HIGH); |
| 100 | + return spi_speed; |
| 101 | +} |
| 102 | + |
| 103 | +uint8_t u8g_com_HAL_HC32_sw_spi_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr) { |
| 104 | + switch (msg) { |
| 105 | + case U8G_COM_MSG_INIT: |
| 106 | + SPI_speed = swSpiInit(LCD_SPI_SPEED); |
| 107 | + break; |
| 108 | + |
| 109 | + case U8G_COM_MSG_STOP: |
| 110 | + break; |
| 111 | + |
| 112 | + case U8G_COM_MSG_RESET: |
| 113 | + #if PIN_EXISTS(LCD_RESET) |
| 114 | + WRITE(LCD_RESET_PIN, arg_val); |
| 115 | + #endif |
| 116 | + break; |
| 117 | + |
| 118 | + case U8G_COM_MSG_CHIP_SELECT: |
| 119 | + #if ANY(FYSETC_MINI_12864, MKS_MINI_12864) // This LCD SPI is running mode 3 while SD card is running mode 0 |
| 120 | + if (arg_val) { // SCK idle state needs to be set to the proper idle state before |
| 121 | + // the next chip select goes active |
| 122 | + WRITE(DOGLCD_SCK, HIGH); // Set SCK to mode 3 idle state before CS goes active |
| 123 | + WRITE(DOGLCD_CS, LOW); |
| 124 | + } |
| 125 | + else { |
| 126 | + WRITE(DOGLCD_CS, HIGH); |
| 127 | + WRITE(DOGLCD_SCK, LOW); // Set SCK to mode 0 idle state after CS goes inactive |
| 128 | + } |
| 129 | + #else |
| 130 | + WRITE(DOGLCD_CS, !arg_val); |
| 131 | + #endif |
| 132 | + break; |
| 133 | + |
| 134 | + case U8G_COM_MSG_WRITE_BYTE: |
| 135 | + u8g_sw_spi_shift_out(arg_val); |
| 136 | + break; |
| 137 | + |
| 138 | + case U8G_COM_MSG_WRITE_SEQ: { |
| 139 | + uint8_t *ptr = (uint8_t *)arg_ptr; |
| 140 | + while (arg_val > 0) { |
| 141 | + u8g_sw_spi_shift_out(*ptr++); |
| 142 | + arg_val--; |
| 143 | + } |
| 144 | + } break; |
| 145 | + |
| 146 | + case U8G_COM_MSG_WRITE_SEQ_P: { |
| 147 | + uint8_t *ptr = (uint8_t *)arg_ptr; |
| 148 | + while (arg_val > 0) { |
| 149 | + u8g_sw_spi_shift_out(u8g_pgm_read(ptr)); |
| 150 | + ptr++; |
| 151 | + arg_val--; |
| 152 | + } |
| 153 | + } break; |
| 154 | + |
| 155 | + case U8G_COM_MSG_ADDRESS: /* define cmd (arg_val = 0) or data mode (arg_val = 1) */ |
| 156 | + WRITE(DOGLCD_A0, arg_val); |
| 157 | + break; |
| 158 | + } |
| 159 | + return 1; |
| 160 | +} |
| 161 | + |
| 162 | +#endif // U8G_SW_SPI_HC32 |
| 163 | +#endif // ARDUINO_ARCH_HC32 |
0 commit comments