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