diff options
Diffstat (limited to 'protocol')
| -rw-r--r-- | protocol/serial.h | 1 | ||||
| -rw-r--r-- | protocol/serial_soft.c | 57 |
2 files changed, 42 insertions, 16 deletions
diff --git a/protocol/serial.h b/protocol/serial.h index 93b9ee922..bd071bec9 100644 --- a/protocol/serial.h +++ b/protocol/serial.h | |||
| @@ -41,5 +41,6 @@ POSSIBILITY OF SUCH DAMAGE. | |||
| 41 | /* host role */ | 41 | /* host role */ |
| 42 | void serial_init(void); | 42 | void serial_init(void); |
| 43 | uint8_t serial_recv(void); | 43 | uint8_t serial_recv(void); |
| 44 | void serial_send(uint8_t data); | ||
| 44 | 45 | ||
| 45 | #endif | 46 | #endif |
diff --git a/protocol/serial_soft.c b/protocol/serial_soft.c index b7d06b644..beddc353c 100644 --- a/protocol/serial_soft.c +++ b/protocol/serial_soft.c | |||
| @@ -51,9 +51,10 @@ POSSIBILITY OF SUCH DAMAGE. | |||
| 51 | void serial_init(void) | 51 | void serial_init(void) |
| 52 | { | 52 | { |
| 53 | SERIAL_RXD_INIT(); | 53 | SERIAL_RXD_INIT(); |
| 54 | SERIAL_TXD_INIT(); | ||
| 54 | } | 55 | } |
| 55 | 56 | ||
| 56 | // RX ring buffer | 57 | /* RX ring buffer */ |
| 57 | #define RBUF_SIZE 8 | 58 | #define RBUF_SIZE 8 |
| 58 | static uint8_t rbuf[RBUF_SIZE]; | 59 | static uint8_t rbuf[RBUF_SIZE]; |
| 59 | static uint8_t rbuf_head = 0; | 60 | static uint8_t rbuf_head = 0; |
| @@ -71,39 +72,63 @@ uint8_t serial_recv(void) | |||
| 71 | return data; | 72 | return data; |
| 72 | } | 73 | } |
| 73 | 74 | ||
| 74 | //ISR(INT2_vect) | 75 | void serial_send(uint8_t data) |
| 76 | { | ||
| 77 | /* signal state: IDLE: ON, START: OFF, STOP: ON, DATA0: OFF, DATA1: ON */ | ||
| 78 | /* start bit */ | ||
| 79 | SERIAL_TXD_OFF(); | ||
| 80 | _delay_us(WAIT_US); | ||
| 81 | |||
| 82 | #ifdef SERIAL_BIT_ORDER_MSB | ||
| 83 | uint8_t mask = 0x80; | ||
| 84 | #else | ||
| 85 | uint8_t mask = 0x01; | ||
| 86 | #endif | ||
| 87 | while (mask) { | ||
| 88 | if (data&mask) { SERIAL_TXD_ON(); } else { SERIAL_TXD_OFF(); } | ||
| 89 | _delay_us(WAIT_US); | ||
| 90 | |||
| 91 | #ifdef SERIAL_BIT_ORDER_MSB | ||
| 92 | mask >>= 1; | ||
| 93 | #else | ||
| 94 | mask <<= 1; | ||
| 95 | #endif | ||
| 96 | } | ||
| 97 | |||
| 98 | /* stop bit */ | ||
| 99 | SERIAL_TXD_ON(); | ||
| 100 | _delay_us(WAIT_US); | ||
| 101 | } | ||
| 102 | |||
| 103 | /* detect edge of start bit */ | ||
| 75 | ISR(SERIAL_RXD_VECT) | 104 | ISR(SERIAL_RXD_VECT) |
| 76 | { | 105 | { |
| 77 | SERIAL_RXD_INT_ENTER() | 106 | SERIAL_RXD_INT_ENTER() |
| 78 | 107 | ||
| 79 | uint8_t data = 0; | 108 | uint8_t data = 0; |
| 80 | #ifdef SERIAL_BIT_ORDER_MSB | 109 | #ifdef SERIAL_BIT_ORDER_MSB |
| 81 | uint8_t pos = 0x80; | 110 | uint8_t mask = 0x80; |
| 82 | #else | 111 | #else |
| 83 | uint8_t pos = 0x01; | 112 | uint8_t mask = 0x01; |
| 84 | #endif | 113 | #endif |
| 85 | // to center of start bit | 114 | /* to center of start bit */ |
| 86 | _delay_us(WAIT_US/2); | 115 | _delay_us(WAIT_US/2); |
| 87 | do { | 116 | do { |
| 88 | // to center of next bit | 117 | /* to center of next bit */ |
| 89 | _delay_us(WAIT_US); | 118 | _delay_us(WAIT_US); |
| 90 | 119 | ||
| 91 | if (SERIAL_RXD_PIN&(1<<SERIAL_RXD_BIT)) { | 120 | if (SERIAL_RXD_READ()) { |
| 92 | data |= pos; | 121 | data |= mask; |
| 93 | } | 122 | } |
| 94 | #ifdef SERIAL_BIT_ORDER_MSB | 123 | #ifdef SERIAL_BIT_ORDER_MSB |
| 95 | pos >>= 1; | 124 | mask >>= 1; |
| 96 | #else | 125 | #else |
| 97 | pos <<= 1; | 126 | mask <<= 1; |
| 98 | #endif | 127 | #endif |
| 99 | } while (pos); | 128 | } while (mask); |
| 100 | // to center of stop bit | 129 | /* to center of stop bit */ |
| 101 | _delay_us(WAIT_US); | 130 | _delay_us(WAIT_US); |
| 102 | 131 | ||
| 103 | #ifdef SERIAL_NEGATIVE_LOGIC | ||
| 104 | data = ~data; | ||
| 105 | #endif | ||
| 106 | |||
| 107 | uint8_t next = (rbuf_head + 1) % RBUF_SIZE; | 132 | uint8_t next = (rbuf_head + 1) % RBUF_SIZE; |
| 108 | if (next != rbuf_tail) { | 133 | if (next != rbuf_tail) { |
| 109 | rbuf[rbuf_head] = data; | 134 | rbuf[rbuf_head] = data; |
