Example #1
0
/************************************************************************
* NAME: fapp_init
*
* DESCRIPTION: FNET Application initialization.
************************************************************************/
void fapp_init(void)
{
    static fnet_uint8_t         stack_heap[FNET_CFG_HEAP_SIZE];
    struct fnet_init_params     init_params;
    struct fnet_shell_params    shell_params;

    /* Input parameters for FNET stack initialization */
    init_params.netheap_ptr = stack_heap;
    init_params.netheap_size = FNET_CFG_HEAP_SIZE;

    
    /* Add event handler on duplicated IP address */
#if FNET_CFG_IP4    
    fnet_netif_dupip_handler_init (fapp_dup_ip_handler);
#endif    
            
    /* Init FNET stack */
    if(fnet_init(&init_params) != FNET_ERR)
    {
    #if FAPP_CFG_PARAMS_READ_FLASH
        /* During bootup, the most recently stored customer configuration data will be read and used to configure the interfaces.*/
        if(fapp_params_from_flash() == FNET_OK)
        {
            fnet_printf(FAPP_PARAMS_LOAD_STR);
        }
    #endif

        if(fnet_netif_get_default() == FNET_NULL)
        {
            fnet_printf(FAPP_NET_ERR);
        }
            
    #if (FAPP_CFG_EXP_CMD && FNET_CFG_FS) || (FAPP_CFG_HTTP_CMD && FNET_CFG_HTTP)   
        fapp_fs_mount(); /* Init FS and mount FS Image. */
    #endif   
            
        /* Init main shell. */
        shell_params.shell = &fapp_shell;
        shell_params.cmd_line_buffer = fapp_cmd_line_buffer;
        shell_params.cmd_line_buffer_size = sizeof(fapp_cmd_line_buffer);
        shell_params.stream = FNET_SERIAL_STREAM_DEFAULT;
        shell_params.echo = FNET_TRUE;
            
        if((fapp_shell_desc = fnet_shell_init(&shell_params)) != FNET_ERR)
        {
            fapp_boot(fapp_shell_desc);
        }
        else
        {
            fnet_printf(FAPP_INIT_ERR, "Shell");
        }
    }
    else
    {
        fnet_printf(FAPP_INIT_ERR, "FNET");
    }

}
void debug_pending_interrupts() {
    fnet_isr_entry_t    *isr_temp;

    isr_temp = fnet_isr_table;
    fnet_printf("Interrupt profile: -------------------\n");
    fnet_printf("Id | Serviced | Pended \n");
    while(isr_temp != 0) {
        fnet_printf("%d | %d | %d\n",isr_temp->vector_number,isr_temp->num_of_times_serviced,isr_temp->num_of_times_pended);
        isr_temp = isr_temp->next;
    }
}
Example #3
0
int wait_dhcp(int tries) {
	fnet_netif_desc_t netif;
	struct fnet_dhcp_params dhcp_params;
	struct callback_params_t callback_params;
	
	/* Get Default netif */
	netif =  fnet_netif_get_default();
	if (!netif) {
		return -1;
	}
	
	/* Clear params struct */
	fnet_memset_zero(&dhcp_params, sizeof(struct fnet_dhcp_params));
	
	/* Turn on link-local support */
	dhcp_params.retries_until_link_local = tries;
	
	/* Initialise DHCP client */
	if(fnet_dhcp_init(netif, &dhcp_params) == FNET_ERR) {
		return -1;
	}
	
	/* Setup param for callbacks */
	callback_params.max_tries = tries + 1; /* Add one so we get link local before timeout */
	callback_params.cur_try = 1;
	callback_params.state = WAITING;
	
	
	/* Print message */
	fnet_printf("\nDHCP: Waiting for server \n");
	
	/* Register call backs */
	fnet_dhcp_handler_updated_set(handler_updated, &callback_params);
	fnet_dhcp_handler_discover_set(handler_discover, &callback_params);
	
	/* Poll background services until address found or n_tries exceeded */
	while (callback_params.state == WAITING) {
		fnet_poll_services();
	}
	
	/* Now evaluate response */
	if (callback_params.state == SUCCESS) {
		fnet_printf("DHCP: Success! \n");
		return 0;  /* Success */
	}
	else {
		fnet_printf("DHCP: Failed! Using link local address \n");
		/* Keep DHCP server running in background */
		return -1; /* Failure */
	}
}
static void fnet_arp_trace(char *str, fnet_arp_header_t *arp_hdr)
{
    char mac_str[FNET_MAC_ADDR_STR_SIZE];
    char ip_str[FNET_IP4_ADDR_STR_SIZE];

    fnet_printf(FNET_SERIAL_ESC_FG_GREEN"%s", str); /* Print app-specific header.*/
    fnet_println("[ARP header]"FNET_SERIAL_ESC_FG_BLACK);
    fnet_println("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+");                    
    fnet_println("|(HWType)                0x%04x |(PrType)                0x%04x |", 
                    fnet_ntohs(arp_hdr->hard_type),
                    fnet_ntohs(arp_hdr->prot_type));
    fnet_println("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+");
    fnet_println("|(HWSize)  0x%02x |(PrSize)  0x%02x |(Opcode)                 %5u |",
                    arp_hdr->hard_size,
                    arp_hdr->prot_size,
                    fnet_ntohs(arp_hdr->op));
    fnet_println("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+/\\/\\/\\/-+");
    fnet_mac_to_str(arp_hdr->sender_hard_addr, mac_str);
    fnet_println("|(SenderHWAddr)                                        "FNET_SERIAL_ESC_FG_BLUE"%17s"FNET_SERIAL_ESC_FG_BLACK" |", mac_str);
    fnet_println("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+/\\/\\/\\/-+");
    fnet_println("|(SenderPrAddr)                                 "FNET_SERIAL_ESC_FG_BLUE"%15s"FNET_SERIAL_ESC_FG_BLACK" |",
                    fnet_inet_ntoa(*(struct in_addr *)(&arp_hdr->sender_prot_addr), ip_str)); 
    fnet_println("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+/\\/\\/\\/-+");
    fnet_mac_to_str(arp_hdr->target_hard_addr, mac_str);
    fnet_println("|(TargetHWAddr)                                        "FNET_SERIAL_ESC_FG_BLUE"%17s"FNET_SERIAL_ESC_FG_BLACK" |", mac_str);
    fnet_println("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+/\\/\\/\\/-+");
    fnet_println("|(TargetPrAddr)                                 "FNET_SERIAL_ESC_FG_BLUE"%15s"FNET_SERIAL_ESC_FG_BLACK" |",
                    fnet_inet_ntoa(*(struct in_addr *)(&arp_hdr->targer_prot_addr), ip_str));  
    fnet_println("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+");  
}
Example #5
0
/************************************************************************
* NAME: fapp_fs_mount
*
* DESCRIPTION: Mount FS image.
************************************************************************/
void fapp_fs_mount(void)
{
    if( fnet_fs_init( ) == FNET_OK)
    {
        /* Register the FNET ROM FS. */
        fnet_fs_rom_register( );
 
        /* Mount the FNET ROM FS image. */
        if( fnet_fs_mount( FNET_FS_ROM_NAME, FAPP_FS_MOUNT_NAME, &fnet_fs_image ) == FNET_ERR )
            fnet_printf(FAPP_FS_FSMOUNT_ERR);
    }
    else
    {
        fnet_printf(FAPP_FS_FSINIT_ERR);
    } 
}
Example #6
0
void disable_irq (int irq)
{
    int div;
    
    /* Make sure that the IRQ is an allowable number. Right now up to 91 is 
     * used.
     */
    if (irq > 91)
        fnet_printf("\nERR! Invalid IRQ value passed to disable irq function!\n");
    
    /* Determine which of the NVICICERs corresponds to the irq */
    div = irq/32;
    
    switch (div)
    {
    	case 0x0:
               NVICICER0 |= 1 << (irq%32);
              break;
    	case 0x1:
              NVICICER1 |= 1 << (irq%32);
              break;
    	case 0x2:
              NVICICER2 |= 1 << (irq%32);
              break;
    }              
}
Example #7
0
void set_irq_priority (int irq, int prio)
{
    /*irq priority pointer*/
    uint8 *prio_reg;
    
    /* Make sure that the IRQ is an allowable number. Right now up to 91 is 
     * used.
     */
    if (irq > 91)
        fnet_printf("\nERR! Invalid IRQ value passed to priority irq function!\n");

    if (prio > 15)
        fnet_printf("\nERR! Invalid priority value passed to priority irq function!\n");
    
    /* Determine which of the NVICIPx corresponds to the irq */
    prio_reg = (uint8 *)(((uint32)&NVICIP0) + irq);
    /* Assign priority to IRQ */
    *prio_reg = ( (prio&0xF) << (8 - ARM_INTERRUPT_LEVEL_BITS) );             
}
/************************************************************************
* NAME: fnet_eth_change_addr_notify
*
* DESCRIPTION:  This function is called on IP address change.
*               It issues a gratuitous ARP request.
*************************************************************************/
void fnet_eth_change_addr_notify(fnet_netif_t *netif)
{
#if FNET_CFG_IP4
fnet_printf("ETH - Change IP4 to: %x\n", netif->ip4_addr.address);

    if(netif->ip4_addr.address)
    	fnet_arp_request(netif, netif->ip4_addr.address); /* Gratuitous ARP request.*/
#else
    FNET_COMP_UNUSED_ARG(netif);
#endif /* FNET_CFG_IP4 */   
}
Example #9
0
static void fapp_dup_ip_handler( fnet_netif_desc_t netif )
{
    fnet_char_t     name[FNET_NETIF_NAMELEN];
    fnet_char_t     ip_str[FNET_IP4_ADDR_STR_SIZE];
    fnet_ip4_addr_t addr;
    
    fnet_netif_get_name( netif, name, sizeof(name) );
    addr = fnet_netif_get_ip4_addr( netif );
    fnet_inet_ntoa(*(struct in_addr *)( &addr), ip_str);

    fnet_printf(FAPP_DUP_IP_WARN, name, ip_str);
}
Example #10
0
void fnet_icmp_trace(fnet_uint8_t *str, fnet_icmp_header_t *icmp_hdr)
{

    fnet_printf(FNET_SERIAL_ESC_FG_GREEN"%s", str); /* Print app-specific header.*/
    fnet_println("[ICMP header]"FNET_SERIAL_ESC_FG_BLACK);
    fnet_println("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+");
    fnet_println("|(Type)     "FNET_SERIAL_ESC_FG_BLUE"%3u"FNET_SERIAL_ESC_FG_BLACK" |(Code)     %3u |(Cheksum)               0x%04x |",
                    icmp_hdr->type,
                    icmp_hdr->code,
                    fnet_ntohs(icmp_hdr->checksum));
    fnet_println("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+");
    
}
Example #11
0
void fnet_eth_trace(fnet_uint8_t *str, fnet_eth_header_t *eth_hdr)
{
    fnet_uint8_t mac_str[FNET_MAC_ADDR_STR_SIZE];

    fnet_printf(FNET_SERIAL_ESC_FG_GREEN"%s", str); /* Print app-specific header.*/
    fnet_println("[ETH header]"FNET_SERIAL_ESC_FG_BLACK);
    fnet_println("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+/\\/\\/\\/-+");
    fnet_println("|(Dest)                                                "FNET_SERIAL_ESC_FG_BLUE"%17s"FNET_SERIAL_ESC_FG_BLACK" |", fnet_mac_to_str(eth_hdr->destination_addr, mac_str));
    fnet_println("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+/\\/\\/\\/-+");
    fnet_println("|(Src)                                                 "FNET_SERIAL_ESC_FG_BLUE"%17s"FNET_SERIAL_ESC_FG_BLACK" |", fnet_mac_to_str(eth_hdr->source_addr, mac_str));
    fnet_println("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+/\\/\\/\\/-+");
    fnet_println("|(Type)                  0x%04x |", fnet_ntohs(eth_hdr->type));
    fnet_println("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+");
}
Example #12
0
static void fnet_udp_trace(fnet_uint8_t *str, fnet_udp_header_t *udp_hdr)
{

    fnet_printf(FNET_SERIAL_ESC_FG_GREEN"%s", str); /* Print app-specific header.*/
    fnet_println("[UDP header]"FNET_SERIAL_ESC_FG_BLACK);
    fnet_println("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+");
    fnet_println("|(SrcPort)                  "FNET_SERIAL_ESC_FG_BLUE"%3u"FNET_SERIAL_ESC_FG_BLACK" |(DestPort)                 "FNET_SERIAL_ESC_FG_BLUE"%3u"FNET_SERIAL_ESC_FG_BLACK" |",
                    fnet_ntohs(udp_hdr->source_port),
                    fnet_ntohs(udp_hdr->destination_port));
    fnet_println("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+");
    fnet_println("|(Lenghth)                  "FNET_SERIAL_ESC_FG_BLUE"%3u"FNET_SERIAL_ESC_FG_BLACK" |(Checksum)              0x%04X |",
                    fnet_ntohs(udp_hdr->length),
                    fnet_ntohs(udp_hdr->checksum));
    fnet_println("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+");    
}
Example #13
0
/* Callback for discover message sent (in progress) */
static void handler_discover(fnet_netif_desc_t netif, void *param) {
	struct callback_params_t *callback_param;
	callback_param = (struct callback_params_t *)param;
	
	/* Print Status */
	fnet_printf("DHCP: attempt %d/%d \n",callback_param->cur_try, callback_param->max_tries);
	
	/* Check if number of tries exceeded */
	if (callback_param->max_tries == callback_param->cur_try) {
			callback_param->state = TIMEOUT;
	}
	else { /* Increment number of tries */
			callback_param->cur_try += 1;
	}
}
Example #14
0
static void fapp_go ( fnet_shell_desc_t desc, fnet_uint32_t address )
{
    if((*(fnet_uint32_t*)address == 0) || (*(fnet_uint32_t*)address == 0xffffffffu))
    {
        fnet_printf("\nThere is no code on user application startup vector.\n");
    }
    else
    {    
        
      fnet_shell_println(desc, FAPP_GO_STR, address);
   
      fnet_release(); /* Release the FNET stack.*/
     
      (( void(*)(void) )FNET_CPU_ADDR_TO_INSTRUCTION(address))(); /* Jump. */
    }
}
static void fnet_igmp_trace(char *str, fnet_igmp_header_t *igmp_hdr)
{
    char ip_str[FNET_IP4_ADDR_STR_SIZE];

    fnet_printf(FNET_SERIAL_ESC_FG_GREEN"%s", str); /* Print app-specific header.*/
    fnet_println("[IGMP header]"FNET_SERIAL_ESC_FG_BLACK);
    fnet_println("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+");
    fnet_println("|(Type)    "FNET_SERIAL_ESC_FG_BLUE"0x%2x"FNET_SERIAL_ESC_FG_BLACK" |(Res Time) %3u |(Cheksum)               0x%04x |",
                    igmp_hdr->type,
                    igmp_hdr->max_resp_time,
                    fnet_ntohs(igmp_hdr->checksum));
    fnet_println("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+");                    
    fnet_println("|(Group Addr)                                   "FNET_SERIAL_ESC_FG_BLUE"%15s"FNET_SERIAL_ESC_FG_BLACK" |",
                    fnet_inet_ntoa(*(struct in_addr *)(&igmp_hdr->group_addr), ip_str));                        
    fnet_println("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+");
    
}
Example #16
0
File: rcm.c Project: 8bitgeek/fnet
/* OutSRS routine - checks the value in the SRS registers and sends
 * messages to the terminal announcing the status at the start of the 
 * code.
 */
