| 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
| RST | Reserved | WOL | PB | PPPoE | Reserved | FARP | Reserved |
| 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
| CONFLICT | UNREACH | PPPoE | MP | Reserved | Reserved | Reserved | Reserved |
| 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
| IM_IR7 | IM_IR6 | IM_IR5 | IM_IR4 | Reserved | Reserved | Reserved | Reserved |
| 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
| MULTI/MFEN | BCASTB | ND/MC/MMB | UCASTB/MIP6B | Protocol[3] | Protocol[2] | Protocol[1] | Protocol[0] |
| Protocol[3] | Protocol[2] | Protocol[1] | Protocol[0] | @b Meaning |
| 0 | 0 | 0 | 0 | Closed |
| 0 | 0 | 0 | 1 | TCP |
| 0 | 0 | 1 | 0 | UDP |
| 0 | 1 | 0 | 0 | MACRAW |
| 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
| Reserved | Reserved | Reserved | SEND_OK | TIMEOUT | RECV | DISCON | CON |
| \b Sn_MR (P[3:0]) | \b Sn_SR |
| Sn_MR_CLOSE (000) | |
| Sn_MR_TCP (001) | SOCK_INIT (0x13) |
| Sn_MR_UDP (010) | SOCK_UDP (0x22) |
| S0_MR_MACRAW (100) | SOCK_MACRAW (0x02) |
| API | WIZnet | Berkeley |
| socket() | O | O |
| bind() | X | O |
| listen() | O | O |
| connect() | O | O |
| accept() | X | O |
| recv() | O | O |
| send() | O | O |
| recvfrom() | O | O |
| sendto() | O | O |
| closesocket() | O close() & disconnect() | O |
| @b cstype | @b data type | @b value |
| @ref CS_SET_IOMODE \n @ref CS_GET_IOMODE | uint8_t | @ref SOCK_IO_BLOCK @ref SOCK_IO_NONBLOCK |
| @ref CS_GET_MAXTXBUF \n @ref CS_GET_MAXRXBUF | uint16_t | 0 ~ 16K |
| @ref CS_CLR_INTERRUPT \n @ref CS_GET_INTERRUPT \n @ref CS_SET_INTMASK \n @ref CS_GET_INTMASK | @ref sockint_kind | @ref SIK_CONNECTED, etc. |
| @b sotype | @b data type | @b value |
| @ref SO_TTL | uint8_t | 0 ~ 255 |
| @ref SO_TOS | uint8_t | 0 ~ 255 |
| @ref SO_MSS | uint16_t | 0 ~ 65535 |
| @ref SO_DESTIP | uint8_t[4] | |
| @ref SO_DESTPORT | uint16_t | 0 ~ 65535 |
| @ref SO_KEEPALIVESEND | null | null |
| @ref SO_KEEPALIVEAUTO | uint8_t | 0 ~ 255 |
| @b sotype | @b data type | @b value |
| @ref SO_FLAG | uint8_t | @ref SF_ETHER_OWN, etc... |
| @ref SO_TOS | uint8_t | 0 ~ 255 |
| @ref SO_MSS | uint16_t | 0 ~ 65535 |
| @ref SO_DESTIP | uint8_t[4] | |
| @ref SO_DESTPORT | uint16_t | |
| @ref SO_KEEPALIVEAUTO | uint8_t | 0 ~ 255 |
| @ref SO_SENDBUF | uint16_t | 0 ~ 65535 |
| @ref SO_RECVBUF | uint16_t | 0 ~ 65535 |
| @ref SO_STATUS | uint8_t | @ref SOCK_ESTABLISHED, etc.. |
| @ref SO_REMAINSIZE | uint16_t | 0~ 65535 |
| @ref SO_PACKINFO | uint8_t | @ref PACK_FIRST, etc... |
#define \_WIZCHIP_ W5500
+ */
+
+#define W5100 5100
+#define W5100S 5100+5
+#define W5200 5200
+#define W5300 5300
+#define W5500 5500
+
+#ifndef _WIZCHIP_
+#define _WIZCHIP_ W5500 // W5100, W5100S, W5200, W5300, W5500
+#endif
+
+#define _WIZCHIP_IO_MODE_NONE_ 0x0000
+#define _WIZCHIP_IO_MODE_BUS_ 0x0100 /**< Bus interface mode */
+#define _WIZCHIP_IO_MODE_SPI_ 0x0200 /**< SPI interface mode */
+//#define _WIZCHIP_IO_MODE_IIC_ 0x0400
+//#define _WIZCHIP_IO_MODE_SDIO_ 0x0800
+// Add to
+//
+
+#define _WIZCHIP_IO_MODE_BUS_DIR_ (_WIZCHIP_IO_MODE_BUS_ + 1) /**< BUS interface mode for direct */
+#define _WIZCHIP_IO_MODE_BUS_INDIR_ (_WIZCHIP_IO_MODE_BUS_ + 2) /**< BUS interface mode for indirect */
+
+#define _WIZCHIP_IO_MODE_SPI_VDM_ (_WIZCHIP_IO_MODE_SPI_ + 1) /**< SPI interface mode for variable length data*/
+#define _WIZCHIP_IO_MODE_SPI_FDM_ (_WIZCHIP_IO_MODE_SPI_ + 2) /**< SPI interface mode for fixed length data mode*/
+#define _WIZCHIP_IO_MODE_SPI_5500_ (_WIZCHIP_IO_MODE_SPI_ + 3) /**< SPI interface mode for fixed length data mode*/
+
+#if (_WIZCHIP_ == W5100)
+ #define _WIZCHIP_ID_ "W5100\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_
+ */
+// #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_SPI_
+
+//A20150601 : Define the unit of IO DATA.
+ typedef uint8_t iodata_t;
+//A20150401 : Indclude W5100.h file
+ #include "W5100/w5100.h"
+
+#elif (_WIZCHIP_ == W5100S)
+#define _WIZCHIP_ID_ "W5100S\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_
+*/
+// #define _WIZCHIP_IO_MODE_ _WIZCHIP_IO_MODE_BUS_INDIR_
+ //#define _WIZCHIP_IO_MODE_ _WIZCHIP_IO_MODE_SPI_5500_
+ #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 "W5100S/w5100s.h"
+#elif (_WIZCHIP_ == W5200)
+ #define _WIZCHIP_ID_ "W5200\0"
+/**
+ * @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_
+ */
+#ifndef _WIZCHIP_IO_MODE_
+// #define _WIZCHIP_IO_MODE_ _WIZCHIP_IO_MODE_BUS_INDIR_
+ #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"
+#elif (_WIZCHIP_ == W5500)
+ #define _WIZCHIP_ID_ "W5500\0"
+
+/**
+ * @brief Define interface mode. \n
+ * @todo Should select interface mode as chip.
+ * - @ref \_WIZCHIP_IO_MODE_SPI_ \n
+ * -@ref \_WIZCHIP_IO_MODE_SPI_VDM_ : Valid only in @ref \_WIZCHIP_ == W5500 \n
+ * -@ref \_WIZCHIP_IO_MODE_SPI_FDM_ : Valid only in @ref \_WIZCHIP_ == W5500 \n
+ * - @ref \_WIZCHIP_IO_MODE_BUS_ \n
+ * - @ref \_WIZCHIP_IO_MODE_BUS_DIR_ \n
+ * - @ref \_WIZCHIP_IO_MODE_BUS_INDIR_ \n
+ * - Others will be defined in future. \n\n
+ * ex> #define \_WIZCHIP_IO_MODE_ \_WIZCHIP_IO_MODE_SPI_VDM_
+ *
+ */
+#ifndef _WIZCHIP_IO_MODE_
+ //#define _WIZCHIP_IO_MODE_ _WIZCHIP_IO_MODE_SPI_FDM_
+ #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"
+#elif ( _WIZCHIP_ == W5300)
+ #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
+ #error "Unknown defined _WIZCHIP_. You should define one of 5100, 5200, and 5500 !!!"
+#endif
+
+#ifndef _WIZCHIP_IO_MODE_
+ #error "Undefined _WIZCHIP_IO_MODE_. You should define it !!!"
+#endif
+
+/**
+ * @brief Define I/O base address when BUS IF mode.
+ * @todo Should re-define it to fit your system when BUS IF Mode (@ref \_WIZCHIP_IO_MODE_BUS_,
+ * @ref \_WIZCHIP_IO_MODE_BUS_DIR_, @ref \_WIZCHIP_IO_MODE_BUS_INDIR_). \n\n
+ * ex> #define \_WIZCHIP_IO_BASE_ 0x00008000
+ */
+#if _WIZCHIP_IO_MODE_ & _WIZCHIP_IO_MODE_BUS_
+ #define _WIZCHIP_IO_BASE_ 0x60000000 // for 5100S IND
+#elif _WIZCHIP_IO_MODE_ & _WIZCHIP_IO_MODE_SPI_
+ #define _WIZCHIP_IO_BASE_ 0x00000000 // for 5100S SPI
+#endif
+
+#ifndef _WIZCHIP_IO_BASE_
+#define _WIZCHIP_IO_BASE_ 0x00000000 // 0x8000
+#endif
+
+//M20150401 : Typing Error
+//#if _WIZCHIP_IO_MODE_ & _WIZCHIP_IO_MODE_BUS
+#if _WIZCHIP_IO_MODE_ & _WIZCHIP_IO_MODE_BUS_
+ #ifndef _WIZCHIP_IO_BASE_
+ #error "You should be define _WIZCHIP_IO_BASE to fit your system memory map."
+ #endif
+#endif
+
+#if _WIZCHIP_ >= W5200
+ #define _WIZCHIP_SOCK_NUM_ 8 ///< The count of independant socket of @b WIZCHIP
+#else
+ #define _WIZCHIP_SOCK_NUM_ 4 ///< The count of independant socket of @b WIZCHIP
+#endif
+
+
+/********************************************************
+* WIZCHIP BASIC IF functions for SPI, SDIO, I2C , ETC.
+*********************************************************/
+/**
+ * @ingroup DATA_TYPE
+ * @brief The set of callback functions for W5500:@ref WIZCHIP_IO_Functions W5200:@ref WIZCHIP_IO_Functions_W5200
+ */
+typedef struct __WIZCHIP
+{
+ uint16_t if_mode; ///< host interface mode
+ uint8_t id[6]; ///< @b WIZCHIP ID such as @b 5100, @b 5200, @b 5500, and so on.
+ /**
+ * The set of critical section callback func.
+ */
+ struct _CRIS
+ {
+ void (*_enter) (void); ///< crtical section enter
+ void (*_exit) (void); ///< critial section exit
+ }CRIS;
+ /**
+ * The set of @ref \_WIZCHIP_ select control callback func.
+ */
+ struct _CS
+ {
+ void (*_select) (void); ///< @ref \_WIZCHIP_ selected
+ void (*_deselect)(void); ///< @ref \_WIZCHIP_ deselected
+ }CS;
+ /**
+ * The set of interface IO callback func.
+ */
+ union _IF
+ {
+ /**
+ * 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
+ {
+ iodata_t (*_read_data) (uint32_t AddrSel);
+ void (*_write_data) (uint32_t AddrSel, iodata_t wb);
+ }BUS;
+
+ /**
+ * For SPI interface IO
+ */
+ struct
+ {
+ uint8_t (*_read_byte) (void);
+ void (*_write_byte) (uint8_t wb);
+ void (*_read_burst) (uint8_t* pBuf, uint16_t len);
+ void (*_write_burst) (uint8_t* pBuf, uint16_t len);
+ }SPI;
+ // To be added
+ //
+ }IF;
+}_WIZCHIP;
+
+extern _WIZCHIP WIZCHIP;
+
+/**
+ * @ingroup DATA_TYPE
+ * WIZCHIP control type enumration used in @ref ctlwizchip().
+ */
+typedef enum
+{
+ CW_RESET_WIZCHIP, ///< Resets WIZCHIP by softly
+ CW_INIT_WIZCHIP, ///< Initializes to WIZCHIP with SOCKET buffer size 2 or 1 dimension array typed uint8_t.
+ CW_GET_INTERRUPT, ///< Get Interrupt status of WIZCHIP
+ CW_CLR_INTERRUPT, ///< Clears interrupt
+ CW_SET_INTRMASK, ///< Masks interrupt
+ CW_GET_INTRMASK, ///< Get interrupt mask
+ CW_SET_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.
+
+//D20150601 : For no modification your application code
+//#if _WIZCHIP_ == W5500
+ 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_GET_PHYCONF, ///< Get PHY operation mode in internal register. Valid Only W5500
+ 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
+//#endif
+//D20150601 : For no modification your application code
+//#if _WIZCHIP_ == W5200 || _WIZCHIP_ == W5500
+ 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;
+
+/**
+ * @ingroup DATA_TYPE
+ * Network control type enumration used in @ref ctlnetwork().
+ */
+typedef enum
+{
+ CN_SET_NETINFO, ///< Set Network with @ref wiz_NetInfo
+ CN_GET_NETINFO, ///< Get Network with @ref wiz_NetInfo
+ CN_SET_NETMODE, ///< Set network mode as WOL, PPPoE, Ping Block, and Force ARP mode
+ CN_GET_NETMODE, ///< Get network mode as WOL, PPPoE, Ping Block, and Force ARP mode
+ CN_SET_TIMEOUT, ///< Set network timeout as retry count and time.
+ CN_GET_TIMEOUT, ///< Get network timeout as retry count and time.
+}ctlnetwork_type;
+
+/**
+ * @ingroup DATA_TYPE
+ * Interrupt kind when CW_SET_INTRRUPT, CW_GET_INTERRUPT, CW_SET_INTRMASK
+ * and CW_GET_INTRMASK is used in @ref ctlnetwork().
+ * It can be used with OR operation.
+ */
+typedef enum
+{
+#if _WIZCHIP_ == W5500
+ IK_WOL = (1 << 4), ///< Wake On Lan by receiving the magic packet. Valid in W500.
+#elif _WIZCHIP_ == W5300
+ IK_FMTU = (1 << 4), ///< Received a ICMP message (Fragment MTU)
+#endif
+
+ IK_PPPOE_TERMINATED = (1 << 5), ///< PPPoE Disconnected
+
+#if _WIZCHIP_ != W5200
+ IK_DEST_UNREACH = (1 << 6), ///< Destination IP & Port Unreachable, No use in W5200
+#endif
+
+ IK_IP_CONFLICT = (1 << 7), ///< IP conflict occurred
+
+ IK_SOCK_0 = (1 << 8), ///< Socket 0 interrupt
+ IK_SOCK_1 = (1 << 9), ///< Socket 1 interrupt
+ IK_SOCK_2 = (1 << 10), ///< Socket 2 interrupt
+ IK_SOCK_3 = (1 << 11), ///< Socket 3 interrupt
+#if _WIZCHIP_ > W5100S
+ IK_SOCK_4 = (1 << 12), ///< Socket 4 interrupt, No use in 5100
+ IK_SOCK_5 = (1 << 13), ///< Socket 5 interrupt, No use in 5100
+ IK_SOCK_6 = (1 << 14), ///< Socket 6 interrupt, No use in 5100
+ IK_SOCK_7 = (1 << 15), ///< Socket 7 interrupt, No use in 5100
+#endif
+
+#if _WIZCHIP_ > W5100S
+ IK_SOCK_ALL = (0xFF << 8) ///< All Socket interrupt
+#else
+ IK_SOCK_ALL = (0x0F << 8) ///< All Socket interrupt
+#endif
+}intr_kind;
+
+#define PHY_CONFBY_HW 0 ///< Configured PHY operation mode by HW pin
+#define PHY_CONFBY_SW 1 ///< Configured PHY operation mode by SW register
+#define PHY_MODE_MANUAL 0 ///< Configured PHY operation mode with user setting.
+#define PHY_MODE_AUTONEGO 1 ///< Configured PHY operation mode with auto-negotiation
+#define PHY_SPEED_10 0 ///< Link Speed 10
+#define PHY_SPEED_100 1 ///< Link Speed 100
+#define PHY_DUPLEX_HALF 0 ///< Link Half-Duplex
+#define PHY_DUPLEX_FULL 1 ///< Link Full-Duplex
+#define PHY_LINK_OFF 0 ///< Link Off
+#define PHY_LINK_ON 1 ///< Link On
+#define PHY_POWER_NORM 0 ///< PHY power normal mode
+#define PHY_POWER_DOWN 1 ///< PHY power down mode
+
+
+#if _WIZCHIP_ == W5100S || _WIZCHIP_ == W5500
+/**
+ * @ingroup DATA_TYPE
+ * It configures PHY configuration when CW_SET PHYCONF or CW_GET_PHYCONF in W5500,
+ * and it indicates the real PHY status configured by HW or SW in all WIZCHIP. \n
+ * Valid only in W5500.
+ */
+typedef struct wiz_PhyConf_t
+{
+ uint8_t by; ///< set by @ref PHY_CONFBY_HW or @ref PHY_CONFBY_SW
+ uint8_t mode; ///< set by @ref PHY_MODE_MANUAL or @ref PHY_MODE_AUTONEGO
+ uint8_t speed; ///< set by @ref PHY_SPEED_10 or @ref PHY_SPEED_100
+ uint8_t duplex; ///< set by @ref PHY_DUPLEX_HALF @ref PHY_DUPLEX_FULL
+ //uint8_t power; ///< set by @ref PHY_POWER_NORM or @ref PHY_POWER_DOWN
+ //uint8_t link; ///< Valid only in CW_GET_PHYSTATUS. set by @ref PHY_LINK_ON or PHY_DUPLEX_OFF
+ }wiz_PhyConf;
+#endif
+
+/**
+ * @ingroup DATA_TYPE
+ * It used in setting dhcp_mode of @ref wiz_NetInfo.
+ */
+typedef enum
+{
+ NETINFO_STATIC = 1, ///< Static IP configuration by manually.
+ NETINFO_DHCP ///< Dynamic IP configruation from a DHCP sever
+}dhcp_mode;
+
+/**
+ * @ingroup DATA_TYPE
+ * Network Information for WIZCHIP
+ */
+typedef struct wiz_NetInfo_t
+{
+ uint8_t mac[6]; ///< Source Mac Address
+ uint8_t ip[4]; ///< Source IP Address
+ uint8_t sn[4]; ///< Subnet Mask
+ uint8_t gw[4]; ///< Gateway IP Address
+ uint8_t dns[4]; ///< DNS server IP Address
+ dhcp_mode dhcp; ///< 1 - Static, 2 - DHCP
+}wiz_NetInfo;
+
+/**
+ * @ingroup DATA_TYPE
+ * Network mode
+ */
+typedef enum
+{
+#if _WIZCHIP_ == W5500
+ NM_FORCEARP = (1<<1), ///< Force to APP send whenever udp data is sent. Valid only in W5500
+#endif
+ NM_WAKEONLAN = (1<<5), ///< Wake On Lan
+ NM_PINGBLOCK = (1<<4), ///< Block ping-request
+ NM_PPPOE = (1<<3), ///< PPPoE mode
+}netmode_type;
+
+/**
+ * @ingroup DATA_TYPE
+ * Used in CN_SET_TIMEOUT or CN_GET_TIMEOUT of @ref ctlwizchip() for timeout configruation.
+ */
+typedef struct wiz_NetTimeout_t
+{
+ uint8_t retry_cnt; ///< retry count
+ uint16_t time_100us; ///< time unit 100us
+}wiz_NetTimeout;
+
+/**
+ *@brief Registers call back function for critical section of I/O functions such as
+ *\ref WIZCHIP_READ, @ref WIZCHIP_WRITE, @ref WIZCHIP_READ_BUF and @ref WIZCHIP_WRITE_BUF.
+ *@param cris_en : callback function for critical section enter.
+ *@param cris_ex : callback function for critical section exit.
+ *@todo Describe @ref WIZCHIP_CRITICAL_ENTER and @ref WIZCHIP_CRITICAL_EXIT marco or register your functions.
+ *@note If you do not describe or register, default functions(@ref wizchip_cris_enter & @ref wizchip_cris_exit) is called.
+ */
+void reg_wizchip_cris_cbfunc(void(*cris_en)(void), void(*cris_ex)(void));
+
+
+/**
+ *@brief Registers call back function for WIZCHIP select & deselect.
+ *@param cs_sel : callback function for WIZCHIP select
+ *@param cs_desel : callback fucntion for WIZCHIP deselect
+ *@todo Describe @ref wizchip_cs_select and @ref wizchip_cs_deselect function or register your functions.
+ *@note If you do not describe or register, null function is called.
+ */
+void reg_wizchip_cs_cbfunc(void(*cs_sel)(void), void(*cs_desel)(void));
+
+/**
+ *@brief Registers call back function for bus interface.
+ *@param bus_rb : callback function to read byte data using system bus
+ *@param bus_wb : callback function to write byte data using system bus
+ *@todo Describe @ref wizchip_bus_readbyte and @ref wizchip_bus_writebyte function
+ *or register your functions.
+ *@note If you do not describe or register, null function is called.
+ */
+//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.
+ *@param spi_rb : callback function to read byte using SPI
+ *@param spi_wb : callback function to write byte using SPI
+ *@todo Describe \ref wizchip_spi_readbyte and \ref wizchip_spi_writebyte function
+ *or register your functions.
+ *@note If you do not describe or register, null function is called.
+ */
+void reg_wizchip_spi_cbfunc(uint8_t (*spi_rb)(void), void (*spi_wb)(uint8_t wb));
+
+/**
+ *@brief Registers call back function for SPI interface.
+ *@param spi_rb : callback function to burst read using SPI
+ *@param spi_wb : callback function to burst write using SPI
+ *@todo Describe \ref wizchip_spi_readbyte and \ref wizchip_spi_writebyte function
+ *or register your functions.
+ *@note If you do not describe or register, null function is called.
+ */
+void reg_wizchip_spiburst_cbfunc(void (*spi_rb)(uint8_t* pBuf, uint16_t len), void (*spi_wb)(uint8_t* pBuf, uint16_t len));
+
+/**
+ * @ingroup extra_functions
+ * @brief Controls to the WIZCHIP.
+ * @details Resets WIZCHIP & internal PHY, Configures PHY mode, Monitor PHY(Link,Speed,Half/Full/Auto),
+ * controls interrupt & mask and so on.
+ * @param cwtype : Decides to the control type
+ * @param arg : arg type is dependent on cwtype.
+ * @return 0 : Success \n
+ * -1 : Fail because of invalid \ref ctlwizchip_type or unsupported \ref ctlwizchip_type in WIZCHIP
+ */
+int8_t ctlwizchip(ctlwizchip_type cwtype, void* arg);
+
+/**
+ * @ingroup extra_functions
+ * @brief Controls to network.
+ * @details Controls to network environment, mode, timeout and so on.
+ * @param cntype : Input. Decides to the control type
+ * @param arg : Inout. arg type is dependent on cntype.
+ * @return -1 : Fail because of invalid \ref ctlnetwork_type or unsupported \ref ctlnetwork_type in WIZCHIP \n
+ * 0 : Success
+ */
+int8_t ctlnetwork(ctlnetwork_type cntype, void* arg);
+
+
+/*
+ * The following functions are implemented for internal use.
+ * but You can call these functions for code size reduction instead of ctlwizchip() and ctlnetwork().
+ */
+
+/**
+ * @ingroup extra_functions
+ * @brief Reset WIZCHIP by softly.
+ */
+void wizchip_sw_reset(void);
+
+/**
+ * @ingroup extra_functions
+ * @brief Initializes WIZCHIP with socket buffer size
+ * @param txsize Socket tx buffer sizes. If null, initialized the default size 2KB.
+ * @param rxsize Socket rx buffer sizes. If null, initialized the default size 2KB.
+ * @return 0 : succcess \n
+ * -1 : fail. Invalid buffer size
+ */
+int8_t wizchip_init(uint8_t* txsize, uint8_t* rxsize);
+
+/**
+ * @ingroup extra_functions
+ * @brief Clear Interrupt of WIZCHIP.
+ * @param intr : @ref intr_kind value operated OR. It can type-cast to uint16_t.
+ */
+void wizchip_clrinterrupt(intr_kind intr);
+
+/**
+ * @ingroup extra_functions
+ * @brief Get Interrupt of WIZCHIP.
+ * @return @ref intr_kind value operated OR. It can type-cast to uint16_t.
+ */
+intr_kind wizchip_getinterrupt(void);
+
+/**
+ * @ingroup extra_functions
+ * @brief Mask or Unmask Interrupt of WIZCHIP.
+ * @param intr : @ref intr_kind value operated OR. It can type-cast to uint16_t.
+ */
+void wizchip_setinterruptmask(intr_kind intr);
+
+/**
+ * @ingroup extra_functions
+ * @brief Get Interrupt mask of WIZCHIP.
+ * @return : The operated OR vaule of @ref intr_kind. It can type-cast to uint16_t.
+ */
+intr_kind wizchip_getinterruptmask(void);
+
+//todo
+#if _WIZCHIP_ > W5100
+ int8_t wizphy_getphylink(void); ///< get the link status of phy in WIZCHIP. No use in W5100
+ int8_t wizphy_getphypmode(void); ///< get the power mode of PHY in WIZCHIP. No use in W5100
+#endif
+
+#if _WIZCHIP_ == W5100S || _WIZCHIP_ == W5500
+ void wizphy_reset(void); ///< Reset phy. Vailid only in W5500
+/**
+ * @ingroup extra_functions
+ * @brief Set the phy information for WIZCHIP without power mode
+ * @param phyconf : @ref wiz_PhyConf
+ */
+ void wizphy_setphyconf(wiz_PhyConf* phyconf);
+ /**
+ * @ingroup extra_functions
+ * @brief Get phy configuration information.
+ * @param phyconf : @ref wiz_PhyConf
+ */
+ void wizphy_getphyconf(wiz_PhyConf* phyconf);
+ /**
+ * @ingroup extra_functions
+ * @brief Get phy status.
+ * @param phyconf : @ref wiz_PhyConf
+ */
+ void wizphy_getphystat(wiz_PhyConf* phyconf);
+ /**
+ * @ingroup extra_functions
+ * @brief set the power mode of phy inside WIZCHIP. Refer to @ref PHYCFGR in W5500, @ref PHYSTATUS in W5200
+ * @param pmode Settig value of power down mode.
+ */
+ int8_t wizphy_setphypmode(uint8_t pmode);
+#endif
+
+/**
+* @ingroup extra_functions
+ * @brief Set the network information for WIZCHIP
+ * @param pnetinfo : @ref wizNetInfo
+ */
+void wizchip_setnetinfo(wiz_NetInfo* pnetinfo);
+
+/**
+ * @ingroup extra_functions
+ * @brief Get the network information for WIZCHIP
+ * @param pnetinfo : @ref wizNetInfo
+ */
+void wizchip_getnetinfo(wiz_NetInfo* pnetinfo);
+
+/**
+ * @ingroup extra_functions
+ * @brief Set the network mode such WOL, PPPoE, Ping Block, and etc.
+ * @param pnetinfo Value of network mode. Refer to @ref netmode_type.
+ */
+int8_t wizchip_setnetmode(netmode_type netmode);
+
+/**
+ * @ingroup extra_functions
+ * @brief Get the network mode such WOL, PPPoE, Ping Block, and etc.
+ * @return Value of network mode. Refer to @ref netmode_type.
+ */
+netmode_type wizchip_getnetmode(void);
+
+/**
+ * @ingroup extra_functions
+ * @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.
+ * @param nettime @ref _RTR_ value and @ref _RCR_ value. Refer to @ref wiz_NetTimeout.
+ */
+void wizchip_settimeout(wiz_NetTimeout* nettime);
+
+/**
+ * @ingroup extra_functions
+ * @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.
+ * @param nettime @ref _RTR_ value and @ref _RCR_ value. Refer to @ref wiz_NetTimeout.
+ */
+void wizchip_gettimeout(wiz_NetTimeout* nettime);
+#ifdef __cplusplus
+ }
+#endif
+
+#endif // _WIZCHIP_CONF_H_
diff --git a/14_m1284p_WIZNET_FTPC_FATFS_v1.2/Internet/FTPClient_avr/ftpc.c b/14_m1284p_WIZNET_FTPC_FATFS_v1.2/Internet/FTPClient_avr/ftpc.c
new file mode 100644
index 0000000..59a33b6
--- /dev/null
+++ b/14_m1284p_WIZNET_FTPC_FATFS_v1.2/Internet/FTPClient_avr/ftpc.c
@@ -0,0 +1,923 @@
+#include "ftpc.h"
+//FTP Data socket Connected
+
+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_SRV_destip[4] = {192, 168, 0, 100}; // For FTP client examples; destination network info - WORK STATION IP
+//uint8_t FTP_SRV_destip[4] = {192, 168, 1, 81}; // For FTP client examples; destination network info - HOME STATION IP
+uint16_t FTP_SRV_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,};
+
+//dir waiting
+//put waiting
+//Rcvd Command:
+
+struct ftpc ftpc;
+struct Command Command;
+
+/*
+uint8_t ftpc_try(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, "/");
+ //uint8_t sock_res_open;
+ //socket(CTRL_SOCK, Sn_MR_TCP, FTP_destport, 0); //SF_IO_NONBLOCK
+ socket(CTRL_SOCK, Sn_MR_TCP, FTP_destport, SF_IO_NONBLOCK);
+
+ connect(CTRL_SOCK, FTP_destip, FTP_destport);
+
+ //Wait connection to FTP Server
+ if(Sn_SR(CTRL_SOCK) != SOCK_ESTABLISHED) // check the tcp connect success
+ {
+ PRINTF("FTP Client Wait CTRL_SOCK connect..\r\n");
+ close(CTRL_SOCK);
+ wdt_reset();
+ _delay_ms(1000);
+ return 0;
+ }
+ else
+ {
+ close(CTRL_SOCK);
+ wdt_reset();
+ _delay_ms(1000);
+ return 1;
+ }
+}
+*/
+
+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_SRV_destport, 0x0); //- Blocking type - ugly, when sure that FTP server exist
+}
+
+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:[SOCK_ESTABLISHED]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 SDCARD 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 File from SDCARD\r\n");
+#endif
+ PRINTF("8> Delete File from FTP Server\r\n");
+ PRINTF("9> Execute custom command onto FTP Server\r\n");
+ PRINTF("T> Test message\r\n");
+ PRINTF("S> Status board (FreeRam and UpTime)\r\n");
+ PRINTF("R> Reboot board\r\n");
+ PRINTF("----------------------------------------\r\n");
+ while(1){
+ msg_c=ftp_getc();
+ if((msg_c=='t')||(msg_c=='T')){
+ PRINTF("\r\nTest message: Hello!\r\n");
+ gMenuStart = 1;
+ _delay_ms(100);
+ uart0_rx_flash();
+ break;
+ }
+ else if((msg_c=='s')||(msg_c=='S')){
+ PRINTF("\r\nUptime: %lu sec\r\nFreeRam: %u bytes\r\n", millis()/1000, freeRam());
+ gMenuStart = 1;
+ _delay_ms(100);
+ uart0_rx_flash();
+ break;
+ }
+ else if((msg_c=='r')||(msg_c=='R')){
+ PRINTF("\r\nReboot the board");
+ while(1)
+ {
+ _delay_ms(1000);
+ PRINTF(".");
+ }
+ break;
+ }
+ else if(msg_c=='1'){
+ _delay_ms(100);
+ uart0_rx_flash();
+
+ 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'){
+ _delay_ms(100);
+ uart0_rx_flash();
+ 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'){
+ _delay_ms(100);
+ uart0_rx_flash();
+ 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'){
+ _delay_ms(100);
+ uart0_rx_flash();
+#if defined(F_FILESYSTEM)
+ PRINTF ("\r\n\r\nSD-Card root file list:\r\n");
+ PRINTF ("===============================================\r\n");
+ ls_dir(ftpc.workingdir); //Works OK
+ PRINTF ("===============================================\r\n\r\n");
+ //scan_files(ftpc.workingdir, dbuf, (int *)&size); //Broken implementation
+ //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("\r\n1> ASCII\r\n");
+ PRINTF("2> BINARY\r\n");
+ _delay_ms(100);
+ uart0_rx_flash();
+ while(1){
+ msg_c=ftp_getc();
+ _delay_ms(100);
+ uart0_rx_flash();
+ 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("\r\n1> ACTIVE\r\n");
+ PRINTF("2> PASSIVE(Disabled)\r\n");
+ _delay_ms(100);
+ uart0_rx_flash();
+ while(1){
+ msg_c=ftp_getc();
+ _delay_ms(100);
+ uart0_rx_flash();
+ if(msg_c=='1'){
+ ftpc.dsock_mode=ACTIVE_MODE;
+ break;
+ }
+ else if(msg_c=='2'){
+ //PASSIVE mode with bugs, so disabled
+ //ftpc.dsock_mode=PASSIVE_MODE;
+ ftpc.dsock_mode=ACTIVE_MODE;
+ break;
+ }
+ else{
+ PRINTF("\r\nRetry...\r\n");
+ }
+ }
+ gMenuStart = 1;
+ break;
+ }
+#if defined(F_FILESYSTEM)
+ else if(msg_c=='7'){
+ PRINTF(">del filename (SD-CARD)?");
+ _delay_ms(100);
+ uart0_rx_flash();
+ 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 if(msg_c=='8'){
+ _delay_ms(100);
+ uart0_rx_flash();
+ PRINTF(">del filename (FTPD)?");
+ _delay_ms(100);
+ uart0_rx_flash();
+ sprintf(dat,"DELE %s\r\n", User_Keyboard_MSG());
+ send(CTRL_SOCK, (uint8_t *)dat, strlen(dat));
+
+ //Should not used here
+ //gModeActivePassiveflag = 1;
+ break;
+ }
+ else if(msg_c=='9'){
+ _delay_ms(100);
+ uart0_rx_flash();
+ PRINTF(">FTPD execute command(HELP etc..)?");
+ _delay_ms(100);
+ uart0_rx_flash();
+ sprintf(dat,"%s\r\n", User_Keyboard_MSG());
+ send(CTRL_SOCK, (uint8_t *)dat, strlen(dat));
+
+ //Should not used here
+ //gModeActivePassiveflag = 0;
+ break;
+ }
+ else if((msg_c == '\r')||(msg_c == '\n'))
+ {
+ //Suppress CR and LF symbols
+ //gMenuStart = 1;
+ _delay_ms(100);
+ uart0_rx_flash();
+ }
+ else{
+ PRINTF("\r\n??Unknown command: <%c>\r\nRetry...\r\n", msg_c);
+ _delay_ms(100);
+ uart0_rx_flash();
+ //gMenuStart = 1;
+ //break;
+ }
+ }
+ }
+ 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_FTP);
+ if(size > _MAX_SS_FTP) size = _MAX_SS_FTP - 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("\r\nRcvd 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_SRV_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:[SOCK_INIT]Opened\r\n",CTRL_SOCK);
+ if((ret = connect(CTRL_SOCK, FTP_SRV_destip, FTP_SRV_destport)) != SOCK_OK){
+ PRINTF("%d:Connect error\r\n",CTRL_SOCK);
+ return ret;
+ }
+ connect_state_control_ftpc = 0;
+ PRINTF("%d:[SOCK_INIT]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");
+ _delay_ms(1);
+
+ if((remain_datasize = getSn_RX_RSR(DATA_SOCK)) > 0){ //#1 if .. Don't need to check SOCKERR_BUSY because it doesn't not occur.
+
+ uint32_t timer_dir_list = millis();
+ wdt_reset();
+
+ PRINTF("\r\nFTP server root file list:\r\n");
+ PRINTF ("===============================================\r\n");
+ while(1)//#1 while(1)
+ {
+ if((remain_datasize = getSn_RX_RSR(DATA_SOCK)) > 0){ //#2 if .. Don't need to check SOCKERR_BUSY because it doesn't not occur.
+ //!!Debug only
+ //PRINTF("\r\n++SIZE of FILE %lu\r\n", remain_datasize);
+ while(1)//#2 while(1)
+ {
+ memset(dbuf, 0, _MAX_SS_FTP);
+ if(remain_datasize > (_MAX_SS_FTP-1)) recv_byte = _MAX_SS_FTP-1;
+ else recv_byte = remain_datasize;
+ ret = recv(DATA_SOCK, dbuf, recv_byte);
+ remain_datasize -= ret;
+ //!!Debug only
+ //PRINTF("\r\n++ ret: %lu; recv_byte: %lu, remain_datasize: %lu\r\n", ret, recv_byte, remain_datasize);
+ if(ret != recv_byte){
+ if(ret==SOCK_BUSY) return 0;
+ if(ret < 0){
+ PRINTF("%d:recv() error:%ld\r\n",CTRL_SOCK,ret);
+ close(DATA_SOCK);
+ return ret;
+ }
+ }
+ else
+ {
+ dbuf[ret] = '\0';
+ //PRINTF("\r\nRcvd Data:\n\r%s\n\r", dbuf);
+ PRINTF("%s", dbuf);
+ }
+ if(remain_datasize <= 0) break;
+ }//#2 while(1)
+ //GET packets from dir_list store time tag
+ timer_dir_list = millis();
+ wdt_reset();
+ }//#2 if((remain_datasize = getSn_RX_RSR(DATA_SOCK)) > 0){
+ else
+ {
+ if((millis()-timer_dir_list)>FTP_DIR_LIST_PAUSE)
+ {
+ //If on packet ~ 0.5..3sec break receive dir list
+ break;
+ }
+ }//#2 if.. else
+ }//#1 while(1)
+ //Here when all list data received
+ PRINTF ("===============================================\r\n");
+ gDataPutGetStart = 0;
+ Command.Second = s_nocmd;
+ }//#1 if((remain_datasize = getSn_RX_RSR(DATA_SOCK)) > 0){
+
+ break;
+ case s_put:
+ PRINTF("put waiting...\r\n");
+ _delay_ms(1);
+
+ 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");
+ //!!Debug only
+ //PRINTF("\r\n++SIZE of FILE %lu\r\n", remain_filesize);
+ do{
+ wdt_reset();
+ memset(dbuf, 0, _MAX_SS_FTP);
+ if(remain_filesize > _MAX_SS_FTP)
+ send_byte = _MAX_SS_FTP;
+ else
+ send_byte = remain_filesize;
+ //!!Debug only
+ //PRINTF("++SEND BYTE %lu\r\n", send_byte);
+ ftpc.fr = f_read(&(ftpc.fil), (void *)dbuf, send_byte , (UINT *)&blocklen);
+ if(ftpc.fr != FR_OK){
+ break;
+ }
+ PRINTF("#");
+ int32_t _send_size;
+ _send_size = send(DATA_SOCK, dbuf,(uint16_t) blocklen);
+ //remain_filesize -= blocklen;
+ remain_filesize -= (uint32_t)_send_size;
+
+ //!!Debug only
+ //PRINTF("++REMAIN SIZE of FILE %lu\r\n\r\n", remain_filesize);
+ }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_FTP);
+ 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");
+ _delay_ms(1);
+
+ 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){ //while #1..
+ wdt_reset();
+ if((remain_datasize = getSn_RX_RSR(DATA_SOCK)) > 0){//if((remain_datasize = getSn_RX_RSR(DATA_SOCK)) > 0){
+ //!!Debug only
+ //PRINTF("++Arrive new packet: remain_datasize %lu\r\n\r\n", remain_datasize);
+ while(1){//while #2..
+ wdt_reset();
+ memset(dbuf, 0, _MAX_SS_FTP);
+ if(remain_datasize > _MAX_SS_FTP) recv_byte = _MAX_SS_FTP;
+ else recv_byte = remain_datasize;
+ ret = recv(DATA_SOCK, dbuf,(uint16_t) recv_byte);
+ uint16_t _blocklen;
+ 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;
+ PRINTF("#");
+ f_sync(&(ftpc.fil)); //Flush data to SDCARD from cache
+ //!!Debug only
+ //PRINTF("++remain_datasize: %lu\r\n\r\n", remain_datasize);
+ }//while #2..
+ if(ftpc.fr != FR_OK){
+ PRINTF("f_write failed\r\n");
+ break;
+ }
+ }//if((remain_datasize = getSn_RX_RSR(DATA_SOCK)) > 0){
+ else{
+ if(getSn_SR(DATA_SOCK) != SOCK_ESTABLISHED) break;
+ }
+ }// //while #1..
+ 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_FTP);
+ if(remain_datasize > _MAX_SS_FTP)
+ recv_byte = _MAX_SS_FTP;
+ 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 [PASSIVE_MODE], port : %u\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;
+ local_port -= 20000;
+ }else{
+ PRINTF("%d:FTPDataStart [ACTIVE_MODE], port : %u\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;
+ local_port -= 20000;
+ }
+ 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_530: /* 530 Permission denied */
+ PRINTF("\r\n??USER/PASS not correct..\r\n");
+
+ //var.1 Reboot the board on incorrect user-pass (Used on defined AUTOLOGON_ANONYMOUS)
+ PRINTF("\r\nReboot the board");
+ while(1)
+ {
+ _delay_ms(1000);
+ PRINTF(".");
+ }
+
+ //var.2 Try re-login (Used on undefined AUTOLOGON_ANONYMOUS)
+ //PRINTF("Try re-login\r\n");
+
+ //Should not used here on both variants
+ /*break;*/
+ case R_220: /* Service ready for new user. */
+ PRINTF("\r\nInput your User ID > ");
+#ifdef AUTOLOGON_ANONYMOUS
+ _delay_ms(100), sprintf(dat,"USER %s\r\n", "anonymous"); //Use default