stm32f769discovery 寄存器配置ETH,无操作系统移植lwip141

一直以来都想搞个寄存器配置版本的ETH,最近时间充裕,花了近2周,昨天终于实现了以太网的连接,上图

捕获.JPG (28.93 KB, 下载次数: 0)

下载附件  保存到相册

2018-3-23 10:48 上传



这是打印结果
下面详细说明一下
首先实现lan8742的驱动
  1. /* Ethernet pins configuration ************************************************/
  2.   /*
  3.         RMII_REF_CLK ----------------------> PA1
  4.         RMII_MDIO -------------------------> PA2
  5.         RMII_MDC --------------------------> PC1
  6.         RMII_MII_CRS_DV -------------------> PA7
  7.         RMII_MII_RXD0 ---------------------> PC4
  8.         RMII_MII_RXD1 ---------------------> PC5
  9.         RMII_MII_RXER ---------------------> PG2/PD5
  10.         RMII_MII_TX_EN --------------------> PG11
  11.         RMII_MII_TXD0 ---------------------> PG13
  12.         RMII_MII_TXD1 ---------------------> PG14
  13.   */
  14.     *(uint32_t *)0x40023830 |=  0x45; //使能PORTA\C\G时钟 0x4d,ACDG
  15.     *(uint32_t *)0x40023830 |=  0xf000000; //使能ETH,TX,RX,MAC时钟
  16.     GPIO_Set(GPIOA,PIN1|PIN2|PIN7,GPIO_MODE_AF,GPIO_OTYPE_PP,GPIO_SPEED_100M,GPIO_PUPD_NONE); //PB3,PB4设置
  17.         GPIO_AF_Set(GPIOA,1,11);        //PA1,AF11
  18.     GPIO_AF_Set(GPIOA,2,11);        //PA2,AF11
  19.     GPIO_AF_Set(GPIOA,7,11);        //PA7,AF11
  20.     GPIO_Set(GPIOC,PIN1|PIN4|PIN5,GPIO_MODE_AF,GPIO_OTYPE_PP,GPIO_SPEED_100M,GPIO_PUPD_NONE); //PB3,PB4设置
  21.         GPIO_AF_Set(GPIOC,1,11);        //PC1,AF11
  22.     GPIO_AF_Set(GPIOC,4,11);        //PC4,AF11
  23.     GPIO_AF_Set(GPIOC,5,11);        //PC5,AF11
  24.     GPIO_Set(GPIOD,PIN5,GPIO_MODE_AF,GPIO_OTYPE_PP,GPIO_SPEED_100M,GPIO_PUPD_NONE); //PB3,PB4设置
  25.         GPIO_AF_Set(GPIOD,5,11);        //PD5,AF11
  26.     GPIO_Set(GPIOG,PIN2|PIN11|PIN13|PIN14,GPIO_MODE_AF,GPIO_OTYPE_PP,GPIO_SPEED_100M,GPIO_PUPD_NONE); //PB3,PB4设置
  27.         GPIO_AF_Set(GPIOG,2,11);        //PG2,AF11
  28.     GPIO_AF_Set(GPIOG,11,11);        //PG11,AF11
  29.     GPIO_AF_Set(GPIOG,13,11);        //PG13,AF11
  30.     GPIO_AF_Set(GPIOG,14,11);        //PG14,AF11

  31.     MY_NVIC_Init(0,0,ETH_IRQn,2);        //配置ETH中的分组
复制代码


