BOOL ENC28J60_LWIP_Driver::Close( ENC28J60_LWIP_DRIVER_CONFIG* config, int index )
{
    NATIVE_PROFILE_HAL_DRIVERS_ETHERNET();

    if(config == NULL) return FALSE;

    LwipUpTimeCompletion.Abort();

    netif_set_link_down( &g_ENC28J60_NetIF );
    netif_set_down( &g_ENC28J60_NetIF );
    netif_remove( &g_ENC28J60_NetIF );

    /* Disable the INTERRUPT pin */                            
    CPU_GPIO_EnableInputPin2(config->INT_Pin, 
                              FALSE,                         /* Glitch filter enable */
                              NULL,                          /* ISR                  */
                              0,                             /* minor number         */
                              GPIO_INT_NONE,                 /* Interrupt edge       */
                              RESISTOR_PULLUP);              /* Resistor State       */

    InterruptTaskContinuation.Abort();

    LwipNetworkStatus = FALSE;
    
    enc28j60_lwip_close( &g_ENC28J60_NetIF );

    memset(&g_ENC28J60_NetIF, 0, sizeof(g_ENC28J60_NetIF));

    return TRUE;

}
Beispiel #2
0
/*-----------------------------------------------------------------------------------*/
static void
low_level_init(struct netif *netif)
{
  char adapter_mac_addr[ETHARP_HWADDR_LEN];
  u8_t my_mac_addr[ETHARP_HWADDR_LEN] = LWIP_MAC_ADDR_BASE;
  int adapter_num = PACKET_LIB_ADAPTER_NR;
  enum link_adapter_event linkstate;

#ifdef PACKET_LIB_GET_ADAPTER_NETADDRESS
  ip_addr_t netaddr;
#define GUID_LEN 128
  char guid[GUID_LEN + 1];
  memset(&guid, 0, sizeof(guid));
  PACKET_LIB_GET_ADAPTER_NETADDRESS(&netaddr);
  if (get_adapter_index_from_addr((struct in_addr *)&netaddr, guid, GUID_LEN) < 0) {
     printf("ERROR initializing network adapter, failed to get GUID for network address %s\n", ip_ntoa(&netaddr));
     LWIP_ASSERT("ERROR initializing network adapter, failed to get GUID for network address!", 0);
     return;
  }
  adapter_num = get_adapter_index(guid);
  if (adapter_num < 0) {
     printf("ERROR finding network adapter with GUID \"%s\"!\n", guid);
     LWIP_ASSERT("ERROR finding network adapter with expected GUID!", 0);
     return;
  }

#else /* PACKET_LIB_GET_ADAPTER_NETADDRESS */
#ifdef PACKET_LIB_ADAPTER_GUID
  /* get adapter index for guid string */
  adapter_num = get_adapter_index(PACKET_LIB_ADAPTER_GUID);
  if (adapter_num < 0) {
    printf("ERROR finding network adapter with GUID \"%s\"!\n", PACKET_LIB_ADAPTER_GUID);
    LWIP_ASSERT("ERROR initializing network adapter!\n", 0);
    return;
  }
#endif /* PACKET_LIB_ADAPTER_GUID */
#endif /* PACKET_LIB_GET_ADAPTER_NETADDRESS */

  /* Do whatever else is needed to initialize interface. */
  if ((netif->state = init_adapter(adapter_num, adapter_mac_addr,
                      ethernetif_process_input, netif, &linkstate)) == NULL) {
    printf("ERROR initializing network adapter %d!\n", PACKET_LIB_ADAPTER_NR);
    LWIP_ASSERT("ERROR initializing network adapter!", 0);
    return;
  }

  /* change the MAC address to a unique value
     so that multiple ethernetifs are supported */
  my_mac_addr[ETHARP_HWADDR_LEN - 1] += netif->num;
  /* Copy MAC addr */
  memcpy(&netif->hwaddr, my_mac_addr, ETHARP_HWADDR_LEN);

  if (linkstate == LINKEVENT_UP) {
    netif_set_link_up(netif);
  } else {
    netif_set_link_down(netif);
  }

  LWIP_DEBUGF(NETIF_DEBUG, ("pktif: eth_addr %02X%02X%02X%02X%02X%02X\n",netif->hwaddr[0],netif->hwaddr[1],netif->hwaddr[2],netif->hwaddr[3],netif->hwaddr[4],netif->hwaddr[5]));
}
/**
* @brief  This function handles Ethernet link status.
* @param  None
* @retval None
*/
void Eth_Link_IT_task( void * pvParameters )
{
  uint32_t pcPHYAddress;
  
  pcPHYAddress = ( uint32_t  ) pvParameters;
  
  for(;;)
  {
    if (xSemaphoreTake( ETH_link_xSemaphore, emacBLOCK_TIME_WAITING_ETH_LINK_IT)==pdTRUE)
    {
      /* Check whether the link interrupt has occurred or not */
      if(((ETH_ReadPHYRegister((uint16_t) pcPHYAddress, PHY_MISR)) & PHY_LINK_STATUS) != 0)
      {
        if((ETH_ReadPHYRegister((uint16_t) pcPHYAddress, PHY_SR) & 1))
        {
          netif_set_link_up(&xnetif);
        }
        else
        {
          netif_set_link_down(&xnetif);
        }
      }
    }
  }
}
Beispiel #4
0
void 
main(void)
{
  struct netif netif;

  lwip_init();

  netif_add(&netif, IP4_ADDR_ANY, IP4_ADDR_ANY, IP4_ADDR_ANY, NULL, netif_init, netif_input);
  netif.name[0] = 'e';
  netif.name[1] = '0';
  netif_create_ip6_linklocal_address(&netif, 1);
  netif.ip6_autoconfig_enabled = 1;
  netif_set_status_callback(&netif, netif_status_callback);
  netif_set_default(&netif);
  netif_set_up(&netif);
  
  /* Start DHCP and HTTPD */
  dhcp_start(&netif );
  httpd_init();

  while(1) {
    /* Check link state, e.g. via MDIO communication with PHY */
    if(link_state_changed()) {
      if(link_is_up()) {
        netif_set_link_up(&netif);
      } else {
        netif_set_link_down(&netif);
      }
    }

    /* Check for received frames, feed them to lwIP */
    lock_interrupts();
    struct pbuf* p = queue_try_get(&queue);
    unlock_interrupts();

    if(p != NULL) {
      LINK_STATS_INC(link.recv);
 
      /* Update SNMP stats (only if you use SNMP) */
      MIB2_STATS_NETIF_ADD(netif, ifinoctets, p->tot_len);
      int unicast = ((p->payload[0] & 0x01) == 0);
      if (unicast) {
        MIB2_STATS_NETIF_INC(netif, ifinucastpkts);
      } else {
        MIB2_STATS_NETIF_INC(netif, ifinnucastpkts);
      }

      if(netif.input(p, &netif) != ERR_OK) {
        pbuf_free(p);
      }
    }
     
    /* Cyclic lwIP timers check */
    sys_check_timeouts();
     
    /* your application goes here */
  }
}
STATIC void wiznet5k_send_ethernet(wiznet5k_obj_t *self, size_t len, const uint8_t *buf) {
    uint8_t ip[4] = {1, 1, 1, 1}; // dummy
    int ret = WIZCHIP_EXPORT(sendto)(0, (byte*)buf, len, ip, 11); // dummy port
    if (ret != len) {
        printf("wiznet5k_send_ethernet: fatal error %d\n", ret);
        netif_set_link_down(&self->netif);
        netif_set_down(&self->netif);
    }
}
static void handleDisconnectEvt(cbBCM_Handle connHandle)
{
    printf("%s\n",__FUNCTION__);
    
    MBED_ASSERT(panIf.connHandle == connHandle);

    struct netif* netif = &panIf.hInterface;
    netif_set_link_down(netif);
    panIf.connHandle = cbBCM_INVALID_CONNECTION;
}
/**
 * @brief  This function handles Ethernet link status.
 * @param  None
 * @retval None
 */
