找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索
查看: 9877|回復(fù): 10
打印 上一主題 下一主題
收起左側(cè)

STM32驅(qū)動(dòng)w5500_dhcp+http TCP/IP網(wǎng)絡(luò)集成模塊的源碼

  [復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:296725 發(fā)表于 2018-3-25 14:31 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
W5500 TCP/IP網(wǎng)絡(luò)集成模塊 STM32驅(qū)動(dòng)
stm32_w5500_dhcp+http

單片機(jī)源程序如下:

  1. /* Includes ------------------------------------------------------------------*/
  2. #include "main.h"
  3. #include "usart.h"
  4. #include "delay.h"
  5. #include "spi.h"
  6. #include "socket.h"        // Just include one header for WIZCHIP
  7. #include "Internet/DHCP/dhcp.h"
  8. #include "httpServer.h"
  9. #include "httpParser.h"
  10. #include "httpUtil.h"
  11. /* Private typedef -----------------------------------------------------------*/
  12. /* Private define ------------------------------------------------------------*/
  13. #define SOCK_DHCP                        0
  14. #define MY_MAX_DHCP_RETRY        3
  15. #define DATA_BUF_SIZE   2048
  16. /* Private macro -------------------------------------------------------------*/
  17. uint8_t gDATABUF[DATA_BUF_SIZE];
  18. // Default Network Configuration

  19. uint8_t http_tx_buf[2048];
  20. uint8_t http_rx_buf[2048];
  21. const uint8_t flash_buffer[2048];
  22. uint8_t memsize[2][8] = {{2,2,2,2,2,2,2,2},{2,2,2,2,2,2,2,2}};
  23. uint8_t socklist[] = {0,1,2,3,4,5,6 };
  24. int g_time6=0;
  25. extern char index_html[4096];

  26. wiz_NetInfo gWIZNETINFO = { .mac = {0x00, 0x08, 0xdc,0x00, 0xab, 0xcd},
  27.                             .ip = {192, 168, 1, 123},
  28.                             .sn = {255,255,255,0},
  29.                             .gw = {192, 168, 1, 1},
  30.                             .dns = {0,0,0,0},
  31.                             .dhcp = NETINFO_STATIC };
  32. /* Private variables ---------------------------------------------------------*/
  33. /* Private function prototypes -----------------------------------------------*/
  34. /* Private functions ---------------------------------------------------------*/
  35. void platform_init(void);                                                                // initialize the dependent host peripheral
  36. void my_ip_assign(void);
  37. void network_init(void);
  38.                            
  39.                            
  40.                                                    
  41.                            
  42.                            
  43. /************************************
  44. * @ brief Call back for ip Conflict
  45. ************************************/
  46. void my_ip_conflict(void)
  47. {
  48.         printf("CONFLICT IP from DHCP\r\n");
  49.         //halt or reset or any...
  50.         while(1); // this example is halt.
  51. }
  52. /**
  53.   * @brief  串口打印輸出
  54.   * @param  None
  55.   * @retval None
  56.   */
  57. int main(void)
  58. {
  59.         uint8_t tmp;
  60.         uint8_t memsize[2][8] = { {2,2,2,2,2,2,2,2},{2,2,2,2,2,2,2,2}};
  61.         uint8_t my_dhcp_retry = 0;
  62.         

  63.         //Host dependent peripheral initialized
  64.         platform_init();
  65.         // First of all, Should register SPI callback functions implemented by user for accessing WIZCHIP
  66.         /* Critical section callback */
  67.         reg_wizchip_cris_cbfunc(SPI_CrisEnter, SPI_CrisExit);        //注冊(cè)臨界區(qū)函數(shù)
  68.         /* Chip selection call back */
  69. #if   _WIZCHIP_IO_MODE_ == _WIZCHIP_IO_MODE_SPI_VDM_
  70.         reg_wizchip_cs_cbfunc(SPI_CS_Select, SPI_CS_Deselect);//注冊(cè)SPI片選信號(hào)函數(shù)
  71. #elif _WIZCHIP_IO_MODE_ == _WIZCHIP_IO_MODE_SPI_FDM_
  72.         reg_wizchip_cs_cbfunc(SPI_CS_Select, SPI_CS_Deselect);  // CS must be tried with LOW.
  73. #else
  74.    #if (_WIZCHIP_IO_MODE_ & _WIZCHIP_IO_MODE_SIP_) != _WIZCHIP_IO_MODE_SIP_
  75.       #error "Unknown _WIZCHIP_IO_MODE_"
  76.    #else
  77.       reg_wizchip_cs_cbfunc(wizchip_select, wizchip_deselect);
  78.    #endif
  79. #endif
  80.         /* SPI Read & Write callback function */
  81.         reg_wizchip_spi_cbfunc(SPI_ReadByte, SPI_WriteByte);        //注冊(cè)讀寫函數(shù)

  82.         /* WIZCHIP SOCKET Buffer initialize */
  83.         if(ctlwizchip(CW_INIT_WIZCHIP,(void*)memsize) == -1){
  84.                  printf("WIZCHIP Initialized fail.\r\n");
  85.                  while(1);
  86.         }

  87.         /* PHY link status check */
  88.         do{
  89.                  if(ctlwizchip(CW_GET_PHYLINK, (void*)&tmp) == -1){
  90.                                 printf("Unknown PHY Link stauts.\r\n");
  91.                  }
  92.         }while(tmp == PHY_LINK_OFF);

  93.     /************************************************/
  94.     /* WIZnet W5500 Code Examples :                                 */
  95.     /* Implemented using ioLibrary_BSD Socket APIs        */
  96.     /************************************************/
  97.     /* >> DHCP Client                                                                 */
  98.     /************************************************/

  99.     // must be set the default mac before DHCP started.
  100.         setSHAR(gWIZNETINFO.mac);

  101.         DHCP_init(SOCK_DHCP, gDATABUF);
  102.         // if you want defiffent action instead defalut ip assign,update, conflict,
  103.         // if cbfunc == 0, act as default.
  104.         reg_dhcp_cbfunc(my_ip_assign, my_ip_assign, my_ip_conflict);
  105.     httpServer_init(http_tx_buf,http_rx_buf,7,socklist);
  106.     sprintf(index_html,INDEX_HTML_C,"");
  107.         /* Main Loop */
  108.         while(1)
  109.         {
  110.                 switch(DHCP_run())
  111.                 {
  112.                         case DHCP_IP_ASSIGN:
  113.                         case DHCP_IP_CHANGED:
  114.                                 /* If this block empty, act with default_ip_assign & default_ip_update */
  115.                                 //
  116.                                 // This example calls my_ip_assign in the two case.
  117.                                 //
  118.                                 // Add to ...
  119.                                 //
  120.                                 break;
  121.                         case DHCP_IP_LEASED:
  122.                                 //
  123.                                 // TO DO YOUR NETWORK APPs.
  124.                                 //
  125.                                 break;
  126.                         case DHCP_FAILED:
  127.                                 /* ===== Example pseudo code =====  */
  128.                                 // The below code can be replaced your code or omitted.
  129.                                 // if omitted, retry to process DHCP
  130.                                 my_dhcp_retry++;
  131.                                 if(my_dhcp_retry > MY_MAX_DHCP_RETRY)
  132.                                 {
  133.                                         printf(">> DHCP %d Failed\r\n", my_dhcp_retry);
  134.                                         my_dhcp_retry = 0;
  135.                                         DHCP_stop();      // if restart, recall DHCP_init()
  136.                                         network_init();   // apply the default static network and print out netinfo to serial
  137.                                 }
  138.                                 break;
  139.                         default:
  140.                                 break;
  141.                 }

  142.         httpServer_run(0);
  143.         httpServer_run(1);
  144.         httpServer_run(2);
  145.         httpServer_run(3);
  146.         httpServer_run(4);
  147.         httpServer_run(5);
  148.         httpServer_run(6);

  149.         }
  150. } // end of main()




  151. /**
  152.   * @brief  Loopback Test Example Code using ioLibrary_BSD       
  153.   * @retval None
  154.   */
  155. void platform_init(void)
  156. {
  157.         SystemInit();//系統(tǒng)時(shí)鐘初始化
  158.         USART_Configuration();//串口1初始化
  159.         //Config SPI
  160.         SPI_Configuration();
  161.         //延時(shí)初始化
  162.         delay_init();
  163. }

  164. /*******************************************************
  165. * @ brief Call back for ip assing & ip update from DHCP
  166. *******************************************************/
  167. void my_ip_assign(void)
  168. {
  169.    getIPfromDHCP(gWIZNETINFO.ip);
  170.    getGWfromDHCP(gWIZNETINFO.gw);
  171.    getSNfromDHCP(gWIZNETINFO.sn);
  172.    getDNSfromDHCP(gWIZNETINFO.dns);
  173.    gWIZNETINFO.dhcp = NETINFO_DHCP;
  174.    /* Network initialization */
  175.    network_init();      // apply from dhcp
  176.    printf("DHCP LEASED TIME : %d Sec.\r\n", getDHCPLeasetime());
  177. }

  178. /******************************************************************************
  179. * @brief  Network Init
  180. * Intialize the network information to be used in WIZCHIP
  181. *****************************************************************************/
  182. void network_init(void)
  183. {
  184.         uint8_t tmpstr[6] = {0};
  185.         wiz_NetInfo netinfo;

  186.         // Set Network information from netinfo structure
  187.         ctlnetwork(CN_SET_NETINFO, (void*)&gWIZNETINFO);

  188.         // Get Network information
  189.         ctlnetwork(CN_GET_NETINFO, (void*)&netinfo);

  190.         // Display Network Information
  191.         ctlwizchip(CW_GET_ID,(void*)tmpstr);

  192.         if(netinfo.dhcp == NETINFO_DHCP) printf("\r\n=== %s NET CONF : DHCP ===\r\n",(char*)tmpstr);
  193.         else printf("\r\n=== %s NET CONF : Static ===\r\n",(char*)tmpstr);

  194.         printf("MAC: %02X:%02X:%02X:%02X:%02X:%02X\r\n",netinfo.mac[0],netinfo.mac[1],netinfo.mac[2],
  195.                         netinfo.mac[3],netinfo.mac[4],netinfo.mac[5]);
  196.         printf("SIP: %d.%d.%d.%d\r\n", netinfo.ip[0],netinfo.ip[1],netinfo.ip[2],netinfo.ip[3]);
  197.         printf("GAR: %d.%d.%d.%d\r\n", netinfo.gw[0],netinfo.gw[1],netinfo.gw[2],netinfo.gw[3]);
  198.         printf("SUB: %d.%d.%d.%d\r\n", netinfo.sn[0],netinfo.sn[1],netinfo.sn[2],netinfo.sn[3]);
  199.         printf("DNS: %d.%d.%d.%d\r\n", netinfo.dns[0],netinfo.dns[1],netinfo.dns[2],netinfo.dns[3]);
  200.         printf("===========================\r\n");

  201. }

  202. /*********************************END OF FILE**********************************/
復(fù)制代碼

所有資料51hei提供下載:
stm32_w5500_dhcp+http.rar (744.49 KB, 下載次數(shù): 404)


評(píng)分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎(jiǎng)勵(lì)!

查看全部評(píng)分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏8 分享淘帖 頂1 踩
回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:298882 發(fā)表于 2018-3-29 14:54 | 只看該作者
正好是我想找的,只有這里有,下來看看學(xué)習(xí)一下
回復(fù)

使用道具 舉報(bào)

板凳
ID:552154 發(fā)表于 2019-7-16 09:43 | 只看該作者
還是下載不了
回復(fù)

使用道具 舉報(bào)

地板
ID:580104 發(fā)表于 2019-7-16 10:20 | 只看該作者
沒幣,先留個(gè)標(biāo)記,等有黑幣了再下吧!
回復(fù)

使用道具 舉報(bào)

5#
ID:322573 發(fā)表于 2019-9-12 21:30 | 只看該作者
學(xué)習(xí)一下,不錯(cuò)啊
回復(fù)

使用道具 舉報(bào)

6#
ID:625716 發(fā)表于 2019-10-17 16:38 | 只看該作者
下載不了啥情況 樓主能發(fā)我下么  我最近正想搞這一塊
回復(fù)

使用道具 舉報(bào)

7#
ID:39439 發(fā)表于 2019-10-18 08:28 | 只看該作者
好東西
回復(fù)

使用道具 舉報(bào)

8#
ID:402986 發(fā)表于 2020-7-21 17:53 | 只看該作者
一直不知道怎么通過HTTP去接收數(shù)據(jù),感謝分享~
回復(fù)

使用道具 舉報(bào)

9#
ID:984493 發(fā)表于 2021-11-25 10:39 | 只看該作者
非常感謝樓主分享,也算是值得的。
回復(fù)

使用道具 舉報(bào)

10#
ID:974713 發(fā)表于 2022-2-22 15:38 | 只看該作者
謝謝樓主,不過不是我想要的,樓主使用的代碼基本上來源于github上面,大家在github上搜索下w5500就用,我想要的是監(jiān)聽w5500拔插,發(fā)現(xiàn)重新連接上之后,重新運(yùn)行dhcp獲取新ip地址。
回復(fù)

使用道具 舉報(bào)

11#
ID:974713 發(fā)表于 2022-2-22 15:40 | 只看該作者
剛剛找到解決辦法了,CW_GET_PHYLINK就是獲取鏈路狀態(tài)。
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表