AT91: distinct CPU definitions for AT91SAM7X and AT91SAM7S family
[bertos.git] / bertos / drv / eth.h
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2010 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \author Andrea Righi <arighi@develer.com>
34  *
35  * \brief Ethernet standard descriptors
36  *
37  * $WIZ$ module_name = "eth"
38  * $WIZ$ module_configuration = "bertos/cfg/cfg_eth.h"
39  * $WIZ$ module_supports = "at91sam7x"
40  */
41
42 #ifndef DRV_ETH_H
43 #define DRV_ETH_H
44
45 #include <cpu/types.h>
46
47 #define ETH_ADDR_LEN    6
48 #define ETH_HEAD_LEN    14
49 #define ETH_DATA_LEN    1500
50 #define ETH_FRAME_LEN   (ETH_HEAD_LEN + ETH_DATA_LEN)
51
52 #define ETH_TYPE_IP     0x0800
53
54 typedef union Ethernet
55 {
56         struct
57         {
58                 uint8_t  dst[ETH_ADDR_LEN];
59                 uint8_t  src[ETH_ADDR_LEN];
60                 uint16_t type;
61                 uint8_t data[0];
62         };
63         uint8_t raw[ETH_FRAME_LEN];
64 } PACKED Ethernet;
65
66 /**
67  * Determine if ethernet address \a addr is a all zero.
68  */
69 INLINE int eth_addrIsZero(const uint8_t *addr)
70 {
71         return !(addr[0] | addr[1] | addr[2] |
72                  addr[3] | addr[4] | addr[5]);
73 }
74
75 /**
76  * Determine if ethernet address \a addr is a multicast address.
77  */
78 INLINE int eth_addrIsMcast(const uint8_t *addr)
79 {
80         return (0x01 & addr[0]);
81 }
82
83 /**
84  * Determine if ethernet address \a addr is locally-assigned (IEEE 802).
85  */
86 INLINE int eth_addrIsLocal(const uint8_t *addr)
87 {
88         return (0x02 & addr[0]);
89 }
90
91 /**
92  * Determine if ethernet address \a addr is broadcast.
93  */
94 INLINE bool eth_addrIsBcast(const uint8_t *addr)
95 {
96         return (addr[0] & addr[1] & addr[2] &
97                 addr[3] & addr[4] & addr[5]) == 0xff;
98 }
99
100 /**
101  * Check if the ethernet address \a addr is not all zero, is not a multicast
102  * address, and is not broadcast.
103  */
104 INLINE bool eth_addrIsValid(const uint8_t *addr)
105 {
106         return !eth_addrIsMcast(addr) && !eth_addrIsZero(addr);
107 }
108
109 /**
110  * Compare two ethernet addresses: \a addr1 and \a addr2, returns 0 if equal.
111  */
112 INLINE bool eth_addrCmp(const uint8_t *addr1, const uint8_t *addr2)
113 {
114         return !!((addr1[0] ^ addr2[0]) |
115                         (addr1[1] ^ addr2[1]) |
116                         (addr1[2] ^ addr2[2]) |
117                         (addr1[3] ^ addr2[3]) |
118                         (addr1[4] ^ addr2[4]) |
119                         (addr1[5] ^ addr2[5]));
120 }
121
122 ssize_t eth_send(const uint8_t *buf, size_t len);
123 ssize_t eth_recv(uint8_t *buf, size_t len);
124 int eth_init(void);
125
126 extern uint8_t mac_addr[ETH_ADDR_LEN];
127
128 #endif /* DRV_ETH_H */