void Eth_Link_ITHandler(uint16_t PHYAddress) {
	/* Check whether the link interrupt has occurred or not */
	if (((ETH_ReadPHYRegister(PHYAddress, PHY_MISR )) & PHY_LINK_STATUS )!= 0){
		if ((ETH_ReadPHYRegister(PHYAddress, PHY_SR ) & 1)) {
			netif_set_link_up(&gnetif);
		} else {
			netif_set_link_down(&gnetif);
		}
	}
}
Beispiel #8
0
void VBoxNetLwipNAT::onLwipTcpIpFini(void* arg)
{
    AssertPtrReturnVoid(arg);
    VBoxNetLwipNAT *pThis = (VBoxNetLwipNAT *)arg;

    /* XXX: proxy finalization */
    netif_set_link_down(&g_pLwipNat->m_LwipNetIf);
    netif_set_down(&g_pLwipNat->m_LwipNetIf);
    netif_remove(&g_pLwipNat->m_LwipNetIf);

}
/**
  * @brief  Stop lwip service in a certain operation mode.
  * @param[in] uint8_t opmode: the current operation mode.
  * @retval None
  */
void lwip_net_stop(uint8_t opmode)
{
    struct netif *sta_if;
    struct netif *ap_if;

    sta_if = netif_find_by_type(NETIF_TYPE_STA);
    ap_if = netif_find_by_type(NETIF_TYPE_AP);
    switch (opmode) {
        case WIFI_MODE_AP_ONLY:
            dhcpd_stop();
            netif_set_link_down(ap_if);
            break;
        case WIFI_MODE_STA_ONLY:
        case WIFI_MODE_REPEATER:
            netif_set_status_callback(sta_if, NULL);
            dhcp_release(sta_if);
            dhcp_stop(sta_if);
            netif_set_link_down(sta_if);
            break;
    }
}
Beispiel #10
0
static void
test_ip6_ll_addr_iter(int expected_ctr1, int expected_ctr2)
{
  fail_unless(linkoutput_ctr == 0);

  /* test that nothing is sent with link uo but netif down */
  netif_set_link_up(&test_netif6);
  ip6_test_handle_timers(500);
  fail_unless(linkoutput_ctr == 0);
  netif_set_link_down(&test_netif6);
  fail_unless(linkoutput_ctr == 0);

  /* test that nothing is sent with link down but netif up */
  netif_set_up(&test_netif6);
  ip6_test_handle_timers(500);
  fail_unless(linkoutput_ctr == 0);
  netif_set_down(&test_netif6);
  fail_unless(linkoutput_ctr == 0);

  /* test what is sent with link up + netif up */
  netif_set_link_up(&test_netif6);
  netif_set_up(&test_netif6);
  ip6_test_handle_timers(500);
  fail_unless(linkoutput_ctr == expected_ctr1);
  netif_set_down(&test_netif6);
  netif_set_link_down(&test_netif6);
  fail_unless(linkoutput_ctr == expected_ctr1);
  linkoutput_ctr = 0;

  netif_set_up(&test_netif6);
  netif_set_link_up(&test_netif6);
  ip6_test_handle_timers(500);
  fail_unless(linkoutput_ctr == expected_ctr2);
  netif_set_link_down(&test_netif6);
  netif_set_down(&test_netif6);
  fail_unless(linkoutput_ctr == expected_ctr2);
  linkoutput_ctr = 0;
}
Beispiel #11
0
VOID
TCPUpdateInterfaceLinkStatus(PIP_INTERFACE IF)
{
#if 0
    ULONG OperationalStatus;
    
    GetInterfaceConnectionStatus(IF, &OperationalStatus);
    
    if (OperationalStatus == MIB_IF_OPER_STATUS_OPERATIONAL)
        netif_set_link_up(IF->TCPContext);
    else
        netif_set_link_down(IF->TCPContext);
#endif
}
// Stores the frame in self->eth_frame and returns number of bytes in the frame, 0 for no frame
STATIC uint16_t wiznet5k_recv_ethernet(wiznet5k_obj_t *self) {
    uint16_t len = getSn_RX_RSR(0);
    if (len == 0) {
        return 0;
    }

    byte ip[4];
    uint16_t port;
    int ret = WIZCHIP_EXPORT(recvfrom)(0, self->eth_frame, 1514, ip, &port);
    if (ret <= 0) {
        printf("wiznet5k_lwip_poll: fatal error len=%u ret=%d\n", len, ret);
        netif_set_link_down(&self->netif);
        netif_set_down(&self->netif);
        return 0;
    }

    return ret;
}
STATIC mp_obj_t wiznet5k_active(size_t n_args, const mp_obj_t *args) {
    wiznet5k_obj_t *self = MP_OBJ_TO_PTR(args[0]);
    if (n_args == 1) {
        return mp_obj_new_bool(self->netif.flags & NETIF_FLAG_UP);
    } else {
        if (mp_obj_is_true(args[1])) {
            if (!(self->netif.flags & NETIF_FLAG_UP)) {
                wiznet5k_init();
                netif_set_link_up(&self->netif);
                netif_set_up(&self->netif);
            }
        } else {
            if (self->netif.flags & NETIF_FLAG_UP) {
                netif_set_down(&self->netif);
                netif_set_link_down(&self->netif);
                wiznet5k_deinit();
            }
        }
        return mp_const_none;
    }
}
Beispiel #14
0
/**
  * @brief  wifi disconnected will call this callback function. set lwip status in this function
  * @param[in] wifi_event_t event: not used.
  * @param[in] uint8_t *payload: not used.
  * @param[in] uint32_t length: not used.
  * @retval None
  */
static int32_t wifi_station_disconnected_event_handler(wifi_event_t event,
        uint8_t *payload,
        uint32_t length)
{
    uint8_t opmode  = 0;

    wifi_config_get_opmode(&opmode);
    if ((WIFI_MODE_AP_ONLY != opmode) && WIFI_EVENT_IOT_DISCONNECTED == event) {
        uint8_t link_status = 1;
        //should check link status, it will emit this event when sp disconnect with host under repeater mode.
        wifi_connection_get_link_status(&link_status);
        if (link_status == 0) {
            struct netif *sta_if;
            sta_if = netif_find_by_type(NETIF_TYPE_STA);
            netif_set_link_down(sta_if);
        #if USE_DHCP
            netif_set_addr(sta_if, IP4_ADDR_ANY, IP4_ADDR_ANY, IP4_ADDR_ANY);
        #endif
            LOG_I(common, "wifi disconnected");
        }
    }
    return 1;
}
static int32_t _wifi_event_handler(wifi_event_t event,
        uint8_t *payload,
        uint32_t length)
{
    struct netif *sta_if;
    LOG_I(common, "wifi event: %d", event);

    switch(event)
    {
    case WIFI_EVENT_IOT_PORT_SECURE:
        sta_if = netif_find_by_type(NETIF_TYPE_STA);
        netif_set_link_up(sta_if);
        LOG_I(common, "wifi connected");
        break;
    case WIFI_EVENT_IOT_DISCONNECTED:
        sta_if = netif_find_by_type(NETIF_TYPE_STA);
        netif_set_link_down(sta_if);
        LOG_I(common, "wifi disconnected");
        break;
    }

    return 1;
}
Beispiel #16
0
/** Low-level initialization: find the correct adapter and initialize it.
 */
