Contiki 3.x
ethernet.c
1 /*
2  * Copyright (c) 2007, Swedish Institute of Computer Science
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. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  * Author: Oliver Schmidt <ol.sc@web.de>
32  *
33  */
34 
35 #include <modload.h>
36 
37 #include "contiki-net.h"
38 #include "cfs/cfs.h"
39 #include "sys/log.h"
40 #include "lib/error.h"
41 #include "net/ethernet-drv.h"
42 
43 #include "net/ethernet.h"
44 
45 struct {
46  char signature[4];
47  struct uip_eth_addr ethernet_address;
48  uint8_t *buffer;
49  uint16_t buffer_size;
50  char jmp_init;
51  char __fastcall__ (* init)(uint16_t reg);
52  char jmp_poll;
53  uint16_t (* poll)(void);
54  char jmp_send;
55  void __fastcall__ (* send)(uint16_t len);
56  char jmp_exit;
57  void (* exit)(void);
58 } *module;
59 
60 /*---------------------------------------------------------------------------*/
61 void CC_FASTCALL
62 ethernet_init(struct ethernet_config *config)
63 {
64  static const char signature[4] = {0x65, 0x74, 0x68, 0x01};
65 
66 #ifndef ETHERNET
67 
68  struct mod_ctrl module_control = {cfs_read};
69  uint8_t byte;
70 
71  module_control.callerdata = cfs_open(config->name, CFS_READ);
72  if(module_control.callerdata < 0) {
73  log_message(config->name, ": File not found");
74  error_exit();
75  }
76 
77  byte = mod_load(&module_control);
78  if(byte != MLOAD_OK) {
79  log_message(config->name, byte == MLOAD_ERR_MEM? ": Out of memory":
80  ": No module");
81  error_exit();
82  }
83 
84  cfs_close(module_control.callerdata);
85  module = module_control.module;
86 
87  for(byte = 0; byte < 4; ++byte) {
88  if(module->signature[byte] != signature[byte]) {
89  log_message(config->name, ": No ETH driver");
90  error_exit();
91  }
92  }
93 
94 #else /* !ETHERNET */
95 
96  extern void ETHERNET;
97 
98  module = &ETHERNET;
99 
100 #endif /* !ETHERNET */
101 
102  module->buffer = uip_buf;
103  module->buffer_size = UIP_BUFSIZE;
104  if(module->init(config->addr)) {
105  log_message(config->name, ": No hardware");
106  error_exit();
107  }
108 
109  uip_setethaddr(module->ethernet_address);
110 }
111 /*---------------------------------------------------------------------------*/
112 uint16_t
113 ethernet_poll(void)
114 {
115  return module->poll();
116 }
117 /*---------------------------------------------------------------------------*/
118 void
119 ethernet_send(void)
120 {
121  module->send(uip_len);
122 }
123 /*---------------------------------------------------------------------------*/
124 void
125 ethernet_exit(void)
126 {
127  module->exit();
128 
129 #ifndef ETHERNET
130  mod_free(module);
131 #endif /* !ETHERNET */
132 }
133 /*---------------------------------------------------------------------------*/
uip_len
The length of the packet in the uip_buf buffer.
Definition: tcp_loader.c:75
#define uip_setethaddr(eaddr)
Specifiy the Ethernet MAC address.
Definition: uip_arp.h:131
int cfs_open(const char *name, int flags)
Open a file.
Definition: cfs-coffee.c:996
#define CFS_READ
Specify that cfs_open() should open a file for reading.
Definition: cfs.h:90
#define UIP_BUFSIZE
The size of the uIP packet buffer.
Definition: uipopt.h:173
802.3 address
Definition: uip.h:135
#define CC_FASTCALL
Configure if the C compiler supports fastcall function declarations.
Definition: cc.h:77
void cfs_close(int fd)
Close an open file.
Definition: cfs-coffee.c:1032
CFS header file.