main.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /*
  2. Copyright 2011 Jun Wako <wakojun@gmail.com>
  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. // 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. USB_INTR_ENABLE |= (1 << USB_INTR_ENABLE_BIT);
  80. usbDeviceConnect();
  81. }
  82. static void init_vusb(void) {
  83. uint8_t i = 0;
  84. usbInit();
  85. disable_vusb();
  86. /* fake USB disconnect for > 250 ms */
  87. while (--i) {
  88. _delay_ms(1);
  89. }
  90. enable_vusb();
  91. }
  92. #endif
  93. void change_driver(host_driver_t *driver) {
  94. /*
  95. host_clear_keyboard_report();
  96. host_swap_keyboard_report();
  97. host_clear_keyboard_report();
  98. host_send_keyboard_report();
  99. */
  100. clear_keyboard();
  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. MCUSR = 0;
  109. clock_prescale_set(clock_div_1);
  110. WD_SET(WD_OFF);
  111. // power saving: the result is worse than nothing... why?
  112. // pullup_pins();
  113. // set_prr();
  114. #ifdef PROTOCOL_VUSB
  115. disable_vusb();
  116. #endif
  117. uart_init(115200);
  118. keyboard_init();
  119. print("\nSend BREAK for UART Console Commands.\n");
  120. // TODO: move to iWRAP/suart file
  121. print("suart init\n");
  122. // suart init
  123. // PC4: Tx Output IDLE(Hi)
  124. PORTC |= (1 << 4);
  125. DDRC |= (1 << 4);
  126. // PC5: Rx Input(pull-up)
  127. PORTC |= (1 << 5);
  128. DDRC &= ~(1 << 5);
  129. // suart receive interrut(PC5/PCINT13)
  130. PCMSK1 = 0b00100000;
  131. PCICR = 0b00000010;
  132. host_set_driver(iwrap_driver());
  133. print("iwrap_init()\n");
  134. iwrap_init();
  135. iwrap_call();
  136. last_timer = timer_read();
  137. while (true) {
  138. #ifdef PROTOCOL_VUSB
  139. if (host_get_driver() == vusb_driver()) usbPoll();
  140. #endif
  141. keyboard_task();
  142. #ifdef PROTOCOL_VUSB
  143. if (host_get_driver() == vusb_driver()) vusb_transfer_keyboard();
  144. #endif
  145. // TODO: depricated
  146. if (matrix_is_modified() || console()) {
  147. last_timer = timer_read();
  148. sleeping = false;
  149. } else if (!sleeping && timer_elapsed(last_timer) > 4000) {
  150. sleeping = true;
  151. iwrap_check_connection();
  152. }
  153. // TODO: suspend.h
  154. if (host_get_driver() == iwrap_driver()) {
  155. if (sleeping && !insomniac) {
  156. _delay_ms(1); // wait for UART to send
  157. iwrap_sleep();
  158. sleep(WDTO_60MS);
  159. }
  160. }
  161. }
  162. }
  163. static void sleep(uint8_t term) {
  164. WD_SET(WD_IRQ, term);
  165. cli();
  166. set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  167. sleep_enable();
  168. sleep_bod_disable();
  169. sei();
  170. sleep_cpu();
  171. sleep_disable();
  172. WD_SET(WD_OFF);
  173. }
  174. static bool console(void) {
  175. // Send to Bluetoot module WT12
  176. static bool breaked = false;
  177. if (!uart_available())
  178. return false;
  179. else {
  180. uint8_t c;
  181. c = uart_getchar();
  182. uart_putchar(c);
  183. switch (c) {
  184. case 0x00: // BREAK signal
  185. if (!breaked) {
  186. print("break(? for help): ");
  187. breaked = true;
  188. }
  189. break;
  190. case '\r':
  191. uart_putchar('\n');
  192. iwrap_buf_send();
  193. break;
  194. case '\b':
  195. iwrap_buf_del();
  196. break;
  197. default:
  198. if (breaked) {
  199. print("\n");
  200. console_command(c);
  201. breaked = false;
  202. } else {
  203. iwrap_buf_add(c);
  204. }
  205. break;
  206. }
  207. return true;
  208. }
  209. }
  210. bool command_extra(uint8_t code) { return console_command(key2asc(code)); }
  211. static bool console_command(uint8_t c) {
  212. switch (c) {
  213. case 'h':
  214. case '?':
  215. print("\nCommands for Bluetooth(WT12/iWRAP):\n");
  216. print("r: reset. software reset by watchdog\n");
  217. print("i: insomniac. prevent KB from sleeping\n");
  218. print("c: iwrap_call. CALL for BT connection.\n");
  219. #ifdef PROTOCOL_VUSB
  220. print("u: USB mode. switch to USB.\n");
  221. print("w: BT mode. switch to Bluetooth.\n");
  222. #endif
  223. print("k: kill first connection.\n");
  224. print("Del: unpair first pairing.\n");
  225. print("\n");
  226. return 0;
  227. case 'r':
  228. print("reset\n");
  229. WD_AVR_RESET();
  230. return 1;
  231. case 'i':
  232. insomniac = !insomniac;
  233. if (insomniac)
  234. print("insomniac\n");
  235. else
  236. print("not insomniac\n");
  237. return 1;
  238. case 'c':
  239. print("iwrap_call()\n");
  240. iwrap_call();
  241. return 1;
  242. #ifdef PROTOCOL_VUSB
  243. case 'u':
  244. print("USB mode\n");
  245. init_vusb();
  246. change_driver(vusb_driver());
  247. // iwrap_kill();
  248. // iwrap_sleep();
  249. // disable suart receive interrut(PC5/PCINT13)
  250. PCMSK1 &= ~(0b00100000);
  251. PCICR &= ~(0b00000010);
  252. return 1;
  253. case 'w':
  254. print("iWRAP mode\n");
  255. change_driver(iwrap_driver());
  256. disable_vusb();
  257. // enable suart receive interrut(PC5/PCINT13)
  258. PCMSK1 |= 0b00100000;
  259. PCICR |= 0b00000010;
  260. return 1;
  261. #endif
  262. case 'k':
  263. print("kill\n");
  264. iwrap_kill();
  265. return 1;
  266. case 0x7F: // DELETE
  267. print("unpair\n");
  268. iwrap_unpair();
  269. return 1;
  270. }
  271. return 0;
  272. }
  273. // convert keycode into ascii charactor
  274. static uint8_t key2asc(uint8_t key) {
  275. switch (key) {
  276. case KC_A:
  277. return 'a';
  278. case KC_B:
  279. return 'b';
  280. case KC_C:
  281. return 'c';
  282. case KC_D:
  283. return 'd';
  284. case KC_E:
  285. return 'e';
  286. case KC_F:
  287. return 'f';
  288. case KC_G:
  289. return 'g';
  290. case KC_H:
  291. return 'h';
  292. case KC_I:
  293. return 'i';
  294. case KC_J:
  295. return 'j';
  296. case KC_K:
  297. return 'k';
  298. case KC_L:
  299. return 'l';
  300. case KC_M:
  301. return 'm';
  302. case KC_N:
  303. return 'n';
  304. case KC_O:
  305. return 'o';
  306. case KC_P:
  307. return 'p';
  308. case KC_Q:
  309. return 'q';
  310. case KC_R:
  311. return 'r';
  312. case KC_S:
  313. return 's';
  314. case KC_T:
  315. return 't';
  316. case KC_U:
  317. return 'u';
  318. case KC_V:
  319. return 'v';
  320. case KC_W:
  321. return 'w';
  322. case KC_X:
  323. return 'x';
  324. case KC_Y:
  325. return 'y';
  326. case KC_Z:
  327. return 'z';
  328. case KC_1:
  329. return '1';
  330. case KC_2:
  331. return '2';
  332. case KC_3:
  333. return '3';
  334. case KC_4:
  335. return '4';
  336. case KC_5:
  337. return '5';
  338. case KC_6:
  339. return '6';
  340. case KC_7:
  341. return '7';
  342. case KC_8:
  343. return '8';
  344. case KC_9:
  345. return '9';
  346. case KC_0:
  347. return '0';
  348. case KC_ENTER:
  349. return '\n';
  350. case KC_ESCAPE:
  351. return 0x1B;
  352. case KC_BSPACE:
  353. return '\b';
  354. case KC_TAB:
  355. return '\t';
  356. case KC_SPACE:
  357. return ' ';
  358. case KC_MINUS:
  359. return '-';
  360. case KC_EQUAL:
  361. return '=';
  362. case KC_LBRACKET:
  363. return '[';
  364. case KC_RBRACKET:
  365. return ']';
  366. case KC_BSLASH:
  367. return '\\';
  368. case KC_NONUS_HASH:
  369. return '\\';
  370. case KC_SCOLON:
  371. return ';';
  372. case KC_QUOTE:
  373. return '\'';
  374. case KC_GRAVE:
  375. return '`';
  376. case KC_COMMA:
  377. return ',';
  378. case KC_DOT:
  379. return '.';
  380. case KC_SLASH:
  381. return '/';
  382. default:
  383. return 0x00;
  384. }
  385. }