static void
pcapif_low_level_init(struct netif *netif)
{
  u8_t my_mac_addr[ETHARP_HWADDR_LEN] = LWIP_MAC_ADDR_BASE;
  int adapter_num = PACKET_LIB_ADAPTER_NR;
  struct pcapif_private *pa;

  /* If 'state' is != NULL at this point, we assume it is an 'int' giving
     the index of the adapter to use (+ 1 because 0==NULL is invalid).
     This can be used to instantiate multiple PCAP drivers. */
  if (netif->state != NULL) {
    adapter_num = ((int)netif->state) - 1;
    if (adapter_num < 0) {
      printf("ERROR: invalid adapter index \"%d\"!\n", adapter_num);
      LWIP_ASSERT("ERROR initializing network adapter!\n", 0);
      return;
    }
  }

#ifdef PACKET_LIB_GET_ADAPTER_NETADDRESS
  ip_addr_t netaddr;
#define GUID_LEN 128
  char guid[GUID_LEN + 1];
  memset(&guid, 0, sizeof(guid));
  PACKET_LIB_GET_ADAPTER_NETADDRESS(&netaddr);
  if (get_adapter_index_from_addr((struct in_addr *)&netaddr, guid, GUID_LEN) < 0) {
     printf("ERROR initializing network adapter, failed to get GUID for network address %s\n", ip_ntoa(&netaddr));
     LWIP_ASSERT("ERROR initializing network adapter, failed to get GUID for network address!", 0);
     return;
  }
  adapter_num = get_adapter_index(guid);
  if (adapter_num < 0) {
     printf("ERROR finding network adapter with GUID \"%s\"!\n", guid);
     LWIP_ASSERT("ERROR finding network adapter with expected GUID!", 0);
     return;
  }

#else /* PACKET_LIB_GET_ADAPTER_NETADDRESS */
#ifdef PACKET_LIB_ADAPTER_GUID
  /* get adapter index for guid string */
  adapter_num = get_adapter_index(PACKET_LIB_ADAPTER_GUID);
  if (adapter_num < 0) {
    printf("ERROR finding network adapter with GUID \"%s\"!\n", PACKET_LIB_ADAPTER_GUID);
    LWIP_ASSERT("ERROR initializing network adapter!\n", 0);
    return;
  }
#endif /* PACKET_LIB_ADAPTER_GUID */
#endif /* PACKET_LIB_GET_ADAPTER_NETADDRESS */

  /* Do whatever else is needed to initialize interface. */
  pa = pcapif_init_adapter(adapter_num, netif);
  if (pa == NULL) {
    printf("ERROR initializing network adapter %d!\n", adapter_num);
    LWIP_ASSERT("ERROR initializing network adapter!", 0);
    return;
  }
  netif->state = pa;

  /* change the MAC address to a unique value
     so that multiple ethernetifs are supported */
  /* @todo: this does NOT support multiple processes using this adapter! */
  my_mac_addr[ETHARP_HWADDR_LEN - 1] += netif->num;
  /* Copy MAC addr */
  memcpy(&netif->hwaddr, my_mac_addr, ETHARP_HWADDR_LEN);

  /* get the initial link state of the selected interface */
#if PCAPIF_HANDLE_LINKSTATE
  pa->last_link_event = pcapifh_linkstate_get(pa->link_state);
  if (pa->last_link_event == PCAPIF_LINKEVENT_DOWN) {
    netif_set_link_down(netif);
  } else {
    netif_set_link_up(netif);
  }
  sys_timeout(PCAPIF_LINKCHECK_INTERVAL_MS, pcapif_check_linkstate, netif);
#endif /* PCAPIF_HANDLE_LINKSTATE */

#if PCAPIF_RX_USE_THREAD
  pa->rx_run = 1;
  pa->rx_running = 1;
  sys_thread_new("pcapif_rxthread", pcapif_input_thread, netif, 0, 0);
#endif

  LWIP_DEBUGF(NETIF_DEBUG, ("pcapif: eth_addr %02X%02X%02X%02X%02X%02X\n",netif->hwaddr[0],netif->hwaddr[1],netif->hwaddr[2],netif->hwaddr[3],netif->hwaddr[4],netif->hwaddr[5]));
}
Beispiel #17
0
//*****************************************************************************
//
//! Service the lwIP timers.
//!
//! This function services all of the lwIP periodic timers, including TCP and
//! Host timers.  This should be called from the lwIP context, which may be
//! the Ethernet interrupt (in the case of a non-RTOS system) or the lwIP
//! thread, in the event that an RTOS is used.
//!
//! \return None.
//
//*****************************************************************************
static void lwIPServiceTimers(struct netif *netif)
{
	LWIP_DRIVER_DATA* drv_data = (LWIP_DRIVER_DATA*)netif;
	MAC_Type* mac = (MAC_Type*)netif->state;
    //
    // Service the MDIX timer.
    //
    if(drv_data->timer_main > drv_data->timer_mdix)
    {
        //
        // See if there has not been a link for 2 seconds.
        //
        if((EthernetPHYRead(mac, PHY_MR1) & PHY_MR1_LINK) == 0)
        {
            // There has not been a link for 2 seconds, so flip the MDI/MDIX
            // switch.  This is handled automatically by Fury rev A2, but is
            // harmless.
            //
        	mac->MDIX  ^= MAC_MDIX_EN;
       	    if (netif->flags & NETIF_FLAG_LINK_UP)
			{
#if LWIP_DHCP
                autoip_stop(netif);
                dhcp_start(netif);
#endif
				netif_set_link_down(netif);
			}
		}
		else
			netif_set_link_up(netif);
        //
        // Reset the MDIX timer.
        //
    	drv_data->timer_mdix = drv_data->timer_main + 2048;
    }

    //
    // Service the host timer.
    //
#if HOST_TMR_INTERVAL
    if(drv_data->timer_main > drv_data->timer_host )
    {
    	drv_data->timer_host = drv_data->timer_main + HOST_TMR_INTERVAL;
        lwIPHostTimerHandler();
    }
#endif

    //
    // Service the ARP timer.
    //
#if LWIP_ARP
    if(drv_data->timer_main > drv_data->timer_arp)
    {
    	drv_data->timer_arp = drv_data->timer_main + ARP_TMR_INTERVAL;
        etharp_tmr();
    }
#endif

    //
    // Service the TCP timer.
    //
#if LWIP_TCP
    if(drv_data->timer_main > drv_data->timer_tcp)
    {
    	drv_data->timer_tcp = drv_data->timer_main + TCP_TMR_INTERVAL;
        tcp_tmr();
    }
#endif

    //
    // Service the AutoIP timer.
    //
#if LWIP_AUTOIP
    if(drv_data->timer_main - drv_data->timer_autoIP)
    {
    	drv_data->timer_autoIP = drv_data->timer_main + AUTOIP_TMR_INTERVAL;
        autoip_tmr();
    }
#endif

    //
    // Service the DCHP Coarse Timer.
    //
#if LWIP_DHCP
    if(drv_data->timer_main > drv_data->timer_DHCPCoarse)
    {
    	drv_data->timer_DHCPCoarse = drv_data->timer_main + DHCP_COARSE_TIMER_MSECS;
        dhcp_coarse_tmr();
    }
#endif

    //
    // Service the DCHP Fine Timer.
    //
#if LWIP_DHCP
    if(drv_data->timer_main > drv_data->timer_DHCPFine)
    {
    	drv_data->timer_DHCPFine = drv_data->timer_main + DHCP_FINE_TIMER_MSECS;
        dhcp_fine_tmr();
    }
#endif
}
Beispiel #18
0
void ftMac100_InterruptHandler( void )
{
	extern portBASE_TYPE xInsideISR;
	int i, status, phycr;

	/* Ensure the pbuf handling functions don't attempt to use critical
	sections. */
	xInsideISR++;

	status = FTMAC100_OFFSET_ISR;

	if( status & (FTMAC100_INT_RPKT_FINISH | FTMAC100_INT_NORXBUF) )
	{
		sys_sem_signal( &pgMac100If->rx_sem );
	}

	if( status & (FTMAC100_INT_NORXBUF | FTMAC100_INT_RPKT_LOST |
		      FTMAC100_INT_AHB_ERR | FTMAC100_INT_PHYSTS_CHG) )
	{
		LWIP_DEBUGF( NETIF_DEBUG, ( "[ISR] = 0x%x: %s%s%s%s\n", status,
				    status & FTMAC100_INT_NORXBUF ? "NORXBUF " : "",
				    status & FTMAC100_INT_RPKT_LOST ? "RPKT_LOST " : "",
				    status & FTMAC100_INT_AHB_ERR ? "AHB_ERR " : "",
				    status & FTMAC100_INT_PHYSTS_CHG ? "PHYSTS_CHG" : "") );

		if( status & FTMAC100_INT_NORXBUF )
		{
			/* RX buffer unavailable */
#if LINK_STATS
			LINK_STATS_INC( link.memerr );
#endif
		}

		if( status & FTMAC100_INT_RPKT_LOST )
		{
			/* received packet lost due to RX FIFO full */
#if LINK_STATS
			LINK_STATS_INC( link.memerr );
#endif
		}

		if( status & FTMAC100_INT_PHYSTS_CHG )
		{
			/* PHY link status change */
			phycr = FTMAC100_PHYCR_PHYAD(0) |
				FTMAC100_PHYCR_REGAD(1) |
				FTMAC100_PHYCR_MIIRD;

			FTMAC100_OFFSET_PHYCR = phycr;

			for (i = 0; i < 10; i++) {
				phycr = FTMAC100_OFFSET_PHYCR;

				if( (phycr & FTMAC100_PHYCR_MIIRD) == 0 )
					break;
			}

			if( phycr & 0x4 )
				netif_set_link_up( pgMac100If->netIf );
			else
				netif_set_link_down( pgMac100If->netIf );
		}
	}

	xInsideISR--;
}
/** \brief  Update PHY status from passed value
 *
 *  This function updates the current PHY status based on the
 *  passed PHY status word. The PHY status indicate if the link
 *  is active, the connection speed, and duplex.
 *
 *  \param[in]    netif   NETIF structure
 *  \param[in]    linksts Status word with link state
 *  \param[in]    sdsts   Status word with speed and duplex states
 *  \return        1 if the status has changed, otherwise 0
 */