void outSRS(void){                         //[outSRS]

  
 	if (RCM_SRS1 & RCM_SRS1_TAMPER_MASK)
 		fnet_printf("Tamper Detect Reset\n");
	if (RCM_SRS1 & RCM_SRS1_SACKERR_MASK)
		fnet_printf("Stop Mode Acknowledge Error Reset\n");
	if (RCM_SRS1 & RCM_SRS1_EZPT_MASK)
		fnet_printf("EzPort Reset\n");
	if (RCM_SRS1 & RCM_SRS1_MDM_AP_MASK)
		fnet_printf("MDM-AP Reset\n");
	if (RCM_SRS1 & RCM_SRS1_SW_MASK)
		fnet_printf("Software Reset\n");
	if (RCM_SRS1 & RCM_SRS1_LOCKUP_MASK)
		fnet_printf("Core Lockup Event Reset\n");
	if (RCM_SRS1 & RCM_SRS1_JTAG_MASK)
		fnet_printf("JTAG Reset\n");
	
	if (RCM_SRS0 & RCM_SRS0_POR_MASK)
		fnet_printf("Power-on Reset\n");
	if (RCM_SRS0 & RCM_SRS0_PIN_MASK)
		fnet_printf("External Pin Reset\n");
	if (RCM_SRS0 & RCM_SRS0_WDOG_MASK)
		fnet_printf("Watchdog(COP) Reset\n");
	if (RCM_SRS0 & RCM_SRS0_LOC_MASK)
		fnet_printf("Loss of Clock Reset\n");
	if (RCM_SRS0 & RCM_SRS0_LVD_MASK)
		fnet_printf("Low-voltage Detect Reset\n");
	if (RCM_SRS0 & RCM_SRS0_WAKEUP_MASK)
        {
		fnet_printf("[outSRS]Wakeup bit set from low power mode exit\n");
		fnet_printf("[outSRS]SMC_PMPROT   = %#02X \r\n", (SMC_PMPROT))  ;
		fnet_printf("[outSRS]SMC_PMCTRL   = %#02X \r\n", (SMC_PMCTRL))  ;
		fnet_printf("[outSRS]SMC_VLLSCTRL   = %#02X \r\n", (SMC_VLLSCTRL))  ;
		fnet_printf("[outSRS]SMC_PMSTAT   = %#02X \r\n", (SMC_PMSTAT))  ;

          if ((SMC_PMCTRL & SMC_PMCTRL_STOPM_MASK)== 3)
        	  fnet_printf("[outSRS] LLS exit \n") ;
          if (((SMC_PMCTRL & SMC_PMCTRL_STOPM_MASK)== 4) && ((SMC_VLLSCTRL & SMC_VLLSCTRL_VLLSM_MASK)== 1))
        	  fnet_printf("[outSRS] VLLS1 exit \n") ;
          if (((SMC_PMCTRL & SMC_PMCTRL_STOPM_MASK)== 4) && ((SMC_VLLSCTRL & SMC_VLLSCTRL_VLLSM_MASK)== 2))
        	  fnet_printf("[outSRS] VLLS2 exit \n") ;
          if (((SMC_PMCTRL & SMC_PMCTRL_STOPM_MASK)== 4) && ((SMC_VLLSCTRL & SMC_VLLSCTRL_VLLSM_MASK)== 3))
        	  fnet_printf("[outSRS] VLLS3 exit \n") ; 
	}

        if ((RCM_SRS0 == 0) && (RCM_SRS1 == 0)) 
        {
        	fnet_printf("[outSRS]RCM_SRS0 is ZERO   = %#02X \r\n", (RCM_SRS0))  ;
        	fnet_printf("[outSRS]RCM_SRS1 is ZERO   = %#02X \r\n", (RCM_SRS1))  ;	 
        }
  }
