main
Default mainpagebasic-emac-lwip-projectmain
Description Source Call Graph
Start Line: 275
int main()
{
    struct ip_addr ipaddr, netmask, gw;
    struct netif NetIf, *netif;
#if LWIP_DHCP
    u8_t   dhcp_state = DHCP_INIT;    
#endif

    Dm9161       *pDm = &gDm9161;
    unsigned int errCount = 0;

    TRACE_CONFIGURE(DBGU_STANDARD, 115200, BOARD_MCK);
    printf("-- Basic EMAC lwIP Project %s --\n\r", SOFTPACK_VERSION);
    printf("-- %s\n\r", BOARD_NAME);
    printf("-- Compiled: %s %s --\n\r", __DATE__, __TIME__);

    // Display MAC & IP settings
    printf(" - MAC %x:%x:%x:%x:%x:%x\n\r",
           MacAddress[0], MacAddress[1], MacAddress[2],
           MacAddress[3], MacAddress[4], MacAddress[5]);

#if !LWIP_DHCP
    printf(" - Host IP  %d.%d.%d.%d\n\r",
           IpAddress[0], IpAddress[1],
           IpAddress[2], IpAddress[3]);
    printf(" - Gateway IP  %d.%d.%d.%d\n\r",
           GateWay[0], GateWay[1], GateWay[2], GateWay[3]);
    printf(" - Net Mask  %d.%d.%d.%d\n\r",
           NetMask[0], NetMask[1], NetMask[2], NetMask[3]);
#else
    printf(" - DHCP Enabled\n\r");
#endif

#if !defined(BOARD_EMAC_POWER_ALWAYS_ON)
    // clear PHY power down mode
    PIO_Configure(emacPwrDn, 1);
#endif

    // Init EMAC driver structure
    EMAC_Init(AT91C_ID_EMAC, MacAddress, EMAC_CAF_ENABLE, EMAC_NBC_DISABLE);

    // Init DM9161 driver
    DM9161_Init(pDm, EMAC_PHY_ADDR);

    // PHY initialize
    if (!DM9161_InitPhy(pDm, BOARD_MCK,
                        emacRstPins, PIO_LISTSIZE(emacRstPins),
                        emacPins, PIO_LISTSIZE(emacPins))) {

        printf("P: PHY Initialize ERROR!\n\r");
        return -1;
    }
    // Auto Negotiate
    if (!DM9161_AutoNegotiate(pDm)) {

        printf("P: Auto Negotiate ERROR!\n\r");
        return -1;
    }

    while( DM9161_GetLinkSpeed(pDm, 1) == 0 ) {

        errCount++;
    }
    printf("P: Link detected 0x%X\n\r", errCount);

    // Setup EMAC buffers and interrupts
    AIC_ConfigureIT(AT91C_ID_EMAC, AT91C_AIC_SRCTYPE_INT_HIGH_LEVEL, ISR_Emac);
    AIC_EnableIT(AT91C_ID_EMAC);

    // Initialize system timing
    sys_init_timing();

    // Initialize lwIP modules
    lwip_init();

    // Initialize net interface for lwIP
    emacif_setmac((u8_t*)MacAddress);

#if !LWIP_DHCP
    IP4_ADDR(&gw, GateWay[0], GateWay[1], GateWay[2], GateWay[3]);
    IP4_ADDR(&ipaddr, IpAddress[0], IpAddress[1], IpAddress[2], IpAddress[3]);
    IP4_ADDR(&netmask, NetMask[0], NetMask[1], NetMask[2], NetMask[3]);
#else
    IP4_ADDR(&gw, 0, 0, 0, 0);
    IP4_ADDR(&ipaddr, 0, 0, 0, 0);
    IP4_ADDR(&netmask, 0, 0, 0, 0);
#endif

    netif = netif_add(&NetIf, &ipaddr, &netmask, &gw, NULL, emacif_init, ip_input);
    netif_set_default(netif);
    netif_set_up(netif);

  #if LWIP_DHCP
    dhcp_start(netif);
  #endif

    // Initialize http server application
    if (ERR_OK != httpd_init()) {

        return -1;
    }

    while(1) {

        // Display Statistics
        if ( USART_IsDataAvailable((AT91S_USART *)AT91C_BASE_DBGU) ) {

            EmacStats    stats;

            DBGU_GetChar();
            EMAC_GetStatistics(&stats, 1);
            printf("=== EMAC Statistics ===\n\r");
            printf(" .tx_packets = %d\n\r", stats.tx_packets);
            printf(" .tx_comp = %d\n\r", stats.tx_comp);
            printf(" .tx_errors = %d\n\r", stats.tx_errors);
            printf(" .collisions = %d\n\r", stats.collisions);
            printf(" .tx_exausts = %d\n\r", stats.tx_exausts);
            printf(" .tx_underruns = %d\n\r", stats.tx_underruns);
            printf(" .rx_packets = %d\n\r", stats.rx_packets);
            printf(" .rx_eof = %d\n\r", stats.rx_eof);
            printf(" .rx_ovrs = %d\n\r", stats.rx_ovrs);
            printf(" .rx_bnas = %d\n\r", stats.rx_bnas);
        }

        // Run periodic tasks
        timers_update();

        // Run polling tasks
        emacif_poll(netif);

      #if LWIP_DHCP
        // Dump DHCP addresses
        if (netif->dhcp) {

            u8_t tmp_state = netif->dhcp->state;
            if (tmp_state != dhcp_state) {

                //printf("DHCP: %d -> %d\n\r",
                //    dhcp_state, tmp_state);

                if (tmp_state == DHCP_BOUND &&
                    dhcp_state != DHCP_RENEWING) {

                    u8_t * pAddr;
                    printf("\n\r");
                    printf("=== DHCP Configurations ===\n\r");
                    pAddr = (u8_t*)&netif->ip_addr;
                    printf("- IP   : %d.%d.%d.%d\n\r",
                        pAddr[0], pAddr[1], pAddr[2], pAddr[3]);
                    pAddr = (u8_t*)&netif->netmask;
                    printf("- Mask : %d.%d.%d.%d\n\r",
                        pAddr[0], pAddr[1], pAddr[2], pAddr[3]);
                    pAddr = (u8_t*)&netif->gw;
                    printf("- GW   : %d.%d.%d.%d\n\r",
                        pAddr[0], pAddr[1], pAddr[2], pAddr[3]);
                    printf("===========================\n\r\n");
                }
                dhcp_state = tmp_state;
            }
        }
      #endif
    }
    return 0;
}