Backup 1

Committer:
borlanic
Date:
Tue Apr 24 11:45:18 2018 +0000
Revision:
0:02dd72d1d465
BaBoRo_test2 - backup 1

Who changed what in which revision?

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