transport.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #include <string.h>
  2. #include "config.h"
  3. #include "matrix.h"
  4. #include "quantum.h"
  5. #define ROWS_PER_HAND (MATRIX_ROWS / 2)
  6. #ifdef RGBLIGHT_ENABLE
  7. # include "rgblight.h"
  8. #endif
  9. #ifdef BACKLIGHT_ENABLE
  10. # include "backlight.h"
  11. extern backlight_config_t backlight_config;
  12. #endif
  13. #if defined(USE_I2C) || defined(EH)
  14. # include "i2c_master.h"
  15. # include "i2c_slave.h"
  16. # define I2C_BACKLIT_START 0x00
  17. // Need 4 bytes for RGB (32 bit)
  18. # define I2C_RGB_START 0x01
  19. # define I2C_KEYMAP_START 0x05
  20. # define TIMEOUT 100
  21. # ifndef SLAVE_I2C_ADDRESS
  22. # define SLAVE_I2C_ADDRESS 0x32
  23. # endif
  24. // Get rows from other half over i2c
  25. bool transport_master(matrix_row_t matrix[]) {
  26. i2c_readReg(SLAVE_I2C_ADDRESS, I2C_KEYMAP_START, (void *)matrix, ROWS_PER_HAND * sizeof(matrix_row_t), TIMEOUT);
  27. // write backlight info
  28. # ifdef BACKLIGHT_ENABLE
  29. static uint8_t prev_level = ~0;
  30. uint8_t level = get_backlight_level();
  31. if (level != prev_level) {
  32. if (i2c_writeReg(SLAVE_I2C_ADDRESS, I2C_BACKLIT_START, (void *)&level, sizeof(level), TIMEOUT) >= 0) {
  33. prev_level = level;
  34. }
  35. }
  36. # endif
  37. # ifdef RGBLIGHT_ENABLE
  38. static uint32_t prev_rgb = ~0;
  39. uint32_t rgb = rgblight_read_dword();
  40. if (rgb != prev_rgb) {
  41. if (i2c_writeReg(SLAVE_I2C_ADDRESS, I2C_RGB_START, (void *)&rgb, sizeof(rgb), TIMEOUT) >= 0) {
  42. prev_rgb = rgb;
  43. }
  44. }
  45. # endif
  46. return true;
  47. }
  48. void transport_slave(matrix_row_t matrix[]) {
  49. // Copy matrix to I2C buffer
  50. memcpy((void*)(i2c_slave_reg + I2C_KEYMAP_START), (void *)matrix, ROWS_PER_HAND * sizeof(matrix_row_t) );
  51. // Read Backlight Info
  52. # ifdef BACKLIGHT_ENABLE
  53. backlight_set(i2c_slave_reg[I2C_BACKLIT_START]);
  54. # endif
  55. # ifdef RGBLIGHT_ENABLE
  56. uint32_t rgb = *(uint32_t *)(i2c_slave_reg + I2C_RGB_START);
  57. // Update the RGB with the new data
  58. rgblight_update_dword(rgb);
  59. # endif
  60. }
  61. void transport_master_init(void) { i2c_init(); }
  62. void transport_slave_init(void) { i2c_slave_init(SLAVE_I2C_ADDRESS); }
  63. #else // USE_SERIAL
  64. # include "serial.h"
  65. typedef struct _Serial_s2m_buffer_t {
  66. // TODO: if MATRIX_COLS > 8 change to uint8_t packed_matrix[] for pack/unpack
  67. matrix_row_t smatrix[ROWS_PER_HAND];
  68. } Serial_s2m_buffer_t;
  69. typedef struct _Serial_m2s_buffer_t {
  70. # ifdef BACKLIGHT_ENABLE
  71. uint8_t backlight_level;
  72. # endif
  73. # if defined(RGBLIGHT_ENABLE) && defined(RGBLED_SPLIT)
  74. rgblight_config_t rgblight_config; // not yet use
  75. //
  76. // When MCUs on both sides drive their respective RGB LED chains,
  77. // it is necessary to synchronize, so it is necessary to communicate RGB
  78. // information. In that case, define RGBLED_SPLIT with info on the number
  79. // of LEDs on each half.
  80. //
  81. // Otherwise, if the master side MCU drives both sides RGB LED chains,
  82. // there is no need to communicate.
  83. # endif
  84. } Serial_m2s_buffer_t;
  85. volatile Serial_s2m_buffer_t serial_s2m_buffer = {};
  86. volatile Serial_m2s_buffer_t serial_m2s_buffer = {};
  87. uint8_t volatile status0 = 0;
  88. SSTD_t transactions[] = {
  89. {
  90. (uint8_t *)&status0,
  91. sizeof(serial_m2s_buffer),
  92. (uint8_t *)&serial_m2s_buffer,
  93. sizeof(serial_s2m_buffer),
  94. (uint8_t *)&serial_s2m_buffer,
  95. },
  96. };
  97. void transport_master_init(void) { soft_serial_initiator_init(transactions, TID_LIMIT(transactions)); }
  98. void transport_slave_init(void) { soft_serial_target_init(transactions, TID_LIMIT(transactions)); }
  99. bool transport_master(matrix_row_t matrix[]) {
  100. if (soft_serial_transaction()) {
  101. return false;
  102. }
  103. // TODO: if MATRIX_COLS > 8 change to unpack()
  104. for (int i = 0; i < ROWS_PER_HAND; ++i) {
  105. matrix[i] = serial_s2m_buffer.smatrix[i];
  106. }
  107. # ifdef BACKLIGHT_ENABLE
  108. // Write backlight level for slave to read
  109. serial_m2s_buffer.backlight_level = backlight_config.enable ? backlight_config.level : 0;
  110. # endif
  111. # if defined(RGBLIGHT_ENABLE) && defined(RGBLED_SPLIT)
  112. static rgblight_config_t prev_rgb = {~0};
  113. uint32_t rgb = rgblight_read_dword();
  114. if (rgb != prev_rgb.raw) {
  115. serial_m2s_buffer.rgblight_config.raw = rgb;
  116. prev_rgb.raw = rgb;
  117. }
  118. # endif
  119. return true;
  120. }
  121. void transport_slave(matrix_row_t matrix[]) {
  122. // TODO: if MATRIX_COLS > 8 change to pack()
  123. for (int i = 0; i < ROWS_PER_HAND; ++i) {
  124. serial_s2m_buffer.smatrix[i] = matrix[i];
  125. }
  126. # ifdef BACKLIGHT_ENABLE
  127. backlight_set(serial_m2s_buffer.backlight_level);
  128. # endif
  129. # if defined(RGBLIGHT_ENABLE) && defined(RGBLED_SPLIT)
  130. // Update RGB config with the new data
  131. rgblight_update_dword(serial_m2s_buffer.rgblight_config.raw);
  132. # endif
  133. }
  134. #endif