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.

234 lines
6.1 KiB

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