Simulated product dispenser

Dependencies:   HTS221

Fork of mbed-cloud-workshop-connect-HTS221 by Jim Carver

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers NetworkInit.c Source File

NetworkInit.c

00001 /*******************************************************************************
00002  * Copyright 2016, 2017 ARM Ltd.
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  *******************************************************************************/
00016 #include "lwip/netif.h"
00017 #include "lwip/sys.h"
00018 #include "lwip/arch.h"
00019 #include "lwip/api.h"
00020 #include "lwip/tcpip.h"
00021 #include "netif/etharp.h"
00022 #include "lwip/dhcp.h"
00023 #include "ethernetif.h"
00024 #include "lwip/inet.h"
00025 
00026 
00027 struct netif fsl_netif0;
00028 ip_addr_t fsl_netif0_ipaddr, fsl_netif0_netmask, fsl_netif0_gw;
00029 
00030 #ifndef NETWORK_BRINGUP
00031 #define NETWORK_BRINGUP 1
00032 #endif
00033 
00034 bool dhcpDone = false;
00035 
00036 #define configIP_ADDR0 0
00037 #define configIP_ADDR1 0
00038 #define configIP_ADDR2 0
00039 #define configIP_ADDR3 0
00040 
00041 /* Netmask configuration. */
00042 #define configNET_MASK0 0
00043 #define configNET_MASK1 0
00044 #define configNET_MASK2 0
00045 #define configNET_MASK3 0
00046 
00047 /* Default gateway address configuration */
00048 #define configGW_ADDR0 0
00049 #define configGW_ADDR1 0
00050 #define configGW_ADDR2 0
00051 #define configGW_ADDR3 0
00052 
00053 #ifndef HTTPD_STACKSIZE
00054 #define HTTPD_STACKSIZE 3000
00055 #endif
00056 
00057 #ifndef HTTPD_DEBUG
00058 #define HTTPD_DEBUG LWIP_DBG_ON
00059 #endif
00060 
00061 
00062 void networkInit(void *arg)
00063 {
00064     PRINTF("%s : Starting HTTP thread! \r\n", __FUNCTION__);
00065     if (NETWORK_BRINGUP)
00066     {
00067         err_t err = 0;
00068         LWIP_UNUSED_ARG(arg);
00069 
00070         ///// MAC
00071         fsl_netif0.hwaddr_len = 6;
00072 
00073         // Fetch word 0
00074         uint32_t word0 = *(uint32_t *)0x40048060;
00075         // Fetch word 1
00076         // we only want bottom 16 bits of word1 (MAC bits 32-47)
00077         // and bit 9 forced to 1, bit 8 forced to 0
00078         // Locally administered MAC, reduced conflicts
00079         // http://en.wikipedia.org/wiki/MAC_address
00080         uint32_t word1 = *(uint32_t *)0x4004805C;
00081         word1 |= 0x00000200;
00082         word1 &= 0x0000FEFF;
00083 
00084         fsl_netif0.hwaddr[0] = (word1 & 0x000000ff);
00085         fsl_netif0.hwaddr[1] = (word1 & 0x0000ff00) >> 8;
00086         fsl_netif0.hwaddr[2] = (word0 & 0xff000000) >> 24;
00087         fsl_netif0.hwaddr[3] = (word0 & 0x00ff0000) >> 16;
00088         fsl_netif0.hwaddr[4] = (word0 & 0x0000ff00) >> 8;
00089         fsl_netif0.hwaddr[5] = (word0 & 0x000000ff);
00090         ////
00091 
00092         tcpip_init(NULL, NULL);
00093         LWIP_DEBUGF(HTTPD_DEBUG, ("TCP/IP initialized.\r\n"));
00094         IP4_ADDR(&fsl_netif0_ipaddr, configIP_ADDR0, configIP_ADDR1, configIP_ADDR2, configIP_ADDR3);
00095         IP4_ADDR(&fsl_netif0_netmask, configNET_MASK0, configNET_MASK1, configNET_MASK2, configNET_MASK3);
00096         IP4_ADDR(&fsl_netif0_gw, configGW_ADDR0, configGW_ADDR1, configGW_ADDR2, configGW_ADDR3);
00097 
00098         netif_add(&fsl_netif0, &fsl_netif0_ipaddr, &fsl_netif0_netmask, &fsl_netif0_gw, NULL, ethernetif_init, tcpip_input);
00099         netif_set_default(&fsl_netif0);
00100 
00101         PRINTF("%s : Starting DCHP request\r\n", __FUNCTION__);
00102         /* obtain the IP address, default gateway and subnet mask by using DHCP*/
00103         err = dhcp_start(&fsl_netif0);
00104         PRINTF("%s : Started DCHP request (%s)\r\n", __FUNCTION__, lwip_strerr(err));
00105         for(int i=0; i < 40 && fsl_netif0.dhcp->state != DHCP_BOUND; i++)
00106         {
00107             PRINTF("%s : Current DHCP State : %d\r\n", __FUNCTION__, fsl_netif0.dhcp->state);
00108             vTaskDelay(1000/portTICK_PERIOD_MS);
00109         }
00110 
00111         /**/
00112         PRINTF("%s : DHCP state, activating interface (%d)\r\n", __FUNCTION__,fsl_netif0.dhcp->state);
00113         if (fsl_netif0.dhcp->state != DHCP_BOUND)
00114         {
00115             PRINTF("%s : DHCP state, TIMEOUT (%d)\r\n", __FUNCTION__,fsl_netif0.dhcp->state);
00116         }
00117 
00118         LWIP_DEBUGF(HTTPD_DEBUG, ("http_server_netconn_thread: init interface START!"));
00119         netif_set_up(&fsl_netif0);
00120         LWIP_DEBUGF(HTTPD_DEBUG, ("http_server_netconn_thread: init interface END!"));
00121 
00122         PRINTF("%s : Interface is up : %d\r\n", __FUNCTION__, fsl_netif0.dhcp->state);
00123         PRINTF("%s : IP %s\r\n", __FUNCTION__, ipaddr_ntoa(&fsl_netif0.ip_addr));
00124         PRINTF("%s : NM %s\r\n", __FUNCTION__, ipaddr_ntoa(&fsl_netif0.netmask));
00125         PRINTF("%s : GW %s\r\n", __FUNCTION__, ipaddr_ntoa(&fsl_netif0.gw));
00126     }
00127     PRINTF("before run tests: \r\n");
00128     dhcpDone = true;
00129 
00130     vTaskDelete( NULL );
00131 
00132 }
00133 
00134 // Currently we support only one interface
00135 void* palTestGetNetWorkInterfaceContext()
00136 {
00137     return (void *)&fsl_netif0;
00138 }
00139 
00140 
00141 
00142 
00143 
00144