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