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.

389 lines
8.9 KiB

  1. /*
  2. Copyright 2019 Basic I/O Instruments(Scott Wei) <scot.wei@gmail.com>
  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. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include <avr/pgmspace.h>
  15. #include <util/delay.h>
  16. #include "report.h"
  17. #include "host.h"
  18. #include "host_driver.h"
  19. #include "keyboard.h"
  20. #include "action.h"
  21. #include "led.h"
  22. #include "sendchar.h"
  23. #include "debug.h"
  24. #include "print.h"
  25. #ifdef SLEEP_LED_ENABLE
  26. #include "sleep_led.h"
  27. #endif
  28. #include "suspend.h"
  29. #include "usb_descriptor.h"
  30. #include "lufa.h"
  31. #include "quantum.h"
  32. #include <util/atomic.h>
  33. #include "outputselect.h"
  34. #ifdef NKRO_ENABLE
  35. #include "keycode_config.h"
  36. extern keymap_config_t keymap_config;
  37. #endif
  38. #ifdef AUDIO_ENABLE
  39. #include <audio.h>
  40. #endif
  41. #ifdef BLUETOOTH_ENABLE
  42. #ifdef MODULE_ADAFRUIT_BLE
  43. #include "adafruit_ble.h"
  44. #else
  45. #include "bluetooth.h"
  46. #endif
  47. #endif
  48. #ifdef VIRTSER_ENABLE
  49. #include "virtser.h"
  50. #endif
  51. #if (defined(RGB_MIDI) | defined(RGBLIGHT_ANIMATIONS)) & defined(RGBLIGHT_ENABLE)
  52. #include "rgblight.h"
  53. #endif
  54. #ifdef MIDI_ENABLE
  55. #include "qmk_midi.h"
  56. #endif
  57. #ifdef RAW_ENABLE
  58. #include "raw_hid.h"
  59. #endif
  60. #include "ble.h"
  61. #include "usart.h"
  62. #include <avr/power.h>
  63. #include <avr/sleep.h>
  64. bool force_usb = false; //Reserved for FORCE USB Mode function.
  65. bool force_ble = false; //Reserved for FORCE USB Mode function.
  66. bool usb_connected = false;
  67. bool ble_enabled = false;
  68. uint32_t kb_idle_timer = 0;
  69. bool usb_state_sent = false;
  70. uint8_t USB_DeviceLastState = 0;
  71. #ifdef RAW_ENABLE
  72. /** \brief Raw HID Task
  73. *
  74. * FIXME: Needs doc
  75. */
  76. static void raw_hid_task(void)
  77. {
  78. // Create a temporary buffer to hold the read in data from the host
  79. uint8_t data[RAW_EPSIZE];
  80. bool data_read = false;
  81. // Device must be connected and configured for the task to run
  82. if (USB_DeviceState != DEVICE_STATE_Configured)
  83. return;
  84. Endpoint_SelectEndpoint(RAW_OUT_EPNUM);
  85. // Check to see if a packet has been sent from the host
  86. if (Endpoint_IsOUTReceived())
  87. {
  88. // Check to see if the packet contains data
  89. if (Endpoint_IsReadWriteAllowed())
  90. {
  91. /* Read data */
  92. Endpoint_Read_Stream_LE(data, sizeof(data), NULL);
  93. data_read = true;
  94. }
  95. // Finalize the stream transfer to receive the last packet
  96. Endpoint_ClearOUT();
  97. if (data_read)
  98. {
  99. raw_hid_receive(data, sizeof(data));
  100. }
  101. }
  102. }
  103. #endif
  104. static void setup_mcu(void)
  105. {
  106. /* Disable watchdog if enabled by bootloader/fuses */
  107. MCUSR &= ~(1 << WDRF);
  108. wdt_disable();
  109. CLKPR = (1 << CLKPCE);
  110. CLKPR = (0 << CLKPS3) | (0 << CLKPS2) | (0 << CLKPS1) | (0 << CLKPS0);
  111. }
  112. static void setup_usb(void)
  113. {
  114. // Leonardo needs. Without this USB device is not recognized.
  115. USB_Disable();
  116. USB_Init();
  117. // for Console_Task
  118. USB_Device_EnableSOFEvents();
  119. print_set_sendchar(sendchar);
  120. }
  121. void power_saving(void)
  122. {
  123. power_adc_disable();
  124. power_usart0_disable();
  125. power_spi_disable();
  126. power_twi_disable();
  127. USBCON |= (1 << FRZCLK); // Freeze the USB Clock
  128. PLLCSR &= ~(1 << PLLE); // Disable the USB Clock (PPL)
  129. USBCON &= ~(1 << USBE);
  130. }
  131. void power_recover(void)
  132. {
  133. USBCON |= (1 << USBE);
  134. PLLCSR |= (1 << PLLE); // Resume the USB Clock (PPL)
  135. USBCON &= ~(1 << FRZCLK); // Resume the USB Clock
  136. power_adc_enable();
  137. power_usart0_enable();
  138. power_spi_enable();
  139. power_twi_enable();
  140. }
  141. void ble_task_init(void)
  142. {
  143. kb_idle_timer = timer_read32(); //Mark current time, reserved for further usage;
  144. }
  145. void ble_task(void)
  146. {
  147. if (USB_DeviceLastState != USB_DeviceState)
  148. {
  149. usb_state_sent = false;
  150. #ifdef BLE_DEBUG
  151. send_str(PSTR("USB State Changed\r\n"));
  152. if (USB_DeviceState == DEVICE_STATE_Unattached)
  153. {
  154. send_str(PSTR("USB State Unattached\r\n"));
  155. }
  156. #endif
  157. if (USB_DeviceState == DEVICE_STATE_Powered)
  158. {
  159. #ifdef BLE_DEBUG
  160. send_str(PSTR("USB State Powered\r\n"));
  161. #endif
  162. power_recover();
  163. host_set_driver(&null_driver);
  164. }
  165. #ifdef BLE_DEBUG
  166. if ((USB_DeviceState == DEVICE_STATE_Default))
  167. {
  168. send_str(PSTR("USB State Default\r\n"));
  169. }
  170. if ((USB_DeviceState == DEVICE_STATE_Addressed))
  171. {
  172. send_str(PSTR("USB State Addressed\r\n"));
  173. }
  174. if (USB_DeviceState == DEVICE_STATE_Configured)
  175. {
  176. send_str(PSTR("USB State Configured\r\n"));
  177. }
  178. if (USB_DeviceState > DEVICE_STATE_Unattached)
  179. {
  180. }
  181. else
  182. {
  183. //
  184. }
  185. #endif
  186. }
  187. else
  188. {
  189. #ifdef BLE_DEBUG
  190. if (!usb_state_sent)
  191. {
  192. if (USB_DeviceState == DEVICE_STATE_Unattached)
  193. {
  194. send_str(PSTR("USB State Stopped at Unattached\r\n"));
  195. }
  196. if (USB_DeviceState == DEVICE_STATE_Powered)
  197. {
  198. send_str(PSTR("USB State Stopped at Powered\r\n"));
  199. }
  200. if ((USB_DeviceState == DEVICE_STATE_Default))
  201. {
  202. send_str(PSTR("USB State Stopped at Default\r\n"));
  203. }
  204. if ((USB_DeviceState == DEVICE_STATE_Addressed))
  205. {
  206. send_str(PSTR("USB State Stopped at Addressed\r\n"));
  207. }
  208. if (USB_DeviceState == DEVICE_STATE_Configured)
  209. {
  210. send_str(PSTR("USB State Stopped at Configured\r\n"));
  211. }
  212. }
  213. #endif
  214. if (USB_DeviceState == DEVICE_STATE_Unattached)
  215. {
  216. if (host_get_driver() && host_get_driver() != &bluefruit_driver)
  217. {
  218. #ifdef BLE_DEBUG
  219. send_str(PSTR("USB State stopped at Unattached\r\n"));
  220. #endif
  221. ble_task_init();
  222. force_usb = 0;
  223. usb_connected = 0;
  224. //Reinit USB to prepare for next connection.
  225. USB_Init();
  226. USB_Detach();
  227. USB_Attach();
  228. #ifdef BLE_DEBUG
  229. send_str(PSTR("Loading &bluefruit_driver\r\n"));
  230. #endif
  231. host_set_driver(&bluefruit_driver);
  232. clear_keyboard();
  233. power_saving();
  234. }
  235. else
  236. {
  237. //Do nothing if USB is unattached and the driver is &bluefruit_driver
  238. }
  239. }
  240. if (USB_DeviceState == DEVICE_STATE_Configured)
  241. {
  242. if (host_get_driver() && host_get_driver() != &lufa_driver)
  243. {
  244. #ifdef BLE_DEBUG
  245. send_str(PSTR("USB State stopped at Configured\r\n"));
  246. #endif
  247. power_recover();
  248. usb_connected = 1;
  249. ble_enabled = 0;
  250. #ifdef BLE_DEBUG
  251. send_str(PSTR("Loading &lufa_driver\r\n"));
  252. #endif
  253. host_set_driver(&lufa_driver);
  254. clear_keyboard();
  255. }
  256. else
  257. {
  258. //Do nothing if the driver is &lufa_driver
  259. }
  260. }
  261. usb_state_sent = true;
  262. }
  263. USB_DeviceLastState = USB_DeviceState;
  264. }
  265. // Use a custom main() function because the task logic is different from the common one.
  266. int main(void)
  267. {
  268. #ifdef MIDI_ENABLE
  269. setup_midi();
  270. #endif
  271. setup_mcu();
  272. keyboard_setup();
  273. setup_usb();
  274. sei();
  275. #if defined(MODULE_ADAFRUIT_EZKEY) || defined(MODULE_RN42)
  276. serial_init();
  277. #endif
  278. /* wait for USB startup to get ready for debug output */
  279. uint8_t timeout = 255; // timeout when USB is not available(Bluetooth)
  280. while (timeout-- && USB_DeviceState != DEVICE_STATE_Configured)
  281. {
  282. wait_ms(4);
  283. #if defined(INTERRUPT_CONTROL_ENDPOINT)
  284. ;
  285. #else
  286. USB_USBTask();
  287. #endif
  288. }
  289. print("\nUSB init\n");
  290. keyboard_init();
  291. host_set_driver(&lufa_driver);
  292. backlight_disable();
  293. //host_set_driver(&lufa_driver);
  294. print("Keyboard initialized.\n");
  295. //Init Hardware UART
  296. usart_init();
  297. #ifdef BLE_DEBUG
  298. send_str(PSTR("Keyboard has been setup up\r\n"));
  299. if (usb_connected)
  300. {
  301. send_str(PSTR("usb_connected=1\r\n"));
  302. }
  303. else
  304. {
  305. send_str(PSTR("usb_connected=0\r\n"));
  306. }
  307. #endif
  308. #ifdef SLEEP_LED_ENABLE
  309. sleep_led_init();
  310. #endif
  311. #ifdef VIRTSER_ENABLE
  312. virtser_init();
  313. #endif
  314. while (1)
  315. {
  316. ble_task();
  317. keyboard_task();
  318. #ifdef RAW_ENABLE
  319. raw_hid_task();
  320. #endif
  321. #if defined(RGBLIGHT_ANIMATIONS) && defined(RGBLIGHT_ENABLE)
  322. rgblight_task();
  323. #endif
  324. #if !defined(INTERRUPT_CONTROL_ENDPOINT)
  325. USB_USBTask();
  326. #endif
  327. }
  328. }