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