//配置网卡硬件,并设置MAC地址 //返回值:0,正常;1,失败; u8 tapdev_init(void) { u32 wait_count; u8 i, res = 0; #ifndef DHCP_ENABLE uip_ipaddr_t ipaddr; #endif res = ENC28J60_Init((u8*)Modbus.mac_addr); //初始化ENC28J60 //把IP地址和MAC地址写入缓存区 for (i = 0; i < 6; i++) { uip_ethaddr.addr[i] = Modbus.mac_addr[i]; } //指示灯状态:0x476 is PHLCON LEDA(绿)=links status, LEDB(红)=receive/transmit //PHLCON:PHY 模块LED 控制寄存器 ENC28J60_PHY_Write(PHLCON, 0x0476); uip_init(); //uIP初始化 if(Modbus.tcp_type == 0) { U8_T temp[4]; uip_ipaddr(ipaddr, Modbus.ip_addr[0], Modbus.ip_addr[1], Modbus.ip_addr[2], Modbus.ip_addr[3]); //设置本地设置IP地址 uip_sethostaddr(ipaddr); uip_ipaddr(ipaddr, Modbus.getway[0], Modbus.getway[1], Modbus.getway[2], Modbus.getway[3]); //设置网关IP地址(其实就是你路由器的IP地址) uip_setdraddr(ipaddr); uip_ipaddr(ipaddr, Modbus.subnet[0], Modbus.subnet[1], Modbus.subnet[2], Modbus.subnet[3]); //设置网络掩码 uip_setnetmask(ipaddr); // flag_dhcp_configured = 2; temp[0] = Modbus.ip_addr[0]; temp[1] = Modbus.ip_addr[1]; temp[2] = Modbus.ip_addr[2]; temp[3] = Modbus.ip_addr[3]; temp[0] |= (255 - Modbus.subnet[0]); temp[1] |= (255 - Modbus.subnet[1]); temp[2] |= (255 - Modbus.subnet[2]); temp[3] |= (255 - Modbus.subnet[3]); uip_ipaddr(uip_hostaddr_submask,temp[0], temp[1],temp[2] ,temp[3]); delay_ms(1); } else // DHCP { // flag_dhcp_configured = 0; dhcpc_init(Modbus.mac_addr, 6); dhcpc_request(); } // printf("res=%u\n\r",res); return res; }
/** * @brief Main program. * @param None * @retval None */ int main(void) { //Init system and pheripherals Platform_Config(); printf("System init complete!\n"); //Init NetDev ENC28J60_Init(hostMac); printf("NetDev init complete!\n"); //Init packet buffer of NIC arp_hdr = (struct net_arp_hdr *) malloc (sizeof(struct net_arp_hdr)); icmp_hdr = (struct net_icmpip_hdr *) malloc (sizeof(struct net_icmpip_hdr)); memset(pktBuffer, 0, sizeof(pktBuffer)); for(int i=0; i<200; i++) { pktBuffer[i] = i; } //Init heart beat LED TIM1_PWM_Config(1000000); TIM1_PWM_SetDuty(1, 0); TIM1_PWM_SetDuty(2, 0); TIM15_General_Config(1000); printf("=== ARP & ICMP Test Program ===\n By Nights 2015-01-31\n"); while(1) { //Check Link { isLinkedNow = ENC28J60_GetLinkStatus(); if(isLinkedBefore == ENC_LINK_DOWN && isLinkedNow == ENC_LINK_UP) printf("NetDev: link up!\n"); else if(isLinkedBefore == ENC_LINK_UP && isLinkedNow == ENC_LINK_DOWN) printf("NetDev: link down!\n"); isLinkedBefore = isLinkedNow; } //ARP & ICMP responding. if(isLinkedNow == ENC_LINK_UP) { pktLen = ENC28J60_PacketReceive(pktBuffer, sizeof(pktBuffer)); if(pktLen) { // // Dump the pkt. // printf("NetDev: received data:"); // for(int i=0; i<pktLen; i++) // { // printf(" %02X", pktBuffer[i]); // } // printf("\n---\n"); // Check if it's an ARP pkt. memcpy(arp_hdr, pktBuffer, sizeof(struct net_arp_hdr)); //arp_hdr = (struct net_arp_hdr *) &pktBuffer[0]; if(arp_hdr->ethhdr.type == HTONS(UIP_ETHTYPE_ARP) && arp_hdr->opcode == HTONS(ARP_REQUEST)) { printf("NetDev: received ARP request from %d.%d.%d.%d\n", (arp_hdr->sipaddr[0] & 0xFF), (arp_hdr->sipaddr[0] >> 8), (arp_hdr->sipaddr[1] & 0xFF), (arp_hdr->sipaddr[0] >> 8)); /* The reply opcode is 2. */ arp_hdr->opcode = HTONS(2); memcpy(arp_hdr->dhwaddr.addr, arp_hdr->shwaddr.addr, 6); memcpy(arp_hdr->shwaddr.addr, hostMac, 6); memcpy(arp_hdr->ethhdr.src.addr, hostMac, 6); memcpy(arp_hdr->ethhdr.dest.addr, arp_hdr->dhwaddr.addr, 6); arp_hdr->dipaddr[0] = arp_hdr->sipaddr[0]; arp_hdr->dipaddr[1] = arp_hdr->sipaddr[1]; arp_hdr->sipaddr[0] = HTONS(hostIpaddr[0]); arp_hdr->sipaddr[1] = HTONS(hostIpaddr[1]); arp_hdr->opcode = HTONS(ARP_REPLY); memcpy(pktBuffer, arp_hdr, sizeof(struct net_arp_hdr)); printf("NetDev: sending ARP reply..."); ENC28J60_PacketSend(pktBuffer, sizeof(struct net_arp_hdr)); printf("done!\n"); } // Check if it's an ICMP pkt. else if(arp_hdr->ethhdr.type == HTONS(UIP_ETHTYPE_IP)) { memcpy(icmp_hdr, pktBuffer, sizeof(struct net_icmpip_hdr)); if(icmp_hdr->proto == UIP_PROTO_ICMP && icmp_hdr->type == ICMP_ECHO) { printf("NetDev: received ICMP request from %d.%d.%d.%d\n", (icmp_hdr->srcipaddr[0] & 0xFF), (icmp_hdr->srcipaddr[0] >> 8), (icmp_hdr->srcipaddr[1] & 0xFF), (icmp_hdr->srcipaddr[0] >> 8)); // Dill with MAC part memcpy(icmp_hdr->ethhdr.dest.addr, icmp_hdr->ethhdr.src.addr, 6); memcpy(icmp_hdr->ethhdr.src.addr, hostMac, 6); // Dill with IP&ICMP part memcpy(icmp_hdr->destipaddr, icmp_hdr->srcipaddr, sizeof(icmp_hdr->srcipaddr)); icmp_hdr->srcipaddr[0] = HTONS(hostIpaddr[0]); icmp_hdr->srcipaddr[1] = HTONS(hostIpaddr[1]); icmp_hdr->type = ICMP_ECHO_REPLY; if(icmp_hdr->icmpchksum >= HTONS(0xffff - (ICMP_ECHO << 8))) { icmp_hdr->icmpchksum += HTONS(ICMP_ECHO << 8) + 1; } else { icmp_hdr->icmpchksum += HTONS(ICMP_ECHO << 8); } memcpy(pktBuffer, icmp_hdr, sizeof(struct net_icmpip_hdr)); printf("NetDev: sending ICMP reply..."); ENC28J60_PacketSend(pktBuffer, sizeof(struct net_icmpip_hdr) + UIP_LLH_LEN); printf("done!\n"); }