transport.c 4.2 KB

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