kthacker | 62e146c | 2006-04-17 15:11:35 +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. |
| 13 | * 3. The name of the author may not be used to endorse or promote |
| 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 | * |
| 31 | * $Id: rs232dev.c,v 1.1 2006/04/17 15:12:01 kthacker Exp $ |
| 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 <serial.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(ser_get(&c) == SER_ERR_NO_DATA) |
| 65 | #define SIO_POLL(c) (ser_get(&c) != SER_ERR_NO_DATA) |
| 66 | #define SIO_SEND(c) ser_put(c) |
| 67 | |
| 68 | #define MAX_SIZE (UIP_BUFSIZE - UIP_LLH_LEN) |
| 69 | |
| 70 | static u8_t slip_buf[MAX_SIZE + 2]; |
| 71 | |
| 72 | static u16_t len, tmplen; |
| 73 | |
| 74 | static char loaded = 0; |
| 75 | |
| 76 | #if 1 |
| 77 | #define printf(x) |
| 78 | #else |
| 79 | #include <stdio.h> |
| 80 | #endif |
| 81 | |
| 82 | |
| 83 | /*-----------------------------------------------------------------------------------*/ |
| 84 | /*static void |
| 85 | rs232_err(char err) |
| 86 | { |
| 87 | switch(err) { |
| 88 | case RS_ERR_OK: |
| 89 | printf("RS232 OK\n"); |
| 90 | break; |
| 91 | case RS_ERR_NOT_INITIALIZED: |
| 92 | printf("RS232 not initialized\n"); |
| 93 | break; |
| 94 | case RS_ERR_BAUD_TOO_FAST: |
| 95 | printf("RS232 baud too fast\n"); |
| 96 | break; |
| 97 | case RS_ERR_BAUD_NOT_AVAIL: |
| 98 | printf("RS232 baud rate not available\n"); |
| 99 | break; |
| 100 | case RS_ERR_NO_DATA: |
| 101 | printf("RS232 nothing to read\n"); |
| 102 | break; |
| 103 | case RS_ERR_OVERFLOW: |
| 104 | printf("RS232 overflow\n"); |
| 105 | break; |
| 106 | } |
| 107 | }*/ |
| 108 | /*-----------------------------------------------------------------------------------*/ |
| 109 | /* |
| 110 | * rs232dev_send(): |
| 111 | * |
| 112 | * Sends the packet in the uip_buf and uip_appdata buffers. The first |
| 113 | * 40 bytes of the packet (the IP and TCP headers) are read from the |
| 114 | * uip_buf buffer, and the following bytes (the application data) are |
| 115 | * read from the uip_appdata buffer. |
| 116 | * |
| 117 | */ |
| 118 | /*-----------------------------------------------------------------------------------*/ |
| 119 | void |
| 120 | rs232dev_send(void) |
| 121 | { |
| 122 | u16_t i; |
| 123 | u8_t *ptr; |
| 124 | u8_t c; |
| 125 | |
| 126 | SIO_SEND(SLIP_END); |
| 127 | |
| 128 | ptr = &uip_buf[UIP_LLH_LEN]; |
| 129 | for(i = 0; i < uip_len; ++i) { |
| 130 | if(i == 40) { |
| 131 | ptr = uip_appdata; |
| 132 | } |
| 133 | c = *ptr++; |
| 134 | switch(c) { |
| 135 | case SLIP_END: |
| 136 | SIO_SEND(SLIP_ESC); |
| 137 | SIO_SEND(SLIP_ESC_END); |
| 138 | break; |
| 139 | case SLIP_ESC: |
| 140 | SIO_SEND(SLIP_ESC); |
| 141 | SIO_SEND(SLIP_ESC_ESC); |
| 142 | break; |
| 143 | default: |
| 144 | SIO_SEND(c); |
| 145 | break; |
| 146 | } |
| 147 | } |
| 148 | SIO_SEND(SLIP_END); |
| 149 | } |
| 150 | /*-----------------------------------------------------------------------------------*/ |
| 151 | /* |
| 152 | * rs232dev_poll(): |
| 153 | * |
| 154 | * Read all avaliable bytes from the RS232 interface into the slip_buf |
| 155 | * buffer. If no more bytes are avaliable, it returns with 0 to |
| 156 | * indicate that no packet was immediately ready. When a full packet |
| 157 | * has been read into the buffer, the packet is copied into the |
| 158 | * uip_buf buffer and the length of the packet is returned. |
| 159 | * |
| 160 | */ |
| 161 | /*-----------------------------------------------------------------------------------*/ |
| 162 | u16_t |
| 163 | rs232dev_poll(void) |
| 164 | { |
| 165 | u8_t c; |
| 166 | static u8_t lastc; |
| 167 | |
| 168 | if(loaded == 0) { |
| 169 | return 0; |
| 170 | } |
| 171 | |
| 172 | while(SIO_POLL(c)) { |
| 173 | |
| 174 | switch(c) { |
| 175 | case SLIP_ESC: |
| 176 | lastc = c; |
| 177 | break; |
| 178 | |
| 179 | case SLIP_END: |
| 180 | lastc = c; |
| 181 | /* End marker found, we copy our input buffer to the uip_buf |
| 182 | buffer and return the size of the packet we copied. */ |
| 183 | memcpy(&uip_buf[UIP_LLH_LEN], slip_buf, len); |
| 184 | tmplen = len; |
| 185 | len = 0; |
| 186 | return tmplen; |
| 187 | |
| 188 | default: |
| 189 | if(lastc == SLIP_ESC) { |
| 190 | lastc = c; |
| 191 | /* Previous read byte was an escape byte, so this byte will be |
| 192 | interpreted differently from others. */ |
| 193 | switch(c) { |
| 194 | case SLIP_ESC_END: |
| 195 | c = SLIP_END; |
| 196 | break; |
| 197 | case SLIP_ESC_ESC: |
| 198 | c = SLIP_ESC; |
| 199 | break; |
| 200 | } |
| 201 | } else { |
| 202 | lastc = c; |
| 203 | } |
| 204 | |
| 205 | slip_buf[len] = c; |
| 206 | ++len; |
| 207 | |
| 208 | if(len > MAX_SIZE) { |
| 209 | len = 0; |
| 210 | } |
| 211 | |
| 212 | break; |
| 213 | } |
| 214 | } |
| 215 | return 0; |
| 216 | } |
| 217 | /*-----------------------------------------------------------------------------------*/ |
| 218 | /* |
| 219 | * rs232dev_init(): |
| 220 | * |
| 221 | * Initializes the RS232 device and sets the parameters of the device. |
| 222 | * |
| 223 | */ |
| 224 | /*-----------------------------------------------------------------------------------*/ |
| 225 | void |
| 226 | rs232dev_init(void) |
| 227 | { |
| 228 | char err; |
| 229 | struct ser_params p; |
| 230 | |
| 231 | err = ser_load_driver("c64-swlink.ser"); |
| 232 | |
| 233 | if(err != SER_ERR_OK) { |
| 234 | asm("inc $d020"); |
| 235 | return; |
| 236 | } |
| 237 | |
| 238 | p.baudrate = SER_BAUD_9600; |
| 239 | p.databits = SER_BITS_8; |
| 240 | p.stopbits = SER_STOP_1; |
| 241 | p.parity = SER_PAR_NONE; |
| 242 | p.handshake = SER_HS_HW; |
| 243 | |
| 244 | err = ser_open(&p); |
| 245 | |
| 246 | if(err != SER_ERR_OK) { |
| 247 | asm("inc $d020"); |
| 248 | return; |
| 249 | } |
| 250 | |
| 251 | |
| 252 | loaded = 1; |
| 253 | |
| 254 | /* err = rs232_init(0); */ |
| 255 | /* rs232_err(err);*/ |
| 256 | /* err = rs232_params(RS_BAUD_9600 | RS_BITS_8 | RS_STOP_1, RS_PAR_NONE);*/ |
| 257 | /* rs232_err(err);*/ |
| 258 | |
| 259 | len = 0; |
| 260 | |
| 261 | return; |
| 262 | } |
| 263 | /*-----------------------------------------------------------------------------------*/ |
| 264 | void |
| 265 | rs232dev_unload(void) |
| 266 | { |
| 267 | if(loaded){ |
| 268 | ser_unload(); |
| 269 | } |
| 270 | } |
| 271 | /*-----------------------------------------------------------------------------------*/ |
| 272 | |