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