static s32_t lpc_update_phy_sts(struct netif *netif, u32_t linksts, u32_t sdsts)
{
	s32_t changed = 0;

	/* Update link active status */
	if (linksts & LAN8_LINK_STATUS)
		physts.phy_link_active = 1;
	else
		physts.phy_link_active = 0;

	switch (sdsts & LAN8_SPEEDMASK) {
		case LAN8_SPEED100F:
		default:
			physts.phy_speed_100mbs = 1;
			physts.phy_full_duplex = 1;
			break;

		case LAN8_SPEED10F:
			physts.phy_speed_100mbs = 0;
			physts.phy_full_duplex = 1;
			break;

		case LAN8_SPEED100H:
			physts.phy_speed_100mbs = 1;
			physts.phy_full_duplex = 0;
			break;

		case LAN8_SPEED10H:
			physts.phy_speed_100mbs = 0;
			physts.phy_full_duplex = 0;
			break;
	}

	if (physts.phy_speed_100mbs != olddphysts.phy_speed_100mbs) {
		changed = 1;
		if (physts.phy_speed_100mbs) {
			/* 100MBit mode. */
			lpc_emac_set_speed(1);

			NETIF_INIT_SNMP(netif, snmp_ifType_ethernet_csmacd, 100000000);
		}
		else {
			/* 10MBit mode. */
			lpc_emac_set_speed(0);

			NETIF_INIT_SNMP(netif, snmp_ifType_ethernet_csmacd, 10000000);
		}

		olddphysts.phy_speed_100mbs = physts.phy_speed_100mbs;
	}

	if (physts.phy_full_duplex != olddphysts.phy_full_duplex) {
		changed = 1;
		if (physts.phy_full_duplex)
			lpc_emac_set_duplex(1);
		else
			lpc_emac_set_duplex(0);

		olddphysts.phy_full_duplex = physts.phy_full_duplex;
	}

	if (physts.phy_link_active != olddphysts.phy_link_active) {
		changed = 1;
#if NO_SYS == 1
		if (physts.phy_link_active)
            netif_set_link_up(netif);
		else
			netif_set_link_down(netif);
#else
		if (physts.phy_link_active)
            tcpip_callback_with_block((tcpip_callback_fn) netif_set_link_up,
                (void*) netif, 1);
         else
            tcpip_callback_with_block((tcpip_callback_fn) netif_set_link_down,
                (void*) netif, 1);
#endif

		olddphysts.phy_link_active = physts.phy_link_active;
	}

	return changed;
}
Beispiel #20
0
/**
 * @brief	main routine for example_lwip_tcpecho_sa_17xx40xx
 * @return	Function should not exit.
 */
