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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

serial_soft.c 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. * is still useful for negative logic signal like Sun protocol not supported by hardware USART.
  38. */
  39. #define WAIT_US (1000000/SERIAL_BAUD)
  40. void serial_init(void)
  41. {
  42. SERIAL_RXD_INIT();
  43. SERIAL_TXD_INIT();
  44. }
  45. /* RX ring buffer */
  46. #define RBUF_SIZE 8
  47. static uint8_t rbuf[RBUF_SIZE];
  48. static uint8_t rbuf_head = 0;
  49. static uint8_t rbuf_tail = 0;
  50. uint8_t serial_recv(void)
  51. {
  52. uint8_t data = 0;
  53. if (rbuf_head == rbuf_tail) {
  54. return 0;
  55. }
  56. data = rbuf[rbuf_tail];
  57. rbuf_tail = (rbuf_tail + 1) % RBUF_SIZE;
  58. return data;
  59. }
  60. void serial_send(uint8_t data)
  61. {
  62. /* signal state: IDLE: ON, START: OFF, STOP: ON, DATA0: OFF, DATA1: ON */
  63. /* start bit */
  64. SERIAL_TXD_OFF();
  65. _delay_us(WAIT_US);
  66. #ifdef SERIAL_BIT_ORDER_MSB
  67. uint8_t mask = 0x80;
  68. #else
  69. uint8_t mask = 0x01;
  70. #endif
  71. while (mask) {
  72. if (data&mask) { SERIAL_TXD_ON(); } else { SERIAL_TXD_OFF(); }
  73. _delay_us(WAIT_US);
  74. #ifdef SERIAL_BIT_ORDER_MSB
  75. mask >>= 1;
  76. #else
  77. mask <<= 1;
  78. #endif
  79. }
  80. /* stop bit */
  81. SERIAL_TXD_ON();
  82. _delay_us(WAIT_US);
  83. }
  84. /* detect edge of start bit */
  85. ISR(SERIAL_RXD_VECT)
  86. {
  87. SERIAL_RXD_INT_ENTER()
  88. uint8_t data = 0;
  89. #ifdef SERIAL_BIT_ORDER_MSB
  90. uint8_t mask = 0x80;
  91. #else
  92. uint8_t mask = 0x01;
  93. #endif
  94. #ifdef SERIAL_PARITY_ODD
  95. uint8_t parity = 0;
  96. #else
  97. uint8_t parity = 1;
  98. #endif
  99. /* to center of start bit */
  100. _delay_us(WAIT_US/2);
  101. do {
  102. /* to center of next bit */
  103. _delay_us(WAIT_US);
  104. if (SERIAL_RXD_READ()) {
  105. data |= mask;
  106. parity ^= 1;
  107. }
  108. #ifdef SERIAL_BIT_ORDER_MSB
  109. mask >>= 1;
  110. #else
  111. mask <<= 1;
  112. #endif
  113. } while (mask);
  114. /* to center of parity bit */
  115. _delay_us(WAIT_US);
  116. parity ^= SERIAL_RXD_READ();
  117. /* to center of stop bit */
  118. _delay_us(WAIT_US);
  119. _delay_us(WAIT_US/2);
  120. parity = 1;
  121. uint8_t next = (rbuf_head + 1) % RBUF_SIZE;
  122. if (parity && next != rbuf_tail) {
  123. rbuf[rbuf_head] = data;
  124. rbuf_head = next;
  125. }
  126. SERIAL_RXD_INT_EXIT();
  127. }