adamdunkels | cbb42c3 | 2003-08-20 22:51:48 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2001, Adam Dunkels. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
adamdunkels | fb24dff | 2004-09-12 14:07:30 +0000 | [diff] [blame] | 13 | * 3. The name of the author may not be used to endorse or promote |
adamdunkels | cbb42c3 | 2003-08-20 22:51:48 +0000 | [diff] [blame] | 14 | * products derived from this software without specific prior |
| 15 | * written permission. |
| 16 | * |
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS |
| 18 | * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 19 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 21 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
| 23 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 24 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 25 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 26 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 27 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | * |
| 29 | * This file is part of the uIP TCP/IP stack. |
| 30 | * |
oliverschmidt | 4affe76 | 2005-02-23 22:43:00 +0000 | [diff] [blame] | 31 | * $Id: rs232dev-ss.c,v 1.4 2005/02/23 22:43:00 oliverschmidt Exp $ |
adamdunkels | cbb42c3 | 2003-08-20 22:51:48 +0000 | [diff] [blame] | 32 | * |
| 33 | */ |
| 34 | |
| 35 | /* |
| 36 | * This is a generic implementation of the SLIP protocol over an RS232 |
| 37 | * (serial) device. While initially intented for the C64, the code can |
| 38 | * easily be ported to other platforms as well. |
| 39 | * |
| 40 | * Huge thanks to Ullrich von Bassewitz <uz@cc65.org> of cc65 fame for |
| 41 | * and endless supply of bugfixes, insightsful comments and |
| 42 | * suggestions, and improvements to this code! |
| 43 | */ |
| 44 | |
| 45 | #include "rs232silversurfer.h" |
| 46 | #include <time.h> |
| 47 | #include <string.h> |
| 48 | |
| 49 | /* This will include the system specific header files as well */ |
| 50 | #if defined(__CBM__) |
| 51 | # include <cbm.h> |
| 52 | #elif defined(__ATARI__) |
| 53 | # include <atari.h> |
| 54 | #endif |
| 55 | |
| 56 | #include "uip.h" |
| 57 | |
| 58 | #define SLIP_END 0300 |
| 59 | #define SLIP_ESC 0333 |
| 60 | #define SLIP_ESC_END 0334 |
| 61 | #define SLIP_ESC_ESC 0335 |
| 62 | |
| 63 | |
| 64 | #define SIO_RECV(c) while(rs232_get(&c) == RS_ERR_NO_DATA) |
| 65 | #define SIO_POLL(c) (rs232_get(&c) != RS_ERR_NO_DATA) |
| 66 | #define SIO_SEND(c) while(rs232_put(c) == RS_ERR_OVERFLOW) |
| 67 | |
| 68 | #define MAX_SIZE (UIP_BUFSIZE - UIP_LLH_LEN) |
| 69 | |
| 70 | static u8_t slip_buf[MAX_SIZE + 2]; |
| 71 | |
adamdunkels | cbb42c3 | 2003-08-20 22:51:48 +0000 | [diff] [blame] | 72 | static u16_t len, tmplen; |
adamdunkels | cbb42c3 | 2003-08-20 22:51:48 +0000 | [diff] [blame] | 73 | |
| 74 | #if 1 |
| 75 | #define printf(x) |
| 76 | #else |
| 77 | #include <stdio.h> |
| 78 | #endif |
| 79 | |
| 80 | |
| 81 | /*-----------------------------------------------------------------------------------*/ |
| 82 | static void |
| 83 | rs232_err(char err) |
| 84 | { |
| 85 | switch(err) { |
| 86 | case RS_ERR_OK: |
| 87 | printf("RS232 OK\n"); |
| 88 | break; |
| 89 | case RS_ERR_NOT_INITIALIZED: |
| 90 | printf("RS232 not initialized\n"); |
| 91 | break; |
| 92 | case RS_ERR_BAUD_TOO_FAST: |
| 93 | printf("RS232 baud too fast\n"); |
| 94 | break; |
| 95 | case RS_ERR_BAUD_NOT_AVAIL: |
| 96 | printf("RS232 baud rate not available\n"); |
| 97 | break; |
| 98 | case RS_ERR_NO_DATA: |
| 99 | printf("RS232 nothing to read\n"); |
| 100 | break; |
| 101 | case RS_ERR_OVERFLOW: |
| 102 | printf("RS232 overflow\n"); |
| 103 | break; |
| 104 | } |
| 105 | |
| 106 | } |
| 107 | /*-----------------------------------------------------------------------------------*/ |
| 108 | /* |
| 109 | * rs232dev_send(): |
| 110 | * |
| 111 | * Sends the packet in the uip_buf and uip_appdata buffers. The first |
| 112 | * 40 bytes of the packet (the IP and TCP headers) are read from the |
| 113 | * uip_buf buffer, and the following bytes (the application data) are |
| 114 | * read from the uip_appdata buffer. |
| 115 | * |
| 116 | */ |
| 117 | /*-----------------------------------------------------------------------------------*/ |
| 118 | void |
| 119 | rs232dev_send(void) |
| 120 | { |
adamdunkels | cbb42c3 | 2003-08-20 22:51:48 +0000 | [diff] [blame] | 121 | u16_t i; |
adamdunkels | cbb42c3 | 2003-08-20 22:51:48 +0000 | [diff] [blame] | 122 | u8_t *ptr; |
| 123 | u8_t c; |
| 124 | |
| 125 | SIO_SEND(SLIP_END); |
| 126 | |
| 127 | ptr = &uip_buf[UIP_LLH_LEN]; |
| 128 | for(i = 0; i < uip_len; ++i) { |
oliverschmidt | 4affe76 | 2005-02-23 22:43:00 +0000 | [diff] [blame] | 129 | if(i == UIP_TCPIP_HLEN) { |
adamdunkels | cbb42c3 | 2003-08-20 22:51:48 +0000 | [diff] [blame] | 130 | ptr = uip_appdata; |
| 131 | } |
| 132 | c = *ptr++; |
| 133 | switch(c) { |
| 134 | case SLIP_END: |
| 135 | SIO_SEND(SLIP_ESC); |
| 136 | SIO_SEND(SLIP_ESC_END); |
| 137 | break; |
| 138 | case SLIP_ESC: |
| 139 | SIO_SEND(SLIP_ESC); |
| 140 | SIO_SEND(SLIP_ESC_ESC); |
| 141 | break; |
| 142 | default: |
| 143 | SIO_SEND(c); |
| 144 | break; |
| 145 | } |
| 146 | } |
| 147 | SIO_SEND(SLIP_END); |
| 148 | } |
| 149 | /*-----------------------------------------------------------------------------------*/ |
| 150 | /* |
| 151 | * rs232dev_poll(): |
| 152 | * |
| 153 | * Read all avaliable bytes from the RS232 interface into the slip_buf |
| 154 | * buffer. If no more bytes are avaliable, it returns with 0 to |
| 155 | * indicate that no packet was immediately ready. When a full packet |
| 156 | * has been read into the buffer, the packet is copied into the |
| 157 | * uip_buf buffer and the length of the packet is returned. |
| 158 | * |
| 159 | */ |
| 160 | /*-----------------------------------------------------------------------------------*/ |
adamdunkels | cbb42c3 | 2003-08-20 22:51:48 +0000 | [diff] [blame] | 161 | u16_t |
adamdunkels | cbb42c3 | 2003-08-20 22:51:48 +0000 | [diff] [blame] | 162 | rs232dev_poll(void) |
| 163 | { |
| 164 | u8_t c; |
| 165 | static u8_t lastc; |
| 166 | |
| 167 | while(SIO_POLL(c)) { |
| 168 | /* printf("c %x\n", c);*/ |
| 169 | switch(c) { |
| 170 | case SLIP_ESC: |
| 171 | lastc = c; |
| 172 | break; |
| 173 | |
| 174 | case SLIP_END: |
| 175 | lastc = c; |
| 176 | /* End marker found, we copy our input buffer to the uip_buf |
| 177 | buffer and return the size of the packet we copied. */ |
| 178 | memcpy(&uip_buf[UIP_LLH_LEN], slip_buf, len); |
| 179 | tmplen = len; |
| 180 | len = 0; |
| 181 | return tmplen; |
| 182 | |
| 183 | default: |
| 184 | if(lastc == SLIP_ESC) { |
| 185 | lastc = c; |
| 186 | /* Previous read byte was an escape byte, so this byte will be |
| 187 | interpreted differently from others. */ |
| 188 | switch(c) { |
| 189 | case SLIP_ESC_END: |
| 190 | c = SLIP_END; |
| 191 | break; |
| 192 | case SLIP_ESC_ESC: |
| 193 | c = SLIP_ESC; |
| 194 | break; |
| 195 | } |
| 196 | } else { |
| 197 | lastc = c; |
| 198 | } |
| 199 | |
| 200 | |
| 201 | slip_buf[len] = c; |
| 202 | ++len; |
| 203 | |
| 204 | if(len > MAX_SIZE) { |
| 205 | len = 0; |
| 206 | } |
| 207 | |
| 208 | break; |
| 209 | } |
| 210 | } |
| 211 | return 0; |
| 212 | } |
| 213 | /*-----------------------------------------------------------------------------------*/ |
| 214 | /* |
| 215 | * rs232dev_init(): |
| 216 | * |
| 217 | * Initializes the RS232 device and sets the parameters of the device. |
| 218 | * |
| 219 | */ |
| 220 | /*-----------------------------------------------------------------------------------*/ |
| 221 | void |
| 222 | rs232dev_init(void) |
| 223 | { |
| 224 | char err; |
| 225 | |
| 226 | err = rs232_init(0); |
| 227 | rs232_err(err); |
| 228 | err = rs232_params(RS_BAUD_9600 | RS_BITS_8 | RS_STOP_1, RS_PAR_NONE); |
| 229 | rs232_err(err); |
| 230 | |
| 231 | len = 0; |
| 232 | |
| 233 | return; |
| 234 | } |
| 235 | /*-----------------------------------------------------------------------------------*/ |
| 236 | |