moccos mizuki / EthernetXpresso

Dependents:   XNetServicesMin

LPC1769Emac.h

Committer:
moccos
Date:
2012-05-06
Revision:
4:7c859e671f9c
Parent:
3:7ba8ebe32420

File content as of revision 4:7c859e671f9c:

/* mbed-like wrapper class for LPCXpresso LPC1769 ethernet functions
 * Copyright (c) 2012 moccos
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
#ifndef LPC1769EMAC_H
#define LPC1769EMAC_H
#include <stdint.h>
#include "Frame.h"

/**
 * LPCXpresso LPC1769 ethernet library.
 * @author moccos <tt.izawa, gmail>
 */
class LPC1769Emac {
public:
    // same as mbed
    enum LinkMode {
        AutoNegotiate,
        HalfDuplex10,
        FullDuplex10,
        HalfDuplex100,
        FullDuplex100
    };

public:
    /**
     * Enable LPC1769 ethernet block.
     */
    LPC1769Emac();
    
    /**
     * Disable LPC1769 ethernet block.
     */
    ~LPC1769Emac();
    
    /**
     * Write 16-bit value to PHY register.
     * @param reg register index. See LAN8710AReg.h.
     * @param value
     */
    bool PhyWrite(uint8_t reg, uint16_t value);
    
    /**
     * Read 16-bit value from PHY register.
     * @param reg register index. See LAN8710AReg.h.
     */
    uint16_t PhyRead(uint8_t reg);
    
    /**
     * Set ethernet address before initialization for debug.
     */
    static void SetAddress(uint8_t a5, uint8_t a4, uint8_t a3, uint8_t a2, uint8_t a1, uint8_t a0);

    /**
     * Update ethernet address.
     */
    void UpdateAddress(uint8_t a5, uint8_t a4, uint8_t a3, uint8_t a2, uint8_t a1, uint8_t a0);
    
    /**
     * Enable RX flags.
     */
    void StartRx();

    /**
     * Enable TX flags.
     */
    void StartTx();

    /**
     * Disable RX flags.
     */
    void StopRx();

    /**
     * Disable TX flags.
     */
    void StopTx();

    /**
     * Check link status.
     * @return true if the link is up
     */
    bool Link();
    
    /**
     * Read data from received packet.
     * @param buf destination buffer
     * @param max_size
     */
    uint16_t Read(void *buf, uint16_t max_size);
    
    /**
     * Check received packet.
     * @return The size of readabe data
     */
    uint16_t ReadyToReceive();
    
    /**
     * Write data to the TX buffer.
     * @param buf data
     * @size
     */
    uint16_t Write(void *buf, uint16_t size);
    
    /**
     * Send data from the TX buffer to the network.
     */
    bool Send();
    
    /**
     * Send data from the user buffer to the network.
     * Current TX buffer is overwritten.
     * @param buf data
     * @size
     */
    bool Send(void *buf, uint16_t size);
    
    /**
     * Reset ethernet registers and PHY registers.
     * @param Linkmode select auto-negotiation or fixed link speed and duplex
     */
    bool Reset(LinkMode mode=AutoNegotiate);
    
    /**
     * Gets ethernet address.
     * @return ethernet address
     */
    static const char* getHwAddr() { return (const char*)mac_; }

private:
    // The number of RX resources
    static const uint8_t N_RX_BUF = 5;
    // The number of TX resources
    static const uint8_t N_TX_BUF = 3;
    static const uint16_t PHY_ADDR = 0x0100;
    static uint8_t mac_[6];
    static Descriptor rx_desc_[N_RX_BUF];
    static Descriptor tx_desc_[N_TX_BUF];
    static StatusRx rx_status_[N_RX_BUF];
    static StatusTx tx_status_[N_TX_BUF];
    static Frame rx_frame_[N_RX_BUF];
    static Frame tx_frame_[N_TX_BUF];
    uint8_t *write_next_;
    uint8_t *read_next_;
    uint16_t write_size_;
    uint16_t read_size_;

private:
    void WriteAddress_();
    bool CheckAutoNeg_();
    void InitRxBuffer_();
    void InitTxBuffer_();
};

#endif