moccos mizuki / EthernetXpresso

Dependents:   XNetServicesMin

Committer:
moccos
Date:
Sun May 06 11:35:17 2012 +0000
Revision:
4:7c859e671f9c
Parent:
3:7ba8ebe32420

        

Who changed what in which revision?

UserRevisionLine numberNew 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 LPC1769EMAC_H
moccos 0:b4bf563e9741 23 #define LPC1769EMAC_H
moccos 0:b4bf563e9741 24 #include <stdint.h>
moccos 0:b4bf563e9741 25 #include "Frame.h"
moccos 0:b4bf563e9741 26
moccos 1:95a4c234aaf6 27 /**
moccos 1:95a4c234aaf6 28 * LPCXpresso LPC1769 ethernet library.
moccos 4:7c859e671f9c 29 * @author moccos <tt.izawa, gmail>
moccos 1:95a4c234aaf6 30 */
moccos 0:b4bf563e9741 31 class LPC1769Emac {
moccos 0:b4bf563e9741 32 public:
moccos 1:95a4c234aaf6 33 // same as mbed
moccos 0:b4bf563e9741 34 enum LinkMode {
moccos 0:b4bf563e9741 35 AutoNegotiate,
moccos 0:b4bf563e9741 36 HalfDuplex10,
moccos 0:b4bf563e9741 37 FullDuplex10,
moccos 0:b4bf563e9741 38 HalfDuplex100,
moccos 0:b4bf563e9741 39 FullDuplex100
moccos 0:b4bf563e9741 40 };
moccos 0:b4bf563e9741 41
moccos 0:b4bf563e9741 42 public:
moccos 1:95a4c234aaf6 43 /**
moccos 1:95a4c234aaf6 44 * Enable LPC1769 ethernet block.
moccos 1:95a4c234aaf6 45 */
moccos 0:b4bf563e9741 46 LPC1769Emac();
moccos 1:95a4c234aaf6 47
moccos 1:95a4c234aaf6 48 /**
moccos 1:95a4c234aaf6 49 * Disable LPC1769 ethernet block.
moccos 1:95a4c234aaf6 50 */
moccos 0:b4bf563e9741 51 ~LPC1769Emac();
moccos 1:95a4c234aaf6 52
moccos 1:95a4c234aaf6 53 /**
moccos 1:95a4c234aaf6 54 * Write 16-bit value to PHY register.
moccos 1:95a4c234aaf6 55 * @param reg register index. See LAN8710AReg.h.
moccos 1:95a4c234aaf6 56 * @param value
moccos 1:95a4c234aaf6 57 */
moccos 0:b4bf563e9741 58 bool PhyWrite(uint8_t reg, uint16_t value);
moccos 1:95a4c234aaf6 59
moccos 1:95a4c234aaf6 60 /**
moccos 1:95a4c234aaf6 61 * Read 16-bit value from PHY register.
moccos 1:95a4c234aaf6 62 * @param reg register index. See LAN8710AReg.h.
moccos 1:95a4c234aaf6 63 */
moccos 1:95a4c234aaf6 64 uint16_t PhyRead(uint8_t reg);
moccos 1:95a4c234aaf6 65
moccos 1:95a4c234aaf6 66 /**
moccos 1:95a4c234aaf6 67 * Set ethernet address before initialization for debug.
moccos 1:95a4c234aaf6 68 */
moccos 0:b4bf563e9741 69 static void SetAddress(uint8_t a5, uint8_t a4, uint8_t a3, uint8_t a2, uint8_t a1, uint8_t a0);
moccos 1:95a4c234aaf6 70
moccos 1:95a4c234aaf6 71 /**
moccos 1:95a4c234aaf6 72 * Update ethernet address.
moccos 1:95a4c234aaf6 73 */
moccos 1:95a4c234aaf6 74 void UpdateAddress(uint8_t a5, uint8_t a4, uint8_t a3, uint8_t a2, uint8_t a1, uint8_t a0);
moccos 1:95a4c234aaf6 75
moccos 1:95a4c234aaf6 76 /**
moccos 1:95a4c234aaf6 77 * Enable RX flags.
moccos 1:95a4c234aaf6 78 */
moccos 0:b4bf563e9741 79 void StartRx();
moccos 1:95a4c234aaf6 80
moccos 1:95a4c234aaf6 81 /**
moccos 1:95a4c234aaf6 82 * Enable TX flags.
moccos 1:95a4c234aaf6 83 */
moccos 0:b4bf563e9741 84 void StartTx();
moccos 1:95a4c234aaf6 85
moccos 1:95a4c234aaf6 86 /**
moccos 1:95a4c234aaf6 87 * Disable RX flags.
moccos 1:95a4c234aaf6 88 */
moccos 0:b4bf563e9741 89 void StopRx();
moccos 1:95a4c234aaf6 90
moccos 1:95a4c234aaf6 91 /**
moccos 1:95a4c234aaf6 92 * Disable TX flags.
moccos 1:95a4c234aaf6 93 */
moccos 0:b4bf563e9741 94 void StopTx();
moccos 1:95a4c234aaf6 95
moccos 1:95a4c234aaf6 96 /**
moccos 1:95a4c234aaf6 97 * Check link status.
moccos 1:95a4c234aaf6 98 * @return true if the link is up
moccos 1:95a4c234aaf6 99 */
moccos 0:b4bf563e9741 100 bool Link();
moccos 1:95a4c234aaf6 101
moccos 1:95a4c234aaf6 102 /**
moccos 1:95a4c234aaf6 103 * Read data from received packet.
moccos 1:95a4c234aaf6 104 * @param buf destination buffer
moccos 1:95a4c234aaf6 105 * @param max_size
moccos 1:95a4c234aaf6 106 */
moccos 1:95a4c234aaf6 107 uint16_t Read(void *buf, uint16_t max_size);
moccos 1:95a4c234aaf6 108
moccos 1:95a4c234aaf6 109 /**
moccos 1:95a4c234aaf6 110 * Check received packet.
moccos 1:95a4c234aaf6 111 * @return The size of readabe data
moccos 1:95a4c234aaf6 112 */
moccos 0:b4bf563e9741 113 uint16_t ReadyToReceive();
moccos 1:95a4c234aaf6 114
moccos 1:95a4c234aaf6 115 /**
moccos 1:95a4c234aaf6 116 * Write data to the TX buffer.
moccos 1:95a4c234aaf6 117 * @param buf data
moccos 1:95a4c234aaf6 118 * @size
moccos 1:95a4c234aaf6 119 */
moccos 0:b4bf563e9741 120 uint16_t Write(void *buf, uint16_t size);
moccos 1:95a4c234aaf6 121
moccos 1:95a4c234aaf6 122 /**
moccos 1:95a4c234aaf6 123 * Send data from the TX buffer to the network.
moccos 1:95a4c234aaf6 124 */
moccos 0:b4bf563e9741 125 bool Send();
moccos 1:95a4c234aaf6 126
moccos 1:95a4c234aaf6 127 /**
moccos 1:95a4c234aaf6 128 * Send data from the user buffer to the network.
moccos 1:95a4c234aaf6 129 * Current TX buffer is overwritten.
moccos 1:95a4c234aaf6 130 * @param buf data
moccos 1:95a4c234aaf6 131 * @size
moccos 1:95a4c234aaf6 132 */
moccos 0:b4bf563e9741 133 bool Send(void *buf, uint16_t size);
moccos 1:95a4c234aaf6 134
moccos 1:95a4c234aaf6 135 /**
moccos 1:95a4c234aaf6 136 * Reset ethernet registers and PHY registers.
moccos 1:95a4c234aaf6 137 * @param Linkmode select auto-negotiation or fixed link speed and duplex
moccos 1:95a4c234aaf6 138 */
moccos 0:b4bf563e9741 139 bool Reset(LinkMode mode=AutoNegotiate);
moccos 1:95a4c234aaf6 140
moccos 1:95a4c234aaf6 141 /**
moccos 1:95a4c234aaf6 142 * Gets ethernet address.
moccos 1:95a4c234aaf6 143 * @return ethernet address
moccos 1:95a4c234aaf6 144 */
moccos 0:b4bf563e9741 145 static const char* getHwAddr() { return (const char*)mac_; }
moccos 0:b4bf563e9741 146
moccos 0:b4bf563e9741 147 private:
moccos 3:7ba8ebe32420 148 // The number of RX resources
moccos 0:b4bf563e9741 149 static const uint8_t N_RX_BUF = 5;
moccos 3:7ba8ebe32420 150 // The number of TX resources
moccos 0:b4bf563e9741 151 static const uint8_t N_TX_BUF = 3;
moccos 0:b4bf563e9741 152 static const uint16_t PHY_ADDR = 0x0100;
moccos 0:b4bf563e9741 153 static uint8_t mac_[6];
moccos 0:b4bf563e9741 154 static Descriptor rx_desc_[N_RX_BUF];
moccos 0:b4bf563e9741 155 static Descriptor tx_desc_[N_TX_BUF];
moccos 0:b4bf563e9741 156 static StatusRx rx_status_[N_RX_BUF];
moccos 0:b4bf563e9741 157 static StatusTx tx_status_[N_TX_BUF];
moccos 0:b4bf563e9741 158 static Frame rx_frame_[N_RX_BUF];
moccos 0:b4bf563e9741 159 static Frame tx_frame_[N_TX_BUF];
moccos 0:b4bf563e9741 160 uint8_t *write_next_;
moccos 0:b4bf563e9741 161 uint8_t *read_next_;
moccos 0:b4bf563e9741 162 uint16_t write_size_;
moccos 0:b4bf563e9741 163 uint16_t read_size_;
moccos 0:b4bf563e9741 164
moccos 0:b4bf563e9741 165 private:
moccos 0:b4bf563e9741 166 void WriteAddress_();
moccos 0:b4bf563e9741 167 bool CheckAutoNeg_();
moccos 0:b4bf563e9741 168 void InitRxBuffer_();
moccos 0:b4bf563e9741 169 void InitTxBuffer_();
moccos 0:b4bf563e9741 170 };
moccos 0:b4bf563e9741 171
moccos 0:b4bf563e9741 172 #endif