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