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.

serial_soft.c 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. Copyright 2012 Jun WAKO <[email protected]>
  3. This software is licensed with a Modified BSD License.
  4. All of this is supposed to be Free Software, Open Source, DFSG-free,
  5. GPL-compatible, and OK to use in both free and proprietary applications.
  6. Additions and corrections to this file are welcome.
  7. Redistribution and use in source and binary forms, with or without
  8. modification, are permitted provided that the following conditions are met:
  9. * Redistributions of source code must retain the above copyright
  10. notice, this list of conditions and the following disclaimer.
  11. * Redistributions in binary form must reproduce the above copyright
  12. notice, this list of conditions and the following disclaimer in
  13. the documentation and/or other materials provided with the
  14. distribution.
  15. * Neither the name of the copyright holders nor the names of
  16. contributors may be used to endorse or promote products derived
  17. from this software without specific prior written permission.
  18. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  19. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  22. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  23. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  24. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  25. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  26. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #include <stdbool.h>
  31. #include <avr/io.h>
  32. #include <avr/interrupt.h>
  33. #include <util/delay.h>
  34. #include "serial.h"
  35. /*
  36. * Stupid Inefficient Busy-wait Software Serial
  37. * which is still useful for negative logic signal like Sun protocol
  38. * if it is not supported by hardware UART.
  39. *
  40. * TODO: delay is not accurate enough. Instruction cycle should be counted and inline assemby is needed.
  41. */
  42. #define WAIT_US (1000000L/SERIAL_SOFT_BAUD)
  43. #ifdef SERIAL_SOFT_LOGIC_NEGATIVE
  44. #define SERIAL_SOFT_RXD_IN() !(SERIAL_SOFT_RXD_READ())
  45. #define SERIAL_SOFT_TXD_ON() SERIAL_SOFT_TXD_LO()
  46. #define SERIAL_SOFT_TXD_OFF() SERIAL_SOFT_TXD_HI()
  47. #else
  48. #define SERIAL_SOFT_RXD_IN() !!(SERIAL_SOFT_RXD_READ())
  49. #define SERIAL_SOFT_TXD_ON() SERIAL_SOFT_TXD_HI()
  50. #define SERIAL_SOFT_TXD_OFF() SERIAL_SOFT_TXD_LO()
  51. #endif
  52. #ifdef SERIAL_SOFT_PARITY_EVEN
  53. #define SERIAL_SOFT_PARITY_VAL 0
  54. #elif defined(SERIAL_SOFT_PARITY_ODD)
  55. #define SERIAL_SOFT_PARITY_VAL 1
  56. #endif
  57. /* debug for signal timing, see debug pin with oscilloscope */
  58. #define SERIAL_SOFT_DEBUG
  59. #ifdef SERIAL_SOFT_DEBUG
  60. #define SERIAL_SOFT_DEBUG_INIT() (DDRD |= 1<<7)
  61. #define SERIAL_SOFT_DEBUG_TGL() (PORTD ^= 1<<7)
  62. #else
  63. #define SERIAL_SOFT_DEBUG_INIT()
  64. #define SERIAL_SOFT_DEBUG_TGL()
  65. #endif
  66. void serial_init(void)
  67. {
  68. SERIAL_SOFT_DEBUG_INIT();
  69. SERIAL_SOFT_RXD_INIT();
  70. SERIAL_SOFT_TXD_INIT();
  71. }
  72. /* RX ring buffer */
  73. #define RBUF_SIZE 8
  74. static uint8_t rbuf[RBUF_SIZE];
  75. static uint8_t rbuf_head = 0;
  76. static uint8_t rbuf_tail = 0;
  77. uint8_t serial_recv(void)
  78. {
  79. uint8_t data = 0;
  80. if (rbuf_head == rbuf_tail) {
  81. return 0;
  82. }
  83. data = rbuf[rbuf_tail];
  84. rbuf_tail = (rbuf_tail + 1) % RBUF_SIZE;
  85. return data;
  86. }
  87. int16_t serial_recv2(void)
  88. {
  89. uint8_t data = 0;
  90. if (rbuf_head == rbuf_tail) {
  91. return -1;
  92. }
  93. data = rbuf[rbuf_tail];
  94. rbuf_tail = (rbuf_tail + 1) % RBUF_SIZE;
  95. return data;
  96. }
  97. void serial_send(uint8_t data)
  98. {
  99. /* signal state: IDLE: ON, START: OFF, STOP: ON, DATA0: OFF, DATA1: ON */
  100. #ifdef SERIAL_SOFT_BIT_ORDER_MSB
  101. #ifdef SERIAL_SOFT_DATA_7BIT
  102. uint8_t mask = 0x40;
  103. #else
  104. uint8_t mask = 0x80;
  105. #endif
  106. #else
  107. uint8_t mask = 0x01;
  108. #endif
  109. uint8_t parity = 0;
  110. /* start bit */
  111. SERIAL_SOFT_TXD_OFF();
  112. _delay_us(WAIT_US);
  113. #ifdef SERIAL_SOFT_DATA_7BIT
  114. while (mask&0x7F) {
  115. #else
  116. while (mask&0xFF) {
  117. #endif
  118. if (data&mask) {
  119. SERIAL_SOFT_TXD_ON();
  120. parity ^= 1;
  121. } else {
  122. SERIAL_SOFT_TXD_OFF();
  123. }
  124. _delay_us(WAIT_US);
  125. #ifdef SERIAL_SOFT_BIT_ORDER_MSB
  126. mask >>= 1;
  127. #else
  128. mask <<= 1;
  129. #endif
  130. }
  131. #if defined(SERIAL_SOFT_PARITY_EVEN) || defined(SERIAL_SOFT_PARITY_ODD)
  132. /* to center of parity bit */
  133. if (parity != SERIAL_SOFT_PARITY_VAL) {
  134. SERIAL_SOFT_TXD_ON();
  135. } else {
  136. SERIAL_SOFT_TXD_OFF();
  137. }
  138. _delay_us(WAIT_US);
  139. #endif
  140. /* stop bit */
  141. SERIAL_SOFT_TXD_ON();
  142. _delay_us(WAIT_US);
  143. }
  144. /* detect edge of start bit */
  145. ISR(SERIAL_SOFT_RXD_VECT)
  146. {
  147. SERIAL_SOFT_DEBUG_TGL();
  148. SERIAL_SOFT_RXD_INT_ENTER()
  149. uint8_t data = 0;
  150. #ifdef SERIAL_SOFT_BIT_ORDER_MSB
  151. #ifdef SERIAL_SOFT_DATA_7BIT
  152. uint8_t mask = 0x40;
  153. #else
  154. uint8_t mask = 0x80;
  155. #endif
  156. #else
  157. uint8_t mask = 0x01;
  158. #endif
  159. uint8_t parity = 0;
  160. /* to center of start bit */
  161. _delay_us(WAIT_US/2);
  162. SERIAL_SOFT_DEBUG_TGL();
  163. do {
  164. /* to center of next bit */
  165. _delay_us(WAIT_US);
  166. SERIAL_SOFT_DEBUG_TGL();
  167. if (SERIAL_SOFT_RXD_IN()) {
  168. data |= mask;
  169. parity ^= 1;
  170. }
  171. #ifdef SERIAL_SOFT_BIT_ORDER_MSB
  172. mask >>= 1;
  173. #else
  174. mask <<= 1;
  175. #endif
  176. #ifdef SERIAL_SOFT_DATA_7BIT
  177. } while (mask&0x7F);
  178. #else
  179. } while (mask&0xFF);
  180. #endif
  181. #if defined(SERIAL_SOFT_PARITY_EVEN) || defined(SERIAL_SOFT_PARITY_ODD)
  182. /* to center of parity bit */
  183. _delay_us(WAIT_US);
  184. if (SERIAL_SOFT_RXD_IN()) { parity ^= 1; }
  185. SERIAL_SOFT_DEBUG_TGL();
  186. #endif
  187. /* to center of stop bit */
  188. _delay_us(WAIT_US);
  189. uint8_t next = (rbuf_head + 1) % RBUF_SIZE;
  190. #if defined(SERIAL_SOFT_PARITY_EVEN) || defined(SERIAL_SOFT_PARITY_ODD)
  191. if ((parity == SERIAL_SOFT_PARITY_VAL) && next != rbuf_tail) {
  192. #else
  193. if (next != rbuf_tail) {
  194. #endif
  195. rbuf[rbuf_head] = data;
  196. rbuf_head = next;
  197. }
  198. SERIAL_SOFT_RXD_INT_EXIT();
  199. SERIAL_SOFT_DEBUG_TGL();
  200. }