qmk_firmware/keyboard/hhkb/rn42/main.c

112 lines
2.4 KiB
C
Raw Normal View History

2014-06-24 21:57:36 -07:00
#include <avr/io.h>
#include <avr/power.h>
#include <avr/wdt.h>
#include "lufa.h"
#include "print.h"
#include "sendchar.h"
2014-06-30 12:29:45 -07:00
#include "rn42.h"
2014-07-17 00:59:22 -07:00
#include "rn42_task.h"
2014-06-30 12:29:45 -07:00
#include "serial.h"
#include "keyboard.h"
#include "keycode.h"
#include "action.h"
#include "action_util.h"
#include "wait.h"
2014-07-13 23:20:10 -07:00
#include "suart.h"
2014-07-18 10:33:23 -07:00
#include "suspend.h"
2014-06-24 21:57:36 -07:00
2014-07-13 23:20:10 -07:00
static int8_t sendchar_func(uint8_t c)
{
xmit(c); // SUART
sendchar(c); // LUFA
2014-07-17 00:59:22 -07:00
return 0;
2014-07-13 23:20:10 -07:00
}
2014-06-24 21:57:36 -07:00
static void SetupHardware(void)
{
/* Disable watchdog if enabled by bootloader/fuses */
MCUSR &= ~(1 << WDRF);
wdt_disable();
/* Disable clock division */
clock_prescale_set(clock_div_1);
// Leonardo needs. Without this USB device is not recognized.
USB_Disable();
USB_Init();
// for Console_Task
USB_Device_EnableSOFEvents();
2014-07-13 23:20:10 -07:00
print_set_sendchar(sendchar_func);
// SUART PD0:output, PD1:input
DDRD |= (1<<0);
PORTD |= (1<<0);
DDRD &= ~(1<<1);
PORTD |= (1<<1);
2014-06-24 21:57:36 -07:00
}
int main(void) __attribute__ ((weak));
int main(void)
{
SetupHardware();
sei();
2014-06-30 12:29:45 -07:00
/* wait for USB startup to get ready for debug output */
uint8_t timeout = 255; // timeout when USB is not available(Bluetooth)
while (timeout-- && USB_DeviceState != DEVICE_STATE_Configured) {
wait_ms(4);
2014-06-24 21:57:36 -07:00
#if defined(INTERRUPT_CONTROL_ENDPOINT)
;
#else
USB_USBTask();
#endif
}
print("\nUSB init\n");
2014-06-24 21:57:36 -07:00
2014-06-30 12:29:45 -07:00
rn42_init();
2014-07-17 00:59:22 -07:00
rn42_task_init();
2014-06-30 12:29:45 -07:00
print("RN-42 init\n");
2014-06-24 21:57:36 -07:00
/* init modules */
keyboard_init();
2014-06-30 12:29:45 -07:00
2014-07-16 21:06:48 -07:00
if (!rn42_rts()) {
2014-06-30 12:29:45 -07:00
host_set_driver(&rn42_driver);
} else {
host_set_driver(&lufa_driver);
}
2014-06-24 21:57:36 -07:00
#ifdef SLEEP_LED_ENABLE
sleep_led_init();
#endif
print("Keyboard start\n");
2014-06-24 21:57:36 -07:00
while (1) {
while (rn42_rts() && // RN42 is off
USB_DeviceState == DEVICE_STATE_Suspended) {
print("[s]");
matrix_power_down();
suspend_power_down();
suspend_power_down();
suspend_power_down();
suspend_power_down();
suspend_power_down();
suspend_power_down();
2014-11-25 18:25:45 -08:00
suspend_power_down();
2014-06-24 21:57:36 -07:00
if (USB_Device_RemoteWakeupEnabled && suspend_wakeup_condition()) {
USB_Device_SendRemoteWakeup();
}
}
keyboard_task();
#if !defined(INTERRUPT_CONTROL_ENDPOINT)
USB_USBTask();
#endif
2014-06-30 12:29:45 -07:00
2014-07-17 00:59:22 -07:00
rn42_task();
2014-06-30 12:29:45 -07:00
}
}