|
| 1 | +/** |
| 2 | + * Marlin 3D Printer Firmware |
| 3 | + * Copyright (c) 2025 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 | +/** |
| 24 | + * 2-Wire I2C COM Driver |
| 25 | + * |
| 26 | + * Handles both Hardware and Software I2C so any pins can be used as SDA and SLC. |
| 27 | + * Wire library is used for Hardware I2C. |
| 28 | + * SlowSoftWire is used for Software I2C. |
| 29 | + * |
| 30 | + * Wire / SoftWire library selection can be done automatically at runtime. |
| 31 | + * |
| 32 | + * SDA and SLC pins must be named DOGLCD_SDA_PIN, DOGLCD_SCL_PIN to distinguish |
| 33 | + * from other I2C devices (e.g., EEPROM) that use I2C_SDA_PIN, I2C_SLC_PIN. |
| 34 | + */ |
| 35 | +#ifdef ARDUINO_ARCH_STM32 |
| 36 | + |
| 37 | +#include "../../../inc/MarlinConfig.h" |
| 38 | + |
| 39 | +#if HAS_U8GLIB_I2C_OLED |
| 40 | + |
| 41 | +#include <U8glib-HAL.h> |
| 42 | + |
| 43 | +#if ENABLED(U8G_USES_HW_I2C) |
| 44 | + #include <Wire.h> |
| 45 | + #ifndef MASTER_ADDRESS |
| 46 | + #define MASTER_ADDRESS 0x01 |
| 47 | + #endif |
| 48 | +#endif |
| 49 | + |
| 50 | +#if ENABLED(U8G_USES_SW_I2C) |
| 51 | + #include <SlowSoftI2CMaster.h> |
| 52 | + #include <SlowSoftWire.h> |
| 53 | +#endif |
| 54 | + |
| 55 | +/** |
| 56 | + * BUFFER_LENGTH is defined in libraries\Wire\utility\WireBase.h |
| 57 | + * Default value is 32 |
| 58 | + * Increase this value to 144 to send U8G_COM_MSG_WRITE_SEQ in single block |
| 59 | + */ |
| 60 | +#ifndef BUFFER_LENGTH |
| 61 | + #define BUFFER_LENGTH 32 |
| 62 | +#endif |
| 63 | +#if BUFFER_LENGTH > 144 |
| 64 | + #error "BUFFER_LENGTH should not be greater than 144." |
| 65 | +#endif |
| 66 | +#define I2C_MAX_LENGTH (BUFFER_LENGTH - 1) |
| 67 | + |
| 68 | +uint8_t u8g_com_stm32duino_ssd_i2c_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr) { |
| 69 | + // Hardware I2C flag |
| 70 | + #ifdef COMPILE_TIME_I2C_IS_HARDWARE |
| 71 | + constexpr bool isHardI2C = ENABLED(COMPILE_TIME_I2C_IS_HARDWARE); |
| 72 | + #else |
| 73 | + static bool isHardI2C = false; |
| 74 | + static bool i2c_initialized = false; // Flag to only run init/linking code once |
| 75 | + if (!i2c_initialized) { // Init runtime linkages |
| 76 | + i2c_initialized = true; // Only do this once |
| 77 | + I2C_TypeDef *i2cInstance1 = (I2C_TypeDef *)pinmap_peripheral(digitalPinToPinName(DOGLCD_SDA_PIN), PinMap_I2C_SDA); |
| 78 | + I2C_TypeDef *i2cInstance2 = (I2C_TypeDef *)pinmap_peripheral(digitalPinToPinName(DOGLCD_SCL_PIN), PinMap_I2C_SCL); |
| 79 | + isHardI2C = (i2cInstance1 && (i2cInstance1 == i2cInstance2)); // Found hardware I2C controller |
| 80 | + } |
| 81 | + #endif |
| 82 | + |
| 83 | + static uint8_t msgInitCount = 0; // Ignore all messages until 2nd U8G_COM_MSG_INIT |
| 84 | + if (msgInitCount) { |
| 85 | + if (msg == U8G_COM_MSG_INIT) msgInitCount--; |
| 86 | + if (msgInitCount) return -1; |
| 87 | + } |
| 88 | + |
| 89 | + static uint8_t control; |
| 90 | + if (isHardI2C) { // Found hardware I2C controller |
| 91 | + #if ENABLED(U8G_USES_HW_I2C) |
| 92 | + static TwoWire wire2; // A TwoWire object for use below |
| 93 | + switch (msg) { |
| 94 | + case U8G_COM_MSG_INIT: |
| 95 | + wire2.setClock(400000); |
| 96 | + wire2.setSCL(DOGLCD_SCL_PIN); |
| 97 | + wire2.setSDA(DOGLCD_SDA_PIN); |
| 98 | + wire2.begin(MASTER_ADDRESS, 0); // Start as master |
| 99 | + break; |
| 100 | + |
| 101 | + case U8G_COM_MSG_ADDRESS: // Define cmd (arg_val = 0) or data mode (arg_val = 1) |
| 102 | + control = arg_val ? 0x40 : 0x00; |
| 103 | + break; |
| 104 | + |
| 105 | + case U8G_COM_MSG_WRITE_BYTE: |
| 106 | + wire2.beginTransmission(0x3C); |
| 107 | + wire2.write(control); |
| 108 | + wire2.write(arg_val); |
| 109 | + wire2.endTransmission(); |
| 110 | + break; |
| 111 | + |
| 112 | + case U8G_COM_MSG_WRITE_SEQ: { |
| 113 | + uint8_t* dataptr = (uint8_t*)arg_ptr; |
| 114 | + #ifdef I2C_MAX_LENGTH |
| 115 | + while (arg_val > 0) { |
| 116 | + wire2.beginTransmission(0x3C); |
| 117 | + wire2.write(control); |
| 118 | + if (arg_val <= I2C_MAX_LENGTH) { |
| 119 | + wire2.write(dataptr, arg_val); |
| 120 | + arg_val = 0; |
| 121 | + } |
| 122 | + else { |
| 123 | + wire2.write(dataptr, I2C_MAX_LENGTH); |
| 124 | + arg_val -= I2C_MAX_LENGTH; |
| 125 | + dataptr += I2C_MAX_LENGTH; |
| 126 | + } |
| 127 | + wire2.endTransmission(); |
| 128 | + } |
| 129 | + #else |
| 130 | + wire2.beginTransmission(0x3C); |
| 131 | + wire2.write(control); |
| 132 | + wire2.write(dataptr, arg_val); |
| 133 | + wire2.endTransmission(); |
| 134 | + #endif // I2C_MAX_LENGTH |
| 135 | + break; |
| 136 | + } |
| 137 | + } |
| 138 | + #endif // U8G_USES_HW_I2C |
| 139 | + } |
| 140 | + else { // Software I2C |
| 141 | + #if ENABLED(U8G_USES_SW_I2C) |
| 142 | + static SlowSoftWire sWire = SlowSoftWire(DOGLCD_SDA_PIN, DOGLCD_SCL_PIN); |
| 143 | + |
| 144 | + switch (msg) { |
| 145 | + case U8G_COM_MSG_INIT: |
| 146 | + sWire.setClock(400000); |
| 147 | + sWire.begin(); // Start as master |
| 148 | + break; |
| 149 | + |
| 150 | + case U8G_COM_MSG_ADDRESS: // Define cmd (arg_val = 0) or data mode (arg_val = 1) |
| 151 | + control = arg_val ? 0x40 : 0x00; |
| 152 | + break; |
| 153 | + |
| 154 | + case U8G_COM_MSG_WRITE_BYTE: |
| 155 | + sWire.beginTransmission((uint8_t)0x3C); |
| 156 | + sWire.write((uint8_t)control); |
| 157 | + sWire.write((uint8_t)arg_val); |
| 158 | + sWire.endTransmission(); |
| 159 | + break; |
| 160 | + |
| 161 | + case U8G_COM_MSG_WRITE_SEQ: { |
| 162 | + uint8_t* dataptr = (uint8_t*)arg_ptr; |
| 163 | + #ifdef I2C_MAX_LENGTH |
| 164 | + while (arg_val > 0) { |
| 165 | + sWire.beginTransmission((uint8_t)0x3C); |
| 166 | + sWire.write((uint8_t)control); |
| 167 | + if (arg_val <= I2C_MAX_LENGTH) { |
| 168 | + sWire.write((const uint8_t *)dataptr, (size_t)arg_val); |
| 169 | + arg_val = 0; |
| 170 | + } |
| 171 | + else { |
| 172 | + sWire.write((const uint8_t *)dataptr, I2C_MAX_LENGTH); |
| 173 | + arg_val -= I2C_MAX_LENGTH; |
| 174 | + dataptr += I2C_MAX_LENGTH; |
| 175 | + } |
| 176 | + sWire.endTransmission(); |
| 177 | + } |
| 178 | + #else |
| 179 | + sWire.beginTransmission((uint8_t)0x3C); |
| 180 | + sWire.write((uint8_t)control); |
| 181 | + sWire.write((const uint8_t *)dataptr, (size_t)arg_val); |
| 182 | + sWire.endTransmission(); |
| 183 | + #endif // I2C_MAX_LENGTH |
| 184 | + break; |
| 185 | + } |
| 186 | + } |
| 187 | + #endif // U8G_USES_SW_I2C |
| 188 | + } |
| 189 | + |
| 190 | + return 1; |
| 191 | +} |
| 192 | + |
| 193 | +#endif // HAS_U8GLIB_I2C_OLED |
| 194 | +#endif // ARDUINO_ARCH_STM32 |
0 commit comments