然后实现LAN8742寄存器的配置
  1. *(uint32_t *)0x40023830 |=  0x400000;
  2. RCC->AHB1RSTR |= 0x02000000;
  3.     /* Enable SYSCFG Clock */
  4.     *(uint32_t *)0x40023844 |= 0x4000;
  5.     SYSCFG->PMC &= ~(SYSCFG_PMC_MII_RMII_SEL);
  6.     SYSCFG->PMC |= 0x800000;//RMII
  7.     RCC->AHB1RSTR &=  ~ 0x02000000;
  8.     ETH->DMABMR |= 0x1; //software reset
  9.     while (ETH->DMABMR & ETH_DMABMR_SR);   

  10.     /* Write to ETHERNET MAC MIIAR: Configure the ETHERNET CSR Clock Range */
  11.     ETH->MACMIIAR = (uint32_t)(1<<4);
  12.     write_PHY(PHY_BCR, PHY_RESET);
  13.     /* 等待复位完成 */
  14.     for (tout = 0; tout < 0x10000; tout++)
  15.     {
  16.         read_PHY (PHY_BSR,®v);
  17.         if (!(regv & PHY_LINKED_STATUS))
  18.         {
  19.             /* 复位完成 */
  20.             printf("2. Reset Complete\r\n");
  21.             break;
  22.         }
  23.     }
  24.     write_PHY(PHY_BCR, PHY_AUTONEGOTIATION);
  25.     for (tout = 0; tout < 0x10000; tout++)
  26.     {
  27.          read_PHY (PHY_BSR,®v);
  28.         if (!(regv & PHY_AUTONEGO_COMPLETE))
  29.         {
  30.             /* 复位完成 */
  31.             printf("3. Auto-Negotiation Complete\r\n");
  32.             break;
  33.         }
  34.     }
  35.     for (tout = 0; tout < 0x1000; tout++);
  36.     read_PHY (PHY_SR,®v);
  37.     printf("%x\r\n",regv);
  38.      /* Configure the MAC with the Duplex Mode fixed by the auto-negotiation process */
  39.     if((regv & PHY_DUPLEX_STATUS) != (uint32_t)RESET)
  40.     {
  41.       /* Set Ethernet duplex mode to Full-duplex following the auto-negotiation */
  42.       printf("4. Full-duplex connection\r\n");
  43.         phyreg |= ETH_MODE_FULLDUPLEX;
  44.     }
  45.     else
  46.     {
  47.       /* Set Ethernet duplex mode to Half-duplex following the auto-negotiation */
  48.       printf("4. half-duplex connection\r\n");
  49.         phyreg |= ETH_MODE_HALFDUPLEX;        
  50.     }
  51.     if((regv & PHY_SPEED_STATUS) != (uint32_t)RESET)
  52.     {
  53.       /* Set Ethernet duplex mode to Full-duplex following the auto-negotiation */
  54.       printf("5. 10Mbps Mode\r\n");
  55.         phyreg |= ETH_SPEED_10M;
  56.     }
  57.     else
  58.     {
  59.       /* Set Ethernet duplex mode to Half-duplex following the auto-negotiation */
  60.       printf("5. 100Mbps Mode\r\n");  
  61.         phyreg |= ETH_SPEED_100M;        
  62.     }

  63.      /* Config MAC and DMA */
  64.   ETH_MACDMAConfig(heth, phyreg);
复制代码


