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