int main(void)
{
	uint32_t physts;
	ip_addr_t ipaddr, netmask, gw;
	static int prt_ip = 0;

	prvSetupHardware();






	/* Initialize LWIP */
	lwip_init();



	///LWIP_DEBUGF(LWIP_DBG_ON, ("Starting LWIP TCP echo server...\n"));

	/* Static IP assignment */

	IP4_ADDR(&gw, 0, 0, 0, 0);
	IP4_ADDR(&ipaddr, 0, 0, 0, 0);
	IP4_ADDR(&netmask, 0, 0, 0, 0);

	/* Add netif interface for lpc17xx_8x */


	netif_add(&lpc_netif, &ipaddr, &netmask, &gw, NULL, lpc_enetif_init,  ethernet_input);
	netif_set_default(&lpc_netif);
	netif_set_up(&lpc_netif);


	dhcp_start(&lpc_netif);



	uint8_t fl = 0 ;



	while (1)

	{



		/* Handle packets as part of this loop, not in the IRQ handler */



				lpc_enetif_input(&lpc_netif);



				/* lpc_rx_queue will re-qeueu receive buffers. This normally occurs
				   automatically, but in systems were memory is constrained, pbufs
				   may not always be able to get allocated, so this function can be
				   optionally enabled to re-queue receive buffers. */

		#if 0
				while (lpc_rx_queue(&lpc_netif)) {}
		#endif

				/* Free TX buffers that are done sending */
				lpc_tx_reclaim(&lpc_netif);

				/* LWIP timers - ARP, DHCP, TCP, etc. */
				sys_check_timeouts();

				/* Call the PHY status update state machine once in a while
				   to keep the link status up-to-date */
				physts = lpcPHYStsPoll();

				/* Only check for connection state when the PHY status has changed */

				if (physts & PHY_LINK_CHANGED)
				{
					if (physts & PHY_LINK_CONNECTED)
					{



						prt_ip = 0;

						/* Set interface speed and duplex */
						if (physts & PHY_LINK_SPEED100)
						{
							Chip_ENET_Set100Mbps(LPC_ETHERNET);
							NETIF_INIT_SNMP(&lpc_netif, snmp_ifType_ethernet_csmacd, 100000000);
						}
						else
						{
							Chip_ENET_Set10Mbps(LPC_ETHERNET);
							NETIF_INIT_SNMP(&lpc_netif, snmp_ifType_ethernet_csmacd, 10000000);
						}
						if (physts & PHY_LINK_FULLDUPLX) {
							Chip_ENET_SetFullDuplex(LPC_ETHERNET);
						}
						else {
							Chip_ENET_SetHalfDuplex(LPC_ETHERNET);
						}

						netif_set_link_up(&lpc_netif);
					}
					else
					{



						netif_set_link_down(&lpc_netif);
					}

					DEBUGOUT("Link connect status: %d\r\n", ((physts & PHY_LINK_CONNECTED) != 0));
				}

				/* Print IP address info */

				if (!prt_ip)
				{
					if (lpc_netif.ip_addr.addr)
					{
						static char tmp_buff[16];
						DEBUGOUT("IP_ADDR    : %s\r\n", ipaddr_ntoa_r((const ip_addr_t *) &lpc_netif.ip_addr, tmp_buff, 16));
						DEBUGOUT("NET_MASK   : %s\r\n", ipaddr_ntoa_r((const ip_addr_t *) &lpc_netif.netmask, tmp_buff, 16));
						DEBUGOUT("GATEWAY_IP : %s\r\n", ipaddr_ntoa_r((const ip_addr_t *) &lpc_netif.gw, tmp_buff, 16));
						prt_ip = 1;




						mqttAppInit();

						mqttAppConnect();



						//////   mqttAppDisconnect();


					}
				}

				if(mqttIsConnected()==1)
				{
					mqttAppHandle();


					if (fl == 0)
					{



						mqttAppSubscribe("lpc/#");
						mqttAppSubscribe("lpc/teste");


						fl = 1;

					}

				}








	}



	return 0;
}
/** \brief  Update PHY status from passed value
 *
 *  This function updates the current PHY status based on the
 *  passed PHY status word. The PHY status indicate if the link
 *  is active, the connection speed, and duplex.
 *
 *  \param[in]    netif   NETIF structure
 *  \param[in]    linksts Status word from PHY
 *  \return        1 if the status has changed, otherwise 0
 */
