Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.c 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. Bluefruit Protocol for TMK firmware
  3. Author: Benjamin Gould, 2013
  4. Based on code Copyright 2011 Jun Wako <[email protected]>
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <stdint.h>
  17. #include <avr/interrupt.h>
  18. #include <avr/wdt.h>
  19. #include <avr/sleep.h>
  20. #include <util/delay.h>
  21. #include "serial.h"
  22. #include "keyboard.h"
  23. #include "usb.h"
  24. #include "host.h"
  25. #include "timer.h"
  26. #include "print.h"
  27. #include "debug.h"
  28. #include "sendchar.h"
  29. #include "suspend.h"
  30. #include "bluefruit.h"
  31. #include "pjrc.h"
  32. #define CPU_PRESCALE(n) (CLKPR = 0x80, CLKPR = (n))
  33. #define HOST_DRIVER_NOT_SET 0
  34. #define BLUEFRUIT_HOST_DRIVER 1
  35. #define PJRC_HOST_DRIVER 2
  36. int main(void)
  37. {
  38. CPU_PRESCALE(0);
  39. DDRD = _BV(PD5);
  40. DDRB = _BV(PB0);
  41. PORTD = _BV(PD5);
  42. PORTB = _BV(PB0);
  43. print_set_sendchar(sendchar);
  44. usb_init();
  45. _delay_ms(2000);
  46. // while (!usb_configured()) /* wait */
  47. dprintf("Initializing keyboard...\n");
  48. keyboard_init();
  49. // This implementation is pretty simplistic... if the USB connection
  50. // is not configured, choose the Bluefruit, otherwise use USB
  51. // Definitely would prefer to have this driven by an input pin and make
  52. // it switch dynamically - BCG
  53. if (!usb_configured()) {
  54. // Send power to Bluefruit... Adafruit says it takes 27 mA, I think
  55. // the pins should provide 40 mA, but just in case I switch the
  56. // Bluefruit using a transistor - BCG
  57. DDRB = _BV(PB6);
  58. PORTB |= _BV(PB6);
  59. dprintf("Setting host driver to bluefruit...\n");
  60. host_set_driver(bluefruit_driver());
  61. dprintf("Initializing serial...\n");
  62. serial_init();
  63. // wait an extra second for the PC's operating system
  64. // to load drivers and do whatever it does to actually
  65. // be ready for input
  66. _delay_ms(1000);
  67. PORTD = ~_BV(PD5);
  68. dprintf("Starting main loop");
  69. while (1) {
  70. keyboard_task();
  71. }
  72. } else {
  73. // I'm not smart enough to get this done with LUFA - BCG
  74. dprintf("Setting host driver to PJRC...\n");
  75. host_set_driver(pjrc_driver());
  76. #ifdef SLEEP_LED_ENABLE
  77. sleep_led_init();
  78. #endif
  79. // wait an extra second for the PC's operating system
  80. // to load drivers and do whatever it does to actually
  81. // be ready for input
  82. _delay_ms(1000);
  83. PORTB = ~_BV(PB0);
  84. dprintf("Starting main loop");
  85. while (1) {
  86. while (suspend) {
  87. suspend_power_down();
  88. if (remote_wakeup && suspend_wakeup_condition()) {
  89. usb_remote_wakeup();
  90. }
  91. }
  92. keyboard_task();
  93. }
  94. }
  95. }