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