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