Keyboard firmwares for Atmel AVR and Cortex-M
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.

main.c 2.1KB

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