transport.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. #ifdef RGBLIGHT_ENABLE
  8. # include "rgblight.h"
  9. #endif
  10. #ifdef BACKLIGHT_ENABLE
  11. # include "backlight.h"
  12. extern backlight_config_t backlight_config;
  13. #endif
  14. #ifdef ENCODER_ENABLE
  15. # include "encoder.h"
  16. #endif
  17. #if defined(USE_I2C) || defined(EH)
  18. # include "i2c_master.h"
  19. # include "i2c_slave.h"
  20. typedef struct _I2C_slave_buffer_t {
  21. matrix_row_t smatrix[ROWS_PER_HAND];
  22. uint8_t backlight_level;
  23. #if defined(RGBLIGHT_ENABLE) && defined(RGBLIGHT_SPLIT)
  24. rgblight_syncinfo_t rgblight_sync;
  25. #endif
  26. #ifdef ENCODER_ENABLE
  27. uint8_t encoder_state[NUMBER_OF_ENCODERS];
  28. #endif
  29. } I2C_slave_buffer_t;
  30. static I2C_slave_buffer_t * const i2c_buffer = (I2C_slave_buffer_t *)i2c_slave_reg;
  31. # define I2C_BACKLIGHT_START offsetof(I2C_slave_buffer_t, backlight_level)
  32. # define I2C_RGB_START offsetof(I2C_slave_buffer_t, rgblight_sync)
  33. # define I2C_KEYMAP_START offsetof(I2C_slave_buffer_t, smatrix)
  34. # define I2C_ENCODER_START offsetof(I2C_slave_buffer_t, encoder_state)
  35. # define TIMEOUT 100
  36. # ifndef SLAVE_I2C_ADDRESS
  37. # define SLAVE_I2C_ADDRESS 0x32
  38. # endif
  39. // Get rows from other half over i2c
  40. bool transport_master(matrix_row_t matrix[]) {
  41. i2c_readReg(SLAVE_I2C_ADDRESS, I2C_KEYMAP_START, (void *)matrix, sizeof(i2c_buffer->smatrix), TIMEOUT);
  42. // write backlight info
  43. # ifdef BACKLIGHT_ENABLE
  44. uint8_t level = get_backlight_level();
  45. if (level != i2c_buffer->backlight_level) {
  46. if (i2c_writeReg(SLAVE_I2C_ADDRESS, I2C_BACKLIGHT_START, (void *)&level, sizeof(level), TIMEOUT) >= 0) {
  47. i2c_buffer->backlight_level = level;
  48. }
  49. }
  50. # endif
  51. # if defined(RGBLIGHT_ENABLE) && defined(RGBLIGHT_SPLIT)
  52. if (rgblight_get_change_flags()) {
  53. rgblight_syncinfo_t rgblight_sync;
  54. rgblight_get_syncinfo(&rgblight_sync);
  55. if (i2c_writeReg(SLAVE_I2C_ADDRESS, I2C_RGB_START,
  56. (void *)&rgblight_sync, sizeof(rgblight_sync), TIMEOUT) >= 0) {
  57. rgblight_clear_change_flags();
  58. }
  59. }
  60. # endif
  61. # ifdef ENCODER_ENABLE
  62. i2c_readReg(SLAVE_I2C_ADDRESS, I2C_ENCODER_START, (void *)i2c_buffer->encoder_state, sizeof(I2C_slave_buffer_t.encoder_state), TIMEOUT);
  63. encoder_update_raw(i2c_buffer->encoder_state);
  64. # endif
  65. return true;
  66. }
  67. void transport_slave(matrix_row_t matrix[]) {
  68. // Copy matrix to I2C buffer
  69. memcpy((void*)i2c_buffer->smatrix, (void *)matrix, sizeof(i2c_buffer->smatrix));
  70. // Read Backlight Info
  71. # ifdef BACKLIGHT_ENABLE
  72. backlight_set(i2c_buffer->backlight_level);
  73. # endif
  74. # if defined(RGBLIGHT_ENABLE) && defined(RGBLIGHT_SPLIT)
  75. // Update the RGB with the new data
  76. if (i2c_buffer->rgblight_sync.status.change_flags != 0) {
  77. rgblight_update_sync(&i2c_buffer->rgblight_sync, false);
  78. i2c_buffer->rgblight_sync.status.change_flags = 0;
  79. }
  80. # endif
  81. # ifdef ENCODER_ENABLE
  82. encoder_state_raw(i2c_buffer->encoder_state);
  83. # endif
  84. }
  85. void transport_master_init(void) { i2c_init(); }
  86. void transport_slave_init(void) { i2c_slave_init(SLAVE_I2C_ADDRESS); }
  87. #else // USE_SERIAL
  88. # include "serial.h"
  89. typedef struct _Serial_s2m_buffer_t {
  90. // TODO: if MATRIX_COLS > 8 change to uint8_t packed_matrix[] for pack/unpack
  91. matrix_row_t smatrix[ROWS_PER_HAND];
  92. # ifdef ENCODER_ENABLE
  93. uint8_t encoder_state[NUMBER_OF_ENCODERS];
  94. # endif
  95. } Serial_s2m_buffer_t;
  96. typedef struct _Serial_m2s_buffer_t {
  97. # ifdef BACKLIGHT_ENABLE
  98. uint8_t backlight_level;
  99. # endif
  100. } Serial_m2s_buffer_t;
  101. #if defined(RGBLIGHT_ENABLE) && defined(RGBLIGHT_SPLIT)
  102. // When MCUs on both sides drive their respective RGB LED chains,
  103. // it is necessary to synchronize, so it is necessary to communicate RGB
  104. // information. In that case, define RGBLIGHT_SPLIT with info on the number
  105. // of LEDs on each half.
  106. //
  107. // Otherwise, if the master side MCU drives both sides RGB LED chains,
  108. // there is no need to communicate.
  109. typedef struct _Serial_rgblight_t {
  110. rgblight_syncinfo_t rgblight_sync;
  111. } Serial_rgblight_t;
  112. volatile Serial_rgblight_t serial_rgblight = {};
  113. uint8_t volatile status_rgblight = 0;
  114. #endif
  115. volatile Serial_s2m_buffer_t serial_s2m_buffer = {};
  116. volatile Serial_m2s_buffer_t serial_m2s_buffer = {};
  117. uint8_t volatile status0 = 0;
  118. enum serial_transaction_id {
  119. GET_SLAVE_MATRIX = 0,
  120. #if defined(RGBLIGHT_ENABLE) && defined(RGBLIGHT_SPLIT)
  121. PUT_RGBLIGHT,
  122. #endif
  123. };
  124. SSTD_t transactions[] = {
  125. [GET_SLAVE_MATRIX] = {
  126. (uint8_t *)&status0,
  127. sizeof(serial_m2s_buffer),
  128. (uint8_t *)&serial_m2s_buffer,
  129. sizeof(serial_s2m_buffer),
  130. (uint8_t *)&serial_s2m_buffer,
  131. },
  132. #if defined(RGBLIGHT_ENABLE) && defined(RGBLIGHT_SPLIT)
  133. [PUT_RGBLIGHT] = {
  134. (uint8_t *)&status_rgblight,
  135. sizeof(serial_rgblight),
  136. (uint8_t *)&serial_rgblight,
  137. 0, NULL // no slave to master transfer
  138. },
  139. #endif
  140. };
  141. void transport_master_init(void) { soft_serial_initiator_init(transactions, TID_LIMIT(transactions)); }
  142. void transport_slave_init(void) { soft_serial_target_init(transactions, TID_LIMIT(transactions)); }
  143. #if defined(RGBLIGHT_ENABLE) && defined(RGBLIGHT_SPLIT)
  144. // rgblight synchronization information communication.
  145. void transport_rgblight_master(void) {
  146. if (rgblight_get_change_flags()) {
  147. rgblight_get_syncinfo((rgblight_syncinfo_t *)&serial_rgblight.rgblight_sync);
  148. if (soft_serial_transaction(PUT_RGBLIGHT) == TRANSACTION_END) {
  149. rgblight_clear_change_flags();
  150. }
  151. }
  152. }
  153. void transport_rgblight_slave(void) {
  154. if (status_rgblight == TRANSACTION_ACCEPTED) {
  155. rgblight_update_sync((rgblight_syncinfo_t *)&serial_rgblight.rgblight_sync,
  156. false);
  157. status_rgblight = TRANSACTION_END;
  158. }
  159. }
  160. #else
  161. #define transport_rgblight_master()
  162. #define transport_rgblight_slave()
  163. #endif
  164. bool transport_master(matrix_row_t matrix[]) {
  165. #ifndef SERIAL_USE_MULTI_TRANSACTION
  166. if (soft_serial_transaction() != TRANSACTION_END) {
  167. return false;
  168. }
  169. #else
  170. transport_rgblight_master();
  171. if (soft_serial_transaction(GET_SLAVE_MATRIX) != TRANSACTION_END) {
  172. return false;
  173. }
  174. #endif
  175. // TODO: if MATRIX_COLS > 8 change to unpack()
  176. for (int i = 0; i < ROWS_PER_HAND; ++i) {
  177. matrix[i] = serial_s2m_buffer.smatrix[i];
  178. }
  179. # ifdef BACKLIGHT_ENABLE
  180. // Write backlight level for slave to read
  181. serial_m2s_buffer.backlight_level = backlight_config.enable ? backlight_config.level : 0;
  182. # endif
  183. # ifdef ENCODER_ENABLE
  184. encoder_update_raw((uint8_t *)serial_s2m_buffer.encoder_state);
  185. # endif
  186. return true;
  187. }
  188. void transport_slave(matrix_row_t matrix[]) {
  189. transport_rgblight_slave();
  190. // TODO: if MATRIX_COLS > 8 change to pack()
  191. for (int i = 0; i < ROWS_PER_HAND; ++i) {
  192. serial_s2m_buffer.smatrix[i] = matrix[i];
  193. }
  194. # ifdef BACKLIGHT_ENABLE
  195. backlight_set(serial_m2s_buffer.backlight_level);
  196. # endif
  197. # ifdef ENCODER_ENABLE
  198. encoder_state_raw((uint8_t *)serial_s2m_buffer.encoder_state);
  199. # endif
  200. }
  201. #endif