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