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 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /*
  2. Copyright 2011 Jun Wako <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include <stdint.h>
  15. #include <avr/interrupt.h>
  16. #include <avr/io.h>
  17. //#include <avr/wdt.h>
  18. #include "wd.h" // in order to use watchdog in interrupt mode
  19. #include <avr/sleep.h>
  20. #include <util/delay.h>
  21. #include <avr/power.h>
  22. #include "keyboard.h"
  23. #include "matrix.h"
  24. #include "host.h"
  25. #include "iwrap.h"
  26. #ifdef HOST_VUSB
  27. # include "vusb.h"
  28. # include "usbdrv.h"
  29. #endif
  30. #include "uart.h"
  31. #include "suart.h"
  32. #include "timer.h"
  33. #include "debug.h"
  34. #include "keycode.h"
  35. #include "command.h"
  36. static void sleep(uint8_t term);
  37. static bool console(void);
  38. static uint8_t console_command(uint8_t c);
  39. static uint8_t key2asc(uint8_t key);
  40. /*
  41. static void set_prr(void)
  42. {
  43. power_adc_disable();
  44. power_spi_disable();
  45. power_twi_disable();
  46. #ifndef TIMER_H
  47. //power_timer0_disable(); // used in timer.c
  48. #endif
  49. power_timer1_disable();
  50. power_timer2_disable();
  51. }
  52. */
  53. /*
  54. static void pullup_pins(void)
  55. {
  56. // DDRs are set to 0(input) by default.
  57. #ifdef PORTA
  58. PORTA = 0xFF;
  59. #endif
  60. PORTB = 0xFF;
  61. PORTC = 0xFF;
  62. PORTD = 0xFF;
  63. #ifdef PORTE
  64. PORTE = 0xFF;
  65. #endif
  66. #ifdef PORTE
  67. PORTF = 0xFF;
  68. #endif
  69. }
  70. */
  71. #ifdef HOST_VUSB
  72. static void disable_vusb(void)
  73. {
  74. // disable interrupt & disconnect to prevent host from enumerating
  75. USB_INTR_ENABLE &= ~(1 << USB_INTR_ENABLE_BIT);
  76. usbDeviceDisconnect();
  77. }
  78. static void enable_vusb(void)
  79. {
  80. USB_INTR_ENABLE |= (1 << USB_INTR_ENABLE_BIT);
  81. usbDeviceConnect();
  82. }
  83. static void init_vusb(void)
  84. {
  85. uint8_t i = 0;
  86. usbInit();
  87. disable_vusb();
  88. /* fake USB disconnect for > 250 ms */
  89. while(--i){
  90. _delay_ms(1);
  91. }
  92. enable_vusb();
  93. }
  94. #endif
  95. void change_driver(host_driver_t *driver)
  96. {
  97. host_clear_keyboard_report();
  98. host_swap_keyboard_report();
  99. host_clear_keyboard_report();
  100. host_send_keyboard_report();
  101. _delay_ms(1000);
  102. host_set_driver(driver);
  103. }
  104. static bool sleeping = false;
  105. static bool insomniac = false; // TODO: should be false for power saving
  106. static uint16_t last_timer = 0;
  107. int main(void)
  108. {
  109. MCUSR = 0;
  110. clock_prescale_set(clock_div_1);
  111. WD_SET(WD_OFF);
  112. // power saving: the result is worse than nothing... why?
  113. //pullup_pins();
  114. //set_prr();
  115. print_enable = true;
  116. debug_enable = false;
  117. #ifdef HOST_VUSB
  118. disable_vusb();
  119. #endif
  120. uart_init(115200);
  121. keyboard_init();
  122. print("\nSend BREAK for UART Console Commands.\n");
  123. // TODO: move to iWRAP/suart file
  124. print("suart init\n");
  125. // suart init
  126. // PC4: Tx Output IDLE(Hi)
  127. PORTC |= (1<<4);
  128. DDRC |= (1<<4);
  129. // PC5: Rx Input(pull-up)
  130. PORTC |= (1<<5);
  131. DDRC &= ~(1<<5);
  132. // suart receive interrut(PC5/PCINT13)
  133. PCMSK1 = 0b00100000;
  134. PCICR = 0b00000010;
  135. host_set_driver(iwrap_driver());
  136. print("iwrap_init()\n");
  137. iwrap_init();
  138. iwrap_call();
  139. last_timer = timer_read();
  140. while (true) {
  141. #ifdef HOST_VUSB
  142. if (host_get_driver() == vusb_driver())
  143. usbPoll();
  144. #endif
  145. keyboard_task();
  146. #ifdef HOST_VUSB
  147. if (host_get_driver() == vusb_driver())
  148. vusb_transfer_keyboard();
  149. #endif
  150. if (matrix_is_modified() || console()) {
  151. last_timer = timer_read();
  152. sleeping = false;
  153. } else if (!sleeping && timer_elapsed(last_timer) > 4000) {
  154. sleeping = true;
  155. iwrap_check_connection();
  156. }
  157. if (host_get_driver() == iwrap_driver()) {
  158. if (sleeping && !insomniac) {
  159. _delay_ms(1); // wait for UART to send
  160. iwrap_sleep();
  161. sleep(WDTO_60MS);
  162. }
  163. }
  164. }
  165. }
  166. static void sleep(uint8_t term)
  167. {
  168. WD_SET(WD_IRQ, term);
  169. cli();
  170. set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  171. sleep_enable();
  172. sleep_bod_disable();
  173. sei();
  174. sleep_cpu();
  175. sleep_disable();
  176. WD_SET(WD_OFF);
  177. }
  178. ISR(WDT_vect)
  179. {
  180. // wake up
  181. }
  182. static bool console(void)
  183. {
  184. // Send to Bluetoot module WT12
  185. static bool breaked = false;
  186. if (!uart_available())
  187. return false;
  188. else {
  189. uint8_t c;
  190. c = uart_getchar();
  191. uart_putchar(c);
  192. switch (c) {
  193. case 0x00: // BREAK signal
  194. if (!breaked) {
  195. print("break(? for help): ");
  196. breaked = true;
  197. }
  198. break;
  199. case '\r':
  200. uart_putchar('\n');
  201. iwrap_buf_send();
  202. break;
  203. case '\b':
  204. iwrap_buf_del();
  205. break;
  206. default:
  207. if (breaked) {
  208. print("\n");
  209. console_command(c);
  210. breaked = false;
  211. } else {
  212. iwrap_buf_add(c);
  213. }
  214. break;
  215. }
  216. return true;
  217. }
  218. }
  219. uint8_t command_extra()
  220. {
  221. return console_command(key2asc(host_get_first_key()));
  222. }
  223. static uint8_t console_command(uint8_t c)
  224. {
  225. switch (c) {
  226. case 'h':
  227. case '?':
  228. print("\nCommands for Bluetooth(WT12/iWRAP):\n");
  229. print("r: reset. software reset by watchdog\n");
  230. print("i: insomniac. prevent KB from sleeping\n");
  231. print("c: iwrap_call. CALL for BT connection.\n");
  232. #ifdef HOST_VUSB
  233. print("u: USB mode. switch to USB.\n");
  234. print("w: BT mode. switch to Bluetooth.\n");
  235. #endif
  236. print("k: kill first connection.\n");
  237. print("Del: unpair first pairing.\n");
  238. print("\n");
  239. return 0;
  240. case 'r':
  241. print("reset\n");
  242. WD_AVR_RESET();
  243. return 1;
  244. case 'i':
  245. insomniac = !insomniac;
  246. if (insomniac)
  247. print("insomniac\n");
  248. else
  249. print("not insomniac\n");
  250. return 1;
  251. case 'c':
  252. print("iwrap_call()\n");
  253. iwrap_call();
  254. return 1;
  255. #ifdef HOST_VUSB
  256. case 'u':
  257. print("USB mode\n");
  258. init_vusb();
  259. change_driver(vusb_driver());
  260. //iwrap_kill();
  261. //iwrap_sleep();
  262. // disable suart receive interrut(PC5/PCINT13)
  263. PCMSK1 &= ~(0b00100000);
  264. PCICR &= ~(0b00000010);
  265. return 1;
  266. case 'w':
  267. print("iWRAP mode\n");
  268. change_driver(iwrap_driver());
  269. disable_vusb();
  270. // enable suart receive interrut(PC5/PCINT13)
  271. PCMSK1 |= 0b00100000;
  272. PCICR |= 0b00000010;
  273. return 1;
  274. #endif
  275. case 'k':
  276. print("kill\n");
  277. iwrap_kill();
  278. return 1;
  279. case 0x7F: // DELETE
  280. print("unpair\n");
  281. iwrap_unpair();
  282. return 1;
  283. }
  284. return 0;
  285. }
  286. // convert keycode into ascii charactor
  287. static uint8_t key2asc(uint8_t key)
  288. {
  289. switch (key) {
  290. case KC_A: return 'a';
  291. case KC_B: return 'b';
  292. case KC_C: return 'c';
  293. case KC_D: return 'd';
  294. case KC_E: return 'e';
  295. case KC_F: return 'f';
  296. case KC_G: return 'g';
  297. case KC_H: return 'h';
  298. case KC_I: return 'i';
  299. case KC_J: return 'j';
  300. case KC_K: return 'k';
  301. case KC_L: return 'l';
  302. case KC_M: return 'm';
  303. case KC_N: return 'n';
  304. case KC_O: return 'o';
  305. case KC_P: return 'p';
  306. case KC_Q: return 'q';
  307. case KC_R: return 'r';
  308. case KC_S: return 's';
  309. case KC_T: return 't';
  310. case KC_U: return 'u';
  311. case KC_V: return 'v';
  312. case KC_W: return 'w';
  313. case KC_X: return 'x';
  314. case KC_Y: return 'y';
  315. case KC_Z: return 'z';
  316. case KC_1: return '1';
  317. case KC_2: return '2';
  318. case KC_3: return '3';
  319. case KC_4: return '4';
  320. case KC_5: return '5';
  321. case KC_6: return '6';
  322. case KC_7: return '7';
  323. case KC_8: return '8';
  324. case KC_9: return '9';
  325. case KC_0: return '0';
  326. case KC_ENTER: return '\n';
  327. case KC_ESCAPE: return 0x1B;
  328. case KC_BSPACE: return '\b';
  329. case KC_TAB: return '\t';
  330. case KC_SPACE: return ' ';
  331. case KC_MINUS: return '-';
  332. case KC_EQUAL: return '=';
  333. case KC_LBRACKET: return '[';
  334. case KC_RBRACKET: return ']';
  335. case KC_BSLASH: return '\\';
  336. case KC_NONUS_HASH: return '\\';
  337. case KC_SCOLON: return ';';
  338. case KC_QUOTE: return '\'';
  339. case KC_GRAVE: return '`';
  340. case KC_COMMA: return ',';
  341. case KC_DOT: return '.';
  342. case KC_SLASH: return '/';
  343. default: return 0x00;
  344. }
  345. }