/*********************************************************************************************************************
* TC264 Opensourec Library TC264 Դ⣩һڹٷ SDK ӿڵĵԴ
* Copyright (c) 2022 SEEKFREE ɿƼ
*
* ļ TC264 Դһ
*
* TC264 Դ 
* Ըᷢ GPLGNU General Public License GNUͨù֤
*  GPL ĵ3棨 GPL3.0ѡģκκİ汾·/޸
*
* Դķϣܷãδκεı֤
* ûԻʺض;ı֤
* ϸμ GPL
*
* ӦյԴͬʱյһ GPL ĸ
* ûУ<https://www.gnu.org/licenses/>
*
* ע
* Դʹ GPL3.0 Դ֤Э Ϊİ汾
* Ӣİ libraries/doc ļµ GPL3_permission_statement.txt ļ
* ֤ libraries ļ ļµ LICENSE ļ
* ӭλʹò ޸ʱ뱣ɿƼİȨ
*
* ļ          zf_device_wifi_spi
* ˾          ɶɿƼ޹˾
* 汾Ϣ          鿴 libraries/doc ļ version ļ 汾˵
*           ADS v1.9.4
* ƽ̨          TC264D
*           https://seekfree.taobao.com/
*
* ޸ļ¼
*                               ע
* 2023-05-27       pudding           first version
********************************************************************************************************************/
/*********************************************************************************************************************
* ߶壺
*                   ------------------------------------
*                   ģܽ            Ƭܽ
*                   RST                 鿴 zf_device_wifi_spi.h  WIFI_SPI_RST_PIN 궨
*                   INT                 鿴 zf_device_wifi_spi.h  WIFI_SPI_INT_PIN 궨
*                   CS                  鿴 zf_device_wifi_spi.h  WIFI_SPI_CS_PIN 궨
*                   MISO                鿴 zf_device_wifi_spi.h  WIFI_SPI_MISO_PIN 궨
*                   SCK                 鿴 zf_device_wifi_spi.h  WIFI_SPI_SCK_PIN 궨
*                   MOSI                鿴 zf_device_wifi_spi.h  WIFI_SPI_MOSI_PIN 궨
*                   5V                  5V Դ
*                   GND                 Դ
*                   
*                   ------------------------------------
*********************************************************************************************************************/

#ifndef _zf_device_wifi_spi_h
#define _zf_device_wifi_spi_h

#include "zf_common_typedef.h"

#define WIFI_SPI_INDEX              (SPI_3             )        // ʹõSPI
#define WIFI_SPI_SPEED              (30 * 1000 * 1000  )        // Ӳ SPI 
#define WIFI_SPI_SCK_PIN            (SPI3_SCLK_P22_3   )        // SPI_SCK
#define WIFI_SPI_MOSI_PIN           (SPI3_MOSI_P22_0   )        // SPI_MOSI
#define WIFI_SPI_MISO_PIN           (SPI3_MISO_P22_1   )        // SPI_MISO  IPSûMISOţȻҪ壬spiĳʼʱҪʹ
#define WIFI_SPI_CS_PIN             (P22_2             )        // SPI_CS CS
#define WIFI_SPI_INT_PIN            (ERU_CH5_REQ1_P15_8)        // ж
#define WIFI_SPI_RST_PIN            (P23_1             )        // 帴λ
                                
                                
#define WIFI_SPI_BUFFER_SIZE        (1024)                      // SPIյĻС

#define WIFI_SPI_AUTO_CONNECT       (0)                         // ǷʼʱTCPUDP    0-  1-ԶTCP͸ģʽ  2-ԶUDP͸ģʽ  3ԶTCP
    
#if     (WIFI_SPI_AUTO_CONNECT > 2)
#error "WIFI_SPI_AUTO_CONNECT ֵֻΪ [0,1,2]"
#else   
#define WIFI_SPI_TARGET_IP          "192.168.2.28"              // Ŀ IP
#define WIFI_SPI_TARGET_PORT        "8080"                      // ĿĶ˿
#define WIFI_SPI_LOCAL_PORT         "8080"                      // ˿
#endif

#define WIFI_SPI_MAX_MULTI          (17)                        // ַͣ8ַ

typedef enum    
{   
    BUFFER_IDLE,                                                // ģĻǿе
    BUFFER_READ,                                                // ģĻҪȡ
    BUFFER_WRITE,                                               // ģĻǿд
}wifi_spi_buffer_state_enum;            

