transport.c 11 KB

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