下面是实现读写的函数
  1. void write_PHY (uint32_t PhyReg, uint16_t RegValue)
  2. {
  3.         uint32_t tout;
  4.     uint32_t tmpreg = 0;

  5.   ETH->MACMIIDR = (uint16_t)RegValue;


  6.         /* Get the ETHERNET MACMIIAR value */
  7.   tmpreg = ETH->MACMIIAR;
  8.   
  9.   /* Keep only the CSR Clock Range CR[2:0] bits value */
  10.   tmpreg &= ~ETH_MACMIIAR_CR_MASK;
  11.   
  12.   /* Prepare the MII register address value */
  13.   tmpreg |=(((uint32_t)(LAN8742A_PHY_ADDRESS << 11)) & ETH_MACMIIAR_PA); /* Set the PHY device address */
  14.   tmpreg |=(((uint32_t)PhyReg<<6) & ETH_MACMIIAR_MR);                 /* Set the PHY register address */
  15.   tmpreg |= ETH_MACMIIAR_MW;                                          /* Set the write mode */
  16.   tmpreg |= ETH_MACMIIAR_MB;                                          /* Set the MII Busy bit */
  17.   
  18.   /* Write the result value into the MII Address register */
  19.   ETH->MACMIIAR = tmpreg;
  20.         /* 等待操作完成,即等待MMAR_MB位被清零 */
  21.         tout = 0;
  22.         for (tout = 0; tout < MII_WR_TOUT; tout++)
  23.         {
  24.                 if ((ETH->MACMIIAR & ETH_MACMIIAR_MB) == 0)
  25.                 {
  26.                         break;
  27.                 }
  28.         }
  29. }

  30. void read_PHY (uint32_t PhyReg,uint32_t *RegValue)
  31. {
  32.         uint32_t tout;
  33.      uint32_t tmpreg = 0;

  34.         tmpreg = ETH->MACMIIAR;
  35.   
  36.   /* Keep only the CSR Clock Range CR[2:0] bits value */
  37.   tmpreg &= ~ETH_MACMIIAR_CR_MASK;
  38.   
  39.   /* Prepare the MII address register value */
  40.   tmpreg |=(((uint32_t)(LAN8742A_PHY_ADDRESS << 11)) & ETH_MACMIIAR_PA); /* Set the PHY device address   */
  41.   tmpreg |=(((uint32_t)PhyReg<<6) & ETH_MACMIIAR_MR);                   /* Set the PHY register address */
  42.   tmpreg &= ~ETH_MACMIIAR_MW;                                           /* Set the read mode            */
  43.   tmpreg |= ETH_MACMIIAR_MB;                                            /* Set the MII Busy bit         */
  44.   
  45.   /* Write the result value into the MII Address register */
  46.   ETH->MACMIIAR = tmpreg;

  47.         /* 等待操作完成,即等待MMAR_MB位被清零 */
  48.         tout = 0;
  49.         for (tout = 0; tout < MII_RD_TOUT; tout++)
  50.         {
  51.                 if ((ETH->MACMIIAR & ETH_MACMIIAR_MB) == 0)
  52.                 {
  53.                         break;
  54.                 }
  55.         }
  56.          /* Get MACMIIDR value */
  57.   *RegValue = (uint16_t)(ETH->MACMIIDR);
  58.         /* 从 PHY 中读取16bit的数据值 */
  59. }
复制代码


保存可以正常运行,并且连上lan8742芯片
下面开始移植lwp141,步骤就不说了,网上有

捕获1.JPG (33.6 KB, 下载次数: 0)

下载附件  保存到相册

2018-3-23 11:04 上传