static s32_t lpc_update_phy_sts(struct netif *netif, u32_t linksts)
{
    s32_t changed = 0;

    /* Update link active status */
    if (linksts & DP8_VALID_LINK)
        physts.phy_link_active = 1;
    else
        physts.phy_link_active = 0;

    /* Full or half duplex */
    if (linksts & DP8_FULLDUPLEX)
        physts.phy_full_duplex = 1;
    else
        physts.phy_full_duplex = 0;

    /* Configure 100MBit/10MBit mode. */
    if (linksts & DP8_SPEED10MBPS)
        physts.phy_speed_100mbs = 0;
    else
        physts.phy_speed_100mbs = 1;

    if (physts.phy_speed_100mbs != olddphysts.phy_speed_100mbs) {
        changed = 1;
        if (physts.phy_speed_100mbs) {
            /* 100MBit mode. */
            lpc_emac_set_speed(1);

            NETIF_INIT_SNMP(netif, snmp_ifType_ethernet_csmacd, 100000000);
        }
        else {
            /* 10MBit mode. */
            lpc_emac_set_speed(0);

            NETIF_INIT_SNMP(netif, snmp_ifType_ethernet_csmacd, 10000000);
        }

        olddphysts.phy_speed_100mbs = physts.phy_speed_100mbs;
    }

    if (physts.phy_full_duplex != olddphysts.phy_full_duplex) {
        changed = 1;
        if (physts.phy_full_duplex)
            lpc_emac_set_duplex(1);
        else
            lpc_emac_set_duplex(0);

        olddphysts.phy_full_duplex = physts.phy_full_duplex;
    }

    if (physts.phy_link_active != olddphysts.phy_link_active) {
        changed = 1;
#if NO_SYS == 1
        if (physts.phy_link_active)
            netif_set_link_up(netif);
        else
            netif_set_link_down(netif);
#else
        if (physts.phy_link_active)
            tcpip_callback_with_block((tcpip_callback_fn) netif_set_link_up,
                (void*) netif, 1);
         else
            tcpip_callback_with_block((tcpip_callback_fn) netif_set_link_down,
                (void*) netif, 1);
#endif

        olddphysts.phy_link_active = physts.phy_link_active;
    }

    return changed;
}
Beispiel #22
0
/**
 * @brief	main routine for example_lwip_tcpecho_sa_18xx43xx
 * @return	Function should not exit.
 */
