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 4.9KB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 "wait.h"
  8. /* Host driver */
  9. static uint8_t keyboard_leds(void);
  10. static void send_keyboard(report_keyboard_t *report);
  11. static void send_mouse(report_mouse_t *report);
  12. static void send_system(uint16_t data);
  13. static void send_consumer(uint16_t data);
  14. host_driver_t rn42_driver = {
  15. keyboard_leds,
  16. send_keyboard,
  17. send_mouse,
  18. send_system,
  19. send_consumer
  20. };
  21. void rn42_init(void)
  22. {
  23. // JTAG disable for PORT F. write JTD bit twice within four cycles.
  24. MCUCR |= (1<<JTD);
  25. MCUCR |= (1<<JTD);
  26. // PF7: BT connection control(high: connect, low: disconnect)
  27. rn42_autoconnect();
  28. // PF6: linked(input without pull-up)
  29. DDRF &= ~(1<<6);
  30. PORTF |= (1<<6);
  31. // PF1: RTS(low: allowed to send, high: not allowed)
  32. DDRF &= ~(1<<1);
  33. PORTF &= ~(1<<1);
  34. // PD5: CTS(low: allow to send, high:not allow)
  35. DDRD |= (1<<5);
  36. PORTD &= ~(1<<5);
  37. serial_init();
  38. }
  39. void rn42_putc(uint8_t c)
  40. {
  41. serial_send(c);
  42. }
  43. bool rn42_autoconnecting(void)
  44. {
  45. // GPIO6 for control connection(high: auto connect, low: disconnect)
  46. // Note that this needs config: SM,4(Auto-Connect DTR Mode)
  47. return (PORTF & (1<<7) ? true : false);
  48. }
  49. void rn42_autoconnect(void)
  50. {
  51. // hi to auto connect
  52. DDRF |= (1<<7);
  53. PORTF |= (1<<7);
  54. }
  55. void rn42_disconnect(void)
  56. {
  57. // low to disconnect
  58. DDRF |= (1<<7);
  59. PORTF &= ~(1<<7);
  60. }
  61. bool rn42_rts(void)
  62. {
  63. // low when RN-42 is powered and ready to receive
  64. return PINF&(1<<1);
  65. }
  66. void rn42_cts_hi(void)
  67. {
  68. // not allow to send
  69. PORTD |= (1<<5);
  70. }
  71. void rn42_cts_lo(void)
  72. {
  73. // allow to send
  74. PORTD &= ~(1<<5);
  75. }
  76. bool rn42_linked(void)
  77. {
  78. // RN-42 GPIO2
  79. // Hi-Z: Not powered
  80. // High: Linked
  81. // Low: Connecting
  82. return !rn42_rts() && PINF&(1<<6);
  83. }
  84. static uint8_t leds = 0;
  85. static uint8_t keyboard_leds(void) { return leds; }
  86. void rn42_set_leds(uint8_t l) { leds = l; }
  87. static void send_keyboard(report_keyboard_t *report)
  88. {
  89. // wake from deep sleep
  90. /*
  91. PORTD |= (1<<5); // high
  92. wait_ms(5);
  93. PORTD &= ~(1<<5); // low
  94. */
  95. serial_send(0xFD); // Raw report mode
  96. serial_send(9); // length
  97. serial_send(1); // descriptor type
  98. serial_send(report->mods);
  99. serial_send(0x00);
  100. serial_send(report->keys[0]);
  101. serial_send(report->keys[1]);
  102. serial_send(report->keys[2]);
  103. serial_send(report->keys[3]);
  104. serial_send(report->keys[4]);
  105. serial_send(report->keys[5]);
  106. }
  107. static void send_mouse(report_mouse_t *report)
  108. {
  109. // wake from deep sleep
  110. /*
  111. PORTD |= (1<<5); // high
  112. wait_ms(5);
  113. PORTD &= ~(1<<5); // low
  114. */
  115. serial_send(0xFD); // Raw report mode
  116. serial_send(5); // length
  117. serial_send(2); // descriptor type
  118. serial_send(report->buttons);
  119. serial_send(report->x);
  120. serial_send(report->y);
  121. serial_send(report->v);
  122. }
  123. static void send_system(uint16_t data)
  124. {
  125. // Table 5-6 of RN-BT-DATA-UB
  126. // 81,82,83 scan codes can be used?
  127. }
  128. static uint16_t usage2bits(uint16_t usage)
  129. {
  130. switch (usage) {
  131. case AC_HOME: return 0x01;
  132. case AL_EMAIL: return 0x02;
  133. case AC_SEARCH: return 0x04;
  134. //case AL_KBD_LAYOUT: return 0x08; // Apple virtual keybaord toggle
  135. case AUDIO_VOL_UP: return 0x10;
  136. case AUDIO_VOL_DOWN: return 0x20;
  137. case AUDIO_MUTE: return 0x40;
  138. case TRANSPORT_PLAY_PAUSE: return 0x80;
  139. case TRANSPORT_NEXT_TRACK: return 0x100;
  140. case TRANSPORT_PREV_TRACK: return 0x200;
  141. case TRANSPORT_STOP: return 0x400;
  142. case TRANSPORT_STOP_EJECT: return 0x800;
  143. //case return 0x1000; // Fast forward
  144. //case return 0x2000; // Rewind
  145. //case return 0x4000; // Stop/eject
  146. //case return 0x8000; // Internet browser
  147. };
  148. return 0;
  149. }
  150. static void send_consumer(uint16_t data)
  151. {
  152. uint16_t bits = usage2bits(data);
  153. serial_send(0xFD); // Raw report mode
  154. serial_send(3); // length
  155. serial_send(3); // descriptor type
  156. serial_send(bits&0xFF);
  157. serial_send((bits>>8)&0xFF);
  158. }
  159. /* Null driver for config_mode */
  160. static uint8_t config_keyboard_leds(void);
  161. static void config_send_keyboard(report_keyboard_t *report);
  162. static void config_send_mouse(report_mouse_t *report);
  163. static void config_send_system(uint16_t data);
  164. static void config_send_consumer(uint16_t data);
  165. host_driver_t rn42_config_driver = {
  166. config_keyboard_leds,
  167. config_send_keyboard,
  168. config_send_mouse,
  169. config_send_system,
  170. config_send_consumer
  171. };
  172. static uint8_t config_keyboard_leds(void) { return leds; }
  173. static void config_send_keyboard(report_keyboard_t *report) {}
  174. static void config_send_mouse(report_mouse_t *report) {}
  175. static void config_send_system(uint16_t data) {}
  176. static void config_send_consumer(uint16_t data) {}