transport.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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(RGBLIGHT_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 the RGBLIGHT_SPLIT macro.
  79. //
  80. // Otherwise, if the master side MCU drives both sides RGB LED chains,
  81. // there is no need to communicate.
  82. # endif
  83. } Serial_m2s_buffer_t;
  84. volatile Serial_s2m_buffer_t serial_s2m_buffer = {};
  85. volatile Serial_m2s_buffer_t serial_m2s_buffer = {};
  86. uint8_t volatile status0 = 0;
  87. SSTD_t transactions[] = {
  88. {
  89. (uint8_t *)&status0,
  90. sizeof(serial_m2s_buffer),
  91. (uint8_t *)&serial_m2s_buffer,
  92. sizeof(serial_s2m_buffer),
  93. (uint8_t *)&serial_s2m_buffer,
  94. },
  95. };
  96. void transport_master_init(void) { soft_serial_initiator_init(transactions, TID_LIMIT(transactions)); }
  97. void transport_slave_init(void) { soft_serial_target_init(transactions, TID_LIMIT(transactions)); }
  98. bool transport_master(matrix_row_t matrix[]) {
  99. if (soft_serial_transaction()) {
  100. return false;
  101. }
  102. // TODO: if MATRIX_COLS > 8 change to unpack()
  103. for (int i = 0; i < ROWS_PER_HAND; ++i) {
  104. matrix[i] = serial_s2m_buffer.smatrix[i];
  105. }
  106. # if defined(RGBLIGHT_ENABLE) && defined(RGBLIGHT_SPLIT)
  107. // Code to send RGB over serial goes here (not implemented yet)
  108. # endif
  109. # ifdef BACKLIGHT_ENABLE
  110. // Write backlight level for slave to read
  111. serial_m2s_buffer.backlight_level = backlight_config.enable ? backlight_config.level : 0;
  112. # endif
  113. return true;
  114. }
  115. void transport_slave(matrix_row_t matrix[]) {
  116. // TODO: if MATRIX_COLS > 8 change to pack()
  117. for (int i = 0; i < ROWS_PER_HAND; ++i) {
  118. serial_s2m_buffer.smatrix[i] = matrix[i];
  119. }
  120. # ifdef BACKLIGHT_ENABLE
  121. backlight_set(serial_m2s_buffer.backlight_level);
  122. # endif
  123. # if defined(RGBLIGHT_ENABLE) && defined(RGBLIGHT_SPLIT)
  124. // Add serial implementation for RGB here
  125. # endif
  126. }
  127. #endif