snled27351-mono.h (9642B)
1 /* Copyright 2021 @ Keychron (https://www.keychron.com) 2 * 3 * This program is free software: you can redistribute it and/or modify 4 * it under the terms of the GNU General Public License as published by 5 * the Free Software Foundation, either version 2 of the License, or 6 * (at your option) any later version. 7 * 8 * This program is distributed in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * GNU General Public License for more details. 12 * 13 * You should have received a copy of the GNU General Public License 14 * along with this program. If not, see <http://www.gnu.org/licenses/>. 15 */ 16 17 #pragma once 18 19 #include <stdint.h> 20 #include <stdbool.h> 21 #include "progmem.h" 22 #include "util.h" 23 24 #define SNLED27351_REG_COMMAND 0xFD 25 #define SNLED27351_COMMAND_LED_CONTROL 0x00 26 #define SNLED27351_COMMAND_PWM 0x01 27 #define SNLED27351_COMMAND_FUNCTION 0x03 28 #define SNLED27351_COMMAND_CURRENT_TUNE 0x04 29 30 #define SNLED27351_FUNCTION_REG_SOFTWARE_SHUTDOWN 0x00 31 #define SNLED27351_SOFTWARE_SHUTDOWN_SSD_SHUTDOWN (0x0 << 0) 32 #define SNLED27351_SOFTWARE_SHUTDOWN_SSD_NORMAL (0x1 << 0) 33 34 #define SNLED27351_FUNCTION_REG_ID 0x11 35 #define SNLED27351_DRIVER_ID 0x8A 36 37 #define SNLED27351_FUNCTION_REG_PULLDOWNUP 0x13 38 #define SNLED27351_PULLDOWNUP_ALL_ENABLED 0xAA 39 40 #define SNLED27351_FUNCTION_REG_SCAN_PHASE 0x14 41 #define SNLED27351_SCAN_PHASE_12_CHANNEL 0x00 42 #define SNLED27351_SCAN_PHASE_11_CHANNEL 0x01 43 #define SNLED27351_SCAN_PHASE_10_CHANNEL 0x02 44 #define SNLED27351_SCAN_PHASE_9_CHANNEL 0x03 45 #define SNLED27351_SCAN_PHASE_8_CHANNEL 0x04 46 #define SNLED27351_SCAN_PHASE_7_CHANNEL 0x05 47 #define SNLED27351_SCAN_PHASE_6_CHANNEL 0x06 48 #define SNLED27351_SCAN_PHASE_5_CHANNEL 0x07 49 #define SNLED27351_SCAN_PHASE_4_CHANNEL 0x08 50 #define SNLED27351_SCAN_PHASE_3_CHANNEL 0x09 51 #define SNLED27351_SCAN_PHASE_2_CHANNEL 0x0A 52 #define SNLED27351_SCAN_PHASE_1_CHANNEL 0x0B 53 54 #define SNLED27351_FUNCTION_REG_SLEW_RATE_CONTROL_MODE_1 0x15 55 #define SNLED27351_SLEW_RATE_CONTROL_MODE_1_PDP_ENABLE (0b1 << 2) 56 57 #define SNLED27351_FUNCTION_REG_SLEW_RATE_CONTROL_MODE_2 0x16 58 #define SNLED27351_SLEW_RATE_CONTROL_MODE_2_SSL_ENABLE (0b1 << 6) 59 #define SNLED27351_SLEW_RATE_CONTROL_MODE_2_DSL_ENABLE (0b1 << 7) 60 61 #define SNLED27351_FUNCTION_REG_OPEN_SHORT_ENABLE 0x17 62 #define SNLED27351_OPEN_SHORT_ENABLE_SDS_ENABLE (0b1 << 6) 63 #define SNLED27351_OPEN_SHORT_ENABLE_ODS_ENABLE (0b1 << 7) 64 65 #define SNLED27351_FUNCTION_REG_OPEN_SHORT_DUTY 0x18 66 67 #define SNLED27351_FUNCTION_REG_OPEN_SHORT_FLAG 0x19 68 #define SNLED27351_OPEN_SHORT_FLAG_OSINT_ENABLE (0b1 << 6) 69 #define SNLED27351_OPEN_SHORT_FLAG_ODINT_ENABLE (0b1 << 7) 70 71 #define SNLED27351_FUNCTION_REG_SOFTWARE_SLEEP 0x1A 72 #define SNLED27351_SOFTWARE_SLEEP_ENABLE (0b1 << 1) 73 74 // LED Control Registers 75 #define SNLED27351_LED_CONTROL_ON_OFF_FIRST_ADDR 0x0 76 #define SNLED27351_LED_CONTROL_ON_OFF_LAST_ADDR 0x17 77 #define SNLED27351_LED_CONTROL_ON_OFF_LENGTH ((SNLED27351_LED_CONTROL_ON_OFF_LAST_ADDR - SNLED27351_LED_CONTROL_ON_OFF_FIRST_ADDR) + 1) 78 79 #define SNLED27351_LED_CONTROL_OPEN_FIRST_ADDR 0x18 80 #define SNLED27351_LED_CONTROL_OPEN_LAST_ADDR 0x2F 81 #define SNLED27351_LED_CONTROL_OPEN_LENGTH ((SNLED27351_LED_CONTROL_OPEN_LAST_ADDR - SNLED27351_LED_CONTROL_OPEN_FIRST_ADDR) + 1) 82 83 #define SNLED27351_LED_CONTROL_SHORT_FIRST_ADDR 0x30 84 #define SNLED27351_LED_CONTROL_SHORT_LAST_ADDR 0x47 85 #define SNLED27351_LED_CONTROL_SHORT_LENGTH ((SNLED27351_LED_CONTROL_SHORT_LAST_ADDR - SNLED27351_LED_CONTROL_SHORT_FIRST_ADDR) + 1) 86 87 #define SNLED27351_LED_CONTROL_PAGE_LENGTH 0x48 88 89 // LED Control Registers 90 #define SNLED27351_LED_PWM_FIRST_ADDR 0x00 91 #define SNLED27351_LED_PWM_LAST_ADDR 0xBF 92 #define SNLED27351_LED_PWM_LENGTH 0xC0 93 94 // Current Tune Registers 95 #define SNLED27351_LED_CURRENT_TUNE_FIRST_ADDR 0x00 96 #define SNLED27351_LED_CURRENT_TUNE_LAST_ADDR 0x0B 97 #define SNLED27351_LED_CURRENT_TUNE_LENGTH 0x0C 98 99 #define SNLED27351_I2C_ADDRESS_GND 0x74 100 #define SNLED27351_I2C_ADDRESS_SCL 0x75 101 #define SNLED27351_I2C_ADDRESS_SDA 0x76 102 #define SNLED27351_I2C_ADDRESS_VDDIO 0x77 103 104 #if defined(LED_MATRIX_SNLED27351) 105 # define SNLED27351_LED_COUNT LED_MATRIX_LED_COUNT 106 #endif 107 108 #if defined(SNLED27351_I2C_ADDRESS_4) 109 # define SNLED27351_DRIVER_COUNT 4 110 #elif defined(SNLED27351_I2C_ADDRESS_3) 111 # define SNLED27351_DRIVER_COUNT 3 112 #elif defined(SNLED27351_I2C_ADDRESS_2) 113 # define SNLED27351_DRIVER_COUNT 2 114 #elif defined(SNLED27351_I2C_ADDRESS_1) 115 # define SNLED27351_DRIVER_COUNT 1 116 #endif 117 118 typedef struct snled27351_led_t { 119 uint8_t driver : 2; 120 uint8_t v; 121 } PACKED snled27351_led_t; 122 123 extern const snled27351_led_t PROGMEM g_snled27351_leds[SNLED27351_LED_COUNT]; 124 125 void snled27351_init_drivers(void); 126 void snled27351_init(uint8_t index); 127 void snled27351_select_page(uint8_t index, uint8_t page); 128 void snled27351_write_register(uint8_t index, uint8_t reg, uint8_t data); 129 130 void snled27351_set_value(int index, uint8_t value); 131 void snled27351_set_value_all(uint8_t value); 132 133 void snled27351_set_led_control_register(uint8_t index, bool value); 134 135 // This should not be called from an interrupt 136 // (eg. from a timer interrupt). 137 // Call this while idle (in between matrix scans). 138 // If the buffer is dirty, it will update the driver with the buffer. 139 void snled27351_update_pwm_buffers(uint8_t index); 140 void snled27351_update_led_control_registers(uint8_t index); 141 142 void snled27351_flush(void); 143 144 void snled27351_sw_return_normal(uint8_t index); 145 void snled27351_sw_shutdown(uint8_t index); 146 147 #define CB1_CA1 0x00 148 #define CB1_CA2 0x01 149 #define CB1_CA3 0x02 150 #define CB1_CA4 0x03 151 #define CB1_CA5 0x04 152 #define CB1_CA6 0x05 153 #define CB1_CA7 0x06 154 #define CB1_CA8 0x07 155 #define CB1_CA9 0x08 156 #define CB1_CA10 0x09 157 #define CB1_CA11 0x0A 158 #define CB1_CA12 0x0B 159 #define CB1_CA13 0x0C 160 #define CB1_CA14 0x0D 161 #define CB1_CA15 0x0E 162 #define CB1_CA16 0x0F 163 164 #define CB2_CA1 0x10 165 #define CB2_CA2 0x11 166 #define CB2_CA3 0x12 167 #define CB2_CA4 0x13 168 #define CB2_CA5 0x14 169 #define CB2_CA6 0x15 170 #define CB2_CA7 0x16 171 #define CB2_CA8 0x17 172 #define CB2_CA9 0x18 173 #define CB2_CA10 0x19 174 #define CB2_CA11 0x1A 175 #define CB2_CA12 0x1B 176 #define CB2_CA13 0x1C 177 #define CB2_CA14 0x1D 178 #define CB2_CA15 0x1E 179 #define CB2_CA16 0x1F 180 181 #define CB3_CA1 0x20 182 #define CB3_CA2 0x21 183 #define CB3_CA3 0x22 184 #define CB3_CA4 0x23 185 #define CB3_CA5 0x24 186 #define CB3_CA6 0x25 187 #define CB3_CA7 0x26 188 #define CB3_CA8 0x27 189 #define CB3_CA9 0x28 190 #define CB3_CA10 0x29 191 #define CB3_CA11 0x2A 192 #define CB3_CA12 0x2B 193 #define CB3_CA13 0x2C 194 #define CB3_CA14 0x2D 195 #define CB3_CA15 0x2E 196 #define CB3_CA16 0x2F 197 198 #define CB4_CA1 0x30 199 #define CB4_CA2 0x31 200 #define CB4_CA3 0x32 201 #define CB4_CA4 0x33 202 #define CB4_CA5 0x34 203 #define CB4_CA6 0x35 204 #define CB4_CA7 0x36 205 #define CB4_CA8 0x37 206 #define CB4_CA9 0x38 207 #define CB4_CA10 0x39 208 #define CB4_CA11 0x3A 209 #define CB4_CA12 0x3B 210 #define CB4_CA13 0x3C 211 #define CB4_CA14 0x3D 212 #define CB4_CA15 0x3E 213 #define CB4_CA16 0x3F 214 215 #define CB5_CA1 0x40 216 #define CB5_CA2 0x41 217 #define CB5_CA3 0x42 218 #define CB5_CA4 0x43 219 #define CB5_CA5 0x44 220 #define CB5_CA6 0x45 221 #define CB5_CA7 0x46 222 #define CB5_CA8 0x47 223 #define CB5_CA9 0x48 224 #define CB5_CA10 0x49 225 #define CB5_CA11 0x4A 226 #define CB5_CA12 0x4B 227 #define CB5_CA13 0x4C 228 #define CB5_CA14 0x4D 229 #define CB5_CA15 0x4E 230 #define CB5_CA16 0x4F 231 232 #define CB6_CA1 0x50 233 #define CB6_CA2 0x51 234 #define CB6_CA3 0x52 235 #define CB6_CA4 0x53 236 #define CB6_CA5 0x54 237 #define CB6_CA6 0x55 238 #define CB6_CA7 0x56 239 #define CB6_CA8 0x57 240 #define CB6_CA9 0x58 241 #define CB6_CA10 0x59 242 #define CB6_CA11 0x5A 243 #define CB6_CA12 0x5B 244 #define CB6_CA13 0x5C 245 #define CB6_CA14 0x5D 246 #define CB6_CA15 0x5E 247 #define CB6_CA16 0x5F 248 249 #define CB7_CA1 0x60 250 #define CB7_CA2 0x61 251 #define CB7_CA3 0x62 252 #define CB7_CA4 0x63 253 #define CB7_CA5 0x64 254 #define CB7_CA6 0x65 255 #define CB7_CA7 0x66 256 #define CB7_CA8 0x67 257 #define CB7_CA9 0x68 258 #define CB7_CA10 0x69 259 #define CB7_CA11 0x6A 260 #define CB7_CA12 0x6B 261 #define CB7_CA13 0x6C 262 #define CB7_CA14 0x6D 263 #define CB7_CA15 0x6E 264 #define CB7_CA16 0x6F 265 266 #define CB8_CA1 0x70 267 #define CB8_CA2 0x71 268 #define CB8_CA3 0x72 269 #define CB8_CA4 0x73 270 #define CB8_CA5 0x74 271 #define CB8_CA6 0x75 272 #define CB8_CA7 0x76 273 #define CB8_CA8 0x77 274 #define CB8_CA9 0x78 275 #define CB8_CA10 0x79 276 #define CB8_CA11 0x7A 277 #define CB8_CA12 0x7B 278 #define CB8_CA13 0x7C 279 #define CB8_CA14 0x7D 280 #define CB8_CA15 0x7E 281 #define CB8_CA16 0x7F 282 283 #define CB9_CA1 0x80 284 #define CB9_CA2 0x81 285 #define CB9_CA3 0x82 286 #define CB9_CA4 0x83 287 #define CB9_CA5 0x84 288 #define CB9_CA6 0x85 289 #define CB9_CA7 0x86 290 #define CB9_CA8 0x87 291 #define CB9_CA9 0x88 292 #define CB9_CA10 0x89 293 #define CB9_CA11 0x8A 294 #define CB9_CA12 0x8B 295 #define CB9_CA13 0x8C 296 #define CB9_CA14 0x8D 297 #define CB9_CA15 0x8E 298 #define CB9_CA16 0x8F 299 300 #define CB10_CA1 0x90 301 #define CB10_CA2 0x91 302 #define CB10_CA3 0x92 303 #define CB10_CA4 0x93 304 #define CB10_CA5 0x94 305 #define CB10_CA6 0x95 306 #define CB10_CA7 0x96 307 #define CB10_CA8 0x97 308 #define CB10_CA9 0x98 309 #define CB10_CA10 0x99 310 #define CB10_CA11 0x9A 311 #define CB10_CA12 0x9B 312 #define CB10_CA13 0x9C 313 #define CB10_CA14 0x9D 314 #define CB10_CA15 0x9E 315 #define CB10_CA16 0x9F 316 317 #define CB11_CA1 0xA0 318 #define CB11_CA2 0xA1 319 #define CB11_CA3 0xA2 320 #define CB11_CA4 0xA3 321 #define CB11_CA5 0xA4 322 #define CB11_CA6 0xA5 323 #define CB11_CA7 0xA6 324 #define CB11_CA8 0xA7 325 #define CB11_CA9 0xA8 326 #define CB11_CA10 0xA9 327 #define CB11_CA11 0xAA 328 #define CB11_CA12 0xAB 329 #define CB11_CA13 0xAC 330 #define CB11_CA14 0xAD 331 #define CB11_CA15 0xAE 332 #define CB11_CA16 0xAF 333 334 #define CB12_CA1 0xB0 335 #define CB12_CA2 0xB1 336 #define CB12_CA3 0xB2 337 #define CB12_CA4 0xB3 338 #define CB12_CA5 0xB4 339 #define CB12_CA6 0xB5 340 #define CB12_CA7 0xB6 341 #define CB12_CA8 0xB7 342 #define CB12_CA9 0xB8 343 #define CB12_CA10 0xB9 344 #define CB12_CA11 0xBA 345 #define CB12_CA12 0xBB 346 #define CB12_CA13 0xBC 347 #define CB12_CA14 0xBD 348 #define CB12_CA15 0xBE 349 #define CB12_CA16 0xBF