📄 ethif_cs8900_jz4740.c
字号:
volatile unsigned short *addr; int tmo,i; unsigned short s; int tries = 0; printf("output\n"); for(i=0;i<200;i++) { udelay(1000); } /* initiate a transmit sequence */ put_reg(PP_TxCMD,0x00C9U); put_reg(PP_TxLength,(p->tot_len < MIN_PACKET_SIZE ? MIN_PACKET_SIZE: p->tot_len)); status = get_reg (PP_BusST); if ((status & TxBidErr)) { printf ("Invalid frame size %d!\n",status); return; } /* not ready for transmission and still within 50 retries? */ while (((get_reg(CS8900_PDATA) & 0x0100U/*Rdy4TxNOW*/) == 0) && (tries++ < 50)) { /* throw away the last committed received frame */ put_reg(PP_RxCFG,(0x0003U|RxOKiE|0x0040U)); } /* Write the contents of the packet */ /* assume even number of bytes */ if (status & 0x0100U /*PP_BusSTAT_TxRDY*/) { sent_bytes=0; for (q = p; q != NULL; q = q->next) { for (i = 0; i < q->len; i += 2) { CS8900_RTDATA= *ptr; ptr++; sent_bytes += 2; } #if (ETHIF_STATS > 0) ((struct ETHIF *)netif->state)->sentbytes += q->len;#endif #if (ETHIF_STATS > 0) ((struct ETHIF *)netif->state)->sentpackets++;#endif } while (sent_bytes < MIN_PACKET_SIZE) { CS8900_RTDATA=0x0000; sent_bytes += 2; } } return ERR_OK;}static void cs8900_e2prom_ready(void){ while(get_reg(PP_SelfST) & SI_BUSY);}/***********************************************************//* read a 16-bit word out of the EEPROM *//***********************************************************/static int cs8900_e2prom_read(unsigned char addr, unsigned short *value){ cs8900_e2prom_ready(); put_reg(PP_EECMD, EEPROM_READ_CMD | addr); cs8900_e2prom_ready(); *value = get_reg(PP_EEData); return 0;}/***********************************************************//* write a 16-bit word into the EEPROM *//***********************************************************/static int cs8900_e2prom_write(unsigned char addr, unsigned short value){ cs8900_e2prom_ready(); put_reg(PP_EECMD, EEPROM_WRITE_EN); cs8900_e2prom_ready(); put_reg(PP_EEData, value); put_reg(PP_EECMD, EEPROM_WRITE_CMD | addr); cs8900_e2prom_ready(); put_reg(PP_EECMD, EEPROM_WRITE_DIS); cs8900_e2prom_ready(); return 0;}/*** Writing an IP packet (to be transmitted) to the ethif of jz4730.** Before writing a frame to the ethif of jz4730, the ARP module is asked to resolve the* Ethernet MAC address. The ARP module might undertake actions to resolve the* address first, and queue this packet for later transmission.** @param netif The lwIP network interface data structure belonging to this device.* @param p pbuf to be transmitted (or the first pbuf of a chained list of pbufs).* @param ipaddr destination IP address.*/err_t ETHIF_output(struct netif *netif, struct pbuf *p, struct ip_addr *ipaddr){ struct cs8900if *cs8900if = netif->state; err_t result; /* resolve the link destination hardware address */ p = etharp_output(netif, ipaddr, p); /* network hardware address obtained? */ if (p != NULL) { /* send out the packet */ result = ETH_output(netif, p); p = NULL; } /* { p == NULL } */ else { /* we cannot tell if the packet was sent, the packet could have been queued */ /* on an ARP entry that was already pending. */ return ERR_OK; } return result;}#if 0static err_t ETHIF_output(struct netif *netif, struct pbuf *p, struct ip_addr *ipaddr){ /* resolve hardware address, then send (or queue) packet */ return etharp_output(netif, ipaddr, p);}#endif/** Read a received packet from the jz4730.** This function should be called when a packet is received by the jz4730* and is fully available to read. It moves the received packet to a pbuf* which is forwarded to the IP network layer or ARP module. It transmits* a resulting ARP reply or queued packet.** @param netif The lwIP network interface to read from.**/static void ETHIF_input(struct netif *netif){ struct ETHIF *ETHIF = netif->state; struct eth_hdr *ethhdr = NULL; struct pbuf *p= NULL,*q = NULL; p = ETH_Input(netif); /* no packet could be read */ if(p == NULL) { /* silently ignore this */ return; } /* points to packet payload, which starts with an Ethernet header */ ethhdr = p->payload;#ifdef Print_Header printf("#ETH packet src addr: %x.%x.%x.%x.%x.%x \n",ethhdr->src.addr[0],ethhdr->src.addr[1],ethhdr->src.addr[2],ethhdr->src.addr[3],ethhdr->src.addr[4],ethhdr->src.addr[5]); printf("#ETH packet dest addr: %x.%x.%x.%x.%x.%x \n",ethhdr->dest.addr[0],ethhdr->dest.addr[1],ethhdr->dest.addr[2],ethhdr->dest.addr[3],ethhdr->dest.addr[4],ethhdr->dest.addr[5]); printf("#ETH packet type: %x \n",htons(ethhdr->type));#endif struct etharp_hdr *arphdr = p->payload; q = NULL; switch(htons(ethhdr->type)) { case ETHTYPE_IP: /* IP packet? */ etharp_ip_input(netif, p); /* update ARP table, obtain first queued packet */ pbuf_header(p, -14); /* skip Ethernet header */ netif->input(p, netif); /* pass to network layer */ break; case ETHTYPE_ARP: /* ARP packet? */#ifdef Print_Header printf("#ARP packet proto: %x. hwtype %x \n",htons(arphdr->proto),arphdr->hwtype); printf("#ARP packet src ip: %lu. %lu \n",arphdr->sipaddr.addrw[0],arphdr->sipaddr.addrw[1]); printf("#ARP packet dest ip: %lu. %lu \n",arphdr->dipaddr.addrw[0],arphdr->dipaddr.addrw[1]); printf("#ARP packet src hwaddr: %x.%x.%x.%x.%x.%x \n",arphdr->shwaddr.addr[0],arphdr->shwaddr.addr[1],arphdr->shwaddr.addr[2],arphdr->shwaddr.addr[3],arphdr->shwaddr.addr[4],arphdr->shwaddr.addr[5]); printf("#ARP packet dest hwaddr: %x.%x.%x.%x.%x.%x \n",arphdr->dhwaddr.addr[0],arphdr->dhwaddr.addr[1],arphdr->dhwaddr.addr[2],arphdr->dhwaddr.addr[3],arphdr->dhwaddr.addr[4],arphdr->dhwaddr.addr[5]);#endif etharp_arp_input(netif, (struct eth_addr *)&netif->hwaddr, p);// etharp_arp_input(netif, ((struct ETHIF *)netif->state)->ethaddr, p); break; default: /* unsupported Ethernet packet type */ pbuf_free(p); p=NULL; break; } /* send out the ARP reply or ARP queued packet */ if (q != NULL) { /* q pbuf has been succesfully sent? */ if (ETH_output(netif, q) == ERR_OK) { pbuf_free(q); q = NULL; } else { /* TODO: re-queue packet in the ARP cache here (?) */ pbuf_free(q); q = NULL; } }}static voidarp_timer(void *arg){ etharp_tmr(); sys_timeout(ARP_TMR_INTERVAL, arp_timer, NULL);}static void ETH_Handler(unsigned int arg){ u16 status; __gpio_mask_irq(IRQ_ETH); while(status = get_reg(PP_ISQ)) { switch (RegNum (status)) { case RxEvent: ETHIF_input((struct netif *)arg); break; case TxEvent: printf("TxEvent\n"); break; case BufEvent: printf("BufEvent\n"); break; case TxCOL: printf("TxCOL\n"); break; case RxMISS: printf("RxMISS\n"); break; default: break; } } __gpio_unmask_irq(IRQ_ETH);}err_tETHIF_Init(struct netif *netif){ struct ETHIF *ETHIF; u32 reg;#define RD_N_PIN (32 + 29)#define WE_N_PIN (32 + 30)#define CS4_PIN (32 + 28) __gpio_as_func0(CS4_PIN); __gpio_as_func0(RD_N_PIN); __gpio_as_func0(WE_N_PIN);// REG_EMC_SMCR4 |= (1 << 6); //16bit reg = REG_EMC_SMCR4; reg = (reg & (~EMC_SMCR_BW_MASK)) | EMC_SMCR_BW_16BIT; REG_EMC_SMCR4 = reg; JZ_StartTimer(); ETHIF = mem_malloc(sizeof(struct ETHIF)); if (ETHIF == NULL) return ERR_MEM; netif->name[0] = IFNAME0; netif->name[1] = IFNAME1; netif->output = ETHIF_output; netif->linkoutput = ETH_output; // initialize ETH specific interface structure netif->state = ETHIF; /* maximum transfer unit */ netif->mtu = 1500; /* broadcast capability */ netif->flags = NETIF_FLAG_BROADCAST; /* hardware address length */ netif->hwaddr_len = 6;// int i;// for(i=0; i < netif->hwaddr_len; i++)// netif->hwaddr[i]=0x68;// /* mike not needed*/ ETHIF->ethaddr = (struct eth_addr *)&(netif->hwaddr[0]); ETHIF->needs_service = 0; ETHIF->use_polling = 0; #if (ETHIF_STATS > 0) // number of interrupts (vector calls) ETHIF->interrupts = 0; ETHIF->missed = 0; ETHIF->dropped = 0; ETHIF->sentpackets = 0; ETHIF->sentbytes = 0;#endif /* make up an MAC address. */ netif->hwaddr[0] = (u8_t)ETHADDR0; netif->hwaddr[1] = (u8_t)ETHADDR1; netif->hwaddr[2] = (u8_t)ETHADDR2; netif->hwaddr[3] = (u8_t)ETHADDR3; netif->hwaddr[4] = (u8_t)ETHADDR4; netif->hwaddr[5] = (u8_t)ETHADDR5; request_irq(IRQ_GPIO_0 + ETH_GPIO, ETH_Handler,netif); __gpio_as_irq_high_level(ETH_GPIO); //irq //__gpio_as_irq_rise_edge(ETH_GPIO); //irq __gpio_disable_pull(ETH_GPIO); //disable pull REG_EMC_SMCR4 |= (1 << 6); //__gpio_ack_irq(ETH_GPIO); //__gpio_mask_irq(ETH_GPIO); low_level_init(netif); sys_timeout(ARP_TMR_INTERVAL, arp_timer, NULL); __gpio_unmask_irq(ETH_GPIO); return ERR_OK;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -