main.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #include <avr/io.h>
  2. #include <avr/power.h>
  3. #include <avr/wdt.h>
  4. #include "lufa.h"
  5. #include "print.h"
  6. #include "sendchar.h"
  7. #include "rn42.h"
  8. #include "rn42_task.h"
  9. #include "serial.h"
  10. #include "keyboard.h"
  11. #include "keycode.h"
  12. #include "action.h"
  13. #include "action_util.h"
  14. #include "wait.h"
  15. #include "suart.h"
  16. #include "suspend.h"
  17. #include "matrix.h"
  18. static int8_t sendchar_func(uint8_t c)
  19. {
  20. xmit(c); // SUART
  21. sendchar(c); // LUFA
  22. return 0;
  23. }
  24. static void SetupHardware(void)
  25. {
  26. /* Disable watchdog if enabled by bootloader/fuses */
  27. MCUSR &= ~(1 << WDRF);
  28. wdt_disable();
  29. /* Disable clock division */
  30. clock_prescale_set(clock_div_1);
  31. // Leonardo needs. Without this USB device is not recognized.
  32. USB_Disable();
  33. USB_Init();
  34. // for Console_Task
  35. USB_Device_EnableSOFEvents();
  36. print_set_sendchar(sendchar_func);
  37. // SUART PD0:output, PD1:input
  38. DDRD |= (1<<0);
  39. PORTD |= (1<<0);
  40. DDRD &= ~(1<<1);
  41. PORTD |= (1<<1);
  42. }
  43. int main(void) __attribute__ ((weak));
  44. int main(void)
  45. {
  46. SetupHardware();
  47. sei();
  48. /* wait for USB startup to get ready for debug output */
  49. uint8_t timeout = 255; // timeout when USB is not available(Bluetooth)
  50. while (timeout-- && USB_DeviceState != DEVICE_STATE_Configured) {
  51. wait_ms(4);
  52. #if defined(INTERRUPT_CONTROL_ENDPOINT)
  53. ;
  54. #else
  55. USB_USBTask();
  56. #endif
  57. }
  58. print("\nUSB init\n");
  59. rn42_init();
  60. rn42_task_init();
  61. print("RN-42 init\n");
  62. /* init modules */
  63. keyboard_init();
  64. #ifdef SLEEP_LED_ENABLE
  65. sleep_led_init();
  66. #endif
  67. print("Keyboard start\n");
  68. while (1) {
  69. while (rn42_rts() && // RN42 is off
  70. USB_DeviceState == DEVICE_STATE_Suspended) {
  71. print("[s]");
  72. matrix_power_down();
  73. suspend_power_down();
  74. suspend_power_down();
  75. suspend_power_down();
  76. suspend_power_down();
  77. suspend_power_down();
  78. suspend_power_down();
  79. suspend_power_down();
  80. if (USB_Device_RemoteWakeupEnabled && suspend_wakeup_condition()) {
  81. USB_Device_SendRemoteWakeup();
  82. }
  83. }
  84. keyboard_task();
  85. #if !defined(INTERRUPT_CONTROL_ENDPOINT)
  86. USB_USBTask();
  87. #endif
  88. rn42_task();
  89. }
  90. }