.

Dependents:   RTC

Committer:
jhon309
Date:
Thu Aug 13 00:20:09 2015 +0000
Revision:
0:88e313c910d0
RTC Example

Who changed what in which revision?

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