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.

rn42.c 5.9KB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. #include <avr/io.h>
  2. #include "host.h"
  3. #include "host_driver.h"
  4. #include "serial.h"
  5. #include "rn42.h"
  6. #include "print.h"
  7. #include "timer.h"
  8. #include "wait.h"
  9. /* Host driver */
  10. static uint8_t keyboard_leds(void);
  11. static void send_keyboard(report_keyboard_t *report);
  12. static void send_mouse(report_mouse_t *report);
  13. static void send_system(uint16_t data);
  14. static void send_consumer(uint16_t data);
  15. host_driver_t rn42_driver = {
  16. keyboard_leds,
  17. send_keyboard,
  18. send_mouse,
  19. send_system,
  20. send_consumer
  21. };
  22. void rn42_init(void)
  23. {
  24. // JTAG disable for PORT F. write JTD bit twice within four cycles.
  25. MCUCR |= (1<<JTD);
  26. MCUCR |= (1<<JTD);
  27. // PF7: BT connection control(high: connect, low: disconnect)
  28. rn42_autoconnect();
  29. // PF6: linked(input without pull-up)
  30. DDRF &= ~(1<<6);
  31. PORTF |= (1<<6);
  32. // PF1: RTS(low: allowed to send, high: not allowed)
  33. DDRF &= ~(1<<1);
  34. PORTF &= ~(1<<1);
  35. // PD5: CTS(low: allow to send, high:not allow)
  36. DDRD |= (1<<5);
  37. PORTD &= ~(1<<5);
  38. serial_init();
  39. }
  40. int16_t rn42_getc(void)
  41. {
  42. return serial_recv2();
  43. }
  44. const char *rn42_gets(uint16_t timeout)
  45. {
  46. static char s[24];
  47. uint16_t t = timer_read();
  48. uint8_t i = 0;
  49. int16_t c;
  50. while (i < 23 && timer_elapsed(t) < timeout) {
  51. if ((c = rn42_getc()) != -1) {
  52. if ((char)c == '\r') continue;
  53. if ((char)c == '\n') break;
  54. s[i++] = c;
  55. }
  56. }
  57. s[i] = '\0';
  58. return s;
  59. }
  60. void rn42_putc(uint8_t c)
  61. {
  62. serial_send(c);
  63. }
  64. void rn42_puts(char *s)
  65. {
  66. while (*s)
  67. serial_send(*s++);
  68. }
  69. bool rn42_autoconnecting(void)
  70. {
  71. // GPIO6 for control connection(high: auto connect, low: disconnect)
  72. // Note that this needs config: SM,4(Auto-Connect DTR Mode)
  73. return (PORTF & (1<<7) ? true : false);
  74. }
  75. void rn42_autoconnect(void)
  76. {
  77. // hi to auto connect
  78. DDRF |= (1<<7);
  79. PORTF |= (1<<7);
  80. }
  81. void rn42_disconnect(void)
  82. {
  83. // low to disconnect
  84. DDRF |= (1<<7);
  85. PORTF &= ~(1<<7);
  86. }
  87. bool rn42_rts(void)
  88. {
  89. // low when RN-42 is powered and ready to receive
  90. return PINF&(1<<1);
  91. }
  92. void rn42_cts_hi(void)
  93. {
  94. // not allow to send
  95. PORTD |= (1<<5);
  96. }
  97. void rn42_cts_lo(void)
  98. {
  99. // allow to send
  100. PORTD &= ~(1<<5);
  101. }
  102. bool rn42_linked(void)
  103. {
  104. // RN-42 GPIO2
  105. // Hi-Z: Not powered
  106. // High: Linked
  107. // Low: Connecting
  108. return PINF&(1<<6);
  109. }
  110. static uint8_t leds = 0;
  111. static uint8_t keyboard_leds(void) { return leds; }
  112. void rn42_set_leds(uint8_t l) { leds = l; }
  113. void rn42_send_str(const char *str)
  114. {
  115. uint8_t c;
  116. while ((c = pgm_read_byte(str++)))
  117. rn42_putc(c);
  118. }
  119. const char *rn42_send_command(const char *cmd)
  120. {
  121. static const char *s;
  122. rn42_send_str(cmd);
  123. wait_ms(500);
  124. s = rn42_gets(100);
  125. xprintf("%s\r\n", s);
  126. rn42_print_response();
  127. return s;
  128. }
  129. void rn42_print_response(void)
  130. {
  131. int16_t c;
  132. while ((c = rn42_getc()) != -1) {
  133. xprintf("%c", c);
  134. }
  135. }
  136. static void send_keyboard(report_keyboard_t *report)
  137. {
  138. // wake from deep sleep
  139. /*
  140. PORTD |= (1<<5); // high
  141. wait_ms(5);
  142. PORTD &= ~(1<<5); // low
  143. */
  144. serial_send(0xFD); // Raw report mode
  145. serial_send(9); // length
  146. serial_send(1); // descriptor type
  147. serial_send(report->mods);
  148. serial_send(0x00);
  149. serial_send(report->keys[0]);
  150. serial_send(report->keys[1]);
  151. serial_send(report->keys[2]);
  152. serial_send(report->keys[3]);
  153. serial_send(report->keys[4]);
  154. serial_send(report->keys[5]);
  155. }
  156. static void send_mouse(report_mouse_t *report)
  157. {
  158. // wake from deep sleep
  159. /*
  160. PORTD |= (1<<5); // high
  161. wait_ms(5);
  162. PORTD &= ~(1<<5); // low
  163. */
  164. serial_send(0xFD); // Raw report mode
  165. serial_send(5); // length
  166. serial_send(2); // descriptor type
  167. serial_send(report->buttons);
  168. serial_send(report->x);
  169. serial_send(report->y);
  170. serial_send(report->v);
  171. }
  172. static void send_system(uint16_t data)
  173. {
  174. // Table 5-6 of RN-BT-DATA-UB
  175. // 81,82,83 scan codes can be used?
  176. }
  177. static uint16_t usage2bits(uint16_t usage)
  178. {
  179. switch (usage) {
  180. case APPCONTROL_HOME: return 0x01;
  181. case APPLAUNCH_EMAIL: return 0x02;
  182. case APPCONTROL_SEARCH: return 0x04;
  183. //case AL_KBD_LAYOUT: return 0x08; // Apple virtual keybaord toggle
  184. case AUDIO_VOL_UP: return 0x10;
  185. case AUDIO_VOL_DOWN: return 0x20;
  186. case AUDIO_MUTE: return 0x40;
  187. case TRANSPORT_PLAY_PAUSE: return 0x80;
  188. case TRANSPORT_NEXT_TRACK: return 0x100;
  189. case TRANSPORT_PREV_TRACK: return 0x200;
  190. case TRANSPORT_STOP: return 0x400;
  191. case TRANSPORT_STOP_EJECT: return 0x800;
  192. case TRANSPORT_FAST_FORWARD: return 0x1000;
  193. case TRANSPORT_REWIND: return 0x2000;
  194. //case return 0x4000; // Stop/eject
  195. //case return 0x8000; // Internet browser
  196. };
  197. return 0;
  198. }
  199. static void send_consumer(uint16_t data)
  200. {
  201. uint16_t bits = usage2bits(data);
  202. serial_send(0xFD); // Raw report mode
  203. serial_send(3); // length
  204. serial_send(3); // descriptor type
  205. serial_send(bits&0xFF);
  206. serial_send((bits>>8)&0xFF);
  207. }
  208. /* Null driver for config_mode */
  209. static uint8_t config_keyboard_leds(void);
  210. static void config_send_keyboard(report_keyboard_t *report);
  211. static void config_send_mouse(report_mouse_t *report);
  212. static void config_send_system(uint16_t data);
  213. static void config_send_consumer(uint16_t data);
  214. host_driver_t rn42_config_driver = {
  215. config_keyboard_leds,
  216. config_send_keyboard,
  217. config_send_mouse,
  218. config_send_system,
  219. config_send_consumer
  220. };
  221. static uint8_t config_keyboard_leds(void) { return leds; }
  222. static void config_send_keyboard(report_keyboard_t *report) {}
  223. static void config_send_mouse(report_mouse_t *report) {}
  224. static void config_send_system(uint16_t data) {}
  225. static void config_send_consumer(uint16_t data) {}