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