Commit 67b7333347c0e688d3a1c14a547d7a5fe96ae5c5

Authored by amoreau
1 parent f9271773

Ajout du squelette

Makefile 0 → 100644
@@ -0,0 +1,37 @@ @@ -0,0 +1,37 @@
  1 +#
  2 +# Makefile pour le projet filtrage réseau
  3 +#
  4 +
  5 +#
  6 +# Constantes pour la compilation des programmes
  7 +#
  8 +
  9 +export CC = gcc
  10 +export LD = gcc
  11 +export CLIB = ar cq
  12 +export CFLAGS = -Wall
  13 +export CDEBUG = -g -DDEBUG
  14 +
  15 +#
  16 +# Constantes liees au projet
  17 +#
  18 +
  19 +DIRS=Network Threads
  20 +
  21 +#
  22 +# La cible generale
  23 +#
  24 +
  25 +all: $(patsubst %, _dir_%, $(DIRS))
  26 +
  27 +$(patsubst %,_dir_%,$(DIRS)):
  28 + cd $(patsubst _dir_%,%,$@) && make
  29 +
  30 +#
  31 +# La cible de nettoyage
  32 +#
  33 +
  34 +clean: $(patsubst %, _clean_%, $(DIRS))
  35 +
  36 +$(patsubst %,_clean_%,$(DIRS)):
  37 + cd $(patsubst _clean_%,%,$@) && make clean
Network/Makefile 0 → 100644
@@ -0,0 +1,22 @@ @@ -0,0 +1,22 @@
  1 +#
  2 +# Makefile pour generer la bibliotheque de fonctions de communication
  3 +#
  4 +
  5 +all: libnet.a
  6 +
  7 +#
  8 +# La cible de nettoyage
  9 +#
  10 +
  11 +clean:
  12 + rm -f core *.o libnet.a
  13 +
  14 +#
  15 +# Les cibles pour la bibliotheque
  16 +#
  17 +
  18 +libnet.o: libnet.c libnet.h
  19 +
  20 +libcom.a: libnet.o
  21 + rm -f $@
  22 + $(CLIB) $@ $+
Tangible/Makefile 0 → 100644
@@ -0,0 +1,34 @@ @@ -0,0 +1,34 @@
  1 +MCU = atmega328p
  2 +FCPU = 16000000
  3 +
  4 +CC = avr-gcc
  5 +LD = avr-gcc
  6 +FLAGS = -mmcu=$(MCU)
  7 +CFLAGS = -Wall $(FLAGS) -DF_CPU=$(FCPU) -Os
  8 +LDFLAGS = $(FLAGS)
  9 +
  10 +DUDE = avrdude
  11 +PROTOCOL = stk500v1 # stk500 / stk500v1
  12 +SPEED = 115200 # 57600 / 115200
  13 +SERIAL = /dev/ttyACM0 # ttyUSB0 / ttyACM0
  14 +
  15 +TARGET = broadcast
  16 +SOURCES = $(wildcard *.c)
  17 +OBJECTS = $(SOURCES:.c=.o)
  18 +
  19 +all: $(TARGET).hex
  20 +
  21 +clean:
  22 + rm -f *.o $(TARGET).hex $(TARGET)
  23 +
  24 +$(TARGET): $(OBJECTS)
  25 +
  26 +$(TARGET).hex: $(TARGET)
  27 + avr-objcopy -j .text -j .data -O ihex $(TARGET) $(TARGET).hex
  28 +
  29 +upload: $(TARGET).hex
  30 + stty -F $(SERIAL) hupcl
  31 + $(DUDE) -F -v -p $(MCU) -c $(PROTOCOL) -b $(SPEED) -P $(SERIAL) -U flash:w:$(TARGET).hex
  32 +
  33 +size: $(TARGET)
  34 + avr-size --format=avr --mcu=$(MCU) $(TARGET)
Tangible/ethernet.c 0 → 100644
@@ -0,0 +1,15 @@ @@ -0,0 +1,15 @@
  1 +#include <stdint.h>
  2 +
  3 +#include "ethernet.h"
  4 +#include "w5100.h"
  5 +
  6 +/** Function for Ethernet hardware **/
  7 +
  8 +void ethernet_init(uint8_t *mac,uint8_t *ip,uint8_t *gateway,uint8_t *mask){
  9 +iinchip_init();
  10 +sysinit(0x55,0x55);
  11 +setSHAR(mac);
  12 +setSIPR(ip);
  13 +setGAR(gateway);
  14 +setSUBR(mask);
  15 +}
Tangible/ethernet.h 0 → 100755
@@ -0,0 +1,167 @@ @@ -0,0 +1,167 @@
  1 +/*
  2 +*
  3 +@file ethernet.h
  4 +*
  5 +*/
  6 +
  7 +#ifndef _ETHERNET_H_
  8 +#define _ETHERNET_H_
  9 +
  10 +
  11 +/***************************************************
  12 + * attribute for mcu ( types, ... )
  13 + ***************************************************/
  14 +//#include "mcu_define.h"
  15 +#define __MCU_AVR__ 1
  16 +#define __MCU_TYPE__ __MCU_AVR__
  17 +
  18 +//---- Refer "Rom File Maker Manual Vx.x.pdf"
  19 +#include <avr/pgmspace.h>
  20 +
  21 +#define _ENDIAN_LITTLE_ 0 /**< This must be defined if system is little-endian alignment */
  22 +#define _ENDIAN_BIG_ 1
  23 +#define SYSTEM_ENDIAN _ENDIAN_LITTLE_
  24 +
  25 +#define MAX_SOCK_NUM 4 /**< Maxmium number of socket */
  26 +#define CLK_CPU F_CPU /**< 8Mhz(for serial) */
  27 +
  28 +/* ## __DEF_IINCHIP_xxx__ : define option for iinchip driver *****************/
  29 +#define __DEF_IINCHIP_DBG__ /* involve debug code in driver (socket.c) */
  30 +//#define __DEF_IINCHIP_INT__ /**< involve interrupt service routine (socket.c) */
  31 +//#define __DEF_IINCHIP_PPP__ /* involve pppoe routine (socket.c) */
  32 + /* If it is defined, the source files(md5.h,md5.c) must be included in your project.
  33 + Otherwize, the source files must be removed in your project. */
  34 +
  35 +#define __DEF_IINCHIP_DIRECT_MODE__ 1
  36 +#define __DEF_IINCHIP_INDIRECT_MODE__ 2
  37 +#define __DEF_IINCHIP_SPI_MODE__ 3
  38 +
  39 +//#define __DEF_IINCHIP_BUS__ __DEF_IINCHIP_DIRECT_MODE__
  40 +//#define __DEF_IINCHIP_BUS__ __DEF_IINCHIP_INDIRECT_MODE__
  41 +#define __DEF_IINCHIP_BUS__ __DEF_IINCHIP_SPI_MODE__ /*Enable SPI_mode*/
  42 +
  43 +
  44 +/**
  45 +@brief __DEF_IINCHIP_MAP_xxx__ : define memory map for iinchip
  46 +*/
  47 +#define __DEF_IINCHIP_MAP_BASE__ 0x8000
  48 +#if (__DEF_IINCHIP_BUS__ == __DEF_IINCHIP_DIRECT_MODE__)
  49 + #define COMMON_BASE __DEF_IINCHIP_MAP_BASE__
  50 +#else
  51 + #define COMMON_BASE 0x0000
  52 +#endif
  53 +#define __DEF_IINCHIP_MAP_TXBUF__ (COMMON_BASE + 0x4000) /* Internal Tx buffer address of the iinchip */
  54 +#define __DEF_IINCHIP_MAP_RXBUF__ (COMMON_BASE + 0x6000) /* Internal Rx buffer address of the iinchip */
  55 +
  56 +
  57 +#if (__MCU_TYPE__ == __MCU_AVR__)
  58 + #ifdef __DEF_IINCHIP_INT__
  59 + // iinchip use external interrupt 4
  60 + #define IINCHIP_ISR_DISABLE() (EIMSK &= ~(0x10))
  61 + #define IINCHIP_ISR_ENABLE() (EIMSK |= 0x10)
  62 + #define IINCHIP_ISR_GET(X) (X = EIMSK)
  63 + #define IINCHIP_ISR_SET(X) (EIMSK = X)
  64 + #else
  65 + #define IINCHIP_ISR_DISABLE()
  66 + #define IINCHIP_ISR_ENABLE()
  67 + #define IINCHIP_ISR_GET(X)
  68 + #define IINCHIP_ISR_SET(X)
  69 + #endif
  70 +#else
  71 +#error "unknown MCU type"
  72 +#endif
  73 +
  74 +#ifndef NULL
  75 +#define NULL ((void *) 0)
  76 +#endif
  77 +
  78 +//typedef enum { false, true } bool;
  79 +
  80 +#ifndef _SIZE_T
  81 +#define _SIZE_T
  82 +typedef unsigned int size_t;
  83 +#endif
  84 +
  85 +/**
  86 + * The 8-bit signed data type.
  87 + */
  88 +typedef char int8;
  89 +/**
  90 + * The volatile 8-bit signed data type.
  91 + */
  92 +typedef volatile char vint8;
  93 +/**
  94 + * The 8-bit unsigned data type.
  95 + */
  96 +typedef unsigned char uint8;
  97 +/**
  98 + * The volatile 8-bit unsigned data type.
  99 + */
  100 +typedef volatile unsigned char vuint8;
  101 +
  102 +/**
  103 + * The 16-bit signed data type.
  104 + */
  105 +typedef int int16;
  106 +/**
  107 + * The volatile 16-bit signed data type.
  108 + */
  109 +typedef volatile int vint16;
  110 +/**
  111 + * The 16-bit unsigned data type.
  112 + */
  113 +typedef unsigned int uint16;
  114 +/**
  115 + * The volatile 16-bit unsigned data type.
  116 + */
  117 +typedef volatile unsigned int vuint16;
  118 +/**
  119 + * The 32-bit signed data type.
  120 + */
  121 +typedef long int32;
  122 +/**
  123 + * The volatile 32-bit signed data type.
  124 + */
  125 +typedef volatile long vint32;
  126 +/**
  127 + * The 32-bit unsigned data type.
  128 + */
  129 +typedef unsigned long uint32;
  130 +/**
  131 + * The volatile 32-bit unsigned data type.
  132 + */
  133 +typedef volatile unsigned long vuint32;
  134 +
  135 +/* bsd */
  136 +typedef uint8 u_char; /**< 8-bit value */
  137 +typedef uint8 SOCKET;
  138 +typedef uint16 u_short; /**< 16-bit value */
  139 +typedef uint16 u_int; /**< 16-bit value */
  140 +typedef uint32 u_long; /**< 32-bit value */
  141 +
  142 +typedef union _un_l2cval {
  143 + u_long lVal;
  144 + u_char cVal[4];
  145 +}un_l2cval;
  146 +
  147 +typedef union _un_i2cval {
  148 + u_int iVal;
  149 + u_char cVal[2];
  150 +}un_i2cval;
  151 +
  152 +
  153 +/** global define */
  154 +#define FW_VERSION 0x01010000 /* System F/W Version : 1.1.0.0 */
  155 +#define HW_VERSION 0x01000000
  156 +
  157 +
  158 +#define TX_RX_MAX_BUF_SIZE 2048
  159 +#define TX_BUF 0x1100
  160 +#define RX_BUF (TX_BUF+TX_RX_MAX_BUF_SIZE)
  161 +
  162 +#define UART_DEVICE_CNT 1 /**< UART device number */
  163 +/* #define SUPPORT_UART_ONE */
  164 +
  165 +void ethernet_init(uint8_t *mac,uint8_t *ip,uint8_t *gateway,uint8_t *mask);
  166 +
  167 +#endif /* _ETHERNET_H_ */
Tangible/iic.c 0 → 100644
@@ -0,0 +1,83 @@ @@ -0,0 +1,83 @@
  1 +/************************
  2 +* i2c minimal library *
  3 +************************/
  4 +
  5 +#include <stdint.h>
  6 +#include <avr/io.h>
  7 +
  8 +#include "iic.h"
  9 +
  10 +// Initialize i2c
  11 +void TWI_init(void)
  12 +{
  13 + //set SCL to 400kHz
  14 + TWSR = 0x00;
  15 + TWBR = 0x0C;
  16 + //enable TWI
  17 + TWCR = (1<<TWEN);
  18 +}
  19 +
  20 +// Send start signal
  21 +static void TWI_start(void)
  22 +{
  23 + TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN);
  24 + while ((TWCR & (1<<TWINT)) == 0);
  25 +}
  26 +
  27 +// Send stop signal
  28 +static void TWI_stop(void)
  29 +{
  30 + TWCR = (1<<TWINT)|(1<<TWSTO)|(1<<TWEN);
  31 +}
  32 +
  33 +// Get i2c bus status
  34 +static uint8_t TWIGetStatus(void)
  35 +{
  36 + uint8_t status;
  37 + //mask status
  38 + status = TWSR & 0xF8;
  39 + return status;
  40 +}
  41 +
  42 +// Write single byte to bus
  43 +static void TWI_writebyte(uint8_t u8data)
  44 +{
  45 + TWDR = u8data;
  46 + TWCR = (1<<TWINT)|(1<<TWEN);
  47 + while ((TWCR & (1<<TWINT)) == 0);
  48 +}
  49 +
  50 +// Write i2c minimal message (command)
  51 +uint8_t TWI_writecmd(uint8_t address,uint8_t com_add)
  52 +{
  53 + TWI_start();
  54 + if (TWIGetStatus() != 0x08)
  55 + return ERROR;
  56 + TWI_writebyte(address<<1);
  57 + if (TWIGetStatus() != 0x18)
  58 + return ERROR;
  59 + TWI_writebyte(com_add);
  60 + if (TWIGetStatus() != 0x28)
  61 + return ERROR;
  62 + TWI_stop();
  63 + return SUCCESS;
  64 +}
  65 +
  66 +// Write i2c message (with a single data byte)
  67 +uint8_t TWI_writedata(uint8_t address,uint8_t com_add,uint8_t data)
  68 +{
  69 + TWI_start();
  70 + if (TWIGetStatus() != 0x08)
  71 + return ERROR;
  72 + TWI_writebyte(address<<1);
  73 + if (TWIGetStatus() != 0x18)
  74 + return ERROR;
  75 + TWI_writebyte(com_add);
  76 + if (TWIGetStatus() != 0x28)
  77 + return ERROR;
  78 + TWI_writebyte(data);
  79 + if (TWIGetStatus() != 0x28)
  80 + return ERROR;
  81 + TWI_stop();
  82 + return SUCCESS;
  83 +}
Tangible/iic.h 0 → 100644
@@ -0,0 +1,12 @@ @@ -0,0 +1,12 @@
  1 +/******************************************
  2 +* i2c minimal library public definitions *
  3 +******************************************/
  4 +
  5 +#pragma once
  6 +
  7 +#define ERROR 0
  8 +#define SUCCESS 1
  9 +
  10 +void TWI_init(void);
  11 +uint8_t TWI_writecmd(uint8_t address,uint8_t com_add);
  12 +uint8_t TWI_writedata(uint8_t address,uint8_t com_add,uint8_t data);
Tangible/serial.c 0 → 100644
@@ -0,0 +1,44 @@ @@ -0,0 +1,44 @@
  1 +/** Functions for serial port **/
  2 +
  3 +#include <avr/io.h>
  4 +#include <stdio.h>
  5 +
  6 +#include "serial.h"
  7 +
  8 +static int send_serial_printf(char c,FILE *stream);
  9 +static FILE mystdout=FDEV_SETUP_STREAM(send_serial_printf,NULL,_FDEV_SETUP_WRITE);
  10 +
  11 +void init_serial(int speed){
  12 +/* Set baud rate */
  13 +UBRR0 = F_CPU/(((unsigned long int)speed)<<4)-1;
  14 +
  15 +/* Enable transmitter & receiver */
  16 +UCSR0B = (1<<TXEN0 | 1<<RXEN0);
  17 +
  18 +/* Set 8 bits character and 1 stop bit */
  19 +UCSR0C = (1<<UCSZ01 | 1<<UCSZ00);
  20 +
  21 +/* Set off UART baud doubler */
  22 +UCSR0A &= ~(1 << U2X0);
  23 +}
  24 +
  25 +void send_serial(char c){
  26 +loop_until_bit_is_set(UCSR0A, UDRE0);
  27 +UDR0 = c;
  28 +}
  29 +
  30 +char get_serial(void){
  31 +loop_until_bit_is_set(UCSR0A, RXC0);
  32 +return UDR0;
  33 +}
  34 +
  35 +void init_printf(void){
  36 +init_serial(9600);
  37 +stdout=&mystdout;
  38 +}
  39 +
  40 +static int send_serial_printf(char c,FILE *stream){
  41 +if(c=='\n') send_serial('\r');
  42 +send_serial(c);
  43 +return 0;
  44 +}
Tangible/serial.h 0 → 100644
@@ -0,0 +1,6 @@ @@ -0,0 +1,6 @@
  1 +/** Definitions for serial port **/
  2 +
  3 +void init_serial(int speed);
  4 +void send_serial(char c);
  5 +char get_serial(void);
  6 +void init_printf(void);
Tangible/socket.c 0 → 100755
@@ -0,0 +1,561 @@ @@ -0,0 +1,561 @@
  1 +/*
  2 +*
  3 +@file socket.c
  4 +@brief setting chip register for socket
  5 + last update : 2008. Jan
  6 +*
  7 +*/
  8 +
  9 +#include "ethernet.h"
  10 +#include "w5100.h"
  11 +#include "socket.h"
  12 +
  13 +#ifdef __DEF_IINCHIP_DBG__
  14 +#include <stdio.h>
  15 +#endif
  16 +
  17 +static uint16 local_port;
  18 +
  19 +/**
  20 +@brief This Socket function initialize the channel in perticular mode, and set the port and wait for W5100 done it.
  21 +@return 1 for success else 0.
  22 +*/
  23 +uint8 socket(
  24 + SOCKET s, /**< for socket number */
  25 + uint8 protocol, /**< for socket protocol */
  26 + uint16 port, /**< the source port for the socket */
  27 + uint8 flag /**< the option for the socket */
  28 + )
  29 +{
  30 + uint8 ret;
  31 +#ifdef __DEF_IINCHIP_DBG__
  32 + printf("socket()\r\n");
  33 +#endif
  34 + if ((protocol == Sn_MR_TCP) || (protocol == Sn_MR_UDP) || (protocol == Sn_MR_IPRAW) || (protocol == Sn_MR_MACRAW) || (protocol == Sn_MR_PPPOE))
  35 + {
  36 + close(s);
  37 + IINCHIP_WRITE(Sn_MR(s),protocol | flag);
  38 + if (port != 0) {
  39 + IINCHIP_WRITE(Sn_PORT0(s),(uint8)((port & 0xff00) >> 8));
  40 + IINCHIP_WRITE((Sn_PORT0(s) + 1),(uint8)(port & 0x00ff));
  41 + } else {
  42 + local_port++; // if don't set the source port, set local_port number.
  43 + IINCHIP_WRITE(Sn_PORT0(s),(uint8)((local_port & 0xff00) >> 8));
  44 + IINCHIP_WRITE((Sn_PORT0(s) + 1),(uint8)(local_port & 0x00ff));
  45 + }
  46 + IINCHIP_WRITE(Sn_CR(s),Sn_CR_OPEN); // run sockinit Sn_CR
  47 +
  48 + /* +20071122[chungs]:wait to process the command... */
  49 + while( IINCHIP_READ(Sn_CR(s)) )
  50 + ;
  51 + /* ------- */
  52 + ret = 1;
  53 + }
  54 + else
  55 + {
  56 + ret = 0;
  57 + }
  58 +#ifdef __DEF_IINCHIP_DBG__
  59 + printf("Sn_SR = %.2x , Protocol = %.2x\r\n", IINCHIP_READ(Sn_SR(s)), IINCHIP_READ(Sn_MR(s)));
  60 +#endif
  61 + return ret;
  62 +}
  63 +
  64 +
  65 +/**
  66 +@brief This function close the socket and parameter is "s" which represent the socket number
  67 +*/
  68 +void close(SOCKET s)
  69 +{
  70 +#ifdef __DEF_IINCHIP_DBG__
  71 + printf("close()\r\n");
  72 +#endif
  73 +
  74 + IINCHIP_WRITE(Sn_CR(s),Sn_CR_CLOSE);
  75 +
  76 + /* +20071122[chungs]:wait to process the command... */
  77 + while( IINCHIP_READ(Sn_CR(s)) )
  78 + ;
  79 + /* ------- */
  80 +
  81 + /* +2008.01 [hwkim]: clear interrupt */
  82 + #ifdef __DEF_IINCHIP_INT__
  83 + /* m2008.01 [bj] : all clear */
  84 + putISR(s, 0x00);
  85 + #else
  86 + /* m2008.01 [bj] : all clear */
  87 + IINCHIP_WRITE(Sn_IR(s), 0xFF);
  88 + #endif
  89 +}
  90 +
  91 +
  92 +/**
  93 +@brief This function established the connection for the channel in passive (server) mode. This function waits for the request from the peer.
  94 +@return 1 for success else 0.
  95 +*/
  96 +uint8 listen(
  97 + SOCKET s /**< the socket number */
  98 + )
  99 +{
  100 + uint8 ret;
  101 +#ifdef __DEF_IINCHIP_DBG__
  102 + printf("listen()\r\n");
  103 +#endif
  104 + if (IINCHIP_READ(Sn_SR(s)) == SOCK_INIT)
  105 + {
  106 + IINCHIP_WRITE(Sn_CR(s),Sn_CR_LISTEN);
  107 + /* +20071122[chungs]:wait to process the command... */
  108 + while( IINCHIP_READ(Sn_CR(s)) )
  109 + ;
  110 + /* ------- */
  111 + ret = 1;
  112 + }
  113 + else
  114 + {
  115 + ret = 0;
  116 +#ifdef __DEF_IINCHIP_DBG__
  117 + printf("Fail[invalid ip,port]\r\n");
  118 +#endif
  119 + }
  120 + return ret;
  121 +}
  122 +
  123 +
  124 +/**
  125 +@brief This function established the connection for the channel in Active (client) mode.
  126 + This function waits for the until the connection is established.
  127 +
  128 +@return 1 for success else 0.
  129 +*/
  130 +uint8 connect(SOCKET s, uint8 * addr, uint16 port)
  131 +{
  132 + uint8 ret;
  133 +#ifdef __DEF_IINCHIP_DBG__
  134 + printf("connect()\r\n");
  135 +#endif
  136 + if
  137 + (
  138 + ((addr[0] == 0xFF) && (addr[1] == 0xFF) && (addr[2] == 0xFF) && (addr[3] == 0xFF)) ||
  139 + ((addr[0] == 0x00) && (addr[1] == 0x00) && (addr[2] == 0x00) && (addr[3] == 0x00)) ||
  140 + (port == 0x00)
  141 + )
  142 + {
  143 + ret = 0;
  144 +#ifdef __DEF_IINCHIP_DBG__
  145 + printf("Fail[invalid ip,port]\r\n");
  146 +#endif
  147 + }
  148 + else
  149 + {
  150 + ret = 1;
  151 + // set destination IP
  152 + IINCHIP_WRITE(Sn_DIPR0(s),addr[0]);
  153 + IINCHIP_WRITE((Sn_DIPR0(s) + 1),addr[1]);
  154 + IINCHIP_WRITE((Sn_DIPR0(s) + 2),addr[2]);
  155 + IINCHIP_WRITE((Sn_DIPR0(s) + 3),addr[3]);
  156 + IINCHIP_WRITE(Sn_DPORT0(s),(uint8)((port & 0xff00) >> 8));
  157 + IINCHIP_WRITE((Sn_DPORT0(s) + 1),(uint8)(port & 0x00ff));
  158 + IINCHIP_WRITE(Sn_CR(s),Sn_CR_CONNECT);
  159 + /* m2008.01 [bj] : wait for completion */
  160 + while ( IINCHIP_READ(Sn_CR(s)) ) ;
  161 +
  162 + }
  163 +
  164 + return ret;
  165 +}
  166 +
  167 +
  168 +
  169 +/**
  170 +@brief This function used for disconnect the socket and parameter is "s" which represent the socket number
  171 +@return 1 for success else 0.
  172 +*/
  173 +void disconnect(SOCKET s)
  174 +{
  175 +#ifdef __DEF_IINCHIP_DBG__
  176 + printf("disconnect()\r\n");
  177 +#endif
  178 + IINCHIP_WRITE(Sn_CR(s),Sn_CR_DISCON);
  179 +
  180 + /* +20071122[chungs]:wait to process the command... */
  181 + while( IINCHIP_READ(Sn_CR(s)) )
  182 + ;
  183 + /* ------- */
  184 +}
  185 +
  186 +
  187 +/**
  188 +@brief This function used to send the data in TCP mode
  189 +@return 1 for success else 0.
  190 +*/
  191 +uint16 send(
  192 + SOCKET s, /**< the socket index */
  193 + const uint8 * buf, /**< a pointer to data */
  194 + uint16 len /**< the data size to be send */
  195 + )
  196 +{
  197 + uint8 status=0;
  198 + uint16 ret=0;
  199 + uint16 freesize=0;
  200 +#ifdef __DEF_IINCHIP_DBG__
  201 + printf("send()\r\n");
  202 +#endif
  203 +
  204 + if (len > getIINCHIP_TxMAX(s)) ret = getIINCHIP_TxMAX(s); // check size not to exceed MAX size.
  205 + else ret = len;
  206 +
  207 + // if freebuf is available, start.
  208 + do
  209 + {
  210 + freesize = getSn_TX_FSR(s);
  211 + status = IINCHIP_READ(Sn_SR(s));
  212 + if ((status != SOCK_ESTABLISHED) && (status != SOCK_CLOSE_WAIT))
  213 + {
  214 + ret = 0;
  215 + break;
  216 + }
  217 +#ifdef __DEF_IINCHIP_DBG__
  218 + printf("socket %d freesize(%d) empty or error\r\n", s, freesize);
  219 +#endif
  220 + } while (freesize < ret);
  221 +
  222 + // copy data
  223 + send_data_processing(s, (uint8 *)buf, ret);
  224 + IINCHIP_WRITE(Sn_CR(s),Sn_CR_SEND);
  225 +
  226 + /* +20071122[chungs]:wait to process the command... */
  227 + while( IINCHIP_READ(Sn_CR(s)) )
  228 + ;
  229 + /* ------- */
  230 +
  231 +/* +2008.01 bj */
  232 +#ifdef __DEF_IINCHIP_INT__
  233 + while ( (getISR(s) & Sn_IR_SEND_OK) != Sn_IR_SEND_OK )
  234 +#else
  235 + while ( (IINCHIP_READ(Sn_IR(s)) & Sn_IR_SEND_OK) != Sn_IR_SEND_OK )
  236 +#endif
  237 + {
  238 + /* m2008.01 [bj] : reduce code */
  239 + if ( IINCHIP_READ(Sn_SR(s)) == SOCK_CLOSED )
  240 + {
  241 +#ifdef __DEF_IINCHIP_DBG__
  242 + printf("SOCK_CLOSED.\r\n");
  243 +#endif
  244 + close(s);
  245 + return 0;
  246 + }
  247 + }
  248 +/* +2008.01 bj */
  249 +#ifdef __DEF_IINCHIP_INT__
  250 + putISR(s, getISR(s) & (~Sn_IR_SEND_OK));
  251 +#else
  252 + IINCHIP_WRITE(Sn_IR(s), Sn_IR_SEND_OK);
  253 +#endif
  254 + return ret;
  255 +}
  256 +
  257 +
  258 +/**
  259 +@brief This function is an application I/F function which is used to receive the data in TCP mode.
  260 + It continues to wait for data as much as the application wants to receive.
  261 +
  262 +@return received data size for success else -1.
  263 +*/
  264 +uint16 recv(
  265 + SOCKET s, /**< socket index */
  266 + uint8 * buf, /**< a pointer to copy the data to be received */
  267 + uint16 len /**< the data size to be read */
  268 + )
  269 +{
  270 + uint16 ret=0;
  271 +#ifdef __DEF_IINCHIP_DBG__
  272 + printf("recv()\r\n");
  273 +#endif
  274 +
  275 +
  276 + if ( len > 0 )
  277 + {
  278 + recv_data_processing(s, buf, len);
  279 + IINCHIP_WRITE(Sn_CR(s),Sn_CR_RECV);
  280 +
  281 + /* +20071122[chungs]:wait to process the command... */
  282 + while( IINCHIP_READ(Sn_CR(s)) )
  283 + ;
  284 + /* ------- */
  285 + ret = len;
  286 + }
  287 + return ret;
  288 +}
  289 +
  290 +
  291 +/**
  292 +@brief This function is an application I/F function which is used to send the data for other then TCP mode.
  293 + Unlike TCP transmission, The peer's destination address and the port is needed.
  294 +
  295 +@return This function return send data size for success else -1.
  296 +*/
  297 +uint16 sendto(
  298 + SOCKET s, /**< socket index */
  299 + const uint8 * buf, /**< a pointer to the data */
  300 + uint16 len, /**< the data size to send */
  301 + uint8 * addr, /**< the peer's Destination IP address */
  302 + uint16 port /**< the peer's destination port number */
  303 + )
  304 +{
  305 +// uint8 status=0;
  306 +// uint8 isr=0;
  307 + uint16 ret=0;
  308 +
  309 +#ifdef __DEF_IINCHIP_DBG__
  310 + printf("sendto()\r\n");
  311 +#endif
  312 + if (len > getIINCHIP_TxMAX(s)) ret = getIINCHIP_TxMAX(s); // check size not to exceed MAX size.
  313 + else ret = len;
  314 +
  315 + if
  316 + (
  317 + ((addr[0] == 0x00) && (addr[1] == 0x00) && (addr[2] == 0x00) && (addr[3] == 0x00)) ||
  318 + ((port == 0x00)) ||(ret == 0)
  319 + )
  320 + {
  321 + /* +2008.01 [bj] : added return value */
  322 + ret = 0;
  323 +#ifdef __DEF_IINCHIP_DBG__
  324 + printf("%d Fail[%.2x.%.2x.%.2x.%.2x, %.d, %d]\r\n",s, addr[0], addr[1], addr[2], addr[3] , port, len);
  325 + printf("Fail[invalid ip,port]\r\n");
  326 +#endif
  327 + }
  328 + else
  329 + {
  330 + IINCHIP_WRITE(Sn_DIPR0(s),addr[0]);
  331 + IINCHIP_WRITE((Sn_DIPR0(s) + 1),addr[1]);
  332 + IINCHIP_WRITE((Sn_DIPR0(s) + 2),addr[2]);
  333 + IINCHIP_WRITE((Sn_DIPR0(s) + 3),addr[3]);
  334 + IINCHIP_WRITE(Sn_DPORT0(s),(uint8)((port & 0xff00) >> 8));
  335 + IINCHIP_WRITE((Sn_DPORT0(s) + 1),(uint8)(port & 0x00ff));
  336 +
  337 + // copy data
  338 + send_data_processing(s, (uint8 *)buf, ret);
  339 + IINCHIP_WRITE(Sn_CR(s),Sn_CR_SEND);
  340 +
  341 + /* +20071122[chungs]:wait to process the command... */
  342 + while( IINCHIP_READ(Sn_CR(s)) )
  343 + ;
  344 + /* ------- */
  345 +
  346 +/* +2008.01 bj */
  347 +#ifdef __DEF_IINCHIP_INT__
  348 + while ( (getISR(s) & Sn_IR_SEND_OK) != Sn_IR_SEND_OK )
  349 +#else
  350 + while ( (IINCHIP_READ(Sn_IR(s)) & Sn_IR_SEND_OK) != Sn_IR_SEND_OK )
  351 +#endif
  352 + {
  353 +#ifdef __DEF_IINCHIP_INT__
  354 + if (getISR(s) & Sn_IR_TIMEOUT)
  355 +#else
  356 + if (IINCHIP_READ(Sn_IR(s)) & Sn_IR_TIMEOUT)
  357 +#endif
  358 + {
  359 +#ifdef __DEF_IINCHIP_DBG__
  360 + printf("send fail.\r\n");
  361 +#endif
  362 +/* +2008.01 [bj]: clear interrupt */
  363 +#ifdef __DEF_IINCHIP_INT__
  364 + putISR(s, getISR(s) & ~(Sn_IR_SEND_OK | Sn_IR_TIMEOUT)); /* clear SEND_OK & TIMEOUT */
  365 +#else
  366 + IINCHIP_WRITE(Sn_IR(s), (Sn_IR_SEND_OK | Sn_IR_TIMEOUT)); /* clear SEND_OK & TIMEOUT */
  367 +#endif
  368 + return 0;
  369 + }
  370 + }
  371 +
  372 +/* +2008.01 bj */
  373 +#ifdef __DEF_IINCHIP_INT__
  374 + putISR(s, getISR(s) & (~Sn_IR_SEND_OK));
  375 +#else
  376 + IINCHIP_WRITE(Sn_IR(s), Sn_IR_SEND_OK);
  377 +#endif
  378 +
  379 + }
  380 + return ret;
  381 +}
  382 +
  383 +
  384 +/**
  385 +@brief This function is an application I/F function which is used to receive the data in other then
  386 + TCP mode. This function is used to receive UDP, IP_RAW and MAC_RAW mode, and handle the header as well.
  387 +
  388 +@return This function return received data size for success else -1.
  389 +*/
  390 +uint16 recvfrom(
  391 + SOCKET s, /**< the socket number */
  392 + uint8 * buf, /**< a pointer to copy the data to be received */
  393 + uint16 len, /**< the data size to read */
  394 + uint8 * addr, /**< a pointer to store the peer's IP address */
  395 + uint16 *port /**< a pointer to store the peer's port number. */
  396 + )
  397 +{
  398 + uint8 head[8];
  399 + uint16 data_len=0;
  400 + uint16 ptr=0;
  401 +#ifdef __DEF_IINCHIP_DBG__
  402 + printf("recvfrom()\r\n");
  403 +#endif
  404 +
  405 + if ( len > 0 )
  406 + {
  407 + ptr = IINCHIP_READ(Sn_RX_RD0(s));
  408 + ptr = ((ptr & 0x00ff) << 8) + IINCHIP_READ(Sn_RX_RD0(s) + 1);
  409 +#ifdef __DEF_IINCHIP_DBG__
  410 + printf("ISR_RX: rd_ptr : %.4x\r\n", ptr);
  411 +#endif
  412 + switch (IINCHIP_READ(Sn_MR(s)) & 0x07)
  413 + {
  414 + case Sn_MR_UDP :
  415 + read_data(s, (uint8 *)ptr, head, 0x08);
  416 + ptr += 8;
  417 + // read peer's IP address, port number.
  418 + addr[0] = head[0];
  419 + addr[1] = head[1];
  420 + addr[2] = head[2];
  421 + addr[3] = head[3];
  422 + *port = head[4];
  423 + *port = (*port << 8) + head[5];
  424 + data_len = head[6];
  425 + data_len = (data_len << 8) + head[7];
  426 +
  427 +#ifdef __DEF_IINCHIP_DBG__
  428 + printf("UDP msg arrived\r\n");
  429 + printf("source Port : %d\r\n", *port);
  430 + printf("source IP : %d.%d.%d.%d\r\n", addr[0], addr[1], addr[2], addr[3]);
  431 +#endif
  432 +
  433 + read_data(s, (uint8 *)ptr, buf, data_len); // data copy.
  434 + ptr += data_len;
  435 +
  436 + IINCHIP_WRITE(Sn_RX_RD0(s),(uint8)((ptr & 0xff00) >> 8));
  437 + IINCHIP_WRITE((Sn_RX_RD0(s) + 1),(uint8)(ptr & 0x00ff));
  438 + break;
  439 +
  440 + case Sn_MR_IPRAW :
  441 + read_data(s, (uint8 *)ptr, head, 0x06);
  442 + ptr += 6;
  443 +
  444 + addr[0] = head[0];
  445 + addr[1] = head[1];
  446 + addr[2] = head[2];
  447 + addr[3] = head[3];
  448 + data_len = head[4];
  449 + data_len = (data_len << 8) + head[5];
  450 +
  451 +#ifdef __DEF_IINCHIP_DBG__
  452 + printf("IP RAW msg arrived\r\n");
  453 + printf("source IP : %d.%d.%d.%d\r\n", addr[0], addr[1], addr[2], addr[3]);
  454 +#endif
  455 + read_data(s, (uint8 *)ptr, buf, data_len); // data copy.
  456 + ptr += data_len;
  457 +
  458 + IINCHIP_WRITE(Sn_RX_RD0(s),(uint8)((ptr & 0xff00) >> 8));
  459 + IINCHIP_WRITE((Sn_RX_RD0(s) + 1),(uint8)(ptr & 0x00ff));
  460 + break;
  461 + case Sn_MR_MACRAW :
  462 + read_data(s,(uint8*)ptr,head,2);
  463 + ptr+=2;
  464 + data_len = head[0];
  465 + data_len = (data_len<<8) + head[1] - 2;
  466 +
  467 + read_data(s,(uint8*) ptr,buf,data_len);
  468 + ptr += data_len;
  469 + IINCHIP_WRITE(Sn_RX_RD0(s),(uint8)((ptr & 0xff00) >> 8));
  470 + IINCHIP_WRITE((Sn_RX_RD0(s) + 1),(uint8)(ptr & 0x00ff));
  471 +
  472 +#ifdef __DEF_IINCHIP_DGB__
  473 + printf("MAC RAW msg arrived\r\n");
  474 + printf("dest mac=%.2X.%.2X.%.2X.%.2X.%.2X.%.2X\r\n",buf[0],buf[1],buf[2],buf[3],buf[4],buf[5]);
  475 + printf("src mac=%.2X.%.2X.%.2X.%.2X.%.2X.%.2X\r\n",buf[6],buf[7],buf[8],buf[9],buf[10],buf[11]);
  476 + printf("type =%.2X%.2X\r\n",buf[12],buf[13]);
  477 +#endif
  478 + break;
  479 +
  480 + default :
  481 + break;
  482 + }
  483 + IINCHIP_WRITE(Sn_CR(s),Sn_CR_RECV);
  484 +
  485 + /* +20071122[chungs]:wait to process the command... */
  486 + while( IINCHIP_READ(Sn_CR(s)) )
  487 + ;
  488 + /* ------- */
  489 + }
  490 +#ifdef __DEF_IINCHIP_DBG__
  491 + printf("recvfrom() end ..\r\n");
  492 +#endif
  493 + return data_len;
  494 +}
  495 +
  496 +
  497 +uint16 igmpsend(SOCKET s, const uint8 * buf, uint16 len)
  498 +{
  499 +// uint8 status=0;
  500 +// uint8 isr=0;
  501 + uint16 ret=0;
  502 +
  503 +#ifdef __DEF_IINCHIP_DBG__
  504 + printf("igmpsend()\r\n");
  505 +#endif
  506 + if (len > getIINCHIP_TxMAX(s)) ret = getIINCHIP_TxMAX(s); // check size not to exceed MAX size.
  507 + else ret = len;
  508 +
  509 + if (ret == 0)
  510 + {
  511 + ;
  512 +#ifdef __DEF_IINCHIP_DBG__
  513 + printf("%d Fail[%d]\r\n",len,ret);
  514 +#endif
  515 + }
  516 + else
  517 + {
  518 + // copy data
  519 + send_data_processing(s, (uint8 *)buf, ret);
  520 + IINCHIP_WRITE(Sn_CR(s),Sn_CR_SEND);
  521 +/* +2008.01 bj */
  522 + while( IINCHIP_READ(Sn_CR(s)) )
  523 + ;
  524 +/* ------- */
  525 +
  526 +/* +2008.01 bj */
  527 +#ifdef __DEF_IINCHIP_INT__
  528 + while ( (getISR(s) & Sn_IR_SEND_OK) != Sn_IR_SEND_OK )
  529 +#else
  530 + while ( (IINCHIP_READ(Sn_IR(s)) & Sn_IR_SEND_OK) != Sn_IR_SEND_OK )
  531 +#endif
  532 + {
  533 + IINCHIP_READ(Sn_SR(s));
  534 +#ifdef __DEF_IINCHIP_INT__
  535 + if (getISR(s) & Sn_IR_TIMEOUT)
  536 +#else
  537 + if (IINCHIP_READ(Sn_IR(s)) & Sn_IR_TIMEOUT)
  538 +#endif
  539 + {
  540 +#ifdef __DEF_IINCHIP_DBG__
  541 + printf("igmpsend fail.\r\n");
  542 +#endif
  543 + /* in case of igmp, if send fails, then socket closed */
  544 + /* if you want change, remove this code. */
  545 + close(s);
  546 + /* ----- */
  547 +
  548 + return 0;
  549 + }
  550 + }
  551 +
  552 +/* +2008.01 bj */
  553 +#ifdef __DEF_IINCHIP_INT__
  554 + putISR(s, getISR(s) & (~Sn_IR_SEND_OK));
  555 +#else
  556 + IINCHIP_WRITE(Sn_IR(s), Sn_IR_SEND_OK);
  557 +#endif
  558 + }
  559 + return ret;
  560 +}
  561 +
Tangible/socket.h 0 → 100755
@@ -0,0 +1,23 @@ @@ -0,0 +1,23 @@
  1 +/*
  2 +*
  3 +@file socket.h
  4 +@brief define function of socket API
  5 +*
  6 +*/
  7 +
  8 +#ifndef _SOCKET_H_
  9 +#define _SOCKET_H_
  10 +
  11 +extern uint8 socket(SOCKET s, uint8 protocol, uint16 port, uint8 flag); // Opens a socket(TCP or UDP or IP_RAW mode)
  12 +extern void close(SOCKET s); // Close socket
  13 +extern uint8 connect(SOCKET s, uint8 * addr, uint16 port); // Establish TCP connection (Active connection)
  14 +extern void disconnect(SOCKET s); // disconnect the connection
  15 +extern uint8 listen(SOCKET s); // Establish TCP connection (Passive connection)
  16 +extern uint16 send(SOCKET s, const uint8 * buf, uint16 len); // Send data (TCP)
  17 +extern uint16 recv(SOCKET s, uint8 * buf, uint16 len); // Receive data (TCP)
  18 +extern uint16 sendto(SOCKET s, const uint8 * buf, uint16 len, uint8 * addr, uint16 port); // Send data (UDP/IP RAW)
  19 +extern uint16 recvfrom(SOCKET s, uint8 * buf, uint16 len, uint8 * addr, uint16 *port); // Receive data (UDP/IP RAW)
  20 +
  21 +extern uint16 igmpsend(SOCKET s, const uint8 * buf, uint16 len);
  22 +#endif
  23 +/* _SOCKET_H_ */
Tangible/spi.h 0 → 100755
@@ -0,0 +1,58 @@ @@ -0,0 +1,58 @@
  1 +//-----------------------------------------------------------------------------
  2 +//AVR Mega168 SPI HAL
  3 +#define BIT0 0x01
  4 +#define BIT1 0x02
  5 +#define BIT2 0x04
  6 +#define BIT3 0x08
  7 +#define BIT4 0x10
  8 +#define BIT5 0x20
  9 +#define BIT6 0x40
  10 +#define BIT7 0x80
  11 +
  12 +#define SPI0_SS_BIT BIT2
  13 +#define SPI0_SS_DDR DDRB
  14 +#define SPI0_SS_PORT PORTB
  15 +
  16 +#define SPI0_SCLK_BIT BIT5
  17 +#define SPI0_SCLK_DDR DDRB
  18 +#define SPI0_SCLK_PORT PORTB
  19 +
  20 +#define SPI0_MOSI_BIT BIT3
  21 +#define SPI0_MOSI_DDR DDRB
  22 +#define SPI0_MOSI_PORT PORTB
  23 +
  24 +#define SPI0_MISO_BIT BIT4
  25 +#define SPI0_MISO_DDR DDRB
  26 +#define SPI0_MISO_PORT PORTB
  27 +
  28 +
  29 +#define SPI0_WaitForReceive()
  30 +#define SPI0_RxData() (SPDR)
  31 +
  32 +#define SPI0_TxData(Data) (SPDR = Data)
  33 +#define SPI0_WaitForSend() while( (SPSR & 0x80)==0x00 )
  34 +
  35 +#define SPI0_SendByte(Data) SPI0_TxData(Data);SPI0_WaitForSend()
  36 +#define SPI0_RecvBute() SPI0_RxData()
  37 +
  38 +// PB4(MISO), PB3(MOSI), PB5(SCK), PB2(/SS) // CS=1, waiting for SPI start // SPI mode 0, 4MHz
  39 +#define SPI0_Init() DDRB |= SPI0_SS_BIT|SPI0_SCLK_BIT|SPI0_MOSI_BIT;\
  40 + PORTB |= SPI0_SS_BIT; PORTB &= ~(SPI0_SCLK_BIT|SPI0_MOSI_BIT);\
  41 + SPCR = 0x50
  42 +//-----------------------------------------------------------------------------
  43 +
  44 +//-----------------------------------------------------------------------------
  45 +//IInChip SPI HAL
  46 +#define IINCHIP_SpiInit SPI0_Init
  47 +#define IINCHIP_SpiSendData SPI0_SendByte
  48 +#define IINCHIP_SpiRecvData SPI0_RxData
  49 +
  50 +
  51 +#define IINCHIP_CS_BIT BIT2
  52 +#define IINCHIP_CS_DDR DDRB
  53 +#define IINCHIP_CS_PORT PORTB
  54 +
  55 +#define IINCHIP_CSInit() (IINCHIP_CS_DDR |= IINCHIP_CS_BIT)
  56 +#define IINCHIP_CSon() (IINCHIP_CS_PORT |= IINCHIP_CS_BIT)
  57 +#define IINCHIP_CSoff() (IINCHIP_CS_PORT &= ~IINCHIP_CS_BIT)
  58 +//-----------------------------------------------------------------------------
Tangible/tangibleInterface.c 0 → 100644
@@ -0,0 +1,18 @@ @@ -0,0 +1,18 @@
  1 +#include <avr/io.h>
  2 +#include <util/delay.h>
  3 +#include <stdio.h>
  4 +
  5 +#include "serial.h"
  6 +#include "ethernet.h"
  7 +#include "w5100.h"
  8 +#include "socket.h"
  9 +
  10 +#define MAC_SIZE 6
  11 +#define IPV4_SIZE 4
  12 +
  13 +int main(void)
  14 +{
  15 +init_printf();
  16 +// Your code here !!
  17 +return 0;
  18 +}
Tangible/w5100.c 0 → 100755
@@ -0,0 +1,1300 @@ @@ -0,0 +1,1300 @@
  1 +/*
  2 + * (c)COPYRIGHT
  3 + * ALL RIGHT RESERVED
  4 + *
  5 + * FileName : w5100.c
  6 + * Revision History :
  7 + * ---------- ------- ------------------------------------------------
  8 + * Date version Description
  9 + * ---------- ------- ------------------------------------------------
  10 + * 01/25/2007 1.1 Bug is Fixed in the Indirect Mode
  11 + * : Memory mapping error
  12 + * ---------- ------- ------------------------------------------------
  13 + * 01/08/2008 1.2 Modification of Socket Command Part
  14 + * : Check if the appropriately performed after writing Sn_CR
  15 + *
  16 + * Modification of SPI Part
  17 + * : SPI code changed by adding 'spi.h'.
  18 + * : Change control type for SPI port from byte to bit.
  19 + * ---------- ------- ------------------------------------------------
  20 + * 01/15/2008 1.3 Bug is Fixed in the pppinit() fuction.
  21 + * : do not clear interrupt value, so fixed.
  22 + *
  23 + * Modification of ISR
  24 + * : Do not exit ISR, if there is interrupt.
  25 + * ---------- ------- ------------------------------------------------
  26 + * 03/21/2008 1.4 Modification of SetMR() function
  27 + * : Use IINCHIP_WRITE() function in Direct or SPI mode.
  28 + * ---------- ------- ------------------------------------------------
  29 + */
  30 +#include <stdio.h>
  31 +#include <string.h>
  32 +
  33 +#include <avr/interrupt.h>
  34 +// #include <avr/io.h>
  35 +
  36 +#include "ethernet.h"
  37 +#include "socket.h"
  38 +#include "w5100.h"
  39 +
  40 +#ifdef __DEF_IINCHIP_PPP__
  41 + #include "md5.h"
  42 +#endif
  43 +
  44 +
  45 +#if (__DEF_IINCHIP_BUS__ == __DEF_IINCHIP_SPI_MODE__)
  46 +#include "spi.h" //+2007113[jhpark]
  47 +#endif
  48 +
  49 +static uint8 I_STATUS[MAX_SOCK_NUM];
  50 +static uint16 SMASK[MAX_SOCK_NUM]; /**< Variable for Tx buffer MASK in each channel */
  51 +static uint16 RMASK[MAX_SOCK_NUM]; /**< Variable for Rx buffer MASK in each channel */
  52 +static uint16 SSIZE[MAX_SOCK_NUM]; /**< Max Tx buffer size by each channel */
  53 +static uint16 RSIZE[MAX_SOCK_NUM]; /**< Max Rx buffer size by each channel */
  54 +static uint16 SBUFBASEADDRESS[MAX_SOCK_NUM]; /**< Tx buffer base address by each channel */
  55 +static uint16 RBUFBASEADDRESS[MAX_SOCK_NUM]; /**< Rx buffer base address by each channel */
  56 +
  57 +uint8 getISR(uint8 s)
  58 +{
  59 + return I_STATUS[s];
  60 +}
  61 +
  62 +void putISR(uint8 s, uint8 val)
  63 +{
  64 + I_STATUS[s] = val;
  65 +}
  66 +
  67 +uint16 getIINCHIP_RxMAX(uint8 s)
  68 +{
  69 + return RSIZE[s];
  70 +}
  71 +uint16 getIINCHIP_TxMAX(uint8 s)
  72 +{
  73 + return SSIZE[s];
  74 +}
  75 +uint16 getIINCHIP_RxMASK(uint8 s)
  76 +{
  77 + return RMASK[s];
  78 +}
  79 +uint16 getIINCHIP_TxMASK(uint8 s)
  80 +{
  81 + return SMASK[s];
  82 +}
  83 +uint16 getIINCHIP_RxBASE(uint8 s)
  84 +{
  85 + return RBUFBASEADDRESS[s];
  86 +}
  87 +uint16 getIINCHIP_TxBASE(uint8 s)
  88 +{
  89 + return SBUFBASEADDRESS[s];
  90 +}
  91 +
  92 + /**
  93 +@brief This function writes the data into W5100 registers.
  94 +*/
  95 +uint8 IINCHIP_WRITE(uint16 addr,uint8 data)
  96 +{
  97 +// DIRECT MODE I/F
  98 +#if (__DEF_IINCHIP_BUS__ == __DEF_IINCHIP_DIRECT_MODE__)
  99 + IINCHIP_ISR_DISABLE();
  100 + *((vuint8*)(addr)) = data;
  101 + IINCHIP_ISR_ENABLE();
  102 +#elif(__DEF_IINCHIP_BUS__ == __DEF_IINCHIP_INDIRECT_MODE__) /* INDIRECT MODE I/F */
  103 + IINCHIP_ISR_DISABLE();
  104 + *((vuint8*)IDM_AR0) = (uint8)((addr & 0xFF00) >> 8);
  105 + *((vuint8*)IDM_AR1) = (uint8)(addr & 0x00FF);
  106 + *((vuint8*)IDM_DR) = data;
  107 + IINCHIP_ISR_ENABLE();
  108 +#elif (__DEF_IINCHIP_BUS__ == __DEF_IINCHIP_SPI_MODE__)
  109 + IINCHIP_ISR_DISABLE();
  110 + IINCHIP_SpiInit();
  111 +
  112 + //SPI MODE I/F
  113 + IINCHIP_CSoff(); // CS=0, SPI start
  114 +
  115 + IINCHIP_SpiSendData(0xF0);
  116 + IINCHIP_SpiSendData((addr & 0xFF00) >> 8);
  117 + IINCHIP_SpiSendData(addr & 0x00FF);
  118 + IINCHIP_SpiSendData(data);
  119 +
  120 + IINCHIP_CSon();
  121 +
  122 + IINCHIP_ISR_ENABLE();
  123 +#else
  124 + #error "unknown bus type"
  125 +#endif
  126 + return 1;
  127 +}
  128 +
  129 +
  130 +/**
  131 +@brief This function reads the value from W5100 registers.
  132 +*/
  133 +uint8 IINCHIP_READ(uint16 addr)
  134 +{
  135 + uint8 data;
  136 +
  137 +// DIRECT MODE I/F
  138 +
  139 +#if (__DEF_IINCHIP_BUS__ == __DEF_IINCHIP_DIRECT_MODE__)
  140 + IINCHIP_ISR_DISABLE();
  141 + data = *((vuint8*)(addr));
  142 + IINCHIP_ISR_ENABLE();
  143 +#elif(__DEF_IINCHIP_BUS__ == __DEF_IINCHIP_INDIRECT_MODE__)
  144 + IINCHIP_ISR_DISABLE();
  145 + *((vuint8*)IDM_AR0) = (uint8)((addr & 0xFF00) >> 8);
  146 + *((vuint8*)IDM_AR1) = (uint8)(addr & 0x00FF);
  147 + data = *((vuint8*)IDM_DR);
  148 + IINCHIP_ISR_ENABLE();
  149 +
  150 +#elif (__DEF_IINCHIP_BUS__ == __DEF_IINCHIP_SPI_MODE__)
  151 + IINCHIP_ISR_DISABLE();
  152 + IINCHIP_SpiInit();
  153 + IINCHIP_CSoff(); // CS=0, SPI start
  154 +
  155 + IINCHIP_SpiSendData(0x0F);
  156 + IINCHIP_SpiSendData((addr & 0xFF00) >> 8);
  157 + IINCHIP_SpiSendData(addr & 0x00FF);
  158 +
  159 +
  160 + IINCHIP_SpiSendData(0);
  161 + data = IINCHIP_SpiRecvData();
  162 +
  163 + IINCHIP_CSon(); // SPI end
  164 + IINCHIP_ISR_ENABLE();
  165 +#else
  166 + #error "unknown bus type"
  167 +#endif
  168 + return data;
  169 +}
  170 +
  171 +
  172 +/**
  173 +@brief This function writes into W5100 memory(Buffer)
  174 +*/
  175 +uint16 wiz_write_buf(uint16 addr,uint8* buf,uint16 len)
  176 +{
  177 +#if (__DEF_IINCHIP_BUS__ == __DEF_IINCHIP_DIRECT_MODE__)
  178 + IINCHIP_ISR_DISABLE();
  179 + memcpy((uint8 *)addr, buf, len);
  180 + IINCHIP_ISR_ENABLE();
  181 +#elif (__DEF_IINCHIP_BUS__ == __DEF_IINCHIP_INDIRECT_MODE__)
  182 + uint16 idx = 0;
  183 + IINCHIP_ISR_DISABLE();
  184 + *((vuint8*)IDM_AR0) = (uint8)((addr & 0xFF00) >> 8);
  185 + *((vuint8*)IDM_AR1) = (uint8)(addr & 0x00FF);
  186 + for (idx = 0; idx < len ; idx++) *((vuint8*)IDM_DR) = buf[idx];
  187 + IINCHIP_ISR_ENABLE();
  188 +#elif (__DEF_IINCHIP_BUS__ == __DEF_IINCHIP_SPI_MODE__)
  189 + uint16 idx = 0;
  190 +
  191 + IINCHIP_ISR_DISABLE();
  192 + IINCHIP_SpiInit();
  193 +
  194 + //SPI MODE I/F
  195 + for(idx=0;idx<len;idx++)
  196 + {
  197 + IINCHIP_CSoff(); // CS=0, SPI start
  198 +
  199 + IINCHIP_SpiSendData(0xF0);
  200 + IINCHIP_SpiSendData(((addr+idx) & 0xFF00) >> 8);
  201 + IINCHIP_SpiSendData((addr+idx) & 0x00FF);
  202 + IINCHIP_SpiSendData(buf[idx]);
  203 +
  204 + IINCHIP_CSon(); // CS=0, SPI end
  205 + }
  206 +
  207 + IINCHIP_ISR_ENABLE();
  208 +#else
  209 + #error "unknown bus type"
  210 +#endif
  211 + return len;
  212 +}
  213 +
  214 +
  215 +/**
  216 +@brief This function reads into W5100 memory(Buffer)
  217 +*/
  218 +uint16 wiz_read_buf(uint16 addr, uint8* buf,uint16 len)
  219 +{
  220 +#if (__DEF_IINCHIP_BUS__ == __DEF_IINCHIP_DIRECT_MODE__)
  221 + IINCHIP_ISR_DISABLE();
  222 + memcpy(buf, (uint8 *)addr, len);
  223 + IINCHIP_ISR_ENABLE();
  224 +#elif(__DEF_IINCHIP_BUS__ == __DEF_IINCHIP_INDIRECT_MODE__)
  225 + uint16 idx = 0;
  226 + IINCHIP_ISR_DISABLE();
  227 + *((vuint8*)IDM_AR0) = (uint8)((addr & 0xFF00) >> 8);
  228 + *((vuint8*)IDM_AR1) = (uint8)(addr & 0x00FF);
  229 + for (idx = 0; idx < len ; idx++) buf[idx] = *((vuint8*)IDM_DR);
  230 + IINCHIP_ISR_ENABLE();
  231 +#elif (__DEF_IINCHIP_BUS__ == __DEF_IINCHIP_SPI_MODE__)
  232 + uint16 idx = 0;
  233 + IINCHIP_ISR_DISABLE();
  234 +
  235 + IINCHIP_SpiInit();
  236 +
  237 + for (idx=0; idx<len; idx++)
  238 + {
  239 + IINCHIP_CSoff(); // CS=0, SPI start
  240 +
  241 + IINCHIP_SpiSendData(0x0F);
  242 + IINCHIP_SpiSendData(((addr+idx) & 0xFF00) >> 8);
  243 + IINCHIP_SpiSendData((addr+idx) & 0x00FF);
  244 +
  245 +
  246 + IINCHIP_SpiSendData(0);
  247 + buf[idx] = IINCHIP_SpiRecvData();
  248 +
  249 + IINCHIP_CSon(); // CS=0, SPI end
  250 + }
  251 +
  252 + IINCHIP_ISR_ENABLE();
  253 +#else
  254 + #error "unknown bus type"
  255 +#endif
  256 + return len;
  257 +}
  258 +
  259 +
  260 +/**
  261 +@brief Socket interrupt routine
  262 +*/
  263 +#ifdef __DEF_IINCHIP_INT__
  264 +ISR(INT4_vect)
  265 +{
  266 + uint8 int_val;
  267 + IINCHIP_ISR_DISABLE();
  268 + int_val = IINCHIP_READ(IR);
  269 +
  270 + /* +200801[bj] process all of interupt */
  271 + do {
  272 + /*---*/
  273 +
  274 + if (int_val & IR_CONFLICT)
  275 + {
  276 + printf("IP conflict : %.2x\r\n", int_val);
  277 + }
  278 + if (int_val & IR_UNREACH)
  279 + {
  280 + printf("INT Port Unreachable : %.2x\r\n", int_val);
  281 + printf("UIPR0 : %d.%d.%d.%d\r\n", IINCHIP_READ(UIPR0), IINCHIP_READ(UIPR0+1), IINCHIP_READ(UIPR0+2), IINCHIP_READ(UIPR0+3));
  282 + printf("UPORT0 : %.2x %.2x\r\n", IINCHIP_READ(UPORT0), IINCHIP_READ(UPORT0+1));
  283 + }
  284 +
  285 + /* +200801[bj] interrupt clear */
  286 + IINCHIP_WRITE(IR, 0xf0);
  287 + /*---*/
  288 +
  289 + if (int_val & IR_SOCK(0))
  290 + {
  291 + /* +-200801[bj] save interrupt value*/
  292 + I_STATUS[0] |= IINCHIP_READ(Sn_IR(0)); // can be come to over two times.
  293 + IINCHIP_WRITE(Sn_IR(0), I_STATUS[0]);
  294 + /*---*/
  295 + }
  296 + if (int_val & IR_SOCK(1))
  297 + {
  298 + /* +-200801[bj] save interrupt value*/
  299 + I_STATUS[1] |= IINCHIP_READ(Sn_IR(1));
  300 + IINCHIP_WRITE(Sn_IR(1), I_STATUS[1]);
  301 + /*---*/
  302 + }
  303 + if (int_val & IR_SOCK(2))
  304 + {
  305 + /* +-200801[bj] save interrupt value*/
  306 + I_STATUS[2] |= IINCHIP_READ(Sn_IR(2));
  307 + IINCHIP_WRITE(Sn_IR(2), I_STATUS[2]);
  308 + /*---*/
  309 + }
  310 + if (int_val & IR_SOCK(3))
  311 + {
  312 + /* +-200801[bj] save interrupt value*/
  313 + I_STATUS[3] |= IINCHIP_READ(Sn_IR(3));
  314 + IINCHIP_WRITE(Sn_IR(3), I_STATUS[3]);
  315 + /*---*/
  316 + }
  317 +
  318 + /* +-200801[bj] re-read interrupt value*/
  319 + int_val = IINCHIP_READ(IR);
  320 +
  321 + /* +200801[bj] if exist, contiue to process */
  322 + } while (int_val != 0x00);
  323 + /*---*/
  324 +
  325 + IINCHIP_ISR_ENABLE();
  326 +}
  327 +#endif
  328 +
  329 +/**
  330 +@brief This function is for resetting of the iinchip. Initializes the iinchip to work in whether DIRECT or INDIRECT mode
  331 +*/
  332 +void iinchip_init(void)
  333 +{
  334 + setMR( MR_RST );
  335 +#if (__DEF_IINCHIP_BUS__ == __DEF_IINCHIP_INDIRECT_MODE__)
  336 + setMR( MR_IND | MR_AI );
  337 +#ifdef __DEF_IINCHIP_DBG__
  338 + printf("MR value is %d \r\n",IINCHIP_READ(MR));
  339 +#endif
  340 +#endif
  341 +}
  342 +
  343 +
  344 +/**
  345 +@brief This function set the transmit & receive buffer size as per the channels is used
  346 +
  347 +Note for TMSR and RMSR bits are as follows\n
  348 +bit 1-0 : memory size of channel #0 \n
  349 +bit 3-2 : memory size of channel #1 \n
  350 +bit 5-4 : memory size of channel #2 \n
  351 +bit 7-6 : memory size of channel #3 \n\n
  352 +Maximum memory size for Tx, Rx in the W5100 is 8K Bytes,\n
  353 +In the range of 8KBytes, the memory size could be allocated dynamically by each channel.\n
  354 +Be attentive to sum of memory size shouldn't exceed 8Kbytes\n
  355 +and to data transmission and receiption from non-allocated channel may cause some problems.\n
  356 +If the 8KBytes memory is already assigned to centain channel, \n
  357 +other 3 channels couldn't be used, for there's no available memory.\n
  358 +If two 4KBytes memory are assigned to two each channels, \n
  359 +other 2 channels couldn't be used, for there's no available memory.\n
  360 +*/
  361 +void sysinit(
  362 + uint8 tx_size, /**< tx_size Tx memory size (00 - 1KByte, 01- 2KBtye, 10 - 4KByte, 11 - 8KByte) */
  363 + uint8 rx_size /**< rx_size Rx memory size (00 - 1KByte, 01- 2KBtye, 10 - 4KByte, 11 - 8KByte) */
  364 + )
  365 +{
  366 + int16 i;
  367 + int16 ssum,rsum;
  368 +
  369 +#ifdef __DEF_IINCHIP_DBG__
  370 + printf("sysinit()\r\n");
  371 +#endif
  372 +
  373 + ssum = 0;
  374 + rsum = 0;
  375 +
  376 + IINCHIP_WRITE(TMSR,tx_size); /* Set Tx memory size for each channel */
  377 + IINCHIP_WRITE(RMSR,rx_size); /* Set Rx memory size for each channel */
  378 +
  379 + SBUFBASEADDRESS[0] = (uint16)(__DEF_IINCHIP_MAP_TXBUF__); /* Set base address of Tx memory for channel #0 */
  380 + RBUFBASEADDRESS[0] = (uint16)(__DEF_IINCHIP_MAP_RXBUF__); /* Set base address of Rx memory for channel #0 */
  381 +
  382 +#ifdef __DEF_IINCHIP_DBG__
  383 + printf("Channel : SEND MEM SIZE : RECV MEM SIZE\r\n");
  384 +#endif
  385 +
  386 + for (i = 0 ; i < MAX_SOCK_NUM; i++) // Set the size, masking and base address of Tx & Rx memory by each channel
  387 + {
  388 + SSIZE[i] = (int16)(0);
  389 + RSIZE[i] = (int16)(0);
  390 + if (ssum < 8192)
  391 + {
  392 + switch((tx_size >> i*2) & 0x03) // Set Tx memory size
  393 + {
  394 + case 0:
  395 + SSIZE[i] = (int16)(1024);
  396 + SMASK[i] = (uint16)(0x03FF);
  397 + break;
  398 + case 1:
  399 + SSIZE[i] = (int16)(2048);
  400 + SMASK[i] = (uint16)(0x07FF);
  401 + break;
  402 + case 2:
  403 + SSIZE[i] = (int16)(4096);
  404 + SMASK[i] = (uint16)(0x0FFF);
  405 + break;
  406 + case 3:
  407 + SSIZE[i] = (int16)(8192);
  408 + SMASK[i] = (uint16)(0x1FFF);
  409 + break;
  410 + }
  411 + }
  412 + if (rsum < 8192)
  413 + {
  414 + switch((rx_size >> i*2) & 0x03) // Set Rx memory size
  415 + {
  416 + case 0:
  417 + RSIZE[i] = (int16)(1024);
  418 + RMASK[i] = (uint16)(0x03FF);
  419 + break;
  420 + case 1:
  421 + RSIZE[i] = (int16)(2048);
  422 + RMASK[i] = (uint16)(0x07FF);
  423 + break;
  424 + case 2:
  425 + RSIZE[i] = (int16)(4096);
  426 + RMASK[i] = (uint16)(0x0FFF);
  427 + break;
  428 + case 3:
  429 + RSIZE[i] = (int16)(8192);
  430 + RMASK[i] = (uint16)(0x1FFF);
  431 + break;
  432 + }
  433 + }
  434 + ssum += SSIZE[i];
  435 + rsum += RSIZE[i];
  436 +
  437 + if (i != 0) // Sets base address of Tx and Rx memory for channel #1,#2,#3
  438 + {
  439 + SBUFBASEADDRESS[i] = SBUFBASEADDRESS[i-1] + SSIZE[i-1];
  440 + RBUFBASEADDRESS[i] = RBUFBASEADDRESS[i-1] + RSIZE[i-1];
  441 + }
  442 +#ifdef __DEF_IINCHIP_DBG__
  443 + printf("%d : %.4x : %.4x : %.4x : %.4x\r\n", i, (uint16)SBUFBASEADDRESS[i], (uint16)RBUFBASEADDRESS[i], SSIZE[i], RSIZE[i]);
  444 +#endif
  445 + }
  446 +}
  447 +
  448 +
  449 +void setMR(uint8 val)
  450 +{
  451 +
  452 +#if (__DEF_IINCHIP_BUS__ == __DEF_IINCHIP_INDIRECT_MODE__)
  453 + *((volatile uint8*)(MR)) = val;
  454 +#else
  455 + /* DIRECT ACCESS */
  456 + IINCHIP_WRITE(MR,val);
  457 +#endif
  458 +}
  459 +
  460 +
  461 +/**
  462 +@brief This function sets up gateway IP address.
  463 +*/
  464 +void setGAR(
  465 + uint8 * addr /**< a pointer to a 4 -byte array responsible to set the Gateway IP address. */
  466 + )
  467 +{
  468 + IINCHIP_WRITE((GAR0 + 0),addr[0]);
  469 + IINCHIP_WRITE((GAR0 + 1),addr[1]);
  470 + IINCHIP_WRITE((GAR0 + 2),addr[2]);
  471 + IINCHIP_WRITE((GAR0 + 3),addr[3]);
  472 +}
  473 +void getGWIP(uint8 * addr)
  474 +{
  475 + addr[0] = IINCHIP_READ((GAR0 + 0));
  476 + addr[1] = IINCHIP_READ((GAR0 + 1));
  477 + addr[2] = IINCHIP_READ((GAR0 + 2));
  478 + addr[3] = IINCHIP_READ((GAR0 + 3));
  479 +}
  480 +
  481 +
  482 +/**
  483 +@brief It sets up SubnetMask address
  484 +*/
  485 +void setSUBR(
  486 + uint8 * addr /**< a pointer to a 4 -byte array responsible to set the SubnetMask address */
  487 + )
  488 +{
  489 + IINCHIP_WRITE((SUBR0 + 0),addr[0]);
  490 + IINCHIP_WRITE((SUBR0 + 1),addr[1]);
  491 + IINCHIP_WRITE((SUBR0 + 2),addr[2]);
  492 + IINCHIP_WRITE((SUBR0 + 3),addr[3]);
  493 +}
  494 +
  495 +
  496 +/**
  497 +@brief This function sets up MAC address.
  498 +*/
  499 +void setSHAR(
  500 + uint8 * addr /**< a pointer to a 6 -byte array responsible to set the MAC address. */
  501 + )
  502 +{
  503 + IINCHIP_WRITE((SHAR0 + 0),addr[0]);
  504 + IINCHIP_WRITE((SHAR0 + 1),addr[1]);
  505 + IINCHIP_WRITE((SHAR0 + 2),addr[2]);
  506 + IINCHIP_WRITE((SHAR0 + 3),addr[3]);
  507 + IINCHIP_WRITE((SHAR0 + 4),addr[4]);
  508 + IINCHIP_WRITE((SHAR0 + 5),addr[5]);
  509 +}
  510 +
  511 +
  512 +/**
  513 +@brief This function sets up Source IP address.
  514 +*/
  515 +void setSIPR(
  516 + uint8 * addr /**< a pointer to a 4 -byte array responsible to set the Source IP address. */
  517 + )
  518 +{
  519 + IINCHIP_WRITE((SIPR0 + 0),addr[0]);
  520 + IINCHIP_WRITE((SIPR0 + 1),addr[1]);
  521 + IINCHIP_WRITE((SIPR0 + 2),addr[2]);
  522 + IINCHIP_WRITE((SIPR0 + 3),addr[3]);
  523 +}
  524 +
  525 +
  526 +/**
  527 +@brief This function gets Interrupt register in common register.
  528 + */
  529 +uint8 getIR( void )
  530 +{
  531 + return IINCHIP_READ(IR);
  532 +}
  533 +
  534 +
  535 +
  536 +/**
  537 +@brief This function sets up Retransmission time.
  538 +
  539 +If there is no response from the peer or delay in response then retransmission
  540 +will be there as per RTR (Retry Time-value Register)setting
  541 +*/
  542 +void setRTR(uint16 timeout)
  543 +{
  544 + IINCHIP_WRITE(RTR0,(uint8)((timeout & 0xff00) >> 8));
  545 + IINCHIP_WRITE((RTR0 + 1),(uint8)(timeout & 0x00ff));
  546 +}
  547 +
  548 +
  549 +/**
  550 +@brief This function set the number of Retransmission.
  551 +
  552 +If there is no response from the peer or delay in response then recorded time
  553 +as per RTR & RCR register seeting then time out will occur.
  554 +*/
  555 +void setRCR(uint8 retry)
  556 +{
  557 + IINCHIP_WRITE(RCR,retry);
  558 +}
  559 +
  560 +
  561 +/**
  562 +@brief This function set the interrupt mask Enable/Disable appropriate Interrupt. ('1' : interrupt enable)
  563 +
  564 +If any bit in IMR is set as '0' then there is not interrupt signal though the bit is
  565 +set in IR register.
  566 +*/
  567 +void setIMR(uint8 mask)
  568 +{
  569 + IINCHIP_WRITE(IMR,mask); // must be setted 0x10.
  570 +}
  571 +
  572 +
  573 +/**
  574 +@brief These below functions are used to get the Gateway, SubnetMask
  575 + and Source Hardware Address (MAC Address) and Source IP address
  576 +*/
  577 +void getGAR(uint8 * addr)
  578 +{
  579 + addr[0] = IINCHIP_READ(GAR0);
  580 + addr[1] = IINCHIP_READ(GAR0+1);
  581 + addr[2] = IINCHIP_READ(GAR0+2);
  582 + addr[3] = IINCHIP_READ(GAR0+3);
  583 +}
  584 +void getSUBR(uint8 * addr)
  585 +{
  586 + addr[0] = IINCHIP_READ(SUBR0);
  587 + addr[1] = IINCHIP_READ(SUBR0+1);
  588 + addr[2] = IINCHIP_READ(SUBR0+2);
  589 + addr[3] = IINCHIP_READ(SUBR0+3);
  590 +}
  591 +void getSHAR(uint8 * addr)
  592 +{
  593 + addr[0] = IINCHIP_READ(SHAR0);
  594 + addr[1] = IINCHIP_READ(SHAR0+1);
  595 + addr[2] = IINCHIP_READ(SHAR0+2);
  596 + addr[3] = IINCHIP_READ(SHAR0+3);
  597 + addr[4] = IINCHIP_READ(SHAR0+4);
  598 + addr[5] = IINCHIP_READ(SHAR0+5);
  599 +}
  600 +void getSIPR(uint8 * addr)
  601 +{
  602 + addr[0] = IINCHIP_READ(SIPR0);
  603 + addr[1] = IINCHIP_READ(SIPR0+1);
  604 + addr[2] = IINCHIP_READ(SIPR0+2);
  605 + addr[3] = IINCHIP_READ(SIPR0+3);
  606 +}
  607 +
  608 +
  609 +/**
  610 +@brief These below functions are used to get the Destination Hardware Address (MAC Address), Destination IP address and Destination Port.
  611 +*/
  612 +void getSn_DHAR(SOCKET s, uint8 * addr)
  613 +{
  614 + addr[0] = IINCHIP_READ(Sn_DHAR0(s));
  615 + addr[1] = IINCHIP_READ(Sn_DHAR0(s)+1);
  616 + addr[2] = IINCHIP_READ(Sn_DHAR0(s)+2);
  617 + addr[3] = IINCHIP_READ(Sn_DHAR0(s)+3);
  618 + addr[4] = IINCHIP_READ(Sn_DHAR0(s)+4);
  619 + addr[5] = IINCHIP_READ(Sn_DHAR0(s)+5);
  620 +}
  621 +void setSn_DHAR(SOCKET s, uint8 * addr)
  622 +{
  623 + IINCHIP_WRITE((Sn_DHAR0(s) + 0),addr[0]);
  624 + IINCHIP_WRITE((Sn_DHAR0(s) + 1),addr[1]);
  625 + IINCHIP_WRITE((Sn_DHAR0(s) + 2),addr[2]);
  626 + IINCHIP_WRITE((Sn_DHAR0(s) + 3),addr[3]);
  627 + IINCHIP_WRITE((Sn_DHAR0(s) + 4),addr[4]);
  628 + IINCHIP_WRITE((Sn_DHAR0(s) + 5),addr[5]);
  629 +}
  630 +void getSn_DIPR(SOCKET s, uint8 * addr)
  631 +{
  632 + addr[0] = IINCHIP_READ(Sn_DIPR0(s));
  633 + addr[1] = IINCHIP_READ(Sn_DIPR0(s)+1);
  634 + addr[2] = IINCHIP_READ(Sn_DIPR0(s)+2);
  635 + addr[3] = IINCHIP_READ(Sn_DIPR0(s)+3);
  636 +}
  637 +void setSn_DIPR(SOCKET s, uint8 * addr)
  638 +{
  639 + IINCHIP_WRITE((Sn_DIPR0(s) + 0),addr[0]);
  640 + IINCHIP_WRITE((Sn_DIPR0(s) + 1),addr[1]);
  641 + IINCHIP_WRITE((Sn_DIPR0(s) + 2),addr[2]);
  642 + IINCHIP_WRITE((Sn_DIPR0(s) + 3),addr[3]);
  643 +}
  644 +void getSn_DPORT(SOCKET s, uint8 * addr)
  645 +{
  646 + addr[0] = IINCHIP_READ(Sn_DPORT0(s));
  647 + addr[1] = IINCHIP_READ(Sn_DPORT0(s)+1);
  648 +}
  649 +void setSn_DPORT(SOCKET s, uint8 * addr)
  650 +{
  651 + IINCHIP_WRITE((Sn_DPORT0(s) + 0),addr[0]);
  652 + IINCHIP_WRITE((Sn_DPORT0(s) + 1),addr[1]);
  653 +}
  654 +
  655 +
  656 +/**
  657 +@brief This sets the maximum segment size of TCP in Active Mode), while in Passive Mode this is set by peer
  658 +*/
  659 +void setSn_MSS(SOCKET s, uint16 Sn_MSSR0)
  660 +{
  661 + IINCHIP_WRITE(Sn_MSSR0(s),(uint8)((Sn_MSSR0 & 0xff00) >> 8));
  662 + IINCHIP_WRITE((Sn_MSSR0(s) + 1),(uint8)(Sn_MSSR0 & 0x00ff));
  663 +}
  664 +
  665 +void setSn_TTL(SOCKET s, uint8 ttl)
  666 +{
  667 + IINCHIP_WRITE(Sn_TTL(s), ttl);
  668 +}
  669 +
  670 +
  671 +/**
  672 +@brief These below function is used to setup the Protocol Field of IP Header when
  673 + executing the IP Layer RAW mode.
  674 +*/
  675 +void setSn_PROTO(SOCKET s, uint8 proto)
  676 +{
  677 + IINCHIP_WRITE(Sn_PROTO(s),proto);
  678 +}
  679 +
  680 +
  681 +/**
  682 +@brief get socket interrupt status
  683 +
  684 +These below functions are used to read the Interrupt & Soket Status register
  685 +*/
  686 +uint8 getSn_IR(SOCKET s)
  687 +{
  688 + return IINCHIP_READ(Sn_IR(s));
  689 +}
  690 +
  691 +
  692 +/**
  693 +@brief get socket status
  694 +*/
  695 +uint8 getSn_SR(SOCKET s)
  696 +{
  697 + return IINCHIP_READ(Sn_SR(s));
  698 +}
  699 +
  700 +
  701 +/**
  702 +@brief get socket TX free buf size
  703 +
  704 +This gives free buffer size of transmit buffer. This is the data size that user can transmit.
  705 +User shuold check this value first and control the size of transmitting data
  706 +*/
  707 +uint16 getSn_TX_FSR(SOCKET s)
  708 +{
  709 + uint16 val=0,val1=0;
  710 + do
  711 + {
  712 + val1 = IINCHIP_READ(Sn_TX_FSR0(s));
  713 + val1 = (val1 << 8) + IINCHIP_READ(Sn_TX_FSR0(s) + 1);
  714 + if (val1 != 0)
  715 + {
  716 + val = IINCHIP_READ(Sn_TX_FSR0(s));
  717 + val = (val << 8) + IINCHIP_READ(Sn_TX_FSR0(s) + 1);
  718 + }
  719 + } while (val != val1);
  720 + return val;
  721 +}
  722 +
  723 +
  724 +/**
  725 +@brief get socket RX recv buf size
  726 +
  727 +This gives size of received data in receive buffer.
  728 +*/
  729 +uint16 getSn_RX_RSR(SOCKET s)
  730 +{
  731 + uint16 val=0,val1=0;
  732 + do
  733 + {
  734 + val1 = IINCHIP_READ(Sn_RX_RSR0(s));
  735 + val1 = (val1 << 8) + IINCHIP_READ(Sn_RX_RSR0(s) + 1);
  736 + if(val1 != 0)
  737 + {
  738 + val = IINCHIP_READ(Sn_RX_RSR0(s));
  739 + val = (val << 8) + IINCHIP_READ(Sn_RX_RSR0(s) + 1);
  740 + }
  741 + } while (val != val1);
  742 + return val;
  743 +}
  744 +
  745 +
  746 +/**
  747 +@brief This function is being called by send() and sendto() function also.
  748 +
  749 +This function read the Tx write pointer register and after copy the data in buffer update the Tx write pointer
  750 +register. User should read upper byte first and lower byte later to get proper value.
  751 +*/
  752 +void send_data_processing(SOCKET s, uint8 *data, uint16 len)
  753 +{
  754 + uint16 ptr;
  755 + ptr = IINCHIP_READ(Sn_TX_WR0(s));
  756 + ptr = ((ptr & 0x00ff) << 8) + IINCHIP_READ(Sn_TX_WR0(s) + 1);
  757 + write_data(s, data, (uint8 *)(ptr), len);
  758 + ptr += len;
  759 + IINCHIP_WRITE(Sn_TX_WR0(s),(uint8)((ptr & 0xff00) >> 8));
  760 + IINCHIP_WRITE((Sn_TX_WR0(s) + 1),(uint8)(ptr & 0x00ff));
  761 +}
  762 +
  763 +
  764 +/**
  765 +@brief This function is being called by recv() also.
  766 +
  767 +This function read the Rx read pointer register
  768 +and after copy the data from receive buffer update the Rx write pointer register.
  769 +User should read upper byte first and lower byte later to get proper value.
  770 +*/
  771 +void recv_data_processing(SOCKET s, uint8 *data, uint16 len)
  772 +{
  773 + uint16 ptr;
  774 + ptr = IINCHIP_READ(Sn_RX_RD0(s));
  775 + ptr = ((ptr & 0x00ff) << 8) + IINCHIP_READ(Sn_RX_RD0(s) + 1);
  776 +#ifdef __DEF_IINCHIP_DBG__
  777 + printf("ISR_RX: rd_ptr : %.4x\r\n", ptr);
  778 +#endif
  779 + read_data(s, (uint8 *)ptr, data, len); // read data
  780 + ptr += len;
  781 + IINCHIP_WRITE(Sn_RX_RD0(s),(uint8)((ptr & 0xff00) >> 8));
  782 + IINCHIP_WRITE((Sn_RX_RD0(s) + 1),(uint8)(ptr & 0x00ff));
  783 +}
  784 +
  785 +
  786 +/**
  787 +@brief for copy the data form application buffer to Transmite buffer of the chip.
  788 +
  789 +This function is being used for copy the data form application buffer to Transmite
  790 +buffer of the chip. It calculate the actual physical address where one has to write
  791 +the data in transmite buffer. Here also take care of the condition while it exceed
  792 +the Tx memory uper-bound of socket.
  793 +*/
  794 +void write_data(SOCKET s, vuint8 * src, vuint8 * dst, uint16 len)
  795 +{
  796 + uint16 size;
  797 + uint16 dst_mask;
  798 + uint8 * dst_ptr;
  799 +
  800 + dst_mask = (uint16)dst & getIINCHIP_TxMASK(s);
  801 + dst_ptr = (uint8 *)(getIINCHIP_TxBASE(s) + dst_mask);
  802 +
  803 + if (dst_mask + len > getIINCHIP_TxMAX(s))
  804 + {
  805 + size = getIINCHIP_TxMAX(s) - dst_mask;
  806 + wiz_write_buf((uint16)dst_ptr, (uint8*)src, size);
  807 + src += size;
  808 + size = len - size;
  809 + dst_ptr = (uint8 *)(getIINCHIP_TxBASE(s));
  810 + wiz_write_buf((uint16)dst_ptr, (uint8*)src, size);
  811 + }
  812 + else
  813 + {
  814 + wiz_write_buf((uint16)dst_ptr, (uint8*)src, len);
  815 + }
  816 +}
  817 +
  818 +
  819 +/**
  820 +@brief This function is being used for copy the data form Receive buffer of the chip to application buffer.
  821 +
  822 +It calculate the actual physical address where one has to read
  823 +the data from Receive buffer. Here also take care of the condition while it exceed
  824 +the Rx memory uper-bound of socket.
  825 +*/
  826 +void read_data(SOCKET s, vuint8 * src, vuint8 * dst, uint16 len)
  827 +{
  828 + uint16 size;
  829 + uint16 src_mask;
  830 + uint8 * src_ptr;
  831 +
  832 + src_mask = (uint16)src & getIINCHIP_RxMASK(s);
  833 + src_ptr = (uint8 *)(getIINCHIP_RxBASE(s) + src_mask);
  834 +
  835 + if( (src_mask + len) > getIINCHIP_RxMAX(s) )
  836 + {
  837 + size = getIINCHIP_RxMAX(s) - src_mask;
  838 + wiz_read_buf((uint16)src_ptr, (uint8*)dst,size);
  839 + dst += size;
  840 + size = len - size;
  841 + src_ptr = (uint8 *)(getIINCHIP_RxBASE(s));
  842 + wiz_read_buf((uint16)src_ptr, (uint8*) dst,size);
  843 + }
  844 + else
  845 + {
  846 + wiz_read_buf((uint16)src_ptr, (uint8*) dst,len);
  847 + }
  848 +}
  849 +
  850 +
  851 +#ifdef __DEF_IINCHIP_PPP__
  852 +#define PPP_OPTION_BUF_LEN 64
  853 +
  854 +uint8 pppinit_in(uint8 * id, uint8 idlen, uint8 * passwd, uint8 passwdlen);
  855 +
  856 +
  857 +/**
  858 +@brief make PPPoE connection
  859 +@return 1 => success to connect, 2 => Auth fail, 3 => timeout, 4 => Auth type not support
  860 +
  861 +*/
  862 +uint8 pppinit(uint8 * id, uint8 idlen, uint8 * passwd, uint8 passwdlen)
  863 +{
  864 + uint8 ret;
  865 + uint8 isr;
  866 +
  867 + // PHASE0. W5100 PPPoE(ADSL) setup
  868 + // enable pppoe mode
  869 + printf("-- PHASE 0. W5100 PPPoE(ADSL) setup process --\r\n");
  870 + printf("\r\n");
  871 + IINCHIP_WRITE(MR,IINCHIP_READ(MR) | MR_PPPOE);
  872 +
  873 + // open socket in pppoe mode
  874 + isr = IINCHIP_READ(Sn_IR(0));// first clear isr(0), W5100 at present time
  875 + IINCHIP_WRITE(Sn_IR(0),isr);
  876 +
  877 + IINCHIP_WRITE(PTIMER,200); // 5sec timeout
  878 + IINCHIP_WRITE(PMAGIC,0x01); // magic number
  879 + IINCHIP_WRITE(Sn_MR(0),Sn_MR_PPPOE);
  880 + IINCHIP_WRITE(Sn_CR(0),Sn_CR_OPEN);
  881 +
  882 + /* +20071122[chungs]:wait to process the command... */
  883 + while( IINCHIP_READ(Sn_CR(0)) )
  884 + ;
  885 + /* ------- */
  886 +
  887 + ret = pppinit_in(id, idlen, passwd, passwdlen);
  888 +
  889 + // close ppp connection socket
  890 + /* +200801 (hwkim) */
  891 + close(0);
  892 + /* ------- */
  893 +
  894 + return ret;
  895 +}
  896 +
  897 +
  898 +uint8 pppinit_in(uint8 * id, uint8 idlen, uint8 * passwd, uint8 passwdlen)
  899 +{
  900 + uint8 loop_idx = 0;
  901 + uint8 isr = 0;
  902 + uint8 buf[PPP_OPTION_BUF_LEN];
  903 + uint16 len;
  904 + uint8 str[PPP_OPTION_BUF_LEN];
  905 + uint8 str_idx,dst_idx;
  906 +
  907 + // PHASE1. PPPoE Discovery
  908 + // start to connect pppoe connection
  909 + printf("-- PHASE 1. PPPoE Discovery process --");
  910 + printf(" ok\r\n");
  911 + printf("\r\n");
  912 + IINCHIP_WRITE(Sn_CR(0),Sn_CR_PCON);
  913 + /* +20071122[chungs]:wait to process the command... */
  914 + while( IINCHIP_READ(Sn_CR(0)) )
  915 + ;
  916 + /* ------- */
  917 +
  918 + wait_10ms(100);
  919 +
  920 + loop_idx = 0;
  921 + //check whether PPPoE discovery end or not
  922 + while (!(IINCHIP_READ(Sn_IR(0)) & Sn_IR_PNEXT))
  923 + {
  924 + printf(".");
  925 + if (loop_idx++ == 10) // timeout
  926 + {
  927 + printf("timeout before LCP\r\n");
  928 + return 3;
  929 + }
  930 + wait_10ms(100);
  931 + }
  932 +
  933 + /* +200801[bj] clear interrupt value*/
  934 + IINCHIP_WRITE(Sn_IR(0), 0xff);
  935 + /*---*/
  936 +
  937 + // PHASE2. LCP process
  938 + printf("-- PHASE 2. LCP process --");
  939 +
  940 + // send LCP Request
  941 + {
  942 + // Magic number option
  943 + // option format (type value + length value + data)
  944 + // write magic number value
  945 + buf[0] = 0x05; // type value
  946 + buf[1] = 0x06; // length value
  947 + buf[2] = 0x01; buf[3] = 0x01; buf[4] = 0x01; buf[5]= 0x01; // data
  948 + // for MRU option, 1492 0x05d4
  949 + // buf[6] = 0x01; buf[7] = 0x04; buf[8] = 0x05; buf[9] = 0xD4;
  950 + }
  951 + send_data_processing(0, buf, 0x06);
  952 + IINCHIP_WRITE(Sn_CR(0),Sn_CR_PCR); // send request
  953 + /* +20071122[chungs]:wait to process the command... */
  954 + while( IINCHIP_READ(Sn_CR(0)) )
  955 + ;
  956 + /* ------- */
  957 +
  958 + wait_10ms(100);
  959 +
  960 + while (!((isr = IINCHIP_READ(Sn_IR(0))) & Sn_IR_PNEXT))
  961 + {
  962 + if (isr & Sn_IR_PRECV) // Not support option
  963 + {
  964 + /* +200801[bj] clear interrupt value*/
  965 + IINCHIP_WRITE(Sn_IR(0), Sn_IR_PRECV);
  966 + /*---*/
  967 + len = getSn_RX_RSR(0);
  968 + if ( len > 0 )
  969 + {
  970 + recv_data_processing(0, str, len);
  971 + IINCHIP_WRITE(Sn_CR(0),Sn_CR_RECV);
  972 + /* +20071122[chungs]:wait to process the command... */
  973 + while( IINCHIP_READ(Sn_CR(0)) )
  974 + ;
  975 + /* ------- */
  976 +
  977 + // for debug
  978 + //printf("LCP proc\r\n"); for (i = 0; i < len; i++) printf ("%02x ", str[i]); printf("\r\n");
  979 + // get option length
  980 + len = str[4]; len = ((len & 0x00ff) << 8) + str[5];
  981 + len += 2;
  982 + str_idx = 6; dst_idx = 0; // ppp header is 6 byte, so starts at 6.
  983 + do
  984 + {
  985 + if ((str[str_idx] == 0x01) || (str[str_idx] == 0x02) || (str[str_idx] == 0x03) || (str[str_idx] == 0x05))
  986 + {
  987 + // skip as length of support option. str_idx+1 is option's length.
  988 + str_idx += str[str_idx+1];
  989 + }
  990 + else
  991 + {
  992 + // not support option , REJECT
  993 + memcpy((uint8 *)(buf+dst_idx), (uint8 *)(str+str_idx), str[str_idx+1]);
  994 + dst_idx += str[str_idx+1]; str_idx += str[str_idx+1];
  995 + }
  996 + } while (str_idx != len);
  997 + // for debug
  998 + // printf("LCP dst proc\r\n"); for (i = 0; i < dst_idx; i++) printf ("%02x ", dst[i]); printf("\r\n");
  999 +
  1000 + // send LCP REJECT packet
  1001 + send_data_processing(0, buf, dst_idx);
  1002 + IINCHIP_WRITE(Sn_CR(0),Sn_CR_PCJ);
  1003 + /* +20071122[chungs]:wait to process the command... */
  1004 + while( IINCHIP_READ(Sn_CR(0)) )
  1005 + ;
  1006 + /* ------- */
  1007 + }
  1008 + }
  1009 + printf(".");
  1010 + if (loop_idx++ == 10) // timeout
  1011 + {
  1012 + printf("timeout after LCP\r\n");
  1013 + return 3;
  1014 + }
  1015 + wait_10ms(100);
  1016 + }
  1017 + printf(" ok\r\n");
  1018 + printf("\r\n");
  1019 +
  1020 + /* +200801[bj] clear interrupt value*/
  1021 + IINCHIP_WRITE(Sn_IR(0), 0xff);
  1022 + /*---*/
  1023 +
  1024 + printf("-- PHASE 3. PPPoE(ADSL) Authentication mode --\r\n");
  1025 + printf("Authentication protocol : %.2x %.2x, ", IINCHIP_READ(PATR0), IINCHIP_READ(PATR0+1));
  1026 +
  1027 + loop_idx = 0;
  1028 + if (IINCHIP_READ(PATR0) == 0xc0 && IINCHIP_READ(PATR0+1) == 0x23)
  1029 + {
  1030 + printf("PAP\r\n"); // in case of adsl normally supports PAP.
  1031 + // send authentication data
  1032 + // copy (idlen + id + passwdlen + passwd)
  1033 + buf[loop_idx] = idlen; loop_idx++;
  1034 + memcpy((uint8 *)(buf+loop_idx), (uint8 *)(id), idlen); loop_idx += idlen;
  1035 + buf[loop_idx] = passwdlen; loop_idx++;
  1036 + memcpy((uint8 *)(buf+loop_idx), (uint8 *)(passwd), passwdlen); loop_idx += passwdlen;
  1037 + send_data_processing(0, buf, loop_idx);
  1038 + IINCHIP_WRITE(Sn_CR(0),Sn_CR_PCR);
  1039 + /* +20071122[chungs]:wait to process the command... */
  1040 + while( IINCHIP_READ(Sn_CR(0)) )
  1041 + ;
  1042 + /* ------- */
  1043 + wait_10ms(100);
  1044 + }
  1045 + else if (IINCHIP_READ(PATR0) == 0xc2 && IINCHIP_READ(PATR0+1) == 0x23)
  1046 + {
  1047 + uint8 chal_len;
  1048 + md5_ctx context;
  1049 + uint8 digest[16];
  1050 +
  1051 + len = getSn_RX_RSR(0);
  1052 + if ( len > 0 )
  1053 + {
  1054 + recv_data_processing(0, str, len);
  1055 + IINCHIP_WRITE(Sn_CR(0),Sn_CR_RECV);
  1056 + /* +20071122[chungs]:wait to process the command... */
  1057 + while( IINCHIP_READ(Sn_CR(0)) )
  1058 + ;
  1059 + /* ------- */
  1060 +#ifdef __DEF_IINCHIP_DBG__
  1061 + printf("recv CHAP\r\n");
  1062 + {
  1063 + int16 i;
  1064 +
  1065 + for (i = 0; i < 32; i++)
  1066 + printf ("%02x ", str[i]);
  1067 + }
  1068 + printf("\r\n");
  1069 +#endif
  1070 +// str is C2 23 xx CHAL_ID xx xx CHAP_LEN CHAP_DATA
  1071 +// index 0 1 2 3 4 5 6 7 ...
  1072 +
  1073 + memset(buf,0x00,64);
  1074 + buf[loop_idx] = str[3]; loop_idx++; // chal_id
  1075 + memcpy((uint8 *)(buf+loop_idx), (uint8 *)(passwd), passwdlen); loop_idx += passwdlen; //passwd
  1076 + chal_len = str[6]; // chal_id
  1077 + memcpy((uint8 *)(buf+loop_idx), (uint8 *)(str+7), chal_len); loop_idx += chal_len; //challenge
  1078 + buf[loop_idx] = 0x80;
  1079 +#ifdef __DEF_IINCHIP_DBG__
  1080 + printf("CHAP proc d1\r\n");
  1081 + {
  1082 + int16 i;
  1083 + for (i = 0; i < 64; i++)
  1084 + printf ("%02x ", buf[i]);
  1085 + }
  1086 + printf("\r\n");
  1087 +#endif
  1088 +
  1089 + md5_init(&context);
  1090 + md5_update(&context, buf, loop_idx);
  1091 + md5_final(digest, &context);
  1092 +
  1093 +#ifdef __DEF_IINCHIP_DBG__
  1094 + printf("CHAP proc d1\r\n");
  1095 + {
  1096 + int16 i;
  1097 + for (i = 0; i < 16; i++)
  1098 + printf ("%02x", digest[i]);
  1099 + }
  1100 + printf("\r\n");
  1101 +#endif
  1102 + loop_idx = 0;
  1103 + buf[loop_idx] = 16; loop_idx++; // hash_len
  1104 + memcpy((uint8 *)(buf+loop_idx), (uint8 *)(digest), 16); loop_idx += 16; // hashed value
  1105 + memcpy((uint8 *)(buf+loop_idx), (uint8 *)(id), idlen); loop_idx += idlen; // id
  1106 + send_data_processing(0, buf, loop_idx);
  1107 + IINCHIP_WRITE(Sn_CR(0),Sn_CR_PCR);
  1108 + /* +20071122[chungs]:wait to process the command... */
  1109 + while( IINCHIP_READ(Sn_CR(0)) )
  1110 + ;
  1111 + /* ------- */
  1112 + wait_10ms(100);
  1113 + }
  1114 + }
  1115 + else
  1116 + {
  1117 + printf("Not support\r\n");
  1118 +#ifdef __DEF_IINCHIP_DBG__
  1119 + printf("Not support PPP Auth type: %.2x%.2x\r\n",IINCHIP_READ(PATR0), IINCHIP_READ(PATR0+1));
  1120 +#endif
  1121 + return 4;
  1122 + }
  1123 + printf("\r\n");
  1124 +
  1125 + printf("-- Waiting for PPPoE server's admission --");
  1126 + loop_idx = 0;
  1127 + while (!((isr = IINCHIP_READ(Sn_IR(0))) & Sn_IR_PNEXT))
  1128 + {
  1129 + if (isr & Sn_IR_PFAIL)
  1130 + {
  1131 + /* +200801[bj] clear interrupt value*/
  1132 + IINCHIP_WRITE(Sn_IR(0), 0xff);
  1133 + /*---*/
  1134 + printf("failed\r\nReinput id, password..\r\n");
  1135 + return 2;
  1136 + }
  1137 + printf(".");
  1138 + if (loop_idx++ == 10) // timeout
  1139 + {
  1140 + /* +200801[bj] clear interrupt value*/
  1141 + IINCHIP_WRITE(Sn_IR(0), 0xff);
  1142 + /*---*/
  1143 + printf("timeout after PAP\r\n");
  1144 + return 3;
  1145 + }
  1146 + wait_10ms(100);
  1147 + }
  1148 + /* +200801[bj] clear interrupt value*/
  1149 + IINCHIP_WRITE(Sn_IR(0), 0xff);
  1150 + /*---*/
  1151 + printf("ok\r\n");
  1152 + printf("\r\n");
  1153 + printf("-- PHASE 4. IPCP process --");
  1154 + // IP Address
  1155 + buf[0] = 0x03; buf[1] = 0x06; buf[2] = 0x00; buf[3] = 0x00; buf[4] = 0x00; buf[5] = 0x00;
  1156 + send_data_processing(0, buf, 6);
  1157 + IINCHIP_WRITE(Sn_CR(0),Sn_CR_PCR);
  1158 + /* +20071122[chungs]:wait to process the command... */
  1159 + while( IINCHIP_READ(Sn_CR(0)) )
  1160 + ;
  1161 + /* ------- */
  1162 + wait_10ms(100);
  1163 +
  1164 + loop_idx = 0;
  1165 + while (1)
  1166 + {
  1167 + if (IINCHIP_READ(Sn_IR(0)) & Sn_IR_PRECV)
  1168 + {
  1169 + /* +200801[bj] clear interrupt value*/
  1170 + IINCHIP_WRITE(Sn_IR(0), 0xff);
  1171 + /*---*/
  1172 + len = getSn_RX_RSR(0);
  1173 + if ( len > 0 )
  1174 + {
  1175 + recv_data_processing(0, str, len);
  1176 + IINCHIP_WRITE(Sn_CR(0),Sn_CR_RECV);
  1177 + /* +20071122[chungs]:wait to process the command... */
  1178 + while( IINCHIP_READ(Sn_CR(0)) )
  1179 + ;
  1180 + /* ------- */
  1181 + //for debug
  1182 + //printf("IPCP proc\r\n"); for (i = 0; i < len; i++) printf ("%02x ", str[i]); printf("\r\n");
  1183 + str_idx = 6; dst_idx = 0;
  1184 + if (str[2] == 0x03) // in case of NAK
  1185 + {
  1186 + do
  1187 + {
  1188 + if (str[str_idx] == 0x03) // request only ip information
  1189 + {
  1190 + memcpy((uint8 *)(buf+dst_idx), (uint8 *)(str+str_idx), str[str_idx+1]);
  1191 + dst_idx += str[str_idx+1]; str_idx += str[str_idx+1];
  1192 + }
  1193 + else
  1194 + {
  1195 + // skip byte
  1196 + str_idx += str[str_idx+1];
  1197 + }
  1198 + // for debug
  1199 + //printf("s: %d, d: %d, l: %d", str_idx, dst_idx, len);
  1200 + } while (str_idx != len);
  1201 + send_data_processing(0, buf, dst_idx);
  1202 + IINCHIP_WRITE(Sn_CR(0),Sn_CR_PCR); // send ipcp request
  1203 + /* +20071122[chungs]:wait to process the command... */
  1204 + while( IINCHIP_READ(Sn_CR(0)) )
  1205 + ;
  1206 + /* ------- */
  1207 + wait_10ms(100);
  1208 + break;
  1209 + }
  1210 + }
  1211 + }
  1212 + printf(".");
  1213 + if (loop_idx++ == 10) // timeout
  1214 + {
  1215 + printf("timeout after IPCP\r\n");
  1216 + return 3;
  1217 + }
  1218 + wait_10ms(100);
  1219 + send_data_processing(0, buf, 6);
  1220 + IINCHIP_WRITE(Sn_CR(0),Sn_CR_PCR); //ipcp re-request
  1221 + /* +20071122[chungs]:wait to process the command... */
  1222 + while( IINCHIP_READ(Sn_CR(0)) )
  1223 + ;
  1224 + /* ------- */
  1225 + }
  1226 +
  1227 + loop_idx = 0;
  1228 + while (!(IINCHIP_READ(Sn_IR(0)) & Sn_IR_PNEXT))
  1229 + {
  1230 + printf(".");
  1231 + if (loop_idx++ == 10) // timeout
  1232 + {
  1233 + printf("timeout after IPCP NAK\r\n");
  1234 + return 3;
  1235 + }
  1236 + wait_10ms(100);
  1237 + IINCHIP_WRITE(Sn_CR(0),Sn_CR_PCR); // send ipcp request
  1238 + /* +20071122[chungs]:wait to process the command... */
  1239 + while( IINCHIP_READ(Sn_CR(0)) )
  1240 + ;
  1241 + /* ------- */
  1242 + }
  1243 + /* +200801[bj] clear interrupt value*/
  1244 + IINCHIP_WRITE(Sn_IR(0), 0xff);
  1245 + /*---*/
  1246 + printf("ok\r\n");
  1247 + printf("\r\n");
  1248 + return 1;
  1249 + // after this function, User must save the pppoe server's mac address and pppoe session id in current connection
  1250 +}
  1251 +
  1252 +
  1253 +/**
  1254 +@brief terminate PPPoE connection
  1255 +*/
  1256 +uint8 pppterm(uint8 * mac, uint8 * sessionid)
  1257 +{
  1258 + uint16 i;
  1259 + uint8 isr;
  1260 +#ifdef __DEF_IINCHIP_DBG__
  1261 + printf("pppterm()\r\n");
  1262 +#endif
  1263 + /* Set PPPoE bit in MR(Common Mode Register) : enable socket0 pppoe */
  1264 + IINCHIP_WRITE(MR,IINCHIP_READ(MR) | MR_PPPOE);
  1265 +
  1266 + // write pppoe server's mac address and session id
  1267 + // must be setted these value.
  1268 + for (i = 0; i < 6; i++) IINCHIP_WRITE((Sn_DHAR0(0)+i),mac[i]);
  1269 + for (i = 0; i < 2; i++) IINCHIP_WRITE((Sn_DPORT0(0)+i),sessionid[i]);
  1270 + isr = IINCHIP_READ(Sn_IR(0));
  1271 + IINCHIP_WRITE(Sn_IR(0),isr);
  1272 +
  1273 + //open socket in pppoe mode
  1274 + IINCHIP_WRITE(Sn_MR(0),Sn_MR_PPPOE);
  1275 + IINCHIP_WRITE(Sn_CR(0),Sn_CR_OPEN);
  1276 + /* +20071122[chungs]:wait to process the command... */
  1277 + while( IINCHIP_READ(Sn_CR(0)) )
  1278 + ;
  1279 + /* ------- */
  1280 + wait_1us(1);
  1281 + // close pppoe connection
  1282 + IINCHIP_WRITE(Sn_CR(0),Sn_CR_PDISCON);
  1283 + /* +20071122[chungs]:wait to process the command... */
  1284 + while( IINCHIP_READ(Sn_CR(0)) )
  1285 + ;
  1286 + /* ------- */
  1287 + wait_10ms(100);
  1288 + // close socket
  1289 + /* +200801 (hwkim) */
  1290 + close(0);
  1291 + /* ------- */
  1292 +
  1293 +
  1294 +#ifdef __DEF_IINCHIP_DBG__
  1295 + printf("pppterm() end ..\r\n");
  1296 +#endif
  1297 +
  1298 + return 1;
  1299 +}
  1300 +#endif
