Rtos API example

Committer:
marcozecchini
Date:
Sat Feb 23 12:13:36 2019 +0000
Revision:
0:9fca2b23d0ba
final commit

Who changed what in which revision?

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