moccos mizuki / EthernetXpresso

Dependents:   XNetServicesMin

LPC1769Emac.h

Committer:
moccos
Date:
2012-05-06
Revision:
1:95a4c234aaf6
Parent:
0:b4bf563e9741
Child:
3:7ba8ebe32420

File content as of revision 1:95a4c234aaf6:

#ifndef LPC1769EMAC_H
#define LPC1769EMAC_H
#include <stdint.h>
#include "Frame.h"

/**
 * LPCXpresso LPC1769 ethernet library.
 * @author @moccos
 */
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