moccos mizuki / EthernetXpresso

Dependents:   XNetServicesMin

EthernetXpresso.h

Committer:
moccos
Date:
2012-05-06
Revision:
4:7c859e671f9c
Parent:
1:95a4c234aaf6

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 ETHERNET_XPRESSO_H
#define ETHERNET_XPRESSO_H
#include <stdint.h>
#include "LPC1769Emac.h"

/**
 * mbed-like wrapper class for LPCXpresso LPC1769 ethernet functions.
 * @author moccos <tt.izawa, gmail>
 */
class EthernetXpresso {
public:
    EthernetXpresso();
    virtual ~EthernetXpresso();

    // same as mbed
    enum Mode {
        AutoNegotiate
        , HalfDuplex10
        , FullDuplex10
        , HalfDuplex100
        , FullDuplex100
    };
    /**
     *  Writes into an outgoing ethernet packet.
     *  It will append size bytes of data to the previously written bytes.
     *
     *  @param data An array to write.
     *  @param size The size of data.
     *  @return The number of written bytes.
     */
    int write(const char *data, int size) { return emac_.Write((void*)data, size); }

    /**
     *  Send an outgoing ethernet packet.
     *  After filling in the data in an ethernet packet it must be send.
     *  Send will provide a new packet to write to.
     *
     *  @retval 0 If the sending was failed.
     *  @retval 1 If the package is successfully sent.
     */
    int send() { return emac_.Send() ? 1 : 0; }

    /**
     *  Recevies an arrived ethernet packet.
     *
     *  Receiving an ethernet packet will drop the last received ethernet packet
     *  and make a new ethernet packet ready to read.
     *  If no ethernet packet is arrived it will return 0.
     *
     *  @return The size of the arrived packet.
     */
    int receive() { return emac_.ReadyToReceive(); }

    /**
     *  Read from an recevied ethernet packet.
     *
     *  After receive returnd a number bigger than 0, it is
     *  possible to read bytes from this packet.
     *  Read will write up to size bytes into data.
     *
     *  It is possible to use read multible times.
     *  Each time read will start reading after the last read byte before.
     *
     *  @return The number of byte read.
     */
    int read(char *data, int size) { return emac_.Read((void*)data, size); }

    /**
     *  Gives the ethernet address.
     *
     *  @param mac Must be a pointer to a 6 byte char array to copy the ethernet address in.
     */
    void address(char *mac) {
        emac_.UpdateAddress(mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
    }

    /**
     *  Returns if an ethernet link is pressent or not. It takes a wile after Ethernet initializion to show up.
     *
     *  @retval 0 If no ethernet link is pressent.
     *  @retval 1 If an ethernet link is pressent.
     */
    int link() { return emac_.Link() ? 1 : 0; }

    /**
     *  Sets the speed and duplex parameters of an ethernet link
     *
     *  note: currently auto-neg only
     *  @param mode the speed and duplex mode to set the link to:
     */
    void set_link(Mode mode);
    
    /**
     *  Gets the ethernet address.
     */
    const char* getHwAddr() { return emac_.getHwAddr(); }

private:
    LPC1769Emac emac_;

private:
    bool ResetEmac_();
};

#endif