mbed library sources(for async_print)
Fork of mbed-src by
cpp/Ethernet.h@0:fd0d7bdfcdc2, 2012-11-20 (annotated)
- Committer:
- mbed_official
- Date:
- Tue Nov 20 17:24:08 2012 +0000
- Revision:
- 0:fd0d7bdfcdc2
- Child:
- 2:143cac498751
mbed sources
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 0:fd0d7bdfcdc2 | 1 | /* mbed Microcontroller Library |
mbed_official | 0:fd0d7bdfcdc2 | 2 | * Copyright (c) 2006-2012 ARM Limited |
mbed_official | 0:fd0d7bdfcdc2 | 3 | * |
mbed_official | 0:fd0d7bdfcdc2 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
mbed_official | 0:fd0d7bdfcdc2 | 5 | * of this software and associated documentation files (the "Software"), to deal |
mbed_official | 0:fd0d7bdfcdc2 | 6 | * in the Software without restriction, including without limitation the rights |
mbed_official | 0:fd0d7bdfcdc2 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
mbed_official | 0:fd0d7bdfcdc2 | 8 | * copies of the Software, and to permit persons to whom the Software is |
mbed_official | 0:fd0d7bdfcdc2 | 9 | * furnished to do so, subject to the following conditions: |
mbed_official | 0:fd0d7bdfcdc2 | 10 | * |
mbed_official | 0:fd0d7bdfcdc2 | 11 | * The above copyright notice and this permission notice shall be included in |
mbed_official | 0:fd0d7bdfcdc2 | 12 | * all copies or substantial portions of the Software. |
mbed_official | 0:fd0d7bdfcdc2 | 13 | * |
mbed_official | 0:fd0d7bdfcdc2 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
mbed_official | 0:fd0d7bdfcdc2 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
mbed_official | 0:fd0d7bdfcdc2 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
mbed_official | 0:fd0d7bdfcdc2 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
mbed_official | 0:fd0d7bdfcdc2 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
mbed_official | 0:fd0d7bdfcdc2 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
mbed_official | 0:fd0d7bdfcdc2 | 20 | * SOFTWARE. |
mbed_official | 0:fd0d7bdfcdc2 | 21 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 22 | #ifndef MBED_ETHERNET_H |
mbed_official | 0:fd0d7bdfcdc2 | 23 | #define MBED_ETHERNET_H |
mbed_official | 0:fd0d7bdfcdc2 | 24 | |
mbed_official | 0:fd0d7bdfcdc2 | 25 | #include "platform.h" |
mbed_official | 0:fd0d7bdfcdc2 | 26 | |
mbed_official | 0:fd0d7bdfcdc2 | 27 | #if DEVICE_ETHERNET |
mbed_official | 0:fd0d7bdfcdc2 | 28 | |
mbed_official | 0:fd0d7bdfcdc2 | 29 | namespace mbed { |
mbed_official | 0:fd0d7bdfcdc2 | 30 | |
mbed_official | 0:fd0d7bdfcdc2 | 31 | /** An ethernet interface, to use with the ethernet pins. |
mbed_official | 0:fd0d7bdfcdc2 | 32 | * |
mbed_official | 0:fd0d7bdfcdc2 | 33 | * Example: |
mbed_official | 0:fd0d7bdfcdc2 | 34 | * @code |
mbed_official | 0:fd0d7bdfcdc2 | 35 | * // Read destination and source from every ethernet packet |
mbed_official | 0:fd0d7bdfcdc2 | 36 | * |
mbed_official | 0:fd0d7bdfcdc2 | 37 | * #include "mbed.h" |
mbed_official | 0:fd0d7bdfcdc2 | 38 | * |
mbed_official | 0:fd0d7bdfcdc2 | 39 | * Ethernet eth; |
mbed_official | 0:fd0d7bdfcdc2 | 40 | * |
mbed_official | 0:fd0d7bdfcdc2 | 41 | * int main() { |
mbed_official | 0:fd0d7bdfcdc2 | 42 | * char buf[0x600]; |
mbed_official | 0:fd0d7bdfcdc2 | 43 | * |
mbed_official | 0:fd0d7bdfcdc2 | 44 | * while(1) { |
mbed_official | 0:fd0d7bdfcdc2 | 45 | * int size = eth.receive(); |
mbed_official | 0:fd0d7bdfcdc2 | 46 | * if(size > 0) { |
mbed_official | 0:fd0d7bdfcdc2 | 47 | * eth.read(buf, size); |
mbed_official | 0:fd0d7bdfcdc2 | 48 | * printf("Destination: %02X:%02X:%02X:%02X:%02X:%02X\n", |
mbed_official | 0:fd0d7bdfcdc2 | 49 | * buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]); |
mbed_official | 0:fd0d7bdfcdc2 | 50 | * printf("Source: %02X:%02X:%02X:%02X:%02X:%02X\n", |
mbed_official | 0:fd0d7bdfcdc2 | 51 | * buf[6], buf[7], buf[8], buf[9], buf[10], buf[11]); |
mbed_official | 0:fd0d7bdfcdc2 | 52 | * } |
mbed_official | 0:fd0d7bdfcdc2 | 53 | * |
mbed_official | 0:fd0d7bdfcdc2 | 54 | * wait(1); |
mbed_official | 0:fd0d7bdfcdc2 | 55 | * } |
mbed_official | 0:fd0d7bdfcdc2 | 56 | * } |
mbed_official | 0:fd0d7bdfcdc2 | 57 | * @endcode |
mbed_official | 0:fd0d7bdfcdc2 | 58 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 59 | class Ethernet { |
mbed_official | 0:fd0d7bdfcdc2 | 60 | |
mbed_official | 0:fd0d7bdfcdc2 | 61 | public: |
mbed_official | 0:fd0d7bdfcdc2 | 62 | |
mbed_official | 0:fd0d7bdfcdc2 | 63 | /** Initialise the ethernet interface. |
mbed_official | 0:fd0d7bdfcdc2 | 64 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 65 | Ethernet(); |
mbed_official | 0:fd0d7bdfcdc2 | 66 | |
mbed_official | 0:fd0d7bdfcdc2 | 67 | /** Powers the hardware down. |
mbed_official | 0:fd0d7bdfcdc2 | 68 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 69 | virtual ~Ethernet(); |
mbed_official | 0:fd0d7bdfcdc2 | 70 | |
mbed_official | 0:fd0d7bdfcdc2 | 71 | enum Mode { |
mbed_official | 0:fd0d7bdfcdc2 | 72 | AutoNegotiate, |
mbed_official | 0:fd0d7bdfcdc2 | 73 | HalfDuplex10, |
mbed_official | 0:fd0d7bdfcdc2 | 74 | FullDuplex10, |
mbed_official | 0:fd0d7bdfcdc2 | 75 | HalfDuplex100, |
mbed_official | 0:fd0d7bdfcdc2 | 76 | FullDuplex100 |
mbed_official | 0:fd0d7bdfcdc2 | 77 | }; |
mbed_official | 0:fd0d7bdfcdc2 | 78 | |
mbed_official | 0:fd0d7bdfcdc2 | 79 | /** Writes into an outgoing ethernet packet. |
mbed_official | 0:fd0d7bdfcdc2 | 80 | * |
mbed_official | 0:fd0d7bdfcdc2 | 81 | * It will append size bytes of data to the previously written bytes. |
mbed_official | 0:fd0d7bdfcdc2 | 82 | * |
mbed_official | 0:fd0d7bdfcdc2 | 83 | * @param data An array to write. |
mbed_official | 0:fd0d7bdfcdc2 | 84 | * @param size The size of data. |
mbed_official | 0:fd0d7bdfcdc2 | 85 | * |
mbed_official | 0:fd0d7bdfcdc2 | 86 | * @returns |
mbed_official | 0:fd0d7bdfcdc2 | 87 | * The number of written bytes. |
mbed_official | 0:fd0d7bdfcdc2 | 88 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 89 | int write(const char *data, int size); |
mbed_official | 0:fd0d7bdfcdc2 | 90 | |
mbed_official | 0:fd0d7bdfcdc2 | 91 | /** Send an outgoing ethernet packet. |
mbed_official | 0:fd0d7bdfcdc2 | 92 | * |
mbed_official | 0:fd0d7bdfcdc2 | 93 | * After filling in the data in an ethernet packet it must be send. |
mbed_official | 0:fd0d7bdfcdc2 | 94 | * Send will provide a new packet to write to. |
mbed_official | 0:fd0d7bdfcdc2 | 95 | * |
mbed_official | 0:fd0d7bdfcdc2 | 96 | * @returns |
mbed_official | 0:fd0d7bdfcdc2 | 97 | * 0 if the sending was failed, |
mbed_official | 0:fd0d7bdfcdc2 | 98 | * 1 if the package is successfully sent. |
mbed_official | 0:fd0d7bdfcdc2 | 99 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 100 | int send(); |
mbed_official | 0:fd0d7bdfcdc2 | 101 | |
mbed_official | 0:fd0d7bdfcdc2 | 102 | /** Recevies an arrived ethernet packet. |
mbed_official | 0:fd0d7bdfcdc2 | 103 | * |
mbed_official | 0:fd0d7bdfcdc2 | 104 | * Receiving an ethernet packet will drop the last received ethernet packet |
mbed_official | 0:fd0d7bdfcdc2 | 105 | * and make a new ethernet packet ready to read. |
mbed_official | 0:fd0d7bdfcdc2 | 106 | * If no ethernet packet is arrived it will return 0. |
mbed_official | 0:fd0d7bdfcdc2 | 107 | * |
mbed_official | 0:fd0d7bdfcdc2 | 108 | * @returns |
mbed_official | 0:fd0d7bdfcdc2 | 109 | * 0 if no ethernet packet is arrived, |
mbed_official | 0:fd0d7bdfcdc2 | 110 | * or the size of the arrived packet. |
mbed_official | 0:fd0d7bdfcdc2 | 111 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 112 | int receive(); |
mbed_official | 0:fd0d7bdfcdc2 | 113 | |
mbed_official | 0:fd0d7bdfcdc2 | 114 | /** Read from an recevied ethernet packet. |
mbed_official | 0:fd0d7bdfcdc2 | 115 | * |
mbed_official | 0:fd0d7bdfcdc2 | 116 | * After receive returnd a number bigger than 0it is |
mbed_official | 0:fd0d7bdfcdc2 | 117 | * possible to read bytes from this packet. |
mbed_official | 0:fd0d7bdfcdc2 | 118 | * Read will write up to size bytes into data. |
mbed_official | 0:fd0d7bdfcdc2 | 119 | * |
mbed_official | 0:fd0d7bdfcdc2 | 120 | * It is possible to use read multible times. |
mbed_official | 0:fd0d7bdfcdc2 | 121 | * Each time read will start reading after the last read byte before. |
mbed_official | 0:fd0d7bdfcdc2 | 122 | * |
mbed_official | 0:fd0d7bdfcdc2 | 123 | * @returns |
mbed_official | 0:fd0d7bdfcdc2 | 124 | * The number of byte read. |
mbed_official | 0:fd0d7bdfcdc2 | 125 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 126 | int read(char *data, int size); |
mbed_official | 0:fd0d7bdfcdc2 | 127 | |
mbed_official | 0:fd0d7bdfcdc2 | 128 | /** Gives the ethernet address of the mbed. |
mbed_official | 0:fd0d7bdfcdc2 | 129 | * |
mbed_official | 0:fd0d7bdfcdc2 | 130 | * @param mac Must be a pointer to a 6 byte char array to copy the ethernet address in. |
mbed_official | 0:fd0d7bdfcdc2 | 131 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 132 | void address(char *mac); |
mbed_official | 0:fd0d7bdfcdc2 | 133 | |
mbed_official | 0:fd0d7bdfcdc2 | 134 | /** Returns if an ethernet link is pressent or not. It takes a wile after Ethernet initializion to show up. |
mbed_official | 0:fd0d7bdfcdc2 | 135 | * |
mbed_official | 0:fd0d7bdfcdc2 | 136 | * @returns |
mbed_official | 0:fd0d7bdfcdc2 | 137 | * 0 if no ethernet link is pressent, |
mbed_official | 0:fd0d7bdfcdc2 | 138 | * 1 if an ethernet link is pressent. |
mbed_official | 0:fd0d7bdfcdc2 | 139 | * |
mbed_official | 0:fd0d7bdfcdc2 | 140 | * Example: |
mbed_official | 0:fd0d7bdfcdc2 | 141 | * @code |
mbed_official | 0:fd0d7bdfcdc2 | 142 | * // Using the Ethernet link function |
mbed_official | 0:fd0d7bdfcdc2 | 143 | * #include "mbed.h" |
mbed_official | 0:fd0d7bdfcdc2 | 144 | * |
mbed_official | 0:fd0d7bdfcdc2 | 145 | * Ethernet eth; |
mbed_official | 0:fd0d7bdfcdc2 | 146 | * |
mbed_official | 0:fd0d7bdfcdc2 | 147 | * int main() { |
mbed_official | 0:fd0d7bdfcdc2 | 148 | * wait(1); // Needed after startup. |
mbed_official | 0:fd0d7bdfcdc2 | 149 | * if (eth.link()) { |
mbed_official | 0:fd0d7bdfcdc2 | 150 | * printf("online\n"); |
mbed_official | 0:fd0d7bdfcdc2 | 151 | * } else { |
mbed_official | 0:fd0d7bdfcdc2 | 152 | * printf("offline\n"); |
mbed_official | 0:fd0d7bdfcdc2 | 153 | * } |
mbed_official | 0:fd0d7bdfcdc2 | 154 | * } |
mbed_official | 0:fd0d7bdfcdc2 | 155 | * @endcode |
mbed_official | 0:fd0d7bdfcdc2 | 156 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 157 | int link(); |
mbed_official | 0:fd0d7bdfcdc2 | 158 | |
mbed_official | 0:fd0d7bdfcdc2 | 159 | /** Sets the speed and duplex parameters of an ethernet link |
mbed_official | 0:fd0d7bdfcdc2 | 160 | * |
mbed_official | 0:fd0d7bdfcdc2 | 161 | * - AutoNegotiate Auto negotiate speed and duplex |
mbed_official | 0:fd0d7bdfcdc2 | 162 | * - HalfDuplex10 10 Mbit, half duplex |
mbed_official | 0:fd0d7bdfcdc2 | 163 | * - FullDuplex10 10 Mbit, full duplex |
mbed_official | 0:fd0d7bdfcdc2 | 164 | * - HalfDuplex100 100 Mbit, half duplex |
mbed_official | 0:fd0d7bdfcdc2 | 165 | * - FullDuplex100 100 Mbit, full duplex |
mbed_official | 0:fd0d7bdfcdc2 | 166 | * |
mbed_official | 0:fd0d7bdfcdc2 | 167 | * @param mode the speed and duplex mode to set the link to: |
mbed_official | 0:fd0d7bdfcdc2 | 168 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 169 | void set_link(Mode mode); |
mbed_official | 0:fd0d7bdfcdc2 | 170 | }; |
mbed_official | 0:fd0d7bdfcdc2 | 171 | |
mbed_official | 0:fd0d7bdfcdc2 | 172 | } // namespace mbed |
mbed_official | 0:fd0d7bdfcdc2 | 173 | |
mbed_official | 0:fd0d7bdfcdc2 | 174 | #endif |
mbed_official | 0:fd0d7bdfcdc2 | 175 | |
mbed_official | 0:fd0d7bdfcdc2 | 176 | #endif |