32 Commits

Author SHA1 Message Date
adf6d9c3ee Update w5300.h
fix some bug
2015-09-15 15:03:13 +09:00
241045068d Merge branch 'hotfix/v2.0.3' 2015-08-28 13:55:55 +09:00
0cabf536fd Fixed an issue #9. 2015-08-28 13:54:59 +09:00
d9e2bd1d65 Merge pull request #8 from fetzerch/wizchip_conf
wizchip_conf: Enable configuration without modifying file
2015-08-13 14:45:36 +09:00
fb3fdf47cc wizchip_conf: Enable configuration without modifying file
This encloses all user configuration options with preprocessor
ifndef ... endif guards, so that the configuration can be applied
already from the Makefile (for example with gcc -D_WIZCHIP_=5100 or
when using CMake: add_definitions(-D_WIZCHIP_=5100)).

The advantage of this change is that the ioLibrary can then simply be
included as a git submodule from the official upstream repository into
the own project without having to carry along a patch of
wizchip_conf.h.
2015-07-24 10:24:47 +02:00
942164d421 V3.0.1
Bug fixed : In W5100, CS control problem to read/write buffer with SPI.
2015-07-15 16:22:02 +09:00
1e940bbf14 Version V3.0
Add to W5300
2015-06-04 20:41:00 +09:00
bd4a3a8d55 Merge branch 'hotfix/v2.0.2' 2015-04-24 13:54:12 +09:00
989f08ab2d Removed unnessary code. 2015-04-24 13:52:22 +09:00
cd0b8f5ba9 getSIMR() & getSIMR() in w5200.h
In w5200.h,
Remove  the Mask value 0xA0 in getSIMR() & setSIMR()
Add to the Mask value 0xA0 in getIR() & setIR()
2015-04-17 13:33:42 +09:00
8508482bc4 Merge branch 'hotfix/opimization_FTP_client' 2015-04-15 17:29:54 +09:00
c2b6716e43 optimization FTP client 2015-04-15 17:29:42 +09:00
580241080d Merge branch 'hotfix/fix_compile_error' 2015-04-15 16:40:19 +09:00
c29dc21040 fix compile error 2015-04-15 16:39:46 +09:00
b840aa13a2 Merge pull request #6 from Wiznet/feature/FTP_Client_optimization
FTP Client optimization
2015-04-14 16:55:41 +09:00
b6b808b306 FTP Client optimization 2015-04-14 15:26:20 +09:00
924ceea741 Merge pull request #5 from Wiznet/develop
FTP Client optimization
2015-04-14 11:21:19 +09:00
86726229f0 FTP Client optimization 2015-04-14 11:05:02 +09:00
2857b805d9 Sync the code & doxygen document! 2015-04-13 09:42:44 +09:00
065b096c92 Modify setIMR() & getIMR() and setSIMR() & getSIMR()
In order to integrate W5200 with ioLibrary
setIMR() & getIMR(): Set/Get a value to IMR2 intead of _IMR_.
setIMR2() & getIMR2() (setSIMR() & getSIMR()) : Set/Get a value to _IMR_
instead of IMR2
2015-04-13 09:28:29 +09:00
f36dd4352a Fixed the Type casting Error in send() ,socket.c 2015-04-09 14:56:48 +09:00
19ae79ee07 Added W5100, W5200 drivers. 2015-04-06 13:27:41 +09:00
fc3430b71a Add to W5200 & W5100
Integrate ioLibrary with WIZnet chips as W5500,W5200, W5100 and so on.
2015-04-06 10:58:45 +09:00
3508acad10 FTP Client variable, function name modify 2015-04-06 10:55:39 +09:00
413ad6fc84 FTP Client 2015-04-06 09:47:06 +09:00
1f37c8efa5 Update README.md 2015-03-30 14:38:07 +09:00
999cb97cc8 Update README.md 2015-03-30 14:14:50 +09:00
455ab71314 Fixed a compile error. 2015-03-30 13:20:17 +09:00
f0dcff0671 Fixed a casting issue of variable. 2015-03-30 13:12:10 +09:00
460b04a42b Added a SNTP protocol. 2015-03-18 13:01:54 +09:00
97857ce12c Added a protocol of TFTP. 2015-02-23 13:12:34 +09:00
461b1b16b1 Change ftpd to support a ftp program of Windows. 2015-02-23 10:31:25 +09:00
26 changed files with 10175 additions and 220 deletions

BIN
Ethernet/Socket_APIs.chm Normal file

Binary file not shown.

Binary file not shown.

386
Ethernet/W5100/w5100.c Normal file
View File

@ -0,0 +1,386 @@
//*****************************************************************************
//
//! \file w5100.c
//! \brief W5100 HAL Interface.
//! \version 1.0.0
//! \date 2013/10/21
//! \par Revision history
//! <2013/10/21> 1st Release
//! \author MidnightCow
//!
//! Copyright (c) 2013, WIZnet Co., LTD.
//! All rights reserved.
//!
//! Redistribution and use in source and binary forms, with or without
//! modification, are permitted provided that the following conditions
//! are met:
//!
//! * Redistributions of source code must retain the above copyright
//! notice, this list of conditions and the following disclaimer.
//! * Redistributions in binary form must reproduce the above copyright
//! notice, this list of conditions and the following disclaimer in the
//! documentation and/or other materials provided with the distribution.
//! * Neither the name of the <ORGANIZATION> nor the names of its
//! contributors may be used to endorse or promote products derived
//! from this software without specific prior written permission.
//!
//! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
//! AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
//! IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
//! ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
//! LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
//! CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
//! SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
//! INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
//! CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
//! ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
//! THE POSSIBILITY OF SUCH DAMAGE.
//
//*****************************************************************************
#include "w5100.h"
#if (_WIZCHIP_ == 5100)
/**
@brief This function writes the data into W5200 registers.
*/
void WIZCHIP_WRITE(uint32_t AddrSel, uint8_t wb )
{
WIZCHIP_CRITICAL_ENTER();
WIZCHIP.CS._select();
#if( (_WIZCHIP_IO_MODE_ & _WIZCHIP_IO_MODE_SPI_))
WIZCHIP.IF.SPI._write_byte(0xF0);
WIZCHIP.IF.SPI._write_byte((AddrSel & 0xFF00) >> 8);
WIZCHIP.IF.SPI._write_byte((AddrSel & 0x00FF) >> 0);
WIZCHIP.IF.SPI._write_byte(wb); // Data write (write 1byte data)
#elif ( (_WIZCHIP_IO_MODE_ == _WIZCHIP_IO_MODE_BUS_DIR_) )
//M20150601 : Rename the function for integrating with ioLibrary
//WIZCHIP.IF.BUS._write_byte(AddrSel,wb);
WIZCHIP.IF.BUS._write_data(AddrSel,wb);
#elif ( (_WIZCHIP_IO_MODE_ == _WIZCHIP_IO_MODE_BUS_INDIR_) )
//add indirect bus
//M20150601 : Rename the function for integrating with ioLibrary
//WIZCHIP.IF.BUS._write_byte(IDM_AR0,(AddrSel & 0xFF00) >> 8);
//WIZCHIP.IF.BUS._write_byte(IDM_AR1,(AddrSel & 0x00FF));
//WIZCHIP.IF.BUS._write_byte(IDM_DR,wb);
WIZCHIP.IF.BUS._write_data(IDM_AR0,(AddrSel & 0xFF00) >> 8);
WIZCHIP.IF.BUS._write_data(IDM_AR1,(AddrSel & 0x00FF));
WIZCHIP.IF.BUS._write_data(IDM_DR,wb);
#else
#error "Unknown _WIZCHIP_IO_MODE_ in W5100. !!!"
#endif
WIZCHIP.CS._deselect();
WIZCHIP_CRITICAL_EXIT();
}
/**
@brief This function reads the value from W5200 registers.
*/
uint8_t WIZCHIP_READ(uint32_t AddrSel)
{
uint8_t ret;
WIZCHIP_CRITICAL_ENTER();
WIZCHIP.CS._select();
#if( (_WIZCHIP_IO_MODE_ & _WIZCHIP_IO_MODE_SPI_))
WIZCHIP.IF.SPI._write_byte(0x0F);
WIZCHIP.IF.SPI._write_byte((AddrSel & 0xFF00) >> 8);
WIZCHIP.IF.SPI._write_byte((AddrSel & 0x00FF) >> 0);
ret = WIZCHIP.IF.SPI._read_byte();
#elif ( (_WIZCHIP_IO_MODE_ == _WIZCHIP_IO_MODE_BUS_DIR_) )
//M20150601 : Rename the function for integrating with ioLibrary
//ret = WIZCHIP.IF.BUS._read_byte(AddrSel);
ret = WIZCHIP.IF.BUS._read_data(AddrSel);
#elif ( (_WIZCHIP_IO_MODE_ == _WIZCHIP_IO_MODE_BUS_INDIR_) )
//add indirect bus
//M20150601 : Rename the function for integrating with ioLibrary
//WIZCHIP.IF.BUS._write_byte(IDM_AR0,(AddrSel & 0xFF00) >> 8);
//WIZCHIP.IF.BUS._write_byte(IDM_AR1,(AddrSel & 0x00FF));
//ret = WIZCHIP.IF.BUS._read_byte(IDM_DR);
WIZCHIP.IF.BUS._write_data(IDM_AR0,(AddrSel & 0xFF00) >> 8);
WIZCHIP.IF.BUS._write_data(IDM_AR1,(AddrSel & 0x00FF));
ret = WIZCHIP.IF.BUS._read_data(IDM_DR);
#else
#error "Unknown _WIZCHIP_IO_MODE_ in W5100. !!!"
#endif
WIZCHIP.CS._deselect();
WIZCHIP_CRITICAL_EXIT();
return ret;
}
/**
@brief This function writes into W5200 memory(Buffer)
*/
void WIZCHIP_WRITE_BUF(uint32_t AddrSel, uint8_t* pBuf, uint16_t len)
{
uint16_t i = 0;
WIZCHIP_CRITICAL_ENTER();
WIZCHIP.CS._select(); //M20150601 : Moved here.
#if( (_WIZCHIP_IO_MODE_ & _WIZCHIP_IO_MODE_SPI_))
for(i = 0; i < len; i++)
{
//M20160715 : Depricated "M20150601 : Remove _select() to top-side"
// CS should be controlled every SPI frames
WIZCHIP.CS._select();
WIZCHIP.IF.SPI._write_byte(0xF0);
WIZCHIP.IF.SPI._write_byte((((uint16_t)(AddrSel+i)) & 0xFF00) >> 8);
WIZCHIP.IF.SPI._write_byte((((uint16_t)(AddrSel+i)) & 0x00FF) >> 0);
WIZCHIP.IF.SPI._write_byte(pBuf[i]); // Data write (write 1byte data)
//M20160715 : Depricated "M20150601 : Remove _select() to top-side"
WIZCHIP.CS._deselect();
}
#elif ( (_WIZCHIP_IO_MODE_ == _WIZCHIP_IO_MODE_BUS_DIR_) )
for(i = 0; i < len; i++)
//M20150601 : Rename the function for integrating with ioLibrary
// WIZCHIP.IF.BUS._write_byte(AddrSel+i,pBuf[i]);
WIZCHIP.IF.BUS._write_data(AddrSel+i,pBuf[i]);
#elif ( (_WIZCHIP_IO_MODE_ == _WIZCHIP_IO_MODE_BUS_INDIR_) )
//M20150601 : Rename the function for integrating with ioLibrary
/*
WIZCHIP_WRITE(MR,WIZCHIP_READ(MR) | MR_AI);
WIZCHIP.IF.BUS._write_byte(IDM_AR0,(AddrSel & 0xFF00) >> 8);
WIZCHIP.IF.BUS._write_byte(IDM_AR1,(AddrSel & 0x00FF));
for(i = 0 ; i < len; i++)
WIZCHIP.IF.BUS._write_byte(IDM_DR,pBuf[i]);
WIZCHIP_WRITE(MR, WIZCHIP_READ(MR) & ~MR_AI);
*/
setMR(getMR()|MR_AI);
WIZCHIP.IF.BUS._write_data(IDM_AR0,(AddrSel & 0xFF00) >> 8);
WIZCHIP.IF.BUS._write_data(IDM_AR1,(AddrSel & 0x00FF));
for(i = 0 ; i < len; i++)
WIZCHIP.IF.BUS._write_data(IDM_DR,pBuf[i]);
setMR(getMR() & ~MR_AI);
#else
#error "Unknown _WIZCHIP_IO_MODE_ in W5100. !!!!"
#endif
WIZCHIP.CS._deselect(); //M20150601 : Moved here.
WIZCHIP_CRITICAL_EXIT();
}
/**
@brief This function reads into W5200 memory(Buffer)
*/
void WIZCHIP_READ_BUF (uint32_t AddrSel, uint8_t* pBuf, uint16_t len)
{
uint16_t i = 0;
WIZCHIP_CRITICAL_ENTER();
WIZCHIP.CS._select(); //M20150601 : Moved here.
#if( (_WIZCHIP_IO_MODE_ & _WIZCHIP_IO_MODE_SPI_))
for(i = 0; i < len; i++)
{
//M20160715 : Depricated "M20150601 : Remove _select() to top-side"
// CS should be controlled every SPI frames
WIZCHIP.CS._select();
WIZCHIP.IF.SPI._write_byte(0x0F);
WIZCHIP.IF.SPI._write_byte((uint16_t)((AddrSel+i) & 0xFF00) >> 8);
WIZCHIP.IF.SPI._write_byte((uint16_t)((AddrSel+i) & 0x00FF) >> 0);
pBuf[i] = WIZCHIP.IF.SPI._read_byte();
//M20160715 : Depricated "M20150601 : Remove _select() to top-side"
WIZCHIP.CS._deselect();
}
#elif ( (_WIZCHIP_IO_MODE_ == _WIZCHIP_IO_MODE_BUS_DIR_) )
for(i = 0 ; i < len; i++)
//M20150601 : Rename the function for integrating with ioLibrary
// pBuf[i] = WIZCHIP.IF.BUS._read_byte(AddrSel+i);
pBuf[i] = WIZCHIP.IF.BUS._read_data(AddrSel+i);
#elif ( (_WIZCHIP_IO_MODE_ == _WIZCHIP_IO_MODE_BUS_INDIR_) )
//M20150601 : Rename the function for integrating with ioLibrary
/*
WIZCHIP_WRITE(MR, WIZCHIP_READ(MR) | MR_AI);
WIZCHIP.IF.BUS._write_byte(IDM_AR0,(AddrSel & 0xFF00) >> 8);
WIZCHIP.IF.BUS._write_byte(IDM_AR1,(AddrSel & 0x00FF));
for(i = 0 ; i < len; i++)
pBuf[i] = WIZCHIP.IF.BUS._read_byte(IDM_DR);
WIZCHIP_WRITE(MR, WIZCHIP_READ(MR) & ~MR_AI);
*/
setMR(getMR() | MR_AI);
WIZCHIP.IF.BUS._write_data(IDM_AR0,(AddrSel & 0xFF00) >> 8);
WIZCHIP.IF.BUS._write_data(IDM_AR1,(AddrSel & 0x00FF));
for(i = 0 ; i < len; i++)
pBuf[i] = WIZCHIP.IF.BUS._read_data(IDM_DR);
setMR(getMR() & ~MR_AI);
#else
#error "Unknown _WIZCHIP_IO_MODE_ in W5100. !!!!"
#endif
WIZCHIP.CS._deselect(); //M20150601 : Moved Here.
WIZCHIP_CRITICAL_EXIT();
}
///////////////////////////////////
// Socket N regsiter IO function //
///////////////////////////////////
uint16_t getSn_TX_FSR(uint8_t sn)
{
uint16_t val=0,val1=0;
do
{
val1 = WIZCHIP_READ(Sn_TX_FSR(sn));
val1 = (val1 << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(Sn_TX_FSR(sn),1));
if (val1 != 0)
{
val = WIZCHIP_READ(Sn_TX_FSR(sn));
val = (val << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(Sn_TX_FSR(sn),1));
}
}while (val != val1);
return val;
}
uint16_t getSn_RX_RSR(uint8_t sn)
{
uint16_t val=0,val1=0;
do
{
val1 = WIZCHIP_READ(Sn_RX_RSR(sn));
val1 = (val1 << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(Sn_RX_RSR(sn),1));
if (val1 != 0)
{
val = WIZCHIP_READ(Sn_RX_RSR(sn));
val = (val << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(Sn_RX_RSR(sn),1));
}
}while (val != val1);
return val;
}
/////////////////////////////////////
// Sn_TXBUF & Sn_RXBUF IO function //
/////////////////////////////////////
uint32_t getSn_RxBASE(uint8_t sn)
{
int8_t i;
#if ( _WIZCHIP_IO_MODE_ == _WIZCHIP_IO_MODE_BUS_DIR_)
uint32_t rxbase = _W5100_IO_BASE_ + _WIZCHIP_IO_RXBUF_;
#else
uint32_t rxbase = _WIZCHIP_IO_RXBUF_;
#endif
for(i = 0; i < sn; i++)
rxbase += getSn_RxMAX(i);
return rxbase;
}
uint32_t getSn_TxBASE(uint8_t sn)
{
int8_t i;
#if ( _WIZCHIP_IO_MODE_ == _WIZCHIP_IO_MODE_BUS_DIR_)
uint32_t txbase = _W5100_IO_BASE_ + _WIZCHIP_IO_TXBUF_;
#else
uint32_t txbase = _WIZCHIP_IO_TXBUF_;
#endif
for(i = 0; i < sn; i++)
txbase += getSn_TxMAX(i);
return txbase;
}
/**
@brief This function is being called by send() and sendto() function also. for copy the data form application buffer to Transmite buffer of the chip.
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.
And this function is being used for copy the data form application buffer to Transmite
buffer of the chip. It calculate the actual physical address where one has to write
the data in transmite buffer. Here also take care of the condition while it exceed
the Tx memory uper-bound of socket.
*/
void wiz_send_data(uint8_t sn, uint8_t *wizdata, uint16_t len)
{
uint16_t ptr;
uint16_t size;
uint16_t dst_mask;
uint16_t dst_ptr;
ptr = getSn_TX_WR(sn);
dst_mask = ptr & getSn_TxMASK(sn);
dst_ptr = getSn_TxBASE(sn) + dst_mask;
if (dst_mask + len > getSn_TxMAX(sn))
{
size = getSn_TxMAX(sn) - dst_mask;
WIZCHIP_WRITE_BUF(dst_ptr, wizdata, size);
wizdata += size;
size = len - size;
dst_ptr = getSn_TxBASE(sn);
WIZCHIP_WRITE_BUF(dst_ptr, wizdata, size);
}
else
{
WIZCHIP_WRITE_BUF(dst_ptr, wizdata, len);
}
ptr += len;
setSn_TX_WR(sn, ptr);
}
/**
@brief This function is being called by recv() also. This function is being used for copy the data form Receive buffer of the chip to application buffer.
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.
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 wiz_recv_data(uint8_t sn, uint8_t *wizdata, uint16_t len)
{
uint16_t ptr;
uint16_t size;
uint16_t src_mask;
uint16_t src_ptr;
ptr = getSn_RX_RD(sn);
src_mask = (uint32_t)ptr & getSn_RxMASK(sn);
src_ptr = (getSn_RxBASE(sn) + src_mask);
if( (src_mask + len) > getSn_RxMAX(sn) )
{
size = getSn_RxMAX(sn) - src_mask;
WIZCHIP_READ_BUF((uint32_t)src_ptr, (uint8_t*)wizdata, size);
wizdata += size;
size = len - size;
src_ptr = getSn_RxBASE(sn);
WIZCHIP_READ_BUF(src_ptr, (uint8_t*)wizdata, size);
}
else
{
WIZCHIP_READ_BUF(src_ptr, (uint8_t*)wizdata, len);
}
ptr += len;
setSn_RX_RD(sn, ptr);
}
void wiz_recv_ignore(uint8_t sn, uint16_t len)
{
uint16_t ptr;
ptr = getSn_RX_RD(sn);
ptr += len;
setSn_RX_RD(sn,ptr);
}
#endif

1856
Ethernet/W5100/w5100.h Normal file

File diff suppressed because it is too large Load Diff

353
Ethernet/W5200/w5200.c Normal file
View File

@ -0,0 +1,353 @@
//*****************************************************************************
//
//! \file w5200.c
//! \brief W5200 HAL Interface.
//! \version 1.0.0
//! \date 2013/10/21
//! \par Revision history
//! <2013/10/21> 1st Release
//! \author MidnightCow
//!
//! Copyright (c) 2013, WIZnet Co., LTD.
//! All rights reserved.
//!
//! Redistribution and use in source and binary forms, with or without
//! modification, are permitted provided that the following conditions
//! are met:
//!
//! * Redistributions of source code must retain the above copyright
//! notice, this list of conditions and the following disclaimer.
//! * Redistributions in binary form must reproduce the above copyright
//! notice, this list of conditions and the following disclaimer in the
//! documentation and/or other materials provided with the distribution.
//! * Neither the name of the <ORGANIZATION> nor the names of its
//! contributors may be used to endorse or promote products derived
//! from this software without specific prior written permission.
//!
//! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
//! AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
//! IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
//! ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
//! LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
//! CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
//! SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
//! INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
//! CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
//! ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
//! THE POSSIBILITY OF SUCH DAMAGE.
//
//*****************************************************************************
#include "w5200.h"
#if (_WIZCHIP_ == 5200)
/**
@brief This function writes the data into W5200 registers.
*/
void WIZCHIP_WRITE(uint32_t AddrSel, uint8_t wb )
{
WIZCHIP_CRITICAL_ENTER();
WIZCHIP.CS._select();
#if( (_WIZCHIP_IO_MODE_ & _WIZCHIP_IO_MODE_SPI_))
WIZCHIP.IF.SPI._write_byte((AddrSel & 0x0000FF00) >> 8);
WIZCHIP.IF.SPI._write_byte((AddrSel & 0x000000FF) >> 0);
WIZCHIP.IF.SPI._write_byte(_W5200_SPI_WRITE_); // Data write command and Write data length upper
WIZCHIP.IF.SPI._write_byte(0x01); // Write data length lower
WIZCHIP.IF.SPI._write_byte(wb); // Data write (write 1byte data)
#elif ( (_WIZCHIP_IO_MODE_ & _WIZCHIP_IO_MODE_BUS_) )
//add indirect bus
//M20150601 : Rename the function for integrating with W5300
//WIZCHIP.IF.BUS._write_byte(IDM_AR0,(AddrSel & 0x0000FF00) >> 8);
//WIZCHIP.IF.BUS._write_byte(IDM_AR1,(AddrSel & 0x000000FF));
//WIZCHIP.IF.BUS._write_byte(IDM_DR,wb);
WIZCHIP.IF.BUS._write_data(IDM_AR0,(AddrSel & 0x0000FF00) >> 8);
WIZCHIP.IF.BUS._write_data(IDM_AR1,(AddrSel & 0x000000FF));
WIZCHIP.IF.BUS._write_data(IDM_DR,wb);
#else
#error "Unknown _WIZCHIP_IO_MODE_ in W5200. !!!"
#endif
WIZCHIP.CS._deselect();
WIZCHIP_CRITICAL_EXIT();
}
/**
@brief This function reads the value from W5200 registers.
*/
uint8_t WIZCHIP_READ(uint32_t AddrSel)
{
uint8_t ret;
WIZCHIP_CRITICAL_ENTER();
WIZCHIP.CS._select();
#if( (_WIZCHIP_IO_MODE_ & _WIZCHIP_IO_MODE_SPI_))
WIZCHIP.IF.SPI._write_byte((AddrSel & 0x0000FF00) >> 8);
WIZCHIP.IF.SPI._write_byte((AddrSel & 0x000000FF) >> 0);
WIZCHIP.IF.SPI._write_byte(_W5200_SPI_READ_); // Read data length upper
WIZCHIP.IF.SPI._write_byte(0x01); // Data length lower
ret = WIZCHIP.IF.SPI._read_byte();
#elif ( (_WIZCHIP_IO_MODE_ & _WIZCHIP_IO_MODE_BUS_) )
//add indirect bus
//M20150601 : Rename the function for integrating with W5300
//WIZCHIP.IF.BUS._write_byte(IDM_AR0,(AddrSel & 0x0000FF00) >> 8);
//WIZCHIP.IF.BUS._write_byte(IDM_AR1,(AddrSel & 0x000000FF));
//ret = WIZCHIP.IF.BUS._read_byte(IDM_DR);
WIZCHIP.IF.BUS._write_data(IDM_AR0,(AddrSel & 0x0000FF00) >> 8);
WIZCHIP.IF.BUS._write_data(IDM_AR1,(AddrSel & 0x000000FF));
ret = WIZCHIP.IF.BUS._read_data(IDM_DR);
#else
#error "Unknown _WIZCHIP_IO_MODE_ in W5200. !!!"
#endif
WIZCHIP.CS._deselect();
WIZCHIP_CRITICAL_EXIT();
return ret;
}
/**
@brief This function writes into W5200 memory(Buffer)
*/
void WIZCHIP_WRITE_BUF(uint32_t AddrSel, uint8_t* pBuf, uint16_t len)
{
uint16_t i = 0;
WIZCHIP_CRITICAL_ENTER();
WIZCHIP.CS._select();
#if( (_WIZCHIP_IO_MODE_ & _WIZCHIP_IO_MODE_SPI_))
WIZCHIP.IF.SPI._write_byte((AddrSel & 0x0000FF00) >> 8);
WIZCHIP.IF.SPI._write_byte((AddrSel & 0x000000FF) >> 0);
WIZCHIP.IF.SPI._write_byte(_W5200_SPI_WRITE_ | ((len & 0x7F00) >> 8)); // Write data op code and length upper
WIZCHIP.IF.SPI._write_byte((len & 0x00FF) >> 0); // length lower
for(i = 0; i < len; i++)
WIZCHIP.IF.SPI._write_byte(pBuf[i]);
#elif ( (_WIZCHIP_IO_MODE_ & _WIZCHIP_IO_MODE_BUS_) )
//M20150601 : Rename the function for integrating with W5300
/*
WIZCHIP_WRITE(MR,WIZCHIP_READ(MR) | MR_AI);
WIZCHIP.IF.BUS._write_byte(IDM_AR0,(AddrSel & 0x0000FF00) >> 8);
WIZCHIP.IF.BUS._write_byte(IDM_AR1,(AddrSel & 0x000000FF));
for(i = 0 ; i < len; i++)
WIZCHIP.IF.BUS._write_byte(IDM_DR,pBuf[i]);
WIZCHIP_WRITE(MR, WIZCHIP_READ(MR) & ~MR_AI);
*/
setMR(getMR() | MR_AI);
WIZCHIP.IF.BUS._write_data(IDM_AR0,(AddrSel & 0x0000FF00) >> 8);
WIZCHIP.IF.BUS._write_data(IDM_AR1,(AddrSel & 0x000000FF));
for(i = 0 ; i < len; i++)
WIZCHIP.IF.BUS._write_data(IDM_DR,pBuf[i]);
WIZCHIP_WRITE(MR, WIZCHIP_READ(MR) & ~MR_AI);
#else
#error "Unknown _WIZCHIP_IO_MODE_ in W5200. !!!!"
#endif
WIZCHIP.CS._deselect();
WIZCHIP_CRITICAL_EXIT();
}
/**
@brief This function reads into W5200 memory(Buffer)
*/
void WIZCHIP_READ_BUF (uint32_t AddrSel, uint8_t* pBuf, uint16_t len)
{
uint16_t i = 0;
WIZCHIP_CRITICAL_ENTER();
WIZCHIP.CS._select();
#if( (_WIZCHIP_IO_MODE_ & _WIZCHIP_IO_MODE_SPI_))
WIZCHIP.IF.SPI._write_byte((AddrSel & 0x0000FF00) >> 8);
WIZCHIP.IF.SPI._write_byte((AddrSel & 0x000000FF) >> 0);
WIZCHIP.IF.SPI._write_byte( _W5200_SPI_READ_ | ((len & 0x7F00) >> 8)); // Write data op code and length upper
WIZCHIP.IF.SPI._write_byte((len & 0x00FF) >> 0); // length lower
for(i = 0; i < len; i++)
pBuf[i] = WIZCHIP.IF.SPI._read_byte();
#elif ( (_WIZCHIP_IO_MODE_ & _WIZCHIP_IO_MODE_BUS_) )
//M20150601 : Rename the function for integrating with W5300
/*
WIZCHIP_WRITE(MR, WIZCHIP_READ(MR) | MR_AI);
WIZCHIP.IF.BUS._write_byte(IDM_AR0,(AddrSel & 0x0000FF00) >> 8);
WIZCHIP.IF.BUS._write_byte(IDM_AR1,(AddrSel & 0x000000FF));
for(i = 0 ; i < len; i++)
pBuf[i] = WIZCHIP.IF.BUS._read_byte(IDM_DR);
WIZCHIP_WRITE(MR, WIZCHIP_READ(MR) & ~MR_AI);
*/
setMR(getMR() | MR_AI);
WIZCHIP.IF.BUS._write_data(IDM_AR0,(AddrSel & 0x0000FF00) >> 8);
WIZCHIP.IF.BUS._write_data(IDM_AR1,(AddrSel & 0x000000FF));
for(i = 0 ; i < len; i++)
pBuf[i] = WIZCHIP.IF.BUS._read_data(IDM_DR);
setMR(getMR() & ~MR_AI);
#else
#error "Unknown _WIZCHIP_IO_MODE_ in W5200. !!!!"
#endif
WIZCHIP.CS._deselect();
WIZCHIP_CRITICAL_EXIT();
}
///////////////////////////////////
// Socket N regsiter IO function //
///////////////////////////////////
uint16_t getSn_TX_FSR(uint8_t sn)
{
uint16_t val=0,val1=0;
do
{
val1 = WIZCHIP_READ(Sn_TX_FSR(sn));
val1 = (val1 << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(Sn_TX_FSR(sn),1));
if (val1 != 0)
{
val = WIZCHIP_READ(Sn_TX_FSR(sn));
val = (val << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(Sn_TX_FSR(sn),1));
}
}while (val != val1);
return val;
}
uint16_t getSn_RX_RSR(uint8_t sn)
{
uint16_t val=0,val1=0;
do
{
val1 = WIZCHIP_READ(Sn_RX_RSR(sn));
val1 = (val1 << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(Sn_RX_RSR(sn),1));
if (val1 != 0)
{
val = WIZCHIP_READ(Sn_RX_RSR(sn));
val = (val << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(Sn_RX_RSR(sn),1));
}
}while (val != val1);
return val;
}
/////////////////////////////////////
// Sn_TXBUF & Sn_RXBUF IO function //
/////////////////////////////////////
uint16_t getSn_RxBASE(uint8_t sn)
{
int8_t i;
uint16_t rxbase = _WIZCHIP_IO_RXBUF_;
for(i = 0; i < sn; i++)
rxbase += getSn_RxMAX(i);
return rxbase;
}
uint16_t getSn_TxBASE(uint8_t sn)
{
int8_t i;
uint16_t txbase = _WIZCHIP_IO_TXBUF_;
for(i = 0; i < sn; i++)
txbase += getSn_TxMAX(i);
return txbase;
}
/**
@brief This function is being called by send() and sendto() function also. for copy the data form application buffer to Transmite buffer of the chip.
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.
And this function is being used for copy the data form application buffer to Transmite
buffer of the chip. It calculate the actual physical address where one has to write
the data in transmite buffer. Here also take care of the condition while it exceed
the Tx memory uper-bound of socket.
*/
void wiz_send_data(uint8_t sn, uint8_t *wizdata, uint16_t len)
{
uint16_t ptr;
uint16_t size;
uint16_t dst_mask;
uint8_t * dst_ptr;
ptr = getSn_TX_WR(sn);
dst_mask = (uint32_t)ptr & getSn_TxMASK(sn);
dst_ptr = (uint8_t*)((uint32_t)getSn_TxBASE(sn) + dst_mask);
if (dst_mask + len > getSn_TxMAX(sn))
{
size = getSn_TxMAX(sn) - dst_mask;
WIZCHIP_WRITE_BUF((uint32_t)dst_ptr, wizdata, size);
wizdata += size;
size = len - size;
dst_ptr = (uint8_t*)((uint32_t)getSn_TxBASE(sn));
WIZCHIP_WRITE_BUF((uint32_t)dst_ptr, wizdata, size);
}
else
{
WIZCHIP_WRITE_BUF((uint32_t)dst_ptr, wizdata, len);
}
ptr += len;
setSn_TX_WR(sn, ptr);
}
/**
@brief This function is being called by recv() also. This function is being used for copy the data form Receive buffer of the chip to application buffer.
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.
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 wiz_recv_data(uint8_t sn, uint8_t *wizdata, uint16_t len)
{
uint16_t ptr;
uint16_t size;
uint16_t src_mask;
uint8_t * src_ptr;
ptr = getSn_RX_RD(sn);
src_mask = (uint32_t)ptr & getSn_RxMASK(sn);
src_ptr = (uint8_t *)((uint32_t)getSn_RxBASE(sn) + src_mask);
if( (src_mask + len) > getSn_RxMAX(sn) )
{
size = getSn_RxMAX(sn) - src_mask;
WIZCHIP_READ_BUF((uint32_t)src_ptr, (uint8_t*)wizdata, size);
wizdata += size;
size = len - size;
src_ptr = (uint8_t*)((uint32_t)getSn_RxBASE(sn));
WIZCHIP_READ_BUF((uint32_t)src_ptr, (uint8_t*)wizdata, size);
}
else
{
WIZCHIP_READ_BUF((uint32_t)src_ptr, (uint8_t*)wizdata, len);
}
ptr += len;
setSn_RX_RD(sn, ptr);
}
void wiz_recv_ignore(uint8_t sn, uint16_t len)
{
uint16_t ptr;
ptr = getSn_RX_RD(sn);
ptr += len;
setSn_RX_RD(sn,ptr);
}
#endif

2101
Ethernet/W5200/w5200.h Normal file

File diff suppressed because it is too large Load Diff

225
Ethernet/W5300/w5300.c Normal file
View File

@ -0,0 +1,225 @@
//*****************************************************************************
//
//! \file w5300.h
//! \brief W5300 HAL implement File.
//! \version 1.0.0
//! \date 2015/05/01
//! \par Revision history
//! <2015/05/01> 1st Released for integrating with ioLibrary
//! Download the latest version directly from GitHub. Please visit the our GitHub repository for ioLibrary.
//! >> https://github.com/Wiznet/ioLibrary_Driver
//! \author MidnightCow
//! \copyright
//!
//! Copyright (c) 2015, WIZnet Co., LTD.
//! All rights reserved.
//!
//! Redistribution and use in source and binary forms, with or without
//! modification, are permitted provided that the following conditions
//! are met:
//!
//! * Redistributions of source code must retain the above copyright
//! notice, this list of conditions and the following disclaimer.
//! * Redistributions in binary form must reproduce the above copyright
//! notice, this list of conditions and the following disclaimer in the
//! documentation and/or other materials provided with the distribution.
//! * Neither the name of the <ORGANIZATION> nor the names of its
//! contributors may be used to endorse or promote products derived
//! from this software without specific prior written permission.
//!
//! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
//! AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
//! IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
//! ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
//! LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
//! CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
//! SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
//! INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
//! CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
//! ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
//! THE POSSIBILITY OF SUCH DAMAGE.
//
//*****************************************************************************
#include <stdint.h>
#include "wizchip_conf.h"
#if _WIZCHIP_ == 5300
extern uint8_t sock_remained_byte[_WIZCHIP_SOCK_NUM_];
extern uint8_t sock_pack_info[_WIZCHIP_SOCK_NUM_];
/***********************
* Basic I/O Function *
***********************/
void WIZCHIP_WRITE(uint32_t AddrSel, uint16_t wb )
{
WIZCHIP_CRITICAL_ENTER();
WIZCHIP.CS._select();
#if ( (_WIZCHIP_IO_MODE_ == _WIZCHIP_IO_MODE_BUS_DIR_) )
#if(_WIZCHIP_IO_BUS_WIDTH_ == 8)
WIZCHIP.IF.BUS._write_data(AddrSel, (uint8_t)(wb>>8));
WIZCHIP.IF.BUS._write_data(WIZCHIP_OFFSET_INC(AddrSel,1),(uint8_t)wb);
#elif(_WIZCHIP_IO_BUS_WIDTH_ == 16)
WIZCHIP.IF.BUS._write_data(AddrSel, wb);
#else
#error "Abnoraml _WIZCHIP_IO_BUS_WIDTH_. Should be 8 or 16"
#endif
#elif ( (_WIZCHIP_IO_MODE_ == _WIZCHIP_IO_MODE_BUS_INDIR_) )
#if(_WIZCHIP_IO_BUS_WIDTH_ == 8)
WIZCHIP.IF.BUS._write_data(IDM_AR, (uint8_t)(AddrSel >> 8));
WIZCHIP.IF.BUS._write_data(WIZCHIP_OFFSET_INC(IDM_AR,1),(uint8_t)AddrSel);
WIZCHIP.IF.BUS._write_data(IDM_DR,(uint8_t)(wb>>8));
WIZCHIP.IF.BUS._write_data(WIZCHIP_OFFSET_INC(IDM_DR,1),(uint8_t)wb);
#elif(_WIZCHIP_IO_BUS_WIDTH_ == 16)
WIZCHIP.IF.BUS._write_data(IDM_AR, (uint16_t)AddrSel);
WIZCHIP.IF.BUS._write_data(IDM_DR, wb);
#else
#error "Abnoraml _WIZCHIP_IO_BUS_WIDTH_. Should be 8 or 16"
#endif
#else
#error "Unknown _WIZCHIP_IO_MODE_ in W5300. !!!"
#endif
WIZCHIP.CS._deselect();
WIZCHIP_CRITICAL_EXIT();
}
uint16_t WIZCHIP_READ(uint32_t AddrSel)
{
uint16_t ret;
WIZCHIP_CRITICAL_ENTER();
WIZCHIP.CS._select();
#if ( (_WIZCHIP_IO_MODE_ == _WIZCHIP_IO_MODE_BUS_DIR_) )
#if (_WIZCHIP_IO_BUS_WIDTH_ == 8)
ret = (((uint16_t)WIZCHIP.IF.BUS._read_data(AddrSel)) << 8) |
(((uint16_t)WIZCHIP.IF.BUS._read_data(WIZCHIP_OFFSET_INC(AddrSel,1))) & 0x00FF) ;
#elif(_WIZCHIP_IO_BUS_WIDTH_ == 16)
ret = WIZCHIP.IF.BUS._read_data(AddrSel);
#else
#error "Abnoraml _WIZCHIP_IO_BUS_WIDTH_. Should be 8 or 16"
#endif
#elif ( (_WIZCHIP_IO_MODE_ == _WIZCHIP_IO_MODE_BUS_INDIR_) )
#if(_WIZCHIP_IO_BUS_WIDTH_ == 8)
WIZCHIP.IF.BUS._write_data(IDM_AR, (uint8_t)(AddrSel >> 8));
WIZCHIP.IF.BUS._write_data(WIZCHIP_OFFSET_INC(IDM_AR,1),(uint8_t)AddrSel);
ret = (((uint16_t)WIZCHIP.IF.BUS._read_data(IDM_DR)) << 8) |
(((uint16_t)WIZCHIP.IF.BUS._read_data(WIZCHIP_OFFSET_INC(IDM_DR,1))) & 0x00FF);
#elif(_WIZCHIP_IO_BUS_WIDTH_ == 16)
WIZCHIP.IF.BUS._write_data(IDM_AR, (uint16_t)AddrSel);
ret = WIZCHIP.IF.BUS._read_data(IDM_DR);
#else
#error "Abnoraml _WIZCHIP_IO_BUS_WIDTH_. Should be 8 or 16"
#endif
#else
#error "Unknown _WIZCHIP_IO_MODE_ in W5300. !!!"
#endif
WIZCHIP.CS._deselect();
WIZCHIP_CRITICAL_EXIT();
return ret;
}
void setTMSR(uint8_t sn,uint8_t tmsr)
{
uint16_t tmem;
tmem = WIZCHIP_READ(WIZCHIP_OFFSET_INC(TMS01R, (sn & 0xFE)));
if(sn & 0x01) tmem = (tmem & 0xFF00) | (((uint16_t)tmsr ) & 0x00FF) ;
else tmem = (tmem & 0x00FF) | (((uint16_t)tmsr) << 8) ;
WIZCHIP_WRITE(WIZCHIP_OFFSET_INC(TMS01R, (sn & 0xFE)),tmem);
}
uint8_t getTMSR(uint8_t sn)
{
if(sn & 0x01)
return (uint8_t)(WIZCHIP_READ(WIZCHIP_OFFSET_INC(TMS01R, (sn & 0xFE))) & 0x00FF);
return (uint8_t)(WIZCHIP_READ(WIZCHIP_OFFSET_INC(TMS01R, (sn & 0xFE))) >> 8);
}
void setRMSR(uint8_t sn,uint8_t rmsr)
{
uint16_t rmem;
rmem = WIZCHIP_READ(WIZCHIP_OFFSET_INC(RMS01R, (sn & 0xFE)));
if(sn & 0x01) rmem = (rmem & 0xFF00) | (((uint16_t)rmsr ) & 0x00FF) ;
else rmem = (rmem & 0x00FF) | (((uint16_t)rmsr) << 8) ;
WIZCHIP_WRITE(WIZCHIP_OFFSET_INC(RMS01R, (sn & 0xFE)),rmem);
}
uint8_t getRMSR(uint8_t sn)
{
if(sn & 0x01)
return (uint8_t)(WIZCHIP_READ(WIZCHIP_OFFSET_INC(RMS01R, (sn & 0xFE))) & 0x00FF);
return (uint8_t)(WIZCHIP_READ(WIZCHIP_OFFSET_INC(RMS01R, (sn & 0xFE))) >> 8);
}
uint32_t getSn_TX_FSR(uint8_t sn)
{
uint32_t free_tx_size=0;
uint32_t free_tx_size1=1;
while(1)
{
free_tx_size = (((uint32_t)WIZCHIP_READ(Sn_TX_FSR(sn))) << 16) |
(((uint32_t)WIZCHIP_READ(WIZCHIP_OFFSET_INC(Sn_TX_FSR(sn),2))) & 0x0000FFFF); // read
if(free_tx_size == free_tx_size1) break; // if first == sencond, Sn_TX_FSR value is valid.
free_tx_size1 = free_tx_size; // save second value into first
}
return free_tx_size;
}
uint32_t getSn_RX_RSR(uint8_t sn)
{
uint32_t received_rx_size=0;
uint32_t received_rx_size1=1;
while(1)
{
received_rx_size = (((uint32_t)WIZCHIP_READ(Sn_RX_RSR(sn))) << 16) |
(((uint32_t)WIZCHIP_READ(WIZCHIP_OFFSET_INC(Sn_RX_RSR(sn),2))) & 0x0000FFFF);
if(received_rx_size == received_rx_size1) break;
received_rx_size1 = received_rx_size; // if first == sencond, Sn_RX_RSR value is valid.
} // save second value into first
return received_rx_size + (uint32_t)((sock_pack_info[sn] & 0x02) ? 1 : 0);
}
void wiz_send_data(uint8_t sn, uint8_t *wizdata, uint32_t len)
{
uint32_t i = 0;
if(len == 0) return;
for(i = 0; i < len ; i += 2)
setSn_TX_FIFOR(sn, (((uint16_t)wizdata[i]) << 8) | (((uint16_t)wizdata[i+1]) & 0x00FF))
}
void wiz_recv_data(uint8_t sn, uint8_t *wizdata, uint32_t len)
{
uint16_t rd = 0;
uint32_t i = 0;
if(len == 0) return;
for(i = 0; i < len; i++)
{
if((i & 0x01)==0)
{
rd = getSn_RX_FIFOR(sn);
wizdata[i] = (uint8_t)(rd >> 8);
}
else wizdata[i] = (uint8_t)rd; // For checking the memory access violation
}
sock_remained_byte[sn] = (uint8_t)rd; // back up the remaind fifo byte.
}
void wiz_recv_ignore(uint8_t sn, uint32_t len)
{
uint32_t i = 0;
for(i = 0; i < len ; i += 2) getSn_RX_FIFOR(sn);
}
#endif

