]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboard/hhkb_rn42/rn42/main.c
Fix lufa sendchar() infinite wait loop
[qmk_firmware.git] / keyboard / hhkb_rn42 / 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("USB configured.\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 (USB_DeviceState == DEVICE_STATE_Suspended) {
88             print("[s]");
89             suspend_power_down();
90             if (USB_Device_RemoteWakeupEnabled && suspend_wakeup_condition()) {
91                     USB_Device_SendRemoteWakeup();
92             }
93         }
94
95         keyboard_task();
96
97 #if !defined(INTERRUPT_CONTROL_ENDPOINT)
98         USB_USBTask();
99 #endif
100
101         rn42_task();
102     }
103 }