W5200 - SPI/LAN BRIDGE

Description

Library for interfacing Wiznet W5200 SPI/LAN Bridge

The W5200 chip is a Hardwired TCP/IP embedded Ethernet controller that enables easier LAN connection for embedded systems. It is a single chip solution to implement TCP/IP Stack, 10/100 Ethernet MAC and PHY.

OVERVIEW

W5200_Abstract:
This library can be used to handle the W5200 chip, a hardwired TCP/IP embedded Ethernet controller.

Features of W5200:
  • Single chip to implement TCP/IP Stack, 10/100 Ethernet MAC and PHY
  • Supports 8 independent sockets simultaneously
  • Support High Speed Serial Peripheral Interface (SPI)
  • Internal 32Kbytes Memory for Socket Tx/Rx Buffers

  • W5200_How_to_use:
    new lanHandle; #callback Main_AppInit() { /*prepare W5200 for use*/ W5200_Init(lanHandle); } /* application entry point */ main() { salve(Main_AppInit) } W5200_Event(handle, iEvent) { #log("W5200_%d_Event: %d", handle, iEvent); if(iEvent == W5200_EVENT_LINKUP) { #log("Link up"); } else if(iEvent == W5200_EVENT_LINKDOWN) { #log("Link down"); } } /* socket event handler */ W5200_SocketEvent(handle, socket, iEvent) { #log("W5200_%d_Socket_%d_Event: %d", handle, socket, iEvent); }

    REQUIRED

    W5200_Events:

    Available W5200 events

    W5200_EVENT_ERROR - internal ERROR occured
    W5200_EVENT_LINKUP - Link is up (physical connection)
    W5200_EVENT_LINKDOWN - Link is down (physical connection lost)
    This events will trigger the W5200_Event function.
    W5200_Event(handle, event)

    W5200 Eventhandler

    handle : s32 - device handle
    event : s32 - Eventcode, see W5200_Events
    The W5200_Event function must be implemented within usercode.
    W5200_SOCKET_EVENTS:

    Socket events

    W5200_SOCK_CLOSED = 0x00 closed
    W5200_SOCK_INIT = 0x13 init state
    W5200_SOCK_LISTEN = 0x14 listen state
    W5200_SOCK_SYNSENT = 0x15 connection state
    W5200_SOCK_SYNRECV = 0x16 connection state
    W5200_SOCK_ESTABLISHED = 0x17 success to connect
    W5200_SOCK_FIN_WAIT = 0x18 closing state
    W5200_SOCK_CLOSING = 0x1A closing state
    W5200_SOCK_TIME_WAIT = 0x1B closing state
    W5200_SOCK_CLOSE_WAIT = 0x1C closing state
    W5200_SOCK_LAST_ACK = 0x1D closing state
    W5200_SOCK_UDP = 0x22 udp socket
    W5200_SOCK_IPRAW = 0x32 ip raw mode socket
    W5200_SOCK_MACRAW = 0x42 mac raw mode socket
    W5200_SOCK_PPPOE = 0x5F pppoe socket
    This events will trigger the W5200_SocketEvent function.
    W5200_SocketEvent(handle, socket, Event)

    This function is called when an socket event happend

    handle : s32 - device handle
    socket : s32 - socket on which the event happend
    Event : s32 - Eventcode, see W5200_SOCKET_EVENTS
    The W5200_Event function must be implemented within usercode.

    BASIC

    W5200_Init(&handle, iSpi = 0, iRst = 2, iCS = 5, iPD = 3)

    Initializes the SPI communication with the W5200 and the control pins

    handle : s32 - device handle assigned
    iSpi : s32 - SPI Interface where W5200 is connected. default = 0
    iRst : s32 - pin for the reset signal. default = 2
    iCS : s32 - pin used for the chip select signal. default = 5
    iPD : s32 - pin used for power down mode. default = 3
    returns : s32
    OK - if successful
    ERROR - if Init failed
    Current only one W5200 device (handle) is supported.
    The default pin assignment is according to rapidM2M PoC LAN shield.
    TW5200_Setup:

    setup structure

    aIP{4} = Own IPv4 address
    aSub{4} = Subnetmask
    aGW{4} = Default Gateway
    aMAC{6} = MAC address
    aRxMem{8} = Rx Memory for every socket in kByte
    aTxMem{8} = Tx Memory for every socket in kByte
    The W5200 chip itself has no MAC address stored. Following mechanism is implemented:
    • If MAC address is provided through setup structure, this address is used
    • If no MAC address is provided (.aMAC = {0}), configuration entry LANBridgeMAC
    • from RM2M_REG_SYS_OTP is used. The availability of this configuration entry depends on the HW board.
    • If configuration entry is not available, MAC address is generated interally
    • based on rapidM2M serial number.
    /* Hardware setup */ static W5200_Setup[TW5200_Setup] = [ {192, 168, 10, 101}, /* dummy local IP Address */ {255, 255, 255, 0}, /* dummy SubnetMask Address */ {192, 168, 10, 1}, /* dummy Gateway Address */ {0x00, 0x08, 0xDC, 0x01, 0x02, 0x03}, /* dummy WizNet MAC Address */ {2,2,2,2,2,2,2,2}, /* Rx mem config */ {2,2,2,2,2,2,2,2} /* Tx mem config */ ];
    W5200_Setup(handle, const sSetup[TW5200_Setup])

    Setup W5200 with basic configuration from sSetupt

    handle : s32 - device handle
    sSetup : TW5200_Setup - setup config for W5200
    returns : s32
    OK - if setup successful
    ERROR - if setup failed
    W5200_Close(handle)

    Close W5200 driver for the selected handle (interface)

    The pins RST (Reset), CS (Chip select) and PD (power down) configured with W5200_Init function will be driven to OUTPUT Low.
    handle : s32 - device handle
    returns : s32
    OK - if successful
    ERROR - if device handle is not valid
    W5200_PowerUp(handle)

    Power up W5200 chip

    Deactivate PD (power down) pin configured with W5200_Init function
    handle : s32 - device handle
    returns : s32
    OK - if successful
    ERROR - if device handle is not valid
    W5200_PowerDown(handle)

    Enter W5200 power down mode

    Activate PD (power down) pin configured with W5200_Init function
    handle : s32 - device handle
    returns : s32
    OK - if successful
    ERROR - if device handle is not valid
    W5200_Reset(handle)

    Reset W5200 chip

    handle : s32 - device handle
    returns : s32
    OK - if successful
    ERROR - if device handle is not valid
    W5200_STATES:

    possible W5200 operating states

    W5200_STATE_OFF = 0 - W5200 off
    W5200_STATE_RESET = 1 - W5200 in reset state
    W5200_STATE_RESET_DELAY = 2 - W5200 in reset delay state
    W5200_STATE_READY = 3 - W5200 in ready state
    W5200_STATE_ERROR = 4 - W5200 in error state
    W5200_GetState(handle)

    This function returns the current W5200 operating state

    handle : s32 - device handle
    returns : s32
    W5200_STATES - if successfuly
    ERROR - if device handle is not valid

    SOCKET

    W5200_Sn_MR:

    Mode register values

    W5200_Sn_MR_CLOSE = 0x00 - unused socket
    W5200_Sn_MR_TCP = 0x01 - TCP
    W5200_Sn_MR_UDP = 0x02 - UDP
    W5200_Sn_MR_IPRAW = 0x03 - IP LAYER RAW SOCK
    W5200_Sn_MR_MACRAW = 0x04 - MAC LAYER RAW SOCK
    W5200_Sn_MR_PPPOE = 0x05 - PPPoE
    W5200_Sn_MR_ND = 0x20 - No Delayed Ack(TCP) flag
    W5200_Sn_MR_MULTI = 0x80 - support multicasting
    W5200_Socket(handle, socket, protocol, port=0, flags=0)

    Create a socket

    handle : s32 - device handle
    socket : s32 - socket which should be used (0 - 7)
    protocol : s32 - used protocol, see W5200_Sn_MR
    port : s32 - local port number, optional
    If local port number is not given (0), it will be defined within the library. Starting with port number 1000 that will be incremented with every call to W5200_Socket function. If port number 2000 is reached, it will be reset back to 1000 again.
    flags : s32 - additional flags, optional, see W5200_Sn_MR
    returns : s32
    OK - if successful
    ERROR - if any of the following error occurs
  • device handle not valid
  • if any other socket command is currently active (e.g. CLOSE)
  • W5200_SocketClose(handle, socket)

    Close Socket

    handle : s32 - device handle
    socket : s32 - socket which should be used (0 - 7)
    returns : s32
    OK - if successful
    ERROR - if any of the following error occurs
  • device handle not valid
  • if any other socket command is currently active (e.g. DISCONNECT)
  • W5200_Connect(handle, socket, IP{4}, port)

    TCP Client: Connect to TCP server

    handle : s32 - device handle
    socket : s32 - socket which should be connected
    IP{4} : u8 - destination ip address to connect (server ip)
    Port : s32 - destination port to connect
    returns : s32
    OK - if successful
    ERROR - if any of the following error occurs
  • device handle is not valid
  • socket state is not valid
  • The W5200_Connect function works non blocking. If the function returns, TCP connect was only started. Userfunction W5200_SocketEvent is called (event W5200_SOCK_ESTABLISHED) if Connect finished successfully.
    W5200_Disconnect(handle, socket)

    TCP Client: Disconnect from TCP Server

    handle : s32 - device handle
    socket : s32 - socket which should be disconnected
    returns : s32
    OK - if successful
    ERROR - if any of the following error occurs
  • device handle is not valid
  • socket state is not valid
  • another socket command is currently active (e.g. SEND)
  • The W5200_Disconnect function works non blocking. If the functions returns, TCP connection is not yet fully closed.
    W5200_Send(handle, socket, data{}, len)

    TCP: Send data to a connected device

    handle : s32 - device handle
    socket : s32 - socket on which the data should be sent
    data{} : u8 - data which should be sent
    len : s32 - number of bytes to send
    returns : s32
    size - number of bytes written to socket
    If the returned size is smaller than len, there is probably not enough space within socket transmit buffer. W5200_Send function must be called again starting at proper offset within data{} buffer.
    ERROR - if any of the following error occurs
  • device handle is not valid
  • another socket command is currently active (e.g. DISCONNECT)
  • The W5200_Send function works non blocking. If the functions returns, data bytes are only copied to W5200 socket send buffer and probably (not yet) sent physically.
    W5200_Recv(handle, socket, dataBuf{}, bufLen)

    Receive the data in TCP mode.

    handle : s32 - device handle
    socket : s32 - socket on which the data is received
    dataBuf{} : u8 - buffer where the received data should be stored
    bufLen : s32 - number of bytes available in dataBuf{}
    returns : s32
    size - received data size, i.e. number of bytes stored in dataBuf{}
    0 if no receive data is available or another socket command is currently active (e.g. send). If return value equals parameter bufLen it is recommended to call W5200_Recv() again immediately. There is probably more receive data available.
    ERROR - if device handle is not valid

    EXPERT

    W5200_Read(handle, regAddr, len, data{})

    Directly read W5200 register values

    handle : s32 - device handle
    regAddr : s32 - register address
    len : s32 - number of bytes to read
    data{} : u8 - buffer where read data is stored
    returns : s32
    OK - if successful
    ERROR - if device handle is not valid
    W5200_Write(handle, regAddr, len, data{})

    Directly write to W5200 registers

    handle : s32 - device handle
    regAddr : s32 - register address
    len : s32 - number of bytes to write
    data{} : u8 - data to write
    returns : s32
    OK - if successful
    ERROR - if device handle is not valid

    On this page

    W5200 - SPI/LAN BRIDGE