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