rn42.c 5.8 KB

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