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