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