err_t mcf523xfec_init( struct netif *netif ) { err_t res; mcf523xfec_if_t *fecif = mem_malloc( sizeof( mcf523xfec_if_t ) ); if( fecif != NULL ) { /* Global copy used in ISR. */ fecif_g = fecif; fecif->self = ( struct eth_addr * )&netif->hwaddr[0]; fecif->netif = netif; fecif->tx_sem = NULL; fecif->rx_sem = NULL; if( ( fecif->tx_sem = sys_sem_new( 1 ) ) == NULL ) { res = ERR_MEM; } else if( ( fecif->rx_sem = sys_sem_new( 0 ) ) == NULL ) { res = ERR_MEM; } else if( sys_thread_new( mcf523xfec_rx_task, fecif, TASK_PRIORITY ) == NULL ) { res = ERR_MEM; } else { netif->state = fecif; netif->name[0] = 'C'; netif->name[1] = 'F'; netif->hwaddr_len = ETH_ADDR_LEN; netif->mtu = MCF_FEC_MTU; netif->flags = NETIF_FLAG_BROADCAST; netif->output = mcf523xfec_output; netif->linkoutput = mcf523xfec_output_raw; nbuf_init( ); mcf523xfec_get_mac( fecif, fecif->self ); mcf523xfec_reset( fecif ); mcf523xfec_enable( fecif ); etharp_init( ); sys_timeout( ARP_TMR_INTERVAL, arp_timer, NULL ); res = ERR_OK; } if( res != ERR_OK ) { free( fecif ); if( fecif->tx_sem != NULL ) { mem_free( fecif->tx_sem ); } if( fecif->rx_sem != NULL ) { mem_free( fecif->rx_sem ); } } } else { res = ERR_MEM; } return res; }
/** * Starts MAC controller * * @param MAC interface descriptor * @return error code */ err_t MAC_init( struct netif *netif ) { err_t res; #if SPEED_10BASET uint16 mymrdata; //temp variable for MII read data //uint16 reg0; #endif mcf5xxxfec_if_t *fecif = mem_malloc( sizeof( mcf5xxxfec_if_t ) ); if( fecif != NULL ) { /* Global copy used in ISR. */ fecif_g = fecif; fecif->self = ( struct eth_addr * )&netif->hwaddr[0]; fecif->netif = netif; fecif->tx_sem = NULL; fecif->rx_sem = NULL; if( ( fecif->tx_sem = sys_sem_new( 1 ) ) == NULL ) { res = ERR_MEM; } else if( ( fecif->rx_sem = sys_mult_sem_new/*sys_sem_new*/( 0 ) ) == NULL ) { res = ERR_MEM; } else if( sys_thread_new("FEC", MAC_Rx_Task, (void *)fecif, FEC_RX_STACK_SPACE, FEC_TASK_PRIORITY) == NULL ) { res = ERR_MEM; } else { netif->state = fecif; netif->name[0] = 'C'; netif->name[1] = 'F'; netif->hwaddr_len = ETH_ADDR_LEN; netif->mtu = MCF_FEC_MTU; netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP; netif->output = MAC_Send; netif->linkoutput = MAC_output_raw; NBUF_init( ); MAC_GetMacAddress(fecif->self ); MAC_Reset( fecif ); #if PHY_ON_CHIP PHY_init(); #endif #if 0 /*FSL:reset the PHY: quick fix*/ while(!MII_write(FEC_PHY0, PHY_REG_CR, 0x8000)) ; //Force reset mymrdata = 0; do { // read if link is up MII_read(FEC_PHY0, PHY_REG_SR, &mymrdata); }while( !(mymrdata&PHY_R1_LS) ); #endif #if SPEED_10BASET while(!(MII_read(FEC_PHY0, PHY_REG_SR, &mymrdata))) ; // read PHY AN Ad register if ((mymrdata & PHY_R1_LS)==1) { //gotlink =1; goto exit; } else { while(!MII_write(FEC_PHY0, PHY_REG_CR, /*mymrdata|*/0x0200)) ; //Force re-negotiation } exit: asm(nop); #endif FEC_SetRxCallback(MAC_ISR); MAC_Enable( fecif ); etharp_init( ); sys_timeout( ARP_TMR_INTERVAL, arp_timer, NULL ); res = ERR_OK; } if( res != ERR_OK ) { /*FSL:change free(fecif) order; from top to bottom*/ //free( fecif ); if( fecif->tx_sem != NULL ) { mem_free( fecif->tx_sem ); } if( fecif->rx_sem != NULL ) { mem_free( fecif->rx_sem ); } mem_free( fecif ); } }
/******** * MAIN * ********/ void main() { #ifdef SIMULATOR struct ip_addr dump_ipaddr, dump_netmask, dump_gw; #else struct ip_addr eth_ipaddr, eth_netmask, eth_gw; #endif struct ip_addr ipsec_ipaddr, ipsec_netmask, ipsec_gw; unsigned int i; serinit(); /* init serial port */ printf("lwIP - IPsec Dynamic SAD/SPD configuration demo (compiled %s at %s)\n", __DATE__, __TIME__); printf("CVS ID: $Id: main.c,v 1.8 2003/12/12 14:36:33 schec2 Exp $\n\n"); /* initialize lwIP */ etharp_init(); mem_init(); memp_init(); pbuf_init(); netif_init(); ip_init(); udp_init(); tcp_init(); printf("TCP/IP initialized.\n"); #ifdef SIMULATOR /* configure dummy device */ IP4_ADDR(&dump_ipaddr, 192,168,1,3); IP4_ADDR(&dump_netmask, 255,255,255,0); IP4_ADDR(&dump_gw, 192,168,1,1); /* configure IPsec device */ IP4_ADDR(&ipsec_ipaddr, 192,168,1,3); IP4_ADDR(&ipsec_netmask, 255,255,255,0); IP4_ADDR(&ipsec_gw, 192,168,1,1); #endif #ifdef MCB167NET /* configure Ethernet device */ IP4_ADDR(ð_ipaddr, 192,168,1,3); IP4_ADDR(ð_netmask, 255,255,255,0); IP4_ADDR(ð_gw, 192,168,1,1); /* configure IPsec device */ IP4_ADDR(&ipsec_ipaddr, 192,168,1,3); IP4_ADDR(&ipsec_netmask, 255,255,255,0); IP4_ADDR(&ipsec_gw, 192,168,1,1); #endif #ifdef PHYCORE167HSE /* configure Ethernet device */ IP4_ADDR(ð_ipaddr, 192,168,1,4); IP4_ADDR(ð_netmask, 255,255,255,0); IP4_ADDR(ð_gw, 192,168,1,1); /* configure IPsec device */ IP4_ADDR(&ipsec_ipaddr, 192,168,1,4); IP4_ADDR(&ipsec_netmask, 255,255,255,0); IP4_ADDR(&ipsec_gw, 192,168,1,1); #endif /* Initialize the physical device first (so that ethif->next will point * * to ipsecif). The IPsec device will process the packets and pass them * * upper to the IP layer (using ip_input) * * If the simulator is used, dumpdev will replace cs8900. */ #ifdef SIMULATOR printf("Setting up dp0..."); dumpif = netif_add(&dump_ipaddr, &dump_netmask, &dump_gw, NULL, dumpdev_init, ipsecdev_input); printf("OK\n"); printf("Setting up is0..."); ipsecif = netif_add(&ipsec_ipaddr, &ipsec_netmask, &ipsec_gw, NULL, ipsecdev_init, ip_input); printf("OK\n"); #else printf("Setting up et0..."); ethif = netif_add(ð_ipaddr, ð_netmask, ð_gw, NULL, cs8900if_init, ipsecdev_input); printf("OK\n"); printf("Setting up is0..."); ipsecif = netif_add(&ipsec_ipaddr, &ipsec_netmask, &ipsec_gw, NULL, ipsecdev_init, ip_input); printf("OK\n"); #endif /* configure IPsec tunnel */ #ifdef PHYCORE167HSE ipsec_set_tunnel("192.168.1.4", "192.168.1.5"); #else ipsec_set_tunnel("192.168.1.3", "192.168.1.5"); #endif /* set the IPsec interface "is0" as default interface for lwIP */ netif_set_default(ipsecif); /* use ipsec interface by default */ /* start custom applications here */ dumpwebpage_init(); /* start dumpwebpage */ netconfig_init(); /* start dynamic network confiuratio*/ printf("Applications started.\n"); DP2 = 0x00FF; /* configure the status LED on Port2*/ ODP2 = 0x0000; while(1) { P2 = ~P2; /* blink LED */ for (i = 0; i < 50000; i++) { _nop_(); _nop_(); /* delay or use this time for the */ _nop_(); _nop_(); /* application */ _nop_(); _nop_(); if((i % 250) == 0) { /* periodically call to the */ #ifdef SIMULATOR dumpdev_service(dumpif);/* dump device driver (polling mode)*/ #else cs8900if_service(ethif);/* CS8900 driver (polling mode) */ #endif tcp_tmr(); /* poll TCP timer */ } } } }
msg_t LwIpThreadFunc(void* arg) { (void) arg; bool started_phy_read = false; bool link_status = false; rcc::enable_gpio<pins::red, pins::yellow, pins::green, pins::button>(); gpio::configure<pins::red, pins::yellow, pins::green>(gpio::output()); gpio::configure<pins::button>(gpio::input, gpio::pull_up()); netif_init(); etharp_init(); udp_init(); tcpip_init(NULL,NULL); std::copy(mac_addr, mac_addr+6, interface.hwaddr); std::copy(mac_addr, mac_addr+6, cfg.mac_addr.begin()); interface.hwaddr_len = 6; netif_add(&interface, &ip.addr, &netmask.addr, &gateway.addr, NULL, lwip_funcs::ethernetif_init, ::ethernet_input ); netif_set_default(&interface); netif_set_up(&interface); bool dhcp_bound = false; unsigned int history = 0; bool enabled = false; while(true) { auto eventmask = chEvtWaitAnyTimeout( RxAvailableMask | TxCompletedMask, 100 ); if( eventmask & RxAvailableMask ) { rx_walk_descriptors(&interface); } if( eventmask & TxCompletedMask ) { tx_walk_descriptors(); } unsigned int val = pins::button::get(); history = (history << 1) | val; if((history&0xF) == 12 /*~3&0xF*/ ) { if(enabled) { ptp_port.post_thread_command(uptp::SystemPort::ThreadCommands::DisableClock); pins::green::reset(); } else { ptp_port.post_thread_command(uptp::SystemPort::ThreadCommands::EnableClock); pins::green::set(); } enabled = !enabled; } if(started_phy_read) { auto phystats = eth::phy_read_register_finish(); if(!link_status && (phystats & 1)) { start_mac_dma(phystats, interface); netif_set_link_up(&interface); dhcp_start(&interface); ptp_port.start(); link_status = true; } else if(link_status && !(phystats&1)){ //link_status = false; //netif_set_link_down(&interface); } started_phy_read = false; } else { eth::phy_read_register_start(0x10, 1); started_phy_read = true; } if(link_status && interface.dhcp->state == DHCP_STATE_BOUND && !dhcp_bound) { ptp_port.ip_addr_changed(interface.ip_addr); dhcp_bound = true; } } return 0; }
static int sef_cb_init_fresh(__unused int type, __unused sef_init_info_t *info) { int err; unsigned hz; char my_name[16]; int my_priv; err = sys_whoami(&lwip_ep, my_name, sizeof(my_name), &my_priv); if (err != OK) panic("Cannot get own endpoint"); nic_init_all(); inet_read_conf(); /* init lwip library */ stats_init(); sys_init(); mem_init(); memp_init(); pbuf_init(); hz = sys_hz(); arp_ticks = ARP_TMR_INTERVAL / (1000 / hz); tcp_fticks = TCP_FAST_INTERVAL / (1000 / hz); tcp_sticks = TCP_SLOW_INTERVAL / (1000 / hz); etharp_init(); set_timer(&arp_tmr, arp_ticks, arp_watchdog, 0); set_timer(&tcp_ftmr, tcp_fticks, tcp_fwatchdog, 0); set_timer(&tcp_stmr, tcp_sticks, tcp_swatchdog, 0); netif_init(); netif_lo = netif_find("lo0"); /* Read configuration. */ #if 0 nw_conf(); /* Get a random number */ timerand= 1; fd = open(RANDOM_DEV_NAME, O_RDONLY | O_NONBLOCK); if (fd != -1) { err= read(fd, randbits, sizeof(randbits)); if (err == sizeof(randbits)) timerand= 0; else { printf("inet: unable to read random data from %s: %s\n", RANDOM_DEV_NAME, err == -1 ? strerror(errno) : err == 0 ? "EOF" : "not enough data"); } close(fd); } else { printf("inet: unable to open random device %s: %s\n", RANDOM_DEV_NAME, strerror(errno)); } if (timerand) { printf("inet: using current time for random-number seed\n"); err= gettimeofday(&tv, NULL); if (err == -1) { printf("sysutime failed: %s\n", strerror(errno)); exit(1); } memcpy(randbits, &tv, sizeof(tv)); } init_rand256(randbits); #endif /* Subscribe to driver events for network drivers. */ if ((err = ds_subscribe("drv\\.net\\..*", DSF_INITIAL | DSF_OVERWRITE)) != OK) panic(("inet: can't subscribe to driver events")); /* Announce we are up. LWIP announces its presence to VFS just like * any other character driver. */ chardriver_announce(); return(OK); }