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