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