transport.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. #include <string.h>
  2. #include <stddef.h>
  3. #include "config.h"
  4. #include "matrix.h"
  5. #include "quantum.h"
  6. #define ROWS_PER_HAND (MATRIX_ROWS / 2)
  7. #define SYNC_TIMER_OFFSET 2
  8. #ifdef RGBLIGHT_ENABLE
  9. # include "rgblight.h"
  10. #endif
  11. #ifdef BACKLIGHT_ENABLE
  12. # include "backlight.h"
  13. #endif
  14. #ifdef ENCODER_ENABLE
  15. # include "encoder.h"
  16. static pin_t encoders_pad[] = ENCODERS_PAD_A;
  17. # define NUMBER_OF_ENCODERS (sizeof(encoders_pad) / sizeof(pin_t))
  18. #endif
  19. #if defined(RGB_MATRIX_ENABLE) && defined(RGB_MATRIX_SPLIT)
  20. # include "rgb_matrix.h"
  21. #endif
  22. #if defined(USE_I2C)
  23. # include "i2c_master.h"
  24. # include "i2c_slave.h"
  25. typedef struct _I2C_slave_buffer_t {
  26. # ifndef DISABLE_SYNC_TIMER
  27. uint32_t sync_timer;
  28. # endif
  29. # ifdef SPLIT_TRANSPORT_MIRROR
  30. matrix_row_t mmatrix[ROWS_PER_HAND];
  31. # endif
  32. matrix_row_t smatrix[ROWS_PER_HAND];
  33. # ifdef SPLIT_MODS_ENABLE
  34. uint8_t real_mods;
  35. uint8_t weak_mods;
  36. # ifndef NO_ACTION_ONESHOT
  37. uint8_t oneshot_mods;
  38. # endif
  39. # endif
  40. # ifdef BACKLIGHT_ENABLE
  41. uint8_t backlight_level;
  42. # endif
  43. # if defined(RGBLIGHT_ENABLE) && defined(RGBLIGHT_SPLIT)
  44. rgblight_syncinfo_t rgblight_sync;
  45. # endif
  46. # ifdef ENCODER_ENABLE
  47. uint8_t encoder_state[NUMBER_OF_ENCODERS];
  48. # endif
  49. # ifdef WPM_ENABLE
  50. uint8_t current_wpm;
  51. # endif
  52. # if defined(RGB_MATRIX_ENABLE) && defined(RGB_MATRIX_SPLIT)
  53. rgb_config_t rgb_matrix;
  54. bool rgb_suspend_state;
  55. # endif
  56. } I2C_slave_buffer_t;
  57. static I2C_slave_buffer_t *const i2c_buffer = (I2C_slave_buffer_t *)i2c_slave_reg;
  58. # define I2C_SYNC_TIME_START offsetof(I2C_slave_buffer_t, sync_timer)
  59. # define I2C_KEYMAP_MASTER_START offsetof(I2C_slave_buffer_t, mmatrix)
  60. # define I2C_KEYMAP_SLAVE_START offsetof(I2C_slave_buffer_t, smatrix)
  61. # define I2C_REAL_MODS_START offsetof(I2C_slave_buffer_t, real_mods)
  62. # define I2C_WEAK_MODS_START offsetof(I2C_slave_buffer_t, weak_mods)
  63. # define I2C_ONESHOT_MODS_START offsetof(I2C_slave_buffer_t, oneshot_mods)
  64. # define I2C_BACKLIGHT_START offsetof(I2C_slave_buffer_t, backlight_level)
  65. # define I2C_RGB_START offsetof(I2C_slave_buffer_t, rgblight_sync)
  66. # define I2C_ENCODER_START offsetof(I2C_slave_buffer_t, encoder_state)
  67. # define I2C_WPM_START offsetof(I2C_slave_buffer_t, current_wpm)
  68. # define I2C_RGB_MATRIX_START offsetof(I2C_slave_buffer_t, rgb_matrix)
  69. # define I2C_RGB_SUSPEND_START offsetof(I2C_slave_buffer_t, rgb_suspend_state)
  70. # define TIMEOUT 100
  71. # ifndef SLAVE_I2C_ADDRESS
  72. # define SLAVE_I2C_ADDRESS 0x32
  73. # endif
  74. // Get rows from other half over i2c
  75. bool transport_master(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
  76. i2c_readReg(SLAVE_I2C_ADDRESS, I2C_KEYMAP_SLAVE_START, (void *)slave_matrix, sizeof(i2c_buffer->smatrix), TIMEOUT);
  77. # ifdef SPLIT_TRANSPORT_MIRROR
  78. i2c_writeReg(SLAVE_I2C_ADDRESS, I2C_KEYMAP_MASTER_START, (void *)master_matrix, sizeof(i2c_buffer->mmatrix), TIMEOUT);
  79. # endif
  80. // write backlight info
  81. # ifdef BACKLIGHT_ENABLE
  82. uint8_t level = is_backlight_enabled() ? get_backlight_level() : 0;
  83. if (level != i2c_buffer->backlight_level) {
  84. if (i2c_writeReg(SLAVE_I2C_ADDRESS, I2C_BACKLIGHT_START, (void *)&level, sizeof(level), TIMEOUT) >= 0) {
  85. i2c_buffer->backlight_level = level;
  86. }
  87. }
  88. # endif
  89. # if defined(RGBLIGHT_ENABLE) && defined(RGBLIGHT_SPLIT)
  90. if (rgblight_get_change_flags()) {
  91. rgblight_syncinfo_t rgblight_sync;
  92. rgblight_get_syncinfo(&rgblight_sync);
  93. if (i2c_writeReg(SLAVE_I2C_ADDRESS, I2C_RGB_START, (void *)&rgblight_sync, sizeof(rgblight_sync), TIMEOUT) >= 0) {
  94. rgblight_clear_change_flags();
  95. }
  96. }
  97. # endif
  98. # ifdef ENCODER_ENABLE
  99. i2c_readReg(SLAVE_I2C_ADDRESS, I2C_ENCODER_START, (void *)i2c_buffer->encoder_state, sizeof(i2c_buffer->encoder_state), TIMEOUT);
  100. encoder_update_raw(i2c_buffer->encoder_state);
  101. # endif
  102. # ifdef WPM_ENABLE
  103. uint8_t current_wpm = get_current_wpm();
  104. if (current_wpm != i2c_buffer->current_wpm) {
  105. if (i2c_writeReg(SLAVE_I2C_ADDRESS, I2C_WPM_START, (void *)&current_wpm, sizeof(current_wpm), TIMEOUT) >= 0) {
  106. i2c_buffer->current_wpm = current_wpm;
  107. }
  108. }
  109. # endif
  110. # ifdef SPLIT_MODS_ENABLE
  111. uint8_t real_mods = get_mods();
  112. if (real_mods != i2c_buffer->real_mods) {
  113. if (i2c_writeReg(SLAVE_I2C_ADDRESS, I2C_REAL_MODS_START, (void *)&real_mods, sizeof(real_mods), TIMEOUT) >= 0) {
  114. i2c_buffer->real_mods = real_mods;
  115. }
  116. }
  117. uint8_t weak_mods = get_weak_mods();
  118. if (weak_mods != i2c_buffer->weak_mods) {
  119. if (i2c_writeReg(SLAVE_I2C_ADDRESS, I2C_WEAK_MODS_START, (void *)&weak_mods, sizeof(weak_mods), TIMEOUT) >= 0) {
  120. i2c_buffer->weak_mods = weak_mods;
  121. }
  122. }
  123. # ifndef NO_ACTION_ONESHOT
  124. uint8_t oneshot_mods = get_oneshot_mods();
  125. if (oneshot_mods != i2c_buffer->oneshot_mods) {
  126. if (i2c_writeReg(SLAVE_I2C_ADDRESS, I2C_ONESHOT_MODS_START, (void *)&oneshot_mods, sizeof(oneshot_mods), TIMEOUT) >= 0) {
  127. i2c_buffer->oneshot_mods = oneshot_mods;
  128. }
  129. }
  130. # endif
  131. # endif
  132. # if defined(RGB_MATRIX_ENABLE) && defined(RGB_MATRIX_SPLIT)
  133. i2c_writeReg(SLAVE_I2C_ADDRESS, I2C_RGB_MATRIX_START, (void *)rgb_matrix_config, sizeof(i2c_buffer->rgb_matrix), TIMEOUT);
  134. i2c_writeReg(SLAVE_I2C_ADDRESS, I2C_RGB_SUSPEND_START, (void *)g_suspend_state, sizeof(i2c_buffer->rgb_suspend_state), TIMEOUT);
  135. # endif
  136. # ifndef DISABLE_SYNC_TIMER
  137. i2c_buffer->sync_timer = sync_timer_read32() + SYNC_TIMER_OFFSET;
  138. i2c_writeReg(SLAVE_I2C_ADDRESS, I2C_SYNC_TIME_START, (void *)&i2c_buffer->sync_timer, sizeof(i2c_buffer->sync_timer), TIMEOUT);
  139. # endif
  140. return true;
  141. }
  142. void transport_slave(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
  143. # ifndef DISABLE_SYNC_TIMER
  144. sync_timer_update(i2c_buffer->sync_timer);
  145. # endif
  146. // Copy matrix to I2C buffer
  147. memcpy((void *)i2c_buffer->smatrix, (void *)slave_matrix, sizeof(i2c_buffer->smatrix));
  148. # ifdef SPLIT_TRANSPORT_MIRROR
  149. memcpy((void *)master_matrix, (void *)i2c_buffer->mmatrix, sizeof(i2c_buffer->mmatrix));
  150. # endif
  151. // Read Backlight Info
  152. # ifdef BACKLIGHT_ENABLE
  153. backlight_set(i2c_buffer->backlight_level);
  154. # endif
  155. # if defined(RGBLIGHT_ENABLE) && defined(RGBLIGHT_SPLIT)
  156. // Update the RGB with the new data
  157. if (i2c_buffer->rgblight_sync.status.change_flags != 0) {
  158. rgblight_update_sync(&i2c_buffer->rgblight_sync, false);
  159. i2c_buffer->rgblight_sync.status.change_flags = 0;
  160. }
  161. # endif
  162. # ifdef ENCODER_ENABLE
  163. encoder_state_raw(i2c_buffer->encoder_state);
  164. # endif
  165. # ifdef WPM_ENABLE
  166. set_current_wpm(i2c_buffer->current_wpm);
  167. # endif
  168. # ifdef SPLIT_MODS_ENABLE
  169. set_mods(i2c_buffer->real_mods);
  170. set_weak_mods(i2c_buffer->weak_mods);
  171. # ifndef NO_ACTION_ONESHOT
  172. set_oneshot_mods(i2c_buffer->oneshot_mods);
  173. # endif
  174. # endif
  175. # if defined(RGB_MATRIX_ENABLE) && defined(RGB_MATRIX_SPLIT)
  176. memcpy((void *)i2c_buffer->rgb_matrix, (void *)rgb_matrix_config, sizeof(i2c_buffer->rgb_matrix));
  177. memcpy((void *)i2c_buffer->rgb_suspend_state, (void *)g_suspend_state, sizeof(i2c_buffer->rgb_suspend_state));
  178. # endif
  179. }
  180. void transport_master_init(void) { i2c_init(); }
  181. void transport_slave_init(void) { i2c_slave_init(SLAVE_I2C_ADDRESS); }
  182. #else // USE_SERIAL
  183. # include "serial.h"
  184. typedef struct _Serial_s2m_buffer_t {
  185. // TODO: if MATRIX_COLS > 8 change to uint8_t packed_matrix[] for pack/unpack
  186. matrix_row_t smatrix[ROWS_PER_HAND];
  187. # ifdef ENCODER_ENABLE
  188. uint8_t encoder_state[NUMBER_OF_ENCODERS];
  189. # endif
  190. } Serial_s2m_buffer_t;
  191. typedef struct _Serial_m2s_buffer_t {
  192. # ifdef SPLIT_MODS_ENABLE
  193. uint8_t real_mods;
  194. uint8_t weak_mods;
  195. # ifndef NO_ACTION_ONESHOT
  196. uint8_t oneshot_mods;
  197. # endif
  198. # endif
  199. # ifndef DISABLE_SYNC_TIMER
  200. uint32_t sync_timer;
  201. # endif
  202. # ifdef SPLIT_TRANSPORT_MIRROR
  203. matrix_row_t mmatrix[ROWS_PER_HAND];
  204. # endif
  205. # ifdef BACKLIGHT_ENABLE
  206. uint8_t backlight_level;
  207. # endif
  208. # ifdef WPM_ENABLE
  209. uint8_t current_wpm;
  210. # endif
  211. # if defined(RGB_MATRIX_ENABLE) && defined(RGB_MATRIX_SPLIT)
  212. rgb_config_t rgb_matrix;
  213. bool rgb_suspend_state;
  214. # endif
  215. } Serial_m2s_buffer_t;
  216. # if defined(RGBLIGHT_ENABLE) && defined(RGBLIGHT_SPLIT)
  217. // When MCUs on both sides drive their respective RGB LED chains,
  218. // it is necessary to synchronize, so it is necessary to communicate RGB
  219. // information. In that case, define RGBLIGHT_SPLIT with info on the number
  220. // of LEDs on each half.
  221. //
  222. // Otherwise, if the master side MCU drives both sides RGB LED chains,
  223. // there is no need to communicate.
  224. typedef struct _Serial_rgblight_t {
  225. rgblight_syncinfo_t rgblight_sync;
  226. } Serial_rgblight_t;
  227. volatile Serial_rgblight_t serial_rgblight = {};
  228. uint8_t volatile status_rgblight = 0;
  229. # endif
  230. volatile Serial_s2m_buffer_t serial_s2m_buffer = {};
  231. volatile Serial_m2s_buffer_t serial_m2s_buffer = {};
  232. uint8_t volatile status0 = 0;
  233. enum serial_transaction_id {
  234. GET_SLAVE_MATRIX = 0,
  235. # if defined(RGBLIGHT_ENABLE) && defined(RGBLIGHT_SPLIT)
  236. PUT_RGBLIGHT,
  237. # endif
  238. };
  239. SSTD_t transactions[] = {
  240. [GET_SLAVE_MATRIX] =
  241. {
  242. (uint8_t *)&status0,
  243. sizeof(serial_m2s_buffer),
  244. (uint8_t *)&serial_m2s_buffer,
  245. sizeof(serial_s2m_buffer),
  246. (uint8_t *)&serial_s2m_buffer,
  247. },
  248. # if defined(RGBLIGHT_ENABLE) && defined(RGBLIGHT_SPLIT)
  249. [PUT_RGBLIGHT] =
  250. {
  251. (uint8_t *)&status_rgblight, sizeof(serial_rgblight), (uint8_t *)&serial_rgblight, 0, NULL // no slave to master transfer
  252. },
  253. # endif
  254. };
  255. void transport_master_init(void) { soft_serial_initiator_init(transactions, TID_LIMIT(transactions)); }
  256. void transport_slave_init(void) { soft_serial_target_init(transactions, TID_LIMIT(transactions)); }
  257. # if defined(RGBLIGHT_ENABLE) && defined(RGBLIGHT_SPLIT)
  258. // rgblight synchronization information communication.
  259. void transport_rgblight_master(void) {
  260. if (rgblight_get_change_flags()) {
  261. rgblight_get_syncinfo((rgblight_syncinfo_t *)&serial_rgblight.rgblight_sync);
  262. if (soft_serial_transaction(PUT_RGBLIGHT) == TRANSACTION_END) {
  263. rgblight_clear_change_flags();
  264. }
  265. }
  266. }
  267. void transport_rgblight_slave(void) {
  268. if (status_rgblight == TRANSACTION_ACCEPTED) {
  269. rgblight_update_sync((rgblight_syncinfo_t *)&serial_rgblight.rgblight_sync, false);
  270. status_rgblight = TRANSACTION_END;
  271. }
  272. }
  273. # else
  274. # define transport_rgblight_master()
  275. # define transport_rgblight_slave()
  276. # endif
  277. bool transport_master(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
  278. # ifndef SERIAL_USE_MULTI_TRANSACTION
  279. if (soft_serial_transaction() != TRANSACTION_END) {
  280. return false;
  281. }
  282. # else
  283. transport_rgblight_master();
  284. if (soft_serial_transaction(GET_SLAVE_MATRIX) != TRANSACTION_END) {
  285. return false;
  286. }
  287. # endif
  288. // TODO: if MATRIX_COLS > 8 change to unpack()
  289. for (int i = 0; i < ROWS_PER_HAND; ++i) {
  290. slave_matrix[i] = serial_s2m_buffer.smatrix[i];
  291. # ifdef SPLIT_TRANSPORT_MIRROR
  292. serial_m2s_buffer.mmatrix[i] = master_matrix[i];
  293. # endif
  294. }
  295. # ifdef BACKLIGHT_ENABLE
  296. // Write backlight level for slave to read
  297. serial_m2s_buffer.backlight_level = is_backlight_enabled() ? get_backlight_level() : 0;
  298. # endif
  299. # ifdef ENCODER_ENABLE
  300. encoder_update_raw((uint8_t *)serial_s2m_buffer.encoder_state);
  301. # endif
  302. # ifdef WPM_ENABLE
  303. // Write wpm to slave
  304. serial_m2s_buffer.current_wpm = get_current_wpm();
  305. # endif
  306. # ifdef SPLIT_MODS_ENABLE
  307. serial_m2s_buffer.real_mods = get_mods();
  308. serial_m2s_buffer.weak_mods = get_weak_mods();
  309. # ifndef NO_ACTION_ONESHOT
  310. serial_m2s_buffer.oneshot_mods = get_oneshot_mods();
  311. # endif
  312. # endif
  313. # if defined(RGB_MATRIX_ENABLE) && defined(RGB_MATRIX_SPLIT)
  314. serial_m2s_buffer.rgb_matrix = rgb_matrix_config;
  315. serial_m2s_buffer.rgb_suspend_state = g_suspend_state;
  316. # endif
  317. # ifndef DISABLE_SYNC_TIMER
  318. serial_m2s_buffer.sync_timer = sync_timer_read32() + SYNC_TIMER_OFFSET;
  319. # endif
  320. return true;
  321. }
  322. void transport_slave(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
  323. transport_rgblight_slave();
  324. # ifndef DISABLE_SYNC_TIMER
  325. sync_timer_update(serial_m2s_buffer.sync_timer);
  326. # endif
  327. // TODO: if MATRIX_COLS > 8 change to pack()
  328. for (int i = 0; i < ROWS_PER_HAND; ++i) {
  329. serial_s2m_buffer.smatrix[i] = slave_matrix[i];
  330. # ifdef SPLIT_TRANSPORT_MIRROR
  331. master_matrix[i] = serial_m2s_buffer.mmatrix[i];
  332. # endif
  333. }
  334. # ifdef BACKLIGHT_ENABLE
  335. backlight_set(serial_m2s_buffer.backlight_level);
  336. # endif
  337. # ifdef ENCODER_ENABLE
  338. encoder_state_raw((uint8_t *)serial_s2m_buffer.encoder_state);
  339. # endif
  340. # ifdef WPM_ENABLE
  341. set_current_wpm(serial_m2s_buffer.current_wpm);
  342. # endif
  343. # ifdef SPLIT_MODS_ENABLE
  344. set_mods(serial_m2s_buffer.real_mods);
  345. set_weak_mods(serial_m2s_buffer.weak_mods);
  346. # ifndef NO_ACTION_ONESHOT
  347. set_oneshot_mods(serial_m2s_buffer.oneshot_mods);
  348. # endif
  349. # endif
  350. # if defined(RGB_MATRIX_ENABLE) && defined(RGB_MATRIX_SPLIT)
  351. rgb_matrix_config = serial_m2s_buffer.rgb_matrix;
  352. g_suspend_state = serial_m2s_buffer.rgb_suspend_state;
  353. # endif
  354. }
  355. #endif