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