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