logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

rev_0100.c (5870B)


  1. /* Copyright 2023 ArthurCyy <https://github.com/ArthurCyy>
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "rev_0100.h"
  17. #include "usb_main.h"
  18. #include "usb_util.h"
  19. #define LOOP_10HZ_PERIOD 100
  20. deferred_token loop10hz_token = INVALID_DEFERRED_TOKEN;
  21. uint32_t loop_10Hz(uint32_t trigger_time, void *cb_arg);
  22. static const SerialConfig mwproto_uart_config = {
  23. .speed = MWPROTO_BITRATE,
  24. };
  25. static void POWER_EnterSleep(void) {
  26. /* Clear Wake-up flag */
  27. PWR->CR |= PWR_CR_CWUF | PWR_CR_CSBF;
  28. /* Select Sleep mode */
  29. /* PWR->CR |= PWR_CR_PDDS | PWR_CR_LPDS; */
  30. /* Set SLEEPDEEP bit of Cortex System Control Register */
  31. SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
  32. /* Request Wait For Interrupt */
  33. __WFI();
  34. /* Reset SLEEPDEEP bit of Cortex System Control Register */
  35. SCB->SCR &= ~SCB_SCR_SLEEPDEEP_Msk;
  36. NVIC_SystemReset();
  37. }
  38. extern void ws2812_poweron(void);
  39. extern void ws2812_poweroff(void);
  40. static bool p_setup = false;
  41. static bool s_init = false;
  42. void ws2812_poweron(void) {
  43. if(p_setup) return;
  44. p_setup = true;
  45. s_init = false;
  46. gpio_set_pin_output(RGB_EN_PIN);
  47. gpio_write_pin_high(RGB_EN_PIN);
  48. }
  49. void ws2812_poweroff(void) {
  50. if(!p_setup) return;
  51. p_setup = false;
  52. gpio_set_pin_input_low(WS2812_DI_PIN);
  53. gpio_write_pin_low(RGB_EN_PIN);
  54. }
  55. void keyboard_pre_init_kb() {
  56. keyboard_pre_init_user();
  57. gpio_set_pin_input_low(MWPROTO_STATUS_PIN);
  58. gpio_set_pin_output(MWPROTO_WAKEUP_PIN);
  59. gpio_write_pin_low(MWPROTO_WAKEUP_PIN);
  60. wait_ms(2);
  61. gpio_write_pin_high(MWPROTO_WAKEUP_PIN);
  62. palSetLineMode(MWPROTO_TX_PIN, PAL_MODE_ALTERNATE(MWPROTO_TX_PAL_MODE) | PAL_OUTPUT_TYPE_OPENDRAIN);
  63. sdStart(&MWPROTO_DRIVER, &mwproto_uart_config);
  64. }
  65. void keyboard_post_init_kb(void) {
  66. keyboard_post_init_user();
  67. print(/* clang-format off */
  68. "\n<--QMK Keyboard-->\n"
  69. "Vendor: " STR(MANUFACTURER) "(" STR(VENDOR_ID) ")\n"
  70. "Product: " STR(PRODUCT) " (" STR(PRODUCT_ID) ")\n"
  71. "Version: " STR(DEVICE_VER) "\n"
  72. "BUILD: " __DATE__ "\n"
  73. ); /* clang-format on */
  74. gpio_write_pin_low(MWPROTO_WAKEUP_PIN);
  75. wait_ms(50);
  76. sdPutI(&MWPROTO_DRIVER, 0xA5);
  77. sdPutI(&MWPROTO_DRIVER, 0x12);
  78. sdPutI(&MWPROTO_DRIVER, 0x01);
  79. sdPutI(&MWPROTO_DRIVER, 0x02);
  80. sdPutI(&MWPROTO_DRIVER, 0xB4);
  81. gpio_write_pin_high(MWPROTO_WAKEUP_PIN);
  82. ws2812_poweron();
  83. loop10hz_token = defer_exec(LOOP_10HZ_PERIOD, loop_10Hz, NULL);
  84. }
  85. bool shutdown_kb(bool jump_to_bootloader) {
  86. if (shutdown_user(jump_to_bootloader)) {
  87. #ifdef RGB_MATRIX_ENABLE
  88. rgb_matrix_set_suspend_state(true);
  89. #endif // RGB_MATRIX_ENABLE
  90. wait_ms(10);
  91. }
  92. ws2812_poweroff();
  93. return true;
  94. }
  95. #ifdef DIP_SWITCH_ENABLE
  96. bool dip_switch_update_mask_kb(uint32_t state) {
  97. if (!dip_switch_update_mask_user(state)) { return false; }
  98. if(state & 0x01) {
  99. led_suspend();
  100. usbDisconnectBus(&USB_DRIVER);
  101. usbStop(&USB_DRIVER);
  102. shutdown_user(true);
  103. gpio_set_pin_input_high(POWER_SWITCH_PIN);
  104. palEnableLineEvent(POWER_SWITCH_PIN, PAL_EVENT_MODE_RISING_EDGE);
  105. POWER_EnterSleep();
  106. }
  107. return true;
  108. }
  109. #endif
  110. uint32_t loop_10Hz(uint32_t trigger_time, void *cb_arg) {
  111. if(last_input_activity_elapsed() > 1000) {
  112. static uint32_t pmu_timer = 0;
  113. if(timer_elapsed32(pmu_timer) > 3000) {
  114. pmu_timer = timer_read32();
  115. gpio_write_pin_low(MWPROTO_WAKEUP_PIN);
  116. if(gpio_read_pin(MWPROTO_STATUS_PIN))
  117. wait_us(500);
  118. else
  119. wait_us(1500);
  120. sdPutI(&MWPROTO_DRIVER, 0xA5);
  121. sdPutI(&MWPROTO_DRIVER, 0x28);
  122. sdPutI(&MWPROTO_DRIVER, 0x00);
  123. sdPutI(&MWPROTO_DRIVER, 0x8D);
  124. gpio_write_pin_high(MWPROTO_WAKEUP_PIN);
  125. }
  126. }
  127. extern matrix_row_t matrix[MATRIX_ROWS];
  128. static uint32_t restore_tick = 0;
  129. if(matrix[0] == 0x4000 && matrix[1] == 0 &&
  130. matrix[2] == 0 && matrix[3] == 0 && matrix[4] == 0 && matrix[5] == 0x201) {
  131. if(restore_tick++ > 50) {
  132. restore_tick = 0;
  133. gpio_write_pin_low(MWPROTO_WAKEUP_PIN);
  134. if(gpio_read_pin(MWPROTO_STATUS_PIN))
  135. wait_us(500);
  136. else
  137. wait_us(1500);
  138. sdPutI(&MWPROTO_DRIVER, 0xA5);
  139. sdPutI(&MWPROTO_DRIVER, 0x1F);
  140. sdPutI(&MWPROTO_DRIVER, 0x01);
  141. sdPutI(&MWPROTO_DRIVER, 0x0F);
  142. sdPutI(&MWPROTO_DRIVER, 0xB4);
  143. gpio_write_pin_high(MWPROTO_WAKEUP_PIN);
  144. wait_ms(50);
  145. eeconfig_init();
  146. #ifdef RGB_MATRIX_ENABLE
  147. for(int i = 0; i < 5; i++) {
  148. rgb_matrix_set_color_all(RGB_WHITE);
  149. rgb_matrix_update_pwm_buffers();
  150. wait_ms(500);
  151. rgb_matrix_set_color_all(RGB_OFF);
  152. rgb_matrix_update_pwm_buffers();
  153. wait_ms(500);
  154. }
  155. #endif
  156. wait_ms(500);
  157. soft_reset_keyboard();
  158. }
  159. } else {
  160. restore_tick = 0;
  161. }
  162. static uint32_t debug_tick = 0;
  163. if (debug_tick++ > 9 ) {
  164. dprintf("trigger %d\n", trigger_time);
  165. debug_tick = 0;
  166. }
  167. return LOOP_10HZ_PERIOD;
  168. }