You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

250 lines
7.9 KiB

  1. /*
  2. The MIT License (MIT)
  3. Copyright (c) 2016 Fred Sundvik
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.
  19. */
  20. #include "report.h"
  21. #include "host_driver.h"
  22. #include "serial_link/system/serial_link.h"
  23. #include <hal.h>
  24. #include "serial_link/protocol/byte_stuffer.h"
  25. #include "serial_link/protocol/transport.h"
  26. #include "serial_link/protocol/frame_router.h"
  27. #include "matrix.h"
  28. #include "sync_timer.h"
  29. #include <stdbool.h>
  30. #include "print.h"
  31. #include "config.h"
  32. #define SYNC_TIMER_OFFSET 2
  33. static event_source_t new_data_event;
  34. static bool serial_link_connected;
  35. static bool is_master = false;
  36. static uint8_t keyboard_leds(void);
  37. static void send_keyboard(report_keyboard_t* report);
  38. static void send_mouse(report_mouse_t* report);
  39. static void send_system(uint16_t data);
  40. static void send_consumer(uint16_t data);
  41. host_driver_t serial_driver = {keyboard_leds, send_keyboard, send_mouse, send_system, send_consumer};
  42. // Define these in your Config.h file
  43. #ifndef SERIAL_LINK_BAUD
  44. # error "Serial link baud is not set"
  45. #endif
  46. #ifndef SERIAL_LINK_THREAD_PRIORITY
  47. # error "Serial link thread priority not set"
  48. #endif
  49. static SerialConfig config = {.sc_speed = SERIAL_LINK_BAUD};
  50. //#define DEBUG_LINK_ERRORS
  51. static uint32_t read_from_serial(SerialDriver* driver, uint8_t link) {
  52. const uint32_t buffer_size = 16;
  53. uint8_t buffer[buffer_size];
  54. uint32_t bytes_read = sdAsynchronousRead(driver, buffer, buffer_size);
  55. uint8_t* current = buffer;
  56. uint8_t* end = current + bytes_read;
  57. while (current < end) {
  58. byte_stuffer_recv_byte(link, *current);
  59. current++;
  60. }
  61. return bytes_read;
  62. }
  63. static void print_error(char* str, eventflags_t flags, SerialDriver* driver) {
  64. #ifdef DEBUG_LINK_ERRORS
  65. if (flags & SD_PARITY_ERROR) {
  66. print(str);
  67. print(" Parity error\n");
  68. }
  69. if (flags & SD_FRAMING_ERROR) {
  70. print(str);
  71. print(" Framing error\n");
  72. }
  73. if (flags & SD_OVERRUN_ERROR) {
  74. print(str);
  75. uint32_t size = qSpaceI(&(driver->iqueue));
  76. xprintf(" Overrun error, queue size %d\n", size);
  77. }
  78. if (flags & SD_NOISE_ERROR) {
  79. print(str);
  80. print(" Noise error\n");
  81. }
  82. if (flags & SD_BREAK_DETECTED) {
  83. print(str);
  84. print(" Break detected\n");
  85. }
  86. #else
  87. (void)str;
  88. (void)flags;
  89. (void)driver;
  90. #endif
  91. }
  92. bool is_serial_link_master(void) { return is_master; }
  93. // TODO: Optimize the stack size, this is probably way too big
  94. static THD_WORKING_AREA(serialThreadStack, 1024);
  95. static THD_FUNCTION(serialThread, arg) {
  96. (void)arg;
  97. event_listener_t new_data_listener;
  98. event_listener_t sd1_listener;
  99. event_listener_t sd2_listener;
  100. chEvtRegister(&new_data_event, &new_data_listener, 0);
  101. eventflags_t events = CHN_INPUT_AVAILABLE | SD_PARITY_ERROR | SD_FRAMING_ERROR | SD_OVERRUN_ERROR | SD_NOISE_ERROR | SD_BREAK_DETECTED;
  102. chEvtRegisterMaskWithFlags(chnGetEventSource(&SD1), &sd1_listener, EVENT_MASK(1), events);
  103. chEvtRegisterMaskWithFlags(chnGetEventSource(&SD2), &sd2_listener, EVENT_MASK(2), events);
  104. bool need_wait = false;
  105. while (true) {
  106. eventflags_t flags1 = 0;
  107. eventflags_t flags2 = 0;
  108. if (need_wait) {
  109. eventmask_t mask = chEvtWaitAnyTimeout(ALL_EVENTS, TIME_MS2I(1000));
  110. if (mask & EVENT_MASK(1)) {
  111. flags1 = chEvtGetAndClearFlags(&sd1_listener);
  112. print_error("DOWNLINK", flags1, &SD1);
  113. }
  114. if (mask & EVENT_MASK(2)) {
  115. flags2 = chEvtGetAndClearFlags(&sd2_listener);
  116. print_error("UPLINK", flags2, &SD2);
  117. }
  118. }
  119. // Always stay as master, even if the USB goes into sleep mode
  120. is_master |= usbGetDriverStateI(&USBD1) == USB_ACTIVE;
  121. router_set_master(is_master);
  122. need_wait = true;
  123. need_wait &= read_from_serial(&SD2, UP_LINK) == 0;
  124. need_wait &= read_from_serial(&SD1, DOWN_LINK) == 0;
  125. update_transport();
  126. }
  127. }
  128. void send_data(uint8_t link, const uint8_t* data, uint16_t size) {
  129. if (link == DOWN_LINK) {
  130. sdWrite(&SD1, data, size);
  131. } else {
  132. sdWrite(&SD2, data, size);
  133. }
  134. }
  135. static systime_t last_update = 0;
  136. typedef struct {
  137. matrix_row_t rows[MATRIX_ROWS];
  138. } matrix_object_t;
  139. static matrix_object_t last_matrix = {};
  140. SLAVE_TO_MASTER_OBJECT(keyboard_matrix, matrix_object_t);
  141. MASTER_TO_ALL_SLAVES_OBJECT(serial_link_connected, bool);
  142. #ifndef DISABLE_SYNC_TIMER
  143. MASTER_TO_ALL_SLAVES_OBJECT(sync_timer, uint32_t);
  144. #endif
  145. static remote_object_t* remote_objects[] = {
  146. REMOTE_OBJECT(serial_link_connected),
  147. REMOTE_OBJECT(keyboard_matrix),
  148. #ifndef DISABLE_SYNC_TIMER
  149. REMOTE_OBJECT(sync_timer),
  150. #endif
  151. };
  152. void init_serial_link(void) {
  153. serial_link_connected = false;
  154. init_serial_link_hal();
  155. add_remote_objects(remote_objects, sizeof(remote_objects) / sizeof(remote_object_t*));
  156. init_byte_stuffer();
  157. sdStart(&SD1, &config);
  158. sdStart(&SD2, &config);
  159. chEvtObjectInit(&new_data_event);
  160. (void)chThdCreateStatic(serialThreadStack, sizeof(serialThreadStack), SERIAL_LINK_THREAD_PRIORITY, serialThread, NULL);
  161. }
  162. void matrix_set_remote(matrix_row_t* rows, uint8_t index);
  163. void serial_link_update(void) {
  164. if (read_serial_link_connected()) {
  165. serial_link_connected = true;
  166. }
  167. matrix_object_t matrix;
  168. bool changed = false;
  169. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  170. matrix.rows[i] = matrix_get_row(i);
  171. changed |= matrix.rows[i] != last_matrix.rows[i];
  172. }
  173. systime_t current_time = chVTGetSystemTimeX();
  174. systime_t delta = current_time - last_update;
  175. if (changed || delta > TIME_US2I(5000)) {
  176. last_update = current_time;
  177. last_matrix = matrix;
  178. matrix_object_t* m = begin_write_keyboard_matrix();
  179. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  180. m->rows[i] = matrix.rows[i];
  181. }
  182. end_write_keyboard_matrix();
  183. *begin_write_serial_link_connected() = true;
  184. end_write_serial_link_connected();
  185. #ifndef DISABLE_SYNC_TIMER
  186. *begin_write_sync_timer() = sync_timer_read32() + SYNC_TIMER_OFFSET;
  187. end_write_sync_timer();
  188. #endif
  189. }
  190. matrix_object_t* m = read_keyboard_matrix(0);
  191. if (m) {
  192. matrix_set_remote(m->rows, 0);
  193. }
  194. #ifndef DISABLE_SYNC_TIMER
  195. uint32_t* t = read_sync_timer();
  196. if (t) {
  197. sync_timer_update(*t);
  198. }
  199. #endif
  200. }
  201. void signal_data_written(void) { chEvtBroadcast(&new_data_event); }
  202. bool is_serial_link_connected(void) { return serial_link_connected; }
  203. host_driver_t* get_serial_link_driver(void) { return &serial_driver; }
  204. // NOTE: The driver does nothing, because the master handles everything
  205. uint8_t keyboard_leds(void) { return 0; }
  206. void send_keyboard(report_keyboard_t* report) { (void)report; }
  207. void send_mouse(report_mouse_t* report) { (void)report; }
  208. void send_system(uint16_t data) { (void)data; }
  209. void send_consumer(uint16_t data) { (void)data; }