直接给出代码
  1. static void low_level_init(struct netif *netif)
  2. {
  3.   uint8_t macaddress[6]= { MAC_ADDR0, MAC_ADDR1, MAC_ADDR2, MAC_ADDR3, MAC_ADDR4, MAC_ADDR5 };
  4.   
  5.   EthHandle.Instance = ETH;  
  6.   EthHandle.Init.MACAddr = macaddress;
  7.   EthHandle.Init.AutoNegotiation = ETH_AUTONEGOTIATION_ENABLE;
  8.   EthHandle.Init.Speed = ETH_SPEED_100M;
  9.   EthHandle.Init.DuplexMode = ETH_MODE_FULLDUPLEX;
  10.   EthHandle.Init.MediaInterface = ETH_MEDIA_INTERFACE_RMII;
  11.   EthHandle.Init.RxMode = ETH_RXINTERRUPT_MODE;
  12.   EthHandle.Init.ChecksumMode = ETH_CHECKSUM_BY_HARDWARE;
  13.   EthHandle.Init.PhyAddress = LAN8742A_PHY_ADDRESS;
  14.   /* configure ethernet peripheral (GPIOs, clocks, MAC, DMA) */
  15. lan8742_init(&EthHandle);
  16.   /* Initialize Tx Descriptors list: Chain Mode */
  17.   HAL_ETH_DMATxDescListInit(&EthHandle, DMATxDscrTab, &Tx_Buff[0][0], ETH_TXBUFNB);
  18.      
  19.   /* Initialize Rx Descriptors list: Chain Mode  */
  20.   HAL_ETH_DMARxDescListInit(&EthHandle, DMARxDscrTab, &Rx_Buff[0][0], ETH_RXBUFNB);
  21.   
  22.     /* Set netif link flag */
  23.    netif->flags |= NETIF_FLAG_LINK_UP;
  24.   /* set netif MAC hardware address length */
  25.   netif->hwaddr_len = ETHARP_HWADDR_LEN;

  26.   /* set netif MAC hardware address */
  27.   netif->hwaddr[0] =  MAC_ADDR0;
  28.   netif->hwaddr[1] =  MAC_ADDR1;
  29.   netif->hwaddr[2] =  MAC_ADDR2;
  30.   netif->hwaddr[3] =  MAC_ADDR3;
  31.   netif->hwaddr[4] =  MAC_ADDR4;
  32.   netif->hwaddr[5] =  MAC_ADDR5;

  33.   /* set netif maximum transfer unit */
  34.   netif->mtu = 1500;

  35.   /* Accept broadcast address and ARP traffic */
  36.   netif->flags |= NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP;
  37.   
  38. }
  39. err_t low_level_output(struct netif *netif, struct pbuf *p)
  40. {
  41.   err_t errval;
  42.   struct pbuf *q;
  43.   uint8_t *buffer = (uint8_t *)(EthHandle.TxDesc->Buffer1Addr);
  44.   __IO ETH_DMADescTypeDef *DmaTxDesc;
  45.   uint32_t framelength = 0;
  46.   uint32_t bufferoffset = 0;
  47.   uint32_t byteslefttocopy = 0;
  48.   uint32_t payloadoffset = 0;

  49.   DmaTxDesc = EthHandle.TxDesc;
  50.   bufferoffset = 0;
  51.   
  52.   /* copy frame from pbufs to driver buffers */
  53.   for(q = p; q != NULL; q = q->next)
  54.   {
  55.     /* Is this buffer available? If not, goto error */
  56.     if((DmaTxDesc->Status & ETH_DMATXDESC_OWN) != (uint32_t)RESET)
  57.     {
  58.       errval = ERR_USE;
  59.       goto error;
  60.     }
  61.    
  62.     /* Get bytes in current lwIP buffer */
  63.     byteslefttocopy = q->len;
  64.     payloadoffset = 0;
  65.    
  66.     /* Check if the length of data to copy is bigger than Tx buffer size*/
  67.     while( (byteslefttocopy + bufferoffset) > ETH_TX_BUF_SIZE )
  68.     {
  69.       /* Copy data to Tx buffer*/
  70.       memcpy( (uint8_t*)((uint8_t*)buffer + bufferoffset), (uint8_t*)((uint8_t*)q->payload + payloadoffset), (ETH_TX_BUF_SIZE - bufferoffset) );
  71.       
  72.       /* Point to next descriptor */
  73.       DmaTxDesc = (ETH_DMADescTypeDef *)(DmaTxDesc->Buffer2NextDescAddr);
  74.       
  75.       /* Check if the buffer is available */
  76.       if((DmaTxDesc->Status & ETH_DMATXDESC_OWN) != (uint32_t)RESET)
  77.       {
  78.         errval = ERR_USE;
  79.         goto error;
  80.       }
  81.       
  82.       buffer = (uint8_t *)(DmaTxDesc->Buffer1Addr);
  83.       
  84.       byteslefttocopy = byteslefttocopy - (ETH_TX_BUF_SIZE - bufferoffset);
  85.       payloadoffset = payloadoffset + (ETH_TX_BUF_SIZE - bufferoffset);
  86.       framelength = framelength + (ETH_TX_BUF_SIZE - bufferoffset);
  87.       bufferoffset = 0;
  88.     }
  89.    
  90.     /* Copy the remaining bytes */
  91.     memcpy( (uint8_t*)((uint8_t*)buffer + bufferoffset), (uint8_t*)((uint8_t*)q->payload + payloadoffset), byteslefttocopy );
  92.     bufferoffset = bufferoffset + byteslefttocopy;
  93.     framelength = framelength + byteslefttocopy;
  94.   }

  95.   /* Clean and Invalidate data cache */
  96.   SCB_CleanInvalidateDCache ();  
  97.   /* Prepare transmit descriptors to give to DMA */
  98.   HAL_ETH_TransmitFrame(&EthHandle, framelength);
  99.   
  100.   errval = ERR_OK;
  101.   
  102. error:
  103.   
  104.   /* When Transmit Underflow flag is set, clear it and issue a Transmit Poll Demand to resume transmission */
  105.   if ((EthHandle.Instance->DMASR & ETH_DMASR_TUS) != (uint32_t)RESET)
  106.   {
  107.     /* Clear TUS ETHERNET DMA flag */
  108.     EthHandle.Instance->DMASR = ETH_DMASR_TUS;
  109.    
  110.     /* Resume DMA transmission*/
  111.     EthHandle.Instance->DMATPDR = 0;
  112.   }
  113.   return errval;
  114. }

  115. /**
  116.   * [url=home.php?mod=space&uid=159083]@brief[/url] Should allocate a pbuf and transfer the bytes of the incoming
  117.   * packet from the interface into the pbuf.
  118.   *
  119.   * @param netif the lwip network interface structure for this ethernetif
  120.   * [url=home.php?mod=space&uid=784970]@return[/url] a pbuf filled with the received packet (including MAC header)
  121.   *         NULL on memory error
  122.   */
  123. struct pbuf * low_level_input(struct netif *netif)
  124. {
  125.   struct pbuf *p = NULL, *q = NULL;
  126.   uint16_t len = 0;
  127.   uint8_t *buffer;
  128.   __IO ETH_DMADescTypeDef *dmarxdesc;
  129.   uint32_t bufferoffset = 0;
  130.   uint32_t payloadoffset = 0;
  131.   uint32_t byteslefttocopy = 0;
  132.   uint32_t i=0;
  133.   
  134.   /* get received frame */
  135.   if(ETH_GetReceivedFrame(&EthHandle) != 0)
  136.     return NULL;
  137.   
  138.   /* Obtain the size of the packet and put it into the "len" variable. */
  139.   len = EthHandle.RxFrameInfos.length;
  140.   buffer = (uint8_t *)EthHandle.RxFrameInfos.buffer;
  141.   
  142.   if (len > 0)
  143.   {
  144.     /* We allocate a pbuf chain of pbufs from the Lwip buffer pool */
  145.     p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL);
  146.       printf("recv is ok\r\n");
  147.   }
  148.   
  149.   /* Clean and Invalidate data cache */
  150.   SCB_CleanInvalidateDCache ();
  151.   
  152.   if (p != NULL)
  153.   {
  154.     dmarxdesc = EthHandle.RxFrameInfos.FSRxDesc;
  155.     bufferoffset = 0;
  156.    
  157.     for(q = p; q != NULL; q = q->next)
  158.     {
  159.       byteslefttocopy = q->len;
  160.       payloadoffset = 0;
  161.       
  162.       /* Check if the length of bytes to copy in current pbuf is bigger than Rx buffer size */
  163.       while( (byteslefttocopy + bufferoffset) > ETH_RX_BUF_SIZE )
  164.       {
  165.         /* Copy data to pbuf */
  166.         memcpy( (uint8_t*)((uint8_t*)q->payload + payloadoffset), (uint8_t*)((uint8_t*)buffer + bufferoffset), (ETH_RX_BUF_SIZE - bufferoffset));
  167.         
  168.         /* Point to next descriptor */
  169.         dmarxdesc = (ETH_DMADescTypeDef *)(dmarxdesc->Buffer2NextDescAddr);
  170.         buffer = (uint8_t *)(dmarxdesc->Buffer1Addr);
  171.         
  172.         byteslefttocopy = byteslefttocopy - (ETH_RX_BUF_SIZE - bufferoffset);
  173.         payloadoffset = payloadoffset + (ETH_RX_BUF_SIZE - bufferoffset);
  174.         bufferoffset = 0;
  175.       }
  176.       
  177.       /* Copy remaining data in pbuf */
  178.       memcpy( (uint8_t*)((uint8_t*)q->payload + payloadoffset), (uint8_t*)((uint8_t*)buffer + bufferoffset), byteslefttocopy);
  179.       bufferoffset = bufferoffset + byteslefttocopy;
  180.     }
  181.   }
  182.    
  183.   /* Release descriptors to DMA */
  184.   /* Point to first descriptor */
  185.   dmarxdesc = EthHandle.RxFrameInfos.FSRxDesc;
  186.   /* Set Own bit in Rx descriptors: gives the buffers back to DMA */
  187.   for (i=0; i< EthHandle.RxFrameInfos.SegCount; i++)
  188.   {  
  189.     dmarxdesc->Status |= ETH_DMARXDESC_OWN;
  190.     dmarxdesc = (ETH_DMADescTypeDef *)(dmarxdesc->Buffer2NextDescAddr);
  191.   }
  192.    
  193.   /* Clear Segment_Count */
  194.   EthHandle.RxFrameInfos.SegCount =0;
  195.   
  196.   /* When Rx Buffer unavailable flag is set: clear it and resume reception */
  197.   if ((EthHandle.Instance->DMASR & ETH_DMASR_RBUS) != (uint32_t)RESET)  
  198.   {
  199.     /* Clear RBUS ETHERNET DMA flag */
  200.     EthHandle.Instance->DMASR = ETH_DMASR_RBUS;
  201.     /* Resume DMA reception */
  202.     EthHandle.Instance->DMARPDR = 0;
  203.   }
  204.   return p;
  205. }
  206. void ethernetif_input( void const * argument )
  207. {
  208.   struct pbuf *p;
  209.   struct netif *netif = (struct netif *) argument;
  210.   
  211.   
  212.    
  213.    
  214.         p = low_level_input( netif );
  215.         if (p != NULL)
  216.         {
  217.           if (netif->input( p, netif) != ERR_OK )
  218.           {
  219.                 LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_input: IP input error\n"));
  220.                 pbuf_free(p);
  221.           }
  222.         }
  223.    
  224.   
  225. }
  226. err_t ethernetif_init(struct netif *netif)
  227. {
  228.   LWIP_ASSERT("netif != NULL", (netif != NULL));

  229. #if LWIP_NETIF_HOSTNAME
  230.   /* Initialize interface hostname */
  231.   netif->hostname = "lwip";
  232. #endif /* LWIP_NETIF_HOSTNAME */

  233.   netif->name[0] = IFNAME0;
  234.   netif->name[1] = IFNAME1;

  235.   /* We directly use etharp_output() here to save a function call.
  236.    * You can instead declare your own function an call etharp_output()
  237.    * from it if you have to do some checks before sending (e.g. if link
  238.    * is available...) */
  239.   netif->output = etharp_output;
  240.   netif->linkoutput = low_level_output;

  241.   /* initialize the hardware */
  242.   low_level_init(netif);

  243.   return ERR_OK;
  244. }
