Keyboard firmwares for Atmel AVR and Cortex-M
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

rn42.c 4.6KB

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