Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
EthernetXpresso.h@4:7c859e671f9c, 2012-05-06 (annotated)
- Committer:
- moccos
- Date:
- Sun May 06 11:35:17 2012 +0000
- Revision:
- 4:7c859e671f9c
- Parent:
- 1:95a4c234aaf6
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
moccos | 4:7c859e671f9c | 1 | /* mbed-like wrapper class for LPCXpresso LPC1769 ethernet functions |
moccos | 4:7c859e671f9c | 2 | * Copyright (c) 2012 moccos |
moccos | 4:7c859e671f9c | 3 | * |
moccos | 4:7c859e671f9c | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
moccos | 4:7c859e671f9c | 5 | * of this software and associated documentation files (the "Software"), to deal |
moccos | 4:7c859e671f9c | 6 | * in the Software without restriction, including without limitation the rights |
moccos | 4:7c859e671f9c | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
moccos | 4:7c859e671f9c | 8 | * copies of the Software, and to permit persons to whom the Software is |
moccos | 4:7c859e671f9c | 9 | * furnished to do so, subject to the following conditions: |
moccos | 4:7c859e671f9c | 10 | * |
moccos | 4:7c859e671f9c | 11 | * The above copyright notice and this permission notice shall be included in |
moccos | 4:7c859e671f9c | 12 | * all copies or substantial portions of the Software. |
moccos | 4:7c859e671f9c | 13 | * |
moccos | 4:7c859e671f9c | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
moccos | 4:7c859e671f9c | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
moccos | 4:7c859e671f9c | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
moccos | 4:7c859e671f9c | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
moccos | 4:7c859e671f9c | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
moccos | 4:7c859e671f9c | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
moccos | 4:7c859e671f9c | 20 | * THE SOFTWARE. |
moccos | 4:7c859e671f9c | 21 | */ |
moccos | 0:b4bf563e9741 | 22 | #ifndef ETHERNET_XPRESSO_H |
moccos | 0:b4bf563e9741 | 23 | #define ETHERNET_XPRESSO_H |
moccos | 0:b4bf563e9741 | 24 | #include <stdint.h> |
moccos | 0:b4bf563e9741 | 25 | #include "LPC1769Emac.h" |
moccos | 0:b4bf563e9741 | 26 | |
moccos | 1:95a4c234aaf6 | 27 | /** |
moccos | 4:7c859e671f9c | 28 | * mbed-like wrapper class for LPCXpresso LPC1769 ethernet functions. |
moccos | 4:7c859e671f9c | 29 | * @author moccos <tt.izawa, gmail> |
moccos | 1:95a4c234aaf6 | 30 | */ |
moccos | 0:b4bf563e9741 | 31 | class EthernetXpresso { |
moccos | 0:b4bf563e9741 | 32 | public: |
moccos | 0:b4bf563e9741 | 33 | EthernetXpresso(); |
moccos | 0:b4bf563e9741 | 34 | virtual ~EthernetXpresso(); |
moccos | 0:b4bf563e9741 | 35 | |
moccos | 1:95a4c234aaf6 | 36 | // same as mbed |
moccos | 0:b4bf563e9741 | 37 | enum Mode { |
moccos | 0:b4bf563e9741 | 38 | AutoNegotiate |
moccos | 0:b4bf563e9741 | 39 | , HalfDuplex10 |
moccos | 0:b4bf563e9741 | 40 | , FullDuplex10 |
moccos | 0:b4bf563e9741 | 41 | , HalfDuplex100 |
moccos | 0:b4bf563e9741 | 42 | , FullDuplex100 |
moccos | 0:b4bf563e9741 | 43 | }; |
moccos | 1:95a4c234aaf6 | 44 | /** |
moccos | 0:b4bf563e9741 | 45 | * Writes into an outgoing ethernet packet. |
moccos | 0:b4bf563e9741 | 46 | * It will append size bytes of data to the previously written bytes. |
moccos | 0:b4bf563e9741 | 47 | * |
moccos | 1:95a4c234aaf6 | 48 | * @param data An array to write. |
moccos | 1:95a4c234aaf6 | 49 | * @param size The size of data. |
moccos | 1:95a4c234aaf6 | 50 | * @return The number of written bytes. |
moccos | 0:b4bf563e9741 | 51 | */ |
moccos | 0:b4bf563e9741 | 52 | int write(const char *data, int size) { return emac_.Write((void*)data, size); } |
moccos | 0:b4bf563e9741 | 53 | |
moccos | 1:95a4c234aaf6 | 54 | /** |
moccos | 0:b4bf563e9741 | 55 | * Send an outgoing ethernet packet. |
moccos | 0:b4bf563e9741 | 56 | * After filling in the data in an ethernet packet it must be send. |
moccos | 0:b4bf563e9741 | 57 | * Send will provide a new packet to write to. |
moccos | 0:b4bf563e9741 | 58 | * |
moccos | 1:95a4c234aaf6 | 59 | * @retval 0 If the sending was failed. |
moccos | 1:95a4c234aaf6 | 60 | * @retval 1 If the package is successfully sent. |
moccos | 0:b4bf563e9741 | 61 | */ |
moccos | 0:b4bf563e9741 | 62 | int send() { return emac_.Send() ? 1 : 0; } |
moccos | 0:b4bf563e9741 | 63 | |
moccos | 1:95a4c234aaf6 | 64 | /** |
moccos | 0:b4bf563e9741 | 65 | * Recevies an arrived ethernet packet. |
moccos | 0:b4bf563e9741 | 66 | * |
moccos | 0:b4bf563e9741 | 67 | * Receiving an ethernet packet will drop the last received ethernet packet |
moccos | 0:b4bf563e9741 | 68 | * and make a new ethernet packet ready to read. |
moccos | 0:b4bf563e9741 | 69 | * If no ethernet packet is arrived it will return 0. |
moccos | 0:b4bf563e9741 | 70 | * |
moccos | 1:95a4c234aaf6 | 71 | * @return The size of the arrived packet. |
moccos | 0:b4bf563e9741 | 72 | */ |
moccos | 0:b4bf563e9741 | 73 | int receive() { return emac_.ReadyToReceive(); } |
moccos | 0:b4bf563e9741 | 74 | |
moccos | 1:95a4c234aaf6 | 75 | /** |
moccos | 0:b4bf563e9741 | 76 | * Read from an recevied ethernet packet. |
moccos | 0:b4bf563e9741 | 77 | * |
moccos | 1:95a4c234aaf6 | 78 | * After receive returnd a number bigger than 0, it is |
moccos | 0:b4bf563e9741 | 79 | * possible to read bytes from this packet. |
moccos | 0:b4bf563e9741 | 80 | * Read will write up to size bytes into data. |
moccos | 0:b4bf563e9741 | 81 | * |
moccos | 0:b4bf563e9741 | 82 | * It is possible to use read multible times. |
moccos | 0:b4bf563e9741 | 83 | * Each time read will start reading after the last read byte before. |
moccos | 0:b4bf563e9741 | 84 | * |
moccos | 1:95a4c234aaf6 | 85 | * @return The number of byte read. |
moccos | 0:b4bf563e9741 | 86 | */ |
moccos | 1:95a4c234aaf6 | 87 | int read(char *data, int size) { return emac_.Read((void*)data, size); } |
moccos | 0:b4bf563e9741 | 88 | |
moccos | 1:95a4c234aaf6 | 89 | /** |
moccos | 1:95a4c234aaf6 | 90 | * Gives the ethernet address. |
moccos | 0:b4bf563e9741 | 91 | * |
moccos | 1:95a4c234aaf6 | 92 | * @param mac Must be a pointer to a 6 byte char array to copy the ethernet address in. |
moccos | 0:b4bf563e9741 | 93 | */ |
moccos | 0:b4bf563e9741 | 94 | void address(char *mac) { |
moccos | 1:95a4c234aaf6 | 95 | emac_.UpdateAddress(mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); |
moccos | 0:b4bf563e9741 | 96 | } |
moccos | 0:b4bf563e9741 | 97 | |
moccos | 1:95a4c234aaf6 | 98 | /** |
moccos | 0:b4bf563e9741 | 99 | * Returns if an ethernet link is pressent or not. It takes a wile after Ethernet initializion to show up. |
moccos | 0:b4bf563e9741 | 100 | * |
moccos | 1:95a4c234aaf6 | 101 | * @retval 0 If no ethernet link is pressent. |
moccos | 1:95a4c234aaf6 | 102 | * @retval 1 If an ethernet link is pressent. |
moccos | 0:b4bf563e9741 | 103 | */ |
moccos | 0:b4bf563e9741 | 104 | int link() { return emac_.Link() ? 1 : 0; } |
moccos | 0:b4bf563e9741 | 105 | |
moccos | 1:95a4c234aaf6 | 106 | /** |
moccos | 0:b4bf563e9741 | 107 | * Sets the speed and duplex parameters of an ethernet link |
moccos | 0:b4bf563e9741 | 108 | * |
moccos | 1:95a4c234aaf6 | 109 | * note: currently auto-neg only |
moccos | 1:95a4c234aaf6 | 110 | * @param mode the speed and duplex mode to set the link to: |
moccos | 0:b4bf563e9741 | 111 | */ |
moccos | 0:b4bf563e9741 | 112 | void set_link(Mode mode); |
moccos | 0:b4bf563e9741 | 113 | |
moccos | 1:95a4c234aaf6 | 114 | /** |
moccos | 1:95a4c234aaf6 | 115 | * Gets the ethernet address. |
moccos | 1:95a4c234aaf6 | 116 | */ |
moccos | 0:b4bf563e9741 | 117 | const char* getHwAddr() { return emac_.getHwAddr(); } |
moccos | 0:b4bf563e9741 | 118 | |
moccos | 0:b4bf563e9741 | 119 | private: |
moccos | 0:b4bf563e9741 | 120 | LPC1769Emac emac_; |
moccos | 0:b4bf563e9741 | 121 | |
moccos | 0:b4bf563e9741 | 122 | private: |
moccos | 0:b4bf563e9741 | 123 | bool ResetEmac_(); |
moccos | 0:b4bf563e9741 | 124 | }; |
moccos | 0:b4bf563e9741 | 125 | |
moccos | 0:b4bf563e9741 | 126 | #endif |