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