|
| 1 | +/** |
| 2 | + * Marlin 3D Printer Firmware |
| 3 | + * Copyright (c) 2021 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 | +#if defined(ARDUINO_ARCH_STM32) && !defined(STM32GENERIC) |
| 23 | + |
| 24 | +#include "../../../inc/MarlinConfig.h" |
| 25 | + |
| 26 | +#if ENABLED(TFT_TOUCH_DEVICE_GT911) |
| 27 | + |
| 28 | +#include "gt911.h" |
| 29 | +#include "pinconfig.h" |
| 30 | + |
| 31 | +SW_IIC::SW_IIC(uint16_t sda, uint16_t scl) { |
| 32 | + scl_pin = scl; |
| 33 | + sda_pin = sda; |
| 34 | +} |
| 35 | + |
| 36 | +// Software I2C hardware io init |
| 37 | +void SW_IIC::init() { |
| 38 | + OUT_WRITE(scl_pin, HIGH); |
| 39 | + OUT_WRITE(sda_pin, HIGH); |
| 40 | +} |
| 41 | + |
| 42 | +// Software I2C start signal |
| 43 | +void SW_IIC::start() { |
| 44 | + write_sda(HIGH); // SDA = 1 |
| 45 | + write_scl(HIGH); // SCL = 1 |
| 46 | + iic_delay(2); |
| 47 | + write_sda(LOW); // SDA = 0 |
| 48 | + iic_delay(1); |
| 49 | + write_scl(LOW); // SCL = 0 // keep SCL low, avoid false stop caused by level jump caused by SDA switching IN/OUT |
| 50 | +} |
| 51 | + |
| 52 | +// Software I2C stop signal |
| 53 | +void SW_IIC::stop() { |
| 54 | + write_scl(LOW); // SCL = 0 |
| 55 | + iic_delay(2); |
| 56 | + write_sda(LOW); // SDA = 0 |
| 57 | + iic_delay(2); |
| 58 | + write_scl(HIGH); // SCL = 1 |
| 59 | + iic_delay(2); |
| 60 | + write_sda(HIGH); // SDA = 1 |
| 61 | +} |
| 62 | + |
| 63 | +// Software I2C sends ACK or NACK signal |
| 64 | +void SW_IIC::send_ack(bool ack) { |
| 65 | + write_sda(ack ? LOW : HIGH); // SDA = !ack |
| 66 | + iic_delay(2); |
| 67 | + write_scl(HIGH); // SCL = 1 |
| 68 | + iic_delay(2); |
| 69 | + write_scl(LOW); // SCL = 0 |
| 70 | +} |
| 71 | + |
| 72 | +// Software I2C read ACK or NACK signal |
| 73 | +bool SW_IIC::read_ack() { |
| 74 | + bool error = 0; |
| 75 | + set_sda_in(); |
| 76 | + |
| 77 | + iic_delay(2); |
| 78 | + |
| 79 | + write_scl(HIGH); // SCL = 1 |
| 80 | + error = read_sda(); |
| 81 | + |
| 82 | + iic_delay(2); |
| 83 | + |
| 84 | + write_scl(LOW); // SCL = 0 |
| 85 | + |
| 86 | + set_sda_out(); |
| 87 | + return error; |
| 88 | +} |
| 89 | + |
| 90 | +void SW_IIC::send_byte(uint8_t txd) { |
| 91 | + LOOP_L_N(i, 8) { |
| 92 | + write_sda(txd & 0x80); // write data bit |
| 93 | + txd <<= 1; |
| 94 | + iic_delay(1); |
| 95 | + write_scl(HIGH); // SCL = 1 |
| 96 | + iic_delay(2); |
| 97 | + write_scl(LOW); // SCL = 0 |
| 98 | + iic_delay(1); |
| 99 | + } |
| 100 | + |
| 101 | + read_ack(); // wait ack |
| 102 | +} |
| 103 | + |
| 104 | +uint8_t SW_IIC::read_byte(bool ack) { |
| 105 | + uint8_t data = 0; |
| 106 | + |
| 107 | + set_sda_in(); |
| 108 | + LOOP_L_N(i, 8) { |
| 109 | + write_scl(HIGH); // SCL = 1 |
| 110 | + iic_delay(1); |
| 111 | + data <<= 1; |
| 112 | + if (read_sda()) data++; |
| 113 | + write_scl(LOW); // SCL = 0 |
| 114 | + iic_delay(2); |
| 115 | + } |
| 116 | + set_sda_out(); |
| 117 | + |
| 118 | + send_ack(ack); |
| 119 | + |
| 120 | + return data; |
| 121 | +} |
| 122 | + |
| 123 | +GT911_REG_MAP GT911::reg; |
| 124 | +SW_IIC GT911::sw_iic = SW_IIC(GT911_SW_I2C_SDA_PIN, GT911_SW_I2C_SCL_PIN); |
| 125 | + |
| 126 | +void GT911::write_reg(uint16_t reg, uint8_t reg_len, uint8_t* w_data, uint8_t w_len) { |
| 127 | + sw_iic.start(); |
| 128 | + sw_iic.send_byte(gt911_slave_address); // Set IIC Slave address |
| 129 | + LOOP_L_N(i, reg_len) { // Set reg address |
| 130 | + uint8_t r = (reg >> (8 * (reg_len - 1 - i))) & 0xFF; |
| 131 | + sw_iic.send_byte(r); |
| 132 | + } |
| 133 | + |
| 134 | + LOOP_L_N(i, w_len) { // Write data to reg |
| 135 | + sw_iic.send_byte(w_data[i]); |
| 136 | + } |
| 137 | + sw_iic.stop(); |
| 138 | +} |
| 139 | + |
| 140 | +void GT911::read_reg(uint16_t reg, uint8_t reg_len, uint8_t* r_data, uint8_t r_len) { |
| 141 | + sw_iic.start(); |
| 142 | + sw_iic.send_byte(gt911_slave_address); // Set IIC Slave address |
| 143 | + LOOP_L_N(i, reg_len) { // Set reg address |
| 144 | + uint8_t r = (reg >> (8 * (reg_len - 1 - i))) & 0xFF; |
| 145 | + sw_iic.send_byte(r); |
| 146 | + } |
| 147 | + |
| 148 | + sw_iic.start(); |
| 149 | + sw_iic.send_byte(gt911_slave_address + 1); // Set read mode |
| 150 | + |
| 151 | + LOOP_L_N(i, r_len) { |
| 152 | + r_data[i] = sw_iic.read_byte(1); // Read data from reg |
| 153 | + } |
| 154 | + sw_iic.stop(); |
| 155 | +} |
| 156 | + |
| 157 | +void GT911::Init() { |
| 158 | + OUT_WRITE(GT911_RST_PIN, LOW); |
| 159 | + OUT_WRITE(GT911_INT_PIN, LOW); |
| 160 | + delay(20); |
| 161 | + WRITE(GT911_RST_PIN, HIGH); |
| 162 | + SET_INPUT(GT911_INT_PIN); |
| 163 | + |
| 164 | + sw_iic.init(); |
| 165 | + |
| 166 | + uint8_t clear_reg = 0x0000; |
| 167 | + write_reg(0x814E, 2, &clear_reg, 2); // Reset to 0 for start |
| 168 | +} |
| 169 | + |
| 170 | +bool GT911::getFirstTouchPoint(int16_t *x, int16_t *y) { |
| 171 | + read_reg(0x814E, 2, ®.REG.status, 1); |
| 172 | + |
| 173 | + if (reg.REG.status & 0x80) { |
| 174 | + uint8_t clear_reg = 0x00; |
| 175 | + write_reg(0x814E, 2, &clear_reg, 1); // Reset to 0 for start |
| 176 | + read_reg(0x8150, 2, reg.map + 2, 8 * (reg.REG.status & 0x0F)); |
| 177 | + |
| 178 | + // First touch point |
| 179 | + *x = ((reg.REG.point[0].xh & 0x0F) << 8) | reg.REG.point[0].xl; |
| 180 | + *y = ((reg.REG.point[0].yh & 0x0F) << 8) | reg.REG.point[0].yl; |
| 181 | + return true; |
| 182 | + } |
| 183 | + return false; |
| 184 | +} |
| 185 | + |
| 186 | +bool GT911::getPoint(int16_t *x, int16_t *y) { |
| 187 | + static bool touched = 0; |
| 188 | + static int16_t read_x = 0, read_y = 0; |
| 189 | + static millis_t next_time = 0; |
| 190 | + |
| 191 | + if (ELAPSED(millis(), next_time)) { |
| 192 | + touched = getFirstTouchPoint(&read_x, &read_y); |
| 193 | + next_time = millis() + 20; |
| 194 | + } |
| 195 | + |
| 196 | + *x = read_x; |
| 197 | + *y = read_y; |
| 198 | + return touched; |
| 199 | +} |
| 200 | + |
| 201 | +#endif // TFT_TOUCH_DEVICE_GT911 |
| 202 | +#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC |
0 commit comments