W5500 TCP/IP网络集成模块 STM32驱动
stm32_w5500_dhcp+http
单片机源程序如下:
- /* Includes ------------------------------------------------------------------*/
- #include "main.h"
- #include "usart.h"
- #include "delay.h"
- #include "spi.h"
- #include "socket.h" // Just include one header for WIZCHIP
- #include "Internet/DHCP/dhcp.h"
- #include "httpServer.h"
- #include "httpParser.h"
- #include "httpUtil.h"
- /* Private typedef -----------------------------------------------------------*/
- /* Private define ------------------------------------------------------------*/
- #define SOCK_DHCP 0
- #define MY_MAX_DHCP_RETRY 3
- #define DATA_BUF_SIZE 2048
- /* Private macro -------------------------------------------------------------*/
- uint8_t gDATABUF[DATA_BUF_SIZE];
- // Default Network Configuration
- uint8_t http_tx_buf[2048];
- uint8_t http_rx_buf[2048];
- const uint8_t flash_buffer[2048];
- uint8_t memsize[2][8] = {{2,2,2,2,2,2,2,2},{2,2,2,2,2,2,2,2}};
- uint8_t socklist[] = {0,1,2,3,4,5,6 };
- int g_time6=0;
- extern char index_html[4096];
- wiz_NetInfo gWIZNETINFO = { .mac = {0x00, 0x08, 0xdc,0x00, 0xab, 0xcd},
- .ip = {192, 168, 1, 123},
- .sn = {255,255,255,0},
- .gw = {192, 168, 1, 1},
- .dns = {0,0,0,0},
- .dhcp = NETINFO_STATIC };
- /* Private variables ---------------------------------------------------------*/
- /* Private function prototypes -----------------------------------------------*/
- /* Private functions ---------------------------------------------------------*/
- void platform_init(void); // initialize the dependent host peripheral
- void my_ip_assign(void);
- void network_init(void);
-
-
-
-
-
- /************************************
- * @ brief Call back for ip Conflict
- ************************************/
- void my_ip_conflict(void)
- {
- printf("CONFLICT IP from DHCP\r\n");
- //halt or reset or any...
- while(1); // this example is halt.
- }
- /**
- * @brief 串口打印输出
- * @param None
- * @retval None
- */
- int main(void)
- {
- uint8_t tmp;
- uint8_t memsize[2][8] = { {2,2,2,2,2,2,2,2},{2,2,2,2,2,2,2,2}};
- uint8_t my_dhcp_retry = 0;
-
- //Host dependent peripheral initialized
- platform_init();
- // First of all, Should register SPI callback functions implemented by user for accessing WIZCHIP
- /* Critical section callback */
- reg_wizchip_cris_cbfunc(SPI_CrisEnter, SPI_CrisExit); //注册临界区函数
- /* Chip selection call back */
- #if _WIZCHIP_IO_MODE_ == _WIZCHIP_IO_MODE_SPI_VDM_
- reg_wizchip_cs_cbfunc(SPI_CS_Select, SPI_CS_Deselect);//注册SPI片选信号函数
- #elif _WIZCHIP_IO_MODE_ == _WIZCHIP_IO_MODE_SPI_FDM_
- reg_wizchip_cs_cbfunc(SPI_CS_Select, SPI_CS_Deselect); // CS must be tried with LOW.
- #else
- #if (_WIZCHIP_IO_MODE_ & _WIZCHIP_IO_MODE_SIP_) != _WIZCHIP_IO_MODE_SIP_
- #error "Unknown _WIZCHIP_IO_MODE_"
- #else
- reg_wizchip_cs_cbfunc(wizchip_select, wizchip_deselect);
- #endif
- #endif
- /* SPI Read & Write callback function */
- reg_wizchip_spi_cbfunc(SPI_ReadByte, SPI_WriteByte); //注册读写函数
- /* WIZCHIP SOCKET Buffer initialize */
- if(ctlwizchip(CW_INIT_WIZCHIP,(void*)memsize) == -1){
- printf("WIZCHIP Initialized fail.\r\n");
- while(1);
- }
- /* PHY link status check */
- do{
- if(ctlwizchip(CW_GET_PHYLINK, (void*)&tmp) == -1){
- printf("Unknown PHY Link stauts.\r\n");
- }
- }while(tmp == PHY_LINK_OFF);
- /************************************************/
- /* WIZnet W5500 Code Examples : */
- /* Implemented using ioLibrary_BSD Socket APIs */
- /************************************************/
- /* >> DHCP Client */
- /************************************************/
- // must be set the default mac before DHCP started.
- setSHAR(gWIZNETINFO.mac);
- DHCP_init(SOCK_DHCP, gDATABUF);
- // if you want defiffent action instead defalut ip assign,update, conflict,
- // if cbfunc == 0, act as default.
- reg_dhcp_cbfunc(my_ip_assign, my_ip_assign, my_ip_conflict);
- httpServer_init(http_tx_buf,http_rx_buf,7,socklist);
- sprintf(index_html,INDEX_HTML_C,"");
- /* Main Loop */
- while(1)
- {
- switch(DHCP_run())
- {
- case DHCP_IP_ASSIGN:
- case DHCP_IP_CHANGED:
- /* If this block empty, act with default_ip_assign & default_ip_update */
- //
- // This example calls my_ip_assign in the two case.
- //
- // Add to ...
- //
- break;
- case DHCP_IP_LEASED:
- //
- // TO DO YOUR NETWORK APPs.
- //
- break;
- case DHCP_FAILED:
- /* ===== Example pseudo code ===== */
- // The below code can be replaced your code or omitted.
- // if omitted, retry to process DHCP
- my_dhcp_retry++;
- if(my_dhcp_retry > MY_MAX_DHCP_RETRY)
- {
- printf(">> DHCP %d Failed\r\n", my_dhcp_retry);
- my_dhcp_retry = 0;
- DHCP_stop(); // if restart, recall DHCP_init()
- network_init(); // apply the default static network and print out netinfo to serial
- }
- break;
- default:
- break;
- }
- httpServer_run(0);
- httpServer_run(1);
- httpServer_run(2);
- httpServer_run(3);
- httpServer_run(4);
- httpServer_run(5);
- httpServer_run(6);
- }
- } // end of main()
- /**
- * @brief Loopback Test Example Code using ioLibrary_BSD
- * @retval None
- */
- void platform_init(void)
- {
- SystemInit();//系统时钟初始化
- USART_Configuration();//串口1初始化
- //Config SPI
- SPI_Configuration();
- //延时初始化
- delay_init();
- }
- /*******************************************************
- * @ brief Call back for ip assing & ip update from DHCP
- *******************************************************/
- void my_ip_assign(void)
- {
- getIPfromDHCP(gWIZNETINFO.ip);
- getGWfromDHCP(gWIZNETINFO.gw);
- getSNfromDHCP(gWIZNETINFO.sn);
- getDNSfromDHCP(gWIZNETINFO.dns);
- gWIZNETINFO.dhcp = NETINFO_DHCP;
- /* Network initialization */
- network_init(); // apply from dhcp
- printf("DHCP LEASED TIME : %d Sec.\r\n", getDHCPLeasetime());
- }
- /******************************************************************************
- * @brief Network Init
- * Intialize the network information to be used in WIZCHIP
- *****************************************************************************/
- void network_init(void)
- {
- uint8_t tmpstr[6] = {0};
- wiz_NetInfo netinfo;
- // Set Network information from netinfo structure
- ctlnetwork(CN_SET_NETINFO, (void*)&gWIZNETINFO);
- // Get Network information
- ctlnetwork(CN_GET_NETINFO, (void*)&netinfo);
- // Display Network Information
- ctlwizchip(CW_GET_ID,(void*)tmpstr);
- if(netinfo.dhcp == NETINFO_DHCP) printf("\r\n=== %s NET CONF : DHCP ===\r\n",(char*)tmpstr);
- else printf("\r\n=== %s NET CONF : Static ===\r\n",(char*)tmpstr);
- printf("MAC: %02X:%02X:%02X:%02X:%02X:%02X\r\n",netinfo.mac[0],netinfo.mac[1],netinfo.mac[2],
- netinfo.mac[3],netinfo.mac[4],netinfo.mac[5]);
- printf("SIP: %d.%d.%d.%d\r\n", netinfo.ip[0],netinfo.ip[1],netinfo.ip[2],netinfo.ip[3]);
- printf("GAR: %d.%d.%d.%d\r\n", netinfo.gw[0],netinfo.gw[1],netinfo.gw[2],netinfo.gw[3]);
- printf("SUB: %d.%d.%d.%d\r\n", netinfo.sn[0],netinfo.sn[1],netinfo.sn[2],netinfo.sn[3]);
- printf("DNS: %d.%d.%d.%d\r\n", netinfo.dns[0],netinfo.dns[1],netinfo.dns[2],netinfo.dns[3]);
- printf("===========================\r\n");
- }
- /*********************************END OF FILE**********************************/
复制代码
所有资料51hei提供下载:
stm32_w5500_dhcp+http.rar
(744.49 KB, 下载次数: 404)
|