Mistake on this page?
Report an issue in GitHub or email us
Ethernet.h
1 /* mbed Microcontroller Library
2  * Copyright (c) 2006-2013 ARM Limited
3  * SPDX-License-Identifier: Apache-2.0
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 #ifndef MBED_ETHERNET_H
18 #define MBED_ETHERNET_H
19 
20 #include "platform/platform.h"
21 #include "platform/NonCopyable.h"
22 
23 #if DEVICE_ETHERNET || defined(DOXYGEN_ONLY)
24 
25 namespace mbed {
26 /** \addtogroup drivers */
27 
28 /** An ethernet interface, to use with the ethernet pins.
29  *
30  * @note Synchronization level: Not protected
31  *
32  * Example:
33  * @code
34  * // Read destination and source from every ethernet packet
35  *
36  * #include "mbed.h"
37  *
38  * Ethernet eth;
39  *
40  * int main() {
41  * char buf[0x600];
42  *
43  * while(1) {
44  * int size = eth.receive();
45  * if(size > 0) {
46  * eth.read(buf, size);
47  * printf("Destination: %02X:%02X:%02X:%02X:%02X:%02X\n",
48  * buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
49  * printf("Source: %02X:%02X:%02X:%02X:%02X:%02X\n",
50  * buf[6], buf[7], buf[8], buf[9], buf[10], buf[11]);
51  * }
52  *
53  * wait(1);
54  * }
55  * }
56  * @endcode
57  * @ingroup drivers
58  */
59 class Ethernet : private NonCopyable<Ethernet> {
60 
61 public:
62 
63  /** Initialize the ethernet interface.
64  */
65  Ethernet();
66 
67  /** Powers the hardware down.
68  */
69  virtual ~Ethernet();
70 
71  enum Mode {
72  AutoNegotiate,
73  HalfDuplex10,
74  FullDuplex10,
75  HalfDuplex100,
76  FullDuplex100
77  };
78 
79  /** Writes into an outgoing ethernet packet.
80  *
81  * It will append size bytes of data to the previously written bytes.
82  *
83  * @param data An array to write.
84  * @param size The size of data.
85  *
86  * @returns
87  * The number of written bytes.
88  */
89  int write(const char *data, int size);
90 
91  /** Send an outgoing ethernet packet.
92  *
93  * After filling in the data in an ethernet packet it must be send.
94  * Send will provide a new packet to write to.
95  *
96  * @returns
97  * 0 if the sending was failed,
98  * or the size of the packet successfully sent.
99  */
100  int send();
101 
102  /** Receives an arrived ethernet packet.
103  *
104  * Receiving an ethernet packet will drop the last received ethernet packet
105  * and make a new ethernet packet ready to read.
106  * If no ethernet packet is arrived it will return 0.
107  *
108  * @returns
109  * 0 if no ethernet packet is arrived,
110  * or the size of the arrived packet.
111  */
112  int receive();
113 
114  /** Read from an received ethernet packet.
115  *
116  * After receive returned a number bigger than 0 it is
117  * possible to read bytes from this packet.
118  *
119  * @param data Pointer to data packet
120  * @param size Size of data to be read.
121  * @returns The number of byte read.
122  *
123  * @note It is possible to use read multiple times.
124  * Each time read will start reading after the last read byte before.
125  *
126  */
127  int read(char *data, int size);
128 
129  /** Gives the ethernet address of the mbed.
130  *
131  * @param mac Must be a pointer to a 6 byte char array to copy the ethernet address in.
132  */
133  void address(char *mac);
134 
135  /** Returns if an ethernet link is present or not. It takes a while after Ethernet initialization to show up.
136  *
137  * @returns
138  * 0 if no ethernet link is present,
139  * 1 if an ethernet link is present.
140  *
141  * Example:
142  * @code
143  * // Using the Ethernet link function
144  * #include "mbed.h"
145  *
146  * Ethernet eth;
147  *
148  * int main() {
149  * wait(1); // Needed after startup.
150  * if (eth.link()) {
151  * printf("online\n");
152  * } else {
153  * printf("offline\n");
154  * }
155  * }
156  * @endcode
157  */
158  int link();
159 
160  /** Sets the speed and duplex parameters of an ethernet link
161  *
162  * - AutoNegotiate Auto negotiate speed and duplex
163  * - HalfDuplex10 10 Mbit, half duplex
164  * - FullDuplex10 10 Mbit, full duplex
165  * - HalfDuplex100 100 Mbit, half duplex
166  * - FullDuplex100 100 Mbit, full duplex
167  *
168  * @param mode the speed and duplex mode to set the link to:
169  */
170  void set_link(Mode mode);
171 };
172 
173 } // namespace mbed
174 
175 #endif
176 
177 #endif
void address(char *mac)
Gives the ethernet address of the mbed.
int read(char *data, int size)
Read from an received ethernet packet.
Prevents generation of copy constructor and copy assignment operator in derived classes.
Definition: NonCopyable.h:168
Ethernet()
Initialize the ethernet interface.
int link()
Returns if an ethernet link is present or not.
void set_link(Mode mode)
Sets the speed and duplex parameters of an ethernet link.
An ethernet interface, to use with the ethernet pins.
Definition: Ethernet.h:59
int receive()
Receives an arrived ethernet packet.
int send()
Send an outgoing ethernet packet.
int write(const char *data, int size)
Writes into an outgoing ethernet packet.
virtual ~Ethernet()
Powers the hardware down.
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.