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