2327
Ethernet/W5300/w5300.h Normal file

File diff suppressed because it is too large Load Diff

View File

@ -59,6 +59,7 @@
#define _W5500_SPI_FDM_OP_LEN2_ 0x02 #define _W5500_SPI_FDM_OP_LEN2_ 0x02
#define _W5500_SPI_FDM_OP_LEN4_ 0x03 #define _W5500_SPI_FDM_OP_LEN4_ 0x03
#if (_WIZCHIP_ == 5500)
//////////////////////////////////////////////////// ////////////////////////////////////////////////////
uint8_t WIZCHIP_READ(uint32_t AddrSel) uint8_t WIZCHIP_READ(uint32_t AddrSel)
@ -263,3 +264,4 @@ void wiz_recv_ignore(uint8_t sn, uint16_t len)
setSn_RX_RD(sn,ptr); setSn_RX_RD(sn,ptr);
} }
#endif

View File

@ -43,12 +43,18 @@
// //
//***************************************************************************** //*****************************************************************************
//
#ifndef _W5500_H_ #ifndef _W5500_H_
#define _W5500_H_ #define _W5500_H_
#include <stdint.h> #include <stdint.h>
#include "wizchip_conf.h" #include "wizchip_conf.h"
/// @cond DOXY_APPLY_CODE
#if (_WIZCHIP_ == 5500)
/// @endcond
#define _W5500_IO_BASE_ 0x00000000 #define _W5500_IO_BASE_ 0x00000000
#define _W5500_SPI_READ_ (0x00 << 2) //< SPI interface Read operation in Control Phase #define _W5500_SPI_READ_ (0x00 << 2) //< SPI interface Read operation in Control Phase
@ -147,7 +153,7 @@
* @sa MR : Mode register. * @sa MR : Mode register.
* @sa GAR, SUBR, SHAR, SIPR * @sa GAR, SUBR, SHAR, SIPR
* @sa INTLEVEL, IR, IMR, SIR, SIMR : Interrupt. * @sa INTLEVEL, IR, IMR, SIR, SIMR : Interrupt.
* @sa RTR, RCR : Data retransmission. * @sa _RTR_, _RCR_ : Data retransmission.
* @sa PTIMER, PMAGIC, PHAR, PSID, PMRU : PPPoE. * @sa PTIMER, PMAGIC, PHAR, PSID, PMRU : PPPoE.
* @sa UIPR, UPORTR : ICMP message. * @sa UIPR, UPORTR : ICMP message.
* @sa PHYCFGR, VERSIONR : etc. * @sa PHYCFGR, VERSIONR : etc.
@ -262,10 +268,10 @@
/** /**
* @ingroup Common_register_group * @ingroup Common_register_group
* @brief Interrupt mask register(R/W) * @brief Interrupt mask register(R/W)
* @details @ref IMR is used to mask interrupts. Each bit of @ref IMR corresponds to each bit of @ref IR. * @details @ref _IMR_ is used to mask interrupts. Each bit of @ref _IMR_ corresponds to each bit of @ref IR.
* When a bit of @ref IMR is and the corresponding bit of @ref IR is an interrupt will be issued. In other words, * When a bit of @ref _IMR_ is and the corresponding bit of @ref IR is an interrupt will be issued. In other words,
* if a bit of @ref IMR is an interrupt will not be issued even if the corresponding bit of @ref IR is \n\n * if a bit of @ref _IMR_ is an interrupt will not be issued even if the corresponding bit of @ref IR is \n\n
* Each bit of @ref IMR defined as the following. * Each bit of @ref _IMR_ defined as the following.
* <table> * <table>
* <tr> <td>7</td> <td>6</td> <td>5</td> <td>4</td> <td>3</td> <td>2</td> <td>1</td> <td>0</td> </tr> * <tr> <td>7</td> <td>6</td> <td>5</td> <td>4</td> <td>3</td> <td>2</td> <td>1</td> <td>0</td> </tr>
* <tr> <td>IM_IR7</td> <td>IM_IR6</td> <td>IM_IR5</td> <td>IM_IR4</td> <td>Reserved</td> <td>Reserved</td> <td>Reserved</td> <td>Reserved</td> </tr> * <tr> <td>IM_IR7</td> <td>IM_IR6</td> <td>IM_IR5</td> <td>IM_IR4</td> <td>Reserved</td> <td>Reserved</td> <td>Reserved</td> <td>Reserved</td> </tr>
@ -275,7 +281,9 @@
* - \ref IM_IR5 : PPPoE Close Interrupt Mask * - \ref IM_IR5 : PPPoE Close Interrupt Mask
* - \ref IM_IR4 : Magic Packet Interrupt Mask * - \ref IM_IR4 : Magic Packet Interrupt Mask
*/ */
#define IMR (_W5500_IO_BASE_ + (0x0016 << 8) + (WIZCHIP_CREG_BLOCK << 3)) //M20150401 : Rename SYMBOE ( Re-define error in a compile)
//#define IMR (_W5500_IO_BASE_ + (0x0016 << 8) + (WIZCHIP_CREG_BLOCK << 3))
#define _IMR_ (_W5500_IO_BASE_ + (0x0016 << 8) + (WIZCHIP_CREG_BLOCK << 3))
/** /**
* @ingroup Common_register_group * @ingroup Common_register_group
@ -297,20 +305,24 @@
/** /**
* @ingroup Common_register_group * @ingroup Common_register_group
* @brief Timeout register address( 1 is 100us )(R/W) * @brief Timeout register address( 1 is 100us )(R/W)
* @details @ref RTR configures the retransmission timeout period. The unit of timeout period is 100us and the default of @ref RTR is x07D0or 000 * @details @ref _RTR_ configures the retransmission timeout period. The unit of timeout period is 100us and the default of @ref _RTR_ is x07D0.
* And so the default timeout period is 200ms(100us X 2000). During the time configured by @ref RTR, W5500 waits for the peer response * And so the default timeout period is 200ms(100us X 2000). During the time configured by @ref _RTR_, W5500 waits for the peer response
* to the packet that is transmitted by \ref Sn_CR (CONNECT, DISCON, CLOSE, SEND, SEND_MAC, SEND_KEEP command). * to the packet that is transmitted by \ref Sn_CR (CONNECT, DISCON, CLOSE, SEND, SEND_MAC, SEND_KEEP command).
* If the peer does not respond within the @ref RTR time, W5500 retransmits the packet or issues timeout. * If the peer does not respond within the @ref _RTR_ time, W5500 retransmits the packet or issues timeout.
*/ */
#define RTR (_W5500_IO_BASE_ + (0x0019 << 8) + (WIZCHIP_CREG_BLOCK << 3)) //M20150401 : Rename SYMBOE ( Re-define error in a compile)
//#define RTR (_W5500_IO_BASE_ + (0x0019 << 8) + (WIZCHIP_CREG_BLOCK << 3))
#define _RTR_ (_W5500_IO_BASE_ + (0x0019 << 8) + (WIZCHIP_CREG_BLOCK << 3))
/** /**
* @ingroup Common_register_group * @ingroup Common_register_group
* @brief Retry count register(R/W) * @brief Retry count register(R/W)
* @details @ref RCR configures the number of time of retransmission. * @details @ref _RCR_ configures the number of time of retransmission.
* When retransmission occurs as many as ref RCR+1 Timeout interrupt is issued (@ref Sn_IR[TIMEOUT] = . * When retransmission occurs as many as ref _RCR_+1 Timeout interrupt is issued (@ref Sn_IR_TIMEOUT = '1').
*/ */
#define RCR (_W5500_IO_BASE_ + (0x001B << 8) + (WIZCHIP_CREG_BLOCK << 3)) //M20150401 : Rename SYMBOE ( Re-define error in a compile)
//#define RCR (_W5500_IO_BASE_ + (0x001B << 8) + (WIZCHIP_CREG_BLOCK << 3))
#define _RCR_ (_W5500_IO_BASE_ + (0x001B << 8) + (WIZCHIP_CREG_BLOCK << 3))
/** /**
* @ingroup Common_register_group * @ingroup Common_register_group
@ -351,7 +363,7 @@
* @ingroup Common_register_group * @ingroup Common_register_group
* @brief Unreachable IP register address in UDP mode(R) * @brief Unreachable IP register address in UDP mode(R)
* @details W5500 receives an ICMP packet(Destination port unreachable) when data is sent to a port number * @details W5500 receives an ICMP packet(Destination port unreachable) when data is sent to a port number
* which socket is not open and @ref UNREACH bit of @ref IR becomes and @ref UIPR & @ref UPORTR indicates * which socket is not open and @ref IR_UNREACH bit of @ref IR becomes and @ref UIPR & @ref UPORTR indicates
* the destination IP address & port number respectively. * the destination IP address & port number respectively.
*/ */
#define UIPR (_W5500_IO_BASE_ + (0x0028 << 8) + (WIZCHIP_CREG_BLOCK << 3)) #define UIPR (_W5500_IO_BASE_ + (0x0028 << 8) + (WIZCHIP_CREG_BLOCK << 3))
@ -360,7 +372,7 @@
* @ingroup Common_register_group * @ingroup Common_register_group
* @brief Unreachable Port register address in UDP mode(R) * @brief Unreachable Port register address in UDP mode(R)
* @details W5500 receives an ICMP packet(Destination port unreachable) when data is sent to a port number * @details W5500 receives an ICMP packet(Destination port unreachable) when data is sent to a port number
* which socket is not open and @ref UNREACH bit of @ref IR becomes and @ref UIPR & @ref UPORTR * which socket is not open and @ref IR_UNREACH bit of @ref IR becomes and @ref UIPR & @ref UPORTR
* indicates the destination IP address & port number respectively. * indicates the destination IP address & port number respectively.
*/ */
#define UPORTR (_W5500_IO_BASE_ + (0x002C << 8) + (WIZCHIP_CREG_BLOCK << 3)) #define UPORTR (_W5500_IO_BASE_ + (0x002C << 8) + (WIZCHIP_CREG_BLOCK << 3))
@ -488,7 +500,7 @@
* @ingroup Socket_register_group * @ingroup Socket_register_group
* @brief source port register(R/W) * @brief source port register(R/W)
* @details @ref Sn_PORT configures the source port number of Socket n. * @details @ref Sn_PORT configures the source port number of Socket n.
* It is valid when Socket n is used in TCP/UPD mode. It should be set before OPEN command is ordered. * It is valid when Socket n is used in TCP/UDP mode. It should be set before OPEN command is ordered.
*/ */
#define Sn_PORT(N) (_W5500_IO_BASE_ + (0x0004 << 8) + (WIZCHIP_SREG_BLOCK(N) << 3)) #define Sn_PORT(N) (_W5500_IO_BASE_ + (0x0004 << 8) + (WIZCHIP_SREG_BLOCK(N) << 3))
@ -504,8 +516,8 @@
* @ingroup Socket_register_group * @ingroup Socket_register_group
* @brief Peer IP register address(R/W) * @brief Peer IP register address(R/W)
* @details @ref Sn_DIPR configures or indicates the destination IP address of Socket n. It is valid when Socket n is used in TCP/UDP mode. * @details @ref Sn_DIPR configures or indicates the destination IP address of Socket n. It is valid when Socket n is used in TCP/UDP mode.
* In TCP client mode, it configures an IP address of <EFBFBD>TCP serverbefore CONNECT command. * In TCP client mode, it configures an IP address of TCP serverbefore CONNECT command.
* In TCP server mode, it indicates an IP address of <EFBFBD>TCP clientafter successfully establishing connection. * In TCP server mode, it indicates an IP address of TCP clientafter successfully establishing connection.
* In UDP mode, it configures an IP address of peer to be received the UDP packet by SEND or SEND_MAC command. * In UDP mode, it configures an IP address of peer to be received the UDP packet by SEND or SEND_MAC command.
*/ */
#define Sn_DIPR(N) (_W5500_IO_BASE_ + (0x000C << 8) + (WIZCHIP_SREG_BLOCK(N) << 3)) #define Sn_DIPR(N) (_W5500_IO_BASE_ + (0x000C << 8) + (WIZCHIP_SREG_BLOCK(N) << 3))
@ -514,8 +526,8 @@
* @ingroup Socket_register_group * @ingroup Socket_register_group
* @brief Peer port register address(R/W) * @brief Peer port register address(R/W)
* @details @ref Sn_DPORT configures or indicates the destination port number of Socket n. It is valid when Socket n is used in TCP/UDP mode. * @details @ref Sn_DPORT configures or indicates the destination port number of Socket n. It is valid when Socket n is used in TCP/UDP mode.
* In <EFBFBD>TCP clientmode, it configures the listen port number of <EFBFBD>TCP serverbefore CONNECT command. * In TCP clientmode, it configures the listen port number of TCP serverbefore CONNECT command.
* In <EFBFBD>TCP Servermode, it indicates the port number of TCP client after successfully establishing connection. * In TCP Servermode, it indicates the port number of TCP client after successfully establishing connection.
* In UDP mode, it configures the port number of peer to be transmitted the UDP packet by SEND/SEND_MAC command. * In UDP mode, it configures the port number of peer to be transmitted the UDP packet by SEND/SEND_MAC command.
*/ */
#define Sn_DPORT(N) (_W5500_IO_BASE_ + (0x0010 << 8) + (WIZCHIP_SREG_BLOCK(N) << 3)) #define Sn_DPORT(N) (_W5500_IO_BASE_ + (0x0010 << 8) + (WIZCHIP_SREG_BLOCK(N) << 3))
@ -819,7 +831,7 @@
* 1 : Enable No Delayed ACK option\n * 1 : Enable No Delayed ACK option\n
* This bit is applied only during TCP mode (P[3:0] = 001.\n * This bit is applied only during TCP mode (P[3:0] = 001.\n
* When this bit is It sends the ACK packet without delay as soon as a Data packet is received from a peer.\n * When this bit is It sends the ACK packet without delay as soon as a Data packet is received from a peer.\n
* When this bit is It sends the ACK packet after waiting for the timeout time configured by @ref RTR. * When this bit is It sends the ACK packet after waiting for the timeout time configured by @ref _RTR_.
*/ */
#define Sn_MR_ND 0x20 #define Sn_MR_ND 0x20
@ -916,46 +928,46 @@
* The table below shows the value of @ref Sn_SR corresponding to @ref Sn_MR.\n * The table below shows the value of @ref Sn_SR corresponding to @ref Sn_MR.\n
* <table> * <table>
* <tr> <td>\b Sn_MR (P[3:0])</td> <td>\b Sn_SR</td> </tr> * <tr> <td>\b Sn_MR (P[3:0])</td> <td>\b Sn_SR</td> </tr>
* <tr> <td>Sn_MR_CLOSE (000</td> <td></td> </tr> * <tr> <td>Sn_MR_CLOSE (000)</td> <td></td> </tr>
* <tr> <td>Sn_MR_TCP (001</td> <td>SOCK_INIT (0x13)</td> </tr> * <tr> <td>Sn_MR_TCP (001)</td> <td>SOCK_INIT (0x13)</td> </tr>
* <tr> <td>Sn_MR_UDP (010</td> <td>SOCK_UDP (0x22)</td> </tr> * <tr> <td>Sn_MR_UDP (010)</td> <td>SOCK_UDP (0x22)</td> </tr>
* <tr> <td>S0_MR_MACRAW (100</td> <td>SOCK_MACRAW (0x02)</td> </tr> * <tr> <td>S0_MR_MACRAW (100)</td> <td>SOCK_MACRAW (0x02)</td> </tr>
* </table> * </table>
*/ */
#define Sn_CR_OPEN 0x01 #define Sn_CR_OPEN 0x01
/** /**
* @brief Wait connection request in TCP mode(Server mode) * @brief Wait connection request in TCP mode(Server mode)
* @details This is valid only in TCP mode (Sn_MR(P3:P0) = Sn_MR_TCP). * @details This is valid only in TCP mode (\ref Sn_MR(P3:P0) = \ref Sn_MR_TCP).
* In this mode, Socket n operates as a <EFBFBD>TCP serverand waits for connection-request (SYN packet) from any <EFBFBD>TCP client * In this mode, Socket n operates as a TCP serverand waits for connection-request (SYN packet) from any TCP client
* The @ref Sn_SR changes the state from SOCK_INIT to SOCKET_LISTEN. * The @ref Sn_SR changes the state from \ref SOCK_INIT to \ref SOCKET_LISTEN.
* When a <EFBFBD>TCP clientconnection request is successfully established, * When a TCP clientconnection request is successfully established,
* the @ref Sn_SR changes from SOCK_LISTEN to SOCK_ESTABLISHED and the Sn_IR(0) becomes * the @ref Sn_SR changes from SOCK_LISTEN to SOCK_ESTABLISHED and the @ref Sn_IR(0) becomes
* But when a <EFBFBD>TCP clientconnection request is failed, Sn_IR(3) becomes and the status of @ref Sn_SR changes to SOCK_CLOSED. * But when a TCP clientconnection request is failed, @ref Sn_IR(3) becomes and the status of @ref Sn_SR changes to SOCK_CLOSED.
*/ */
#define Sn_CR_LISTEN 0x02 #define Sn_CR_LISTEN 0x02
/** /**
* @brief Send connection request in TCP mode(Client mode) * @brief Send connection request in TCP mode(Client mode)
* @details To connect, a connect-request (SYN packet) is sent to b>TCP server</b>configured by @ref Sn_DIPR & Sn_DPORT(destination address & port). * @details To connect, a connect-request (SYN packet) is sent to <b>TCP server</b>configured by @ref Sn_DIPR & Sn_DPORT(destination address & port).
* If the connect-request is successful, the @ref Sn_SR is changed to @ref SOCK_ESTABLISHED and the Sn_IR(0) becomes \n\n * If the connect-request is successful, the @ref Sn_SR is changed to @ref SOCK_ESTABLISHED and the Sn_IR(0) becomes \n\n
* The connect-request fails in the following three cases.\n * The connect-request fails in the following three cases.\n
* 1. When a @b ARPTO occurs (@ref Sn_IR[3] = ) because destination hardware address is not acquired through the ARP-process.\n * 1. When a @b ARPTO occurs (@ref Sn_IR[3] = ) because destination hardware address is not acquired through the ARP-process.\n
* 2. When a @b SYN/ACK packet is not received and @b TCPTO (Sn_IR(3) = )\n * 2. When a @b SYN/ACK packet is not received and @b TCPTO (Sn_IR(3) = )\n
* 3. When a @b RST packet is received instead of a @b SYN/ACK packet. In these cases, @ref Sn_SR is changed to @ref SOCK_CLOSED. * 3. When a @b RST packet is received instead of a @b SYN/ACK packet. In these cases, @ref Sn_SR is changed to @ref SOCK_CLOSED.
* @note This is valid only in TCP mode and operates when Socket n acts as b>TCP client</b> * @note This is valid only in TCP mode and operates when Socket n acts as <b>TCP client</b>
*/ */
#define Sn_CR_CONNECT 0x04 #define Sn_CR_CONNECT 0x04
/** /**
* @brief Send closing request in TCP mode * @brief Send closing request in TCP mode
* @details Regardless of b>TCP server</b>or b>TCP client</b> the DISCON command processes the disconnect-process (b>Active close</b>or b>Passive close</b>.\n * @details Regardless of <b>TCP server</b>or <b>TCP client</b> the DISCON command processes the disconnect-process (b>Active close</b>or <b>Passive close</b>.\n
* @par Active close * @par Active close
* it transmits disconnect-request(FIN packet) to the connected peer\n * it transmits disconnect-request(FIN packet) to the connected peer\n
* @par Passive close * @par Passive close
* When FIN packet is received from peer, a FIN packet is replied back to the peer.\n * When FIN packet is received from peer, a FIN packet is replied back to the peer.\n
* @details When the disconnect-process is successful (that is, FIN/ACK packet is received successfully), @ref Sn_SR is changed to @ref SOCK_CLOSED.\n * @details When the disconnect-process is successful (that is, FIN/ACK packet is received successfully), @ref Sn_SR is changed to @ref SOCK_CLOSED.\n
* Otherwise, TCPTO occurs (Sn_IR(3)=)= and then @ref Sn_SR is changed to @ref SOCK_CLOSED. * Otherwise, TCPTO occurs (\ref Sn_IR(3)='1') and then @ref Sn_SR is changed to @ref SOCK_CLOSED.
* @note Valid only in TCP mode. * @note Valid only in TCP mode.
*/ */
#define Sn_CR_DISCON 0x08 #define Sn_CR_DISCON 0x08
@ -1034,24 +1046,24 @@
/* Sn_SR values */ /* Sn_SR values */
/** /**
* @brief Closed * @brief Closed
* @details This indicates that Socket n is released.\N * @details This indicates that Socket n is released.\n
* When DICON, CLOSE command is ordered, or when a timeout occurs, it is changed to @ref SOCK_CLOSED regardless of previous status. * When DICON, CLOSE command is ordered, or when a timeout occurs, it is changed to @ref SOCK_CLOSED regardless of previous status.
*/ */
#define SOCK_CLOSED 0x00 #define SOCK_CLOSED 0x00
/** /**
* @brief Initiate state * @brief Initiate state
* @details This indicates Socket n is opened with TCP mode.\N * @details This indicates Socket n is opened with TCP mode.\n
* It is changed to @ref SOCK_INIT when Sn_MR(P[3:0]) = 001and OPEN command is ordered.\N * It is changed to @ref SOCK_INIT when @ref Sn_MR(P[3:0]) = 001 and OPEN command is ordered.\n
* After @ref SOCK_INIT, user can use LISTEN /CONNECT command. * After @ref SOCK_INIT, user can use LISTEN /CONNECT command.
*/ */
#define SOCK_INIT 0x13 #define SOCK_INIT 0x13
/** /**
* @brief Listen state * @brief Listen state
* @details This indicates Socket n is operating as b>TCP server</b>mode and waiting for connection-request (SYN packet) from a peer (b>TCP client</b>.\n * @details This indicates Socket n is operating as <b>TCP server</b>mode and waiting for connection-request (SYN packet) from a peer <b>TCP client</b>.\n
* It will change to @ref SOCK_ESTALBLISHED when the connection-request is successfully accepted.\n * It will change to @ref SOCK_ESTALBLISHED when the connection-request is successfully accepted.\n
* Otherwise it will change to @ref SOCK_CLOSED after TCPTO occurred (Sn_IR(TIMEOUT) = . * Otherwise it will change to @ref SOCK_CLOSED after TCPTO @ref Sn_IR(TIMEOUT) = '1') is occurred.
*/ */
#define SOCK_LISTEN 0x14 #define SOCK_LISTEN 0x14
@ -1060,7 +1072,7 @@
* @details This indicates Socket n sent the connect-request packet (SYN packet) to a peer.\n * @details This indicates Socket n sent the connect-request packet (SYN packet) to a peer.\n
* It is temporarily shown when @ref Sn_SR is changed from @ref SOCK_INIT to @ref SOCK_ESTABLISHED by CONNECT command.\n * It is temporarily shown when @ref Sn_SR is changed from @ref SOCK_INIT to @ref SOCK_ESTABLISHED by CONNECT command.\n
* If connect-accept(SYN/ACK packet) is received from the peer at SOCK_SYNSENT, it changes to @ref SOCK_ESTABLISHED.\n * If connect-accept(SYN/ACK packet) is received from the peer at SOCK_SYNSENT, it changes to @ref SOCK_ESTABLISHED.\n
* Otherwise, it changes to @ref SOCK_CLOSED after TCPTO (@ref Sn_IR[TIMEOUT] = is occurred. * Otherwise, it changes to @ref SOCK_CLOSED after TCPTO (@ref Sn_IR[TIMEOUT] = '1') is occurred.
*/ */
#define SOCK_SYNSENT 0x15 #define SOCK_SYNSENT 0x15
@ -1068,14 +1080,14 @@
* @brief Connection state * @brief Connection state
* @details It indicates Socket n successfully received the connect-request packet (SYN packet) from a peer.\n * @details It indicates Socket n successfully received the connect-request packet (SYN packet) from a peer.\n
* If socket n sends the response (SYN/ACK packet) to the peer successfully, it changes to @ref SOCK_ESTABLISHED. \n * If socket n sends the response (SYN/ACK packet) to the peer successfully, it changes to @ref SOCK_ESTABLISHED. \n
* If not, it changes to @ref SOCK_CLOSED after timeout occurs (@ref Sn_IR[TIMEOUT] = . * If not, it changes to @ref SOCK_CLOSED after timeout (@ref Sn_IR[TIMEOUT] = '1') is occurred.
*/ */
#define SOCK_SYNRECV 0x16 #define SOCK_SYNRECV 0x16
/** /**
* @brief Success to connect * @brief Success to connect
* @details This indicates the status of the connection of Socket n.\n * @details This indicates the status of the connection of Socket n.\n
* It changes to @ref SOCK_ESTABLISHED when the b>TCP SERVER</b>processed the SYN packet from the b>TCP CLIENT</b>during @ref SOCK_LISTEN, or * It changes to @ref SOCK_ESTABLISHED when the <b>TCP SERVER</b>processed the SYN packet from the <b>TCP CLIENT</b>during @ref SOCK_LISTEN, or
* when the CONNECT command is successful.\n * when the CONNECT command is successful.\n
* During @ref SOCK_ESTABLISHED, DATA packet can be transferred using SEND or RECV command. * During @ref SOCK_ESTABLISHED, DATA packet can be transferred using SEND or RECV command.
*/ */
@ -1116,14 +1128,14 @@
/** /**
* @brief Closing state * @brief Closing state
* @details This indicates Socket n is waiting for the response (FIN/ACK packet) to the disconnect-request (FIN packet) by passive-close.\n * @details This indicates Socket n is waiting for the response (FIN/ACK packet) to the disconnect-request (FIN packet) by passive-close.\n
* It changes to @ref SOCK_CLOSED when Socket n received the response successfully, or when timeout occurs (@ref Sn_IR[TIMEOUT] = . * It changes to @ref SOCK_CLOSED when Socket n received the response successfully, or when timeout(@ref Sn_IR[TIMEOUT] = '1') is occurred.
*/ */
#define SOCK_LAST_ACK 0x1D #define SOCK_LAST_ACK 0x1D
/** /**
* @brief UDP socket * @brief UDP socket
* @details This indicates Socket n is opened in UDP mode(Sn_MR(P[3:0]) = 010.\n * @details This indicates Socket n is opened in UDP mode(@ref Sn_MR(P[3:0]) = '010').\n
* It changes to SOCK_UPD when Sn_MR(P[3:0]) = 010 and OPEN command is ordered.\n * It changes to SOCK_UDP when @ref Sn_MR(P[3:0]) = '010' and @ref Sn_CR_OPEN command is ordered.\n
* Unlike TCP mode, data can be transfered without the connection-process. * Unlike TCP mode, data can be transfered without the connection-process.
*/ */
#define SOCK_UDP 0x22 #define SOCK_UDP 0x22
@ -1169,6 +1181,7 @@
#ifdef _exit #ifdef _exit
#undef _exit #undef _exit
#endif #endif
/** /**
* @brief Exit a critical section * @brief Exit a critical section
* *
@ -1180,11 +1193,9 @@
* @sa WIZCHIP_READ(), WIZCHIP_WRITE(), WIZCHIP_READ_BUF(), WIZCHIP_WRITE_BUF() * @sa WIZCHIP_READ(), WIZCHIP_WRITE(), WIZCHIP_READ_BUF(), WIZCHIP_WRITE_BUF()
* @sa WIZCHIP_CRITICAL_ENTER() * @sa WIZCHIP_CRITICAL_ENTER()
*/ */
#define WIZCHIP_CRITICAL_EXIT() WIZCHIP.CRIS._exit() #define WIZCHIP_CRITICAL_EXIT() WIZCHIP.CRIS._exit()
//////////////////////// ////////////////////////
// Basic I/O Function // // Basic I/O Function //
//////////////////////// ////////////////////////
@ -1337,8 +1348,13 @@ void WIZCHIP_WRITE_BUF(uint32_t AddrSel, uint8_t* pBuf, uint16_t len);
* @return uint16_t. Value of @ref INTLEVEL register. * @return uint16_t. Value of @ref INTLEVEL register.
* @sa setINTLEVEL() * @sa setINTLEVEL()
*/ */
//M20150401 : Type explict declaration
/*
#define getINTLEVEL() \ #define getINTLEVEL() \
((WIZCHIP_READ(INTLEVEL) << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(INTLEVEL,1))) ((WIZCHIP_READ(INTLEVEL) << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(INTLEVEL,1)))
*/
#define getINTLEVEL() \
(((uint16_t)WIZCHIP_READ(INTLEVEL) << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(INTLEVEL,1)))
/** /**
* @ingroup Common_register_access_function * @ingroup Common_register_access_function
@ -1359,22 +1375,21 @@ void WIZCHIP_WRITE_BUF(uint32_t AddrSel, uint8_t* pBuf, uint16_t len);
(WIZCHIP_READ(IR) & 0xF0) (WIZCHIP_READ(IR) & 0xF0)
/** /**
* @ingroup Common_register_access_function * @ingroup Common_register_access_function
* @brief Set @ref IMR register * @brief Set @ref _IMR_ register
* @param (uint8_t)imr Value to set @ref IMR register. * @param (uint8_t)imr Value to set @ref _IMR_ register.
* @sa getIMR() * @sa getIMR()
*/ */
#define setIMR(imr) \ #define setIMR(imr) \
WIZCHIP_WRITE(IMR, imr) WIZCHIP_WRITE(_IMR_, imr)
/** /**
* @ingroup Common_register_access_function * @ingroup Common_register_access_function
* @brief Get @ref IMR register * @brief Get @ref _IMR_ register
* @return uint8_t. Value of @ref IMR register. * @return uint8_t. Value of @ref _IMR_ register.
* @sa setIMR() * @sa setIMR()
*/ */
#define getIMR() \ #define getIMR() \
WIZCHIP_READ(IMR) WIZCHIP_READ(_IMR_)
/** /**
* @ingroup Common_register_access_function * @ingroup Common_register_access_function
@ -1413,41 +1428,47 @@ void WIZCHIP_WRITE_BUF(uint32_t AddrSel, uint8_t* pBuf, uint16_t len);
/** /**
* @ingroup Common_register_access_function * @ingroup Common_register_access_function
* @brief Set @ref RTR register * @brief Set @ref _RTR_ register
* @param (uint16_t)rtr Value to set @ref RTR register. * @param (uint16_t)rtr Value to set @ref _RTR_ register.
* @sa getRTR() * @sa getRTR()
*/ */
#define setRTR(rtr) {\ #define setRTR(rtr) {\
WIZCHIP_WRITE(RTR, (uint8_t)(rtr >> 8)); \ WIZCHIP_WRITE(_RTR_, (uint8_t)(rtr >> 8)); \
WIZCHIP_WRITE(WIZCHIP_OFFSET_INC(RTR,1), (uint8_t) rtr); \ WIZCHIP_WRITE(WIZCHIP_OFFSET_INC(_RTR_,1), (uint8_t) rtr); \
} }
/** /**
* @ingroup Common_register_access_function * @ingroup Common_register_access_function
* @brief Get @ref RTR register * @brief Get @ref _RTR_ register
* @return uint16_t. Value of @ref RTR register. * @return uint16_t. Value of @ref _RTR_ register.
* @sa setRTR() * @sa setRTR()
*/ */
//M20150401 : Type explict declaration
/*
#define getRTR() \ #define getRTR() \
((WIZCHIP_READ(RTR) << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(RTR,1))) ((WIZCHIP_READ(_RTR_) << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(_RTR_,1)))
*/
#define getRTR() \
(((uint16_t)WIZCHIP_READ(_RTR_) << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(_RTR_,1)))
/** /**
* @ingroup Common_register_access_function * @ingroup Common_register_access_function
* @brief Set @ref RCR register * @brief Set @ref _RCR_ register
* @param (uint8_t)rcr Value to set @ref RCR register. * @param (uint8_t)rcr Value to set @ref _RCR_ register.
* @sa getRCR() * @sa getRCR()
*/ */
#define setRCR(rcr) \ #define setRCR(rcr) \
WIZCHIP_WRITE(RCR, rcr) WIZCHIP_WRITE(_RCR_, rcr)
/** /**
* @ingroup Common_register_access_function * @ingroup Common_register_access_function
* @brief Get @ref RCR register * @brief Get @ref _RCR_ register
* @return uint8_t. Value of @ref RCR register. * @return uint8_t. Value of @ref _RCR_ register.
* @sa setRCR() * @sa setRCR()
*/ */
#define getRCR() \ #define getRCR() \
WIZCHIP_READ(RCR) WIZCHIP_READ(_RCR_)
//================================================== test done =========================================================== //================================================== test done ===========================================================
@ -1489,7 +1510,7 @@ void WIZCHIP_WRITE_BUF(uint32_t AddrSel, uint8_t* pBuf, uint16_t len);
/** /**
* @ingroup Common_register_access_function * @ingroup Common_register_access_function
* @brief Set PHAR address * @brief Set @ref PHAR address
* @param (uint8_t*)phar Pointer variable to set PPP destination MAC register address. It should be allocated 6 bytes. * @param (uint8_t*)phar Pointer variable to set PPP destination MAC register address. It should be allocated 6 bytes.
* @sa getPHAR() * @sa getPHAR()
*/ */
@ -1498,7 +1519,7 @@ void WIZCHIP_WRITE_BUF(uint32_t AddrSel, uint8_t* pBuf, uint16_t len);
/** /**
* @ingroup Common_register_access_function * @ingroup Common_register_access_function
* @brief Get local IP address * @brief Get @ref PHAR address
* @param (uint8_t*)phar Pointer variable to PPP destination MAC register address. It should be allocated 6 bytes. * @param (uint8_t*)phar Pointer variable to PPP destination MAC register address. It should be allocated 6 bytes.
* @sa setPHAR() * @sa setPHAR()
*/ */
@ -1523,8 +1544,13 @@ void WIZCHIP_WRITE_BUF(uint32_t AddrSel, uint8_t* pBuf, uint16_t len);
* @sa setPSID() * @sa setPSID()
*/ */
//uint16_t getPSID(void); //uint16_t getPSID(void);
//M20150401 : Type explict declaration
/*
#define getPSID() \ #define getPSID() \
((WIZCHIP_READ(PSID) << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(PSID,1))) ((WIZCHIP_READ(PSID) << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(PSID,1)))
*/
#define getPSID() \
(((uint16_t)WIZCHIP_READ(PSID) << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(PSID,1)))
/** /**
* @ingroup Common_register_access_function * @ingroup Common_register_access_function
@ -1543,24 +1569,39 @@ void WIZCHIP_WRITE_BUF(uint32_t AddrSel, uint8_t* pBuf, uint16_t len);
* @return uint16_t. Value of @ref PMRU register. * @return uint16_t. Value of @ref PMRU register.
* @sa setPMRU() * @sa setPMRU()
*/ */
//M20150401 : Type explict declaration
/*
#define getPMRU() \ #define getPMRU() \
((WIZCHIP_READ(PMRU) << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(PMRU,1))) ((WIZCHIP_READ(PMRU) << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(PMRU,1)))
*/
#define getPMRU() \
(((uint16_t)WIZCHIP_READ(PMRU) << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(PMRU,1)))
/** /**
* @ingroup Common_register_access_function * @ingroup Common_register_access_function
* @brief Get unreachable IP address * @brief Get unreachable IP address
* @param (uint8_t*)uipr Pointer variable to get unreachable IP address. It should be allocated 4 bytes. * @param (uint8_t*)uipr Pointer variable to get unreachable IP address. It should be allocated 4 bytes.
*/ */
//M20150401 : Size Error of UIPR (6 -> 4)
/*
#define getUIPR(uipr) \ #define getUIPR(uipr) \
WIZCHIP_READ_BUF(UIPR,uipr,6) WIZCHIP_READ_BUF(UIPR,uipr,6)
*/
#define getUIPR(uipr) \
WIZCHIP_READ_BUF(UIPR,uipr,4)
/** /**
* @ingroup Common_register_access_function * @ingroup Common_register_access_function
* @brief Get @ref UPORTR register * @brief Get @ref UPORTR register
* @return uint16_t. Value of @ref UPORTR register. * @return uint16_t. Value of @ref UPORTR register.
*/ */
//M20150401 : Type explict declaration
/*
#define getUPORTR() \ #define getUPORTR() \
((WIZCHIP_READ(UPORTR) << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(UPORTR,1))) ((WIZCHIP_READ(UPORTR) << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(UPORTR,1)))
*/
#define getUPORTR() \
(((uint16_t)WIZCHIP_READ(UPORTR) << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(UPORTR,1)))
/** /**
* @ingroup Common_register_access_function * @ingroup Common_register_access_function
@ -1701,8 +1742,13 @@ void WIZCHIP_WRITE_BUF(uint32_t AddrSel, uint8_t* pBuf, uint16_t len);
* @return uint16_t. Value of @ref Sn_PORT. * @return uint16_t. Value of @ref Sn_PORT.
* @sa setSn_PORT() * @sa setSn_PORT()
*/ */
//M20150401 : Type explict declaration
/*
#define getSn_PORT(sn) \ #define getSn_PORT(sn) \
((WIZCHIP_READ(Sn_PORT(sn)) << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(Sn_PORT(sn),1))) ((WIZCHIP_READ(Sn_PORT(sn)) << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(Sn_PORT(sn),1)))
*/
#define getSn_PORT(sn) \
(((uint16_t)WIZCHIP_READ(Sn_PORT(sn)) << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(Sn_PORT(sn),1)))
/** /**
* @ingroup Socket_register_access_function * @ingroup Socket_register_access_function
@ -1739,7 +1785,7 @@ void WIZCHIP_WRITE_BUF(uint32_t AddrSel, uint8_t* pBuf, uint16_t len);
* @brief Get @ref Sn_DIPR register * @brief Get @ref Sn_DIPR register
* @param (uint8_t)sn Socket number. It should be <b>0 ~ 7</b>. * @param (uint8_t)sn Socket number. It should be <b>0 ~ 7</b>.
* @param (uint8_t*)dipr Pointer variable to get socket n destination IP address. It should be allocated 4 bytes. * @param (uint8_t*)dipr Pointer variable to get socket n destination IP address. It should be allocated 4 bytes.
* @sa SetSn_DIPR() * @sa setSn_DIPR()
*/ */
#define getSn_DIPR(sn, dipr) \ #define getSn_DIPR(sn, dipr) \
WIZCHIP_READ_BUF(Sn_DIPR(sn), dipr, 4) WIZCHIP_READ_BUF(Sn_DIPR(sn), dipr, 4)
@ -1763,8 +1809,13 @@ void WIZCHIP_WRITE_BUF(uint32_t AddrSel, uint8_t* pBuf, uint16_t len);
* @return uint16_t. Value of @ref Sn_DPORT. * @return uint16_t. Value of @ref Sn_DPORT.
* @sa setSn_DPORT() * @sa setSn_DPORT()
*/ */
//M20150401 : Type explict declaration
/*
#define getSn_DPORT(sn) \ #define getSn_DPORT(sn) \
((WIZCHIP_READ(Sn_DPORT(sn)) << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(Sn_DPORT(sn),1))) ((WIZCHIP_READ(Sn_DPORT(sn)) << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(Sn_DPORT(sn),1)))
*/
#define getSn_DPORT(sn) \
(((uint16_t)WIZCHIP_READ(Sn_DPORT(sn)) << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(Sn_DPORT(sn),1)))
/** /**
* @ingroup Socket_register_access_function * @ingroup Socket_register_access_function
@ -1785,8 +1836,13 @@ void WIZCHIP_WRITE_BUF(uint32_t AddrSel, uint8_t* pBuf, uint16_t len);
* @return uint16_t. Value of @ref Sn_MSSR. * @return uint16_t. Value of @ref Sn_MSSR.
* @sa setSn_MSSR() * @sa setSn_MSSR()
*/ */
//M20150401 : Type explict declaration
/*
#define getSn_MSSR(sn) \ #define getSn_MSSR(sn) \
((WIZCHIP_READ(Sn_MSSR(sn)) << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(Sn_MSSR(sn),1))) ((WIZCHIP_READ(Sn_MSSR(sn)) << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(Sn_MSSR(sn),1)))
*/
#define getSn_MSSR(sn) \
(((uint16_t)WIZCHIP_READ(Sn_MSSR(sn)) << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(Sn_MSSR(sn),1)))
/** /**
* @ingroup Socket_register_access_function * @ingroup Socket_register_access_function
@ -1885,8 +1941,13 @@ uint16_t getSn_TX_FSR(uint8_t sn);
* @param (uint8_t)sn Socket number. It should be <b>0 ~ 7</b>. * @param (uint8_t)sn Socket number. It should be <b>0 ~ 7</b>.
* @return uint16_t. Value of @ref Sn_TX_RD. * @return uint16_t. Value of @ref Sn_TX_RD.
*/ */
//M20150401 : Type explict declaration
/*
#define getSn_TX_RD(sn) \ #define getSn_TX_RD(sn) \
((WIZCHIP_READ(Sn_TX_RD(sn)) << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(Sn_TX_RD(sn),1))) ((WIZCHIP_READ(Sn_TX_RD(sn)) << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(Sn_TX_RD(sn),1)))
*/
#define getSn_TX_RD(sn) \
(((uint16_t)WIZCHIP_READ(Sn_TX_RD(sn)) << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(Sn_TX_RD(sn),1)))
/** /**
* @ingroup Socket_register_access_function * @ingroup Socket_register_access_function
@ -1907,8 +1968,13 @@ uint16_t getSn_TX_FSR(uint8_t sn);
* @return uint16_t. Value of @ref Sn_TX_WR. * @return uint16_t. Value of @ref Sn_TX_WR.
* @sa setSn_TX_WR() * @sa setSn_TX_WR()
*/ */
//M20150401 : Type explict declaration
/*
#define getSn_TX_WR(sn) \ #define getSn_TX_WR(sn) \
((WIZCHIP_READ(Sn_TX_WR(sn)) << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(Sn_TX_WR(sn),1))) ((WIZCHIP_READ(Sn_TX_WR(sn)) << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(Sn_TX_WR(sn),1)))
*/
#define getSn_TX_WR(sn) \
(((uint16_t)WIZCHIP_READ(Sn_TX_WR(sn)) << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(Sn_TX_WR(sn),1)))
/** /**
@ -1936,11 +2002,16 @@ uint16_t getSn_RX_RSR(uint8_t sn);
* @ingroup Socket_register_access_function * @ingroup Socket_register_access_function
* @brief Get @ref Sn_RX_RD register * @brief Get @ref Sn_RX_RD register
* @param (uint8_t)sn Socket number. It should be <b>0 ~ 7</b>. * @param (uint8_t)sn Socket number. It should be <b>0 ~ 7</b>.
* @regurn uint16_t. Value of @ref Sn_RX_RD. * @return uint16_t. Value of @ref Sn_RX_RD.
* @sa setSn_RX_RD() * @sa setSn_RX_RD()
*/ */
//M20150401 : Type explict declaration
/*
#define getSn_RX_RD(sn) \ #define getSn_RX_RD(sn) \
((WIZCHIP_READ(Sn_RX_RD(sn)) << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(Sn_RX_RD(sn),1))) ((WIZCHIP_READ(Sn_RX_RD(sn)) << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(Sn_RX_RD(sn),1)))
*/
#define getSn_RX_RD(sn) \
(((uint16_t)WIZCHIP_READ(Sn_RX_RD(sn)) << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(Sn_RX_RD(sn),1)))
/** /**
* @ingroup Socket_register_access_function * @ingroup Socket_register_access_function
@ -1948,9 +2019,13 @@ uint16_t getSn_RX_RSR(uint8_t sn);
* @param (uint8_t)sn Socket number. It should be <b>0 ~ 7</b>. * @param (uint8_t)sn Socket number. It should be <b>0 ~ 7</b>.
* @return uint16_t. Value of @ref Sn_RX_WR. * @return uint16_t. Value of @ref Sn_RX_WR.
*/ */
//M20150401 : Type explict declaration
/*
#define getSn_RX_WR(sn) \ #define getSn_RX_WR(sn) \
((WIZCHIP_READ(Sn_RX_WR(sn)) << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(Sn_RX_WR(sn),1))) ((WIZCHIP_READ(Sn_RX_WR(sn)) << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(Sn_RX_WR(sn),1)))
*/
#define getSn_RX_WR(sn) \
(((uint16_t)WIZCHIP_READ(Sn_RX_WR(sn)) << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(Sn_RX_WR(sn),1)))
/** /**
* @ingroup Socket_register_access_function * @ingroup Socket_register_access_function
@ -1971,8 +2046,13 @@ uint16_t getSn_RX_RSR(uint8_t sn);
* @return uint16_t. Value of @ref Sn_FRAG. * @return uint16_t. Value of @ref Sn_FRAG.
* @sa setSn_FRAG() * @sa setSn_FRAG()
*/ */
//M20150401 : Type explict declaration
/*
#define getSn_FRAG(sn) \ #define getSn_FRAG(sn) \
((WIZCHIP_READ(Sn_FRAG(sn)) << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(Sn_FRAG(sn),1))) ((WIZCHIP_READ(Sn_FRAG(sn)) << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(Sn_FRAG(sn),1)))
*/
#define getSn_FRAG(sn) \
(((uint16_t)WIZCHIP_READ(Sn_FRAG(sn)) << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(Sn_FRAG(sn),1)))
/** /**
* @ingroup Socket_register_access_function * @ingroup Socket_register_access_function
@ -2000,21 +2080,32 @@ uint16_t getSn_RX_RSR(uint8_t sn);
// Sn_TXBUF & Sn_RXBUF IO function // // Sn_TXBUF & Sn_RXBUF IO function //
///////////////////////////////////// /////////////////////////////////////
/** /**
* @brief Socket_register_access_function
* @brief Gets the max buffer size of socket sn passed as parameter. * @brief Gets the max buffer size of socket sn passed as parameter.
* @param (uint8_t)sn Socket number. It should be <b>0 ~ 7</b>. * @param (uint8_t)sn Socket number. It should be <b>0 ~ 7</b>.
* @return uint16_t. Value of Socket n RX max buffer size. * @return uint16_t. Value of Socket n RX max buffer size.
*/ */
//M20150401 : Type explict declaration
/*
#define getSn_RxMAX(sn) \ #define getSn_RxMAX(sn) \
(getSn_RXBUF_SIZE(sn) << 10) (getSn_RXBUF_SIZE(sn) << 10)
*/
#define getSn_RxMAX(sn) \
(((uint16_t)getSn_RXBUF_SIZE(sn)) << 10)
/** /**
* @brief Socket_register_access_function
* @brief Gets the max buffer size of socket sn passed as parameters. * @brief Gets the max buffer size of socket sn passed as parameters.
* @param (uint8_t)sn Socket number. It should be <b>0 ~ 7</b>. * @param (uint8_t)sn Socket number. It should be <b>0 ~ 7</b>.
* @return uint16_t. Value of Socket n TX max buffer size. * @return uint16_t. Value of Socket n TX max buffer size.
*/ */
//uint16_t getSn_TxMAX(uint8_t sn); //M20150401 : Type explict declaration
/*
#define getSn_TxMAX(sn) \ #define getSn_TxMAX(sn) \
(getSn_TXBUF_SIZE(sn) << 10) (getSn_TXBUF_SIZE(sn) << 10)
*/
#define getSn_TxMAX(sn) \
(((uint16_t)getSn_TXBUF_SIZE(sn)) << 10)
/** /**
* @ingroup Basic_IO_function * @ingroup Basic_IO_function
@ -2025,7 +2116,6 @@ uint16_t getSn_RX_RSR(uint8_t sn);
* and updates the Tx write pointer register. * and updates the Tx write pointer register.
* This function is being called by send() and sendto() function also. * This function is being called by send() and sendto() function also.
* *
* @note User should read upper byte first and lower byte later to get proper value.
* @param (uint8_t)sn Socket number. It should be <b>0 ~ 7</b>. * @param (uint8_t)sn Socket number. It should be <b>0 ~ 7</b>.
* @param wizdata Pointer buffer to write data * @param wizdata Pointer buffer to write data
* @param len Data length * @param len Data length
@ -2042,7 +2132,6 @@ void wiz_send_data(uint8_t sn, uint8_t *wizdata, uint16_t len);
* to <i>wizdata(pointer variable)</i> of the length of <i>len(variable)</i> bytes. * to <i>wizdata(pointer variable)</i> of the length of <i>len(variable)</i> bytes.
* This function is being called by recv() also. * This function is being called by recv() also.
* *
* @note User should read upper byte first and lower byte later to get proper value.
* @param (uint8_t)sn Socket number. It should be <b>0 ~ 7</b>. * @param (uint8_t)sn Socket number. It should be <b>0 ~ 7</b>.
* @param wizdata Pointer buffer to read data * @param wizdata Pointer buffer to read data
* @param len Data length * @param len Data length
@ -2059,4 +2148,8 @@ void wiz_recv_data(uint8_t sn, uint8_t *wizdata, uint16_t len);
*/ */
void wiz_recv_ignore(uint8_t sn, uint16_t len); void wiz_recv_ignore(uint8_t sn, uint16_t len);
/// @cond DOXY_APPLY_CODE
#endif
/// @endcond
#endif // _W5500_H_ #endif // _W5500_H_

View File

@ -55,18 +55,31 @@
//***************************************************************************** //*****************************************************************************
#include "socket.h" #include "socket.h"
#define SOCK_ANY_PORT_NUM 0xC000; //M20150401 : Typing Error
//#define SOCK_ANY_PORT_NUM 0xC000;
#define SOCK_ANY_PORT_NUM 0xC000
static uint16_t sock_any_port = SOCK_ANY_PORT_NUM; static uint16_t sock_any_port = SOCK_ANY_PORT_NUM;
static uint16_t sock_io_mode = 0; static uint16_t sock_io_mode = 0;
static uint16_t sock_is_sending = 0; static uint16_t sock_is_sending = 0;
static uint16_t sock_remained_size[_WIZCHIP_SOCK_NUM_] = {0,0,}; static uint16_t sock_remained_size[_WIZCHIP_SOCK_NUM_] = {0,0,};
static uint8_t sock_pack_info[_WIZCHIP_SOCK_NUM_] = {0,};
//M20150601 : For extern decleation
//static uint8_t sock_pack_info[_WIZCHIP_SOCK_NUM_] = {0,};
uint8_t sock_pack_info[_WIZCHIP_SOCK_NUM_] = {0,};
//
#if _WIZCHIP_ == 5200 #if _WIZCHIP_ == 5200
static uint16_t sock_next_rd[_WIZCHIP_SOCK_NUM_] ={0,}; static uint16_t sock_next_rd[_WIZCHIP_SOCK_NUM_] ={0,};
#endif #endif
//A20150601 : For integrating with W5300
#if _WIZCHIP_ == 5300
uint8_t sock_remained_byte[_WIZCHIP_SOCK_NUM_] = {0,}; // set by wiz_recv_data()
#endif
#define CHECK_SOCKNUM() \ #define CHECK_SOCKNUM() \
do{ \ do{ \
if(sn > _WIZCHIP_SOCK_NUM_) return SOCKERR_SOCKNUM; \ if(sn > _WIZCHIP_SOCK_NUM_) return SOCKERR_SOCKNUM; \
@ -95,6 +108,16 @@ int8_t socket(uint8_t sn, uint8_t protocol, uint16_t port, uint8_t flag)
switch(protocol) switch(protocol)
{ {
case Sn_MR_TCP : case Sn_MR_TCP :
{
//M20150601 : Fixed the warning - taddr will never be NULL
/*
uint8_t taddr[4];
getSIPR(taddr);
*/
uint32_t taddr;
getSIPR((uint8_t*)&taddr);
if(taddr == 0) return SOCKERR_SOCKINIT;
}
case Sn_MR_UDP : case Sn_MR_UDP :
case Sn_MR_MACRAW : case Sn_MR_MACRAW :
break; break;
@ -106,7 +129,9 @@ int8_t socket(uint8_t sn, uint8_t protocol, uint16_t port, uint8_t flag)
default : default :
return SOCKERR_SOCKMODE; return SOCKERR_SOCKMODE;
} }
if((flag & 0x06) != 0) return SOCKERR_SOCKFLAG; //M20150601 : For SF_TCP_ALIGN & W5300
//if((flag & 0x06) != 0) return SOCKERR_SOCKFLAG;
if((flag & 0x04) != 0) return SOCKERR_SOCKFLAG;
#if _WIZCHIP_ == 5200 #if _WIZCHIP_ == 5200
if(flag & 0x10) return SOCKERR_SOCKFLAG; if(flag & 0x10) return SOCKERR_SOCKFLAG;
#endif #endif
@ -116,7 +141,13 @@ int8_t socket(uint8_t sn, uint8_t protocol, uint16_t port, uint8_t flag)
switch(protocol) switch(protocol)
{ {
case Sn_MR_TCP: case Sn_MR_TCP:
//M20150601 : For SF_TCP_ALIGN & W5300
#if _WIZCHIP_ == 5300
if((flag & (SF_TCP_NODELAY|SF_IO_NONBLOCK|SF_TCP_ALIGN))==0) return SOCKERR_SOCKFLAG;
#else
if((flag & (SF_TCP_NODELAY|SF_IO_NONBLOCK))==0) return SOCKERR_SOCKFLAG; if((flag & (SF_TCP_NODELAY|SF_IO_NONBLOCK))==0) return SOCKERR_SOCKFLAG;
#endif
break; break;
case Sn_MR_UDP: case Sn_MR_UDP:
if(flag & SF_IGMP_VER2) if(flag & SF_IGMP_VER2)
@ -135,7 +166,12 @@ int8_t socket(uint8_t sn, uint8_t protocol, uint16_t port, uint8_t flag)
} }
} }
close(sn); close(sn);
//M20150601
#if _WIZCHIP_ == 5300
setSn_MR(sn, ((uint16_t)(protocol | (flag & 0xF0))) | (((uint16_t)(flag & 0x02)) << 7) );
#else
setSn_MR(sn, (protocol | (flag & 0xF0))); setSn_MR(sn, (protocol | (flag & 0xF0)));
#endif
if(!port) if(!port)
{ {
port = sock_any_port++; port = sock_any_port++;
@ -144,10 +180,16 @@ int8_t socket(uint8_t sn, uint8_t protocol, uint16_t port, uint8_t flag)
setSn_PORT(sn,port); setSn_PORT(sn,port);
setSn_CR(sn,Sn_CR_OPEN); setSn_CR(sn,Sn_CR_OPEN);
while(getSn_CR(sn)); while(getSn_CR(sn));
//A20150401 : For release the previous sock_io_mode
sock_io_mode &= ~(1 <<sn);
//
sock_io_mode |= ((flag & SF_IO_NONBLOCK) << sn); sock_io_mode |= ((flag & SF_IO_NONBLOCK) << sn);
sock_is_sending &= ~(1<<sn); sock_is_sending &= ~(1<<sn);
sock_remained_size[sn] = 0; sock_remained_size[sn] = 0;
sock_pack_info[sn] = 0; //M20150601 : repalce 0 with PACK_COMPLETED
//sock_pack_info[sn] = 0;
sock_pack_info[sn] = PACK_COMPLETED;
//
while(getSn_SR(sn) == SOCK_CLOSED); while(getSn_SR(sn) == SOCK_CLOSED);
return (int8_t)sn; return (int8_t)sn;
} }
@ -161,6 +203,9 @@ int8_t close(uint8_t sn)
while( getSn_CR(sn) ); while( getSn_CR(sn) );
/* clear all interrupt of the socket. */ /* clear all interrupt of the socket. */
setSn_IR(sn, 0xFF); setSn_IR(sn, 0xFF);
//A20150401 : Release the sock_io_mode of socket n.
sock_io_mode &= ~(1<<sn);
//
sock_is_sending &= ~(1<<sn); sock_is_sending &= ~(1<<sn);
sock_remained_size[sn] = 0; sock_remained_size[sn] = 0;
sock_pack_info[sn] = 0; sock_pack_info[sn] = 0;
@ -207,9 +252,6 @@ int8_t connect(uint8_t sn, uint8_t * addr, uint16_t port)
if(port == 0) return SOCKERR_PORTZERO; if(port == 0) return SOCKERR_PORTZERO;
setSn_DIPR(sn,addr); setSn_DIPR(sn,addr);
setSn_DPORT(sn,port); setSn_DPORT(sn,port);
#if _WIZCHIP_ == 5200 // for W5200 ARP errata
setSUBR(0);
#endif
setSn_CR(sn,Sn_CR_CONNECT); setSn_CR(sn,Sn_CR_CONNECT);
while(getSn_CR(sn)); while(getSn_CR(sn));
if(sock_io_mode & (1<<sn)) return SOCK_BUSY; if(sock_io_mode & (1<<sn)) return SOCK_BUSY;
@ -218,9 +260,6 @@ int8_t connect(uint8_t sn, uint8_t * addr, uint16_t port)
if (getSn_IR(sn) & Sn_IR_TIMEOUT) if (getSn_IR(sn) & Sn_IR_TIMEOUT)
{ {
setSn_IR(sn, Sn_IR_TIMEOUT); setSn_IR(sn, Sn_IR_TIMEOUT);
#if _WIZCHIP_ == 5200 // for W5200 ARP errata
setSUBR((uint8_t*)"\x00\x00\x00\x00");
#endif
return SOCKERR_TIMEOUT; return SOCKERR_TIMEOUT;
} }
@ -229,9 +268,6 @@ int8_t connect(uint8_t sn, uint8_t * addr, uint16_t port)
return SOCKERR_SOCKCLOSED; return SOCKERR_SOCKCLOSED;
} }
} }
#if _WIZCHIP_ == 5200 // for W5200 ARP errata
setSUBR((uint8_t*)"\x00\x00\x00\x00");
#endif
return SOCK_OK; return SOCK_OK;
} }
@ -272,12 +308,14 @@ int32_t send(uint8_t sn, uint8_t * buf, uint16_t len)
if(tmp & Sn_IR_SENDOK) if(tmp & Sn_IR_SENDOK)
{ {
setSn_IR(sn, Sn_IR_SENDOK); setSn_IR(sn, Sn_IR_SENDOK);
#if _WZICHIP_ == 5200 //M20150401 : Typing Error
//#if _WZICHIP_ == 5200
#if _WIZCHIP_ == 5200
if(getSn_TX_RD(sn) != sock_next_rd[sn]) if(getSn_TX_RD(sn) != sock_next_rd[sn])
{ {
setSn_CR(sn,Sn_CR_SEND); setSn_CR(sn,Sn_CR_SEND);
while(getSn_CR(sn)); while(getSn_CR(sn));
return SOCKERR_BUSY; return SOCK_BUSY;
} }
#endif #endif
sock_is_sending &= ~(1<<sn); sock_is_sending &= ~(1<<sn);
@ -307,11 +345,18 @@ int32_t send(uint8_t sn, uint8_t * buf, uint16_t len)
#if _WIZCHIP_ == 5200 #if _WIZCHIP_ == 5200
sock_next_rd[sn] = getSn_TX_RD(sn) + len; sock_next_rd[sn] = getSn_TX_RD(sn) + len;
#endif #endif
#if _WIZCHIP_ == 5300
setSn_TX_WRSR(sn,len);
#endif
setSn_CR(sn,Sn_CR_SEND); setSn_CR(sn,Sn_CR_SEND);
/* wait to process the command... */ /* wait to process the command... */
while(getSn_CR(sn)); while(getSn_CR(sn));
sock_is_sending |= (1 << sn); sock_is_sending |= (1 << sn);
return len; //M20150409 : Explicit Type Casting
//return len;
return (int32_t)len;
} }
@ -319,12 +364,26 @@ int32_t recv(uint8_t sn, uint8_t * buf, uint16_t len)
{ {
uint8_t tmp = 0; uint8_t tmp = 0;
uint16_t recvsize = 0; uint16_t recvsize = 0;
//A20150601 : For integarating with W5300
#if _WIZCHIP_ == 5300
uint8_t head[2];
uint16_t mr;
#endif
//
CHECK_SOCKNUM(); CHECK_SOCKNUM();
CHECK_SOCKMODE(Sn_MR_TCP); CHECK_SOCKMODE(Sn_MR_TCP);
CHECK_SOCKDATA(); CHECK_SOCKDATA();
recvsize = getSn_RxMAX(sn); recvsize = getSn_RxMAX(sn);
if(recvsize < len) len = recvsize; if(recvsize < len) len = recvsize;
//A20150601 : For Integrating with W5300
#if _WIZCHIP_ == 5300
//sock_pack_info[sn] = PACK_COMPLETED; // for clear
if(sock_remained_size[sn] == 0)
{
#endif
//
while(1) while(1)
{ {
recvsize = getSn_RX_RSR(sn); recvsize = getSn_RX_RSR(sn);
@ -349,11 +408,61 @@ int32_t recv(uint8_t sn, uint8_t * buf, uint16_t len)
if((sock_io_mode & (1<<sn)) && (recvsize == 0)) return SOCK_BUSY; if((sock_io_mode & (1<<sn)) && (recvsize == 0)) return SOCK_BUSY;
if(recvsize != 0) break; if(recvsize != 0) break;
}; };
#if _WIZCHIP_ == 5300
}
#endif
//A20150601 : For integrating with W5300
#if _WIZCHIP_ == 5300
if((sock_remained_size[sn] == 0) || (getSn_MR(sn) & Sn_MR_ALIGN))
{
mr = getMR();
if((getSn_MR(sn) & Sn_MR_ALIGN)==0)
{
wiz_recv_data(sn,head,2);
if(mr & MR_FS)
recvsize = (((uint16_t)head[1]) << 8) | ((uint16_t)head[0]);
else
recvsize = (((uint16_t)head[0]) << 8) | ((uint16_t)head[1]);
sock_pack_info[sn] = PACK_FIRST;
}
sock_remained_size[sn] = recvsize;
}
if(len > sock_remained_size[sn]) len = sock_remained_size[sn];
recvsize = len;
if(sock_pack_info[sn] & PACK_FIFOBYTE)
{
*buf = sock_remained_byte[sn];
buf++;
sock_pack_info[sn] &= ~(PACK_FIFOBYTE);
recvsize -= 1;
sock_remained_size[sn] -= 1;
}
if(recvsize != 0)
{
wiz_recv_data(sn, buf, recvsize);
setSn_CR(sn,Sn_CR_RECV);
while(getSn_CR(sn));
}
sock_remained_size[sn] -= recvsize;
if(sock_remained_size[sn] != 0)
{
sock_pack_info[sn] |= PACK_REMAINED;
if(recvsize & 0x1) sock_pack_info[sn] |= PACK_FIFOBYTE;
}
else sock_pack_info[sn] = PACK_COMPLETED;
if(getSn_MR(sn) & Sn_MR_ALIGN) sock_remained_size[sn] = 0;
//len = recvsize;
#else
if(recvsize < len) len = recvsize; if(recvsize < len) len = recvsize;
wiz_recv_data(sn, buf, len); wiz_recv_data(sn, buf, len);
setSn_CR(sn,Sn_CR_RECV); setSn_CR(sn,Sn_CR_RECV);
while(getSn_CR(sn)); while(getSn_CR(sn));
return len; #endif
//M20150409 : Explicit Type Casting
//return len;
return (int32_t)len;
} }
int32_t sendto(uint8_t sn, uint8_t * buf, uint16_t len, uint8_t * addr, uint16_t port) int32_t sendto(uint8_t sn, uint8_t * buf, uint16_t len, uint8_t * addr, uint16_t port)
@ -401,16 +510,24 @@ int32_t sendto(uint8_t sn, uint8_t * buf, uint16_t len, uint8_t * addr, uint16_t
}; };
wiz_send_data(sn, buf, len); wiz_send_data(sn, buf, len);
#if _WIZCHIP_ == 5200 // for W5200 ARP errata #if _WIZCHIP_ < 5500 //M20150401 : for WIZCHIP Errata #4, #5 (ARP errata)
setSUBR(0); getSIPR((uint8_t*)&taddr);
if(taddr == 0)
{
getSUBR((uint8_t*)&taddr);
setSUBR((uint8_t*)"\x00\x00\x00\x00");
}
else taddr = 0;
#endif #endif
//A20150601 : For W5300
#if _WIZCHIP_ == 5300
setSn_TX_WRSR(sn, len);
#endif
//
setSn_CR(sn,Sn_CR_SEND); setSn_CR(sn,Sn_CR_SEND);
/* wait to process the command... */ /* wait to process the command... */
while(getSn_CR(sn)); while(getSn_CR(sn));
#if _WIZCHIP_ == 5200 // for W5200 ARP errata
setSUBR((uint8_t*)"\x00\x00\x00\x00");
#endif
while(1) while(1)
{ {
tmp = getSn_IR(sn); tmp = getSn_IR(sn);
@ -424,23 +541,46 @@ int32_t sendto(uint8_t sn, uint8_t * buf, uint16_t len, uint8_t * addr, uint16_t
else if(tmp & Sn_IR_TIMEOUT) else if(tmp & Sn_IR_TIMEOUT)
{ {
setSn_IR(sn, Sn_IR_TIMEOUT); setSn_IR(sn, Sn_IR_TIMEOUT);
//M20150409 : Fixed the lost of sign bits by type casting.
//len = (uint16_t)SOCKERR_TIMEOUT;
//break;
#if _WIZCHIP_ < 5500 //M20150401 : for WIZCHIP Errata #4, #5 (ARP errata)
if(taddr) setSUBR((uint8_t*)&taddr);
#endif
return SOCKERR_TIMEOUT; return SOCKERR_TIMEOUT;
} }
//////////// ////////////
} }
return len; #if _WIZCHIP_ < 5500 //M20150401 : for WIZCHIP Errata #4, #5 (ARP errata)
if(taddr) setSUBR((uint8_t*)&taddr);
#endif
//M20150409 : Explicit Type Casting
//return len;
return (int32_t)len;
} }
int32_t recvfrom(uint8_t sn, uint8_t * buf, uint16_t len, uint8_t * addr, uint16_t *port) int32_t recvfrom(uint8_t sn, uint8_t * buf, uint16_t len, uint8_t * addr, uint16_t *port)
{ {
//M20150601 : For W5300
#if _WIZCHIP_ == 5300
uint16_t mr;
uint16_t mr1;
#else
uint8_t mr; uint8_t mr;
#endif
//
uint8_t head[8]; uint8_t head[8];
uint16_t pack_len=0; uint16_t pack_len=0;
CHECK_SOCKNUM(); CHECK_SOCKNUM();
//CHECK_SOCKMODE(Sn_MR_UDP); //CHECK_SOCKMODE(Sn_MR_UDP);
//A20150601
#if _WIZCHIP_ == 5300
mr1 = getMR();
#endif
switch((mr=getSn_MR(sn)) & 0x0F) switch((mr=getSn_MR(sn)) & 0x0F)
{ {
case Sn_MR_UDP: case Sn_MR_UDP:
@ -465,7 +605,8 @@ int32_t recvfrom(uint8_t sn, uint8_t * buf, uint16_t len, uint8_t * addr, uint16
if(pack_len != 0) break; if(pack_len != 0) break;
}; };
} }
sock_pack_info[sn] = PACK_COMPLETED; //D20150601 : Move it to bottom
// sock_pack_info[sn] = PACK_COMPLETED;
switch (mr & 0x07) switch (mr & 0x07)
{ {
case Sn_MR_UDP : case Sn_MR_UDP :
@ -475,6 +616,22 @@ int32_t recvfrom(uint8_t sn, uint8_t * buf, uint16_t len, uint8_t * addr, uint16
setSn_CR(sn,Sn_CR_RECV); setSn_CR(sn,Sn_CR_RECV);
while(getSn_CR(sn)); while(getSn_CR(sn));
// read peer's IP address, port number & packet length // read peer's IP address, port number & packet length
//A20150601 : For W5300
#if _WIZCHIP_ == 5300
if(mr1 & MR_FS)
{
addr[0] = head[1];
addr[1] = head[0];
addr[2] = head[3];
addr[3] = head[2];
*port = head[5];
*port = (*port << 8) + head[4];
sock_remained_size[sn] = head[7];
sock_remained_size[sn] = (sock_remained_size[sn] << 8) + head[6];
}
else
{
#endif
addr[0] = head[0]; addr[0] = head[0];
addr[1] = head[1]; addr[1] = head[1];
addr[2] = head[2]; addr[2] = head[2];
@ -483,10 +640,24 @@ int32_t recvfrom(uint8_t sn, uint8_t * buf, uint16_t len, uint8_t * addr, uint16
*port = (*port << 8) + head[5]; *port = (*port << 8) + head[5];
sock_remained_size[sn] = head[6]; sock_remained_size[sn] = head[6];
sock_remained_size[sn] = (sock_remained_size[sn] << 8) + head[7]; sock_remained_size[sn] = (sock_remained_size[sn] << 8) + head[7];
#if _WIZCHIP_ == 5300
}
#endif
sock_pack_info[sn] = PACK_FIRST; sock_pack_info[sn] = PACK_FIRST;
} }
if(len < sock_remained_size[sn]) pack_len = len; if(len < sock_remained_size[sn]) pack_len = len;
else pack_len = sock_remained_size[sn]; else pack_len = sock_remained_size[sn];
//A20150601 : For W5300
len = pack_len;
#if _WIZCHIP_ == 5300
if(sock_pack_info[sn] & PACK_FIFOBYTE)
{
*buf++ = sock_remained_byte[sn];
pack_len -= 1;
sock_remained_size[sn] -= 1;
sock_pack_info[sn] &= ~PACK_FIFOBYTE;
}
#endif
// //
// Need to packet length check (default 1472) // Need to packet length check (default 1472)
// //
@ -524,7 +695,9 @@ int32_t recvfrom(uint8_t sn, uint8_t * buf, uint16_t len, uint8_t * addr, uint16
addr[2] = head[2]; addr[2] = head[2];
addr[3] = head[3]; addr[3] = head[3];
sock_remained_size[sn] = head[4]; sock_remained_size[sn] = head[4];
sock_remaiend_size[sn] = (sock_remained_size[sn] << 8) + head[5]; //M20150401 : For Typing Error
//sock_remaiend_size[sn] = (sock_remained_size[sn] << 8) + head[5];
sock_remained_size[sn] = (sock_remained_size[sn] << 8) + head[5];
sock_pack_info[sn] = PACK_FIRST; sock_pack_info[sn] = PACK_FIRST;
} }
// //
@ -544,11 +717,23 @@ int32_t recvfrom(uint8_t sn, uint8_t * buf, uint16_t len, uint8_t * addr, uint16
/* wait to process the command... */ /* wait to process the command... */
while(getSn_CR(sn)) ; while(getSn_CR(sn)) ;
sock_remained_size[sn] -= pack_len; sock_remained_size[sn] -= pack_len;
//M20140501 : replace 0x01 with PACK_REMAINED //M20150601 :
//if(sock_remained_size[sn] != 0) sock_pack_info[sn] |= 0x01; //if(sock_remained_size[sn] != 0) sock_pack_info[sn] |= 0x01;
if(sock_remained_size[sn] != 0) sock_pack_info[sn] |= PACK_REMAINED; if(sock_remained_size[sn] != 0)
{
sock_pack_info[sn] |= PACK_REMAINED;
#if _WIZCHIP_ == 5300
if(pack_len & 0x01) sock_pack_info[sn] |= PACK_FIFOBYTE;
#endif
}
else sock_pack_info[sn] = PACK_COMPLETED;
#if _WIZCHIP_ == 5300
pack_len = len;
#endif
// //
return pack_len; //M20150409 : Explicit Type Casting
//return pack_len;
return (int32_t)pack_len;
} }
@ -583,6 +768,7 @@ int8_t ctlsocket(uint8_t sn, ctlsock_type cstype, void* arg)
case CS_GET_INTERRUPT: case CS_GET_INTERRUPT:
*((uint8_t*)arg) = getSn_IR(sn); *((uint8_t*)arg) = getSn_IR(sn);
break; break;
#if _WIZCHIP_ != 5100
case CS_SET_INTMASK: case CS_SET_INTMASK:
if( (*(uint8_t*)arg) > SIK_ALL) return SOCKERR_ARG; if( (*(uint8_t*)arg) > SIK_ALL) return SOCKERR_ARG;
setSn_IMR(sn,*(uint8_t*)arg); setSn_IMR(sn,*(uint8_t*)arg);
@ -590,6 +776,7 @@ int8_t ctlsocket(uint8_t sn, ctlsock_type cstype, void* arg)
case CS_GET_INTMASK: case CS_GET_INTMASK:
*((uint8_t*)arg) = getSn_IMR(sn); *((uint8_t*)arg) = getSn_IMR(sn);
break; break;
#endif
default: default:
return SOCKERR_ARG; return SOCKERR_ARG;
} }

