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