00001 #include <string.h> 00002 #include "eth.h" 00003 #include "../timer/timer.h" 00004 #include "../lpc_config.h" 00005 #include "../uart/uart.h" 00006 #include "../mac/emac.h" 00007 #include "../lcd/lcd.h" 00008 #include "../cli/cli.h" 00009 #include "../cli/debug.h" 00010 00011 /* uip includes. */ 00012 #include "uip-conf.h" 00013 #include "../mult/mult_uip.h" 00014 #include <uip.h> 00015 #include <uip_arp.h> 00016 #include <timer.h> 00017 #include "clock-arch.h" 00018 00024 static void ENET_Send(void); 00025 00029 void clock_init( void ); 00030 clock_time_t clock_time( void ); 00031 00035 void clock_init(void) 00036 { 00037 T0Init(1); 00038 } 00039 00044 void clock_start(void) 00045 { 00046 T0Start(); 00047 } 00048 00052 void clock_reset(void) 00053 { 00054 T0Init(1); 00055 T0Start(); 00056 } 00057 00061 clock_time_t clock_time( void ) 00062 { 00063 return T0Get(); 00064 } 00065 00066 extern void ( EMAC_ISR_Handler )( void ); 00067 00068 00069 struct timer periodic_timer, arp_timer; 00070 00082 void ethernet_init() 00083 { 00084 int i; 00085 uip_ipaddr_t xIPAddr; 00086 char buff[40]; 00087 struct uip_eth_addr xAddr; 00088 xAddr.addr[ 0 ] = uipMAC_ADDR0; 00089 xAddr.addr[ 1 ] = uipMAC_ADDR1; 00090 xAddr.addr[ 2 ] = uipMAC_ADDR2; 00091 xAddr.addr[ 3 ] = uipMAC_ADDR3; 00092 xAddr.addr[ 4 ] = uipMAC_ADDR4; 00093 xAddr.addr[ 5 ] = uipMAC_ADDR5; 00094 00095 clock_init(); 00096 clock_start(); 00097 00098 /* Initialize the uIP stack. */ 00099 timer_set( &periodic_timer, 1000 ); 00100 timer_set( &arp_timer, 10000 ); 00101 uip_init(); 00102 00103 /* The next two lines define an IP adress */ 00104 set_eth_addr(); 00105 00106 dhcpc_init(&xAddr, 6); //DHCP Client not working yet 00107 services_init(); 00108 00109 /* Initialise the MAC. */ 00110 while( Init_EMAC() != PASS ) 00111 { 00112 delayMs( uipINIT_WAIT ); 00113 } 00114 disableIRQ(); 00115 MAC_INTENABLE = INT_RX_DONE | INT_TX_DONE | INT_TX_UNDERRUN | INT_RX_OVERRUN | INT_TX_FIN | INT_RX_FIN; 00116 VICIntSelect &= ~0x00200000; 00117 VICIntEnable |= 0x00200000; 00118 VICVectAddr21 = ( int ) EMAC_ISR_Handler; 00119 uip_setethaddr( xAddr ); // Set hardware address (MAC) on uIP stack 00120 enableIRQ(); 00121 } 00130 void ethernet_task() 00131 { 00132 int i; 00133 00134 /* Is there received data ready to be processed? */ 00135 uip_len = uiGetEMACRxData( uip_buf ); 00136 00137 if( uip_len > 0 ) 00138 { 00139 /* Standard uIP loop taken from the uIP manual. */ 00140 if( xHeader->type == htons( UIP_ETHTYPE_IP ) ) 00141 { 00142 //uart_putc('.'); 00143 uip_arp_ipin(); 00144 uip_input(); 00145 00146 /* If the above function invocation resulted in data that 00147 should be sent out on the network, the global variable 00148 uip_len is set to a value > 0. */ 00149 if( uip_len > 0 ) 00150 { 00151 //uart_putc('-'); 00152 uip_arp_out(); 00153 ENET_Send(); 00154 } 00155 } 00156 else if( xHeader->type == htons( UIP_ETHTYPE_ARP ) ) 00157 { 00158 //uart_putc('\\'); 00159 uip_arp_arpin(); 00160 00161 /* If the above function invocation resulted in data that 00162 should be sent out on the network, the global variable 00163 uip_len is set to a value > 0. */ 00164 if( uip_len > 0 ) 00165 { 00166 //uart_putc('/'); 00167 ENET_Send(); 00168 } 00169 } 00170 } 00171 else 00172 { 00173 if( timer_expired( &periodic_timer ) ) 00174 { 00175 //uart_putc('|'); 00176 timer_reset( &periodic_timer ); 00177 for( i = 0; i < UIP_CONNS; i++ ) 00178 { 00179 uip_periodic( i ); 00180 00181 /* If the above function invocation resulted in data that 00182 should be sent out on the network, the global variable 00183 uip_len is set to a value > 0. */ 00184 if( uip_len > 0 ) 00185 { 00186 //uart_putc('@'); 00187 uip_arp_out(); 00188 ENET_Send(); 00189 } 00190 } 00191 #if UIP_UDP 00192 for(i = 0; i < UIP_UDP_CONNS; i++) { 00193 uip_udp_periodic(i); 00194 /* If the above function invocation resulted in data that 00195 should be sent out on the network, the global variable 00196 uip_len is set to a value > 0. */ 00197 if(uip_len > 0) { 00198 //uart_putc('%'); 00199 uip_arp_out(); 00200 ENET_Send(); 00201 } 00202 } 00203 #endif /* UIP_UDP */ 00204 00205 /* Call the ARP timer function every 10 seconds. */ 00206 if( timer_expired( &arp_timer ) ) 00207 { 00208 //uart_putc('#'); 00209 timer_reset( &arp_timer ); 00210 uip_arp_timer(); 00211 } 00212 } 00213 } 00214 } 00215 00222 static void ENET_Send(void) 00223 { 00224 RequestSend(); 00225 00226 /* Copy the header into the Tx buffer. */ 00227 CopyToFrame_EMAC( uip_buf, uipTOTAL_FRAME_HEADER_SIZE ); 00228 if( uip_len > uipTOTAL_FRAME_HEADER_SIZE ) 00229 { 00230 CopyToFrame_EMAC( uip_appdata, ( uip_len - uipTOTAL_FRAME_HEADER_SIZE ) ); 00231 } 00232 00233 DoSend_EMAC( uip_len ); 00234 } 00235 00239 void set_eth_addr(void){ 00240 set_IpDefault(); 00241 set_GatewayDefault(); 00242 set_MaskDefault(); 00243 }