initial

Dependencies:   mbed

Committer:
yihui
Date:
Mon Jan 11 02:32:24 2016 +0000
Revision:
0:638edba3adf6
initial

Who changed what in which revision?

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