123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216 |
- /* Includes ------------------------------------------------------------------*/
- #include "lwip/mem.h"
- #include "lwip/memp.h"
- #include "lwip/tcp.h"
- #include "lwip/udp.h"
- #include "netif/etharp.h"
- #include "lwip/dhcp.h"
- #include "ethernetif.h"
- #include "stm32f4x7_eth.h"
- #include "tcpip.h"
- #include "main.h"
- #include "netconf.h"
- #include "common_config.h"
- #include "conf.h"
- #include "settings_api.h"
- #include "tinystdio.h"
- #ifdef LCD_ENABLE
- #include "lcd.h"
- #endif
- #include "lwip/timeouts.h"
- #include "lwip/init.h"
- /* Private typedef -----------------------------------------------------------*/
- #define MAX_DHCP_TRIES 4
- /* Private define ------------------------------------------------------------*/
- typedef enum {
- DHCP_START = 0,
- DHCP_WAIT_ADDRESS,
- DHCP_ADDRESS_ASSIGNED,
- DHCP_TIMEOUT
- }
- DHCP_State_TypeDef;
- /* Private macro -------------------------------------------------------------*/
- /* Private variables ---------------------------------------------------------*/
- struct netif netif;
- uint32_t TCPTimer = 0;
- uint32_t ARPTimer = 0;
- uint32_t DHCPfineTimer = 0;
- uint32_t DHCPcoarseTimer = 0;
- DHCP_State_TypeDef DHCP_state = DHCP_START;
- /**
- * @brief Общая структура настроек
- */
- extern SETTINGS_t sSettings;
- /* Private functions ---------------------------------------------------------*/
- void LwIP_DHCP_Process_Handle(void);
- /**
- * @brief Initializes the lwIP stack
- * @param None
- * @retval None
- */
- void LwIP_Init(void)
- {
- struct ip4_addr ipaddr;
- struct ip4_addr netmask;
- struct ip4_addr gw;
- char str[20];
- lwip_init();
- // /* Initializes the dynamic memory heap defined by MEM_SIZE.*/
- // mem_init();
- //
- // /* Initializes the memory pools defined by MEMP_NUM_x.*/
- // memp_init();
- if (sSettings.sWebParams.dhcpEnable) {
- ipaddr.addr = 0;
- netmask.addr = 0;
- gw.addr = 0;
- } else {
- sprintf(str, " %s\n\r", sSettings.sWebTempParams.ip);
- PRINT_USART("\n\rStatic IP address \n\r");
- PRINT_USART(str);
- ipaddr.addr = ipaddr_addr(sSettings.sWebParams.ip);
- netmask.addr = ipaddr_addr(sSettings.sWebParams.mask);
- gw.addr = ipaddr_addr(sSettings.sWebParams.gate);
- #ifdef LCD_ENABLE
- sprintf(str, "IP: %s", ipaddr_ntoa(&ipaddr));
- LCD_ClearRow(3);
- LCD_PrintAligned(3, alignCENTER, str);
- #endif
- }
- netif_add(&netif, &ipaddr, &netmask, &gw, NULL, ðernetif_init, ðernet_input);
- netif_set_default(&netif);
- netif_set_link_up(&netif);
- netif_set_up(&netif);
- }
- /**
- * @brief LwIP periodic tasks
- * @param localtime the current LocalTime value
- * @retval None
- */
- void LwIP_Periodic_Handle(__IO uint32_t localtime)
- {
- (void)localtime;
- /* Check if any packet received */
- if (ETH_CheckFrameReceived()) {
- /* Read a received packet from the Ethernet buffers and send it to the lwIP for handling */
- ethernetif_input(&netif);
- }
- if (sSettings.sWebParams.dhcpEnable) {
- /* Fine DHCP periodic process every 500ms */
- if ((DHCP_state != DHCP_ADDRESS_ASSIGNED) && (DHCP_state != DHCP_TIMEOUT)) {
- /* process DHCP state machine */
- LwIP_DHCP_Process_Handle();
- }
- }
- sys_check_timeouts();
- }
- /**
- * @brief LwIP_DHCP_Process_Handle
- * @param None
- * @retval None
- */
- void LwIP_DHCP_Process_Handle()
- {
- struct ip4_addr ipaddr;
- struct ip4_addr netmask;
- struct ip4_addr gw;
- struct dhcp *dhcp = netif_dhcp_data(&netif);
- switch (DHCP_state) {
- case DHCP_START: {
- dhcp_start(&netif);
- DHCP_state = DHCP_WAIT_ADDRESS;
- PRINT_USART("\n\rLooking for DHCP server please wait...\n\r");
- #ifdef LCD_ENABLE
- LCD_PrintAligned(3, alignCENTER, "Получение IP адреса..");
- #endif
- }
- break;
- case DHCP_WAIT_ADDRESS: {
- ipaddr = netif.ip_addr;
- netmask = netif.netmask;
- gw = netif.gw;
- if (ipaddr.addr != 0) {
- DHCP_state = DHCP_ADDRESS_ASSIGNED;
- PRINT_USART("Parameters assigned by a DHCP server:\n\r IP: ");
- PRINT_USART(ipaddr_ntoa(&ipaddr));
- PRINT_USART("\n\r");
- PRINT_USART("Netmask: ");
- PRINT_USART(ipaddr_ntoa(&netmask));
- PRINT_USART("\n\r");
- PRINT_USART("Gateway: ");
- PRINT_USART(ipaddr_ntoa(&gw));
- PRINT_USART("\n\r");
- #ifdef LCD_ENABLE
- char str[30];
- sprintf(str, "IP: %s", ipaddr_ntoa(&ipaddr));
- LCD_ClearRow(3);
- LCD_PrintAligned(3, alignCENTER, str);
- // sprintf(str, "Mask: %s", ipaddr_ntoa(&netmask));
- // LCD_ClearRow(4);
- // LCD_PrintAligned(4, alignCENTER, str);
- // sprintf(str, "GW: %s", ipaddr_ntoa(&gw));
- // LCD_ClearRow(5);
- // LCD_PrintAligned(5, alignCENTER, str);
- #endif
- } else {
- /* DHCP timeout */
- if (dhcp->tries > MAX_DHCP_TRIES) {
- DHCP_state = DHCP_TIMEOUT;
- /* Stop DHCP */
- dhcp_stop(&netif);
- ipaddr.addr = ipaddr_addr(sSettings.sWebTempParams.ip);
- netmask.addr = ipaddr_addr(sSettings.sWebTempParams.mask);
- gw.addr = ipaddr_addr(sSettings.sWebTempParams.gate);
- netif_set_addr(&netif, &ipaddr, &netmask, &gw);
- PRINT_USART("DHCP timeout\n\r");
- PRINT_USART("\n\rStatic IP address\n\r");
- PRINT_USART(ipaddr_ntoa(&ipaddr));
- PRINT_USART("\n\r");
- #ifdef LCD_ENABLE
- char str[30];
- sprintf(str, "IP: %s", ipaddr_ntoa(&ipaddr));
- LCD_ClearRow(3);
- LCD_PrintAligned(3, alignCENTER, str);
- LCD_ClearRow(5);
- LCD_PrintAligned(5, alignCENTER, "Таймаут DHCP");
- #endif
- }
- }
- }
- break;
- default: break;
- }
- }
- /*********** Portions COPYRIGHT 2012 Embest Tech. Co., Ltd.*****END OF FILE****/
|