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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. // PF1: check RTS(active low)
  23. DDRF &= ~(1<<1);
  24. PORTF &= ~(1<<1);
  25. // PF7: BT connection control(HiZ: connect, low: disconnect)
  26. // JTAG disable for PORT F. write JTD bit twice within four cycles.
  27. MCUCR |= (1<<JTD);
  28. MCUCR |= (1<<JTD);
  29. rn42_autoconnect();
  30. // PD5: CTS (low: allow to send, high:not allowed)
  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. void rn42_autoconnect(void)
  40. {
  41. DDRF &= ~(1<<7);
  42. PORTF &= ~(1<<7);
  43. }
  44. void rn42_disconnect(void)
  45. {
  46. DDRF |= (1<<7);
  47. PORTF &= ~(1<<7);
  48. }
  49. bool rn42_ready(void)
  50. {
  51. // RTS low
  52. return PINF&(1<<1) ? false : true;
  53. }
  54. static uint8_t keyboard_leds(void) { return 0; }
  55. static void send_keyboard(report_keyboard_t *report)
  56. {
  57. // wake from deep sleep
  58. PORTD |= (1<<5); // high
  59. wait_ms(5);
  60. PORTD &= ~(1<<5); // low
  61. serial_send(0xFD); // Raw report mode
  62. serial_send(9); // length
  63. serial_send(1); // descriptor type
  64. serial_send(report->mods);
  65. serial_send(0x00);
  66. serial_send(report->keys[0]);
  67. serial_send(report->keys[1]);
  68. serial_send(report->keys[2]);
  69. serial_send(report->keys[3]);
  70. serial_send(report->keys[4]);
  71. serial_send(report->keys[5]);
  72. }
  73. static void send_mouse(report_mouse_t *report)
  74. {
  75. // wake from deep sleep
  76. PORTD |= (1<<5); // high
  77. wait_ms(5);
  78. PORTD &= ~(1<<5); // low
  79. serial_send(0xFD); // Raw report mode
  80. serial_send(5); // length
  81. serial_send(2); // descriptor type
  82. serial_send(report->buttons);
  83. serial_send(report->x);
  84. serial_send(report->y);
  85. serial_send(report->v);
  86. }
  87. static void send_system(uint16_t data)
  88. {
  89. // Table 5-6 of RN-BT-DATA-UB
  90. // 81,82,83 scan codes can be used?
  91. }
  92. static uint16_t usage2bits(uint16_t usage)
  93. {
  94. switch (usage) {
  95. case AC_HOME: return 0x01;
  96. case AL_EMAIL: return 0x02;
  97. case AC_SEARCH: return 0x04;
  98. //case AL_KBD_LAYOUT: return 0x08; // Apple virtual keybaord toggle
  99. case AUDIO_VOL_UP: return 0x10;
  100. case AUDIO_VOL_DOWN: return 0x20;
  101. case AUDIO_MUTE: return 0x40;
  102. case TRANSPORT_PLAY_PAUSE: return 0x80;
  103. case TRANSPORT_NEXT_TRACK: return 0x100;
  104. case TRANSPORT_PREV_TRACK: return 0x200;
  105. case TRANSPORT_STOP: return 0x400;
  106. case TRANSPORT_STOP_EJECT: return 0x800;
  107. //case return 0x1000; // Fast forward
  108. //case return 0x2000; // Rewind
  109. //case return 0x4000; // Stop/eject
  110. //case return 0x8000; // Internet browser
  111. };
  112. return 0;
  113. }
  114. static void send_consumer(uint16_t data)
  115. {
  116. uint16_t bits = usage2bits(data);
  117. serial_send(0xFD); // Raw report mode
  118. serial_send(3); // length
  119. serial_send(3); // descriptor type
  120. serial_send((bits>>8)&0xFF);
  121. serial_send(bits&0xFF);
  122. }
  123. /* Null driver for config_mode */
  124. static uint8_t config_keyboard_leds(void);
  125. static void config_send_keyboard(report_keyboard_t *report);
  126. static void config_send_mouse(report_mouse_t *report);
  127. static void config_send_system(uint16_t data);
  128. static void config_send_consumer(uint16_t data);
  129. host_driver_t rn42_config_driver = {
  130. config_keyboard_leds,
  131. config_send_keyboard,
  132. config_send_mouse,
  133. config_send_system,
  134. config_send_consumer
  135. };
  136. static uint8_t config_keyboard_leds(void) { return 0; }
  137. static void config_send_keyboard(report_keyboard_t *report) {}
  138. static void config_send_mouse(report_mouse_t *report) {}
  139. static void config_send_system(uint16_t data) {}
  140. static void config_send_consumer(uint16_t data) {}