复制代码

编译正常,然后开始网络的配置
  1. static void Netif_Config(void)
  2. {
  3.   ip_addr_t ipaddr;
  4.   ip_addr_t netmask;
  5.   ip_addr_t gw;
  6. uint8_t iptab[4] = {0};
  7.   uint8_t iptxt[20];
  8. uint8_t netmasktab[4]={0};
  9.         uint8_t netmasktxt[30];
  10.         
  11.         uint8_t gwtab[4]={0};
  12.         uint8_t gwtxt[30];
  13.    
  14.    
  15.      mem_init();               //3?ê??ˉ?ˉì??ú′???

  16.   memp_init();             //3?ê??ˉ?ú′?3?
  17.    
  18. #ifdef USE_DHCP
  19.   ipaddr.addr = 0;
  20.         netmask.addr = 0;
  21.         gw.addr = 0;
  22. #else
  23.   IP4_ADDR(&ipaddr,IP_ADDR0,IP_ADDR1,IP_ADDR2,IP_ADDR3);
  24.   IP4_ADDR(&netmask,NETMASK_ADDR0,NETMASK_ADDR1,NETMASK_ADDR2,NETMASK_ADDR3);
  25.   IP4_ADDR(&gw,GW_ADDR0,GW_ADDR1,GW_ADDR2,GW_ADDR3);
  26. #endif /* USE_DHCP */
  27.   
  28.   netif_add(&gnetif, &ipaddr, &netmask, &gw, NULL, eernetif_init, eernet_input);
  29.   
  30.   /*  Registers the default network interface. */
  31.   netif_set_default(&gnetif);
  32.   
  33.   if (netif_is_link_up(&gnetif))
  34.   {
  35.     /* When the netif is fully configured this function must be called.*/
  36.     netif_set_up(&gnetif);
  37.       iptab[0] = IP_ADDR3;
  38.     iptab[1] = IP_ADDR2;
  39.     iptab[2] = IP_ADDR1;
  40.     iptab[3] = IP_ADDR0;
  41.                
  42.                 netmasktab[0]=NETMASK_ADDR3;
  43.                 netmasktab[1]=NETMASK_ADDR2;
  44.                 netmasktab[2]=NETMASK_ADDR1;
  45.                 netmasktab[3]=NETMASK_ADDR0;
  46.                
  47.                 gwtab[0]=GW_ADDR3;
  48.                 gwtab[1]=GW_ADDR2;
  49.                 gwtab[2]=GW_ADDR1;
  50.                 gwtab[3]=GW_ADDR0;

  51. //
  52.     sprintf((char*)iptxt, "  %d.%d.%d.%d", iptab[3], iptab[2], iptab[1], iptab[0]);
  53.                
  54.                 sprintf((char*)netmasktxt, "%d.%d.%d.%d",netmasktab[3],netmasktab[2],netmasktab[1],netmasktab[0]);
  55.                
  56.                 sprintf((char*)gwtxt,"%d.%d.%d.%d",gwtab[3],gwtab[2],gwtab[1],gwtab[0]);
  57.                
  58. //
  59.     printf("\r\n");
  60.                 printf("\r\n");
  61.                 printf("        Network Cable is connected            \r\n");
  62.                 printf("     This is lwip1.4.1-ping demo test         \r\n");
  63.                 printf("     The stm32f769 ip address is: %s\r\n",iptxt);
  64.                 printf("The stm32f769 netmask address is: %s\r\n",netmasktxt);
  65.                 printf("     The stm32f769 gw address is: %s\r\n",gwtxt);
  66.                 printf("\r\n");
  67.                 printf("\r\n");
  68.                 printf("\r\n");
  69.   }
  70.   else
  71.   {
  72.     /* When the netif link is down this function must be called */
  73.     netif_set_down(&gnetif);
  74.     printf("            Network Cable is not connected          \r\n");
  75.   }
  76. }
