diff options
| -rw-r--r-- | protocol/serial_soft.c | 39 |
1 files changed, 32 insertions, 7 deletions
diff --git a/protocol/serial_soft.c b/protocol/serial_soft.c index 3c9c914ed..c906c6647 100644 --- a/protocol/serial_soft.c +++ b/protocol/serial_soft.c | |||
| @@ -46,7 +46,7 @@ POSSIBILITY OF SUCH DAMAGE. | |||
| 46 | * is still useful for negative logic signal like Sun protocol not supported by hardware USART. | 46 | * is still useful for negative logic signal like Sun protocol not supported by hardware USART. |
| 47 | */ | 47 | */ |
| 48 | 48 | ||
| 49 | #define WAIT_US (1000000/SERIAL_BAUD) | 49 | #define WAIT_US (1000000L/SERIAL_BAUD) |
| 50 | 50 | ||
| 51 | /* debug for signal timing, see debug pin with oscilloscope */ | 51 | /* debug for signal timing, see debug pin with oscilloscope */ |
| 52 | #ifdef SERIAL_SOFT_DEBUG | 52 | #ifdef SERIAL_SOFT_DEBUG |
| @@ -100,18 +100,33 @@ int16_t serial_recv2(void) | |||
| 100 | void serial_send(uint8_t data) | 100 | void serial_send(uint8_t data) |
| 101 | { | 101 | { |
| 102 | /* signal state: IDLE: ON, START: OFF, STOP: ON, DATA0: OFF, DATA1: ON */ | 102 | /* signal state: IDLE: ON, START: OFF, STOP: ON, DATA0: OFF, DATA1: ON */ |
| 103 | /* start bit */ | ||
| 104 | SERIAL_TXD_OFF(); | ||
| 105 | _delay_us(WAIT_US); | ||
| 106 | 103 | ||
| 107 | #ifdef SERIAL_BIT_ORDER_MSB | 104 | #ifdef SERIAL_BIT_ORDER_MSB |
| 108 | uint8_t mask = 0x80; | 105 | uint8_t mask = 0x80; |
| 109 | #else | 106 | #else |
| 110 | uint8_t mask = 0x01; | 107 | uint8_t mask = 0x01; |
| 111 | #endif | 108 | #endif |
| 109 | |||
| 110 | #ifdef SERIAL_PARITY_ODD | ||
| 111 | uint8_t parity = 1; | ||
| 112 | #elif defined(SERIAL_PARITY_EVEN) | ||
| 113 | uint8_t parity = 0; | ||
| 114 | #endif | ||
| 115 | |||
| 116 | /* start bit */ | ||
| 117 | SERIAL_TXD_OFF(); | ||
| 118 | _delay_us(WAIT_US-2); | ||
| 119 | |||
| 112 | while (mask) { | 120 | while (mask) { |
| 113 | if (data&mask) { SERIAL_TXD_ON(); } else { SERIAL_TXD_OFF(); } | 121 | if (data&mask) { |
| 114 | _delay_us(WAIT_US); | 122 | SERIAL_TXD_ON(); |
| 123 | #if defined(SERIAL_PARITY_EVEN) || defined(SERIAL_PARITY_ODD) | ||
| 124 | parity ^= 1; | ||
| 125 | #endif | ||
| 126 | } else { | ||
| 127 | SERIAL_TXD_OFF(); | ||
| 128 | } | ||
| 129 | _delay_us(WAIT_US-2); | ||
| 115 | 130 | ||
| 116 | #ifdef SERIAL_BIT_ORDER_MSB | 131 | #ifdef SERIAL_BIT_ORDER_MSB |
| 117 | mask >>= 1; | 132 | mask >>= 1; |
| @@ -120,9 +135,19 @@ void serial_send(uint8_t data) | |||
| 120 | #endif | 135 | #endif |
| 121 | } | 136 | } |
| 122 | 137 | ||
| 138 | #if defined(SERIAL_PARITY_EVEN) || defined(SERIAL_PARITY_ODD) | ||
| 139 | /* to center of parity bit */ | ||
| 140 | if (parity) { | ||
| 141 | SERIAL_TXD_ON(); | ||
| 142 | } else { | ||
| 143 | SERIAL_TXD_OFF(); | ||
| 144 | } | ||
| 145 | _delay_us(WAIT_US-2); | ||
| 146 | #endif | ||
| 147 | |||
| 123 | /* stop bit */ | 148 | /* stop bit */ |
| 124 | SERIAL_TXD_ON(); | 149 | SERIAL_TXD_ON(); |
| 125 | _delay_us(WAIT_US); | 150 | _delay_us(WAIT_US-2); |
| 126 | } | 151 | } |
| 127 | 152 | ||
| 128 | /* detect edge of start bit */ | 153 | /* detect edge of start bit */ |
