transport.c 12 KB

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