TA的每日心情 | 奋斗 2021-7-30 11:40 |
---|
签到天数: 1792 天 连续签到: 1 天 [LV.Master]伴坛终老
|
本帖最后由 ky123 于 2018-4-2 17:59 编辑
一直以来都想搞个寄存器配置版本的ETH,最近时间充裕,花了近2周,昨天终于实现了以太网的连接,上图
这是打印结果
下面详细说明一下
首先实现lan8742的驱动
- /* Ethernet pins configuration ************************************************/
- /*
- RMII_REF_CLK ----------------------> PA1
- RMII_MDIO -------------------------> PA2
- RMII_MDC --------------------------> PC1
- RMII_MII_CRS_DV -------------------> PA7
- RMII_MII_RXD0 ---------------------> PC4
- RMII_MII_RXD1 ---------------------> PC5
- RMII_MII_RXER ---------------------> PG2/PD5
- RMII_MII_TX_EN --------------------> PG11
- RMII_MII_TXD0 ---------------------> PG13
- RMII_MII_TXD1 ---------------------> PG14
- */
- *(uint32_t *)0x40023830 |= 0x45; //使能PORTA\C\G时钟 0x4d,ACDG
- *(uint32_t *)0x40023830 |= 0xf000000; //使能ETH,TX,RX,MAC时钟
- GPIO_Set(GPIOA,PIN1|PIN2|PIN7,GPIO_MODE_AF,GPIO_OTYPE_PP,GPIO_SPEED_100M,GPIO_PUPD_NONE); //PB3,PB4设置
- GPIO_AF_Set(GPIOA,1,11); //PA1,AF11
- GPIO_AF_Set(GPIOA,2,11); //PA2,AF11
- GPIO_AF_Set(GPIOA,7,11); //PA7,AF11
- GPIO_Set(GPIOC,PIN1|PIN4|PIN5,GPIO_MODE_AF,GPIO_OTYPE_PP,GPIO_SPEED_100M,GPIO_PUPD_NONE); //PB3,PB4设置
- GPIO_AF_Set(GPIOC,1,11); //PC1,AF11
- GPIO_AF_Set(GPIOC,4,11); //PC4,AF11
- GPIO_AF_Set(GPIOC,5,11); //PC5,AF11
- GPIO_Set(GPIOD,PIN5,GPIO_MODE_AF,GPIO_OTYPE_PP,GPIO_SPEED_100M,GPIO_PUPD_NONE); //PB3,PB4设置
- GPIO_AF_Set(GPIOD,5,11); //PD5,AF11
- GPIO_Set(GPIOG,PIN2|PIN11|PIN13|PIN14,GPIO_MODE_AF,GPIO_OTYPE_PP,GPIO_SPEED_100M,GPIO_PUPD_NONE); //PB3,PB4设置
- GPIO_AF_Set(GPIOG,2,11); //PG2,AF11
- GPIO_AF_Set(GPIOG,11,11); //PG11,AF11
- GPIO_AF_Set(GPIOG,13,11); //PG13,AF11
- GPIO_AF_Set(GPIOG,14,11); //PG14,AF11
- MY_NVIC_Init(0,0,ETH_IRQn,2); //配置ETH中的分组
[color=rgb(51, 102, 153) !important]复制代码
然后实现LAN8742寄存器的配置
- *(uint32_t *)0x40023830 |= 0x400000;
- RCC->AHB1RSTR |= 0x02000000;
- /* Enable SYSCFG Clock */
- *(uint32_t *)0x40023844 |= 0x4000;
- SYSCFG->PMC &= ~(SYSCFG_PMC_MII_RMII_SEL);
- SYSCFG->PMC |= 0x800000;//RMII
- RCC->AHB1RSTR &= ~ 0x02000000;
- ETH->DMABMR |= 0x1; //software reset
- while (ETH->DMABMR & ETH_DMABMR_SR);
- /* Write to ETHERNET MAC MIIAR: Configure the ETHERNET CSR Clock Range */
- ETH->MACMIIAR = (uint32_t)(1<<4);
- write_PHY(PHY_BCR, PHY_RESET);
- /* 等待复位完成 */
- for (tout = 0; tout < 0x10000; tout++)
- {
- read_PHY (PHY_BSR,®v);
- if (!(regv & PHY_LINKED_STATUS))
- {
- /* 复位完成 */
- printf("2. Reset Complete\r\n");
- break;
- }
- }
- write_PHY(PHY_BCR, PHY_AUTONEGOTIATION);
- for (tout = 0; tout < 0x10000; tout++)
- {
- read_PHY (PHY_BSR,®v);
- if (!(regv & PHY_AUTONEGO_COMPLETE))
- {
- /* 复位完成 */
- printf("3. Auto-Negotiation Complete\r\n");
- break;
- }
- }
- for (tout = 0; tout < 0x1000; tout++);
- read_PHY (PHY_SR,®v);
- printf("%x\r\n",regv);
- /* Configure the MAC with the Duplex Mode fixed by the auto-negotiation process */
- if((regv & PHY_DUPLEX_STATUS) != (uint32_t)RESET)
- {
- /* Set Ethernet duplex mode to Full-duplex following the auto-negotiation */
- printf("4. Full-duplex connection\r\n");
- phyreg |= ETH_MODE_FULLDUPLEX;
- }
- else
- {
- /* Set Ethernet duplex mode to Half-duplex following the auto-negotiation */
- printf("4. half-duplex connection\r\n");
- phyreg |= ETH_MODE_HALFDUPLEX;
- }
- if((regv & PHY_SPEED_STATUS) != (uint32_t)RESET)
- {
- /* Set Ethernet duplex mode to Full-duplex following the auto-negotiation */
- printf("5. 10Mbps Mode\r\n");
- phyreg |= ETH_SPEED_10M;
- }
- else
- {
- /* Set Ethernet duplex mode to Half-duplex following the auto-negotiation */
- printf("5. 100Mbps Mode\r\n");
- phyreg |= ETH_SPEED_100M;
- }
- /* Config MAC and DMA */
- ETH_MACDMAConfig(heth, phyreg);
[color=rgb(51, 102, 153) !important]复制代码
下面是实现读写的函数
- void write_PHY (uint32_t PhyReg, uint16_t RegValue)
- {
- uint32_t tout;
- uint32_t tmpreg = 0;
- ETH->MACMIIDR = (uint16_t)RegValue;
- /* Get the ETHERNET MACMIIAR value */
- tmpreg = ETH->MACMIIAR;
- /* Keep only the CSR Clock Range CR[2:0] bits value */
- tmpreg &= ~ETH_MACMIIAR_CR_MASK;
- /* Prepare the MII register address value */
- tmpreg |=(((uint32_t)(LAN8742A_PHY_ADDRESS << 11)) & ETH_MACMIIAR_PA); /* Set the PHY device address */
- tmpreg |=(((uint32_t)PhyReg<<6) & ETH_MACMIIAR_MR); /* Set the PHY register address */
- tmpreg |= ETH_MACMIIAR_MW; /* Set the write mode */
- tmpreg |= ETH_MACMIIAR_MB; /* Set the MII Busy bit */
- /* Write the result value into the MII Address register */
- ETH->MACMIIAR = tmpreg;
- /* 等待操作完成,即等待MMAR_MB位被清零 */
- tout = 0;
- for (tout = 0; tout < MII_WR_TOUT; tout++)
- {
- if ((ETH->MACMIIAR & ETH_MACMIIAR_MB) == 0)
- {
- break;
- }
- }
- }
- void read_PHY (uint32_t PhyReg,uint32_t *RegValue)
- {
- uint32_t tout;
- uint32_t tmpreg = 0;
- tmpreg = ETH->MACMIIAR;
- /* Keep only the CSR Clock Range CR[2:0] bits value */
- tmpreg &= ~ETH_MACMIIAR_CR_MASK;
- /* Prepare the MII address register value */
- tmpreg |=(((uint32_t)(LAN8742A_PHY_ADDRESS << 11)) & ETH_MACMIIAR_PA); /* Set the PHY device address */
- tmpreg |=(((uint32_t)PhyReg<<6) & ETH_MACMIIAR_MR); /* Set the PHY register address */
- tmpreg &= ~ETH_MACMIIAR_MW; /* Set the read mode */
- tmpreg |= ETH_MACMIIAR_MB; /* Set the MII Busy bit */
- /* Write the result value into the MII Address register */
- ETH->MACMIIAR = tmpreg;
- /* 等待操作完成,即等待MMAR_MB位被清零 */
- tout = 0;
- for (tout = 0; tout < MII_RD_TOUT; tout++)
- {
- if ((ETH->MACMIIAR & ETH_MACMIIAR_MB) == 0)
- {
- break;
- }
- }
- /* Get MACMIIDR value */
- *RegValue = (uint16_t)(ETH->MACMIIDR);
- /* 从 PHY 中读取16bit的数据值 */
- }
[color=rgb(51, 102, 153) !important]复制代码
保存可以正常运行,并且连上lan8742芯片
下面开始移植lwp141,步骤就不说了,网上有
直接给出代码
- static void low_level_init(struct netif *netif)
- {
- uint8_t macaddress[6]= { MAC_ADDR0, MAC_ADDR1, MAC_ADDR2, MAC_ADDR3, MAC_ADDR4, MAC_ADDR5 };
- EthHandle.Instance = ETH;
- EthHandle.Init.MACAddr = macaddress;
- EthHandle.Init.AutoNegotiation = ETH_AUTONEGOTIATION_ENABLE;
- EthHandle.Init.Speed = ETH_SPEED_100M;
- EthHandle.Init.DuplexMode = ETH_MODE_FULLDUPLEX;
- EthHandle.Init.MediaInterface = ETH_MEDIA_INTERFACE_RMII;
- EthHandle.Init.RxMode = ETH_RXINTERRUPT_MODE;
- EthHandle.Init.ChecksumMode = ETH_CHECKSUM_BY_HARDWARE;
- EthHandle.Init.PhyAddress = LAN8742A_PHY_ADDRESS;
- /* configure ethernet peripheral (GPIOs, clocks, MAC, DMA) */
- lan8742_init(&EthHandle);
- /* Initialize Tx Descriptors list: Chain Mode */
- HAL_ETH_DMATxDescListInit(&EthHandle, DMATxDscrTab, &Tx_Buff[0][0], ETH_TXBUFNB);
- /* Initialize Rx Descriptors list: Chain Mode */
- HAL_ETH_DMARxDescListInit(&EthHandle, DMARxDscrTab, &Rx_Buff[0][0], ETH_RXBUFNB);
- /* Set netif link flag */
- netif->flags |= NETIF_FLAG_LINK_UP;
- /* set netif MAC hardware address length */
- netif->hwaddr_len = ETHARP_HWADDR_LEN;
- /* set netif MAC hardware address */
- netif->hwaddr[0] = MAC_ADDR0;
- netif->hwaddr[1] = MAC_ADDR1;
- netif->hwaddr[2] = MAC_ADDR2;
- netif->hwaddr[3] = MAC_ADDR3;
- netif->hwaddr[4] = MAC_ADDR4;
- netif->hwaddr[5] = MAC_ADDR5;
- /* set netif maximum transfer unit */
- netif->mtu = 1500;
- /* Accept broadcast address and ARP traffic */
- netif->flags |= NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP;
- }
- err_t low_level_output(struct netif *netif, struct pbuf *p)
- {
- err_t errval;
- struct pbuf *q;
- uint8_t *buffer = (uint8_t *)(EthHandle.TxDesc->Buffer1Addr);
- __IO ETH_DMADescTypeDef *DmaTxDesc;
- uint32_t framelength = 0;
- uint32_t bufferoffset = 0;
- uint32_t byteslefttocopy = 0;
- uint32_t payloadoffset = 0;
- DmaTxDesc = EthHandle.TxDesc;
- bufferoffset = 0;
- /* copy frame from pbufs to driver buffers */
- for(q = p; q != NULL; q = q->next)
- {
- /* Is this buffer available? If not, goto error */
- if((DmaTxDesc->Status & ETH_DMATXDESC_OWN) != (uint32_t)RESET)
- {
- errval = ERR_USE;
- goto error;
- }
- /* Get bytes in current lwIP buffer */
- byteslefttocopy = q->len;
- payloadoffset = 0;
- /* Check if the length of data to copy is bigger than Tx buffer size*/
- while( (byteslefttocopy + bufferoffset) > ETH_TX_BUF_SIZE )
- {
- /* Copy data to Tx buffer*/
- memcpy( (uint8_t*)((uint8_t*)buffer + bufferoffset), (uint8_t*)((uint8_t*)q->payload + payloadoffset), (ETH_TX_BUF_SIZE - bufferoffset) );
- /* Point to next descriptor */
- DmaTxDesc = (ETH_DMADescTypeDef *)(DmaTxDesc->Buffer2NextDescAddr);
- /* Check if the buffer is available */
- if((DmaTxDesc->Status & ETH_DMATXDESC_OWN) != (uint32_t)RESET)
- {
- errval = ERR_USE;
- goto error;
- }
- buffer = (uint8_t *)(DmaTxDesc->Buffer1Addr);
- byteslefttocopy = byteslefttocopy - (ETH_TX_BUF_SIZE - bufferoffset);
- payloadoffset = payloadoffset + (ETH_TX_BUF_SIZE - bufferoffset);
- framelength = framelength + (ETH_TX_BUF_SIZE - bufferoffset);
- bufferoffset = 0;
- }
- /* Copy the remaining bytes */
- memcpy( (uint8_t*)((uint8_t*)buffer + bufferoffset), (uint8_t*)((uint8_t*)q->payload + payloadoffset), byteslefttocopy );
- bufferoffset = bufferoffset + byteslefttocopy;
- framelength = framelength + byteslefttocopy;
- }
- /* Clean and Invalidate data cache */
- SCB_CleanInvalidateDCache ();
- /* Prepare transmit descriptors to give to DMA */
- HAL_ETH_TransmitFrame(&EthHandle, framelength);
- errval = ERR_OK;
- error:
- /* When Transmit Underflow flag is set, clear it and issue a Transmit Poll Demand to resume transmission */
- if ((EthHandle.Instance->DMASR & ETH_DMASR_TUS) != (uint32_t)RESET)
- {
- /* Clear TUS ETHERNET DMA flag */
- EthHandle.Instance->DMASR = ETH_DMASR_TUS;
- /* Resume DMA transmission*/
- EthHandle.Instance->DMATPDR = 0;
- }
- return errval;
- }
- /**
- * @brief Should allocate a pbuf and transfer the bytes of the incoming
- * packet from the interface into the pbuf.
- *
- * @param netif the lwip network interface structure for this ethernetif
- * @return a pbuf filled with the received packet (including MAC header)
- * NULL on memory error
- */
- struct pbuf * low_level_input(struct netif *netif)
- {
- struct pbuf *p = NULL, *q = NULL;
- uint16_t len = 0;
- uint8_t *buffer;
- __IO ETH_DMADescTypeDef *dmarxdesc;
- uint32_t bufferoffset = 0;
- uint32_t payloadoffset = 0;
- uint32_t byteslefttocopy = 0;
- uint32_t i=0;
- /* get received frame */
- if(ETH_GetReceivedFrame(&EthHandle) != 0)
- return NULL;
- /* Obtain the size of the packet and put it into the "len" variable. */
- len = EthHandle.RxFrameInfos.length;
- buffer = (uint8_t *)EthHandle.RxFrameInfos.buffer;
- if (len > 0)
- {
- /* We allocate a pbuf chain of pbufs from the Lwip buffer pool */
- p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL);
- printf("recv is ok\r\n");
- }
- /* Clean and Invalidate data cache */
- SCB_CleanInvalidateDCache ();
- if (p != NULL)
- {
- dmarxdesc = EthHandle.RxFrameInfos.FSRxDesc;
- bufferoffset = 0;
- for(q = p; q != NULL; q = q->next)
- {
- byteslefttocopy = q->len;
- payloadoffset = 0;
- /* Check if the length of bytes to copy in current pbuf is bigger than Rx buffer size */
- while( (byteslefttocopy + bufferoffset) > ETH_RX_BUF_SIZE )
- {
- /* Copy data to pbuf */
- memcpy( (uint8_t*)((uint8_t*)q->payload + payloadoffset), (uint8_t*)((uint8_t*)buffer + bufferoffset), (ETH_RX_BUF_SIZE - bufferoffset));
- /* Point to next descriptor */
- dmarxdesc = (ETH_DMADescTypeDef *)(dmarxdesc->Buffer2NextDescAddr);
- buffer = (uint8_t *)(dmarxdesc->Buffer1Addr);
- byteslefttocopy = byteslefttocopy - (ETH_RX_BUF_SIZE - bufferoffset);
- payloadoffset = payloadoffset + (ETH_RX_BUF_SIZE - bufferoffset);
- bufferoffset = 0;
- }
- /* Copy remaining data in pbuf */
- memcpy( (uint8_t*)((uint8_t*)q->payload + payloadoffset), (uint8_t*)((uint8_t*)buffer + bufferoffset), byteslefttocopy);
- bufferoffset = bufferoffset + byteslefttocopy;
- }
- }
- /* Release descriptors to DMA */
- /* Point to first descriptor */
- dmarxdesc = EthHandle.RxFrameInfos.FSRxDesc;
- /* Set Own bit in Rx descriptors: gives the buffers back to DMA */
- for (i=0; i< EthHandle.RxFrameInfos.SegCount; i++)
- {
- dmarxdesc->Status |= ETH_DMARXDESC_OWN;
- dmarxdesc = (ETH_DMADescTypeDef *)(dmarxdesc->Buffer2NextDescAddr);
- }
- /* Clear Segment_Count */
- EthHandle.RxFrameInfos.SegCount =0;
- /* When Rx Buffer unavailable flag is set: clear it and resume reception */
- if ((EthHandle.Instance->DMASR & ETH_DMASR_RBUS) != (uint32_t)RESET)
- {
- /* Clear RBUS ETHERNET DMA flag */
- EthHandle.Instance->DMASR = ETH_DMASR_RBUS;
- /* Resume DMA reception */
- EthHandle.Instance->DMARPDR = 0;
- }
- return p;
- }
- void ethernetif_input( void const * argument )
- {
- struct pbuf *p;
- struct netif *netif = (struct netif *) argument;
- p = low_level_input( netif );
- if (p != NULL)
- {
- if (netif->input( p, netif) != ERR_OK )
- {
- LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_input: IP input error\n"));
- pbuf_free(p);
- }
- }
- }
- err_t ethernetif_init(struct netif *netif)
- {
- LWIP_ASSERT("netif != NULL", (netif != NULL));
- #if LWIP_NETIF_HOSTNAME
- /* Initialize interface hostname */
- netif->hostname = "lwip";
- #endif /* LWIP_NETIF_HOSTNAME */
- netif->name[0] = IFNAME0;
- netif->name[1] = IFNAME1;
- /* We directly use etharp_output() here to save a function call.
- * You can instead declare your own function an call etharp_output()
- * from it if you have to do some checks before sending (e.g. if link
- * is available...) */
- netif->output = etharp_output;
- netif->linkoutput = low_level_output;
- /* initialize the hardware */
- low_level_init(netif);
- return ERR_OK;
- }
[color=rgb(51, 102, 153) !important]复制代码
编译正常,然后开始网络的配置
- static void Netif_Config(void)
- {
- ip_addr_t ipaddr;
- ip_addr_t netmask;
- ip_addr_t gw;
- uint8_t iptab[4] = {0};
- uint8_t iptxt[20];
- uint8_t netmasktab[4]={0};
- uint8_t netmasktxt[30];
- uint8_t gwtab[4]={0};
- uint8_t gwtxt[30];
- mem_init(); //3?ê??ˉ?ˉì??ú′???
- memp_init(); //3?ê??ˉ?ú′?3?
- #ifdef USE_DHCP
- ipaddr.addr = 0;
- netmask.addr = 0;
- gw.addr = 0;
- #else
- IP4_ADDR(&ipaddr,IP_ADDR0,IP_ADDR1,IP_ADDR2,IP_ADDR3);
- IP4_ADDR(&netmask,NETMASK_ADDR0,NETMASK_ADDR1,NETMASK_ADDR2,NETMASK_ADDR3);
- IP4_ADDR(&gw,GW_ADDR0,GW_ADDR1,GW_ADDR2,GW_ADDR3);
- #endif /* USE_DHCP */
- netif_add(&gnetif, &ipaddr, &netmask, &gw, NULL, eernetif_init, eernet_input);
- /* Registers the default network interface. */
- netif_set_default(&gnetif);
- if (netif_is_link_up(&gnetif))
- {
- /* When the netif is fully configured this function must be called.*/
- netif_set_up(&gnetif);
- iptab[0] = IP_ADDR3;
- iptab[1] = IP_ADDR2;
- iptab[2] = IP_ADDR1;
- iptab[3] = IP_ADDR0;
- netmasktab[0]=NETMASK_ADDR3;
- netmasktab[1]=NETMASK_ADDR2;
- netmasktab[2]=NETMASK_ADDR1;
- netmasktab[3]=NETMASK_ADDR0;
- gwtab[0]=GW_ADDR3;
- gwtab[1]=GW_ADDR2;
- gwtab[2]=GW_ADDR1;
- gwtab[3]=GW_ADDR0;
- //
- sprintf((char*)iptxt, " %d.%d.%d.%d", iptab[3], iptab[2], iptab[1], iptab[0]);
- sprintf((char*)netmasktxt, "%d.%d.%d.%d",netmasktab[3],netmasktab[2],netmasktab[1],netmasktab[0]);
- sprintf((char*)gwtxt,"%d.%d.%d.%d",gwtab[3],gwtab[2],gwtab[1],gwtab[0]);
- //
- printf("\r\n");
- printf("\r\n");
- printf(" Network Cable is connected \r\n");
- printf(" This is lwip1.4.1-ping demo test \r\n");
- printf(" The stm32f769 ip address is: %s\r\n",iptxt);
- printf("The stm32f769 netmask address is: %s\r\n",netmasktxt);
- printf(" The stm32f769 gw address is: %s\r\n",gwtxt);
- printf("\r\n");
- printf("\r\n");
- printf("\r\n");
- }
- else
- {
- /* When the netif link is down this function must be called */
- netif_set_down(&gnetif);
- printf(" Network Cable is not connected \r\n");
- }
- }
[color=rgb(51, 102, 153) !important]复制代码
完成后,实现网络数据的处理
- void LwIP_Pkt_Handle(void)
- {
- /* Read a received packet from the Ethernet buffers and send it to the lwIP for handling */
- ethernetif_input(&gnetif);
- }
- void LwIP_Periodic_Handle(__IO uint32_t localtime)
- {
- #if LWIP_TCP
- /* TCP periodic process every 250 ms */
- if (localtime - TCPTimer >= TCP_TMR_INTERVAL)
- {
- TCPTimer = localtime;
- tcp_tmr();
- }
- #endif
- etharp_tmr();
- }
[color=rgb(51, 102, 153) !important]复制代码
在main函数添加网路初始化,及数据处理
- Netif_Config();
- while(1)
- {
- if (ETH_GetReceivedFrame(&EthHandle)) //?ì2éê?·??óê?μ?êy?Y°ü
- {
- LwIP_Pkt_Handle(); //
- }
- /* handle periodic timers for LwIP */
- LwIP_Periodic_Handle(LocalTime);
- led_toggle();
- delay_ms(5000);
- }
[color=rgb(51, 102, 153) !important]复制代码
其他功能目前还在研究
|
|