Advertisement
Guest User

Untitled

a guest
Jan 18th, 2018
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.42 KB | None | 0 0
  1. #include <stm32f4xx_hal.h>
  2.  
  3. #include "lwip/init.h"
  4. #include "lwip/netif.h"
  5. #include "lwip/tcpip.h"
  6. #include "lwip/dhcp.h"
  7. #include "lwip/api.h"
  8. #include "lwip/opt.h"
  9. #include "lwip/timeouts.h"
  10. #include "netif/etharp.h"
  11. #include "lwip/apps/sntp.h"
  12. #include "lwip/apps/sntp_opts.h"
  13.  
  14. #include "ethernetif.h"
  15.  
  16. #include "NetworkDefaults.h"
  17.  
  18. static struct netif gnetif;
  19.  
  20. static void StartDhcp(void);
  21. static void NetworkConfig(void);
  22. static void PrintIP(void);
  23.  
  24. // Bring up all networking items
  25. void NetworkingInit(void)
  26. {
  27.     // Bring up the network stack
  28.     lwip_init();
  29.    
  30.     // Bring up the network link
  31.     NetworkConfig();
  32.    
  33.     // Start DHCP, we'll start other services will start when we have an IP
  34.     StartDhcp();
  35. }
  36.  
  37. void NetworkingUpdate(void)
  38. {
  39.     // Set link state
  40.     //ethernetif_check_link_state(&gnetif);
  41.    
  42.     // Attempt to process ethernet frames
  43.     ethernetif_input(&gnetif);
  44.  
  45.     // Refresh all timers
  46.     sys_check_timeouts();
  47.    
  48.     //PrintIP();
  49.    
  50.     // If we don't have an IP or the link is down, quit now
  51.     if(gnetif.ip_addr.addr == 0)
  52.     {
  53.         return;
  54.     }
  55.  
  56. }
  57.  
  58. void NetworkingShutdown(void)
  59. {
  60.     dhcp_stop(&gnetif);
  61. }
  62.  
  63. static void StartDhcp(void)
  64. {
  65.     // Zero out IP, netmask, and gateway
  66.     ip_addr_set_zero_ip4(&gnetif.ip_addr);
  67.     ip_addr_set_zero_ip4(&gnetif.netmask);
  68.     ip_addr_set_zero_ip4(&gnetif.gw);
  69.    
  70.     // Start LwIP DHCP
  71.     dhcp_start(&gnetif);
  72.    
  73.     printf("DHCP start\r\n");
  74. }
  75.  
  76. // Attempt to get an address from DHCP
  77. static void PrintIP(void)
  78. {
  79.     printf("IP address: %d.%d.%d.%d\r\n", (uint8_t)gnetif.ip_addr.addr, ((uint8_t)gnetif.ip_addr.addr >> 8), ((uint8_t)gnetif.ip_addr.addr >> 16), ((uint8_t)gnetif.ip_addr.addr >> 24));
  80. }
  81.  
  82. void SetRtcFromNtpTimestamp(const uint32_t seconds, const uint32_t uSeconds)
  83. {
  84.     printf("Trying to set to %lu %lu\r\n", seconds, uSeconds);
  85. }
  86.  
  87. void GetRtcTimestamp(uint32_t* seconds, uint32_t* uSeconds)
  88. {
  89. }
  90.  
  91. // Configure the network
  92. static void NetworkConfig(void)
  93. {
  94.     ip_addr_t ipaddr;
  95.     ip_addr_t netmask;
  96.     ip_addr_t gw;
  97.  
  98.     // Start with settings zero'd for DHCP
  99.     ip_addr_set_zero_ip4(&ipaddr);
  100.     ip_addr_set_zero_ip4(&netmask);
  101.     ip_addr_set_zero_ip4(&gw);
  102.    
  103.     /*IP_ADDR4(&ipaddr, 192, 168, 1, 50);
  104.     IP_ADDR4(&netmask, 255, 255, 255, 0);
  105.     IP_ADDR4(&gw, 192, 168, 1, 1);*/
  106.    
  107.     // Add the network interface
  108.     netif_add(&gnetif, &ipaddr, &netmask, &gw, NULL, &ethernetif_init, &ethernet_input);
  109.  
  110.     // Registers the default network interface
  111.     netif_set_default(&gnetif);
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement