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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. #ifdef SERIAL_SOFT_DEBUG
  59. #define SERIAL_SOFT_DEBUG_INIT() (DDRD |= 1<<7)
  60. #define SERIAL_SOFT_DEBUG_TGL() (PORTD ^= 1<<7)
  61. #else
  62. #define SERIAL_SOFT_DEBUG_INIT()
  63. #define SERIAL_SOFT_DEBUG_TGL()
  64. #endif
  65. void serial_init(void)
  66. {
  67. SERIAL_SOFT_DEBUG_INIT();
  68. SERIAL_SOFT_RXD_INIT();
  69. SERIAL_SOFT_TXD_INIT();
  70. }
  71. /* RX ring buffer */
  72. #define RBUF_SIZE 8
  73. static uint8_t rbuf[RBUF_SIZE];
  74. static uint8_t rbuf_head = 0;
  75. static uint8_t rbuf_tail = 0;
  76. uint8_t serial_recv(void)
  77. {
  78. uint8_t data = 0;
  79. if (rbuf_head == rbuf_tail) {
  80. return 0;
  81. }
  82. data = rbuf[rbuf_tail];
  83. rbuf_tail = (rbuf_tail + 1) % RBUF_SIZE;
  84. return data;
  85. }
  86. int16_t serial_recv2(void)
  87. {
  88. uint8_t data = 0;
  89. if (rbuf_head == rbuf_tail) {
  90. return -1;
  91. }
  92. data = rbuf[rbuf_tail];
  93. rbuf_tail = (rbuf_tail + 1) % RBUF_SIZE;
  94. return data;
  95. }
  96. void serial_send(uint8_t data)
  97. {
  98. /* signal state: IDLE: ON, START: OFF, STOP: ON, DATA0: OFF, DATA1: ON */
  99. #ifdef SERIAL_SOFT_BIT_ORDER_MSB
  100. #ifdef SERIAL_SOFT_DATA_7BIT
  101. uint8_t mask = 0x40;
  102. #else
  103. uint8_t mask = 0x80;
  104. #endif
  105. #else
  106. uint8_t mask = 0x01;
  107. #endif
  108. uint8_t parity = 0;
  109. /* start bit */
  110. SERIAL_SOFT_TXD_OFF();
  111. _delay_us(WAIT_US);
  112. #ifdef SERIAL_SOFT_DATA_7BIT
  113. while (mask&0x7F) {
  114. #else
  115. while (mask&0xFF) {
  116. #endif
  117. if (data&mask) {
  118. SERIAL_SOFT_TXD_ON();
  119. parity ^= 1;
  120. } else {
  121. SERIAL_SOFT_TXD_OFF();
  122. }
  123. _delay_us(WAIT_US);
  124. #ifdef SERIAL_SOFT_BIT_ORDER_MSB
  125. mask >>= 1;
  126. #else
  127. mask <<= 1;
  128. #endif
  129. }
  130. #if defined(SERIAL_SOFT_PARITY_EVEN) || defined(SERIAL_SOFT_PARITY_ODD)
  131. /* to center of parity bit */
  132. if (parity != SERIAL_SOFT_PARITY_VAL) {
  133. SERIAL_SOFT_TXD_ON();
  134. } else {
  135. SERIAL_SOFT_TXD_OFF();
  136. }
  137. _delay_us(WAIT_US);
  138. #endif
  139. /* stop bit */
  140. SERIAL_SOFT_TXD_ON();
  141. _delay_us(WAIT_US);
  142. }
  143. /* detect edge of start bit */
  144. ISR(SERIAL_SOFT_RXD_VECT)
  145. {
  146. SERIAL_SOFT_DEBUG_TGL();
  147. SERIAL_SOFT_RXD_INT_ENTER();
  148. uint8_t data = 0;
  149. #ifdef SERIAL_SOFT_BIT_ORDER_MSB
  150. #ifdef SERIAL_SOFT_DATA_7BIT
  151. uint8_t mask = 0x40;
  152. #else
  153. uint8_t mask = 0x80;
  154. #endif
  155. #else
  156. uint8_t mask = 0x01;
  157. #endif
  158. uint8_t parity = 0;
  159. /* to center of start bit */
  160. _delay_us(WAIT_US/2);
  161. SERIAL_SOFT_DEBUG_TGL();
  162. do {
  163. /* to center of next bit */
  164. _delay_us(WAIT_US);
  165. SERIAL_SOFT_DEBUG_TGL();
  166. if (SERIAL_SOFT_RXD_IN()) {
  167. data |= mask;
  168. parity ^= 1;
  169. }
  170. #ifdef SERIAL_SOFT_BIT_ORDER_MSB
  171. mask >>= 1;
  172. #else
  173. mask <<= 1;
  174. #endif
  175. #ifdef SERIAL_SOFT_DATA_7BIT
  176. } while (mask&0x7F);
  177. #else
  178. } while (mask&0xFF);
  179. #endif
  180. #if defined(SERIAL_SOFT_PARITY_EVEN) || defined(SERIAL_SOFT_PARITY_ODD)
  181. /* to center of parity bit */
  182. _delay_us(WAIT_US);
  183. if (SERIAL_SOFT_RXD_IN()) { parity ^= 1; }
  184. SERIAL_SOFT_DEBUG_TGL();
  185. #endif
  186. /* to center of stop bit */
  187. _delay_us(WAIT_US);
  188. uint8_t next = (rbuf_head + 1) % RBUF_SIZE;
  189. #if defined(SERIAL_SOFT_PARITY_EVEN) || defined(SERIAL_SOFT_PARITY_ODD)
  190. if ((parity == SERIAL_SOFT_PARITY_VAL) && next != rbuf_tail) {
  191. #else
  192. if (next != rbuf_tail) {
  193. #endif
  194. rbuf[rbuf_head] = data;
  195. rbuf_head = next;
  196. }
  197. SERIAL_SOFT_RXD_INT_EXIT();
  198. SERIAL_SOFT_DEBUG_TGL();
  199. }