main.c 8.8 KB

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