qmk_firmware

QMK firmware for my keyboards (Corne, Sweep Ferris) and trackball (Ploopy Adept)
Log | Files | Refs | Submodules | LICENSE

snled27351.h (9747B)


      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(RGB_MATRIX_SNLED27351)
    105 #    define SNLED27351_LED_COUNT RGB_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 r;
    121     uint8_t g;
    122     uint8_t b;
    123 } PACKED snled27351_led_t;
    124 
    125 extern const snled27351_led_t PROGMEM g_snled27351_leds[SNLED27351_LED_COUNT];
    126 
    127 void snled27351_init_drivers(void);
    128 void snled27351_init(uint8_t index);
    129 void snled27351_select_page(uint8_t index, uint8_t page);
    130 void snled27351_write_register(uint8_t index, uint8_t reg, uint8_t data);
    131 
    132 void snled27351_set_color(int index, uint8_t red, uint8_t green, uint8_t blue);
    133 void snled27351_set_color_all(uint8_t red, uint8_t green, uint8_t blue);
    134 
    135 void snled27351_set_led_control_register(uint8_t index, bool red, bool green, bool blue);
    136 
    137 // This should not be called from an interrupt
    138 // (eg. from a timer interrupt).
    139 // Call this while idle (in between matrix scans).
    140 // If the buffer is dirty, it will update the driver with the buffer.
    141 void snled27351_update_pwm_buffers(uint8_t index);
    142 void snled27351_update_led_control_registers(uint8_t index);
    143 
    144 void snled27351_flush(void);
    145 
    146 void snled27351_sw_return_normal(uint8_t index);
    147 void snled27351_sw_shutdown(uint8_t index);
    148 
    149 #define CB1_CA1 0x00
    150 #define CB1_CA2 0x01
    151 #define CB1_CA3 0x02
    152 #define CB1_CA4 0x03
    153 #define CB1_CA5 0x04
    154 #define CB1_CA6 0x05
    155 #define CB1_CA7 0x06
    156 #define CB1_CA8 0x07
    157 #define CB1_CA9 0x08
    158 #define CB1_CA10 0x09
    159 #define CB1_CA11 0x0A
    160 #define CB1_CA12 0x0B
    161 #define CB1_CA13 0x0C
    162 #define CB1_CA14 0x0D
    163 #define CB1_CA15 0x0E
    164 #define CB1_CA16 0x0F
    165 
    166 #define CB2_CA1 0x10
    167 #define CB2_CA2 0x11
    168 #define CB2_CA3 0x12
    169 #define CB2_CA4 0x13
    170 #define CB2_CA5 0x14
    171 #define CB2_CA6 0x15
    172 #define CB2_CA7 0x16
    173 #define CB2_CA8 0x17
    174 #define CB2_CA9 0x18
    175 #define CB2_CA10 0x19
    176 #define CB2_CA11 0x1A
    177 #define CB2_CA12 0x1B
    178 #define CB2_CA13 0x1C
    179 #define CB2_CA14 0x1D
    180 #define CB2_CA15 0x1E
    181 #define CB2_CA16 0x1F
    182 
    183 #define CB3_CA1 0x20
    184 #define CB3_CA2 0x21
    185 #define CB3_CA3 0x22
    186 #define CB3_CA4 0x23
    187 #define CB3_CA5 0x24
    188 #define CB3_CA6 0x25
    189 #define CB3_CA7 0x26
    190 #define CB3_CA8 0x27
    191 #define CB3_CA9 0x28
    192 #define CB3_CA10 0x29
    193 #define CB3_CA11 0x2A
    194 #define CB3_CA12 0x2B
    195 #define CB3_CA13 0x2C
    196 #define CB3_CA14 0x2D
    197 #define CB3_CA15 0x2E
    198 #define CB3_CA16 0x2F
    199 
    200 #define CB4_CA1 0x30
    201 #define CB4_CA2 0x31
    202 #define CB4_CA3 0x32
    203 #define CB4_CA4 0x33
    204 #define CB4_CA5 0x34
    205 #define CB4_CA6 0x35
    206 #define CB4_CA7 0x36
    207 #define CB4_CA8 0x37
    208 #define CB4_CA9 0x38
    209 #define CB4_CA10 0x39
    210 #define CB4_CA11 0x3A
    211 #define CB4_CA12 0x3B
    212 #define CB4_CA13 0x3C
    213 #define CB4_CA14 0x3D
    214 #define CB4_CA15 0x3E
    215 #define CB4_CA16 0x3F
    216 
    217 #define CB5_CA1 0x40
    218 #define CB5_CA2 0x41
    219 #define CB5_CA3 0x42
    220 #define CB5_CA4 0x43
    221 #define CB5_CA5 0x44
    222 #define CB5_CA6 0x45
    223 #define CB5_CA7 0x46
    224 #define CB5_CA8 0x47
    225 #define CB5_CA9 0x48
    226 #define CB5_CA10 0x49
    227 #define CB5_CA11 0x4A
    228 #define CB5_CA12 0x4B
    229 #define CB5_CA13 0x4C
    230 #define CB5_CA14 0x4D
    231 #define CB5_CA15 0x4E
    232 #define CB5_CA16 0x4F
    233 
    234 #define CB6_CA1 0x50
    235 #define CB6_CA2 0x51
    236 #define CB6_CA3 0x52
    237 #define CB6_CA4 0x53
    238 #define CB6_CA5 0x54
    239 #define CB6_CA6 0x55
    240 #define CB6_CA7 0x56
    241 #define CB6_CA8 0x57
    242 #define CB6_CA9 0x58
    243 #define CB6_CA10 0x59
    244 #define CB6_CA11 0x5A
    245 #define CB6_CA12 0x5B
    246 #define CB6_CA13 0x5C
    247 #define CB6_CA14 0x5D
    248 #define CB6_CA15 0x5E
    249 #define CB6_CA16 0x5F
    250 
    251 #define CB7_CA1 0x60
    252 #define CB7_CA2 0x61
    253 #define CB7_CA3 0x62
    254 #define CB7_CA4 0x63
    255 #define CB7_CA5 0x64
    256 #define CB7_CA6 0x65
    257 #define CB7_CA7 0x66
    258 #define CB7_CA8 0x67
    259 #define CB7_CA9 0x68
    260 #define CB7_CA10 0x69
    261 #define CB7_CA11 0x6A
    262 #define CB7_CA12 0x6B
    263 #define CB7_CA13 0x6C
    264 #define CB7_CA14 0x6D
    265 #define CB7_CA15 0x6E
    266 #define CB7_CA16 0x6F
    267 
    268 #define CB8_CA1 0x70
    269 #define CB8_CA2 0x71
    270 #define CB8_CA3 0x72
    271 #define CB8_CA4 0x73
    272 #define CB8_CA5 0x74
    273 #define CB8_CA6 0x75
    274 #define CB8_CA7 0x76
    275 #define CB8_CA8 0x77
    276 #define CB8_CA9 0x78
    277 #define CB8_CA10 0x79
    278 #define CB8_CA11 0x7A
    279 #define CB8_CA12 0x7B
    280 #define CB8_CA13 0x7C
    281 #define CB8_CA14 0x7D
    282 #define CB8_CA15 0x7E
    283 #define CB8_CA16 0x7F
    284 
    285 #define CB9_CA1 0x80
    286 #define CB9_CA2 0x81
    287 #define CB9_CA3 0x82
    288 #define CB9_CA4 0x83
    289 #define CB9_CA5 0x84
    290 #define CB9_CA6 0x85
    291 #define CB9_CA7 0x86
    292 #define CB9_CA8 0x87
    293 #define CB9_CA9 0x88
    294 #define CB9_CA10 0x89
    295 #define CB9_CA11 0x8A
    296 #define CB9_CA12 0x8B
    297 #define CB9_CA13 0x8C
    298 #define CB9_CA14 0x8D
    299 #define CB9_CA15 0x8E
    300 #define CB9_CA16 0x8F
    301 
    302 #define CB10_CA1 0x90
    303 #define CB10_CA2 0x91
    304 #define CB10_CA3 0x92
    305 #define CB10_CA4 0x93
    306 #define CB10_CA5 0x94
    307 #define CB10_CA6 0x95
    308 #define CB10_CA7 0x96
    309 #define CB10_CA8 0x97
    310 #define CB10_CA9 0x98
    311 #define CB10_CA10 0x99
    312 #define CB10_CA11 0x9A
    313 #define CB10_CA12 0x9B
    314 #define CB10_CA13 0x9C
    315 #define CB10_CA14 0x9D
    316 #define CB10_CA15 0x9E
    317 #define CB10_CA16 0x9F
    318 
    319 #define CB11_CA1 0xA0
    320 #define CB11_CA2 0xA1
    321 #define CB11_CA3 0xA2
    322 #define CB11_CA4 0xA3
    323 #define CB11_CA5 0xA4
    324 #define CB11_CA6 0xA5
    325 #define CB11_CA7 0xA6
    326 #define CB11_CA8 0xA7
    327 #define CB11_CA9 0xA8
    328 #define CB11_CA10 0xA9
    329 #define CB11_CA11 0xAA
    330 #define CB11_CA12 0xAB
    331 #define CB11_CA13 0xAC
    332 #define CB11_CA14 0xAD
    333 #define CB11_CA15 0xAE
    334 #define CB11_CA16 0xAF
    335 
    336 #define CB12_CA1 0xB0
    337 #define CB12_CA2 0xB1
    338 #define CB12_CA3 0xB2
    339 #define CB12_CA4 0xB3
    340 #define CB12_CA5 0xB4
    341 #define CB12_CA6 0xB5
    342 #define CB12_CA7 0xB6
    343 #define CB12_CA8 0xB7
    344 #define CB12_CA9 0xB8
    345 #define CB12_CA10 0xB9
    346 #define CB12_CA11 0xBA
    347 #define CB12_CA12 0xBB
    348 #define CB12_CA13 0xBC
    349 #define CB12_CA14 0xBD
    350 #define CB12_CA15 0xBE
    351 #define CB12_CA16 0xBF