nothing changed
Fork of mbed by
Ethernet.h@1:27d81db81dd7, 2015-03-03 (annotated)
- Committer:
- Jing_Qiu
- Date:
- Tue Mar 03 00:55:57 2015 +0000
- Revision:
- 1:27d81db81dd7
- Parent:
- 0:a8fa94490a0a
revise
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gokmenascioglu | 0:a8fa94490a0a | 1 | /* mbed Microcontroller Library - Ethernet |
gokmenascioglu | 0:a8fa94490a0a | 2 | * Copyright (c) 2009-2011 ARM Limited. All rights reserved. |
gokmenascioglu | 0:a8fa94490a0a | 3 | */ |
gokmenascioglu | 0:a8fa94490a0a | 4 | |
gokmenascioglu | 0:a8fa94490a0a | 5 | #ifndef MBED_ETHERNET_H |
gokmenascioglu | 0:a8fa94490a0a | 6 | #define MBED_ETHERNET_H |
gokmenascioglu | 0:a8fa94490a0a | 7 | |
gokmenascioglu | 0:a8fa94490a0a | 8 | #include "device.h" |
gokmenascioglu | 0:a8fa94490a0a | 9 | |
gokmenascioglu | 0:a8fa94490a0a | 10 | #if DEVICE_ETHERNET |
gokmenascioglu | 0:a8fa94490a0a | 11 | |
gokmenascioglu | 0:a8fa94490a0a | 12 | #include "Base.h" |
gokmenascioglu | 0:a8fa94490a0a | 13 | |
gokmenascioglu | 0:a8fa94490a0a | 14 | namespace mbed { |
gokmenascioglu | 0:a8fa94490a0a | 15 | |
gokmenascioglu | 0:a8fa94490a0a | 16 | /* Class: Ethernet |
gokmenascioglu | 0:a8fa94490a0a | 17 | * An ethernet interface, to use with the ethernet pins. |
gokmenascioglu | 0:a8fa94490a0a | 18 | * |
gokmenascioglu | 0:a8fa94490a0a | 19 | * Example: |
gokmenascioglu | 0:a8fa94490a0a | 20 | * > // Read destination and source from every ethernet packet |
gokmenascioglu | 0:a8fa94490a0a | 21 | * > |
gokmenascioglu | 0:a8fa94490a0a | 22 | * > #include "mbed.h" |
gokmenascioglu | 0:a8fa94490a0a | 23 | * > |
gokmenascioglu | 0:a8fa94490a0a | 24 | * > Ethernet eth; |
gokmenascioglu | 0:a8fa94490a0a | 25 | * > |
gokmenascioglu | 0:a8fa94490a0a | 26 | * > int main() { |
gokmenascioglu | 0:a8fa94490a0a | 27 | * > char buf[0x600]; |
gokmenascioglu | 0:a8fa94490a0a | 28 | * > |
gokmenascioglu | 0:a8fa94490a0a | 29 | * > while(1) { |
gokmenascioglu | 0:a8fa94490a0a | 30 | * > int size = eth.receive(); |
gokmenascioglu | 0:a8fa94490a0a | 31 | * > if(size > 0) { |
gokmenascioglu | 0:a8fa94490a0a | 32 | * > eth.read(buf, size); |
gokmenascioglu | 0:a8fa94490a0a | 33 | * > printf("Destination: %02X:%02X:%02X:%02X:%02X:%02X\n", |
gokmenascioglu | 0:a8fa94490a0a | 34 | * > buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]); |
gokmenascioglu | 0:a8fa94490a0a | 35 | * > printf("Source: %02X:%02X:%02X:%02X:%02X:%02X\n", |
gokmenascioglu | 0:a8fa94490a0a | 36 | * > buf[6], buf[7], buf[8], buf[9], buf[10], buf[11]); |
gokmenascioglu | 0:a8fa94490a0a | 37 | * > } |
gokmenascioglu | 0:a8fa94490a0a | 38 | * > |
gokmenascioglu | 0:a8fa94490a0a | 39 | * > wait(1); |
gokmenascioglu | 0:a8fa94490a0a | 40 | * > } |
gokmenascioglu | 0:a8fa94490a0a | 41 | * > } |
gokmenascioglu | 0:a8fa94490a0a | 42 | * |
gokmenascioglu | 0:a8fa94490a0a | 43 | */ |
gokmenascioglu | 0:a8fa94490a0a | 44 | class Ethernet : public Base { |
gokmenascioglu | 0:a8fa94490a0a | 45 | |
gokmenascioglu | 0:a8fa94490a0a | 46 | public: |
gokmenascioglu | 0:a8fa94490a0a | 47 | |
gokmenascioglu | 0:a8fa94490a0a | 48 | /* Constructor: Ethernet |
gokmenascioglu | 0:a8fa94490a0a | 49 | * Initialise the ethernet interface. |
gokmenascioglu | 0:a8fa94490a0a | 50 | */ |
gokmenascioglu | 0:a8fa94490a0a | 51 | Ethernet(); |
gokmenascioglu | 0:a8fa94490a0a | 52 | |
gokmenascioglu | 0:a8fa94490a0a | 53 | /* Destructor: Ethernet |
gokmenascioglu | 0:a8fa94490a0a | 54 | * Powers the hardware down. |
gokmenascioglu | 0:a8fa94490a0a | 55 | */ |
gokmenascioglu | 0:a8fa94490a0a | 56 | virtual ~Ethernet(); |
gokmenascioglu | 0:a8fa94490a0a | 57 | |
gokmenascioglu | 0:a8fa94490a0a | 58 | enum Mode { |
gokmenascioglu | 0:a8fa94490a0a | 59 | AutoNegotiate |
gokmenascioglu | 0:a8fa94490a0a | 60 | , HalfDuplex10 |
gokmenascioglu | 0:a8fa94490a0a | 61 | , FullDuplex10 |
gokmenascioglu | 0:a8fa94490a0a | 62 | , HalfDuplex100 |
gokmenascioglu | 0:a8fa94490a0a | 63 | , FullDuplex100 |
gokmenascioglu | 0:a8fa94490a0a | 64 | }; |
gokmenascioglu | 0:a8fa94490a0a | 65 | |
gokmenascioglu | 0:a8fa94490a0a | 66 | /* Function: write |
gokmenascioglu | 0:a8fa94490a0a | 67 | * Writes into an outgoing ethernet packet. |
gokmenascioglu | 0:a8fa94490a0a | 68 | * |
gokmenascioglu | 0:a8fa94490a0a | 69 | * It will append size bytes of data to the previously written bytes. |
gokmenascioglu | 0:a8fa94490a0a | 70 | * |
gokmenascioglu | 0:a8fa94490a0a | 71 | * Variables: |
gokmenascioglu | 0:a8fa94490a0a | 72 | * data - An array to write. |
gokmenascioglu | 0:a8fa94490a0a | 73 | * size - The size of data. |
gokmenascioglu | 0:a8fa94490a0a | 74 | * |
gokmenascioglu | 0:a8fa94490a0a | 75 | * Returns: |
gokmenascioglu | 0:a8fa94490a0a | 76 | * The number of written bytes. |
gokmenascioglu | 0:a8fa94490a0a | 77 | */ |
gokmenascioglu | 0:a8fa94490a0a | 78 | int write(const char *data, int size); |
gokmenascioglu | 0:a8fa94490a0a | 79 | |
gokmenascioglu | 0:a8fa94490a0a | 80 | /* Function: send |
gokmenascioglu | 0:a8fa94490a0a | 81 | * Send an outgoing ethernet packet. |
gokmenascioglu | 0:a8fa94490a0a | 82 | * |
gokmenascioglu | 0:a8fa94490a0a | 83 | * After filling in the data in an ethernet packet it must be send. |
gokmenascioglu | 0:a8fa94490a0a | 84 | * Send will provide a new packet to write to. |
gokmenascioglu | 0:a8fa94490a0a | 85 | * |
gokmenascioglu | 0:a8fa94490a0a | 86 | * Returns: |
gokmenascioglu | 0:a8fa94490a0a | 87 | * 0 - If the sending was failed. |
gokmenascioglu | 0:a8fa94490a0a | 88 | * 1 - If the package is successfully sent. |
gokmenascioglu | 0:a8fa94490a0a | 89 | */ |
gokmenascioglu | 0:a8fa94490a0a | 90 | int send(); |
gokmenascioglu | 0:a8fa94490a0a | 91 | |
gokmenascioglu | 0:a8fa94490a0a | 92 | /* Function: receive |
gokmenascioglu | 0:a8fa94490a0a | 93 | * Recevies an arrived ethernet packet. |
gokmenascioglu | 0:a8fa94490a0a | 94 | * |
gokmenascioglu | 0:a8fa94490a0a | 95 | * Receiving an ethernet packet will drop the last received ethernet packet |
gokmenascioglu | 0:a8fa94490a0a | 96 | * and make a new ethernet packet ready to read. |
gokmenascioglu | 0:a8fa94490a0a | 97 | * If no ethernet packet is arrived it will return 0. |
gokmenascioglu | 0:a8fa94490a0a | 98 | * |
gokmenascioglu | 0:a8fa94490a0a | 99 | * Returns: |
gokmenascioglu | 0:a8fa94490a0a | 100 | * 0 - If no ethernet packet is arrived. |
gokmenascioglu | 0:a8fa94490a0a | 101 | * The size of the arrived packet. |
gokmenascioglu | 0:a8fa94490a0a | 102 | */ |
gokmenascioglu | 0:a8fa94490a0a | 103 | int receive(); |
gokmenascioglu | 0:a8fa94490a0a | 104 | |
gokmenascioglu | 0:a8fa94490a0a | 105 | /* Function: read |
gokmenascioglu | 0:a8fa94490a0a | 106 | * Read from an recevied ethernet packet. |
gokmenascioglu | 0:a8fa94490a0a | 107 | * |
gokmenascioglu | 0:a8fa94490a0a | 108 | * After receive returnd a number bigger than 0it is |
gokmenascioglu | 0:a8fa94490a0a | 109 | * possible to read bytes from this packet. |
gokmenascioglu | 0:a8fa94490a0a | 110 | * Read will write up to size bytes into data. |
gokmenascioglu | 0:a8fa94490a0a | 111 | * |
gokmenascioglu | 0:a8fa94490a0a | 112 | * It is possible to use read multible times. |
gokmenascioglu | 0:a8fa94490a0a | 113 | * Each time read will start reading after the last read byte before. |
gokmenascioglu | 0:a8fa94490a0a | 114 | * |
gokmenascioglu | 0:a8fa94490a0a | 115 | * Returns: |
gokmenascioglu | 0:a8fa94490a0a | 116 | * The number of byte read. |
gokmenascioglu | 0:a8fa94490a0a | 117 | */ |
gokmenascioglu | 0:a8fa94490a0a | 118 | int read(char *data, int size); |
gokmenascioglu | 0:a8fa94490a0a | 119 | |
gokmenascioglu | 0:a8fa94490a0a | 120 | /* Function: address |
gokmenascioglu | 0:a8fa94490a0a | 121 | * Gives the ethernet address of the mbed. |
gokmenascioglu | 0:a8fa94490a0a | 122 | * |
gokmenascioglu | 0:a8fa94490a0a | 123 | * Variables: |
gokmenascioglu | 0:a8fa94490a0a | 124 | * mac - Must be a pointer to a 6 byte char array to copy the ethernet address in. |
gokmenascioglu | 0:a8fa94490a0a | 125 | */ |
gokmenascioglu | 0:a8fa94490a0a | 126 | void address(char *mac); |
gokmenascioglu | 0:a8fa94490a0a | 127 | |
gokmenascioglu | 0:a8fa94490a0a | 128 | /* Function: link |
gokmenascioglu | 0:a8fa94490a0a | 129 | * Returns if an ethernet link is pressent or not. It takes a wile after Ethernet initializion to show up. |
gokmenascioglu | 0:a8fa94490a0a | 130 | * |
gokmenascioglu | 0:a8fa94490a0a | 131 | * Returns: |
gokmenascioglu | 0:a8fa94490a0a | 132 | * 0 - If no ethernet link is pressent. |
gokmenascioglu | 0:a8fa94490a0a | 133 | * 1 - If an ethernet link is pressent. |
gokmenascioglu | 0:a8fa94490a0a | 134 | * |
gokmenascioglu | 0:a8fa94490a0a | 135 | * Example: |
gokmenascioglu | 0:a8fa94490a0a | 136 | * > // Using the Ethernet link function |
gokmenascioglu | 0:a8fa94490a0a | 137 | * > #include "mbed.h" |
gokmenascioglu | 0:a8fa94490a0a | 138 | * > |
gokmenascioglu | 0:a8fa94490a0a | 139 | * > Ethernet eth; |
gokmenascioglu | 0:a8fa94490a0a | 140 | * > |
gokmenascioglu | 0:a8fa94490a0a | 141 | * > int main() { |
gokmenascioglu | 0:a8fa94490a0a | 142 | * > wait(1); // Needed after startup. |
gokmenascioglu | 0:a8fa94490a0a | 143 | * > if(eth.link()) { |
gokmenascioglu | 0:a8fa94490a0a | 144 | * > printf("online\n"); |
gokmenascioglu | 0:a8fa94490a0a | 145 | * > } else { |
gokmenascioglu | 0:a8fa94490a0a | 146 | * > printf("offline\n"); |
gokmenascioglu | 0:a8fa94490a0a | 147 | * > } |
gokmenascioglu | 0:a8fa94490a0a | 148 | * > } |
gokmenascioglu | 0:a8fa94490a0a | 149 | * |
gokmenascioglu | 0:a8fa94490a0a | 150 | */ |
gokmenascioglu | 0:a8fa94490a0a | 151 | int link(); |
gokmenascioglu | 0:a8fa94490a0a | 152 | |
gokmenascioglu | 0:a8fa94490a0a | 153 | /* Function: set_link |
gokmenascioglu | 0:a8fa94490a0a | 154 | * Sets the speed and duplex parameters of an ethernet link |
gokmenascioglu | 0:a8fa94490a0a | 155 | * |
gokmenascioglu | 0:a8fa94490a0a | 156 | * Variables: |
gokmenascioglu | 0:a8fa94490a0a | 157 | * mode - the speed and duplex mode to set the link to: |
gokmenascioglu | 0:a8fa94490a0a | 158 | * |
gokmenascioglu | 0:a8fa94490a0a | 159 | * > AutoNegotiate Auto negotiate speed and duplex |
gokmenascioglu | 0:a8fa94490a0a | 160 | * > HalfDuplex10 10 Mbit, half duplex |
gokmenascioglu | 0:a8fa94490a0a | 161 | * > FullDuplex10 10 Mbit, full duplex |
gokmenascioglu | 0:a8fa94490a0a | 162 | * > HalfDuplex100 100 Mbit, half duplex |
gokmenascioglu | 0:a8fa94490a0a | 163 | * > FullDuplex100 100 Mbit, full duplex |
gokmenascioglu | 0:a8fa94490a0a | 164 | */ |
gokmenascioglu | 0:a8fa94490a0a | 165 | void set_link(Mode mode); |
gokmenascioglu | 0:a8fa94490a0a | 166 | |
gokmenascioglu | 0:a8fa94490a0a | 167 | }; |
gokmenascioglu | 0:a8fa94490a0a | 168 | |
gokmenascioglu | 0:a8fa94490a0a | 169 | } // namespace mbed |
gokmenascioglu | 0:a8fa94490a0a | 170 | |
gokmenascioglu | 0:a8fa94490a0a | 171 | #endif |
gokmenascioglu | 0:a8fa94490a0a | 172 | |
gokmenascioglu | 0:a8fa94490a0a | 173 | #endif |