The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Dependents:   hello SerialTestv11 SerialTestv12 Sierpinski ... more

mbed 2

This is the mbed 2 library. If you'd like to learn about Mbed OS please see the mbed-os docs.

Committer:
Kojto
Date:
Wed Jul 19 16:46:19 2017 +0100
Revision:
147:a97add6d7e64
Parent:
146:22da6e220af6
Release 147 of the mbed library.

Who changed what in which revision?

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