From da44474fd46f8d9d2cd733a8831b1ac701f57dbf Mon Sep 17 00:00:00 2001 From: oliverschmidt Date: Tue, 20 Nov 2007 21:10:20 +0000 Subject: [PATCH] Generic Ethernet driver for cc65 targets. The actual hardware driver is loaded as relocatable module. --- cpu/6502/Makefile.6502 | 10 ++- cpu/6502/net/ethernet-drv.c | 102 ++++++++++++++++++++++++++++ cpu/6502/net/ethernet-drv.h | 48 ++++++++++++++ cpu/6502/net/ethernet.c | 128 ++++++++++++++++++++++++++++++++++++ cpu/6502/net/ethernet.h | 44 +++++++++++++ 5 files changed, 330 insertions(+), 2 deletions(-) create mode 100644 cpu/6502/net/ethernet-drv.c create mode 100644 cpu/6502/net/ethernet-drv.h create mode 100644 cpu/6502/net/ethernet.c create mode 100644 cpu/6502/net/ethernet.h diff --git a/cpu/6502/Makefile.6502 b/cpu/6502/Makefile.6502 index eb0ce9ffd..fcdb556da 100644 --- a/cpu/6502/Makefile.6502 +++ b/cpu/6502/Makefile.6502 @@ -30,7 +30,7 @@ # # Author: Oliver Schmidt # -# $Id: Makefile.6502,v 1.12 2007/08/30 20:47:14 oliverschmidt Exp $ +# $Id: Makefile.6502,v 1.13 2007/11/20 21:12:00 oliverschmidt Exp $ # ifndef CONTIKI @@ -49,12 +49,15 @@ ifndef LD65_OBJ ${error LD65_OBJ not defined! You must specify where the cc65 objects reside!} endif +all: cs8900a.eth + CONTIKI_TARGET_DIRS = . CONTIKI_CPU_DIRS = . net sys CONTIKI_TARGET_MAIN = ${addprefix $(OBJECTDIR)/,contiki-main.o} CONTIKI_TARGET_SOURCEFILES = contiki-main.c -CONTIKI_CPU_SOURCEFILES = clock.c mtarch.c mtarch-asm.S lc-asm.S uip_arch.c +CONTIKI_CPU_SOURCEFILES = clock.c mtarch.c mtarch-asm.S lc-asm.S \ + uip_arch.c ethernet-drv.c ethernet.c CONTIKI_SOURCEFILES += $(CONTIKI_CPU_SOURCEFILES) $(CONTIKI_TARGET_SOURCEFILES) @@ -93,3 +96,6 @@ CUSTOM_RULE_C_TO_CO = 1 $(CC) $(CFLAGS) -DAUTOSTART_ENABLE $< -o $(@:.co=.s) @$(AS) $(ASFLAGS) $(@:.co=.s) -o $@ @rm -f $(@:.co=.s) + +%.eth: $(OBJECTDIR)/%.o + $(LD) -t module -m $@.map $< -o $@ diff --git a/cpu/6502/net/ethernet-drv.c b/cpu/6502/net/ethernet-drv.c new file mode 100644 index 000000000..de0bcd814 --- /dev/null +++ b/cpu/6502/net/ethernet-drv.c @@ -0,0 +1,102 @@ +/* + * Copyright (c) 2007, Swedish Institute of Computer Science + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + * @(#)$Id: ethernet-drv.c,v 1.1 2007/11/20 21:10:20 oliverschmidt Exp $ + */ + +#include + +#include "contiki-net.h" +#include "ethernet.h" +#include "net/uip-neighbor.h" + +#include "ethernet-drv.h" + +#define BUF ((struct uip_eth_hdr *)&uip_buf[0]) + +PROCESS(ethernet_process, "Ethernet driver"); + +/*---------------------------------------------------------------------------*/ +u8_t +ethernet_output(void) +{ + uip_arp_out(); + ethernet_send(); + + return 0; +} +/*---------------------------------------------------------------------------*/ +static void +pollhandler(void) +{ + process_poll(ðernet_process); + uip_len = ethernet_poll(); + + if(uip_len > 0) { +#if UIP_CONF_IPV6 + if(BUF->type == htons(UIP_ETHTYPE_IPV6)) { + uip_neighbor_add(&IPBUF->srcipaddr, &BUF->src); + tcpip_input(); + } else +#endif /* UIP_CONF_IPV6 */ + if(BUF->type == htons(UIP_ETHTYPE_IP)) { + uip_len -= sizeof(struct uip_eth_hdr); + tcpip_input(); + } else if(BUF->type == htons(UIP_ETHTYPE_ARP)) { + uip_arp_arpin(); + /* If the above function invocation resulted in data that + should be sent out on the network, the global variable + uip_len is set to a value > 0. */ + if(uip_len > 0) { + ethernet_send(); + } + } + } +} +/*---------------------------------------------------------------------------*/ +PROCESS_THREAD(ethernet_process, ev, data) +{ + PROCESS_POLLHANDLER(pollhandler()); + + PROCESS_BEGIN(); + + ethernet_init((struct ethernet_config *)data); + + tcpip_set_outputfunc(ethernet_output); + + process_poll(ðernet_process); + + PROCESS_WAIT_UNTIL(ev == PROCESS_EVENT_EXIT); + + ethernet_exit(); + + PROCESS_END(); +} +/*---------------------------------------------------------------------------*/ diff --git a/cpu/6502/net/ethernet-drv.h b/cpu/6502/net/ethernet-drv.h new file mode 100644 index 000000000..325e753f9 --- /dev/null +++ b/cpu/6502/net/ethernet-drv.h @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2007, Swedish Institute of Computer Science. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + * $Id: ethernet-drv.h,v 1.1 2007/11/20 21:10:20 oliverschmidt Exp $ + */ + +#ifndef __ETHERNET_DRV_H__ +#define __ETHERNET_DRV_H__ + +#include "contiki.h" + +struct ethernet_config { + u16_t addr; + char name[12+1]; +}; + +PROCESS_NAME(ethernet_process); + +u8_t ethernet_output(void); + +#endif /* __ETHERNET_DRV_H__ */ diff --git a/cpu/6502/net/ethernet.c b/cpu/6502/net/ethernet.c new file mode 100644 index 000000000..cb9c948a1 --- /dev/null +++ b/cpu/6502/net/ethernet.c @@ -0,0 +1,128 @@ +/* + * Copyright (c) 2007, Swedish Institute of Computer Science + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + * Author: Oliver Schmidt + * + * @(#)$Id: ethernet.c,v 1.1 2007/11/20 21:10:21 oliverschmidt Exp $ + */ + +#include +#include +#include +#include +#include +#include +#include + +#include "contiki-net.h" +#include "ethernet-drv.h" + +#include "ethernet.h" + +struct { + char signature[4]; + struct uip_eth_addr ethernet_address; + u8_t *buffer; + u16_t buffer_size; + void __fastcall__ (* init)(u16_t reg); + u16_t (* poll)(void); + void __fastcall__ (* send)(u16_t len); + void (* exit)(void); +} *module; + +/*---------------------------------------------------------------------------*/ +static void +error_exit(void) +{ + fprintf(stderr, "Press any key to continue ...\n"); + getchar(); + exit(EXIT_FAILURE); +} +/*---------------------------------------------------------------------------*/ +void +ethernet_init(struct ethernet_config *config) +{ + static const char signature[4] = {0x65, 0x74, 0x68, 0x01}; + struct mod_ctrl module_control = {read}; + u8_t byte; + + module_control.callerdata = open(config->name, O_RDONLY); + if(module_control.callerdata == -1) { + fprintf(stderr, "%s: %s\n", config->name, strerror(errno)); + error_exit(); + } + + byte = mod_load(&module_control); + if(byte != MLOAD_OK) { + if(byte == MLOAD_ERR_MEM) { + fprintf(stderr, "%s: %s\n", config->name, strerror(ENOMEM)); + } else { + fprintf(stderr, "%s: %s %d\n", config->name, "Error", byte); + } + error_exit(); + } + + close(module_control.callerdata); + module = module_control.module; + + for(byte = 0; byte < 4; byte++) { + if(module->signature[byte] != signature[byte]) { + fprintf(stderr, "%s: %s\n", config->name, "No ETH Driver"); + error_exit(); + } + } + + module->buffer = uip_buf; + module->buffer_size = UIP_BUFSIZE; + module->init(config->addr); + + uip_setethaddr(module->ethernet_address); +} +/*---------------------------------------------------------------------------*/ +u16_t +ethernet_poll(void) +{ + return module->poll(); +} +/*---------------------------------------------------------------------------*/ +void +ethernet_send(void) +{ + module->send(uip_len); +} +/*---------------------------------------------------------------------------*/ +void +ethernet_exit(void) +{ + module->exit(); + + mod_free(module); +} +/*---------------------------------------------------------------------------*/ diff --git a/cpu/6502/net/ethernet.h b/cpu/6502/net/ethernet.h new file mode 100644 index 000000000..241e8721f --- /dev/null +++ b/cpu/6502/net/ethernet.h @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2007, Swedish Institute of Computer Science. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + * Author: Oliver Schmidt + * + * $Id: ethernet.h,v 1.1 2007/11/20 21:10:21 oliverschmidt Exp $ + */ + +#ifndef __ETHERNET_H__ +#define __ETHERNET_H__ + +void ethernet_init(struct ethernet_config *config); +u16_t ethernet_poll(void); +void ethernet_send(void); +void ethernet_exit(void); + +#endif /* __ETHERNET_H__ */