test

Dependencies:   mbed Watchdog

Dependents:   STM32-MC_node

Committer:
ommpy
Date:
Mon Jul 06 17:18:59 2020 +0530
Revision:
0:d383e2dee0f7
first commit

Who changed what in which revision?

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