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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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 "action.h"
  26. #include "iwrap.h"
  27. #ifdef PROTOCOL_VUSB
  28. # include "vusb.h"
  29. # include "usbdrv.h"
  30. #endif
  31. #include "uart.h"
  32. #include "suart.h"
  33. #include "timer.h"
  34. #include "debug.h"
  35. #include "keycode.h"
  36. #include "command.h"
  37. static void sleep(uint8_t term);
  38. static bool console(void);
  39. static bool console_command(uint8_t c);
  40. static uint8_t key2asc(uint8_t key);
  41. /*
  42. static void set_prr(void)
  43. {
  44. power_adc_disable();
  45. power_spi_disable();
  46. power_twi_disable();
  47. #ifndef TIMER_H
  48. //power_timer0_disable(); // used in timer.c
  49. #endif
  50. power_timer1_disable();
  51. power_timer2_disable();
  52. }
  53. */
  54. /*
  55. static void pullup_pins(void)
  56. {
  57. // DDRs are set to 0(input) by default.
  58. #ifdef PORTA
  59. PORTA = 0xFF;
  60. #endif
  61. PORTB = 0xFF;
  62. PORTC = 0xFF;
  63. PORTD = 0xFF;
  64. #ifdef PORTE
  65. PORTE = 0xFF;
  66. #endif
  67. #ifdef PORTE
  68. PORTF = 0xFF;
  69. #endif
  70. }
  71. */
  72. #ifdef PROTOCOL_VUSB
  73. static void disable_vusb(void)
  74. {
  75. // disable interrupt & disconnect to prevent host from enumerating
  76. USB_INTR_ENABLE &= ~(1 << USB_INTR_ENABLE_BIT);
  77. usbDeviceDisconnect();
  78. }
  79. static void enable_vusb(void)
  80. {
  81. USB_INTR_ENABLE |= (1 << USB_INTR_ENABLE_BIT);
  82. usbDeviceConnect();
  83. }
  84. static void init_vusb(void)
  85. {
  86. uint8_t i = 0;
  87. usbInit();
  88. disable_vusb();
  89. /* fake USB disconnect for > 250 ms */
  90. while(--i){
  91. _delay_ms(1);
  92. }
  93. enable_vusb();
  94. }
  95. #endif
  96. void change_driver(host_driver_t *driver)
  97. {
  98. /*
  99. host_clear_keyboard_report();
  100. host_swap_keyboard_report();
  101. host_clear_keyboard_report();
  102. host_send_keyboard_report();
  103. */
  104. clear_keyboard();
  105. _delay_ms(1000);
  106. host_set_driver(driver);
  107. }
  108. static bool sleeping = false;
  109. static bool insomniac = false; // TODO: should be false for power saving
  110. static uint16_t last_timer = 0;
  111. int main(void)
  112. {
  113. MCUSR = 0;
  114. clock_prescale_set(clock_div_1);
  115. WD_SET(WD_OFF);
  116. // power saving: the result is worse than nothing... why?
  117. //pullup_pins();
  118. //set_prr();
  119. #ifdef PROTOCOL_VUSB
  120. disable_vusb();
  121. #endif
  122. uart_init(115200);
  123. keyboard_init();
  124. print("\nSend BREAK for UART Console Commands.\n");
  125. // TODO: move to iWRAP/suart file
  126. print("suart init\n");
  127. // suart init
  128. // PC4: Tx Output IDLE(Hi)
  129. PORTC |= (1<<4);
  130. DDRC |= (1<<4);
  131. // PC5: Rx Input(pull-up)
  132. PORTC |= (1<<5);
  133. DDRC &= ~(1<<5);
  134. // suart receive interrut(PC5/PCINT13)
  135. PCMSK1 = 0b00100000;
  136. PCICR = 0b00000010;
  137. host_set_driver(iwrap_driver());
  138. print("iwrap_init()\n");
  139. iwrap_init();
  140. iwrap_call();
  141. last_timer = timer_read();
  142. while (true) {
  143. #ifdef PROTOCOL_VUSB
  144. if (host_get_driver() == vusb_driver())
  145. usbPoll();
  146. #endif
  147. keyboard_task();
  148. #ifdef PROTOCOL_VUSB
  149. if (host_get_driver() == vusb_driver())
  150. vusb_transfer_keyboard();
  151. #endif
  152. // TODO: depricated
  153. if (matrix_is_modified() || console()) {
  154. last_timer = timer_read();
  155. sleeping = false;
  156. } else if (!sleeping && timer_elapsed(last_timer) > 4000) {
  157. sleeping = true;
  158. iwrap_check_connection();
  159. }
  160. // TODO: suspend.h
  161. if (host_get_driver() == iwrap_driver()) {
  162. if (sleeping && !insomniac) {
  163. _delay_ms(1); // wait for UART to send
  164. iwrap_sleep();
  165. sleep(WDTO_60MS);
  166. }
  167. }
  168. }
  169. }
  170. static void sleep(uint8_t term)
  171. {
  172. WD_SET(WD_IRQ, term);
  173. cli();
  174. set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  175. sleep_enable();
  176. sleep_bod_disable();
  177. sei();
  178. sleep_cpu();
  179. sleep_disable();
  180. WD_SET(WD_OFF);
  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. bool command_extra(uint8_t code)
  220. {
  221. return console_command(key2asc(code));
  222. }
  223. static bool 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 PROTOCOL_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 PROTOCOL_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. }