📄 ethif_cs8900_jz4740_poll.c
字号:
// printf("111111111222222221--%08x-%d-%d\n",addr,status,rxlen); if (rxlen > 0) { p = pbuf_alloc(PBUF_RAW, rxlen, PBUF_POOL); //length - 4 { if (p != 0) { for (q = p; q != 0; q = q->next) { // printf("cs8900_input: pbuf @%p tot_len %u len %u\n", q, q->tot_len, q->len); ptr = q->payload; // TODO: CHECK: what if q->len is odd? we don't use the last byte? for (i = 0; i < (q->len + 1)/ 2; i++) { *ptr = CS8900_RTDATA; ptr++; } } } } }// NetReceive (NetRxPackets[0], rxlen); return p;}/* Send a data block via Ethernet. *///static int jz_eth_send (struct eth_device* dev,volatile void *packet, int length)static err_t ETH_output(struct netif *netif, struct pbuf *p){ struct pbuf *q; u16 *ptr =(u16 *)p->payload;//NetTxPacket; u16_t length = p->tot_len; unsigned long sent_bytes; volatile unsigned short *addr; int tmo,i; unsigned short s; retry: /* initiate a transmit sequence */ CS8900_TxCMD = PP_TxCmd_TxStart_Full; CS8900_TxLEN = p->tot_len < MIN_PACKET_SIZE ? MIN_PACKET_SIZE: p->tot_len; /* Test to see if the chip has allocated memory for the packet */ if ((get_reg (PP_BusSTAT) & PP_BusSTAT_TxRDY) == 0) { /* Oops... this should not happen! */ for (tmo = get_timer (0) + 5 * CFG_HZ; get_timer (0) < tmo;) /*NOP*/; eth_reset (); eth_reginit (); goto retry; } /* Write the contents of the packet */ /* assume even number of bytes */ 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 1#if (ETHIF_STATS > 0) ((struct ETHIF *)netif->state)->sentbytes += q->len;#endif #if (ETHIF_STATS > 0) ((struct ETHIF *)netif->state)->sentpackets++;#endif#endif } while (sent_bytes < MIN_PACKET_SIZE) { CS8900_RTDATA=0x0000; sent_bytes += 2; } /* wait for transfer to succeed */ tmo = get_timer (0) + 5 * CFG_HZ; while ((s = get_reg (PP_TER) & ~0x1F) == 0) { if (get_timer (0) >= tmo) break; } /* nothing */ ; if ((s & (PP_TER_CRS | PP_TER_TxOK)) != PP_TER_TxOK) { } 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.*/static 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);}/** 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; while(1) { 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; 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 ETHIF *)netif->state)->ethaddr, p); break; default: /* unsupported Ethernet packet type */ pbuf_free(p); break; } } //while(1)}static voidarp_timer(void *arg){ etharp_tmr(); sys_timeout(ARP_TMR_INTERVAL, arp_timer, NULL);}void ETH_Handler(unsigned int arg){ __intc_mask_irq(IRQ_ETH); ETHIF_input((struct netif *)arg); __intc_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 = 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; #if 0 /* maximum transfer unit */ netif->mtu = 1500; /* broadcast capability */ netif->flags = NETIF_FLAG_BROADCAST;#endif /* 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 = 1; 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; low_level_init(netif);// request_irq(IRQ_ETH, ETH_Handler,netif); sys_timeout(ARP_TMR_INTERVAL, arp_timer, NULL); return ERR_OK;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -