Added Arduino Uno port for W5100 Ethernet shield. Note: requires an Arduino library that is included with the Arduino board, but not included here. Thank you, Miguel Fernandes!
This commit is contained in:
@@ -0,0 +1,118 @@
|
||||
###############################################################################
|
||||
# Makefile for BACnet
|
||||
###############################################################################
|
||||
|
||||
## General Flags
|
||||
MCU = atmega328p
|
||||
AVRDUDE_MCU = m328
|
||||
TARGET = ArduinoEthernet
|
||||
## Tools
|
||||
CC = avr-gcc
|
||||
AR = avr-ar
|
||||
OBJCOPY = avr-objcopy
|
||||
OBJDUMP = avr-objdump
|
||||
SIZE = avr-size
|
||||
AVRDUDE = avrdude
|
||||
LINT = splint
|
||||
|
||||
# programmer id--check the avrdude for complete list
|
||||
# # of available opts. These should include stk500,
|
||||
# # avr910, avrisp, bsd, pony and more. Set this to
|
||||
# # one of the valid "-c PROGRAMMER-ID" values
|
||||
# # described in the avrdude info page.
|
||||
# #
|
||||
AVRDUDE_PROGRAMMERID = avrispmkII
|
||||
#
|
||||
# # port--serial or parallel port to which your
|
||||
# # hardware programmer is attached
|
||||
# #
|
||||
AVRDUDE_PORT = /dev/ttyUSB0
|
||||
|
||||
# local files for this project
|
||||
#CSRC = main.c
|
||||
|
||||
ARDUINOSRC = \
|
||||
src/socket.cpp \
|
||||
src/w5100.cpp \
|
||||
src/SPI.cpp \
|
||||
src/w5100Wrapper.cpp \
|
||||
src/socketWrapper.cpp
|
||||
|
||||
## Include Directories
|
||||
INCLUDES = -Iinclude
|
||||
INCLUDES += -I../core/include
|
||||
|
||||
LIBRARY = lib$(TARGET).a
|
||||
|
||||
## Options common to compile, link and assembly rules
|
||||
COMMON = -mmcu=$(MCU)
|
||||
|
||||
OPTIMIZE_FLAGS = -mcall-prologues
|
||||
#OPTIMIZE_FLAGS += -finline-functions
|
||||
OPTIMIZE_FLAGS += -finline-functions-called-once
|
||||
#OPTIMIZATION = -O0
|
||||
#OPTIMIZATION = -Os
|
||||
OPTIMIZATION = -Os $(OPTIMIZE_FLAGS)
|
||||
#OPTIMIZATION = -O3 $(OPTIMIZE_FLAGS)
|
||||
|
||||
## Compile options common for all C compilation units.
|
||||
CFLAGS = $(COMMON)
|
||||
# dead code removal
|
||||
CFLAGS += -ffunction-sections -fdata-sections
|
||||
CFLAGS += -Wall -gdwarf-2 $(OPTIMIZATION) -fsigned-char
|
||||
CFLAGS += -MD -MP -MT $(*F).o -MF dep/$(@F).d
|
||||
|
||||
## Assembly specific flags
|
||||
ASMFLAGS = $(COMMON)
|
||||
ASMFLAGS += $(CFLAGS)
|
||||
ASMFLAGS += -x assembler-with-cpp -Wa,-gdwarf2
|
||||
|
||||
## Linker flags
|
||||
LDFLAGS = $(COMMON)
|
||||
#dead code removal
|
||||
#LDFLAGS += -Wl,-nostartfiles,-nostdlib
|
||||
LDFLAGS += -Wl,--gc-sections,-static
|
||||
LDFLAGS += -Wl,-Map=$(TARGET).map,-L.,-l$(TARGET)
|
||||
#LDFLAGS += -Wl,-Map=$(TARGET).map
|
||||
LDFLAGS += -L../core/lib,-lArduinoUnoCore
|
||||
|
||||
## Intel Hex file production flags
|
||||
HEX_FLASH_FLAGS = -R .eeprom
|
||||
HEX_EEPROM_FLAGS = -j .eeprom
|
||||
HEX_EEPROM_FLAGS += --set-section-flags=.eeprom="alloc,load"
|
||||
HEX_EEPROM_FLAGS += --change-section-lma .eeprom=0 --no-change-warnings
|
||||
|
||||
## Objects that must be built in order to link
|
||||
OBJS := ${SRCS:.cpp=.o}
|
||||
|
||||
all: $(LIBRARY) size Makefile
|
||||
|
||||
default: all
|
||||
|
||||
lib: $(LIBRARY)
|
||||
|
||||
$(LIBRARY): $(OBJS) Makefile
|
||||
$(AR) rcs lib/$@ $(OBJS)
|
||||
$(OBJDUMP) --syms lib/$@ > lib/$(LIBRARY:.a=.lst)
|
||||
|
||||
.c.o:
|
||||
$(CC) -c $(INCLUDES) $(CFLAGS) $*.c -o $@
|
||||
|
||||
.cpp.o:
|
||||
$(CC) -c $(INCLUDES) $(CFLAGS) $*.cpp -o $@
|
||||
|
||||
size: ${TARGET_ELF}
|
||||
@echo
|
||||
@${SIZE} ${TARGET_ELF}
|
||||
|
||||
lint:
|
||||
$(LINT) $(BFLAGS) $(CSRC)
|
||||
|
||||
## Clean target
|
||||
.PHONY: clean
|
||||
clean:
|
||||
-rm -rf $(OBJS) dep/*
|
||||
-rm -rf $(LIBRARY) $(OBJS)
|
||||
|
||||
## Other dependencies
|
||||
-include $(shell mkdir dep 2>/dev/null) $(wildcard dep/*)
|
||||
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
* Copyright (c) 2010 by Cristian Maglie <c.maglie@bug.st>
|
||||
* SPI Master library for arduino.
|
||||
*
|
||||
* This file is free software; you can redistribute it and/or modify
|
||||
* it under the terms of either the GNU General Public License version 2
|
||||
* or the GNU Lesser General Public License version 2.1, both as
|
||||
* published by the Free Software Foundation.
|
||||
*/
|
||||
|
||||
#ifndef _SPI_H_INCLUDED
|
||||
#define _SPI_H_INCLUDED
|
||||
|
||||
#include <stdio.h>
|
||||
#include <Arduino.h>
|
||||
#include <avr/pgmspace.h>
|
||||
|
||||
#define SPI_CLOCK_DIV4 0x00
|
||||
#define SPI_CLOCK_DIV16 0x01
|
||||
#define SPI_CLOCK_DIV64 0x02
|
||||
#define SPI_CLOCK_DIV128 0x03
|
||||
#define SPI_CLOCK_DIV2 0x04
|
||||
#define SPI_CLOCK_DIV8 0x05
|
||||
#define SPI_CLOCK_DIV32 0x06
|
||||
//#define SPI_CLOCK_DIV64 0x07
|
||||
|
||||
#define SPI_MODE0 0x00
|
||||
#define SPI_MODE1 0x04
|
||||
#define SPI_MODE2 0x08
|
||||
#define SPI_MODE3 0x0C
|
||||
|
||||
#define SPI_MODE_MASK 0x0C // CPOL = bit 3, CPHA = bit 2 on SPCR
|
||||
#define SPI_CLOCK_MASK 0x03 // SPR1 = bit 1, SPR0 = bit 0 on SPCR
|
||||
#define SPI_2XCLOCK_MASK 0x01 // SPI2X = bit 0 on SPSR
|
||||
|
||||
class SPIClass {
|
||||
public:
|
||||
inline static byte transfer(byte _data);
|
||||
|
||||
// SPI Configuration methods
|
||||
|
||||
inline static void attachInterrupt();
|
||||
inline static void detachInterrupt(); // Default
|
||||
|
||||
static void begin(); // Default
|
||||
static void end();
|
||||
|
||||
static void setBitOrder(uint8_t);
|
||||
static void setDataMode(uint8_t);
|
||||
static void setClockDivider(uint8_t);
|
||||
};
|
||||
|
||||
extern SPIClass SPI;
|
||||
|
||||
byte SPIClass::transfer(byte _data)
|
||||
{
|
||||
SPDR = _data;
|
||||
while (!(SPSR & _BV(SPIF)));
|
||||
return SPDR;
|
||||
}
|
||||
|
||||
void SPIClass::attachInterrupt()
|
||||
{
|
||||
SPCR |= _BV(SPIE);
|
||||
}
|
||||
|
||||
void SPIClass::detachInterrupt()
|
||||
{
|
||||
SPCR &= ~_BV(SPIE);
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,13 @@
|
||||
#ifndef UTIL_H
|
||||
#define UTIL_H
|
||||
|
||||
#define htons(x) ( ((x)<<8) | (((x)>>8)&0xFF) )
|
||||
#define ntohs(x) htons(x)
|
||||
|
||||
#define htonl(x) ( ((x)<<24 & 0xFF000000UL) | \
|
||||
((x)<< 8 & 0x00FF0000UL) | \
|
||||
((x)>> 8 & 0x0000FF00UL) | \
|
||||
((x)>>24 & 0x000000FFUL) )
|
||||
#define ntohl(x) htonl(x)
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,515 @@
|
||||
/*
|
||||
* Copyright (c) 2010 by Cristian Maglie <c.maglie@bug.st>
|
||||
*
|
||||
* This file is free software; you can redistribute it and/or modify
|
||||
* it under the terms of either the GNU General Public License version 2
|
||||
* or the GNU Lesser General Public License version 2.1, both as
|
||||
* published by the Free Software Foundation.
|
||||
*/
|
||||
|
||||
#ifndef W5100_H_INCLUDED
|
||||
#define W5100_H_INCLUDED
|
||||
|
||||
#include <avr/pgmspace.h>
|
||||
#include <SPI.h>
|
||||
|
||||
#define MAX_SOCK_NUM 4
|
||||
|
||||
typedef uint8_t SOCKET;
|
||||
|
||||
#define IDM_OR 0x8000
|
||||
#define IDM_AR0 0x8001
|
||||
#define IDM_AR1 0x8002
|
||||
#define IDM_DR 0x8003
|
||||
/*
|
||||
class MR {
|
||||
public:
|
||||
static const uint8_t RST = 0x80;
|
||||
static const uint8_t PB = 0x10;
|
||||
static const uint8_t PPPOE = 0x08;
|
||||
static const uint8_t LB = 0x04;
|
||||
static const uint8_t AI = 0x02;
|
||||
static const uint8_t IND = 0x01;
|
||||
};
|
||||
*/
|
||||
/*
|
||||
class IR {
|
||||
public:
|
||||
static const uint8_t CONFLICT = 0x80;
|
||||
static const uint8_t UNREACH = 0x40;
|
||||
static const uint8_t PPPoE = 0x20;
|
||||
static const uint8_t SOCK0 = 0x01;
|
||||
static const uint8_t SOCK1 = 0x02;
|
||||
static const uint8_t SOCK2 = 0x04;
|
||||
static const uint8_t SOCK3 = 0x08;
|
||||
static inline uint8_t SOCK(SOCKET ch) { return (0x01 << ch); };
|
||||
};
|
||||
*/
|
||||
|
||||
class SnMR {
|
||||
public:
|
||||
static const uint8_t CLOSE = 0x00;
|
||||
static const uint8_t TCP = 0x01;
|
||||
static const uint8_t UDP = 0x02;
|
||||
static const uint8_t IPRAW = 0x03;
|
||||
static const uint8_t MACRAW = 0x04;
|
||||
static const uint8_t PPPOE = 0x05;
|
||||
static const uint8_t ND = 0x20;
|
||||
static const uint8_t MULTI = 0x80;
|
||||
};
|
||||
|
||||
enum SockCMD {
|
||||
Sock_OPEN = 0x01,
|
||||
Sock_LISTEN = 0x02,
|
||||
Sock_CONNECT = 0x04,
|
||||
Sock_DISCON = 0x08,
|
||||
Sock_CLOSE = 0x10,
|
||||
Sock_SEND = 0x20,
|
||||
Sock_SEND_MAC = 0x21,
|
||||
Sock_SEND_KEEP = 0x22,
|
||||
Sock_RECV = 0x40
|
||||
};
|
||||
|
||||
/*class SnCmd {
|
||||
public:
|
||||
static const uint8_t OPEN = 0x01;
|
||||
static const uint8_t LISTEN = 0x02;
|
||||
static const uint8_t CONNECT = 0x04;
|
||||
static const uint8_t DISCON = 0x08;
|
||||
static const uint8_t CLOSE = 0x10;
|
||||
static const uint8_t SEND = 0x20;
|
||||
static const uint8_t SEND_MAC = 0x21;
|
||||
static const uint8_t SEND_KEEP = 0x22;
|
||||
static const uint8_t RECV = 0x40;
|
||||
};
|
||||
*/
|
||||
|
||||
class SnIR {
|
||||
public:
|
||||
static const uint8_t SEND_OK = 0x10;
|
||||
static const uint8_t TIMEOUT = 0x08;
|
||||
static const uint8_t RECV = 0x04;
|
||||
static const uint8_t DISCON = 0x02;
|
||||
static const uint8_t CON = 0x01;
|
||||
};
|
||||
|
||||
class SnSR {
|
||||
public:
|
||||
static const uint8_t CLOSED = 0x00;
|
||||
static const uint8_t INIT = 0x13;
|
||||
static const uint8_t LISTEN = 0x14;
|
||||
static const uint8_t SYNSENT = 0x15;
|
||||
static const uint8_t SYNRECV = 0x16;
|
||||
static const uint8_t ESTABLISHED = 0x17;
|
||||
static const uint8_t FIN_WAIT = 0x18;
|
||||
static const uint8_t CLOSING = 0x1A;
|
||||
static const uint8_t TIME_WAIT = 0x1B;
|
||||
static const uint8_t CLOSE_WAIT = 0x1C;
|
||||
static const uint8_t LAST_ACK = 0x1D;
|
||||
static const uint8_t UDP = 0x22;
|
||||
static const uint8_t IPRAW = 0x32;
|
||||
static const uint8_t MACRAW = 0x42;
|
||||
static const uint8_t PPPOE = 0x5F;
|
||||
};
|
||||
|
||||
class IPPROTO {
|
||||
public:
|
||||
static const uint8_t IP = 0;
|
||||
static const uint8_t ICMP = 1;
|
||||
static const uint8_t IGMP = 2;
|
||||
static const uint8_t GGP = 3;
|
||||
static const uint8_t TCP = 6;
|
||||
static const uint8_t PUP = 12;
|
||||
static const uint8_t UDP = 17;
|
||||
static const uint8_t IDP = 22;
|
||||
static const uint8_t ND = 77;
|
||||
static const uint8_t RAW = 255;
|
||||
};
|
||||
|
||||
class W5100Class {
|
||||
|
||||
public:
|
||||
void init();
|
||||
|
||||
/**
|
||||
* @brief This function is being used for copy the data form Receive buffer of the chip to application buffer.
|
||||
*
|
||||
* It calculate the actual physical address where one has to read
|
||||
* the data from Receive buffer. Here also take care of the condition while it exceed
|
||||
* the Rx memory uper-bound of socket.
|
||||
*/
|
||||
void read_data(SOCKET s,
|
||||
volatile uint8_t * src,
|
||||
volatile uint8_t * dst,
|
||||
uint16_t len);
|
||||
|
||||
/**
|
||||
* @brief This function is being called by send() and sendto() function also.
|
||||
*
|
||||
* This function read the Tx write pointer register and after copy the data in buffer update the Tx write pointer
|
||||
* register. User should read upper byte first and lower byte later to get proper value.
|
||||
*/
|
||||
void send_data_processing(SOCKET s,
|
||||
const uint8_t * data,
|
||||
uint16_t len);
|
||||
/**
|
||||
* @brief A copy of send_data_processing that uses the provided ptr for the
|
||||
* write offset. Only needed for the "streaming" UDP API, where
|
||||
* a single UDP packet is built up over a number of calls to
|
||||
* send_data_processing_ptr, because TX_WR doesn't seem to get updated
|
||||
* correctly in those scenarios
|
||||
* @param ptr value to use in place of TX_WR. If 0, then the value is read
|
||||
* in from TX_WR
|
||||
* @return New value for ptr, to be used in the next call
|
||||
*/
|
||||
// FIXME Update documentation
|
||||
void send_data_processing_offset(SOCKET s,
|
||||
uint16_t data_offset,
|
||||
const uint8_t * data,
|
||||
uint16_t len);
|
||||
|
||||
/**
|
||||
* @brief This function is being called by recv() also.
|
||||
*
|
||||
* This function read the Rx read pointer register
|
||||
* and after copy the data from receive buffer update the Rx write pointer register.
|
||||
* User should read upper byte first and lower byte later to get proper value.
|
||||
*/
|
||||
void recv_data_processing(SOCKET s,
|
||||
uint8_t * data,
|
||||
uint16_t len,
|
||||
uint8_t peek = 0);
|
||||
|
||||
inline void setGatewayIp(uint8_t * _addr);
|
||||
inline void getGatewayIp(uint8_t * _addr);
|
||||
|
||||
inline void setSubnetMask(uint8_t * _addr);
|
||||
inline void getSubnetMask(uint8_t * _addr);
|
||||
|
||||
inline void setMACAddress(uint8_t * addr);
|
||||
inline void getMACAddress(uint8_t * addr);
|
||||
|
||||
inline void setIPAddress(uint8_t * addr);
|
||||
inline void getIPAddress(uint8_t * addr);
|
||||
|
||||
inline void setRetransmissionTime(uint16_t timeout);
|
||||
inline void setRetransmissionCount(uint8_t _retry);
|
||||
|
||||
void execCmdSn(SOCKET s,
|
||||
SockCMD _cmd);
|
||||
|
||||
uint16_t getTXFreeSize(SOCKET s);
|
||||
uint16_t getRXReceivedSize(SOCKET s);
|
||||
|
||||
|
||||
// W5100 Registers
|
||||
// ---------------
|
||||
private:
|
||||
static uint8_t write(uint16_t _addr,
|
||||
uint8_t _data);
|
||||
static uint16_t write(uint16_t addr,
|
||||
const uint8_t * buf,
|
||||
uint16_t len);
|
||||
static uint8_t read(uint16_t addr);
|
||||
static uint16_t read(uint16_t addr,
|
||||
uint8_t * buf,
|
||||
uint16_t len);
|
||||
|
||||
#define __GP_REGISTER8(name, address) \
|
||||
static inline void write##name(uint8_t _data) { \
|
||||
write(address, _data); \
|
||||
} \
|
||||
static inline uint8_t read##name() { \
|
||||
return read(address); \
|
||||
}
|
||||
#define __GP_REGISTER16(name, address) \
|
||||
static void write##name(uint16_t _data) { \
|
||||
write(address, _data >> 8); \
|
||||
write(address+1, _data & 0xFF); \
|
||||
} \
|
||||
static uint16_t read##name() { \
|
||||
uint16_t res = read(address); \
|
||||
res = (res << 8) + read(address + 1); \
|
||||
return res; \
|
||||
}
|
||||
#define __GP_REGISTER_N(name, address, size) \
|
||||
static uint16_t write##name(uint8_t *_buff) { \
|
||||
return write(address, _buff, size); \
|
||||
} \
|
||||
static uint16_t read##name(uint8_t *_buff) { \
|
||||
return read(address, _buff, size); \
|
||||
}
|
||||
|
||||
public:
|
||||
__GP_REGISTER8(MR,
|
||||
0x0000); // Mode
|
||||
__GP_REGISTER_N(GAR,
|
||||
0x0001,
|
||||
4); // Gateway IP address
|
||||
__GP_REGISTER_N(SUBR,
|
||||
0x0005,
|
||||
4); // Subnet mask address
|
||||
__GP_REGISTER_N(SHAR,
|
||||
0x0009,
|
||||
6); // Source MAC address
|
||||
__GP_REGISTER_N(SIPR,
|
||||
0x000F,
|
||||
4); // Source IP address
|
||||
__GP_REGISTER8(IR,
|
||||
0x0015); // Interrupt
|
||||
__GP_REGISTER8(IMR,
|
||||
0x0016); // Interrupt Mask
|
||||
__GP_REGISTER16(RTR,
|
||||
0x0017); // Timeout address
|
||||
__GP_REGISTER8(RCR,
|
||||
0x0019); // Retry count
|
||||
__GP_REGISTER8(RMSR,
|
||||
0x001A); // Receive memory size
|
||||
__GP_REGISTER8(TMSR,
|
||||
0x001B); // Transmit memory size
|
||||
__GP_REGISTER8(PATR,
|
||||
0x001C); // Authentication type address in PPPoE mode
|
||||
__GP_REGISTER8(PTIMER,
|
||||
0x0028); // PPP LCP Request Timer
|
||||
__GP_REGISTER8(PMAGIC,
|
||||
0x0029); // PPP LCP Magic Number
|
||||
__GP_REGISTER_N(UIPR,
|
||||
0x002A,
|
||||
4); // Unreachable IP address in UDP mode
|
||||
__GP_REGISTER16(UPORT,
|
||||
0x002E); // Unreachable Port address in UDP mode
|
||||
|
||||
#undef __GP_REGISTER8
|
||||
#undef __GP_REGISTER16
|
||||
#undef __GP_REGISTER_N
|
||||
|
||||
// W5100 Socket registers
|
||||
// ----------------------
|
||||
private:
|
||||
static inline uint8_t readSn(SOCKET _s,
|
||||
uint16_t _addr);
|
||||
static inline uint8_t writeSn(SOCKET _s,
|
||||
uint16_t _addr,
|
||||
uint8_t _data);
|
||||
static inline uint16_t readSn(SOCKET _s,
|
||||
uint16_t _addr,
|
||||
uint8_t * _buf,
|
||||
uint16_t len);
|
||||
static inline uint16_t writeSn(SOCKET _s,
|
||||
uint16_t _addr,
|
||||
uint8_t * _buf,
|
||||
uint16_t len);
|
||||
|
||||
static const uint16_t CH_BASE = 0x0400;
|
||||
static const uint16_t CH_SIZE = 0x0100;
|
||||
|
||||
#define __SOCKET_REGISTER8(name, address) \
|
||||
static inline void write##name(SOCKET _s, uint8_t _data) { \
|
||||
writeSn(_s, address, _data); \
|
||||
} \
|
||||
static inline uint8_t read##name(SOCKET _s) { \
|
||||
return readSn(_s, address); \
|
||||
}
|
||||
#define __SOCKET_REGISTER16(name, address) \
|
||||
static void write##name(SOCKET _s, uint16_t _data) { \
|
||||
writeSn(_s, address, _data >> 8); \
|
||||
writeSn(_s, address+1, _data & 0xFF); \
|
||||
} \
|
||||
static uint16_t read##name(SOCKET _s) { \
|
||||
uint16_t res = readSn(_s, address); \
|
||||
uint16_t res2 = readSn(_s,address + 1); \
|
||||
res = res << 8; \
|
||||
res2 = res2 & 0xFF; \
|
||||
res = res | res2; \
|
||||
return res; \
|
||||
}
|
||||
#define __SOCKET_REGISTER_N(name, address, size) \
|
||||
static uint16_t write##name(SOCKET _s, uint8_t *_buff) { \
|
||||
return writeSn(_s, address, _buff, size); \
|
||||
} \
|
||||
static uint16_t read##name(SOCKET _s, uint8_t *_buff) { \
|
||||
return readSn(_s, address, _buff, size); \
|
||||
}
|
||||
|
||||
public:
|
||||
__SOCKET_REGISTER8(SnMR,
|
||||
0x0000) // Mode
|
||||
__SOCKET_REGISTER8(SnCR,
|
||||
0x0001) // Command
|
||||
__SOCKET_REGISTER8(SnIR,
|
||||
0x0002) // Interrupt
|
||||
__SOCKET_REGISTER8(SnSR,
|
||||
0x0003) // Status
|
||||
__SOCKET_REGISTER16(SnPORT,
|
||||
0x0004) // Source Port
|
||||
__SOCKET_REGISTER_N(SnDHAR,
|
||||
0x0006,
|
||||
6) // Destination Hardw Addr
|
||||
__SOCKET_REGISTER_N(SnDIPR,
|
||||
0x000C,
|
||||
4) // Destination IP Addr
|
||||
__SOCKET_REGISTER16(SnDPORT,
|
||||
0x0010) // Destination Port
|
||||
__SOCKET_REGISTER16(SnMSSR,
|
||||
0x0012) // Max Segment Size
|
||||
__SOCKET_REGISTER8(SnPROTO,
|
||||
0x0014) // Protocol in IP RAW Mode
|
||||
__SOCKET_REGISTER8(SnTOS,
|
||||
0x0015) // IP TOS
|
||||
__SOCKET_REGISTER8(SnTTL,
|
||||
0x0016) // IP TTL
|
||||
__SOCKET_REGISTER16(SnTX_FSR,
|
||||
0x0020) // TX Free Size
|
||||
__SOCKET_REGISTER16(SnTX_RD,
|
||||
0x0022) // TX Read Pointer
|
||||
__SOCKET_REGISTER16(SnTX_WR,
|
||||
0x0024) // TX Write Pointer
|
||||
__SOCKET_REGISTER16(SnRX_RSR,
|
||||
0x0026) // RX Free Size
|
||||
__SOCKET_REGISTER16(SnRX_RD,
|
||||
0x0028) // RX Read Pointer
|
||||
__SOCKET_REGISTER16(SnRX_WR,
|
||||
0x002A) // RX Write Pointer (supported?)
|
||||
#undef __SOCKET_REGISTER8
|
||||
#undef __SOCKET_REGISTER16
|
||||
#undef __SOCKET_REGISTER_N
|
||||
private:
|
||||
static const uint8_t RST = 7; // Reset BIT
|
||||
|
||||
static const int SOCKETS = 4;
|
||||
static const uint16_t SMASK = 0x07FF; // Tx buffer MASK
|
||||
static const uint16_t RMASK = 0x07FF; // Rx buffer MASK
|
||||
public:
|
||||
static const uint16_t SSIZE = 2048; // Max Tx buffer size
|
||||
private:
|
||||
static const uint16_t RSIZE = 2048; // Max Rx buffer size
|
||||
uint16_t SBASE[SOCKETS]; // Tx buffer base address
|
||||
uint16_t RBASE[SOCKETS]; // Rx buffer base address
|
||||
|
||||
private:
|
||||
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
|
||||
inline static void initSS() {
|
||||
DDRB |= _BV(4);
|
||||
};
|
||||
inline static void setSS() {
|
||||
PORTB &= ~_BV(4);
|
||||
};
|
||||
inline static void resetSS() {
|
||||
PORTB |= _BV(4);
|
||||
};
|
||||
#elif defined(__AVR_ATmega32U4__)
|
||||
inline static void initSS() {
|
||||
DDRB |= _BV(6);
|
||||
};
|
||||
inline static void setSS() {
|
||||
PORTB &= ~_BV(6);
|
||||
};
|
||||
inline static void resetSS() {
|
||||
PORTB |= _BV(6);
|
||||
};
|
||||
#elif defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB162__)
|
||||
inline static void initSS() {
|
||||
DDRB |= _BV(0);
|
||||
};
|
||||
inline static void setSS() {
|
||||
PORTB &= ~_BV(0);
|
||||
};
|
||||
inline static void resetSS() {
|
||||
PORTB |= _BV(0);
|
||||
};
|
||||
#else
|
||||
inline static void initSS() {
|
||||
DDRB |= _BV(2);
|
||||
};
|
||||
inline static void setSS() {
|
||||
PORTB &= ~_BV(2);
|
||||
};
|
||||
inline static void resetSS() {
|
||||
PORTB |= _BV(2);
|
||||
};
|
||||
#endif
|
||||
|
||||
};
|
||||
|
||||
extern W5100Class W5100;
|
||||
|
||||
uint8_t W5100Class::readSn(SOCKET _s,
|
||||
uint16_t _addr)
|
||||
{
|
||||
return read(CH_BASE + _s * CH_SIZE + _addr);
|
||||
}
|
||||
|
||||
uint8_t W5100Class::writeSn(SOCKET _s,
|
||||
uint16_t _addr,
|
||||
uint8_t _data)
|
||||
{
|
||||
return write(CH_BASE + _s * CH_SIZE + _addr, _data);
|
||||
}
|
||||
|
||||
uint16_t W5100Class::readSn(SOCKET _s,
|
||||
uint16_t _addr,
|
||||
uint8_t * _buf,
|
||||
uint16_t _len)
|
||||
{
|
||||
return read(CH_BASE + _s * CH_SIZE + _addr, _buf, _len);
|
||||
}
|
||||
|
||||
uint16_t W5100Class::writeSn(SOCKET _s,
|
||||
uint16_t _addr,
|
||||
uint8_t * _buf,
|
||||
uint16_t _len)
|
||||
{
|
||||
return write(CH_BASE + _s * CH_SIZE + _addr, _buf, _len);
|
||||
}
|
||||
|
||||
void W5100Class::getGatewayIp(uint8_t * _addr)
|
||||
{
|
||||
readGAR(_addr);
|
||||
}
|
||||
|
||||
void W5100Class::setGatewayIp(uint8_t * _addr)
|
||||
{
|
||||
writeGAR(_addr);
|
||||
}
|
||||
|
||||
void W5100Class::getSubnetMask(uint8_t * _addr)
|
||||
{
|
||||
readSUBR(_addr);
|
||||
}
|
||||
|
||||
void W5100Class::setSubnetMask(uint8_t * _addr)
|
||||
{
|
||||
writeSUBR(_addr);
|
||||
}
|
||||
|
||||
void W5100Class::getMACAddress(uint8_t * _addr)
|
||||
{
|
||||
readSHAR(_addr);
|
||||
}
|
||||
|
||||
void W5100Class::setMACAddress(uint8_t * _addr)
|
||||
{
|
||||
writeSHAR(_addr);
|
||||
}
|
||||
|
||||
void W5100Class::getIPAddress(uint8_t * _addr)
|
||||
{
|
||||
readSIPR(_addr);
|
||||
}
|
||||
|
||||
void W5100Class::setIPAddress(uint8_t * _addr)
|
||||
{
|
||||
writeSIPR(_addr);
|
||||
}
|
||||
|
||||
void W5100Class::setRetransmissionTime(uint16_t _timeout)
|
||||
{
|
||||
writeRTR(_timeout);
|
||||
}
|
||||
|
||||
void W5100Class::setRetransmissionCount(uint8_t _retry)
|
||||
{
|
||||
writeRCR(_retry);
|
||||
}
|
||||
|
||||
#endif
|
||||
+139
@@ -0,0 +1,139 @@
|
||||
/*
|
||||
* w5100Wrapper.h
|
||||
*
|
||||
* Created on: 26 de Mai de 2013
|
||||
* Author: mgf
|
||||
*/
|
||||
|
||||
#ifndef W5100WRAPPER_H_
|
||||
#define W5100WRAPPER_H_
|
||||
|
||||
#include <avr/pgmspace.h>
|
||||
|
||||
typedef uint8_t SOCKET;
|
||||
typedef void CSnMR;
|
||||
typedef void CSnIR;
|
||||
typedef void CSnSR;
|
||||
typedef void CIPPROTO;
|
||||
typedef void CW5100Class;
|
||||
|
||||
#define MAX_SOCK_NUM 4
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
CSnMR *CSnMR_new();
|
||||
void CSnMR_delete(const CSnMR * obj);
|
||||
uint8_t SnMR_CLOSE();
|
||||
uint8_t SnMR_UDP();
|
||||
uint8_t SnMR_TCP();
|
||||
uint8_t SnMR_IPRAW();
|
||||
uint8_t SnMR_MACRAW();
|
||||
uint8_t SnMR_PPPOE();
|
||||
uint8_t SnMR_ND();
|
||||
uint8_t SnMR_MULTI();
|
||||
|
||||
CSnIR *CSnIR_new();
|
||||
void CSnIR_delete(const CSnIR * obj);
|
||||
uint8_t SnIR_SEND_OK();
|
||||
uint8_t SnIR_TIMEOUT();
|
||||
uint8_t SnIR_RECV();
|
||||
uint8_t SnIR_DISCON();
|
||||
uint8_t SnIR_CON();
|
||||
|
||||
|
||||
CSnSR *CSnSR_new();
|
||||
void CSnSR_delete(const CSnSR * obj);
|
||||
uint8_t SnSR_CLOSED();
|
||||
uint8_t SnSR_INIT();
|
||||
uint8_t SnSR_LISTEN();
|
||||
uint8_t SnSR_SYNSENT();
|
||||
uint8_t SnSR_SYNRECV();
|
||||
uint8_t SnSR_ESTABLISHED();
|
||||
uint8_t SnSR_FIN_WAIT();
|
||||
uint8_t SnSR_CLOSING();
|
||||
uint8_t SnSR_TIME_WAIT();
|
||||
uint8_t SnSR_CLOSE_WAIT();
|
||||
uint8_t SnSR_LAST_ACK();
|
||||
uint8_t SnSR_UDP();
|
||||
uint8_t SnSR_IPRAW();
|
||||
uint8_t SnSR_MACRAW();
|
||||
uint8_t SnSR_PPPOE();
|
||||
|
||||
CIPPROTO *CIPPROTO_new();
|
||||
void CIPPROTO_delete(const CIPPROTO * obj);
|
||||
uint8_t IPPROTO_IP();
|
||||
uint8_t IPPROTO_ICMP();
|
||||
uint8_t IPPROTO_IGMP();
|
||||
uint8_t IPPROTO_GGP();
|
||||
uint8_t IPPROTO_TCP();
|
||||
uint8_t IPPROTO_PUP();
|
||||
uint8_t IPPROTO_UDP();
|
||||
uint8_t IPPROTO_IDP();
|
||||
uint8_t IPPROTO_ND();
|
||||
uint8_t IPPROTO_RAW();
|
||||
|
||||
CW5100Class *CW5100Class_new();
|
||||
void init_func(const CW5100Class * obj);
|
||||
void CW5100Class_delete(const CW5100Class * obj);
|
||||
|
||||
void read_data_func(const CW5100Class * obj,
|
||||
SOCKET s,
|
||||
volatile uint8_t * src,
|
||||
volatile uint8_t * dst,
|
||||
uint16_t len);
|
||||
|
||||
void send_data_processing_func(const CW5100Class * obj,
|
||||
SOCKET s,
|
||||
const uint8_t * data,
|
||||
uint16_t len);
|
||||
void send_data_processing_offset_func(const CW5100Class * obj,
|
||||
SOCKET s,
|
||||
uint16_t data_offset,
|
||||
const uint8_t * data,
|
||||
uint16_t len);
|
||||
//FIXME: Removed defaul value of 0(zero) from the peek argument
|
||||
void recv_data_processing_func(const CW5100Class * obj,
|
||||
SOCKET s,
|
||||
uint8_t * data,
|
||||
uint16_t len,
|
||||
uint8_t peek);
|
||||
void setGatewayIp_func(const CW5100Class * obj,
|
||||
uint8_t * _addr);
|
||||
void getGatewayIp_func(const CW5100Class * obj,
|
||||
uint8_t * _addr);
|
||||
//
|
||||
void setSubnetMask_func(const CW5100Class * obj,
|
||||
uint8_t * _addr);
|
||||
void getSubnetMask_func(const CW5100Class * obj,
|
||||
uint8_t * _addr);
|
||||
//
|
||||
void setMACAddress_func(const CW5100Class * obj,
|
||||
uint8_t * addr);
|
||||
void getMACAddress_func(const CW5100Class * obj,
|
||||
uint8_t * addr);
|
||||
//
|
||||
void setIPAddress_func(const CW5100Class * obj,
|
||||
uint8_t * addr);
|
||||
void getIPAddress_func(const CW5100Class * obj,
|
||||
uint8_t * addr);
|
||||
//
|
||||
void setRetransmissionTime_func(const CW5100Class * obj,
|
||||
uint16_t timeout);
|
||||
void setRetransmissionCount_func(const CW5100Class * obj,
|
||||
uint8_t _retry);
|
||||
//
|
||||
|
||||
uint16_t getTXFreeSize_func(const CW5100Class * obj,
|
||||
SOCKET s);
|
||||
uint16_t getRXReceivedSize_func(const CW5100Class * obj,
|
||||
SOCKET s);
|
||||
|
||||
uint8_t readSnSR_func(const CW5100Class * obj,
|
||||
SOCKET s);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif /* W5100WRAPPER_H_ */
|
||||
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
* Copyright (c) 2010 by Cristian Maglie <c.maglie@bug.st>
|
||||
* SPI Master library for arduino.
|
||||
*
|
||||
* This file is free software; you can redistribute it and/or modify
|
||||
* it under the terms of either the GNU General Public License version 2
|
||||
* or the GNU Lesser General Public License version 2.1, both as
|
||||
* published by the Free Software Foundation.
|
||||
*/
|
||||
|
||||
#include "pins_arduino.h"
|
||||
#include "SPI.h"
|
||||
|
||||
SPIClass SPI;
|
||||
|
||||
void SPIClass::begin() {
|
||||
|
||||
// Set SS to high so a connected chip will be "deselected" by default
|
||||
digitalWrite(SS, HIGH);
|
||||
|
||||
// When the SS pin is set as OUTPUT, it can be used as
|
||||
// a general purpose output port (it doesn't influence
|
||||
// SPI operations).
|
||||
pinMode(SS, OUTPUT);
|
||||
|
||||
// Warning: if the SS pin ever becomes a LOW INPUT then SPI
|
||||
// automatically switches to Slave, so the data direction of
|
||||
// the SS pin MUST be kept as OUTPUT.
|
||||
SPCR |= _BV(MSTR);
|
||||
SPCR |= _BV(SPE);
|
||||
|
||||
// Set direction register for SCK and MOSI pin.
|
||||
// MISO pin automatically overrides to INPUT.
|
||||
// By doing this AFTER enabling SPI, we avoid accidentally
|
||||
// clocking in a single bit since the lines go directly
|
||||
// from "input" to SPI control.
|
||||
// http://code.google.com/p/arduino/issues/detail?id=888
|
||||
pinMode(SCK, OUTPUT);
|
||||
pinMode(MOSI, OUTPUT);
|
||||
}
|
||||
|
||||
|
||||
void SPIClass::end() {
|
||||
SPCR &= ~_BV(SPE);
|
||||
}
|
||||
|
||||
void SPIClass::setBitOrder(uint8_t bitOrder)
|
||||
{
|
||||
if(bitOrder == LSBFIRST) {
|
||||
SPCR |= _BV(DORD);
|
||||
} else {
|
||||
SPCR &= ~(_BV(DORD));
|
||||
}
|
||||
}
|
||||
|
||||
void SPIClass::setDataMode(uint8_t mode)
|
||||
{
|
||||
SPCR = (SPCR & ~SPI_MODE_MASK) | mode;
|
||||
}
|
||||
|
||||
void SPIClass::setClockDivider(uint8_t rate)
|
||||
{
|
||||
SPCR = (SPCR & ~SPI_CLOCK_MASK) | (rate & SPI_CLOCK_MASK);
|
||||
SPSR = (SPSR & ~SPI_2XCLOCK_MASK) | ((rate >> 2) & SPI_2XCLOCK_MASK);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,400 @@
|
||||
#include "w5100.h"
|
||||
#include "socket.h"
|
||||
|
||||
static uint16_t local_port;
|
||||
|
||||
/**
|
||||
* @brief This Socket function initialize the channel in perticular mode, and set the port and wait for W5100 done it.
|
||||
* @return 1 for success else 0.
|
||||
*/
|
||||
uint8_t socket(SOCKET s, uint8_t protocol, uint16_t port, uint8_t flag)
|
||||
{
|
||||
if ((protocol == SnMR::TCP) || (protocol == SnMR::UDP) || (protocol == SnMR::IPRAW) || (protocol == SnMR::MACRAW) || (protocol == SnMR::PPPOE))
|
||||
{
|
||||
close(s);
|
||||
W5100.writeSnMR(s, protocol | flag);
|
||||
if (port != 0) {
|
||||
W5100.writeSnPORT(s, port);
|
||||
}
|
||||
else {
|
||||
local_port++; // if don't set the source port, set local_port number.
|
||||
W5100.writeSnPORT(s, local_port);
|
||||
}
|
||||
|
||||
W5100.execCmdSn(s, Sock_OPEN);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief This function close the socket and parameter is "s" which represent the socket number
|
||||
*/
|
||||
void close(SOCKET s)
|
||||
{
|
||||
W5100.execCmdSn(s, Sock_CLOSE);
|
||||
W5100.writeSnIR(s, 0xFF);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief This function established the connection for the channel in passive (server) mode. This function waits for the request from the peer.
|
||||
* @return 1 for success else 0.
|
||||
*/
|
||||
uint8_t listen(SOCKET s)
|
||||
{
|
||||
if (W5100.readSnSR(s) != SnSR::INIT)
|
||||
return 0;
|
||||
W5100.execCmdSn(s, Sock_LISTEN);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief This function established the connection for the channel in Active (client) mode.
|
||||
* This function waits for the untill the connection is established.
|
||||
*
|
||||
* @return 1 for success else 0.
|
||||
*/
|
||||
uint8_t connect(SOCKET s, uint8_t * addr, uint16_t port)
|
||||
{
|
||||
if
|
||||
(
|
||||
((addr[0] == 0xFF) && (addr[1] == 0xFF) && (addr[2] == 0xFF) && (addr[3] == 0xFF)) ||
|
||||
((addr[0] == 0x00) && (addr[1] == 0x00) && (addr[2] == 0x00) && (addr[3] == 0x00)) ||
|
||||
(port == 0x00)
|
||||
)
|
||||
return 0;
|
||||
|
||||
// set destination IP
|
||||
W5100.writeSnDIPR(s, addr);
|
||||
W5100.writeSnDPORT(s, port);
|
||||
W5100.execCmdSn(s, Sock_CONNECT);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @brief This function used for disconnect the socket and parameter is "s" which represent the socket number
|
||||
* @return 1 for success else 0.
|
||||
*/
|
||||
void disconnect(SOCKET s)
|
||||
{
|
||||
W5100.execCmdSn(s, Sock_DISCON);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief This function used to send the data in TCP mode
|
||||
* @return 1 for success else 0.
|
||||
*/
|
||||
uint16_t send(SOCKET s, const uint8_t * buf, uint16_t len)
|
||||
{
|
||||
uint8_t status=0;
|
||||
uint16_t ret=0;
|
||||
uint16_t freesize=0;
|
||||
|
||||
if (len > W5100.SSIZE)
|
||||
ret = W5100.SSIZE; // check size not to exceed MAX size.
|
||||
else
|
||||
ret = len;
|
||||
|
||||
// if freebuf is available, start.
|
||||
do
|
||||
{
|
||||
freesize = W5100.getTXFreeSize(s);
|
||||
status = W5100.readSnSR(s);
|
||||
if ((status != SnSR::ESTABLISHED) && (status != SnSR::CLOSE_WAIT))
|
||||
{
|
||||
ret = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
while (freesize < ret);
|
||||
|
||||
// copy data
|
||||
W5100.send_data_processing(s, (uint8_t *)buf, ret);
|
||||
W5100.execCmdSn(s, Sock_SEND);
|
||||
|
||||
/* +2008.01 bj */
|
||||
while ( (W5100.readSnIR(s) & SnIR::SEND_OK) != SnIR::SEND_OK )
|
||||
{
|
||||
/* m2008.01 [bj] : reduce code */
|
||||
if ( W5100.readSnSR(s) == SnSR::CLOSED )
|
||||
{
|
||||
close(s);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
/* +2008.01 bj */
|
||||
W5100.writeSnIR(s, SnIR::SEND_OK);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief This function is an application I/F function which is used to receive the data in TCP mode.
|
||||
* It continues to wait for data as much as the application wants to receive.
|
||||
*
|
||||
* @return received data size for success else -1.
|
||||
*/
|
||||
int16_t recv(SOCKET s, uint8_t *buf, int16_t len)
|
||||
{
|
||||
// Check how much data is available
|
||||
int16_t ret = W5100.getRXReceivedSize(s);
|
||||
if ( ret == 0 )
|
||||
{
|
||||
// No data available.
|
||||
uint8_t status = W5100.readSnSR(s);
|
||||
if ( status == SnSR::LISTEN || status == SnSR::CLOSED || status == SnSR::CLOSE_WAIT )
|
||||
{
|
||||
// The remote end has closed its side of the connection, so this is the eof state
|
||||
ret = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
// The connection is still up, but there's no data waiting to be read
|
||||
ret = -1;
|
||||
}
|
||||
}
|
||||
else if (ret > len)
|
||||
{
|
||||
ret = len;
|
||||
}
|
||||
|
||||
if ( ret > 0 )
|
||||
{
|
||||
W5100.recv_data_processing(s, buf, ret);
|
||||
W5100.execCmdSn(s, Sock_RECV);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Returns the first byte in the receive queue (no checking)
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
uint16_t peek(SOCKET s, uint8_t *buf)
|
||||
{
|
||||
W5100.recv_data_processing(s, buf, 1, 1);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief This function is an application I/F function which is used to send the data for other then TCP mode.
|
||||
* Unlike TCP transmission, The peer's destination address and the port is needed.
|
||||
*
|
||||
* @return This function return send data size for success else -1.
|
||||
*/
|
||||
uint16_t sendto(SOCKET s, const uint8_t *buf, uint16_t len, uint8_t *addr, uint16_t port)
|
||||
{
|
||||
uint16_t ret=0;
|
||||
|
||||
if (len > W5100.SSIZE) ret = W5100.SSIZE; // check size not to exceed MAX size.
|
||||
else ret = len;
|
||||
|
||||
if
|
||||
(
|
||||
((addr[0] == 0x00) && (addr[1] == 0x00) && (addr[2] == 0x00) && (addr[3] == 0x00)) ||
|
||||
((port == 0x00)) ||(ret == 0)
|
||||
)
|
||||
{
|
||||
/* +2008.01 [bj] : added return value */
|
||||
ret = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
W5100.writeSnDIPR(s, addr);
|
||||
W5100.writeSnDPORT(s, port);
|
||||
|
||||
// copy data
|
||||
W5100.send_data_processing(s, (uint8_t *)buf, ret);
|
||||
W5100.execCmdSn(s, Sock_SEND);
|
||||
|
||||
/* +2008.01 bj */
|
||||
while ( (W5100.readSnIR(s) & SnIR::SEND_OK) != SnIR::SEND_OK )
|
||||
{
|
||||
if (W5100.readSnIR(s) & SnIR::TIMEOUT)
|
||||
{
|
||||
/* +2008.01 [bj]: clear interrupt */
|
||||
W5100.writeSnIR(s, (SnIR::SEND_OK | SnIR::TIMEOUT)); /* clear SEND_OK & TIMEOUT */
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/* +2008.01 bj */
|
||||
W5100.writeSnIR(s, SnIR::SEND_OK);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief This function is an application I/F function which is used to receive the data in other then
|
||||
* TCP mode. This function is used to receive UDP, IP_RAW and MAC_RAW mode, and handle the header as well.
|
||||
*
|
||||
* @return This function return received data size for success else -1.
|
||||
*/
|
||||
uint16_t recvfrom(SOCKET s, uint8_t *buf, uint16_t len, uint8_t *addr, uint16_t *port)
|
||||
{
|
||||
uint8_t head[8];
|
||||
uint16_t data_len=0;
|
||||
uint16_t ptr=0;
|
||||
|
||||
if ( len > 0 )
|
||||
{
|
||||
ptr = W5100.readSnRX_RD(s);
|
||||
switch (W5100.readSnMR(s) & 0x07)
|
||||
{
|
||||
case SnMR::UDP :
|
||||
W5100.read_data(s, (uint8_t *)ptr, head, 0x08);
|
||||
ptr += 8;
|
||||
// read peer's IP address, port number.
|
||||
addr[0] = head[0];
|
||||
addr[1] = head[1];
|
||||
addr[2] = head[2];
|
||||
addr[3] = head[3];
|
||||
*port = head[4];
|
||||
*port = (*port << 8) + head[5];
|
||||
data_len = head[6];
|
||||
data_len = (data_len << 8) + head[7];
|
||||
|
||||
W5100.read_data(s, (uint8_t *)ptr, buf, data_len); // data copy.
|
||||
ptr += data_len;
|
||||
|
||||
W5100.writeSnRX_RD(s, ptr);
|
||||
break;
|
||||
|
||||
case SnMR::IPRAW :
|
||||
W5100.read_data(s, (uint8_t *)ptr, head, 0x06);
|
||||
ptr += 6;
|
||||
|
||||
addr[0] = head[0];
|
||||
addr[1] = head[1];
|
||||
addr[2] = head[2];
|
||||
addr[3] = head[3];
|
||||
data_len = head[4];
|
||||
data_len = (data_len << 8) + head[5];
|
||||
|
||||
W5100.read_data(s, (uint8_t *)ptr, buf, data_len); // data copy.
|
||||
ptr += data_len;
|
||||
|
||||
W5100.writeSnRX_RD(s, ptr);
|
||||
break;
|
||||
|
||||
case SnMR::MACRAW:
|
||||
W5100.read_data(s,(uint8_t*)ptr,head,2);
|
||||
ptr+=2;
|
||||
data_len = head[0];
|
||||
data_len = (data_len<<8) + head[1] - 2;
|
||||
|
||||
W5100.read_data(s,(uint8_t*) ptr,buf,data_len);
|
||||
ptr += data_len;
|
||||
W5100.writeSnRX_RD(s, ptr);
|
||||
break;
|
||||
|
||||
default :
|
||||
break;
|
||||
}
|
||||
W5100.execCmdSn(s, Sock_RECV);
|
||||
}
|
||||
return data_len;
|
||||
}
|
||||
|
||||
|
||||
uint16_t igmpsend(SOCKET s, const uint8_t * buf, uint16_t len)
|
||||
{
|
||||
uint8_t status=0;
|
||||
uint16_t ret=0;
|
||||
|
||||
if (len > W5100.SSIZE)
|
||||
ret = W5100.SSIZE; // check size not to exceed MAX size.
|
||||
else
|
||||
ret = len;
|
||||
|
||||
if (ret == 0)
|
||||
return 0;
|
||||
|
||||
W5100.send_data_processing(s, (uint8_t *)buf, ret);
|
||||
W5100.execCmdSn(s, Sock_SEND);
|
||||
|
||||
while ( (W5100.readSnIR(s) & SnIR::SEND_OK) != SnIR::SEND_OK )
|
||||
{
|
||||
status = W5100.readSnSR(s);
|
||||
if (W5100.readSnIR(s) & SnIR::TIMEOUT)
|
||||
{
|
||||
/* in case of igmp, if send fails, then socket closed */
|
||||
/* if you want change, remove this code. */
|
||||
close(s);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
W5100.writeSnIR(s, SnIR::SEND_OK);
|
||||
return ret;
|
||||
}
|
||||
|
||||
uint16_t bufferData(SOCKET s, uint16_t offset, const uint8_t* buf, uint16_t len)
|
||||
{
|
||||
uint16_t ret =0;
|
||||
if (len > W5100.getTXFreeSize(s))
|
||||
{
|
||||
ret = W5100.getTXFreeSize(s); // check size not to exceed MAX size.
|
||||
}
|
||||
else
|
||||
{
|
||||
ret = len;
|
||||
}
|
||||
W5100.send_data_processing_offset(s, offset, buf, ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int startUDP(SOCKET s, uint8_t* addr, uint16_t port)
|
||||
{
|
||||
if
|
||||
(
|
||||
((addr[0] == 0x00) && (addr[1] == 0x00) && (addr[2] == 0x00) && (addr[3] == 0x00)) ||
|
||||
((port == 0x00))
|
||||
)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
W5100.writeSnDIPR(s, addr);
|
||||
W5100.writeSnDPORT(s, port);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
int sendUDP(SOCKET s)
|
||||
{
|
||||
W5100.execCmdSn(s, Sock_SEND);
|
||||
|
||||
/* +2008.01 bj */
|
||||
while ( (W5100.readSnIR(s) & SnIR::SEND_OK) != SnIR::SEND_OK )
|
||||
{
|
||||
if (W5100.readSnIR(s) & SnIR::TIMEOUT)
|
||||
{
|
||||
/* +2008.01 [bj]: clear interrupt */
|
||||
W5100.writeSnIR(s, (SnIR::SEND_OK|SnIR::TIMEOUT));
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/* +2008.01 bj */
|
||||
W5100.writeSnIR(s, SnIR::SEND_OK);
|
||||
|
||||
/* Sent ok */
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,188 @@
|
||||
/*
|
||||
* Copyright (c) 2010 by Cristian Maglie <c.maglie@bug.st>
|
||||
*
|
||||
* This file is free software; you can redistribute it and/or modify
|
||||
* it under the terms of either the GNU General Public License version 2
|
||||
* or the GNU Lesser General Public License version 2.1, both as
|
||||
* published by the Free Software Foundation.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <avr/interrupt.h>
|
||||
|
||||
#include "w5100.h"
|
||||
|
||||
// W5100 controller instance
|
||||
W5100Class W5100;
|
||||
|
||||
#define TX_RX_MAX_BUF_SIZE 2048
|
||||
#define TX_BUF 0x1100
|
||||
#define RX_BUF (TX_BUF + TX_RX_MAX_BUF_SIZE)
|
||||
|
||||
#define TXBUF_BASE 0x4000
|
||||
#define RXBUF_BASE 0x6000
|
||||
|
||||
void W5100Class::init(void)
|
||||
{
|
||||
delay(300);
|
||||
|
||||
SPI.begin();
|
||||
initSS();
|
||||
|
||||
writeMR(1<<RST);
|
||||
writeTMSR(0x55);
|
||||
writeRMSR(0x55);
|
||||
|
||||
for (int i=0; i<MAX_SOCK_NUM; i++) {
|
||||
SBASE[i] = TXBUF_BASE + SSIZE * i;
|
||||
RBASE[i] = RXBUF_BASE + RSIZE * i;
|
||||
}
|
||||
}
|
||||
|
||||
uint16_t W5100Class::getTXFreeSize(SOCKET s)
|
||||
{
|
||||
uint16_t val=0, val1=0;
|
||||
do {
|
||||
val1 = readSnTX_FSR(s);
|
||||
if (val1 != 0)
|
||||
val = readSnTX_FSR(s);
|
||||
}
|
||||
while (val != val1);
|
||||
return val;
|
||||
}
|
||||
|
||||
uint16_t W5100Class::getRXReceivedSize(SOCKET s)
|
||||
{
|
||||
uint16_t val=0,val1=0;
|
||||
do {
|
||||
val1 = readSnRX_RSR(s);
|
||||
if (val1 != 0)
|
||||
val = readSnRX_RSR(s);
|
||||
}
|
||||
while (val != val1);
|
||||
return val;
|
||||
}
|
||||
|
||||
|
||||
void W5100Class::send_data_processing(SOCKET s, const uint8_t *data, uint16_t len)
|
||||
{
|
||||
// This is same as having no offset in a call to send_data_processing_offset
|
||||
send_data_processing_offset(s, 0, data, len);
|
||||
}
|
||||
|
||||
void W5100Class::send_data_processing_offset(SOCKET s, uint16_t data_offset, const uint8_t *data, uint16_t len)
|
||||
{
|
||||
uint16_t ptr = readSnTX_WR(s);
|
||||
ptr += data_offset;
|
||||
uint16_t offset = ptr & SMASK;
|
||||
uint16_t dstAddr = offset + SBASE[s];
|
||||
|
||||
if (offset + len > SSIZE)
|
||||
{
|
||||
// Wrap around circular buffer
|
||||
uint16_t size = SSIZE - offset;
|
||||
write(dstAddr, data, size);
|
||||
write(SBASE[s], data + size, len - size);
|
||||
}
|
||||
else {
|
||||
write(dstAddr, data, len);
|
||||
}
|
||||
|
||||
ptr += len;
|
||||
writeSnTX_WR(s, ptr);
|
||||
}
|
||||
|
||||
|
||||
void W5100Class::recv_data_processing(SOCKET s, uint8_t *data, uint16_t len, uint8_t peek)
|
||||
{
|
||||
uint16_t ptr;
|
||||
ptr = readSnRX_RD(s);
|
||||
read_data(s, (uint8_t *)ptr, data, len);
|
||||
if (!peek)
|
||||
{
|
||||
ptr += len;
|
||||
writeSnRX_RD(s, ptr);
|
||||
}
|
||||
}
|
||||
|
||||
void W5100Class::read_data(SOCKET s, volatile uint8_t *src, volatile uint8_t *dst, uint16_t len)
|
||||
{
|
||||
uint16_t size;
|
||||
uint16_t src_mask;
|
||||
uint16_t src_ptr;
|
||||
|
||||
src_mask = (uint16_t)src & RMASK;
|
||||
src_ptr = RBASE[s] + src_mask;
|
||||
|
||||
if( (src_mask + len) > RSIZE )
|
||||
{
|
||||
size = RSIZE - src_mask;
|
||||
read(src_ptr, (uint8_t *)dst, size);
|
||||
dst += size;
|
||||
read(RBASE[s], (uint8_t *) dst, len - size);
|
||||
}
|
||||
else
|
||||
read(src_ptr, (uint8_t *) dst, len);
|
||||
}
|
||||
|
||||
|
||||
uint8_t W5100Class::write(uint16_t _addr, uint8_t _data)
|
||||
{
|
||||
setSS();
|
||||
SPI.transfer(0xF0);
|
||||
SPI.transfer(_addr >> 8);
|
||||
SPI.transfer(_addr & 0xFF);
|
||||
SPI.transfer(_data);
|
||||
resetSS();
|
||||
return 1;
|
||||
}
|
||||
|
||||
uint16_t W5100Class::write(uint16_t _addr, const uint8_t *_buf, uint16_t _len)
|
||||
{
|
||||
for (uint16_t i=0; i<_len; i++)
|
||||
{
|
||||
setSS();
|
||||
SPI.transfer(0xF0);
|
||||
SPI.transfer(_addr >> 8);
|
||||
SPI.transfer(_addr & 0xFF);
|
||||
_addr++;
|
||||
SPI.transfer(_buf[i]);
|
||||
resetSS();
|
||||
}
|
||||
return _len;
|
||||
}
|
||||
|
||||
uint8_t W5100Class::read(uint16_t _addr)
|
||||
{
|
||||
setSS();
|
||||
SPI.transfer(0x0F);
|
||||
SPI.transfer(_addr >> 8);
|
||||
SPI.transfer(_addr & 0xFF);
|
||||
uint8_t _data = SPI.transfer(0);
|
||||
resetSS();
|
||||
return _data;
|
||||
}
|
||||
|
||||
uint16_t W5100Class::read(uint16_t _addr, uint8_t *_buf, uint16_t _len)
|
||||
{
|
||||
for (uint16_t i=0; i<_len; i++)
|
||||
{
|
||||
setSS();
|
||||
SPI.transfer(0x0F);
|
||||
SPI.transfer(_addr >> 8);
|
||||
SPI.transfer(_addr & 0xFF);
|
||||
_addr++;
|
||||
_buf[i] = SPI.transfer(0);
|
||||
resetSS();
|
||||
}
|
||||
return _len;
|
||||
}
|
||||
|
||||
void W5100Class::execCmdSn(SOCKET s, SockCMD _cmd) {
|
||||
// Send command to socket
|
||||
writeSnCR(s, _cmd);
|
||||
// Wait for command to complete
|
||||
while (readSnCR(s))
|
||||
;
|
||||
}
|
||||
+176
@@ -0,0 +1,176 @@
|
||||
/*
|
||||
* w5100Wrapper.cpp
|
||||
*
|
||||
* Created on: 26 de Mai de 2013
|
||||
* Author: mgf
|
||||
*/
|
||||
|
||||
|
||||
#include "w5100.h"
|
||||
#include "w5100Wrapper.h"
|
||||
|
||||
extern "C"{
|
||||
|
||||
CSnMR * CSnMR_new(){
|
||||
SnMR* s = new SnMR();
|
||||
return (CSnMR*)s;
|
||||
}
|
||||
void CSnMR_delete(const CSnMR* obj){
|
||||
SnMR* s = (SnMR*)obj;
|
||||
delete s;
|
||||
}
|
||||
uint8_t SnMR_CLOSE(){ return SnMR::CLOSE; }
|
||||
uint8_t SnMR_UDP(){ return SnMR::UDP; }
|
||||
uint8_t SnMR_TCP(){ return SnMR::TCP; }
|
||||
uint8_t SnMR_IPRAW(){ return SnMR::IPRAW; }
|
||||
uint8_t SnMR_MACRAW(){ return SnMR::MACRAW; }
|
||||
uint8_t SnMR_PPPOE(){ return SnMR::PPPOE; }
|
||||
uint8_t SnMR_ND(){ return SnMR::ND; }
|
||||
uint8_t SnMR_MULTI(){ return SnMR::MULTI; }
|
||||
|
||||
CSnIR * CSnIR_new(){
|
||||
SnIR* s = new SnIR();
|
||||
return (CSnIR*) s;
|
||||
}
|
||||
void CSnIR_delete(const CSnIR* obj){
|
||||
SnIR* s = (SnIR*)obj;
|
||||
delete s;
|
||||
}
|
||||
uint8_t SnIR_SEND_OK(){ return SnIR::SEND_OK; }
|
||||
uint8_t SnIR_TIMEOUT(){ return SnIR::TIMEOUT; }
|
||||
uint8_t SnIR_RECV(){ return SnIR::RECV; }
|
||||
uint8_t SnIR_DISCON(){ return SnIR::DISCON; }
|
||||
uint8_t SnIR_CON(){ return SnIR::CON; }
|
||||
|
||||
CSnSR * CSnSR_new(){
|
||||
SnSR* s = new SnSR();
|
||||
return (CSnSR*) s;
|
||||
}
|
||||
void CSnSR_delete(const CSnSR* obj){
|
||||
SnSR* s = (SnSR*)obj;
|
||||
delete s;
|
||||
}
|
||||
uint8_t SnSR_CLOSED(){ return SnSR::CLOSED; }
|
||||
uint8_t SnSR_INIT(){ return SnSR::INIT; }
|
||||
uint8_t SnSR_LISTEN(){ return SnSR::LISTEN; }
|
||||
uint8_t SnSR_SYNSENT(){ return SnSR::SYNSENT; }
|
||||
uint8_t SnSR_SYNRECV(){ return SnSR::SYNRECV; }
|
||||
uint8_t SnSR_ESTABLISHED(){ return SnSR::ESTABLISHED; }
|
||||
uint8_t SnSR_FIN_WAIT(){ return SnSR::FIN_WAIT; }
|
||||
uint8_t SnSR_CLOSING(){ return SnSR::CLOSING; }
|
||||
uint8_t SnSR_TIME_WAIT(){ return SnSR::TIME_WAIT; }
|
||||
uint8_t SnSR_CLOSE_WAIT(){ return SnSR::CLOSE_WAIT; }
|
||||
uint8_t SnSR_LAST_ACK(){ return SnSR::LAST_ACK; }
|
||||
uint8_t SnSR_UDP(){ return SnSR::UDP; }
|
||||
uint8_t SnSR_IPRAW(){ return SnSR::IPRAW; }
|
||||
uint8_t SnSR_MACRAW(){ return SnSR::MACRAW; }
|
||||
uint8_t SnSR_PPPOE(){ return SnSR::PPPOE; }
|
||||
|
||||
CIPPROTO * CIPPROTO_new(){
|
||||
IPPROTO* i = new IPPROTO();
|
||||
return (CIPPROTO*) i;
|
||||
}
|
||||
void CIPPROTO_delete(const CIPPROTO* obj){
|
||||
IPPROTO* i = (IPPROTO*) obj;
|
||||
delete i;
|
||||
}
|
||||
uint8_t IPPROTO_IP(){ return IPPROTO::IP; }
|
||||
uint8_t IPPROTO_ICMP(){ return IPPROTO::ICMP; }
|
||||
uint8_t IPPROTO_IGMP(){ return IPPROTO::IGMP; }
|
||||
uint8_t IPPROTO_GGP(){ return IPPROTO::GGP; }
|
||||
uint8_t IPPROTO_TCP(){ return IPPROTO::TCP; }
|
||||
uint8_t IPPROTO_PUP(){ return IPPROTO::PUP; }
|
||||
uint8_t IPPROTO_UDP(){ return IPPROTO::UDP; }
|
||||
uint8_t IPPROTO_IDP(){ return IPPROTO::IDP; }
|
||||
uint8_t IPPROTO_ND(){ return IPPROTO::ND; }
|
||||
uint8_t IPPROTO_RAW(){ return IPPROTO::RAW; }
|
||||
|
||||
CW5100Class * CW5100Class_new(){
|
||||
return (CW5100Class*) &W5100;
|
||||
}
|
||||
|
||||
void init_func(const CW5100Class * obj){
|
||||
W5100Class* w = (W5100Class*) obj;
|
||||
w->init();
|
||||
}
|
||||
|
||||
void read_data_func(const CW5100Class * obj, SOCKET s, volatile uint8_t * src, volatile uint8_t * dst,
|
||||
uint16_t len){
|
||||
W5100Class* w = (W5100Class*) obj;
|
||||
w->read_data(s, src, dst, len);
|
||||
}
|
||||
|
||||
void send_data_processing_func(const CW5100Class * obj, SOCKET s, const uint8_t *data, uint16_t len){
|
||||
W5100Class* w = (W5100Class*) obj;
|
||||
w->send_data_processing(s, data, len);
|
||||
}
|
||||
void send_data_processing_offset_func(const CW5100Class * obj, SOCKET s, uint16_t data_offset,
|
||||
const uint8_t *data, uint16_t len){
|
||||
W5100Class* w = (W5100Class*) obj;
|
||||
w->send_data_processing_offset(s, data_offset, data, len);
|
||||
}
|
||||
//FIXME: Removed defaul value of 0(zero) from the peek argument
|
||||
void recv_data_processing_func(const CW5100Class * obj, SOCKET s, uint8_t *data, uint16_t len,
|
||||
uint8_t peek){
|
||||
W5100Class* w = (W5100Class*) obj;
|
||||
w->recv_data_processing(s, data, len, peek);
|
||||
}
|
||||
void setGatewayIp_func(const CW5100Class * obj, uint8_t *_addr){
|
||||
W5100Class* w = (W5100Class*) obj;
|
||||
w->setGatewayIp(_addr);
|
||||
}
|
||||
void getGatewayIp_func(const CW5100Class * obj, uint8_t *_addr){
|
||||
W5100Class* w = (W5100Class*) obj;
|
||||
w->getGatewayIp(_addr);
|
||||
}
|
||||
|
||||
void setSubnetMask_func(const CW5100Class * obj, uint8_t *_addr){
|
||||
W5100Class* w = (W5100Class*) obj;
|
||||
w->setSubnetMask(_addr);
|
||||
}
|
||||
void getSubnetMask_func(const CW5100Class * obj, uint8_t *_addr){
|
||||
W5100Class* w = (W5100Class*) obj;
|
||||
w->getSubnetMask(_addr);
|
||||
}
|
||||
|
||||
void setMACAddress_func(const CW5100Class * obj, uint8_t * addr){
|
||||
W5100Class* w = (W5100Class*) obj;
|
||||
w->setMACAddress(addr);
|
||||
}
|
||||
void getMACAddress_func(const CW5100Class * obj, uint8_t * addr){
|
||||
W5100Class* w = (W5100Class*) obj;
|
||||
w->getMACAddress(addr);
|
||||
}
|
||||
void setIPAddress_func(const CW5100Class * obj, uint8_t * addr){
|
||||
W5100Class* w = (W5100Class*) obj;
|
||||
w->setIPAddress(addr);
|
||||
}
|
||||
void getIPAddress_func(const CW5100Class * obj, uint8_t * addr){
|
||||
W5100Class* w = (W5100Class*) obj;
|
||||
w->getIPAddress(addr);
|
||||
}
|
||||
void setRetransmissionTime_func(const CW5100Class * obj, uint16_t timeout){
|
||||
W5100Class* w = (W5100Class*) obj;
|
||||
w->setRetransmissionTime(timeout);
|
||||
}
|
||||
void setRetransmissionCount_func(const CW5100Class * obj, uint8_t _retry){
|
||||
W5100Class* w = (W5100Class*) obj;
|
||||
w->setRetransmissionCount(_retry);
|
||||
}
|
||||
|
||||
uint16_t getTXFreeSize_func(const CW5100Class * obj,SOCKET s){
|
||||
W5100Class* w = (W5100Class*) obj;
|
||||
return w->getTXFreeSize(s);
|
||||
}
|
||||
uint16_t getRXReceivedSize_func(const CW5100Class * obj,SOCKET s){
|
||||
W5100Class* w = (W5100Class*) obj;
|
||||
return w->getRXReceivedSize(s);
|
||||
}
|
||||
|
||||
uint8_t readSnSR_func(const CW5100Class* obj, SOCKET s){
|
||||
W5100Class* w = (W5100Class*) obj;
|
||||
return w->readSnSR(s);
|
||||
}
|
||||
|
||||
}//externC
|
||||
|
||||
Reference in New Issue
Block a user