main.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /* Name: main.c
  2. * Project: hid-mouse, a very simple HID example
  3. * Author: Christian Starkjohann
  4. * Creation Date: 2008-04-07
  5. * Tabsize: 4
  6. * Copyright: (c) 2008 by OBJECTIVE DEVELOPMENT Software GmbH
  7. * License: GNU GPL v2 (see License.txt), GNU GPL v3 or proprietary (CommercialLicense.txt)
  8. * This Revision: $Id: main.c 790 2010-05-30 21:00:26Z cs $
  9. */
  10. #include <stdint.h>
  11. #include <avr/interrupt.h>
  12. #include <avr/wdt.h>
  13. #include <avr/sleep.h>
  14. #include <util/delay.h>
  15. #include "usbdrv.h"
  16. #include "oddebug.h"
  17. #include "vusb.h"
  18. #include "keyboard.h"
  19. #include "host.h"
  20. #include "timer.h"
  21. #include "uart.h"
  22. #include "debug.h"
  23. #include "rgblight_reconfig.h"
  24. #if (defined(RGB_MIDI) | defined(RGBLIGHT_ANIMATIONS)) & defined(RGBLIGHT_ENABLE)
  25. # include "rgblight.h"
  26. #endif
  27. #define UART_BAUD_RATE 115200
  28. /* This is from main.c of USBaspLoader */
  29. static void initForUsbConnectivity(void) {
  30. uint8_t i = 0;
  31. usbInit();
  32. /* enforce USB re-enumerate: */
  33. usbDeviceDisconnect(); /* do this while interrupts are disabled */
  34. while (--i) { /* fake USB disconnect for > 250 ms */
  35. wdt_reset();
  36. _delay_ms(1);
  37. }
  38. usbDeviceConnect();
  39. sei();
  40. }
  41. int main(void) {
  42. bool suspended = false;
  43. #if USB_COUNT_SOF
  44. uint16_t last_timer = timer_read();
  45. #endif
  46. #ifdef CLKPR
  47. // avoid unintentional changes of clock frequency in devices that have a
  48. // clock prescaler
  49. CLKPR = 0x80, CLKPR = 0;
  50. #endif
  51. #ifndef NO_UART
  52. uart_init(UART_BAUD_RATE);
  53. #endif
  54. keyboard_setup();
  55. keyboard_init();
  56. host_set_driver(vusb_driver());
  57. debug("initForUsbConnectivity()\n");
  58. initForUsbConnectivity();
  59. debug("main loop\n");
  60. while (1) {
  61. #if USB_COUNT_SOF
  62. if (usbSofCount != 0) {
  63. suspended = false;
  64. usbSofCount = 0;
  65. last_timer = timer_read();
  66. } else {
  67. // Suspend when no SOF in 3ms-10ms(7.1.7.4 Suspending of USB1.1)
  68. if (timer_elapsed(last_timer) > 5) {
  69. suspended = true;
  70. /*
  71. uart_putchar('S');
  72. _delay_ms(1);
  73. cli();
  74. set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  75. sleep_enable();
  76. sleep_bod_disable();
  77. sei();
  78. sleep_cpu();
  79. sleep_disable();
  80. _delay_ms(10);
  81. uart_putchar('W');
  82. */
  83. }
  84. }
  85. #endif
  86. if (!suspended) {
  87. usbPoll();
  88. // TODO: configuration process is incosistent. it sometime fails.
  89. // To prevent failing to configure NOT scan keyboard during configuration
  90. if (usbConfiguration && usbInterruptIsReady()) {
  91. keyboard_task();
  92. #if defined(RGBLIGHT_ANIMATIONS) && defined(RGBLIGHT_ENABLE)
  93. rgblight_task();
  94. #endif
  95. }
  96. vusb_transfer_keyboard();
  97. }
  98. }
  99. }