transport.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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) || defined(EH)
  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,
  57. (void *)&rgblight_sync, sizeof(rgblight_sync), TIMEOUT) >= 0) {
  58. rgblight_clear_change_flags();
  59. }
  60. }
  61. # endif
  62. # ifdef ENCODER_ENABLE
  63. i2c_readReg(SLAVE_I2C_ADDRESS, I2C_ENCODER_START, (void *)i2c_buffer->encoder_state, sizeof(i2c_buffer->encoder_state), TIMEOUT);
  64. encoder_update_raw(i2c_buffer->encoder_state);
  65. # endif
  66. return true;
  67. }
  68. void transport_slave(matrix_row_t matrix[]) {
  69. // Copy matrix to I2C buffer
  70. memcpy((void*)i2c_buffer->smatrix, (void *)matrix, sizeof(i2c_buffer->smatrix));
  71. // Read Backlight Info
  72. # ifdef BACKLIGHT_ENABLE
  73. backlight_set(i2c_buffer->backlight_level);
  74. # endif
  75. # if defined(RGBLIGHT_ENABLE) && defined(RGBLIGHT_SPLIT)
  76. // Update the RGB with the new data
  77. if (i2c_buffer->rgblight_sync.status.change_flags != 0) {
  78. rgblight_update_sync(&i2c_buffer->rgblight_sync, false);
  79. i2c_buffer->rgblight_sync.status.change_flags = 0;
  80. }
  81. # endif
  82. # ifdef ENCODER_ENABLE
  83. encoder_state_raw(i2c_buffer->encoder_state);
  84. # endif
  85. }
  86. void transport_master_init(void) { i2c_init(); }
  87. void transport_slave_init(void) { i2c_slave_init(SLAVE_I2C_ADDRESS); }
  88. #else // USE_SERIAL
  89. # include "serial.h"
  90. typedef struct _Serial_s2m_buffer_t {
  91. // TODO: if MATRIX_COLS > 8 change to uint8_t packed_matrix[] for pack/unpack
  92. matrix_row_t smatrix[ROWS_PER_HAND];
  93. # ifdef ENCODER_ENABLE
  94. uint8_t encoder_state[NUMBER_OF_ENCODERS];
  95. # endif
  96. } Serial_s2m_buffer_t;
  97. typedef struct _Serial_m2s_buffer_t {
  98. # ifdef BACKLIGHT_ENABLE
  99. uint8_t backlight_level;
  100. # endif
  101. } Serial_m2s_buffer_t;
  102. #if defined(RGBLIGHT_ENABLE) && defined(RGBLIGHT_SPLIT)
  103. // When MCUs on both sides drive their respective RGB LED chains,
  104. // it is necessary to synchronize, so it is necessary to communicate RGB
  105. // information. In that case, define RGBLIGHT_SPLIT with info on the number
  106. // of LEDs on each half.
  107. //
  108. // Otherwise, if the master side MCU drives both sides RGB LED chains,
  109. // there is no need to communicate.
  110. typedef struct _Serial_rgblight_t {
  111. rgblight_syncinfo_t rgblight_sync;
  112. } Serial_rgblight_t;
  113. volatile Serial_rgblight_t serial_rgblight = {};
  114. uint8_t volatile status_rgblight = 0;
  115. #endif
  116. volatile Serial_s2m_buffer_t serial_s2m_buffer = {};
  117. volatile Serial_m2s_buffer_t serial_m2s_buffer = {};
  118. uint8_t volatile status0 = 0;
  119. enum serial_transaction_id {
  120. GET_SLAVE_MATRIX = 0,
  121. #if defined(RGBLIGHT_ENABLE) && defined(RGBLIGHT_SPLIT)
  122. PUT_RGBLIGHT,
  123. #endif
  124. };
  125. SSTD_t transactions[] = {
  126. [GET_SLAVE_MATRIX] = {
  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. (uint8_t *)&status_rgblight,
  136. sizeof(serial_rgblight),
  137. (uint8_t *)&serial_rgblight,
  138. 0, NULL // no slave to master transfer
  139. },
  140. #endif
  141. };
  142. void transport_master_init(void) { soft_serial_initiator_init(transactions, TID_LIMIT(transactions)); }
  143. void transport_slave_init(void) { soft_serial_target_init(transactions, TID_LIMIT(transactions)); }
  144. #if defined(RGBLIGHT_ENABLE) && defined(RGBLIGHT_SPLIT)
  145. // rgblight synchronization information communication.
  146. void transport_rgblight_master(void) {
  147. if (rgblight_get_change_flags()) {
  148. rgblight_get_syncinfo((rgblight_syncinfo_t *)&serial_rgblight.rgblight_sync);
  149. if (soft_serial_transaction(PUT_RGBLIGHT) == TRANSACTION_END) {
  150. rgblight_clear_change_flags();
  151. }
  152. }
  153. }
  154. void transport_rgblight_slave(void) {
  155. if (status_rgblight == TRANSACTION_ACCEPTED) {
  156. rgblight_update_sync((rgblight_syncinfo_t *)&serial_rgblight.rgblight_sync,
  157. false);
  158. status_rgblight = TRANSACTION_END;
  159. }
  160. }
  161. #else
  162. #define transport_rgblight_master()
  163. #define transport_rgblight_slave()
  164. #endif
  165. bool transport_master(matrix_row_t matrix[]) {
  166. #ifndef SERIAL_USE_MULTI_TRANSACTION
  167. if (soft_serial_transaction() != TRANSACTION_END) {
  168. return false;
  169. }
  170. #else
  171. transport_rgblight_master();
  172. if (soft_serial_transaction(GET_SLAVE_MATRIX) != TRANSACTION_END) {
  173. return false;
  174. }
  175. #endif
  176. // TODO: if MATRIX_COLS > 8 change to unpack()
  177. for (int i = 0; i < ROWS_PER_HAND; ++i) {
  178. matrix[i] = serial_s2m_buffer.smatrix[i];
  179. }
  180. # ifdef BACKLIGHT_ENABLE
  181. // Write backlight level for slave to read
  182. serial_m2s_buffer.backlight_level = is_backlight_enabled() ? get_backlight_level() : 0;
  183. # endif
  184. # ifdef ENCODER_ENABLE
  185. encoder_update_raw((uint8_t *)serial_s2m_buffer.encoder_state);
  186. # endif
  187. return true;
  188. }
  189. void transport_slave(matrix_row_t matrix[]) {
  190. transport_rgblight_slave();
  191. // TODO: if MATRIX_COLS > 8 change to pack()
  192. for (int i = 0; i < ROWS_PER_HAND; ++i) {
  193. serial_s2m_buffer.smatrix[i] = matrix[i];
  194. }
  195. # ifdef BACKLIGHT_ENABLE
  196. backlight_set(serial_m2s_buffer.backlight_level);
  197. # endif
  198. # ifdef ENCODER_ENABLE
  199. encoder_state_raw((uint8_t *)serial_s2m_buffer.encoder_state);
  200. # endif
  201. }
  202. #endif