View File

@ -96,7 +96,7 @@
#define SOCK_ERROR 0 #define SOCK_ERROR 0
#define SOCKERR_SOCKNUM (SOCK_ERROR - 1) ///< Invalid socket number #define SOCKERR_SOCKNUM (SOCK_ERROR - 1) ///< Invalid socket number
#define SOCKERR_SOCKOPT (SOCK_ERROR - 2) ///< Invalid socket option #define SOCKERR_SOCKOPT (SOCK_ERROR - 2) ///< Invalid socket option
#define SOCKERR_SOCKINIT (SOCK_ERROR - 3) ///< Socket is not initialized #define SOCKERR_SOCKINIT (SOCK_ERROR - 3) ///< Socket is not initialized or SIPR is Zero IP address when Sn_MR_TCP
#define SOCKERR_SOCKCLOSED (SOCK_ERROR - 4) ///< Socket unexpectedly closed. #define SOCKERR_SOCKCLOSED (SOCK_ERROR - 4) ///< Socket unexpectedly closed.
#define SOCKERR_SOCKMODE (SOCK_ERROR - 5) ///< Invalid socket mode for socket operation. #define SOCKERR_SOCKMODE (SOCK_ERROR - 5) ///< Invalid socket mode for socket operation.
#define SOCKERR_SOCKFLAG (SOCK_ERROR - 6) ///< Invalid socket flag #define SOCKERR_SOCKFLAG (SOCK_ERROR - 6) ///< Invalid socket flag
@ -113,16 +113,21 @@
/* /*
* SOCKET FLAG * SOCKET FLAG
*/ */
#define SF_ETHER_OWN (Sn_MR_MFEN) ///< In \ref Sn_MR_MACRAW, Receive only the packet as broadcast, multicast and own packet #define SF_ETHER_OWN (Sn_MR_MFEN) ///< In @ref Sn_MR_MACRAW, Receive only the packet as broadcast, multicast and own packet
#define SF_IGMP_VER2 (Sn_MR_MC) ///< In \ref Sn_MR_UDP with \ref SF_MULTI_ENABLE, Select IGMP version 2. #define SF_IGMP_VER2 (Sn_MR_MC) ///< In @ref Sn_MR_UDP with \ref SF_MULTI_ENABLE, Select IGMP version 2.
#define SF_TCP_NODELAY (Sn_MR_ND) ///< In \ref Sn_MR_TCP, Use to nodelayed ack. #define SF_TCP_NODELAY (Sn_MR_ND) ///< In @ref Sn_MR_TCP, Use to nodelayed ack.
#define SF_MULTI_ENABLE (Sn_MR_MULTI) ///< In \ref Sn_MR_UDP, Enable multicast mode. #define SF_MULTI_ENABLE (Sn_MR_MULTI) ///< In @ref Sn_MR_UDP, Enable multicast mode.
#if _WIZCHIP_ == 5500 #if _WIZCHIP_ == 5500
#define SF_BROAD_BLOCK (Sn_MR_BCASTB) ///< In \ref Sn_MR_UDP or \ref Sn_MR_MACRAW, Block broadcast packet. Valid only in W5500 #define SF_BROAD_BLOCK (Sn_MR_BCASTB) ///< In @ref Sn_MR_UDP or @ref Sn_MR_MACRAW, Block broadcast packet. Valid only in W5500
#define SF_MULTI_BLOCK (Sn_MR_MMB) ///< In \ref Sn_MR_MACRAW, Block multicast packet. Valid only in W5500 #define SF_MULTI_BLOCK (Sn_MR_MMB) ///< In @ref Sn_MR_MACRAW, Block multicast packet. Valid only in W5500
#define SF_IPv6_BLOCK (Sn_MR_MIP6B) ///< In \ref Sn_MR_MACRAW, Block IPv6 packet. Valid only in W5500 #define SF_IPv6_BLOCK (Sn_MR_MIP6B) ///< In @ref Sn_MR_MACRAW, Block IPv6 packet. Valid only in W5500
#define SF_UNI_BLOCK (Sn_MR_UCASTB) ///< In \ref Sn_MR_UDP with \ref SF_MULTI_ENABLE. Valid only in W5500 #define SF_UNI_BLOCK (Sn_MR_UCASTB) ///< In @ref Sn_MR_UDP with \ref SF_MULTI_ENABLE. Valid only in W5500
#endif
//A201505 : For W5300
#if _WIZCHIP_ == 5300
#define SF_TCP_ALIGN 0x02 ///< Valid only \ref Sn_MR_TCP and W5300, refer to \ref Sn_MR_ALIGN
#endif #endif
#define SF_IO_NONBLOCK 0x01 ///< Socket nonblock io mode. It used parameter in \ref socket(). #define SF_IO_NONBLOCK 0x01 ///< Socket nonblock io mode. It used parameter in \ref socket().
@ -130,9 +135,12 @@
/* /*
* UDP & MACRAW Packet Infomation * UDP & MACRAW Packet Infomation
*/ */
#define PACK_FIRST 0x80 ///< In Non-TCP packet, It indicates to start receiving a packet. #define PACK_FIRST 0x80 ///< In Non-TCP packet, It indicates to start receiving a packet. (When W5300, This flag can be applied)
#define PACK_REMAINED 0x01 ///< In Non-TCP packet, It indicates to remain a packet to be received. #define PACK_REMAINED 0x01 ///< In Non-TCP packet, It indicates to remain a packet to be received. (When W5300, This flag can be applied)
#define PACK_COMPLETED 0x00 ///< In Non-TCP packet, It indicates to complete to receive a packet. #define PACK_COMPLETED 0x00 ///< In Non-TCP packet, It indicates to complete to receive a packet. (When W5300, This flag can be applied)
//A20150601 : For Integrating with W5300
#define PACK_FIFOBYTE 0x02 ///< Valid only W5300, It indicate to have read already the Sn_RX_FIFOR.
//
/** /**
* @ingroup WIZnet_socket_APIs * @ingroup WIZnet_socket_APIs
@ -341,7 +349,9 @@ typedef enum
SIK_RECEIVED = (1 << 2), ///< data received SIK_RECEIVED = (1 << 2), ///< data received
SIK_TIMEOUT = (1 << 3), ///< timeout occurred SIK_TIMEOUT = (1 << 3), ///< timeout occurred
SIK_SENT = (1 << 4), ///< send ok SIK_SENT = (1 << 4), ///< send ok
SIK_ALL = 0x1F, ///< all interrupt //M20150410 : Remove the comma of last member
//SIK_ALL = 0x1F, ///< all interrupt
SIK_ALL = 0x1F ///< all interrupt
}sockint_kind; }sockint_kind;
/** /**
@ -356,8 +366,10 @@ typedef enum
CS_GET_MAXRXBUF, ///< get the size of socket buffer allocated in RX memory CS_GET_MAXRXBUF, ///< get the size of socket buffer allocated in RX memory
CS_CLR_INTERRUPT, ///< clear the interrupt of socket with @ref sockint_kind CS_CLR_INTERRUPT, ///< clear the interrupt of socket with @ref sockint_kind
CS_GET_INTERRUPT, ///< get the socket interrupt. refer to @ref sockint_kind CS_GET_INTERRUPT, ///< get the socket interrupt. refer to @ref sockint_kind
CS_SET_INTMASK, ///< set the interrupt mask of socket with @ref sockint_kind #if _WIZCHIP_ > 5100
CS_GET_INTMASK ///< get the masked interrupt of socket. refer to @ref sockint_kind CS_SET_INTMASK, ///< set the interrupt mask of socket with @ref sockint_kind, Not supported in W5100
CS_GET_INTMASK ///< get the masked interrupt of socket. refer to @ref sockint_kind, Not supported in W5100
#endif
}ctlsock_type; }ctlsock_type;
@ -368,15 +380,15 @@ typedef enum
typedef enum typedef enum
{ {
SO_FLAG, ///< Valid only in getsockopt(), For set flag of socket refer to <I>flag</I> in @ref socket(). SO_FLAG, ///< Valid only in getsockopt(), For set flag of socket refer to <I>flag</I> in @ref socket().
SO_TTL, ///< Set/Get TTL. @ref Sn_TTL ( @ref setSn_TTL(), @ref getSn_TTL() ) SO_TTL, ///< Set TTL. @ref Sn_TTL ( @ref setSn_TTL(), @ref getSn_TTL() )
SO_TOS, ///< Set/Get TOS. @ref Sn_TOS ( @ref setSn_TOS(), @ref getSn_TOS() ) SO_TOS, ///< Set TOS. @ref Sn_TOS ( @ref setSn_TOS(), @ref getSn_TOS() )
SO_MSS, ///< Set/Get MSS. @ref Sn_MSSR ( @ref setSn_MSSR(), @ref getSn_MSSR() ) SO_MSS, ///< Set MSS. @ref Sn_MSSR ( @ref setSn_MSSR(), @ref getSn_MSSR() )
SO_DESTIP, ///< Set/Get the destination IP address. @ref Sn_DIPR ( @ref setSn_DIPR(), @ref getSn_DIPR() ) SO_DESTIP, ///< Set the destination IP address. @ref Sn_DIPR ( @ref setSn_DIPR(), @ref getSn_DIPR() )
SO_DESTPORT, ///< Set/Get the destination Port number. @ref Sn_DPORT ( @ref setSn_DPORT(), @ref getSn_DPORT() ) SO_DESTPORT, ///< Set the destination Port number. @ref Sn_DPORT ( @ref setSn_DPORT(), @ref getSn_DPORT() )
#if _WIZCHIP_ != 5100 #if _WIZCHIP_ != 5100
SO_KEEPALIVESEND, ///< Valid only in setsockopt. Manually send keep-alive packet in TCP mode SO_KEEPALIVESEND, ///< Valid only in setsockopt. Manually send keep-alive packet in TCP mode, Not supported in W5100
#if _WIZCHIP_ > 5200 #if _WIZCHIP_ > 5200
SO_KEEPALIVEAUTO, ///< Set/Get keep-alive auto transmission timer in TCP mode SO_KEEPALIVEAUTO, ///< Set/Get keep-alive auto transmission timer in TCP mode, Not supported in W5100, W5200
#endif #endif
#endif #endif
SO_SENDBUF, ///< Valid only in getsockopt. Get the free data size of Socekt TX buffer. @ref Sn_TX_FSR, @ref getSn_TX_FSR() SO_SENDBUF, ///< Valid only in getsockopt. Get the free data size of Socekt TX buffer. @ref Sn_TX_FSR, @ref getSn_TX_FSR()

View File

@ -10,7 +10,7 @@
//! Download the latest version directly from GitHub. Please visit the our GitHub repository for ioLibrary. //! Download the latest version directly from GitHub. Please visit the our GitHub repository for ioLibrary.
//! >> https://github.com/Wiznet/ioLibrary_Driver //! >> https://github.com/Wiznet/ioLibrary_Driver
//! <2014/05/01> V1.0.1 Refer to M20140501 //! <2014/05/01> V1.0.1 Refer to M20140501
//! 1. Explicit type casting in wizchip_bus_readbyte() & wizchip_bus_writebyte() //! 1. Explicit type casting in wizchip_bus_readdata() & wizchip_bus_writedata()
// Issued by Mathias ClauBen. // Issued by Mathias ClauBen.
//! uint32_t type converts into ptrdiff_t first. And then recoverting it into uint8_t* //! uint32_t type converts into ptrdiff_t first. And then recoverting it into uint8_t*
//! For remove the warning when pointer type size is not 32bit. //! For remove the warning when pointer type size is not 32bit.
@ -53,77 +53,101 @@
// //
#include "wizchip_conf.h" #include "wizchip_conf.h"
/////////////
//M20150401 : Remove ; in the default callback function such as wizchip_cris_enter(), wizchip_cs_select() and etc.
/////////////
/** /**
* @brief Default function to enable interrupt. * @brief Default function to enable interrupt.
* @note This function help not to access wrong address. If you do not describe this function or register any functions, * @note This function help not to access wrong address. If you do not describe this function or register any functions,
* null function is called. * null function is called.
*/ */
void wizchip_cris_enter(void) {}; //void wizchip_cris_enter(void) {};
void wizchip_cris_enter(void) {}
/** /**
* @brief Default function to disable interrupt. * @brief Default function to disable interrupt.
* @note This function help not to access wrong address. If you do not describe this function or register any functions, * @note This function help not to access wrong address. If you do not describe this function or register any functions,
* null function is called. * null function is called.
*/ */
void wizchip_cris_exit(void) {}; //void wizchip_cris_exit(void) {};
void wizchip_cris_exit(void) {}
/** /**
* @brief Default function to select chip. * @brief Default function to select chip.
* @note This function help not to access wrong address. If you do not describe this function or register any functions, * @note This function help not to access wrong address. If you do not describe this function or register any functions,
* null function is called. * null function is called.
*/ */
void wizchip_cs_select(void) {}; //void wizchip_cs_select(void) {};
void wizchip_cs_select(void) {}
/** /**
* @brief Default function to deselect chip. * @brief Default function to deselect chip.
* @note This function help not to access wrong address. If you do not describe this function or register any functions, * @note This function help not to access wrong address. If you do not describe this function or register any functions,
* null function is called. * null function is called.
*/ */
void wizchip_cs_deselect(void) {}; //void wizchip_cs_deselect(void) {};
void wizchip_cs_deselect(void) {}
/** /**
* @brief Default function to read in direct or indirect interface. * @brief Default function to read in direct or indirect interface.
* @note This function help not to access wrong address. If you do not describe this function or register any functions, * @note This function help not to access wrong address. If you do not describe this function or register any functions,
* null function is called. * null function is called.
*/ */
//M20140501 : Explict pointer type casting //M20150601 : Rename the function for integrating with W5300
//uint8_t wizchip_bus_readbyte(uint32_t AddrSel) { return * ((volatile uint8_t *) AddrSel); }; //uint8_t wizchip_bus_readbyte(uint32_t AddrSel) { return * ((volatile uint8_t *)((ptrdiff_t) AddrSel)); }
uint8_t wizchip_bus_readbyte(uint32_t AddrSel) { return * ((volatile uint8_t *)((ptrdiff_t) AddrSel)); }; iodata_t wizchip_bus_readdata(uint32_t AddrSel) { return * ((volatile iodata_t *)((ptrdiff_t) AddrSel)); }
/** /**
* @brief Default function to write in direct or indirect interface. * @brief Default function to write in direct or indirect interface.
* @note This function help not to access wrong address. If you do not describe this function or register any functions, * @note This function help not to access wrong address. If you do not describe this function or register any functions,
* null function is called. * null function is called.
*/ */
//M20150601 : Rename the function for integrating with W5300
//M20140501 : Explict pointer type casting //void wizchip_bus_writebyte(uint32_t AddrSel, uint8_t wb) { *((volatile uint8_t*)((ptrdiff_t)AddrSel)) = wb; }
//void wizchip_bus_writebyte(uint32_t AddrSel, uint8_t wb) { *((volatile uint8_t*) AddrSel) = wb; }; void wizchip_bus_writedata(uint32_t AddrSel, iodata_t wb) { *((volatile iodata_t*)((ptrdiff_t)AddrSel)) = wb; }
void wizchip_bus_writebyte(uint32_t AddrSel, uint8_t wb) { *((volatile uint8_t*)((ptrdiff_t)AddrSel)) = wb; };
/** /**
* @brief Default function to read in SPI interface. * @brief Default function to read in SPI interface.
* @note This function help not to access wrong address. If you do not describe this function or register any functions, * @note This function help not to access wrong address. If you do not describe this function or register any functions,
* null function is called. * null function is called.
*/ */
uint8_t wizchip_spi_readbyte(void) {return 0;}; //uint8_t wizchip_spi_readbyte(void) {return 0;};
uint8_t wizchip_spi_readbyte(void) {return 0;}
/** /**
* @brief Default function to write in SPI interface. * @brief Default function to write in SPI interface.
* @note This function help not to access wrong address. If you do not describe this function or register any functions, * @note This function help not to access wrong address. If you do not describe this function or register any functions,
* null function is called. * null function is called.
*/ */
void wizchip_spi_writebyte(uint8_t wb) {}; //void wizchip_spi_writebyte(uint8_t wb) {};
void wizchip_spi_writebyte(uint8_t wb) {}
/** /**
* @brief Default function to burst read in SPI interface. * @brief Default function to burst read in SPI interface.
* @note This function help not to access wrong address. If you do not describe this function or register any functions, * @note This function help not to access wrong address. If you do not describe this function or register any functions,
* null function is called. * null function is called.
*/ */
void wizchip_spi_readburst(uint8_t* pBuf, uint16_t len) {}; //void wizchip_spi_readburst(uint8_t* pBuf, uint16_t len) {};
void wizchip_spi_readburst(uint8_t* pBuf, uint16_t len) {}
/** /**
* @brief Default function to burst write in SPI interface. * @brief Default function to burst write in SPI interface.
* @note This function help not to access wrong address. If you do not describe this function or register any functions, * @note This function help not to access wrong address. If you do not describe this function or register any functions,
* null function is called. * null function is called.
*/ */
void wizchip_spi_writeburst(uint8_t* pBuf, uint16_t len) {}; //void wizchip_spi_writeburst(uint8_t* pBuf, uint16_t len) {};
void wizchip_spi_writeburst(uint8_t* pBuf, uint16_t len) {}
/** /**
* @\ref _WIZCHIP instance * @\ref _WIZCHIP instance
*/ */
//
//M20150401 : For a compiler didnot support a member of structure
// Replace the assignment of struct members with the assingment of array
//
/*
_WIZCHIP WIZCHIP = _WIZCHIP WIZCHIP =
{ {
.id = _WIZCHIP_ID_, .id = _WIZCHIP_ID_,
@ -137,6 +161,24 @@ _WIZCHIP WIZCHIP =
// .IF.SPI._read_byte = wizchip_spi_readbyte, // .IF.SPI._read_byte = wizchip_spi_readbyte,
// .IF.SPI._write_byte = wizchip_spi_writebyte // .IF.SPI._write_byte = wizchip_spi_writebyte
}; };
*/
_WIZCHIP WIZCHIP =
{
_WIZCHIP_IO_MODE_,
_WIZCHIP_ID_ ,
wizchip_cris_enter,
wizchip_cris_exit,
wizchip_cs_select,
wizchip_cs_deselect,
//M20150601 : Rename the function
//wizchip_bus_readbyte,
//wizchip_bus_writebyte
wizchip_bus_readdata,
wizchip_bus_writedata,
// wizchip_spi_readbyte,
// wizchip_spi_writebyte
};
static uint8_t _DNS_[4]; // DNS server ip address static uint8_t _DNS_[4]; // DNS server ip address
static dhcp_mode _DHCP_; // DHCP mode static dhcp_mode _DHCP_; // DHCP mode
@ -169,10 +211,13 @@ void reg_wizchip_cs_cbfunc(void(*cs_sel)(void), void(*cs_desel)(void))
} }
} }
void reg_wizchip_bus_cbfunc(uint8_t(*bus_rb)(uint32_t addr), void (*bus_wb)(uint32_t addr, uint8_t wb)) //M20150515 : For integrating with W5300
//void reg_wizchip_bus_cbfunc(uint8_t(*bus_rb)(uint32_t addr), void (*bus_wb)(uint32_t addr, uint8_t wb))
void reg_wizchip_bus_cbfunc(iodata_t(*bus_rb)(uint32_t addr), void (*bus_wb)(uint32_t addr, iodata_t wb))
{ {
while(!(WIZCHIP.if_mode & _WIZCHIP_IO_MODE_BUS_)); while(!(WIZCHIP.if_mode & _WIZCHIP_IO_MODE_BUS_));
//M20150601 : Rename call back function for integrating with W5300
/*
if(!bus_rb || !bus_wb) if(!bus_rb || !bus_wb)
{ {
WIZCHIP.IF.BUS._read_byte = wizchip_bus_readbyte; WIZCHIP.IF.BUS._read_byte = wizchip_bus_readbyte;
@ -183,6 +228,17 @@ void reg_wizchip_bus_cbfunc(uint8_t(*bus_rb)(uint32_t addr), void (*bus_wb)(uint
WIZCHIP.IF.BUS._read_byte = bus_rb; WIZCHIP.IF.BUS._read_byte = bus_rb;
WIZCHIP.IF.BUS._write_byte = bus_wb; WIZCHIP.IF.BUS._write_byte = bus_wb;
} }
*/
if(!bus_rb || !bus_wb)
{
WIZCHIP.IF.BUS._read_data = wizchip_bus_readdata;
WIZCHIP.IF.BUS._write_data = wizchip_bus_writedata;
}
else
{
WIZCHIP.IF.BUS._read_data = bus_rb;
WIZCHIP.IF.BUS._write_data = bus_wb;
}
} }
void reg_wizchip_spi_cbfunc(uint8_t (*spi_rb)(void), void (*spi_wb)(uint8_t wb)) void reg_wizchip_spi_cbfunc(uint8_t (*spi_rb)(void), void (*spi_wb)(uint8_t wb))
@ -220,7 +276,9 @@ void reg_wizchip_spiburst_cbfunc(void (*spi_rb)(uint8_t* pBuf, uint16_t len), vo
int8_t ctlwizchip(ctlwizchip_type cwtype, void* arg) int8_t ctlwizchip(ctlwizchip_type cwtype, void* arg)
{ {
#if _WIZCHIP_ == 5200 || _WIZCHIP_ == 5500
uint8_t tmp = 0; uint8_t tmp = 0;
#endif
uint8_t* ptmp[2] = {0,0}; uint8_t* ptmp[2] = {0,0};
switch(cwtype) switch(cwtype)
{ {
@ -246,7 +304,9 @@ int8_t ctlwizchip(ctlwizchip_type cwtype, void* arg)
case CW_GET_INTRMASK: case CW_GET_INTRMASK:
*((intr_kind*)arg) = wizchip_getinterruptmask(); *((intr_kind*)arg) = wizchip_getinterruptmask();
break; break;
#if _WIZCHIP_ > 5100 //M20150601 : This can be supported by W5200, W5500
//#if _WIZCHIP_ > 5100
#if (_WIZCHIP_ == 5200 || _WIZCHIP_ == 5500)
case CW_SET_INTRTIME: case CW_SET_INTRTIME:
setINTLEVEL(*(uint16_t*)arg); setINTLEVEL(*(uint16_t*)arg);
break; break;
@ -277,6 +337,7 @@ int8_t ctlwizchip(ctlwizchip_type cwtype, void* arg)
case CW_SET_PHYPOWMODE: case CW_SET_PHYPOWMODE:
return wizphy_setphypmode(*(uint8_t*)arg); return wizphy_setphypmode(*(uint8_t*)arg);
#endif #endif
#if _WIZCHIP_ == 5200 || _WIZCHIP_ == 5500
case CW_GET_PHYPOWMODE: case CW_GET_PHYPOWMODE:
tmp = wizphy_getphypmode(); tmp = wizphy_getphypmode();
if((int8_t)tmp == -1) return -1; if((int8_t)tmp == -1) return -1;
@ -287,6 +348,7 @@ int8_t ctlwizchip(ctlwizchip_type cwtype, void* arg)
if((int8_t)tmp == -1) return -1; if((int8_t)tmp == -1) return -1;
*(uint8_t*)arg = tmp; *(uint8_t*)arg = tmp;
break; break;
#endif
default: default:
return -1; return -1;
} }
@ -326,10 +388,21 @@ void wizchip_sw_reset(void)
{ {
uint8_t gw[4], sn[4], sip[4]; uint8_t gw[4], sn[4], sip[4];
uint8_t mac[6]; uint8_t mac[6];
//A20150601
#if _WIZCHIP_IO_MODE_ == _WIZCHIP_IO_MODE_BUS_INDIR_
uint16_t mr = (uint16_t)getMR();
setMR(mr | MR_IND);
#endif
//
getSHAR(mac); getSHAR(mac);
getGAR(gw); getSUBR(sn); getSIPR(sip); getGAR(gw); getSUBR(sn); getSIPR(sip);
setMR(MR_RST); setMR(MR_RST);
getMR(); // for delay getMR(); // for delay
//A2015051 : For indirect bus mode
#if _WIZCHIP_IO_MODE_ == _WIZCHIP_IO_MODE_BUS_INDIR_
setMR(mr | MR_IND);
#endif
//
setSHAR(mac); setSHAR(mac);
setGAR(gw); setGAR(gw);
setSUBR(sn); setSUBR(sn);
@ -344,18 +417,44 @@ int8_t wizchip_init(uint8_t* txsize, uint8_t* rxsize)
if(txsize) if(txsize)
{ {
tmp = 0; tmp = 0;
//M20150601 : For integrating with W5300
#if _WIZCHIP_ == 5300
for(i = 0 ; i < _WIZCHIP_SOCK_NUM_; i++) for(i = 0 ; i < _WIZCHIP_SOCK_NUM_; i++)
{
if(txsize[i] >= 64) return -1; //No use 64KB even if W5300 support max 64KB memory allocation
tmp += txsize[i];
if(tmp > 128) return -1;
}
if(tmp % 8) return -1;
#else
for(i = 0 ; i < _WIZCHIP_SOCK_NUM_; i++)
{
tmp += txsize[i]; tmp += txsize[i];
if(tmp > 16) return -1; if(tmp > 16) return -1;
}
#endif
for(i = 0 ; i < _WIZCHIP_SOCK_NUM_; i++) for(i = 0 ; i < _WIZCHIP_SOCK_NUM_; i++)
setSn_TXBUF_SIZE(i, txsize[i]); setSn_TXBUF_SIZE(i, txsize[i]);
} }
if(rxsize) if(rxsize)
{ {
tmp = 0; tmp = 0;
#if _WIZCHIP_ == 5300
for(i = 0 ; i < _WIZCHIP_SOCK_NUM_; i++) for(i = 0 ; i < _WIZCHIP_SOCK_NUM_; i++)
{
if(rxsize[i] >= 64) return -1; //No use 64KB even if W5300 support max 64KB memory allocation
tmp += rxsize[i];
if(tmp > 128) return -1;
}
if(tmp % 8) return -1;
#else
for(i = 0 ; i < _WIZCHIP_SOCK_NUM_; i++)
{
tmp += rxsize[i]; tmp += rxsize[i];
if(tmp > 16) return -1; if(tmp > 16) return -1;
}
#endif
for(i = 0 ; i < _WIZCHIP_SOCK_NUM_; i++) for(i = 0 ; i < _WIZCHIP_SOCK_NUM_; i++)
setSn_RXBUF_SIZE(i, rxsize[i]); setSn_RXBUF_SIZE(i, rxsize[i]);
} }
@ -380,6 +479,9 @@ void wizchip_clrinterrupt(intr_kind intr)
#if _WIZCHIP_ == 5100 #if _WIZCHIP_ == 5100
ir |= sir; ir |= sir;
setIR(ir); setIR(ir);
//A20150601 : For integrating with W5300
#elif _WIZCHIP_ == 5300
setIR( ((((uint16_t)ir) << 8) | (((uint16_t)sir) & 0x00FF)) );
#else #else
setIR(ir); setIR(ir);
setSIR(sir); setSIR(sir);
@ -393,13 +495,20 @@ intr_kind wizchip_getinterrupt(void)
uint16_t ret = 0; uint16_t ret = 0;
#if _WIZCHIP_ == 5100 #if _WIZCHIP_ == 5100
ir = getIR(); ir = getIR();
sir = ir 0x0F; sir = ir & 0x0F;
//A20150601 : For integrating with W5300
#elif _WIZCHIP_ == 5300
ret = getIR();
ir = (uint8_t)(ret >> 8);
sir = (uint8_t)ret;
#else #else
ir = getIR(); ir = getIR();
sir = getSIR(); sir = getSIR();
#endif #endif
#if _WIZCHIP_ < 5500 //M20150601 : For Integrating with W5300
//#if _WIZCHIP_ < 5500
#if _WIZCHIP_ < 5200
ir &= ~(1<<4); // IK_WOL ir &= ~(1<<4); // IK_WOL
#endif #endif
#if _WIZCHIP_ == 5200 #if _WIZCHIP_ == 5200
@ -421,13 +530,13 @@ void wizchip_setinterruptmask(intr_kind intr)
imr &= ~(1 << 6); imr &= ~(1 << 6);
#endif #endif
#if _WIZCHIP_ < 5200
simr &= 0x0F;
#endif
#if _WIZCHIP_ == 5100 #if _WIZCHIP_ == 5100
simr &= 0x0F;
imr |= simr; imr |= simr;
setIMR(imr); setIMR(imr);
//A20150601 : For integrating with W5300
#elif _WIZCHIP_ == 5300
setIMR( ((((uint16_t)imr) << 8) | (((uint16_t)simr) & 0x00FF)) );
#else #else
setIMR(imr); setIMR(imr);
setSIMR(simr); setSIMR(simr);
@ -441,7 +550,12 @@ intr_kind wizchip_getinterruptmask(void)
uint16_t ret = 0; uint16_t ret = 0;
#if _WIZCHIP_ == 5100 #if _WIZCHIP_ == 5100
imr = getIMR(); imr = getIMR();
simr = imr 0x0F; simr = imr & 0x0F;
//A20150601 : For integrating with W5300
#elif _WIZCHIP_ == 5300
ret = getIMR();
imr = (uint8_t)(ret >> 8);
simr = (uint8_t)ret;
#else #else
imr = getIMR(); imr = getIMR();
simr = getSIMR(); simr = getSIMR();

View File

@ -57,10 +57,12 @@
#include <stdint.h> #include <stdint.h>
/** /**
* @brief Select WIZCHIP. * @brief Select WIZCHIP.
* @todo You should select one, \b 5100, \b 5200 ,\b 5500 or etc. \n\n * @todo You should select one, \b 5100, \b 5200, \b 5300, \b 5500 or etc. \n\n
* ex> <code> #define \_WIZCHIP_ 5500 </code> * ex> <code> #define \_WIZCHIP_ 5500 </code>
*/ */
#define _WIZCHIP_ 5500 // 5100, 5200, 5500 #ifndef _WIZCHIP_
#define _WIZCHIP_ 5500 // 5100, 5200, 5300, 5500
#endif
#define _WIZCHIP_IO_MODE_NONE_ 0x0000 #define _WIZCHIP_IO_MODE_NONE_ 0x0000
#define _WIZCHIP_IO_MODE_BUS_ 0x0100 /**< Bus interface mode */ #define _WIZCHIP_IO_MODE_BUS_ 0x0100 /**< Bus interface mode */
@ -83,19 +85,27 @@
* @brief Define interface mode. * @brief Define interface mode.
* @todo you should select interface mode as chip. Select one of @ref \_WIZCHIP_IO_MODE_SPI_ , @ref \_WIZCHIP_IO_MODE_BUS_DIR_ or @ref \_WIZCHIP_IO_MODE_BUS_INDIR_ * @todo you should select interface mode as chip. Select one of @ref \_WIZCHIP_IO_MODE_SPI_ , @ref \_WIZCHIP_IO_MODE_BUS_DIR_ or @ref \_WIZCHIP_IO_MODE_BUS_INDIR_
*/ */
// #define _WIZCHIP_IO_MODE_ _WIZCHIP_IO_MODE_BUS_DIR_ // #define _WIZCHIP_IO_MODE_ _WIZCHIP_IO_MODE_BUS_DIR_
// #define _WIZCHIP_IO_MODE_ _WIZCHIP_IO_MODE_BUS_INDIR_ // #define _WIZCHIP_IO_MODE_ _WIZCHIP_IO_MODE_BUS_INDIR_
#define _WIZCHIP_IO_MODE_ _WIZCHIP_IO_MODE_SPI_ #define _WIZCHIP_IO_MODE_ _WIZCHIP_IO_MODE_SPI_
//A20150601 : Define the unit of IO DATA.
typedef uint8_t iodata_t;
//A20150401 : Indclude W5100.h file
#include "W5100/w5100.h"
#elif (_WIZCHIP_ == 5200) #elif (_WIZCHIP_ == 5200)
#define _WIZCHIP_ID_ "W5200\0" #define _WIZCHIP_ID_ "W5200\0"
/** /**
* @brief Define interface mode. * @brief Define interface mode.
* @todo you should select interface mode as chip. Select one of @ref \_WIZCHIP_IO_MODE_SPI_ or @ref \_WIZCHIP_IO_MODE_BUS_INDIR_ * @todo you should select interface mode as chip. Select one of @ref \_WIZCHIP_IO_MODE_SPI_ or @ref \_WIZCHIP_IO_MODE_BUS_INDIR_
*/ */
#ifndef _WIZCHIP_IO_MODE_
// #define _WIZCHIP_IO_MODE_ _WIZCHIP_IO_MODE_BUS_INDIR_ // #define _WIZCHIP_IO_MODE_ _WIZCHIP_IO_MODE_BUS_INDIR_
#define _WIZCHIP_IO_MODE_ _WIZCHIP_IO_MODE_SPI_ #define _WIZCHIP_IO_MODE_ _WIZCHIP_IO_MODE_SPI_
#endif
//A20150601 : Define the unit of IO DATA.
typedef uint8_t iodata_t;
#include "W5200/w5200.h" #include "W5200/w5200.h"
#elif (_WIZCHIP_ == 5500) #elif (_WIZCHIP_ == 5500)
#define _WIZCHIP_ID_ "W5500\0" #define _WIZCHIP_ID_ "W5500\0"
@ -113,9 +123,41 @@
* ex> <code> #define \_WIZCHIP_IO_MODE_ \_WIZCHIP_IO_MODE_SPI_VDM_ </code> * ex> <code> #define \_WIZCHIP_IO_MODE_ \_WIZCHIP_IO_MODE_SPI_VDM_ </code>
* *
*/ */
#ifndef _WIZCHIP_IO_MODE_
//#define _WIZCHIP_IO_MODE_ _WIZCHIP_IO_MODE_SPI_FDM_ //#define _WIZCHIP_IO_MODE_ _WIZCHIP_IO_MODE_SPI_FDM_
#define _WIZCHIP_IO_MODE_ _WIZCHIP_IO_MODE_SPI_VDM_ #define _WIZCHIP_IO_MODE_ _WIZCHIP_IO_MODE_SPI_VDM_
#endif
//A20150601 : Define the unit of IO DATA.
typedef uint8_t iodata_t;
#include "W5500/w5500.h" #include "W5500/w5500.h"
#elif ( _WIZCHIP_ == 5300)
#define _WIZCHIP_ID_ "W5300\0"
/**
* @brief Define interface mode.
* @todo you should select interface mode as chip. Select one of @ref \_WIZCHIP_IO_MODE_SPI_ , @ref \_WIZCHIP_IO_MODE_BUS_DIR_ or @ref \_WIZCHIP_IO_MODE_BUS_INDIR_
*/
#ifndef _WIZCHIP_IO_MODE_
// #define _WIZCHIP_IO_MODE_ _WIZCHIP_IO_MODE_BUS_DIR_
#define _WIZCHIP_IO_MODE_ _WIZCHIP_IO_MODE_BUS_INDIR_
#endif
//A20150601 : Define the unit and bus width of IO DATA.
/**
* @brief Select the data width 8 or 16 bits.
* @todo you should select the bus width. Select one of 8 or 16.
*/
#ifndef _WIZCHIP_IO_BUS_WIDTH_
#define _WIZCHIP_IO_BUS_WIDTH_ 8 // 16
#endif
#if _WIZCHIP_IO_BUS_WIDTH_ == 8
typedef uint8_t iodata_t;
#elif _WIZCHIP_IO_BUS_WIDTH_ == 16
typedef uint16_t iodata_t;
#else
#error "Unknown _WIZCHIP_IO_BUS_WIDTH_. It should be 8 or 16."
#endif
//
#include "W5300/w5300.h"
#else #else
#error "Unknown defined _WIZCHIP_. You should define one of 5100, 5200, and 5500 !!!" #error "Unknown defined _WIZCHIP_. You should define one of 5100, 5200, and 5500 !!!"
#endif #endif
@ -130,9 +172,13 @@
* @ref \_WIZCHIP_IO_MODE_BUS_DIR_, @ref \_WIZCHIP_IO_MODE_BUS_INDIR_). \n\n * @ref \_WIZCHIP_IO_MODE_BUS_DIR_, @ref \_WIZCHIP_IO_MODE_BUS_INDIR_). \n\n
* ex> <code> #define \_WIZCHIP_IO_BASE_ 0x00008000 </code> * ex> <code> #define \_WIZCHIP_IO_BASE_ 0x00008000 </code>
*/ */
#define _WIZCHIP_IO_BASE_ 0x00000000 // #ifndef _WIZCHIP_IO_BASE_
#define _WIZCHIP_IO_BASE_ 0x00000000 // 0x8000
#endif
#if _WIZCHIP_IO_MODE_ & _WIZCHIP_IO_MODE_BUS //M20150401 : Typing Error
//#if _WIZCHIP_IO_MODE_ & _WIZCHIP_IO_MODE_BUS
#if _WIZCHIP_IO_MODE_ & _WIZCHIP_IO_MODE_BUS_
#ifndef _WIZCHIP_IO_BASE_ #ifndef _WIZCHIP_IO_BASE_
#error "You should be define _WIZCHIP_IO_BASE to fit your system memory map." #error "You should be define _WIZCHIP_IO_BASE to fit your system memory map."
#endif #endif
@ -165,7 +211,7 @@ typedef struct __WIZCHIP
void (*_exit) (void); ///< critial section exit void (*_exit) (void); ///< critial section exit
}CRIS; }CRIS;
/** /**
* The set of @ref\_WIZCHIP_ select control callback func. * The set of @ref \_WIZCHIP_ select control callback func.
*/ */
struct _CS struct _CS
{ {
@ -180,11 +226,18 @@ typedef struct __WIZCHIP
/** /**
* For BUS interface IO * For BUS interface IO
*/ */
//M20156501 : Modify the function name for integrating with W5300
//struct
//{
// uint8_t (*_read_byte) (uint32_t AddrSel);
// void (*_write_byte) (uint32_t AddrSel, uint8_t wb);
//}BUS;
struct struct
{ {
uint8_t (*_read_byte) (uint32_t AddrSel); iodata_t (*_read_data) (uint32_t AddrSel);
void (*_write_byte) (uint32_t AddrSel, uint8_t wb); void (*_write_data) (uint32_t AddrSel, iodata_t wb);
}BUS; }BUS;
/** /**
* For SPI interface IO * For SPI interface IO
*/ */
@ -218,15 +271,19 @@ typedef enum
CW_GET_INTRTIME, ///< Set interval time between the current and next interrupt. CW_GET_INTRTIME, ///< Set interval time between the current and next interrupt.
CW_GET_ID, ///< Gets WIZCHIP name. CW_GET_ID, ///< Gets WIZCHIP name.
#if _WIZCHIP_ == 5500 //D20150601 : For no modification your application code
CW_RESET_PHY, ///< Resets internal PHY. Valid Only W5000 //#if _WIZCHIP_ == 5500
CW_RESET_PHY, ///< Resets internal PHY. Valid Only W5500
CW_SET_PHYCONF, ///< When PHY configured by internal register, PHY operation mode (Manual/Auto, 10/100, Half/Full). Valid Only W5000 CW_SET_PHYCONF, ///< When PHY configured by internal register, PHY operation mode (Manual/Auto, 10/100, Half/Full). Valid Only W5000
CW_GET_PHYCONF, ///< Get PHY operation mode in internal register. Valid Only W5000 CW_GET_PHYCONF, ///< Get PHY operation mode in internal register. Valid Only W5500
CW_GET_PHYSTATUS, ///< Get real PHY status on operating. Valid Only W5000 CW_GET_PHYSTATUS, ///< Get real PHY status on operating. Valid Only W5500
CW_SET_PHYPOWMODE, ///< Set PHY power mode as normal and down when PHYSTATUS.OPMD == 1. Valid Only W5500 CW_SET_PHYPOWMODE, ///< Set PHY power mode as normal and down when PHYSTATUS.OPMD == 1. Valid Only W5500
#endif //#endif
CW_GET_PHYPOWMODE, ///< Get PHY Power mode as down or normal //D20150601 : For no modification your application code
CW_GET_PHYLINK ///< Get PHY Link status //#if _WIZCHIP_ == 5200 || _WIZCHIP_ == 5500
CW_GET_PHYPOWMODE, ///< Get PHY Power mode as down or normal, Valid Only W5100, W5200
CW_GET_PHYLINK ///< Get PHY Link status, Valid Only W5100, W5200
//#endif
}ctlwizchip_type; }ctlwizchip_type;
/** /**
@ -251,8 +308,10 @@ typedef enum
*/ */
typedef enum typedef enum
{ {
#if _WIZCHIP_ > 5200 #if _WIZCHIP_ == 5500
IK_WOL = (1 << 4), ///< Wake On Lan by receiving the magic packet. Valid in W500. IK_WOL = (1 << 4), ///< Wake On Lan by receiving the magic packet. Valid in W500.
#elif _WIZCHIP_ == 5300
IK_FMTU = (1 << 4), ///< Received a ICMP message (Fragment MTU)
#endif #endif
IK_PPPOE_TERMINATED = (1 << 5), ///< PPPoE Disconnected IK_PPPOE_TERMINATED = (1 << 5), ///< PPPoE Disconnected
@ -389,7 +448,9 @@ void reg_wizchip_cs_cbfunc(void(*cs_sel)(void), void(*cs_desel)(void));
*or register your functions. *or register your functions.
*@note If you do not describe or register, null function is called. *@note If you do not describe or register, null function is called.
*/ */
void reg_wizchip_bus_cbfunc(uint8_t (*bus_rb)(uint32_t addr), void (*bus_wb)(uint32_t addr, uint8_t wb)); //M20150601 : For integrating with W5300
//void reg_wizchip_bus_cbfunc(uint8_t (*bus_rb)(uint32_t addr), void (*bus_wb)(uint32_t addr, uint8_t wb));
void reg_wizchip_bus_cbfunc(iodata_t (*bus_rb)(uint32_t addr), void (*bus_wb)(uint32_t addr, iodata_t wb));
/** /**
*@brief Registers call back function for SPI interface. *@brief Registers call back function for SPI interface.
@ -547,17 +608,17 @@ netmode_type wizchip_getnetmode(void);
/** /**
* @ingroup extra_functions * @ingroup extra_functions
* @brief Set retry time value(@ref RTR) and retry count(@ref RCR). * @brief Set retry time value(@ref _RTR_) and retry count(@ref _RCR_).
* @details @ref RTR configures the retransmission timeout period and @ref RCR configures the number of time of retransmission. * @details @ref _RTR_ configures the retransmission timeout period and @ref _RCR_ configures the number of time of retransmission.
* @param nettime @ref RTR value and @ref RCR value. Refer to @ref wiz_NetTimeout. * @param nettime @ref _RTR_ value and @ref _RCR_ value. Refer to @ref wiz_NetTimeout.
*/ */
void wizchip_settimeout(wiz_NetTimeout* nettime); void wizchip_settimeout(wiz_NetTimeout* nettime);
/** /**
* @ingroup extra_functions * @ingroup extra_functions
* @brief Get retry time value(@ref RTR) and retry count(@ref RCR). * @brief Get retry time value(@ref _RTR_) and retry count(@ref _RCR_).
* @details @ref RTR configures the retransmission timeout period and @ref RCR configures the number of time of retransmission. * @details @ref _RTR_ configures the retransmission timeout period and @ref _RCR_ configures the number of time of retransmission.
* @param nettime @ref RTR value and @ref RCR value. Refer to @ref wiz_NetTimeout. * @param nettime @ref _RTR_ value and @ref _RCR_ value. Refer to @ref wiz_NetTimeout.
*/ */
void wizchip_gettimeout(wiz_NetTimeout* nettime); void wizchip_gettimeout(wiz_NetTimeout* nettime);

584
Internet/FTPClient/ftpc.c Normal file
View File

@ -0,0 +1,584 @@
#include "ftpc.h"
un_l2cval remote_ip;
uint16_t remote_port;
un_l2cval local_ip;
uint16_t local_port;
uint8_t connect_state_control_ftpc = 0;
uint8_t connect_state_data_ftpc = 0;
uint8_t gModeActivePassiveflag = 0;
uint8_t FTP_destip[4] = {192, 168, 10, 230}; // For FTP client examples; destination network info
uint16_t FTP_destport = 21; // For FTP client examples; destination network info
uint8_t gMenuStart = 0;
uint8_t gDataSockReady = 0;
uint8_t gDataPutGetStart = 0;
static uint8_t gMsgBuf[20]={0,};
struct ftpc ftpc;
struct Command Command;
void ftpc_init(uint8_t * src_ip)
{
ftpc.dsock_mode = ACTIVE_MODE;
local_ip.cVal[0] = src_ip[0];
local_ip.cVal[1] = src_ip[1];
local_ip.cVal[2] = src_ip[2];
local_ip.cVal[3] = src_ip[3];
local_port = 35000;
strcpy(ftpc.workingdir, "/");
socket(CTRL_SOCK, Sn_MR_TCP, FTP_destport, 0x0);
}
uint8_t ftpc_run(uint8_t * dbuf)
{
#ifndef Need_UARTGetCharBlocking_func
uint16_t size = 0;
long ret = 0;
uint32_t send_byte, recv_byte;
uint32_t blocklen;
uint32_t remain_filesize;
uint32_t remain_datasize;
uint8_t msg_c;
uint8_t dat[50]={0,};
uint32_t totalSize = 0, availableSize = 0;
switch(getSn_SR(CTRL_SOCK))
{
case SOCK_ESTABLISHED :
if(!connect_state_control_ftpc){
printf("%d:FTP Connected\r\n", CTRL_SOCK);
strcpy(ftpc.workingdir, "/");
connect_state_control_ftpc = 1;
}
if(gMenuStart){
gMenuStart = 0;
printf("\r\n----------------------------------------\r\n");
printf("Press menu key\r\n");
printf("----------------------------------------\r\n");
printf("1> View FTP Server Directory\r\n");
printf("2> View My Directory\r\n");
printf("3> Sets the type of file to be transferred. Current state : %s\r\n", (ftpc.type==ASCII_TYPE)?"Ascii":"Binary");
printf("4> Sets Data Connection. Current state : %s\r\n", (ftpc.dsock_mode==ACTIVE_MODE)?"Active":"Passive");
printf("5> Put File to Server\r\n");
printf("6> Get File from Server\r\n");
#if defined(F_FILESYSTEM)
printf("7> Delete My File\r\n");
#endif
printf("----------------------------------------\r\n");
while(1){
msg_c=ftp_getc();
if(msg_c=='1'){
if(ftpc.dsock_mode==PASSIVE_MODE){
sprintf(dat,"PASV\r\n");
send(CTRL_SOCK, (uint8_t *)dat, strlen(dat));
Command.First = f_dir;
break;
}
else{
wiz_NetInfo gWIZNETINFO;
ctlnetwork(CN_GET_NETINFO, (void*) &gWIZNETINFO);
sprintf(dat,"PORT %d,%d,%d,%d,%d,%d\r\n", gWIZNETINFO.ip[0], gWIZNETINFO.ip[1], gWIZNETINFO.ip[2], gWIZNETINFO.ip[3], (uint8_t)(local_port>>8), (uint8_t)(local_port&0x00ff));
send(CTRL_SOCK, (uint8_t *)dat, strlen(dat));
Command.First = f_dir;
gModeActivePassiveflag = 1;
break;
}
break;
}
else if(msg_c=='5'){
if(ftpc.dsock_mode==PASSIVE_MODE){
sprintf(dat,"PASV\r\n");
send(CTRL_SOCK, (uint8_t *)dat, strlen(dat));
Command.First = f_put;
break;
}
else{
wiz_NetInfo gWIZNETINFO;
ctlnetwork(CN_GET_NETINFO, (void*) &gWIZNETINFO);
sprintf(dat,"PORT %d,%d,%d,%d,%d,%d\r\n", gWIZNETINFO.ip[0], gWIZNETINFO.ip[1], gWIZNETINFO.ip[2], gWIZNETINFO.ip[3], (uint8_t)(local_port>>8), (uint8_t)(local_port&0x00ff));
send(CTRL_SOCK, (uint8_t *)dat, strlen(dat));
Command.First = f_put;
gModeActivePassiveflag = 1;
break;
}
}
else if(msg_c=='6'){
if(ftpc.dsock_mode==PASSIVE_MODE){
sprintf(dat,"PASV\r\n");
send(CTRL_SOCK, (uint8_t *)dat, strlen(dat));
Command.First = f_get;
break;
}
else{
wiz_NetInfo gWIZNETINFO;
ctlnetwork(CN_GET_NETINFO, (void*) &gWIZNETINFO);
sprintf(dat,"PORT %d,%d,%d,%d,%d,%d\r\n", gWIZNETINFO.ip[0], gWIZNETINFO.ip[1], gWIZNETINFO.ip[2], gWIZNETINFO.ip[3], (uint8_t)(local_port>>8), (uint8_t)(local_port&0x00ff));
send(CTRL_SOCK, (uint8_t *)dat, strlen(dat));
Command.First = f_get;
gModeActivePassiveflag = 1;
break;
}
}
else if(msg_c=='2'){
#if defined(F_FILESYSTEM)
scan_files(ftpc.workingdir, dbuf, (int *)&size);
printf("\r\n%s\r\n", dbuf);
#else
if (strncmp(ftpc.workingdir, "/$Recycle.Bin", sizeof("/$Recycle.Bin")) != 0)
size = sprintf(dbuf, "drwxr-xr-x 1 ftp ftp 0 Dec 31 2014 $Recycle.Bin\r\n-rwxr-xr-x 1 ftp ftp 512 Dec 31 2014 test.txt\r\n");
printf("\r\n%s\r\n", dbuf);
#endif
gMenuStart = 1;
break;
}
else if(msg_c=='3'){
printf("1> ASCII\r\n");
printf("2> BINARY\r\n");
while(1){
msg_c=ftp_getc();
if(msg_c=='1'){
sprintf(dat,"TYPE %c\r\n", TransferAscii);
ftpc.type = ASCII_TYPE;
send(CTRL_SOCK, (uint8_t *)dat, strlen(dat));
break;
}
else if(msg_c=='2'){
sprintf(dat,"TYPE %c\r\n", TransferBinary);
ftpc.type = IMAGE_TYPE;
send(CTRL_SOCK, (uint8_t *)dat, strlen(dat));
break;
}
else{
printf("\r\nRetry...\r\n");
}
}
break;
}
else if(msg_c=='4'){
printf("1> ACTIVE\r\n");
printf("2> PASSIVE\r\n");
while(1){
msg_c=ftp_getc();
if(msg_c=='1'){
ftpc.dsock_mode=ACTIVE_MODE;
break;
}
else if(msg_c=='2'){
ftpc.dsock_mode=PASSIVE_MODE;
break;
}
else{
printf("\r\nRetry...\r\n");
}
}
gMenuStart = 1;
break;
}
#if defined(F_FILESYSTEM)
else if(msg_c=='7'){
printf(">del filename?");
sprintf(ftpc.filename, "/%s\r\n", User_Keyboard_MSG());
if (f_unlink((const char *)ftpc.filename) != 0){
printf("\r\nCould not delete.\r\n");
}
else{
printf("\r\nDeleted.\r\n");
}
gMenuStart = 1;
break;
}
#endif
else{
printf("\r\nRetry...\r\n");
}
}
}
if(gDataSockReady){
gDataSockReady = 0;
switch(Command.First){
case f_dir:
sprintf(dat,"LIST\r\n");
send(CTRL_SOCK, (uint8_t *)dat, strlen(dat));
break;
case f_put:
printf(">put file name?");
sprintf(dat,"STOR %s\r\n", User_Keyboard_MSG());
send(CTRL_SOCK, (uint8_t *)dat, strlen(dat));
break;
case f_get:
printf(">get file name?");
sprintf(dat,"RETR %s\r\n", User_Keyboard_MSG());
send(CTRL_SOCK, (uint8_t *)dat, strlen(dat));
break;
default:
printf("Command.First = default\r\n");
break;
}
}
if((size = getSn_RX_RSR(CTRL_SOCK)) > 0){ // Don't need to check SOCKERR_BUSY because it doesn't not occur.
memset(dbuf, 0, _MAX_SS);
if(size > _MAX_SS) size = _MAX_SS - 1;
ret = recv(CTRL_SOCK,dbuf,size);
dbuf[ret] = '\0';
if(ret != size)
{
if(ret==SOCK_BUSY) return 0;
if(ret < 0){
printf("%d:recv() error:%ld\r\n",CTRL_SOCK,ret);
close(CTRL_SOCK);
return ret;
}
}
printf("Rcvd Command: %s\r\n", dbuf);
proc_ftpc((char *)dbuf);
}
break;
case SOCK_CLOSE_WAIT :
printf("%d:CloseWait\r\n",CTRL_SOCK);
if((ret=disconnect(CTRL_SOCK)) != SOCK_OK) return ret;
printf("%d:Closed\r\n",CTRL_SOCK);
break;
case SOCK_CLOSED :
printf("%d:FTPStart\r\n",CTRL_SOCK);
if((ret=socket(CTRL_SOCK, Sn_MR_TCP, FTP_destport, 0x0)) != CTRL_SOCK){
printf("%d:socket() error:%ld\r\n", CTRL_SOCK, ret);
close(CTRL_SOCK);
return ret;
}
break;
case SOCK_INIT :
printf("%d:Opened\r\n",CTRL_SOCK);
if((ret = connect(CTRL_SOCK, FTP_destip, FTP_destport)) != SOCK_OK){
printf("%d:Connect error\r\n",CTRL_SOCK);
return ret;
}
connect_state_control_ftpc = 0;
printf("%d:Connectting...\r\n",CTRL_SOCK);
break;
default :
break;
}
switch(getSn_SR(DATA_SOCK)){
case SOCK_ESTABLISHED :
if(!connect_state_data_ftpc){
printf("%d:FTP Data socket Connected\r\n", DATA_SOCK);
connect_state_data_ftpc = 1;
}
if(gDataPutGetStart){
switch(Command.Second){
case s_dir:
printf("dir waiting...\r\n");
if((size = getSn_RX_RSR(DATA_SOCK)) > 0){ // Don't need to check SOCKERR_BUSY because it doesn't not occur.
printf("ok\r\n");
memset(dbuf, 0, _MAX_SS);
if(size > _MAX_SS) size = _MAX_SS - 1;
ret = recv(DATA_SOCK,dbuf,size);
dbuf[ret] = '\0';
if(ret != size){
if(ret==SOCK_BUSY) return 0;
if(ret < 0){
printf("%d:recv() error:%ld\r\n",CTRL_SOCK,ret);
close(DATA_SOCK);
return ret;
}
}
printf("Rcvd Data:\n\r%s\n\r", dbuf);
gDataPutGetStart = 0;
Command.Second = s_nocmd;
}
break;
case s_put:
printf("put waiting...\r\n");
if(strlen(ftpc.workingdir) == 1)
sprintf(ftpc.filename, "/%s", (uint8_t *)gMsgBuf);
else
sprintf(ftpc.filename, "%s/%s", ftpc.workingdir, (uint8_t *)gMsgBuf);
#if defined(F_FILESYSTEM)
ftpc.fr = f_open(&(ftpc.fil), (const char *)ftpc.filename, FA_READ);
if(ftpc.fr == FR_OK){
remain_filesize = ftpc.fil.fsize;
printf("f_open return FR_OK\r\n");
do{
memset(dbuf, 0, _MAX_SS);
if(remain_filesize > _MAX_SS)
send_byte = _MAX_SS;
else
send_byte = remain_filesize;
ftpc.fr = f_read(&(ftpc.fil), (void *)dbuf, send_byte , (UINT *)&blocklen);
if(ftpc.fr != FR_OK){
break;
}
printf("#");
send(DATA_SOCK, dbuf, blocklen);
remain_filesize -= blocklen;
}while(remain_filesize != 0);
printf("\r\nFile read finished\r\n");
ftpc.fr = f_close(&(ftpc.fil));
}
else{
printf("File Open Error: %d\r\n", ftpc.fr);
ftpc.fr = f_close(&(ftpc.fil));
}
#else
remain_filesize = strlen(ftpc.filename);
do{
memset(dbuf, 0, _MAX_SS);
blocklen = sprintf(dbuf, "%s", ftpc.filename);
printf("########## dbuf:%s\r\n", dbuf);
send(DATA_SOCK, dbuf, blocklen);
remain_filesize -= blocklen;
}while(remain_filesize != 0);
#endif
gDataPutGetStart = 0;
Command.Second = s_nocmd;
disconnect(DATA_SOCK);
break;
case s_get:
printf("get waiting...\r\n");
if(strlen(ftpc.workingdir) == 1)
sprintf(ftpc.filename, "/%s", (uint8_t *)gMsgBuf);
else
sprintf(ftpc.filename, "%s/%s", ftpc.workingdir, (uint8_t *)gMsgBuf);
#if defined(F_FILESYSTEM)
ftpc.fr = f_open(&(ftpc.fil), (const char *)ftpc.filename, FA_CREATE_ALWAYS | FA_WRITE);
if(ftpc.fr == FR_OK){
printf("f_open return FR_OK\r\n");
while(1){
if((remain_datasize = getSn_RX_RSR(DATA_SOCK)) > 0){
while(1){
memset(dbuf, 0, _MAX_SS);
if(remain_datasize > _MAX_SS) recv_byte = _MAX_SS;
else recv_byte = remain_datasize;
ret = recv(DATA_SOCK, dbuf, recv_byte);
ftpc.fr = f_write(&(ftpc.fil), (const void *)dbuf, (UINT)ret, (UINT *)&blocklen);
remain_datasize -= blocklen;
if(ftpc.fr != FR_OK){
printf("f_write failed\r\n");
break;
}
if(remain_datasize <= 0) break;
}
if(ftpc.fr != FR_OK){
printf("f_write failed\r\n");
break;
}
printf("#");
}
else{
if(getSn_SR(DATA_SOCK) != SOCK_ESTABLISHED) break;
}
}
printf("\r\nFile write finished\r\n");
ftpc.fr = f_close(&(ftpc.fil));
gDataPutGetStart = 0;
}else{
printf("File Open Error: %d\r\n", ftpc.fr);
}
#else
while(1){
if((remain_datasize = getSn_RX_RSR(DATA_SOCK)) > 0){
while(1){
memset(dbuf, 0, _MAX_SS);
if(remain_datasize > _MAX_SS)
recv_byte = _MAX_SS;
else
recv_byte = remain_datasize;
ret = recv(DATA_SOCK, dbuf, recv_byte);
printf("########## dbuf:%s\r\n", dbuf);
remain_datasize -= ret;
if(remain_datasize <= 0)
break;
}
}else{
if(getSn_SR(DATA_SOCK) != SOCK_ESTABLISHED)
break;
}
}
gDataPutGetStart = 0;
Command.Second = s_nocmd;
#endif
break;
default:
printf("Command.Second = default\r\n");
break;
}
}
break;
case SOCK_CLOSE_WAIT :
printf("%d:CloseWait\r\n",DATA_SOCK);
if((ret=disconnect(DATA_SOCK)) != SOCK_OK) return ret;
printf("%d:Closed\r\n",DATA_SOCK);
break;
case SOCK_CLOSED :
if(ftpc.dsock_state == DATASOCK_READY){
if(ftpc.dsock_mode == PASSIVE_MODE){
printf("%d:FTPDataStart, port : %d\r\n",DATA_SOCK, local_port);
if((ret=socket(DATA_SOCK, Sn_MR_TCP, local_port, 0x0)) != DATA_SOCK){
printf("%d:socket() error:%ld\r\n", DATA_SOCK, ret);
close(DATA_SOCK);
return ret;
}
local_port++;
if(local_port > 50000)
local_port = 35000;
}else{
printf("%d:FTPDataStart, port : %d\r\n",DATA_SOCK, local_port);
if((ret=socket(DATA_SOCK, Sn_MR_TCP, local_port, 0x0)) != DATA_SOCK){
printf("%d:socket() error:%ld\r\n", DATA_SOCK, ret);
close(DATA_SOCK);
return ret;
}
local_port++;
if(local_port > 50000)
local_port = 35000;
}
ftpc.dsock_state = DATASOCK_START;
}
break;
case SOCK_INIT :
printf("%d:Opened\r\n",DATA_SOCK);
if(ftpc.dsock_mode == ACTIVE_MODE){
if( (ret = listen(DATA_SOCK)) != SOCK_OK){
printf("%d:Listen error\r\n",DATA_SOCK);
return ret;
}
gDataSockReady = 1;
printf("%d:Listen ok\r\n",DATA_SOCK);
}else{
if((ret = connect(DATA_SOCK, remote_ip.cVal, remote_port)) != SOCK_OK){
printf("%d:Connect error\r\n", DATA_SOCK);
return ret;
}
gDataSockReady = 1;
}
connect_state_data_ftpc = 0;
break;
default :
break;
}
#endif
return 0;
}
char proc_ftpc(char * buf)
{
uint16_t Responses;
uint8_t dat[30]={0,};
Responses =(buf[0]-'0')*100+(buf[1]-'0')*10+(buf[2]-'0');
switch(Responses){
case R_220: /* Service ready for new user. */
printf("\r\nInput your User ID > ");
sprintf(dat,"USER %s\r\n", User_Keyboard_MSG());
printf("\r\n");
send(CTRL_SOCK, (uint8_t *)dat, strlen(dat));
break;
case R_331: /* User name okay, need password. */
printf("\r\nInput your Password > ");
sprintf(dat,"PASS %s\r\n", User_Keyboard_MSG());
printf("\r\n");
send(CTRL_SOCK, (uint8_t *)dat, strlen(dat));
break;
case R_230: /* User logged in, proceed */
printf("\r\nUser logged in, proceed\r\n");
sprintf(dat,"TYPE %c\r\n", TransferAscii);
ftpc.type = ASCII_TYPE;
send(CTRL_SOCK, (uint8_t *)dat, strlen(dat));
break;
case R_200:
if((ftpc.dsock_mode==ACTIVE_MODE)&&gModeActivePassiveflag){
ftpc.dsock_state = DATASOCK_READY;
gModeActivePassiveflag = 0;
}
else{
gMenuStart = 1;
}
break;
case R_150:
switch(Command.First){
case f_dir:
Command.First = f_nocmd;
Command.Second = s_dir;
gDataPutGetStart = 1;
break;
case f_get:
Command.First = f_nocmd;
Command.Second = s_get;
gDataPutGetStart = 1;
break;
case f_put:
Command.First = f_nocmd;
Command.Second = s_put;
gDataPutGetStart = 1;
break;
default :
printf("Command.First = default\r\n");
break;
}
break;
case R_226:
gMenuStart = 1;
break;
case R_227:
if (pportc(buf) == -1){
printf("Bad port syntax\r\n");
}
else{
printf("Go Open Data Sock...\r\n ");
ftpc.dsock_mode = PASSIVE_MODE;
ftpc.dsock_state = DATASOCK_READY;
}
break;
default:
printf("\r\nDefault Status = %d\r\n",(uint16_t)Responses);
gDataSockReady = 1;
break;
}
return 1;
}
int pportc(char * arg)
{
int i;
char* tok=0;
strtok(arg,"(");
for (i = 0; i < 4; i++)
{
if(i==0) tok = strtok(NULL,",\r\n");
else tok = strtok(NULL,",");
remote_ip.cVal[i] = (uint8_t)atoi(tok, 10);
if (!tok){
printf("bad pport : %s\r\n", arg);
return -1;
}
}
remote_port = 0;
for (i = 0; i < 2; i++){
tok = strtok(NULL,",\r\n");
remote_port <<= 8;
remote_port += atoi(tok, 10);
if (!tok){
printf("bad pport : %s\r\n", arg);
return -1;
}
}
printf("ip : %d.%d.%d.%d, port : %d\r\n", remote_ip.cVal[0], remote_ip.cVal[1], remote_ip.cVal[2], remote_ip.cVal[3], remote_port);
return 0;
}
uint8_t* User_Keyboard_MSG()
{
uint8_t i=0;
do{
gMsgBuf[i] = ftp_getc();
i++;
}while(gMsgBuf[i-1]!=0x0d);
gMsgBuf[i-1]=0;
return gMsgBuf;
}

119
Internet/FTPClient/ftpc.h Normal file
View File

@ -0,0 +1,119 @@
#ifndef _FTPC_H_
#define _FTPC_H_
#include <stdint.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <limits.h>
#include <stdarg.h>
#include "stdio_private.h"
#include "socket.h"
#define F_APP_FTPC
/* If your target support a file system, you have to activate this feature and implement. */
//#define F_FILESYSTEM
/* Change to your Chipset Uart function, you have to activate this feature and implement.
* Change!! -> Board_UARTGetCharBlocking()
* Below is an example of a function of lpc_chip library. */
//#define ftp_getc() Board_UARTGetCharBlocking()
#ifdef F_FILESYSTEM
#include "ff.h"
#endif
#ifndef ftp_getc()
#define Need_UARTGetCharBlocking_func
#else
/* Change library
* Change!! -> board_api.h,
* Below is an example of a function of lpc_chip library. */
#include "board_api.h"
#endif
#define LINELEN 100
#ifndef F_FILESYSTEM
#define _MAX_SS 512
#endif
#define CTRL_SOCK 2
#define DATA_SOCK 3
/* FTP Responses */
#define R_150 150 /* File status ok; opening data conn */
#define R_200 200 /* 'Generic' command ok */
#define R_220 220 /* Service ready for new user. */
#define R_226 226 /* Closing data connection. File transfer/abort successful */
#define R_227 227 /* Entering passive mode (h1,h2,h3,h4,p1,p2) */
#define R_230 230 /* User logged in, proceed */
#define R_331 331 /* User name okay, need password. */
#define TransferAscii 'A'
#define TransferBinary 'I'
enum ftpc_type {
ASCII_TYPE,
IMAGE_TYPE,
};
enum ftpc_datasock_state{
DATASOCK_IDLE,
DATASOCK_READY,
DATASOCK_START
};
enum ftpc_datasock_mode{
PASSIVE_MODE,
ACTIVE_MODE
};
enum CommandFirst {
f_nocmd,
f_dir,
f_put,
f_get,
};
enum CommandSecond {
s_nocmd,
s_dir,
s_put,
s_get,
};
struct Command {
enum CommandFirst First;
enum CommandSecond Second;
};
struct ftpc {
uint8_t control; /* Control stream */
uint8_t data; /* Data stream */
enum ftpc_type type; /* Transfer type */
enum ftpc_datasock_state dsock_state;
enum ftpc_datasock_mode dsock_mode;
char workingdir[LINELEN];
char filename[LINELEN];
#ifdef F_FILESYSTEM
FIL fil; // FatFs File objects
FRESULT fr; // FatFs function common result code
#endif
};
#ifndef un_I2cval
typedef union _un_l2cval {
uint32_t lVal;
uint8_t cVal[4];
}un_l2cval;
#endif
void ftpc_init(uint8_t * src_ip);
uint8_t ftpc_run(uint8_t * dbuf);
char proc_ftpc(char * buf);
int pportc(char * arg);
uint8_t* User_Keyboard_MSG();
#endif // _FTPC_H_

View File

@ -0,0 +1,67 @@
/* Copyright (c) 2002, Joerg Wunsch
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
* Neither the name of the copyright holders nor the names of
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
/* $Id: stdio_private.h,v 1.6 2003/01/07 22:17:24 joerg_wunsch Exp $ */
#include <stdint.h>
#include <stdio.h>
struct __file {
char *buf; /* buffer pointer */
unsigned char unget; /* ungetc() buffer */
uint8_t flags; /* flags, see below */
#define __SRD 0x0001 /* OK to read */
#define __SWR 0x0002 /* OK to write */
#define __SSTR 0x0004 /* this is an sprintf/snprintf string */
#define __SPGM 0x0008 /* fmt string is in progmem */
#define __SERR 0x0010 /* found error */
#define __SEOF 0x0020 /* found EOF */
#define __SUNGET 0x040 /* ungetc() happened */
#if 0
/* possible future extensions, will require uint16_t flags */
#define __SRW 0x0080 /* open for reading & writing */
#define __SLBF 0x0100 /* line buffered */
#define __SNBF 0x0200 /* unbuffered */
#define __SMBF 0x0400 /* buf is from malloc */
#endif
int size; /* size of buffer */
int len; /* characters read or written so far */
int (*put)(char); /* function to write one char to device */
int (*get)(void); /* function to read one char from device */
};
/* values for PRINTF_LEVEL */
#define PRINTF_MIN 1
#define PRINTF_STD 2
#define PRINTF_FLT 3
/* values for SCANF_LEVEL */
#define SCANF_MIN 1
#define SCANF_STD 2
#define SCANF_FLT 3

View File

@ -147,7 +147,7 @@ uint8_t ftpd_run(uint8_t * dbuf)
{ {
uint16_t size = 0, i; uint16_t size = 0, i;
long ret = 0; long ret = 0;
uint16_t blocklen, send_byte, recv_byte; uint32_t blocklen, send_byte, recv_byte;
uint32_t remain_filesize; uint32_t remain_filesize;
uint32_t remain_datasize; uint32_t remain_datasize;
#if defined(F_FILESYSTEM) #if defined(F_FILESYSTEM)
@ -322,7 +322,7 @@ uint8_t ftpd_run(uint8_t * dbuf)
else else
send_byte = remain_filesize; send_byte = remain_filesize;
ftp.fr = f_read(&(ftp.fil), dbuf, send_byte , (void *)&blocklen); ftp.fr = f_read(&(ftp.fil), dbuf, send_byte , &blocklen);
if(ftp.fr != FR_OK) if(ftp.fr != FR_OK)
break; break;
#if defined(_FTP_DEBUG_) #if defined(_FTP_DEBUG_)
@ -389,7 +389,7 @@ uint8_t ftpd_run(uint8_t * dbuf)
//printf("----->fn:%s data:%s \r\n", ftp.filename, dbuf); //printf("----->fn:%s data:%s \r\n", ftp.filename, dbuf);
#endif #endif
ftp.fr = f_write(&(ftp.fil), dbuf, (UINT)ret, (void *)&blocklen); ftp.fr = f_write(&(ftp.fil), dbuf, (UINT)ret, &blocklen);
#if defined(_FTP_DEBUG_) #if defined(_FTP_DEBUG_)
//printf("----->dsize:%d recv:%d len:%d \r\n", remain_datasize, ret, blocklen); //printf("----->dsize:%d recv:%d len:%d \r\n", remain_datasize, ret, blocklen);
#endif #endif
@ -766,6 +766,7 @@ char proc_ftpd(char * buf)
break; break;
case PWD_CMD: case PWD_CMD:
case XPWD_CMD:
slen = sprintf(sendbuf, "257 \"%s\" is current directory.\r\n", ftp.workingdir); slen = sprintf(sendbuf, "257 \"%s\" is current directory.\r\n", ftp.workingdir);
send(CTRL_SOCK, (uint8_t *)sendbuf, slen); send(CTRL_SOCK, (uint8_t *)sendbuf, slen);
break; break;
@ -840,6 +841,7 @@ char proc_ftpd(char * buf)
break; break;
case MKD_CMD: case MKD_CMD:
case XMKD_CMD:
slen = strlen(arg); slen = strlen(arg);
arg[slen - 1] = 0x00; arg[slen - 1] = 0x00;
arg[slen - 2] = 0x00; arg[slen - 2] = 0x00;
@ -879,9 +881,7 @@ char proc_ftpd(char * buf)
break; break;
case XCWD_CMD: case XCWD_CMD:
case XPWD_CMD:
case ACCT_CMD: case ACCT_CMD:
case XMKD_CMD:
case XRMD_CMD: case XRMD_CMD:
case RMD_CMD: case RMD_CMD:
case STRU_CMD: case STRU_CMD:

450
Internet/SNTP/sntp.c Normal file
View File

@ -0,0 +1,450 @@
/*
* sntp.c
*
* Created on: 2014. 12. 15.
* Author: Administrator
*/
#include <string.h>
#include "sntp.h"
#include "socket.h"
ntpformat NTPformat;
datetime Nowdatetime;
uint8_t ntpmessage[48];
uint8_t *data_buf;
uint8_t NTP_SOCKET;
uint8_t time_zone;
uint16_t ntp_retry_cnt=0; //counting the ntp retry number
/*
00)UTC-12:00 Baker Island, Howland Island (both uninhabited)
01) UTC-11:00 American Samoa, Samoa
02) UTC-10:00 (Summer)French Polynesia (most), United States (Aleutian Islands, Hawaii)
03) UTC-09:30 Marquesas Islands
04) UTC-09:00 Gambier Islands;(Summer)United States (most of Alaska)
05) UTC-08:00 (Summer)Canada (most of British Columbia), Mexico (Baja California)
06) UTC-08:00 United States (California, most of Nevada, most of Oregon, Washington (state))
07) UTC-07:00 Mexico (Sonora), United States (Arizona); (Summer)Canada (Alberta)
08) UTC-07:00 Mexico (Chihuahua), United States (Colorado)
09) UTC-06:00 Costa Rica, El Salvador, Ecuador (Galapagos Islands), Guatemala, Honduras
10) UTC-06:00 Mexico (most), Nicaragua;(Summer)Canada (Manitoba, Saskatchewan), United States (Illinois, most of Texas)
11) UTC-05:00 Colombia, Cuba, Ecuador (continental), Haiti, Jamaica, Panama, Peru
12) UTC-05:00 (Summer)Canada (most of Ontario, most of Quebec)
13) UTC-05:00 United States (most of Florida, Georgia, Massachusetts, most of Michigan, New York, North Carolina, Ohio, Washington D.C.)
14) UTC-04:30 Venezuela
15) UTC-04:00 Bolivia, Brazil (Amazonas), Chile (continental), Dominican Republic, Canada (Nova Scotia), Paraguay,
16) UTC-04:00 Puerto Rico, Trinidad and Tobago
17) UTC-03:30 Canada (Newfoundland)
18) UTC-03:00 Argentina; (Summer) Brazil (Brasilia, Rio de Janeiro, Sao Paulo), most of Greenland, Uruguay
19) UTC-02:00 Brazil (Fernando de Noronha), South Georgia and the South Sandwich Islands
20) UTC-01:00 Portugal (Azores), Cape Verde
21) UTC&#177;00:00 Cote d'Ivoire, Faroe Islands, Ghana, Iceland, Senegal; (Summer) Ireland, Portugal (continental and Madeira)
22) UTC&#177;00:00 Spain (Canary Islands), Morocco, United Kingdom
23) UTC+01:00 Angola, Cameroon, Nigeria, Tunisia; (Summer)Albania, Algeria, Austria, Belgium, Bosnia and Herzegovina,
24) UTC+01:00 Spain (continental), Croatia, Czech Republic, Denmark, Germany, Hungary, Italy, Kinshasa, Kosovo,
25) UTC+01:00 Macedonia, France (metropolitan), the Netherlands, Norway, Poland, Serbia, Slovakia, Slovenia, Sweden, Switzerland
26) UTC+02:00 Libya, Egypt, Malawi, Mozambique, South Africa, Zambia, Zimbabwe, (Summer)Bulgaria, Cyprus, Estonia,
27) UTC+02:00 Finland, Greece, Israel, Jordan, Latvia, Lebanon, Lithuania, Moldova, Palestine, Romania, Syria, Turkey, Ukraine
28) UTC+03:00 Belarus, Djibouti, Eritrea, Ethiopia, Iraq, Kenya, Madagascar, Russia (Kaliningrad Oblast), Saudi Arabia,
29) UTC+03:00 South Sudan, Sudan, Somalia, South Sudan, Tanzania, Uganda, Yemen
30) UTC+03:30 (Summer)Iran
31) UTC+04:00 Armenia, Azerbaijan, Georgia, Mauritius, Oman, Russia (European), Seychelles, United Arab Emirates
32) UTC+04:30 Afghanistan
33) UTC+05:00 Kazakhstan (West), Maldives, Pakistan, Uzbekistan
34) UTC+05:30 India, Sri Lanka
35) UTC+05:45 Nepal
36) UTC+06:00 Kazakhstan (most), Bangladesh, Russia (Ural: Sverdlovsk Oblast, Chelyabinsk Oblast)
37) UTC+06:30 Cocos Islands, Myanmar
38) UTC+07:00 Jakarta, Russia (Novosibirsk Oblast), Thailand, Vietnam
39) UTC+08:00 China, Hong Kong, Russia (Krasnoyarsk Krai), Malaysia, Philippines, Singapore, Taiwan, most of Mongolia, Western Australia
40) UTC+09:00 Korea, East Timor, Russia (Irkutsk Oblast), Japan
41) UTC+09:30 Australia (Northern Territory);(Summer)Australia (South Australia))
42) UTC+10:00 Russia (Zabaykalsky Krai); (Summer)Australia (New South Wales, Queensland, Tasmania, Victoria)
43) UTC+10:30 Lord Howe Island
44) UTC+11:00 New Caledonia, Russia (Primorsky Krai), Solomon Islands
45) UTC+11:30 Norfolk Island
46) UTC+12:00 Fiji, Russia (Kamchatka Krai);(Summer)New Zealand
47) UTC+12:45 (Summer)New Zealand
48) UTC+13:00 Tonga
49) UTC+14:00 Kiribati (Line Islands)
*/
void get_seconds_from_ntp_server(uint8_t *buf, uint16_t idx)
{
tstamp seconds = 0;
uint8_t i=0;
for (i = 0; i < 4; i++)
{
seconds = (seconds << 8) | buf[idx + i];
}
switch (time_zone)
{
case 0:
seconds -= 12*3600;
break;
case 1:
seconds -= 11*3600;
break;
case 2:
seconds -= 10*3600;
break;
case 3:
seconds -= (9*3600+30*60);
break;
case 4:
seconds -= 9*3600;
break;
case 5:
case 6:
seconds -= 8*3600;
break;
case 7:
case 8:
seconds -= 7*3600;
break;
case 9:
case 10:
seconds -= 6*3600;
break;
case 11:
case 12:
case 13:
seconds -= 5*3600;
break;
case 14:
seconds -= (4*3600+30*60);
break;
case 15:
case 16:
seconds -= 4*3600;
break;
case 17:
seconds -= (3*3600+30*60);
break;
case 18:
seconds -= 3*3600;
break;
case 19:
seconds -= 2*3600;
break;
case 20:
seconds -= 1*3600;
break;
case 21: //<2F><>?
case 22:
break;
case 23:
case 24:
case 25:
seconds += 1*3600;
break;
case 26:
case 27:
seconds += 2*3600;
break;
case 28:
case 29:
seconds += 3*3600;
break;
case 30:
seconds += (3*3600+30*60);
break;
case 31:
seconds += 4*3600;
break;
case 32:
seconds += (4*3600+30*60);
break;
case 33:
seconds += 5*3600;
break;
case 34:
seconds += (5*3600+30*60);
break;
case 35:
seconds += (5*3600+45*60);
break;
case 36:
seconds += 6*3600;
break;
case 37:
seconds += (6*3600+30*60);
break;
case 38:
seconds += 7*3600;
break;
case 39:
seconds += 8*3600;
break;
case 40:
seconds += 9*3600;
break;
case 41:
seconds += (9*3600+30*60);
break;
case 42:
seconds += 10*3600;
break;
case 43:
seconds += (10*3600+30*60);
break;
case 44:
seconds += 11*3600;
break;
case 45:
seconds += (11*3600+30*60);
break;
case 46:
seconds += 12*3600;
break;
case 47:
seconds += (12*3600+45*60);
break;
case 48:
seconds += 13*3600;
break;
case 49:
seconds += 14*3600;
break;
}
//calculation for date
calcdatetime(seconds);
}
void SNTP_init(uint8_t s, uint8_t *ntp_server, uint8_t tz, uint8_t *buf)
{
NTP_SOCKET = s;
NTPformat.dstaddr[0] = ntp_server[0];
NTPformat.dstaddr[1] = ntp_server[1];
NTPformat.dstaddr[2] = ntp_server[2];
NTPformat.dstaddr[3] = ntp_server[3];
time_zone = tz;
data_buf = buf;
uint8_t Flag;
NTPformat.leap = 0; /* leap indicator */
NTPformat.version = 4; /* version number */
NTPformat.mode = 3; /* mode */
NTPformat.stratum = 0; /* stratum */
NTPformat.poll = 0; /* poll interval */
NTPformat.precision = 0; /* precision */
NTPformat.rootdelay = 0; /* root delay */
NTPformat.rootdisp = 0; /* root dispersion */
NTPformat.refid = 0; /* reference ID */
NTPformat.reftime = 0; /* reference time */
NTPformat.org = 0; /* origin timestamp */
NTPformat.rec = 0; /* receive timestamp */
NTPformat.xmt = 1; /* transmit timestamp */
Flag = (NTPformat.leap<<6)+(NTPformat.version<<3)+NTPformat.mode; //one byte Flag
memcpy(ntpmessage,(void const*)(&Flag),1);
}
int8_t SNTP_run(datetime *time)
{
uint16_t RSR_len;
uint32_t destip = 0;
uint16_t destport;
uint16_t startindex = 40; //last 8-byte of data_buf[size is 48 byte] is xmt, so the startindex should be 40
switch(getSn_SR(NTP_SOCKET))
{
case SOCK_UDP:
if ((RSR_len = getSn_RX_RSR(NTP_SOCKET)) > 0)
{
if (RSR_len > MAX_SNTP_BUF_SIZE) RSR_len = MAX_SNTP_BUF_SIZE; // if Rx data size is lager than TX_RX_MAX_BUF_SIZE
recvfrom(NTP_SOCKET, data_buf, RSR_len, (uint8_t *)&destip, &destport);
get_seconds_from_ntp_server(data_buf,startindex);
time->yy = Nowdatetime.yy;
time->mo = Nowdatetime.mo;
time->dd = Nowdatetime.dd;
time->hh = Nowdatetime.hh;
time->mm = Nowdatetime.mm;
time->ss = Nowdatetime.ss;
ntp_retry_cnt=0;
close(NTP_SOCKET);
return 1;
}
if(ntp_retry_cnt<0xFFFF)
{
if(ntp_retry_cnt==0)//first send request, no need to wait
{
sendto(NTP_SOCKET,ntpmessage,sizeof(ntpmessage),NTPformat.dstaddr,ntp_port);
ntp_retry_cnt++;
}
else // send request again? it should wait for a while
{
if((ntp_retry_cnt % 0xFFF) == 0) //wait time
{
sendto(NTP_SOCKET,ntpmessage,sizeof(ntpmessage),NTPformat.dstaddr,ntp_port);
#ifdef _SNTP_DEBUG_
printf("ntp retry: %d\r\n", ntp_retry_cnt);
#endif
ntp_retry_cnt++;
}
}
}
else //ntp retry fail
{
ntp_retry_cnt=0;
#ifdef _SNTP_DEBUG_
printf("ntp retry failed!\r\n");
#endif
close(NTP_SOCKET);
}
break;
case SOCK_CLOSED:
socket(NTP_SOCKET,Sn_MR_UDP,ntp_port,0);
break;
}
// Return value
// 0 - failed / 1 - success
return 0;
}
void calcdatetime(tstamp seconds)
{
uint8_t yf=0;
tstamp n=0,d=0,total_d=0,rz=0;
uint16_t y=0,r=0,yr=0;
signed long long yd=0;
n = seconds;
total_d = seconds/(SECS_PERDAY);
d=0;
uint32_t p_year_total_sec=SECS_PERDAY*365;
uint32_t r_year_total_sec=SECS_PERDAY*366;
while(n>=p_year_total_sec)
{
if((EPOCH+r)%400==0 || ((EPOCH+r)%100!=0 && (EPOCH+r)%4==0))
{
n = n -(r_year_total_sec);
d = d + 366;
}
else
{
n = n - (p_year_total_sec);
d = d + 365;
}
r+=1;
y+=1;
}
y += EPOCH;
Nowdatetime.yy = y;
yd=0;
yd = total_d - d;
yf=1;
while(yd>=28)
{
if(yf==1 || yf==3 || yf==5 || yf==7 || yf==8 || yf==10 || yf==12)
{
yd -= 31;
if(yd<0)break;
rz += 31;
}
if (yf==2)
{
if (y%400==0 || (y%100!=0 && y%4==0))
{
yd -= 29;
if(yd<0)break;
rz += 29;
}
else
{
yd -= 28;
if(yd<0)break;
rz += 28;
}
}
if(yf==4 || yf==6 || yf==9 || yf==11 )
{
yd -= 30;
if(yd<0)break;
rz += 30;
}
yf += 1;
}
Nowdatetime.mo=yf;
yr = total_d-d-rz;
yr += 1;
Nowdatetime.dd=yr;
//calculation for time
seconds = seconds%SECS_PERDAY;
Nowdatetime.hh = seconds/3600;
Nowdatetime.mm = (seconds%3600)/60;
Nowdatetime.ss = (seconds%3600)%60;
}
tstamp changedatetime_to_seconds(void)
{
tstamp seconds=0;
uint32_t total_day=0;
uint16_t i=0,run_year_cnt=0,l=0;
l = Nowdatetime.yy;//low
for(i=EPOCH;i<l;i++)
{
if((i%400==0) || ((i%100!=0) && (i%4==0)))
{
run_year_cnt += 1;
}
}
total_day=(l-EPOCH-run_year_cnt)*365+run_year_cnt*366;
for(i=1;i<=Nowdatetime.mo;i++)
{
if(i==5 || i==7 || i==10 || i==12)
{
total_day += 30;
}
if (i==3)
{
if (l%400==0 && l%100!=0 && l%4==0)
{
total_day += 29;
}
else
{
total_day += 28;
}
}
if(i==2 || i==4 || i==6 || i==8 || i==9 || i==11)
{
total_day += 31;
}
}
seconds = (total_day+Nowdatetime.dd-1)*24*3600;
seconds += Nowdatetime.ss;//seconds
seconds += Nowdatetime.mm*60;//minute
seconds += Nowdatetime.hh*3600;//hour
return seconds;
}

68
Internet/SNTP/sntp.h Normal file
View File

@ -0,0 +1,68 @@
/*
* sntp.h
*
* Created on: 2014. 12. 15.
* Author: Administrator
*/
#ifndef SNTP_H_
#define SNTP_H_
#include <stdint.h>
/*
* @brief Define it for Debug & Monitor DNS processing.
* @note If defined, it dependens on <stdio.h>
*/
//#define _SNTP_DEBUG_
#define MAX_SNTP_BUF_SIZE sizeof(ntpformat) ///< maximum size of DNS buffer. */
/* for ntpclient */
typedef signed char s_char;
typedef unsigned long long tstamp;
typedef unsigned int tdist;
typedef struct _ntpformat
{
uint8_t dstaddr[4]; /* destination (local) address */
char version; /* version number */
char leap; /* leap indicator */
char mode; /* mode */
char stratum; /* stratum */
char poll; /* poll interval */
s_char precision; /* precision */
tdist rootdelay; /* root delay */
tdist rootdisp; /* root dispersion */
char refid; /* reference ID */
tstamp reftime; /* reference time */
tstamp org; /* origin timestamp */
tstamp rec; /* receive timestamp */
tstamp xmt; /* transmit timestamp */
} ntpformat;
typedef struct _datetime
{
uint16_t yy;
uint8_t mo;
uint8_t dd;
uint8_t hh;
uint8_t mm;
uint8_t ss;
} datetime;
#define ntp_port 123 //ntp server port number
#define SECS_PERDAY 86400UL // seconds in a day = 60*60*24
#define UTC_ADJ_HRS 9 // SEOUL : GMT+9
#define EPOCH 1900 // NTP start year
void get_seconds_from_ntp_server(uint8_t *buf, uint16_t idx);
void SNTP_init(uint8_t s, uint8_t *ntp_server, uint8_t tz, uint8_t *buf);
int8_t SNTP_run(datetime *time);
tstamp changedatetime_to_seconds(void);
void calcdatetime(tstamp seconds);
#endif /* SNTP_H_ */

158
Internet/TFTP/netutil.c Normal file
View File

@ -0,0 +1,158 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "netutil.h"
/**
* Convert a 32bit Address into a Dotted Decimal Format string.
*
* @param addr 32bit address.
* @return Dotted Decimal Format string.
*/
int8_t* inet_ntoa(uint32_t addr)
{
static int8_t addr_str[16];
memset(addr_str,0,16);
sprintf((char*)addr_str,"%d.%d.%d.%d",(int32_t)(addr>>24 & 0xFF),(int32_t)(addr>>16 & 0xFF),(int32_t)(addr>>8 & 0xFF),(int32_t)(addr & 0xFF));
return addr_str;
}
/**
* Convert a 32bit Address into a Dotted Decimal Format string.
* This is differ from inet_ntoa in fixed length.
*
* @param addr 32bit address.
* @return Dotted Decimal Format string.
*/
int8_t* inet_ntoa_pad(uint32_t addr)
{
static int8_t addr_str[16];
memset(addr_str,0,16);
sprintf((char*)addr_str,"%03d.%03d.%03d.%03d",(int32_t)(addr>>24 & 0xFF),(int32_t)(addr>>16 & 0xFF),(int32_t)(addr>>8 & 0xFF),(int32_t)(addr & 0xFF));
return addr_str;
}
/**
* Converts a string containing an (Ipv4) Internet Protocol decimal dotted address into a 32bit address.
*
* @param addr Dotted Decimal Format string.
* @return 32bit address.
*/
uint32_t inet_addr(uint8_t* addr)
{
int8_t i;
uint32_t inetaddr = 0;
int8_t taddr[30];
int8_t * nexttok;
int32_t num;
strcpy((char*)taddr,(char*)addr);
nexttok = taddr;
for(i = 0; i < 4 ; i++)
{
nexttok = (int8_t*)strtok((char*)nexttok,".");
if(nexttok[0] == '0' && nexttok[1] == 'x') num = strtol((char*)nexttok+2, NULL, 16);
else num = strtol((char*)nexttok, NULL, 10);
inetaddr = inetaddr << 8;
inetaddr |= (num & 0xFF);
nexttok = NULL;
}
return inetaddr;
}
/**
* Swap the byte order of 16bit(short) wide variable.
*
* @param i 16bit value to swap
* @return Swapped value
*/
uint16_t swaps(uint16_t i)
{
uint16_t ret=0;
ret = (i & 0xFF) << 8;
ret |= ((i >> 8)& 0xFF);
return ret;
}
/**
* Swap the byte order of 32bit(long) wide variable.
*
* @param l 32bit value to convert
* @return Swapped value
*/
uint32_t swapl(uint32_t l)
{
uint32_t ret=0;
ret = (l & 0xFF) << 24;
ret |= ((l >> 8) & 0xFF) << 16;
ret |= ((l >> 16) & 0xFF) << 8;
ret |= ((l >> 24) & 0xFF);
return ret;
}
/**
* htons function converts a unsigned short from host to TCP/IP network byte order (which is big-endian).
*
* @param hostshort The value to convert.
* @return The value in TCP/IP network byte order.
*/
uint16_t htons(uint16_t hostshort)
{
#ifdef SYSTEM_LITTLE_ENDIAN
return swaps(hostshort);
#else
return hostshort;
#endif
}
/**
* htonl function converts a unsigned long from host to TCP/IP network byte order (which is big-endian).
*
* @param hostlong The value to convert.
* @return The value in TCP/IP network byte order.
*/
uint32_t htonl(uint32_t hostlong)
{
#ifdef SYSTEM_LITTLE_ENDIAN
return swapl(hostlong);
#else
return hostlong;
#endif
}
/**
* ntohs function converts a unsigned short from TCP/IP network byte order
* to host byte order (which is little-endian on Intel processors).
*
* @param netshort The value to convert.
* @return A 16-bit number in host byte order
*/
uint32_t ntohs(uint16_t netshort)
{
#ifdef SYSTEM_LITTLE_ENDIAN
return htons(netshort);
#else
return netshort;
#endif
}
/**
* converts a unsigned long from TCP/IP network byte order to host byte order
* (which is little-endian on Intel processors).
*
* @param netlong The value to convert.
* @return A 16-bit number in host byte order
*/
uint32_t ntohl(uint32_t netlong)
{
#ifdef SYSTEM_LITTLE_ENDIAN
return swapl(netlong);
#else
return netlong;
#endif
}
/**
* @}
*/

19
Internet/TFTP/netutil.h Normal file
View File

@ -0,0 +1,19 @@
#ifndef __NETUTIL_H__
#define __NETUTIL_H__
#include <stdint.h>
#define SYSTEM_LITTLE_ENDIAN
int8_t* inet_ntoa(uint32_t addr);
int8_t* inet_ntoa_pad(uint32_t addr);
uint32_t inet_addr(uint8_t* addr);
uint16_t swaps(uint16_t i);
uint32_t swapl(uint32_t l);
uint16_t htons(uint16_t hostshort);
uint32_t htonl(uint32_t hostlong);
uint32_t ntohs(uint16_t netshort);
uint32_t ntohl(uint32_t netlong);
#endif

660
Internet/TFTP/tftp.c Normal file
View File

@ -0,0 +1,660 @@
/**
* @file tftp.c
* @brief TFTP Source File.
* @version 0.1.0
* @author Sang-sik Kim
*/
/* Includes -----------------------------------------------------*/
#include <string.h>
#include "tftp.h"
#include "socket.h"
#include "netutil.h"
/* define -------------------------------------------------------*/
/* typedef ------------------------------------------------------*/
/* Extern Variable ----------------------------------------------*/
/* Extern Functions ---------------------------------------------*/
#ifdef F_STORAGE
extern void save_data(uint8_t *data, uint32_t data_len, uint16_t block_number);
#endif
/* Global Variable ----------------------------------------------*/
static int g_tftp_socket = -1;
static uint8_t g_filename[FILE_NAME_SIZE];
static uint32_t g_server_ip = 0;
static uint16_t g_server_port = 0;
static uint16_t g_local_port = 0;
static uint32_t g_tftp_state = STATE_NONE;
static uint16_t g_block_num = 0;
static uint32_t g_timeout = 5;
static uint32_t g_resend_flag = 0;
static uint32_t tftp_time_cnt = 0;
static uint32_t tftp_retry_cnt = 0;
static uint8_t *g_tftp_rcv_buf = NULL;
static TFTP_OPTION default_tftp_opt = {
.code = (uint8_t *)"timeout",
.value = (uint8_t *)"5"
};
uint8_t g_progress_state = TFTP_PROGRESS;
#ifdef __TFTP_DEBUG__
int dbg_level = (INFO_DBG | ERROR_DBG | IPC_DBG);
#endif
/* static function define ---------------------------------------*/
static void set_filename(uint8_t *file, uint32_t file_size)
{
memcpy(g_filename, file, file_size);
}
static inline void set_server_ip(uint32_t ipaddr)
{
g_server_ip = ipaddr;
}
static inline uint32_t get_server_ip()
{
return g_server_ip;
}
static inline void set_server_port(uint16_t port)
{
g_server_port = port;
}
static inline uint16_t get_server_port()
{
return g_server_port;
}
static inline void set_local_port(uint16_t port)
{
g_local_port = port;
}
static inline uint16_t get_local_port()
{
return g_local_port;
}
static inline uint16_t genernate_port()
{
/* TODO */
return 0;
}
static inline void set_tftp_state(uint32_t state)
{
g_tftp_state = state;
}
static inline uint32_t get_tftp_state()
{
return g_tftp_state;
}
static inline void set_tftp_timeout(uint32_t timeout)
{
g_timeout = timeout;
}
static inline uint32_t get_tftp_timeout()
{
return g_timeout;
}
static inline void set_block_number(uint16_t block_number)
{
g_block_num = block_number;
}
static inline uint16_t get_block_number()
{
return g_block_num;
}
static int open_tftp_socket(uint8_t sock)
{
uint8_t sd, sck_state;
sd = socket(sock, Sn_MR_UDP, 51000, SF_IO_NONBLOCK);
if(sd != sock) {
//DBG_PRINT(ERROR_DBG, "[%s] socket error\r\n", __func__);
return -1;
}
do {
getsockopt(sd , SO_STATUS, &sck_state);
} while(sck_state != SOCK_UDP);
return sd;
}
static int send_udp_packet(int socket, uint8_t *packet, uint32_t len, uint32_t ip, uint16_t port)
{
int snd_len;
ip = htonl(ip);
snd_len = sendto(socket, packet, len, (uint8_t *)&ip, port);
if(snd_len != len) {
//DBG_PRINT(ERROR_DBG, "[%s] sendto error\r\n", __func__);
return -1;
}
return snd_len;
}
static int recv_udp_packet(int socket, uint8_t *packet, uint32_t len, uint32_t *ip, uint16_t *port)
{
int ret;
uint8_t sck_state;
uint16_t recv_len;
/* Receive Packet Process */
ret = getsockopt(socket, SO_STATUS, &sck_state);
if(ret != SOCK_OK) {
//DBG_PRINT(ERROR_DBG, "[%s] getsockopt SO_STATUS error\r\n", __func__);
return -1;
}
if(sck_state == SOCK_UDP) {
ret = getsockopt(socket, SO_RECVBUF, &recv_len);
if(ret != SOCK_OK) {
//DBG_PRINT(ERROR_DBG, "[%s] getsockopt SO_RECVBUF error\r\n", __func__);
return -1;
}
if(recv_len) {
recv_len = recvfrom(socket, packet, len, (uint8_t *)ip, port);
if(recv_len < 0) {
//DBG_PRINT(ERROR_DBG, "[%s] recvfrom error\r\n", __func__);
return -1;
}
*ip = ntohl(*ip);
return recv_len;
}
}
return -1;
}
static void close_tftp_socket(int socket)
{
close(socket);
}
static void init_tftp(void)
{
g_filename[0] = 0;
set_server_ip(0);
set_server_port(0);
set_local_port(0);
set_tftp_state(STATE_NONE);
set_block_number(0);
/* timeout flag */
g_resend_flag = 0;
tftp_retry_cnt = tftp_time_cnt = 0;
g_progress_state = TFTP_PROGRESS;
}
static void tftp_cancel_timeout(void)
{
if(g_resend_flag) {
g_resend_flag = 0;
tftp_retry_cnt = tftp_time_cnt = 0;
}
}
static void tftp_reg_timeout()
{
if(g_resend_flag == 0) {
g_resend_flag = 1;
tftp_retry_cnt = tftp_time_cnt = 0;
}
}
static void process_tftp_option(uint8_t *msg, uint32_t msg_len)
{
/* TODO Option Process */
}
static void send_tftp_rrq(uint8_t *filename, uint8_t *mode, TFTP_OPTION *opt, uint8_t opt_len)
{
uint8_t snd_buf[MAX_MTU_SIZE];
uint8_t *pkt = snd_buf;
uint32_t i, len;
*((uint16_t *)pkt) = htons(TFTP_RRQ);
pkt += 2;
strcpy((char *)pkt, (const char *)filename);
pkt += strlen((char *)filename) + 1;
strcpy((char *)pkt, (const char *)mode);
pkt += strlen((char *)mode) + 1;
for(i = 0 ; i < opt_len ; i++) {
strcpy((char *)pkt, (const char *)opt[i].code);
pkt += strlen((char *)opt[i].code) + 1;
strcpy((char *)pkt, (const char *)opt[i].value);
pkt += strlen((char *)opt[i].value) + 1;
}
len = pkt - snd_buf;
send_udp_packet(g_tftp_socket, snd_buf, len, get_server_ip(), TFTP_SERVER_PORT);
set_tftp_state(STATE_RRQ);
set_filename(filename, strlen((char *)filename) + 1);
tftp_reg_timeout();
#ifdef __TFTP_DEBUG__
DBG_PRINT(IPC_DBG, ">> TFTP RRQ : FileName(%s), Mode(%s)\r\n", filename, mode);
#endif
}
#if 0 // 2014.07.01 sskim
static void send_tftp_wrq(uint8_t *filename, uint8_t *mode, TFTP_OPTION *opt, uint8_t opt_len)
{
uint8_t snd_buf[MAX_MTU_SIZE];
uint8_t *pkt = snd_buf;
uint32_t i, len;
*((uint16_t *)pkt) = htons((uint16_t)TFTP_WRQ);
pkt += 2;
strcpy((char *)pkt, (const char *)filename);
pkt += strlen((char *)filename) + 1;
strcpy((char *)pkt, (const char *)mode);
pkt += strlen((char *)mode) + 1;
for(i = 0 ; i < opt_len ; i++) {
strcpy((char *)pkt, (const char *)opt[i].code);
pkt += strlen((char *)opt[i].code) + 1;
strcpy((char *)pkt, (const char *)opt[i].value);
pkt += strlen((char *)opt[i].value) + 1;
}
len = pkt - snd_buf;
send_udp_packet(g_tftp_socket , snd_buf, len, get_server_ip(), TFTP_SERVER_PORT);
set_tftp_state(STATE_WRQ);
set_filename(filename, strlen((char *)filename) + 1);
tftp_reg_timeout();
#ifdef __TFTP_DEBUG__
DBG_PRINT(IPC_DBG, ">> TFTP WRQ : FileName(%s), Mode(%s)\r\n", filename, mode);
#endif
}
#endif
#if 0 // 2014.07.01 sskim
static void send_tftp_data(uint16_t block_number, uint8_t *data, uint16_t data_len)
{
uint8_t snd_buf[MAX_MTU_SIZE];
uint8_t *pkt = snd_buf;
uint32_t len;
*((uint16_t *)pkt) = htons((uint16_t)TFTP_DATA);
pkt += 2;
*((uint16_t *)pkt) = htons(block_number);
pkt += 2;
memcpy(pkt, data, data_len);
pkt += data_len;
len = pkt - snd_buf;
send_udp_packet(g_tftp_socket , snd_buf, len, get_server_ip(), get_server_port());
tftp_reg_timeout();
#ifdef __TFTP_DEBUG__
DBG_PRINT(IPC_DBG, ">> TFTP DATA : Block Number(%d), Data Length(%d)\r\n", block_number, data_len);
#endif
}
#endif
static void send_tftp_ack(uint16_t block_number)
{
uint8_t snd_buf[4];
uint8_t *pkt = snd_buf;
*((uint16_t *)pkt) = htons((uint16_t)TFTP_ACK);
pkt += 2;
*((uint16_t *)pkt) = htons(block_number);
pkt += 2;
send_udp_packet(g_tftp_socket , snd_buf, 4, get_server_ip(), get_server_port());
tftp_reg_timeout();
#ifdef __TFTP_DEBUG__
DBG_PRINT(IPC_DBG, ">> TFTP ACK : Block Number(%d)\r\n", block_number);
#endif
}
#if 0 // 2014.07.01 sskim
static void send_tftp_oack(TFTP_OPTION *opt, uint8_t opt_len)
{
uint8_t snd_buf[MAX_MTU_SIZE];
uint8_t *pkt = snd_buf;
uint32_t i, len;
*((uint16_t *)pkt) = htons((uint16_t)TFTP_OACK);
pkt += 2;
for(i = 0 ; i < opt_len ; i++) {
strcpy((char *)pkt, (const char *)opt[i].code);
pkt += strlen((char *)opt[i].code) + 1;
strcpy((char *)pkt, (const char *)opt[i].value);
pkt += strlen((char *)opt[i].value) + 1;
}
len = pkt - snd_buf;
send_udp_packet(g_tftp_socket , snd_buf, len, get_server_ip(), get_server_port());
tftp_reg_timeout();
#ifdef __TFTP_DEBUG__
DBG_PRINT(IPC_DBG, ">> TFTP OACK \r\n");
#endif
}
#endif
#if 0 // 2014.07.01 sskim
static void send_tftp_error(uint16_t error_number, uint8_t *error_message)
{
uint8_t snd_buf[MAX_MTU_SIZE];
uint8_t *pkt = snd_buf;
uint32_t len;
*((uint16_t *)pkt) = htons((uint16_t)TFTP_ERROR);
pkt += 2;
*((uint16_t *)pkt) = htons(error_number);
pkt += 2;
strcpy((char *)pkt, (const char *)error_message);
pkt += strlen((char *)error_message) + 1;
len = pkt - snd_buf;
send_udp_packet(g_tftp_socket , snd_buf, len, get_server_ip(), get_server_port());
tftp_reg_timeout();
#ifdef __TFTP_DEBUG__
DBG_PRINT(IPC_DBG, ">> TFTP ERROR : Error Number(%d)\r\n", error_number);
#endif
}
#endif
static void recv_tftp_rrq(uint8_t *msg, uint32_t msg_len)
{
/* When TFTP Server Mode */
}
static void recv_tftp_wrq(uint8_t *msg, uint32_t msg_len)
{
/* When TFTP Server Mode */
}
static void recv_tftp_data(uint8_t *msg, uint32_t msg_len)
{
TFTP_DATA_T *data = (TFTP_DATA_T *)msg;
data->opcode = ntohs(data->opcode);
data->block_num = ntohs(data->block_num);
#ifdef __TFTP_DEBUG__
DBG_PRINT(IPC_DBG, "<< TFTP_DATA : opcode(%d), block_num(%d)\r\n", data->opcode, data->block_num);
#endif
switch(get_tftp_state())
{
case STATE_RRQ :
case STATE_OACK :
if(data->block_num == 1) {
set_tftp_state(STATE_DATA);
set_block_number(data->block_num);
#ifdef F_STORAGE
save_data(data->data, msg_len - 4, data->block_num);
#endif
tftp_cancel_timeout();
}
send_tftp_ack(data->block_num);
if((msg_len - 4) < TFTP_BLK_SIZE) {
init_tftp();
g_progress_state = TFTP_SUCCESS;
}
break;
case STATE_DATA :
if(data->block_num == (get_block_number() + 1)) {
set_block_number(data->block_num);
#ifdef F_STORAGE
save_data(data->data, msg_len - 4, data->block_num);
#endif
tftp_cancel_timeout();
}
send_tftp_ack(data->block_num);
if((msg_len - 4) < TFTP_BLK_SIZE) {
init_tftp();
g_progress_state = TFTP_SUCCESS;
}
break;
default :
/* invalid message */
break;
}
}
static void recv_tftp_ack(uint8_t *msg, uint32_t msg_len)
{
#ifdef __TFTP_DEBUG__
DBG_PRINT(IPC_DBG, "<< TFTP_ACK : \r\n");
#endif
switch(get_tftp_state())
{
case STATE_WRQ :
break;
case STATE_ACK :
break;
default :
/* invalid message */
break;
}
}
static void recv_tftp_oack(uint8_t *msg, uint32_t msg_len)
{
#ifdef __TFTP_DEBUG__
DBG_PRINT(IPC_DBG, "<< TFTP_OACK : \r\n");
#endif
switch(get_tftp_state())
{
case STATE_RRQ :
process_tftp_option(msg, msg_len);
set_tftp_state(STATE_OACK);
tftp_cancel_timeout();
send_tftp_ack(0);
break;
case STATE_WRQ :
process_tftp_option(msg, msg_len);
set_tftp_state(STATE_ACK);
tftp_cancel_timeout();
/* TODO DATA Transfer */
//send_tftp_data(...);
break;
default :
/* invalid message */
break;
}
}
static void recv_tftp_error(uint8_t *msg, uint32_t msg_len)
{
TFTP_ERROR_T *data= (TFTP_ERROR_T *)msg;
data->opcode = ntohs(data->opcode);
data->error_code = ntohs(data->error_code);
#ifdef __TFTP_DEBUG__
DBG_PRINT(IPC_DBG, "<< TFTP_ERROR : %d (%s)\r\n", data->error_code, data->error_msg);
DBG_PRINT(ERROR_DBG, "[%s] Error Code : %d (%s)\r\n", __func__, data->error_code, data->error_msg);
#endif
init_tftp();
g_progress_state = TFTP_FAIL;
}
static void recv_tftp_packet(uint8_t *packet, uint32_t packet_len, uint32_t from_ip, uint16_t from_port)
{
uint16_t opcode;
/* Verify Server IP */
if(from_ip != get_server_ip()) {
#ifdef __TFTP_DEBUG__
DBG_PRINT(ERROR_DBG, "[%s] Server IP faults\r\n", __func__);
DBG_PRINT(ERROR_DBG, "from IP : %08x, Server IP : %08x\r\n", from_ip, get_server_ip());
#endif
return;
}
opcode = ntohs(*((uint16_t *)packet));
/* Set Server Port */
if((get_tftp_state() == STATE_WRQ) || (get_tftp_state() == STATE_RRQ)) {
set_server_port(from_port);
#ifdef __TFTP_DEBUG__
DBG_PRINT(INFO_DBG, "[%s] Set Server Port : %d\r\n", __func__, from_port);
#endif
}
switch(opcode)
{
case TFTP_RRQ : /* When Server */
recv_tftp_rrq(packet, packet_len);
break;
case TFTP_WRQ : /* When Server */
recv_tftp_wrq(packet, packet_len);
break;
case TFTP_DATA :
recv_tftp_data(packet, packet_len);
break;
case TFTP_ACK :
recv_tftp_ack(packet, packet_len);
break;
case TFTP_OACK :
recv_tftp_oack(packet, packet_len);
break;
case TFTP_ERROR :
recv_tftp_error(packet, packet_len);
break;
default :
// Unknown Mesage
break;
}
}
/* Functions ----------------------------------------------------*/
void TFTP_init(uint8_t socket, uint8_t *buf)
{
init_tftp();
g_tftp_socket = open_tftp_socket(socket);
g_tftp_rcv_buf = buf;
}
void TFTP_exit(void)
{
init_tftp();
close_tftp_socket(g_tftp_socket);
g_tftp_socket = -1;
g_tftp_rcv_buf = NULL;
}
int TFTP_run(void)
{
uint16_t len, from_port;
uint32_t from_ip;
/* Timeout Process */
if(g_resend_flag) {
if(tftp_time_cnt >= g_timeout) {
switch(get_tftp_state()) {
case STATE_WRQ: // 미구현
break;
case STATE_RRQ:
send_tftp_rrq(g_filename, (uint8_t *)TRANS_BINARY, &default_tftp_opt, 1);
break;
case STATE_OACK:
case STATE_DATA:
send_tftp_ack(get_block_number());
break;
case STATE_ACK: // 미구현
break;
default:
break;
}
tftp_time_cnt = 0;
tftp_retry_cnt++;
if(tftp_retry_cnt >= 5) {
init_tftp();
g_progress_state = TFTP_FAIL;
}
}
}
/* Receive Packet Process */
len = recv_udp_packet(g_tftp_socket, g_tftp_rcv_buf, MAX_MTU_SIZE, &from_ip, &from_port);
if(len < 0) {
#ifdef __TFTP_DEBUG__
DBG_PRINT(ERROR_DBG, "[%s] recv_udp_packet error\r\n", __func__);
#endif
return g_progress_state;
}
recv_tftp_packet(g_tftp_rcv_buf, len, from_ip, from_port);
return g_progress_state;
}
void TFTP_read_request(uint32_t server_ip, uint8_t *filename)
{
set_server_ip(server_ip);
#ifdef __TFTP_DEBUG__
DBG_PRINT(INFO_DBG, "[%s] Set Tftp Server : %x\r\n", __func__, server_ip);
#endif
g_progress_state = TFTP_PROGRESS;
send_tftp_rrq(filename, (uint8_t *)TRANS_BINARY, &default_tftp_opt, 1);
}
void tftp_timeout_handler(void)
{
if(g_resend_flag)
tftp_time_cnt++;
}

93
Internet/TFTP/tftp.h Normal file
View File

@ -0,0 +1,93 @@
/**
* @file tftp.h
* @brief TFTP Header File.
* @version 0.1.0
* @author Sang-sik Kim
*/
#ifndef __TFTP_H__
#define __TFTP_H__
#include <stdint.h>
#define F_APP_TFTP
#define __TFTP_DEBUG__
#define F_STORAGE // If your target support a storage, you have to activate this feature and implement.
#define SOCK_TFTP 1
#define INFO_DBG 0x01
#define ERROR_DBG 0x02
#define DEBUG_DBG 0x04
#define IPC_DBG 0x08
#define DBG_PRINT(level, format, args...) { \
if(dbg_level & level) \
printf(format, ##args); \
}
#define NORMAL_MODE 0
#define TFTP_MODE 1
extern int dbg_level;
/* tftp message */
#define TFTP_RRQ 1
#define TFTP_WRQ 2
#define TFTP_DATA 3
#define TFTP_ACK 4
#define TFTP_ERROR 5
#define TFTP_OACK 6
/* tftp state */
#define STATE_NONE 0
#define STATE_RRQ 1
#define STATE_WRQ 2
#define STATE_DATA 3
#define STATE_ACK 4
#define STATE_OACK 5
/* tftp transfer mode */
#define TRANS_ASCII "netascii"
#define TRANS_BINARY "octet"
/* tftp progress state */
#define TFTP_PROGRESS 0
#define TFTP_FAIL 1
#define TFTP_SUCCESS 2
/* define */
#define TFTP_SERVER_PORT 69
#define TFTP_TEMP_PORT 51000
#define TFTP_BLK_SIZE 512
#define MAX_MTU_SIZE 1514
#define FILE_NAME_SIZE 20
//#define __TFTP_DEBUG__
/* typedef */
typedef struct tftp_data {
uint16_t opcode;
uint16_t block_num;
uint8_t data[0];
} TFTP_DATA_T;
typedef struct tftp_error {
uint16_t opcode;
uint16_t error_code;
uint8_t error_msg[0];
} TFTP_ERROR_T;
typedef struct tftp_option {
uint8_t *code;
uint8_t *value;
} TFTP_OPTION;
/* Functions */
void TFTP_init(uint8_t socket, uint8_t *buf);
void TFTP_exit(void);
int TFTP_run(void);
void TFTP_read_request(uint32_t server_ip, uint8_t *filename);
void tftp_timeout_handler(void);
#endif /*__TFTP_H__ */

View File

@ -9,8 +9,6 @@
#include "httpParser.h" #include "httpParser.h"
#include "httpUtil.h" #include "httpUtil.h"
#include "common.h"
#ifdef _USE_SDCARD_ #ifdef _USE_SDCARD_
#include "ff.h" // header file for FatFs library (FAT file system) #include "ff.h" // header file for FatFs library (FAT file system)
#endif #endif
@ -400,7 +398,7 @@ static void send_http_response_body(uint8_t s, uint8_t * uri_name, uint8_t * buf
read_userReg_webContent(start_addr, &buf[0], HTTPSock_Status[get_seqnum].file_offset, send_len); read_userReg_webContent(start_addr, &buf[0], HTTPSock_Status[get_seqnum].file_offset, send_len);
} }
#ifdef _USE_SDCARD_ #ifdef _USE_SDCARD_
else if(HTTPSock_Status[get_seqnum]->storage_type == SDCARD) else if(HTTPSock_Status[get_seqnum].storage_type == SDCARD)
{ {
// Data read from SD Card // Data read from SD Card
fr = f_read(&fs, &buf[0], send_len, (void *)&blocklen); fr = f_read(&fs, &buf[0], send_len, (void *)&blocklen);
@ -561,7 +559,7 @@ static void http_process_handler(uint8_t s, st_http_request * p_http_request)
file_len = fs.fsize; file_len = fs.fsize;
content_addr = fs.sclust; content_addr = fs.sclust;
HTTPSock_Status[get_seqnum]->storage_type = SDCARD; HTTPSock_Status[get_seqnum].storage_type = SDCARD;
} }
#elif _USE_FLASH_ #elif _USE_FLASH_
else if(/* Read content from Dataflash */) else if(/* Read content from Dataflash */)

View File

@ -1,6 +1,6 @@
#ioLibrary Driver #ioLibrary Driver
The ioLibrary means “Internet Offload Library” for WIZnet chip. It includes drivers and application protocols. The ioLibrary means “Internet Offload Library” for WIZnet chip. It includes drivers and application protocols.
The driver (ioLibrary) can be used for [W5500](http://wizwiki.net/wiki/doku.php?id=products:w5500:start) application designs. These will be updated continuously. The driver (ioLibrary) can be used for the application design of WIZnet TCP/IP chips as [W5500](http://wizwiki.net/wiki/doku.php?id=products:w5500:start), W5200, W5100 and etc.
##ioLibrary ##ioLibrary
This driver provides the Berkeley Socket type APIs. This driver provides the Berkeley Socket type APIs.
@ -14,5 +14,27 @@ This driver provides the Berkeley Socket type APIs.
- DNS client - DNS client
- Others will be added. - Others will be added.
## How to add an ioLibrary in project through github site.
- Example, refer to https://www.youtube.com/watch?v=mt815RBGdsA
- [ioLibrary Doxygen doument](https://github.com/Wiznet/ioLibrary_Driver/blob/master/Ethernet/SOCKET_APIs_V2.0.chm) : Refer to **TODO** in this document
- Define what chip is used in **wizchip_conf.h**
- Define what Host I/F mode is used in **wizchip_conf.h**
## Revision History ## Revision History
Last release : Nov. 2014 * ioLibrary V3.0.1 Released : 15, July, 2015
* Bug fixed : In W5100, Fixed CS control problem in read/write buffer with SPI. Refer to M20150715.
* ioLibrary V3.0 Released : 01, June, 2015
* Add to W5300
* Typing Error in comments
* Refer to 20150601 in sources.
* Type casting error Fixed : 09, April. 2015
In socket.c, send() : Refer to M20150409
* ioLibrary V2.0 released : April. 2015
* Added to W5100, W5200
* Correct to some typing error
* Fixed the warning of type casting.
* Last release : Nov. 2014