int main(void)
{
	uint32_t physts;
	ip_addr_t ipaddr, netmask, gw;

	prvSetupHardware();

	/* Initialize LWIP */
	lwip_init();

	LWIP_DEBUGF(LWIP_DBG_ON, ("Starting LWIP TCP echo server...\n"));

	/* Static IP assignment */
#if LWIP_DHCP
	IP4_ADDR(&gw, 0, 0, 0, 0);
	IP4_ADDR(&ipaddr, 0, 0, 0, 0);
	IP4_ADDR(&netmask, 0, 0, 0, 0);
#else
	IP4_ADDR(&gw, 10, 1, 10, 1);
	IP4_ADDR(&ipaddr, 10, 1, 10, 234);
	IP4_ADDR(&netmask, 255, 255, 255, 0);
	APP_PRINT_IP(&ipaddr);
#endif

	/* Add netif interface for lpc17xx_8x */
	netif_add(&lpc_netif, &ipaddr, &netmask, &gw, NULL, lpc_enetif_init,
			  ethernet_input);
	netif_set_default(&lpc_netif);
	netif_set_up(&lpc_netif);

#if LWIP_DHCP
	dhcp_start(&lpc_netif);
#endif

	/* Initialize and start application */
	echo_init();

	/* This could be done in the sysTick ISR, but may stay in IRQ context
	   too long, so do this stuff with a background loop. */
	while (1) {
		/* Handle packets as part of this loop, not in the IRQ handler */
		lpc_enetif_input(&lpc_netif);

		/* lpc_rx_queue will re-qeueu receive buffers. This normally occurs
		   automatically, but in systems were memory is constrained, pbufs
		   may not always be able to get allocated, so this function can be
		   optionally enabled to re-queue receive buffers. */
#if 0
		while (lpc_rx_queue(&lpc_netif)) {}
#endif

		/* Free TX buffers that are done sending */
		lpc_tx_reclaim(&lpc_netif);

		/* LWIP timers - ARP, DHCP, TCP, etc. */
		sys_check_timeouts();

		/* Call the PHY status update state machine once in a while
		   to keep the link status up-to-date */
		physts = lpcPHYStsPoll();

		/* Only check for connection state when the PHY status has changed */
		if (physts & PHY_LINK_CHANGED) {
			if (physts & PHY_LINK_CONNECTED) {
				Board_LED_Set(0, true);

				/* Set interface speed and duplex */
				if (physts & PHY_LINK_SPEED100) {
					Chip_ENET_SetSpeed(LPC_ETHERNET, 1);
					NETIF_INIT_SNMP(&lpc_netif, snmp_ifType_ethernet_csmacd, 100000000);
				}
				else {
					Chip_ENET_SetSpeed(LPC_ETHERNET, 0);
					NETIF_INIT_SNMP(&lpc_netif, snmp_ifType_ethernet_csmacd, 10000000);
				}
				if (physts & PHY_LINK_FULLDUPLX) {
					Chip_ENET_SetDuplex(LPC_ETHERNET, true);
				}
				else {
					Chip_ENET_SetDuplex(LPC_ETHERNET, false);
				}

				netif_set_link_up(&lpc_netif);
			}
			else {
				Board_LED_Set(0, false);
				netif_set_link_down(&lpc_netif);
			}

			DEBUGOUT("Link connect status: %d\r\n", ((physts & PHY_LINK_CONNECTED) != 0));
		}
	}

	/* Never returns, for warning only */
	return 0;
}
/**
 * Update PHY status from passed value
 *
 * This function updates the current PHY status based on the
 * passed PHY status word. The PHY status indicate if the link
 * is active, the connection speed, and duplex.
 *
 * \param [in]    netif   NETIF structure
 * \param [in]    linksts Status word with link state
 * \param [in]    sdsts   Status word with speed and duplex states
 * \return        1 if the status has changed, otherwise 0
 */
static s32_t lpc_update_phy_sts(struct netif *netif, u32_t linksts, u32_t sdsts) {
  s32_t changed = 0;

  /* Update link active status */
  if (linksts & LAN8_LINK_STATUS)
    physts.phy_link_active = 1;
  else
    physts.phy_link_active = 0;

  switch (sdsts & LAN8_SPEEDMASK) {
    case LAN8_SPEED100F:
    default:
      physts.phy_speed_100mbs = 1;
      physts.phy_full_duplex = 1;
      break;

    case LAN8_SPEED10F:
      physts.phy_speed_100mbs = 0;
      physts.phy_full_duplex = 1;
      break;

    case LAN8_SPEED100H:
      physts.phy_speed_100mbs = 1;
      physts.phy_full_duplex = 0;
      break;

    case LAN8_SPEED10H:
      physts.phy_speed_100mbs = 0;
      physts.phy_full_duplex = 0;
      break;
  }

  if (physts.phy_speed_100mbs != olddphysts.phy_speed_100mbs) {
    changed = 1;
    if (physts.phy_speed_100mbs) {
      /* 100MBit mode. */
      LPC_EMAC->SUPP |= EMAC_SUPP_SPEED;

      NETIF_INIT_SNMP(netif, snmp_ifType_ethernet_csmacd, 100000000);
    }
    else {
      /* 10MBit mode. */
      LPC_EMAC->SUPP &= ~EMAC_SUPP_SPEED;

      NETIF_INIT_SNMP(netif, snmp_ifType_ethernet_csmacd, 10000000);
    }

    olddphysts.phy_speed_100mbs = physts.phy_speed_100mbs;
  }

  if (physts.phy_full_duplex != olddphysts.phy_full_duplex) {
    changed = 1;
    if (physts.phy_full_duplex) {
      /* Full duplex */
      LPC_EMAC->MAC2    |= EMAC_MAC2_FULL_DUP;
      LPC_EMAC->Command |= EMAC_CR_FULL_DUP;
      LPC_EMAC->IPGT     = EMAC_IPGT_FULL_DUP;
    }
    else {
      /* Half duplex mode. */
      LPC_EMAC->MAC2    &= ~EMAC_MAC2_FULL_DUP;
      LPC_EMAC->Command &= ~EMAC_CR_FULL_DUP;
      LPC_EMAC->IPGT = EMAC_IPGT_HALF_DUP;
    }

    olddphysts.phy_full_duplex = physts.phy_full_duplex;
  }

  if (physts.phy_link_active != olddphysts.phy_link_active) {
    changed = 1;
    if (physts.phy_link_active)
      netif_set_link_up(netif);
    else
      netif_set_link_down(netif);

    olddphysts.phy_link_active = physts.phy_link_active;
  }

  return changed;
}