复制代码

完成后,实现网络数据的处理
  1. void LwIP_Pkt_Handle(void)
  2. {
  3.   /* Read a received packet from the Ethernet buffers and send it to the lwIP for handling */
  4.   ethernetif_input(&gnetif);
  5. }
  6. void LwIP_Periodic_Handle(__IO uint32_t localtime)
  7. {
  8. #if LWIP_TCP
  9.   /* TCP periodic process every 250 ms */
  10.   if (localtime - TCPTimer >= TCP_TMR_INTERVAL)
  11.   {
  12.     TCPTimer =  localtime;
  13.     tcp_tmr();
  14.   }
  15. #endif


  16.     etharp_tmr();

  17. }
复制代码

在main函数添加网路初始化,及数据处理
  1. Netif_Config();
  2.    
  3.         while(1)
  4.         {
  5.                 if (ETH_GetReceivedFrame(&EthHandle))  //?ì2éê?·??óê?μ?êy?Y°ü
  6.     {
  7.       LwIP_Pkt_Handle();   //
  8.     }
  9.     /* handle periodic timers for LwIP */
  10.     LwIP_Periodic_Handle(LocalTime);
  11.         led_toggle();
  12.         delay_ms(5000);
  13.         }
复制代码



其他功能目前还在研究


                                              发稿与2018年3月23日

你可能感兴趣的:(嵌入式)