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