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