Tangible/w5100.h 0 → 100755
@@ -0,0 +1,299 @@ @@ -0,0 +1,299 @@
  1 +/*
  2 +@file w5100.h
  3 +*/
  4 +
  5 +#ifndef _W5100_H_
  6 +#define _W5100_H_
  7 +
  8 +
  9 +#define MR __DEF_IINCHIP_MAP_BASE__
  10 +#define IDM_OR ((__DEF_IINCHIP_MAP_BASE__ + 0x00))
  11 +#define IDM_AR0 ((__DEF_IINCHIP_MAP_BASE__ + 0x01))
  12 +#define IDM_AR1 ((__DEF_IINCHIP_MAP_BASE__ + 0x02))
  13 +#define IDM_DR ((__DEF_IINCHIP_MAP_BASE__ + 0x03))
  14 +
  15 +
  16 +/**
  17 + @brief Gateway IP Register address
  18 + */
  19 +#define GAR0 (COMMON_BASE + 0x0001)
  20 +/**
  21 + @brief Subnet mask Register address
  22 + */
  23 +#define SUBR0 (COMMON_BASE + 0x0005)
  24 +/**
  25 + @brief Source MAC Register address
  26 + */
  27 +#define SHAR0 (COMMON_BASE + 0x0009)
  28 +/**
  29 + @brief Source IP Register address
  30 + */
  31 +#define SIPR0 (COMMON_BASE + 0x000F)
  32 +/**
  33 + @brief Interrupt Register
  34 + */
  35 +#define IR (COMMON_BASE + 0x0015)
  36 +/**
  37 + @brief Interrupt mask register
  38 + */
  39 +#define IMR (COMMON_BASE + 0x0016)
  40 +/**
  41 + @brief Timeout register address( 1 is 100us )
  42 + */
  43 +#define RTR0 (COMMON_BASE + 0x0017)
  44 +/**
  45 + @brief Retry count reigster
  46 + */
  47 +#define RCR (COMMON_BASE + 0x0019)
  48 +/**
  49 + @brief Receive memory size reigster
  50 + */
  51 +#define RMSR (COMMON_BASE + 0x001A)
  52 +/**
  53 + @brief Transmit memory size reigster
  54 + */
  55 +#define TMSR (COMMON_BASE + 0x001B)
  56 +/**
  57 + @brief Authentication type register address in PPPoE mode
  58 + */
  59 +#define PATR0 (COMMON_BASE + 0x001C)
  60 +//#define PPPALGO (COMMON_BASE + 0x001D)
  61 +#define PTIMER (COMMON_BASE + 0x0028)
  62 +#define PMAGIC (COMMON_BASE + 0x0029)
  63 +
  64 +/**
  65 + @brief Unreachable IP register address in UDP mode
  66 + */
  67 +#define UIPR0 (COMMON_BASE + 0x002A)
  68 +/**
  69 + @brief Unreachable Port register address in UDP mode
  70 + */
  71 +#define UPORT0 (COMMON_BASE + 0x002E)
  72 +
  73 +/**
  74 + @brief socket register
  75 +*/
  76 +#define CH_BASE (COMMON_BASE + 0x0400)
  77 +/**
  78 + @brief size of each channel register map
  79 + */
  80 +#define CH_SIZE 0x0100
  81 +/**
  82 + @brief socket Mode register
  83 + */
  84 +#define Sn_MR(ch) (CH_BASE + ch * CH_SIZE + 0x0000)
  85 +/**
  86 + @brief channel Sn_CR register
  87 + */
  88 +#define Sn_CR(ch) (CH_BASE + ch * CH_SIZE + 0x0001)
  89 +/**
  90 + @brief channel interrupt register
  91 + */
  92 +#define Sn_IR(ch) (CH_BASE + ch * CH_SIZE + 0x0002)
  93 +/**
  94 + @brief channel status register
  95 + */
  96 +#define Sn_SR(ch) (CH_BASE + ch * CH_SIZE + 0x0003)
  97 +/**
  98 + @brief source port register
  99 + */
  100 +#define Sn_PORT0(ch) (CH_BASE + ch * CH_SIZE + 0x0004)
  101 +/**
  102 + @brief Peer MAC register address
  103 + */
  104 +#define Sn_DHAR0(ch) (CH_BASE + ch * CH_SIZE + 0x0006)
  105 +/**
  106 + @brief Peer IP register address
  107 + */
  108 +#define Sn_DIPR0(ch) (CH_BASE + ch * CH_SIZE + 0x000C)
  109 +/**
  110 + @brief Peer port register address
  111 + */
  112 +#define Sn_DPORT0(ch) (CH_BASE + ch * CH_SIZE + 0x0010)
  113 +/**
  114 + @brief Maximum Segment Size(Sn_MSSR0) register address
  115 + */
  116 +#define Sn_MSSR0(ch) (CH_BASE + ch * CH_SIZE + 0x0012)
  117 +/**
  118 + @brief Protocol of IP Header field register in IP raw mode
  119 + */
  120 +#define Sn_PROTO(ch) (CH_BASE + ch * CH_SIZE + 0x0014)
  121 +
  122 +/**
  123 + @brief IP Type of Service(TOS) Register
  124 + */
  125 +#define Sn_TOS(ch) (CH_BASE + ch * CH_SIZE + 0x0015)
  126 +/**
  127 + @brief IP Time to live(TTL) Register
  128 + */
  129 +#define Sn_TTL(ch) (CH_BASE + ch * CH_SIZE + 0x0016)
  130 +
  131 +/**
  132 + @brief Transmit free memory size register
  133 + */
  134 +#define Sn_TX_FSR0(ch) (CH_BASE + ch * CH_SIZE + 0x0020)
  135 +/**
  136 + @brief Transmit memory read pointer register address
  137 + */
  138 +#define Sn_TX_RD0(ch) (CH_BASE + ch * CH_SIZE + 0x0022)
  139 +/**
  140 + @brief Transmit memory write pointer register address
  141 + */
  142 +#define Sn_TX_WR0(ch) (CH_BASE + ch * CH_SIZE + 0x0024)
  143 +/**
  144 + @brief Received data size register
  145 + */
  146 +#define Sn_RX_RSR0(ch) (CH_BASE + ch * CH_SIZE + 0x0026)
  147 +/**
  148 + @brief Read point of Receive memory
  149 + */
  150 +#define Sn_RX_RD0(ch) (CH_BASE + ch * CH_SIZE + 0x0028)
  151 +/**
  152 + @brief Write point of Receive memory
  153 + */
  154 +#define Sn_RX_WR0(ch) (CH_BASE + ch * CH_SIZE + 0x002A)
  155 +
  156 +
  157 +
  158 +/* MODE register values */
  159 +#define MR_RST 0x80 /**< reset */
  160 +#define MR_PB 0x10 /**< ping block */
  161 +#define MR_PPPOE 0x08 /**< enable pppoe */
  162 +#define MR_LB 0x04 /**< little or big endian selector in indirect mode */
  163 +#define MR_AI 0x02 /**< auto-increment in indirect mode */
  164 +#define MR_IND 0x01 /**< enable indirect mode */
  165 +
  166 +/* IR register values */
  167 +#define IR_CONFLICT 0x80 /**< check ip confict */
  168 +#define IR_UNREACH 0x40 /**< get the destination unreachable message in UDP sending */
  169 +#define IR_PPPoE 0x20 /**< get the PPPoE close message */
  170 +#define IR_SOCK(ch) (0x01 << ch) /**< check socket interrupt */
  171 +
  172 +/* Sn_MR values */
  173 +#define Sn_MR_CLOSE 0x00 /**< unused socket */
  174 +#define Sn_MR_TCP 0x01 /**< TCP */
  175 +#define Sn_MR_UDP 0x02 /**< UDP */
  176 +#define Sn_MR_IPRAW 0x03 /**< IP LAYER RAW SOCK */
  177 +#define Sn_MR_MACRAW 0x04 /**< MAC LAYER RAW SOCK */
  178 +#define Sn_MR_PPPOE 0x05 /**< PPPoE */
  179 +#define Sn_MR_ND 0x20 /**< No Delayed Ack(TCP) flag */
  180 +#define Sn_MR_MULTI 0x80 /**< support multicating */
  181 +
  182 +
  183 +/* Sn_CR values */
  184 +#define Sn_CR_OPEN 0x01 /**< initialize or open socket */
  185 +#define Sn_CR_LISTEN 0x02 /**< wait connection request in tcp mode(Server mode) */
  186 +#define Sn_CR_CONNECT 0x04 /**< send connection request in tcp mode(Client mode) */
  187 +#define Sn_CR_DISCON 0x08 /**< send closing reqeuset in tcp mode */
  188 +#define Sn_CR_CLOSE 0x10 /**< close socket */
  189 +#define Sn_CR_SEND 0x20 /**< updata txbuf pointer, send data */
  190 +#define Sn_CR_SEND_MAC 0x21 /**< send data with MAC address, so without ARP process */
  191 +#define Sn_CR_SEND_KEEP 0x22 /**< send keep alive message */
  192 +#define Sn_CR_RECV 0x40 /**< update rxbuf pointer, recv data */
  193 +
  194 +#ifdef __DEF_IINCHIP_PPP__
  195 + #define Sn_CR_PCON 0x23
  196 + #define Sn_CR_PDISCON 0x24
  197 + #define Sn_CR_PCR 0x25
  198 + #define Sn_CR_PCN 0x26
  199 + #define Sn_CR_PCJ 0x27
  200 +#endif
  201 +
  202 +/* Sn_IR values */
  203 +#ifdef __DEF_IINCHIP_PPP__
  204 + #define Sn_IR_PRECV 0x80
  205 + #define Sn_IR_PFAIL 0x40
  206 + #define Sn_IR_PNEXT 0x20
  207 +#endif
  208 +#define Sn_IR_SEND_OK 0x10 /**< complete sending */
  209 +#define Sn_IR_TIMEOUT 0x08 /**< assert timeout */
  210 +#define Sn_IR_RECV 0x04 /**< receiving data */
  211 +#define Sn_IR_DISCON 0x02 /**< closed socket */
  212 +#define Sn_IR_CON 0x01 /**< established connection */
  213 +
  214 +/* Sn_SR values */
  215 +#define SOCK_CLOSED 0x00 /**< closed */
  216 +#define SOCK_INIT 0x13 /**< init state */
  217 +#define SOCK_LISTEN 0x14 /**< listen state */
  218 +#define SOCK_SYNSENT 0x15 /**< connection state */
  219 +#define SOCK_SYNRECV 0x16 /**< connection state */
  220 +#define SOCK_ESTABLISHED 0x17 /**< success to connect */
  221 +#define SOCK_FIN_WAIT 0x18 /**< closing state */
  222 +#define SOCK_CLOSING 0x1A /**< closing state */
  223 +#define SOCK_TIME_WAIT 0x1B /**< closing state */
  224 +#define SOCK_CLOSE_WAIT 0x1C /**< closing state */
  225 +#define SOCK_LAST_ACK 0x1D /**< closing state */
  226 +#define SOCK_UDP 0x22 /**< udp socket */
  227 +#define SOCK_IPRAW 0x32 /**< ip raw mode socket */
  228 +#define SOCK_MACRAW 0x42 /**< mac raw mode socket */
  229 +#define SOCK_PPPOE 0x5F /**< pppoe socket */
  230 +
  231 +/* IP PROTOCOL */
  232 +#define IPPROTO_IP 0 /**< Dummy for IP */
  233 +#define IPPROTO_ICMP 1 /**< Control message protocol */
  234 +#define IPPROTO_IGMP 2 /**< Internet group management protocol */
  235 +#define IPPROTO_GGP 3 /**< Gateway^2 (deprecated) */
  236 +#define IPPROTO_TCP 6 /**< TCP */
  237 +#define IPPROTO_PUP 12 /**< PUP */
  238 +#define IPPROTO_UDP 17 /**< UDP */
  239 +#define IPPROTO_IDP 22 /**< XNS idp */
  240 +#define IPPROTO_ND 77 /**< UNOFFICIAL net disk protocol */
  241 +#define IPPROTO_RAW 255 /**< Raw IP packet */
  242 +
  243 +
  244 +/*********************************************************
  245 +* iinchip access function
  246 +*********************************************************/
  247 +extern uint8 IINCHIP_READ(uint16 addr);
  248 +extern uint8 IINCHIP_WRITE(uint16 addr,uint8 data);
  249 +extern uint16 wiz_read_buf(uint16 addr, uint8* buf,uint16 len);
  250 +extern uint16 wiz_write_buf(uint16 addr,uint8* buf,uint16 len);
  251 +
  252 +extern void iinchip_init(void); // reset iinchip
  253 +extern void sysinit(uint8 tx_size, uint8 rx_size); // setting tx/rx buf size
  254 +extern uint8 getISR(uint8 s);
  255 +extern void putISR(uint8 s, uint8 val);
  256 +extern uint16 getIINCHIP_RxMAX(uint8 s);
  257 +extern uint16 getIINCHIP_TxMAX(uint8 s);
  258 +extern uint16 getIINCHIP_RxMASK(uint8 s);
  259 +extern uint16 getIINCHIP_TxMASK(uint8 s);
  260 +extern uint16 getIINCHIP_RxBASE(uint8 s);
  261 +extern uint16 getIINCHIP_TxBASE(uint8 s);
  262 +extern void setGAR(uint8 * addr); // set gateway address
  263 +extern void setSUBR(uint8 * addr); // set subnet mask address
  264 +extern void setSHAR(uint8 * addr); // set local MAC address
  265 +extern void setSIPR(uint8 * addr); // set local IP address
  266 +extern void setRTR(uint16 timeout); // set retry duration for data transmission, connection, closing ...
  267 +extern void setRCR(uint8 retry); // set retry count (above the value, assert timeout interrupt)
  268 +extern void setIMR(uint8 mask); // set interrupt mask.
  269 +extern void getGAR(uint8 * addr);
  270 +extern void getSUBR(uint8 * addr);
  271 +extern void getSHAR(uint8 * addr);
  272 +extern void getSIPR(uint8 * addr);
  273 +extern uint8 getIR( void );
  274 +extern void setSn_MSS(SOCKET s, uint16 Sn_MSSR0); // set maximum segment size
  275 +extern void setSn_PROTO(SOCKET s, uint8 proto); // set IP Protocol value using IP-Raw mode
  276 +extern uint8 getSn_IR(SOCKET s); // get socket interrupt status
  277 +extern uint8 getSn_SR(SOCKET s); // get socket status
  278 +extern uint16 getSn_TX_FSR(SOCKET s); // get socket TX free buf size
  279 +extern uint16 getSn_RX_RSR(SOCKET s); // get socket RX recv buf size
  280 +extern void setSn_DHAR(SOCKET s, uint8 * addr);
  281 +extern void setSn_DIPR(SOCKET s, uint8 * addr);
  282 +extern void setSn_DPORT(SOCKET s, uint8 * addr);
  283 +extern void getSn_DHAR(SOCKET s, uint8 * addr);
  284 +extern void getSn_DIPR(SOCKET s, uint8 * addr);
  285 +extern void getSn_DPORT(SOCKET s, uint8 * addr);
  286 +extern void setSn_TTL(SOCKET s, uint8 ttl);
  287 +extern void setMR(uint8 val);
  288 +
  289 +#ifdef __DEF_IINCHIP_PPP__
  290 +extern uint8 pppinit(uint8 *id, uint8 idlen, uint8 *passwd, uint8 passwdlen);
  291 +extern uint8 pppterm(uint8 *mac,uint8 *sessionid);
  292 +#endif
  293 +
  294 +extern void send_data_processing(SOCKET s, uint8 *data, uint16 len);
  295 +extern void recv_data_processing(SOCKET s, uint8 *data, uint16 len);
  296 +extern void read_data(SOCKET s, vuint8 * src, vuint8 * dst, uint16 len);
  297 +extern void write_data(SOCKET s, vuint8 * src, vuint8 * dst, uint16 len);
  298 +
  299 +#endif