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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

main.c 2.4KB

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