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