/************************************************************************
* NAME: fnet_netif_init
*
* DESCRIPTION: This function installs & inits a network interface.
*************************************************************************/
int fnet_netif_init( fnet_netif_t *netif, unsigned char *hw_addr, unsigned int hw_addr_size )
{
    int result = FNET_OK;
    
    if(netif && netif->api)
    {
        fnet_os_mutex_lock();   
        
        fnet_isr_lock();
        
        netif->next = fnet_netif_list;

        if(netif->next != 0)
            netif->next->prev = netif;

        netif->prev = 0;
        fnet_netif_list = netif;
        
        fnet_netif_assign_scope_id( netif ); /* Asign Scope ID.*/
        
        netif->features = FNET_NETIF_FEATURE_NONE;


        /* Interface HW initialization.*/
        if(netif->api->init && ((result = netif->api->init(netif)) == FNET_OK))
        {
            #if FNET_CFG_IGMP && FNET_CFG_IP4       
                /**************************************************************
                 * RFC1112 7.2
                 *   To support IGMP, every level 2 host must
                 *   join the "all-hosts" group (address 224.0.0.1) on each network
                 *   interface at initialization time and must remain a member for as long
                 *   as the host is active.
                 *
                 * NOTE:(224.0.0.1) membership is never reported by IGMP.
                 **************************************************************/
                 /* Join HW interface. */
                fnet_netif_join_ip4_multicast ( netif, FNET_IP4_ADDR_INIT(224, 0, 0, 1) );
            #endif  /* FNET_CFG_IGMP */
            
            /* Set HW Address.*/    
            fnet_netif_set_hw_addr(netif, hw_addr, hw_addr_size);
                
            /* Interface-Type specific initialisation. */ 
            switch(netif->api->type)
            {
            #if (FNET_CFG_CPU_ETH0 ||FNET_CFG_CPU_ETH1)
                case (FNET_NETIF_TYPE_ETHERNET):
                    result = fnet_eth_init(netif);
                    break;
            #endif /* FNET_CFG_ETH */
                default:
                    break;
            
            }
        }

        fnet_printf("ETH - Initialization: %s, ID:%d\n", netif->name, netif->scope_id);
        fnet_printf("	 - IP: %x, GW: %x\n", netif->ip4_addr.address, netif->ip4_addr.gateway);
        fnet_printf("	 - MASK: %x, DHCP?: %d\n", netif->ip4_addr.netmask, netif->ip4_addr.is_automatic);
        fnet_printf("	 - MTU: %d\n", netif->mtu);
        fnet_printf("	 - MTU: %x\n", netif->if_ptr);



        fnet_isr_unlock();
        
        fnet_os_mutex_unlock();
    }
    
    return result;
}
Example #18
0
int main(void) {
	SOCKET bcast_s, server_s;
	int connected = 0, disconnect = 0;
	
	/* Setup Hardware */
	init_hw();
	
/* Test SPI and PWM */
#if CONFIG_TEST_HW
#if CONFIG_BOARD == CONFIG_BOARD_ADC
	trigger_isr_start(dspi_read);
#endif
#if CONFIG_BOARD == CONFIG_BOARD_PWM
	pwm_start();
#endif
	while(1);
#endif
	
	/* Initialise 7 Seg */
	sevenseg_set(CONFIG_7SEG_DEFAULT,DP_0);
	sevenseg_init();
	
	/* Initialise UART */
	fnet_cpu_serial_init(FNET_CFG_CPU_SERIAL_PORT_DEFAULT, 115200);

	/* Clear some screen */
	fnet_printf("\n\n\n");
	
	/* Print board type and firmware version */
	fnet_printf("Board Type: %s Firmware compiled %s %s \n",
			CONFIG_BOARD_TYPE_STRING, __TIME__, __DATE__);
	
	/* Initialise FNET stack */
	init_fnet();
	
	/* Set MAC address based on K60 UID*/
	set_mac_address();
	
	/* Wait for Ethernet connection */
	fnet_printf("Waiting for Connection \n");
	sevenseg_set(CONFIG_7SEG_DEFAULT,DP_1); /* First dot = waiting for connection */
	while (!check_connected()) {
		fnet_timer_delay(FNET_TIMER_TICK_IN_SEC * 1); /* 1 Sec */
		fnet_printf("."); /* Print some errors */
	}
	
	/* Wait for DHCP server, if it fails use link local */
	wait_dhcp(CONFIG_DHCP_TRIES);  
	
	/* Print current IP address */
	fnet_printf("Current IP Address:");
	print_cur_ip();
	fnet_printf("\n");
	
	sevenseg_set(CONFIG_7SEG_DEFAULT,DP_2); /* Second dot = have IP Address */
	
	/* Start UDP receiver */
	bcast_s = bcast_setup_listener(CONFIG_BCAST_PORT);
	if (bcast_s == -1) {
		fnet_printf("BCAST: Error, could not initialise port \n");
		while (1);
	}
	
	sevenseg_set(CONFIG_7SEG_DEFAULT,DP_3); /* Third dot = waiting for UDP */

	/* MAIN LOOP */
	while (1) {
		/* CONNECT IF NEEDED */
		if(!connected) {
			connected = netprot_connect(bcast_s, &server_s);
			#if CONFIG_BOARD == CONFIG_BOARD_PWM
				if(connected) {
					sevenseg_set("8888",DP_0); /* Change the screen, this will do for now */
				}
			#endif	
			fnet_poll_services(); /* Poll DHCP while unconnected */
		}
		
		/* WHEN CONNECTED */
		if (connected) {
			int sent = 0, err;
			
			/* Check for disconnection */
			if (disconnect) {
				disconnect = 0; /* Reset */
				netprot_goodbye(&server_s);
				/* Flush UDP port to prevent reconnecting to dead server */
				bcast_flush(bcast_s);
				sevenseg_set(CONFIG_7SEG_DEFAULT,DP_3); /* Third dot = waiting for UDP */
				/* Stop what you are doing */
				#if CONFIG_BOARD == CONFIG_BOARD_ADC
					trigger_isr_stop();
					capture_set_empty();
				#endif
				fnet_printf("Server Disconnected \n");
				connected = 0;
				continue;
			}
			
			/* Send data first if we are a capture type board */
			#if CONFIG_CAPTURE_SUPPORT
				sent = netprot_send_capture(server_s);
				if (sent<0) { /* Error - Disconnect */
					disconnect = 1;
					continue;
				}
			#endif
			
			/* Get commands */
			if (sent <= 0) { /* We have sent nothing or error */
				err = netprot_get_commands(server_s);
				if (err) { /* Error - Disconnect */
					disconnect = 1;
					continue;
				}
			}
		}
	}
	/* END OF MAIN LOOP */
	
	/* Should never end up here */
	return 0;
}