Keyboard firmwares for Atmel AVR and Cortex-M
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * WARNING: be careful changing this code, it is very timing dependent
  3. */
  4. #ifndef F_CPU
  5. #define F_CPU 16000000
  6. #endif
  7. #include <avr/io.h>
  8. #include <avr/interrupt.h>
  9. #include <util/delay.h>
  10. #include <stdbool.h>
  11. #include "serial.h"
  12. // Serial pulse period in microseconds. Its probably a bad idea to lower this
  13. // value.
  14. #define SERIAL_DELAY 24
  15. uint8_t volatile serial_slave_buffer[SERIAL_SLAVE_BUFFER_LENGTH] = {0};
  16. uint8_t volatile serial_master_buffer[SERIAL_MASTER_BUFFER_LENGTH] = {0};
  17. #define SLAVE_DATA_CORRUPT (1<<0)
  18. volatile uint8_t status = 0;
  19. inline static
  20. void serial_delay(void) {
  21. _delay_us(SERIAL_DELAY);
  22. }
  23. inline static
  24. void serial_output(void) {
  25. SERIAL_PIN_DDR |= SERIAL_PIN_MASK;
  26. }
  27. // make the serial pin an input with pull-up resistor
  28. inline static
  29. void serial_input(void) {
  30. SERIAL_PIN_DDR &= ~SERIAL_PIN_MASK;
  31. SERIAL_PIN_PORT |= SERIAL_PIN_MASK;
  32. }
  33. inline static
  34. uint8_t serial_read_pin(void) {
  35. return !!(SERIAL_PIN_INPUT & SERIAL_PIN_MASK);
  36. }
  37. inline static
  38. void serial_low(void) {
  39. SERIAL_PIN_PORT &= ~SERIAL_PIN_MASK;
  40. }
  41. inline static
  42. void serial_high(void) {
  43. SERIAL_PIN_PORT |= SERIAL_PIN_MASK;
  44. }
  45. void serial_master_init(void) {
  46. serial_output();
  47. serial_high();
  48. }
  49. void serial_slave_init(void) {
  50. serial_input();
  51. // Enable INT0
  52. EIMSK |= _BV(INT0);
  53. // Trigger on falling edge of INT0
  54. EICRA &= ~(_BV(ISC00) | _BV(ISC01));
  55. }
  56. // Used by the master to synchronize timing with the slave.
  57. static
  58. void sync_recv(void) {
  59. serial_input();
  60. // This shouldn't hang if the slave disconnects because the
  61. // serial line will float to high if the slave does disconnect.
  62. while (!serial_read_pin());
  63. serial_delay();
  64. }
  65. // Used by the slave to send a synchronization signal to the master.
  66. static
  67. void sync_send(void) {
  68. serial_output();
  69. serial_low();
  70. serial_delay();
  71. serial_high();
  72. }
  73. // Reads a byte from the serial line
  74. static
  75. uint8_t serial_read_byte(void) {
  76. uint8_t byte = 0;
  77. serial_input();
  78. for ( uint8_t i = 0; i < 8; ++i) {
  79. byte = (byte << 1) | serial_read_pin();
  80. serial_delay();
  81. _delay_us(1);
  82. }
  83. return byte;
  84. }
  85. // Sends a byte with MSB ordering
  86. static
  87. void serial_write_byte(uint8_t data) {
  88. uint8_t b = 8;
  89. serial_output();
  90. while( b-- ) {
  91. if(data & (1 << b)) {
  92. serial_high();
  93. } else {
  94. serial_low();
  95. }
  96. serial_delay();
  97. }
  98. }
  99. // interrupt handle to be used by the slave device
  100. ISR(SERIAL_PIN_INTERRUPT) {
  101. sync_send();
  102. uint8_t checksum = 0;
  103. for (int i = 0; i < SERIAL_SLAVE_BUFFER_LENGTH; ++i) {
  104. serial_write_byte(serial_slave_buffer[i]);
  105. sync_send();
  106. checksum += serial_slave_buffer[i];
  107. }
  108. serial_write_byte(checksum);
  109. sync_send();
  110. // wait for the sync to finish sending
  111. serial_delay();
  112. // read the middle of pulses
  113. _delay_us(SERIAL_DELAY/2);
  114. uint8_t checksum_computed = 0;
  115. for (int i = 0; i < SERIAL_MASTER_BUFFER_LENGTH; ++i) {
  116. serial_master_buffer[i] = serial_read_byte();
  117. sync_send();
  118. checksum_computed += serial_master_buffer[i];
  119. }
  120. uint8_t checksum_received = serial_read_byte();
  121. sync_send();
  122. serial_input(); // end transaction
  123. if ( checksum_computed != checksum_received ) {
  124. status |= SLAVE_DATA_CORRUPT;
  125. } else {
  126. status &= ~SLAVE_DATA_CORRUPT;
  127. }
  128. }
  129. inline
  130. bool serial_slave_DATA_CORRUPT(void) {
  131. return status & SLAVE_DATA_CORRUPT;
  132. }
  133. // Copies the serial_slave_buffer to the master and sends the
  134. // serial_master_buffer to the slave.
  135. //
  136. // Returns:
  137. // 0 => no error
  138. // 1 => slave did not respond
  139. int serial_update_buffers(void) {
  140. // this code is very time dependent, so we need to disable interrupts
  141. cli();
  142. // signal to the slave that we want to start a transaction
  143. serial_output();
  144. serial_low();
  145. _delay_us(1);
  146. // wait for the slaves response
  147. serial_input();
  148. serial_high();
  149. _delay_us(SERIAL_DELAY);
  150. // check if the slave is present
  151. if (serial_read_pin()) {
  152. // slave failed to pull the line low, assume not present
  153. sei();
  154. return 1;
  155. }
  156. // if the slave is present syncronize with it
  157. sync_recv();
  158. uint8_t checksum_computed = 0;
  159. // receive data from the slave
  160. for (int i = 0; i < SERIAL_SLAVE_BUFFER_LENGTH; ++i) {
  161. serial_slave_buffer[i] = serial_read_byte();
  162. sync_recv();
  163. checksum_computed += serial_slave_buffer[i];
  164. }
  165. uint8_t checksum_received = serial_read_byte();
  166. sync_recv();
  167. if (checksum_computed != checksum_received) {
  168. sei();
  169. return 1;
  170. }
  171. uint8_t checksum = 0;
  172. // send data to the slave
  173. for (int i = 0; i < SERIAL_MASTER_BUFFER_LENGTH; ++i) {
  174. serial_write_byte(serial_master_buffer[i]);
  175. sync_recv();
  176. checksum += serial_master_buffer[i];
  177. }
  178. serial_write_byte(checksum);
  179. sync_recv();
  180. // always, release the line when not in use
  181. serial_output();
  182. serial_high();
  183. sei();
  184. return 0;
  185. }