Fix a bug when reading negative temperatures.
[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 or sam3x"
40  * $WIZ$ module_hw = "bertos/hw/hw_eth.h", "bertos/hw/hw_eth.c"
41  */
42
43 #ifndef DRV_ETH_H
44 #define DRV_ETH_H
45
46 #include "hw/hw_eth.h"
47 #include "cfg/cfg_eth.h"
48
49 #include <drv/phy.h>
50
51 #include <cpu/types.h>
52
53
54 #define ETH_ADDR_LEN    6
55 #define ETH_HEAD_LEN    14
56 #define ETH_DATA_LEN    1500
57 #define ETH_FRAME_LEN   (ETH_HEAD_LEN + ETH_DATA_LEN)
58 #define ETH_TYPE_IP     0x0800
59
60 typedef union Ethernet
61 {
62         struct
63         {
64                 uint8_t  dst[ETH_ADDR_LEN];
65                 uint8_t  src[ETH_ADDR_LEN];
66                 uint16_t type;
67                 uint8_t data[0];
68         };
69         uint8_t raw[ETH_FRAME_LEN];
70 } PACKED Ethernet;
71
72 /**
73  * Determine if ethernet address \a addr is a all zero.
74  */
75 INLINE int eth_addrIsZero(const uint8_t *addr)
76 {
77         return !(addr[0] | addr[1] | addr[2] |
78                  addr[3] | addr[4] | addr[5]);
79 }
80
81 /**
82  * Determine if ethernet address \a addr is a multicast address.
83  */
84 INLINE int eth_addrIsMcast(const uint8_t *addr)
85 {
86         return (0x01 & addr[0]);
87 }
88
89 /**
90  * Determine if ethernet address \a addr is locally-assigned (IEEE 802).
91  */
92 INLINE int eth_addrIsLocal(const uint8_t *addr)
93 {
94         return (0x02 & addr[0]);
95 }
96
97 /**
98  * Determine if ethernet address \a addr is broadcast.
99  */
100 INLINE bool eth_addrIsBcast(const uint8_t *addr)
101 {
102         return (addr[0] & addr[1] & addr[2] &
103                 addr[3] & addr[4] & addr[5]) == 0xff;
104 }
105
106 /**
107  * Check if the ethernet address \a addr is not all zero, is not a multicast
108  * address, and is not broadcast.
109  */
110 INLINE bool eth_addrIsValid(const uint8_t *addr)
111 {
112         return !eth_addrIsMcast(addr) && !eth_addrIsZero(addr);
113 }
114
115 /**
116  * Compare two ethernet addresses: \a addr1 and \a addr2, returns 0 if equal.
117  */
118 INLINE bool eth_addrCmp(const uint8_t *addr1, const uint8_t *addr2)
119 {
120         return !!((addr1[0] ^ addr2[0]) |
121                         (addr1[1] ^ addr2[1]) |
122                         (addr1[2] ^ addr2[2]) |
123                         (addr1[3] ^ addr2[3]) |
124                         (addr1[4] ^ addr2[4]) |
125                         (addr1[5] ^ addr2[5]));
126 }
127
128 ssize_t eth_putFrame(const uint8_t *buf, size_t len);
129 void eth_sendFrame(void);
130
131 size_t eth_getFrameLen(void);
132 ssize_t eth_getFrame(uint8_t *buf, size_t len);
133
134 ssize_t eth_send(const uint8_t *buf, size_t len);
135 ssize_t eth_recv(uint8_t *buf, size_t len);
136
137 int eth_init(void);
138
139 #endif /* DRV_ETH_H */