]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/split_common/transport.c
8d408f6fdc6b0344bffdb7e19e798d86bfb0635a
[qmk_firmware.git] / quantum / split_common / transport.c
1 #include <string.h>
2
3 #include "config.h"
4 #include "matrix.h"
5 #include "quantum.h"
6
7 #define ROWS_PER_HAND (MATRIX_ROWS / 2)
8
9 #ifdef RGBLIGHT_ENABLE
10 #  include "rgblight.h"
11 #endif
12
13 #ifdef BACKLIGHT_ENABLE
14 #  include "backlight.h"
15 extern backlight_config_t backlight_config;
16 #endif
17
18 #if defined(USE_I2C) || defined(EH)
19
20 #  include "i2c_master.h"
21 #  include "i2c_slave.h"
22
23 #  define I2C_BACKLIT_START 0x00
24 // Need 4 bytes for RGB (32 bit)
25 #  define I2C_RGB_START 0x01
26 #  define I2C_KEYMAP_START 0x05
27
28 #  define TIMEOUT 100
29
30 #  ifndef SLAVE_I2C_ADDRESS
31 #    define SLAVE_I2C_ADDRESS 0x32
32 #  endif
33
34 // Get rows from other half over i2c
35 bool transport_master(matrix_row_t matrix[]) {
36   i2c_readReg(SLAVE_I2C_ADDRESS, I2C_KEYMAP_START, (void *)matrix, ROWS_PER_HAND * sizeof(matrix_row_t), TIMEOUT);
37
38   // write backlight info
39 #  ifdef BACKLIGHT_ENABLE
40   static uint8_t prev_level = ~0;
41   uint8_t        level      = get_backlight_level();
42   if (level != prev_level) {
43     if (i2c_writeReg(SLAVE_I2C_ADDRESS, I2C_BACKLIT_START, (void *)&level, sizeof(level), TIMEOUT) >= 0) {
44       prev_level = level;
45     }
46   }
47 #  endif
48
49 #  ifdef RGBLIGHT_ENABLE
50   static uint32_t prev_rgb = ~0;
51   uint32_t        rgb      = rgblight_read_dword();
52   if (rgb != prev_rgb) {
53     if (i2c_writeReg(SLAVE_I2C_ADDRESS, I2C_RGB_START, (void *)&rgb, sizeof(rgb), TIMEOUT) >= 0) {
54       prev_rgb = rgb;
55     }
56   }
57 #  endif
58
59   return true;
60 }
61
62 void transport_slave(matrix_row_t matrix[]) {
63   // Copy matrix to I2C buffer
64   memcpy((void*)(i2c_slave_reg + I2C_KEYMAP_START), (void *)matrix, ROWS_PER_HAND * sizeof(matrix_row_t) );
65
66 // Read Backlight Info
67 #  ifdef BACKLIGHT_ENABLE
68   backlight_set(i2c_slave_reg[I2C_BACKLIT_START]);
69 #  endif
70
71 #  ifdef RGBLIGHT_ENABLE
72   uint32_t rgb = *(uint32_t *)(i2c_slave_reg + I2C_RGB_START);
73   // Update the RGB with the new data
74   rgblight_update_dword(rgb);
75 #  endif
76 }
77
78 void transport_master_init(void) { i2c_init(); }
79
80 void transport_slave_init(void) { i2c_slave_init(SLAVE_I2C_ADDRESS); }
81
82 #else  // USE_SERIAL
83
84 #  include "serial.h"
85
86 typedef struct _Serial_s2m_buffer_t {
87   // TODO: if MATRIX_COLS > 8 change to uint8_t packed_matrix[] for pack/unpack
88   matrix_row_t smatrix[ROWS_PER_HAND];
89 } Serial_s2m_buffer_t;
90
91 typedef struct _Serial_m2s_buffer_t {
92 #  ifdef BACKLIGHT_ENABLE
93   uint8_t           backlight_level;
94 #  endif
95 #  if defined(RGBLIGHT_ENABLE) && defined(RGBLED_SPLIT)
96   rgblight_config_t rgblight_config;  // not yet use
97   //
98   // When MCUs on both sides drive their respective RGB LED chains,
99   // it is necessary to synchronize, so it is necessary to communicate RGB
100   // information. In that case, define RGBLED_SPLIT with info on the number
101   // of LEDs on each half.
102   //
103   // Otherwise, if the master side MCU drives both sides RGB LED chains,
104   // there is no need to communicate.
105 #  endif
106 } Serial_m2s_buffer_t;
107
108 volatile Serial_s2m_buffer_t serial_s2m_buffer = {};
109 volatile Serial_m2s_buffer_t serial_m2s_buffer = {};
110 uint8_t volatile status0                       = 0;
111
112 SSTD_t transactions[] = {
113     {
114         (uint8_t *)&status0,
115         sizeof(serial_m2s_buffer),
116         (uint8_t *)&serial_m2s_buffer,
117         sizeof(serial_s2m_buffer),
118         (uint8_t *)&serial_s2m_buffer,
119     },
120 };
121
122 void transport_master_init(void) { soft_serial_initiator_init(transactions, TID_LIMIT(transactions)); }
123
124 void transport_slave_init(void) { soft_serial_target_init(transactions, TID_LIMIT(transactions)); }
125
126 bool transport_master(matrix_row_t matrix[]) {
127   if (soft_serial_transaction()) {
128     return false;
129   }
130
131   // TODO:  if MATRIX_COLS > 8 change to unpack()
132   for (int i = 0; i < ROWS_PER_HAND; ++i) {
133     matrix[i] = serial_s2m_buffer.smatrix[i];
134   }
135
136 #  ifdef BACKLIGHT_ENABLE
137   // Write backlight level for slave to read
138   serial_m2s_buffer.backlight_level = backlight_config.enable ? backlight_config.level : 0;
139 #  endif
140
141 #  if defined(RGBLIGHT_ENABLE) && defined(RGBLED_SPLIT)
142   static rgblight_config_t prev_rgb = {~0};
143   uint32_t rgb = rgblight_read_dword();
144   if (rgb != prev_rgb.raw) {
145     serial_m2s_buffer.rgblight_config.raw = rgb;
146     prev_rgb.raw = rgb;
147   }
148 #  endif
149
150   return true;
151 }
152
153 void transport_slave(matrix_row_t matrix[]) {
154   // TODO: if MATRIX_COLS > 8 change to pack()
155   for (int i = 0; i < ROWS_PER_HAND; ++i) {
156     serial_s2m_buffer.smatrix[i] = matrix[i];
157   }
158 #  ifdef BACKLIGHT_ENABLE
159   backlight_set(serial_m2s_buffer.backlight_level);
160 #  endif
161 #  if defined(RGBLIGHT_ENABLE) && defined(RGBLED_SPLIT)
162   // Update RGB config with the new data
163   rgblight_update_dword(serial_m2s_buffer.rgblight_config.raw);
164 #  endif
165 }
166
167 #endif