transport.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. #endif
  13. #ifdef ENCODER_ENABLE
  14. # include "encoder.h"
  15. static pin_t encoders_pad[] = ENCODERS_PAD_A;
  16. # define NUMBER_OF_ENCODERS (sizeof(encoders_pad) / sizeof(pin_t))
  17. #endif
  18. #if defined(USE_I2C)
  19. # include "i2c_master.h"
  20. # include "i2c_slave.h"
  21. typedef struct _I2C_slave_buffer_t {
  22. matrix_row_t smatrix[ROWS_PER_HAND];
  23. uint8_t backlight_level;
  24. # if defined(RGBLIGHT_ENABLE) && defined(RGBLIGHT_SPLIT)
  25. rgblight_syncinfo_t rgblight_sync;
  26. # endif
  27. # ifdef ENCODER_ENABLE
  28. uint8_t encoder_state[NUMBER_OF_ENCODERS];
  29. # endif
  30. } I2C_slave_buffer_t;
  31. static I2C_slave_buffer_t *const i2c_buffer = (I2C_slave_buffer_t *)i2c_slave_reg;
  32. # define I2C_BACKLIGHT_START offsetof(I2C_slave_buffer_t, backlight_level)
  33. # define I2C_RGB_START offsetof(I2C_slave_buffer_t, rgblight_sync)
  34. # define I2C_KEYMAP_START offsetof(I2C_slave_buffer_t, smatrix)
  35. # define I2C_ENCODER_START offsetof(I2C_slave_buffer_t, encoder_state)
  36. # define TIMEOUT 100
  37. # ifndef SLAVE_I2C_ADDRESS
  38. # define SLAVE_I2C_ADDRESS 0x32
  39. # endif
  40. // Get rows from other half over i2c
  41. bool transport_master(matrix_row_t matrix[]) {
  42. i2c_readReg(SLAVE_I2C_ADDRESS, I2C_KEYMAP_START, (void *)matrix, sizeof(i2c_buffer->smatrix), TIMEOUT);
  43. // write backlight info
  44. # ifdef BACKLIGHT_ENABLE
  45. uint8_t level = is_backlight_enabled() ? get_backlight_level() : 0;
  46. if (level != i2c_buffer->backlight_level) {
  47. if (i2c_writeReg(SLAVE_I2C_ADDRESS, I2C_BACKLIGHT_START, (void *)&level, sizeof(level), TIMEOUT) >= 0) {
  48. i2c_buffer->backlight_level = level;
  49. }
  50. }
  51. # endif
  52. # if defined(RGBLIGHT_ENABLE) && defined(RGBLIGHT_SPLIT)
  53. if (rgblight_get_change_flags()) {
  54. rgblight_syncinfo_t rgblight_sync;
  55. rgblight_get_syncinfo(&rgblight_sync);
  56. if (i2c_writeReg(SLAVE_I2C_ADDRESS, I2C_RGB_START, (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_buffer->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. {
  127. (uint8_t *)&status0,
  128. sizeof(serial_m2s_buffer),
  129. (uint8_t *)&serial_m2s_buffer,
  130. sizeof(serial_s2m_buffer),
  131. (uint8_t *)&serial_s2m_buffer,
  132. },
  133. # if defined(RGBLIGHT_ENABLE) && defined(RGBLIGHT_SPLIT)
  134. [PUT_RGBLIGHT] =
  135. {
  136. (uint8_t *)&status_rgblight, sizeof(serial_rgblight), (uint8_t *)&serial_rgblight, 0, NULL // no slave to master transfer
  137. },
  138. # endif
  139. };
  140. void transport_master_init(void) { soft_serial_initiator_init(transactions, TID_LIMIT(transactions)); }
  141. void transport_slave_init(void) { soft_serial_target_init(transactions, TID_LIMIT(transactions)); }
  142. # if defined(RGBLIGHT_ENABLE) && defined(RGBLIGHT_SPLIT)
  143. // rgblight synchronization information communication.
  144. void transport_rgblight_master(void) {
  145. if (rgblight_get_change_flags()) {
  146. rgblight_get_syncinfo((rgblight_syncinfo_t *)&serial_rgblight.rgblight_sync);
  147. if (soft_serial_transaction(PUT_RGBLIGHT) == TRANSACTION_END) {
  148. rgblight_clear_change_flags();
  149. }
  150. }
  151. }
  152. void transport_rgblight_slave(void) {
  153. if (status_rgblight == TRANSACTION_ACCEPTED) {
  154. rgblight_update_sync((rgblight_syncinfo_t *)&serial_rgblight.rgblight_sync, false);
  155. status_rgblight = TRANSACTION_END;
  156. }
  157. }
  158. # else
  159. # define transport_rgblight_master()
  160. # define transport_rgblight_slave()
  161. # endif
  162. bool transport_master(matrix_row_t matrix[]) {
  163. # ifndef SERIAL_USE_MULTI_TRANSACTION
  164. if (soft_serial_transaction() != TRANSACTION_END) {
  165. return false;
  166. }
  167. # else
  168. transport_rgblight_master();
  169. if (soft_serial_transaction(GET_SLAVE_MATRIX) != TRANSACTION_END) {
  170. return false;
  171. }
  172. # endif
  173. // TODO: if MATRIX_COLS > 8 change to unpack()
  174. for (int i = 0; i < ROWS_PER_HAND; ++i) {
  175. matrix[i] = serial_s2m_buffer.smatrix[i];
  176. }
  177. # ifdef BACKLIGHT_ENABLE
  178. // Write backlight level for slave to read
  179. serial_m2s_buffer.backlight_level = is_backlight_enabled() ? get_backlight_level() : 0;
  180. # endif
  181. # ifdef ENCODER_ENABLE
  182. encoder_update_raw((uint8_t *)serial_s2m_buffer.encoder_state);
  183. # endif
  184. return true;
  185. }
  186. void transport_slave(matrix_row_t matrix[]) {
  187. transport_rgblight_slave();
  188. // TODO: if MATRIX_COLS > 8 change to pack()
  189. for (int i = 0; i < ROWS_PER_HAND; ++i) {
  190. serial_s2m_buffer.smatrix[i] = matrix[i];
  191. }
  192. # ifdef BACKLIGHT_ENABLE
  193. backlight_set(serial_m2s_buffer.backlight_level);
  194. # endif
  195. # ifdef ENCODER_ENABLE
  196. encoder_state_raw((uint8_t *)serial_s2m_buffer.encoder_state);
  197. # endif
  198. }
  199. #endif