Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
Это архивный репозиторий. Вы можете его клонировать или просматривать файлы, но не вносить изменения или открывать задачи/запросы на слияние.

10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. static void send_keyboard(report_keyboard_t *report)
  114. {
  115. // wake from deep sleep
  116. /*
  117. PORTD |= (1<<5); // high
  118. wait_ms(5);
  119. PORTD &= ~(1<<5); // low
  120. */
  121. serial_send(0xFD); // Raw report mode
  122. serial_send(9); // length
  123. serial_send(1); // descriptor type
  124. serial_send(report->mods);
  125. serial_send(0x00);
  126. serial_send(report->keys[0]);
  127. serial_send(report->keys[1]);
  128. serial_send(report->keys[2]);
  129. serial_send(report->keys[3]);
  130. serial_send(report->keys[4]);
  131. serial_send(report->keys[5]);
  132. }
  133. static void send_mouse(report_mouse_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(5); // length
  143. serial_send(2); // descriptor type
  144. serial_send(report->buttons);
  145. serial_send(report->x);
  146. serial_send(report->y);
  147. serial_send(report->v);
  148. }
  149. static void send_system(uint16_t data)
  150. {
  151. // Table 5-6 of RN-BT-DATA-UB
  152. // 81,82,83 scan codes can be used?
  153. }
  154. static uint16_t usage2bits(uint16_t usage)
  155. {
  156. switch (usage) {
  157. case AC_HOME: return 0x01;
  158. case AL_EMAIL: return 0x02;
  159. case AC_SEARCH: return 0x04;
  160. //case AL_KBD_LAYOUT: return 0x08; // Apple virtual keybaord toggle
  161. case AUDIO_VOL_UP: return 0x10;
  162. case AUDIO_VOL_DOWN: return 0x20;
  163. case AUDIO_MUTE: return 0x40;
  164. case TRANSPORT_PLAY_PAUSE: return 0x80;
  165. case TRANSPORT_NEXT_TRACK: return 0x100;
  166. case TRANSPORT_PREV_TRACK: return 0x200;
  167. case TRANSPORT_STOP: return 0x400;
  168. case TRANSPORT_STOP_EJECT: return 0x800;
  169. //case return 0x1000; // Fast forward
  170. //case return 0x2000; // Rewind
  171. //case return 0x4000; // Stop/eject
  172. //case return 0x8000; // Internet browser
  173. };
  174. return 0;
  175. }
  176. static void send_consumer(uint16_t data)
  177. {
  178. uint16_t bits = usage2bits(data);
  179. serial_send(0xFD); // Raw report mode
  180. serial_send(3); // length
  181. serial_send(3); // descriptor type
  182. serial_send(bits&0xFF);
  183. serial_send((bits>>8)&0xFF);
  184. }
  185. /* Null driver for config_mode */
  186. static uint8_t config_keyboard_leds(void);
  187. static void config_send_keyboard(report_keyboard_t *report);
  188. static void config_send_mouse(report_mouse_t *report);
  189. static void config_send_system(uint16_t data);
  190. static void config_send_consumer(uint16_t data);
  191. host_driver_t rn42_config_driver = {
  192. config_keyboard_leds,
  193. config_send_keyboard,
  194. config_send_mouse,
  195. config_send_system,
  196. config_send_consumer
  197. };
  198. static uint8_t config_keyboard_leds(void) { return leds; }
  199. static void config_send_keyboard(report_keyboard_t *report) {}
  200. static void config_send_mouse(report_mouse_t *report) {}
  201. static void config_send_system(uint16_t data) {}
  202. static void config_send_consumer(uint16_t data) {}