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