typedef enum
{       
    TRANSMIT_IDLE,                                              // ǰûд
    TRANSMIT_WRITE_REQUEST,                                     // ģ鷢һ
    TRANSMIT_READ_STATE,                                        // ȡģ״̬
    TRANSMIT_READ,                                              // ڶȡģڲ
    TRANSMIT_WRITE,                                             // ģд
}wifi_spi_transmit_state_enum;

typedef enum    
{   
    WIFI_SPI_STATION,                                           // 豸ģʽ
    WIFI_SPI_SOFTAP,                                            // APģʽ
}wifi_spi_mode_enum;        
        
typedef enum        
{       
    WIFI_SPI_COMMAND,                                           // ʹķʽ
    WIFI_SPI_SERIANET,                                          // ʹ͸ķʽ
}wifi_spi_transfer_mode_enum;       
        
typedef enum        
{       
    WIFI_SPI_TCP_CLIENT,                                        // ģTCP
    WIFI_SPI_TCP_SERVER,                                        // ģΪTCP
    WIFI_SPI_UDP_CLIENT,                                        // ģUDP
}wifi_spi_connect_mode_enum;        
        
typedef enum        
{       
    WIFI_SPI_SERVER_OFF,                                        // ģδӷ
    WIFI_SPI_SERVER_ON,                                         // ģѾӷ
}wifi_spi_connect_state_enum;       
        
typedef enum        
{       
    WIFI_SPI_LINK_0,                                            // ģ鵱ǰ 0
    WIFI_SPI_LINK_1,                                            // ģ鵱ǰ 1
    WIFI_SPI_LINK_2,                                            // ģ鵱ǰ 2
    WIFI_SPI_LINK_3,                                            // ģ鵱ǰ 3
    WIFI_SPI_LINK_4,                                            // ģ鵱ǰ 4
}wifi_spi_link_id_enum;     

typedef struct
{
    uint8 reserve;
    uint8 cmd;
    uint8 addr;
    uint8 dummy;
    uint8 magic;
    uint8 sequence;
    uint16 length;
}wifi_spi_buffer_struct;

typedef struct      
{       
    uint8                           version[12];                // ̼汾         ַʽ
    uint8                           mac[20];                    //  MAC ַ    ַʽ
    uint8                           local_ip[17];               //  IP ַ     ַʽ
    uint8                           local_port[10];             // ˿ں       ַʽ
    uint8                           remote_ip[5][17];           // Զ IP ַ     ַʽ
    wifi_spi_mode_enum              mode;                       // WIFI ģʽ
    wifi_spi_transfer_mode_enum     transfer_mode;              // ǰģʽ
    wifi_spi_connect_mode_enum      connect_mode;               // ģʽ
    wifi_spi_connect_state_enum     connect_state;              // 
}wifi_spi_information_struct;

typedef struct
{
    uint8  *source[WIFI_SPI_MAX_MULTI];
    uint16 length[WIFI_SPI_MAX_MULTI];
}wifi_spi_send_multi_struct;

extern wifi_spi_information_struct wifi_spi_information;


uint8   wifi_spi_disconnected_wifi         (void);                                                                      // Ͽ WIFI 
uint8   wifi_spi_entry_serianet            (void);                                                                      // ͸ģʽ
uint8   wifi_spi_exit_serianet             (void);                                                                      // ر͸ģʽ
    
uint8   wifi_spi_connect_tcp_servers       (char *ip, char *port, wifi_spi_transfer_mode_enum mode);                    //  TCP 
uint8   wifi_spi_connect_udp_client        (char *ip, char *port, char *local_port, wifi_spi_transfer_mode_enum mode);  //  UDP 
uint8   wifi_spi_disconnect_link           (void);                                                                      // Ͽ TCP Server ʹñӿڽϿ

uint32  wifi_spi_send_byte                 (uint8 data);                                                                // WIFI ģ鷢ֽں
uint32  wifi_spi_send_buffer               (const uint8 *buff, uint32 length);                                          // WIFI ģ鷢ͻ
uint32  wifi_spi_send_buffer_multi         (wifi_spi_send_multi_struct *multi_buffer);                                  // WIFI ģ鷢Ͷ໺
uint32  wifi_spi_send_string               (const char *str);                                                           // WIFI ģ鷢ַ

uint32  wifi_spi_read_buffer               (uint8 *buff, uint32 len);                                                   // WIFI ģݽպ
    
uint8   wifi_spi_init                      (char *wifi_ssid, char *pass_word, wifi_spi_mode_enum wifi_mode);            // WIFI ģʼ

#endif
