Keyboard firmwares for Atmel AVR and Cortex-M
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

rn42.c 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. return PINF&(1<<6);
  79. }
  80. static uint8_t keyboard_leds(void) { return 0; }
  81. static void send_keyboard(report_keyboard_t *report)
  82. {
  83. // wake from deep sleep
  84. /*
  85. PORTD |= (1<<5); // high
  86. wait_ms(5);
  87. PORTD &= ~(1<<5); // low
  88. */
  89. serial_send(0xFD); // Raw report mode
  90. serial_send(9); // length
  91. serial_send(1); // descriptor type
  92. serial_send(report->mods);
  93. serial_send(0x00);
  94. serial_send(report->keys[0]);
  95. serial_send(report->keys[1]);
  96. serial_send(report->keys[2]);
  97. serial_send(report->keys[3]);
  98. serial_send(report->keys[4]);
  99. serial_send(report->keys[5]);
  100. }
  101. static void send_mouse(report_mouse_t *report)
  102. {
  103. // wake from deep sleep
  104. /*
  105. PORTD |= (1<<5); // high
  106. wait_ms(5);
  107. PORTD &= ~(1<<5); // low
  108. */
  109. serial_send(0xFD); // Raw report mode
  110. serial_send(5); // length
  111. serial_send(2); // descriptor type
  112. serial_send(report->buttons);
  113. serial_send(report->x);
  114. serial_send(report->y);
  115. serial_send(report->v);
  116. }
  117. static void send_system(uint16_t data)
  118. {
  119. // Table 5-6 of RN-BT-DATA-UB
  120. // 81,82,83 scan codes can be used?
  121. }
  122. static uint16_t usage2bits(uint16_t usage)
  123. {
  124. switch (usage) {
  125. case AC_HOME: return 0x01;
  126. case AL_EMAIL: return 0x02;
  127. case AC_SEARCH: return 0x04;
  128. //case AL_KBD_LAYOUT: return 0x08; // Apple virtual keybaord toggle
  129. case AUDIO_VOL_UP: return 0x10;
  130. case AUDIO_VOL_DOWN: return 0x20;
  131. case AUDIO_MUTE: return 0x40;
  132. case TRANSPORT_PLAY_PAUSE: return 0x80;
  133. case TRANSPORT_NEXT_TRACK: return 0x100;
  134. case TRANSPORT_PREV_TRACK: return 0x200;
  135. case TRANSPORT_STOP: return 0x400;
  136. case TRANSPORT_STOP_EJECT: return 0x800;
  137. //case return 0x1000; // Fast forward
  138. //case return 0x2000; // Rewind
  139. //case return 0x4000; // Stop/eject
  140. //case return 0x8000; // Internet browser
  141. };
  142. return 0;
  143. }
  144. static void send_consumer(uint16_t data)
  145. {
  146. uint16_t bits = usage2bits(data);
  147. serial_send(0xFD); // Raw report mode
  148. serial_send(3); // length
  149. serial_send(3); // descriptor type
  150. serial_send(bits&0xFF);
  151. serial_send((bits>>8)&0xFF);
  152. }
  153. /* Null driver for config_mode */
  154. static uint8_t config_keyboard_leds(void);
  155. static void config_send_keyboard(report_keyboard_t *report);
  156. static void config_send_mouse(report_mouse_t *report);
  157. static void config_send_system(uint16_t data);
  158. static void config_send_consumer(uint16_t data);
  159. host_driver_t rn42_config_driver = {
  160. config_keyboard_leds,
  161. config_send_keyboard,
  162. config_send_mouse,
  163. config_send_system,
  164. config_send_consumer
  165. };
  166. static uint8_t config_keyboard_leds(void) { return 0; }
  167. static void config_send_keyboard(report_keyboard_t *report) {}
  168. static void config_send_mouse(report_mouse_t *report) {}
  169. static void config_send_system(uint16_t data) {}
  170. static void config_send_consumer(uint16_t data) {}