打印
[技术问答]

M487JIDAE和IP101GR芯片,连接以太网成功一段时间就不行了,求助各位!!!!!!!!!!!

[复制链接]
628|8
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
fst20220608|  楼主 | 2022-6-9 14:17 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
我使用官方给的例程EMAC_TxRx和EC_M480_Ethernet_USBH_Bridge_V1.00这个例程,插入网线刚开始都能正常,过了一段时间,以太网没网了,在电脑上ping ip 也不行,以下是例程EMAC_TxRx代码:
// Our MAC address
uint8_t g_au8MacAddr[6] = {0x00, 0x00, 0x00, 0x59, 0x16, 0x88};
// Our IP address
uint8_t volatile g_au8IpAddr[4] = {0, 0, 0, 0};
uint8_t auPkt[1514];
uint32_t u32PktLen;

// Descriptor pointers holds current Tx and Rx used by IRQ handler here.
uint32_t u32CurrentTxDesc, u32CurrentRxDesc;

void TMR0_IRQHandler(void)
{
//    static unsigned char count = 0;

    //printf("%d sec\n", sec++);
    // clear timer interrupt flag
    TIMER_ClearIntFlag(TIMER0);
    PH0=!PH0;  
    PH1=!PH1;
    PH2=!PH2;
    printf("1s enter TIM interrupt\r\n");
//    __NOP();
}
/**
  * @brief  EMAC Tx interrupt handler.
  * @param  None
  * @return None
  */
void EMAC_TX_IRQHandler(void)
{
    // Clean up Tx resource occupied by previous sent packet(s)
    EMAC_SendPktDone();
}

/**
  * @brief  EMAC Rx interrupt handler.
  * @param  None
  * @return None
  */
void EMAC_RX_IRQHandler(void)
{
    int i=0;
//    while(1)
//    {
        printf("u32PktLen =%d\r\n",u32PktLen);
        // Check if there's any packets available
        if(EMAC_RecvPkt(auPkt, &u32PktLen) == 0)
            return;
            //break;
        // Process receive packet
        process_rx_packet(auPkt, u32PktLen);
        for(i=0;i<u32PktLen;i++)
        {
            if(auPkt==192)
            {
                printf("IP address: %d.%d.%d.%d\n", auPkt, auPkt[i+1],auPkt[i+2], auPkt[i+3]);
            }
        }
        // Clean up Rx resource occupied by previous received packet
        EMAC_RecvPktDone();
//    }
}

void SYS_Init(void)
{

    /* Unlock protected registers */
    SYS_UnlockReg();

    /* Set XT1_OUT(PF.2) and XT1_IN(PF.3) to input mode */
    PF->MODE &= ~(GPIO_MODE_MODE2_Msk | GPIO_MODE_MODE3_Msk);

    /* Enable External XTAL (4~24 MHz) */
    CLK_EnableXtalRC(CLK_PWRCTL_HXTEN_Msk);

    /* Waiting for 12MHz clock ready */
    CLK_WaitClockReady( CLK_STATUS_HXTSTB_Msk);

    /* Switch HCLK clock source to HXT */
    CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_HXT,CLK_CLKDIV0_HCLK(1));

    /* Set core clock as PLL_CLOCK from PLL */
    CLK_SetCoreClock(FREQ_192MHZ);

    /* Set both PCLK0 and PCLK1 as HCLK/2 */
    CLK->PCLKDIV = CLK_PCLKDIV_APB0DIV_DIV2 | CLK_PCLKDIV_APB1DIV_DIV2;

    /* Enable IP clock */
    CLK_EnableModuleClock(UART0_MODULE);
    CLK_EnableModuleClock(EMAC_MODULE);

    /* Select IP clock source */
    CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART0SEL_HXT, CLK_CLKDIV0_UART0(1));

    // Configure MDC clock rate to HCLK / (127 + 1) = 1.5 MHz if system is running at 192 MHz
    CLK_SetModuleClock(EMAC_MODULE, 0, CLK_CLKDIV3_EMAC(127));

    /* Update System Core Clock */
    /* User can use SystemCoreClockUpdate() to calculate SystemCoreClock. */
    SystemCoreClockUpdate();

    /* Set GPB multi-function pins for UART0 RXD and TXD */
    SYS->GPB_MFPH &= ~(SYS_GPB_MFPH_PB12MFP_Msk | SYS_GPB_MFPH_PB13MFP_Msk);
    SYS->GPB_MFPH |= (SYS_GPB_MFPH_PB12MFP_UART0_RXD | SYS_GPB_MFPH_PB13MFP_UART0_TXD);
    // Configure RMII pins
    SYS->GPA_MFPL |= SYS_GPA_MFPL_PA6MFP_EMAC_RMII_RXERR | SYS_GPA_MFPL_PA7MFP_EMAC_RMII_CRSDV;
    SYS->GPC_MFPL |= SYS_GPC_MFPL_PC6MFP_EMAC_RMII_RXD1 | SYS_GPC_MFPL_PC7MFP_EMAC_RMII_RXD0;
    SYS->GPC_MFPH |= SYS_GPC_MFPH_PC8MFP_EMAC_RMII_REFCLK;
    SYS->GPE_MFPH |= SYS_GPE_MFPH_PE8MFP_EMAC_RMII_MDC |
                     SYS_GPE_MFPH_PE9MFP_EMAC_RMII_MDIO |
                     SYS_GPE_MFPH_PE10MFP_EMAC_RMII_TXD0 |
                     SYS_GPE_MFPH_PE11MFP_EMAC_RMII_TXD1 |
                     SYS_GPE_MFPH_PE12MFP_EMAC_RMII_TXEN;

    // Enable high slew rate on all RMII TX output pins
    PE->SLEWCTL = (GPIO_SLEWCTL_HIGH << GPIO_SLEWCTL_HSREN10_Pos) |
                  (GPIO_SLEWCTL_HIGH << GPIO_SLEWCTL_HSREN11_Pos) |
                  (GPIO_SLEWCTL_HIGH << GPIO_SLEWCTL_HSREN12_Pos);

    /* Lock protected registers */
    SYS_LockReg();

}


// This sample application can response to ICMP ECHO packets (ping)
// IP address is configure with DHCP, but if a lease cannot be acquired, a static IP will be used.
int main(void)
{

    SYS_Init();
    UART_Open(UART0, 115200);
    Led_init();
    key_init();
    TIM_init();

    // Select RMII interface by default
    EMAC_Open(g_au8MacAddr);

    // Init phy
    EMAC_PhyInit();

    NVIC_EnableIRQ(EMAC_TX_IRQn);
    NVIC_EnableIRQ(EMAC_RX_IRQn);

    EMAC_ENABLE_RX();
    EMAC_ENABLE_TX();
    printf("emaec enable rx dfksa \r\n");
    if (dhcp_start() < 0)
    {
        // Cannot get a DHCP lease, use static IP.
        printf("DHCP failed, use static IP 192.168.10.10\n");
        g_au8IpAddr[0] = 0xC0;
        g_au8IpAddr[1] = 0xA8;
        g_au8IpAddr[2] = 0x0A;
        g_au8IpAddr[3] = 0x0A;
    }
这是原理图:


   
    while(1);

}

使用特权

评论回复
沙发
zhuotuzi| | 2022-6-9 20:15 | 只看该作者
代码这么贴不容易看,建议跌到代码专用框里。

使用特权

评论回复
板凳
caoqing| | 2022-6-9 21:53 | 只看该作者
改为固定IP试试

使用特权

评论回复
地板
caoqing| | 2022-6-9 21:56 | 只看该作者
该程序使用了中断的方式接收网卡数据包,在每次中断中只读取网卡的一个数据包。如果一次中断发生,而网卡内事实上可能存在有多个数据包,这样如果只读一个,最终导致网卡内数据包积压,所以会出现ping延迟的现象。如果数据包长期积压,还可能出现因网卡缓冲区满而无法接收数据的情况,由于无接收中断产生,控制器也不会处理积压数据的现象,从而发生网卡假死的现象,系统无法接收数据。

使用特权

评论回复
5
fst20220608|  楼主 | 2022-6-10 10:35 | 只看该作者
不知道怎么改成固定IP,我刚入手,还不太熟悉。

使用特权

评论回复
6
fst20220608|  楼主 | 2022-6-10 10:41 | 只看该作者
连不上以太网之后,我运行debug,发现一直卡在EMAC_PhyInit()里的while (!(EMAC_MdioRead(PHY_STATUS_REG, EMAC_PHY_ADDR) & PHY_STATUS_LINK_VALID))

使用特权

评论回复
7
caoqing| | 2022-6-10 20:10 | 只看该作者
#define USE_DHCP    //main.c里面,删除即可

使用特权

评论回复
8
fst20220608|  楼主 | 2022-6-11 19:11 | 只看该作者
caoqing 发表于 2022-6-10 20:10
#define USE_DHCP    //main.c里面,删除即可

我在代码里没看到有#define USE_DHCP这行啊
/*************************************************************************//**
* [url=home.php?mod=space&uid=288409]@file[/url]     main.c
* [url=home.php?mod=space&uid=895143]@version[/url]  V1.00
* @brief    This Ethernet sample tends to get a DHCP lease from DHCP
*           server. And use 192.168.10.10 as IP address it failed to
*           get a lease. After IP address configured, this sample can
*           reply to PING packets.
*
*
* [url=home.php?mod=space&uid=17282]@CopyRight[/url] (C) 2016 Nuvoton Technology Corp. All rights reserved.
*****************************************************************************/
#include <stdio.h>
#include "NuMicro.h"
#include "net.h"
#include "led.h"
#include "key.h"
// Our MAC address
uint8_t g_au8MacAddr[6] = {0x00, 0x00, 0x00, 0x59, 0x16, 0x88};
// Our IP address
uint8_t volatile g_au8IpAddr[4] = {0, 0, 0, 0};
uint8_t auPkt[1514];
uint32_t u32PktLen;

// Descriptor pointers holds current Tx and Rx used by IRQ handler here.
uint32_t u32CurrentTxDesc, u32CurrentRxDesc;

void TMR0_IRQHandler(void)
{
//    static unsigned char count = 0;

    //printf("%d sec\n", sec++);
    // clear timer interrupt flag
    TIMER_ClearIntFlag(TIMER0);
    PH0=!PH0;  
    PH1=!PH1;
    PH2=!PH2;
    printf("1s enter TIM interrupt\r\n");
//    __NOP();
}
/**
  * @brief  EMAC Tx interrupt handler.
  * @param  None
  * @return None
  */
void EMAC_TX_IRQHandler(void)
{
    // Clean up Tx resource occupied by previous sent packet(s)
    EMAC_SendPktDone();
}

/**
  * @brief  EMAC Rx interrupt handler.
  * @param  None
  * @return None
  */
void EMAC_RX_IRQHandler(void)
{
    int i=0;
//    while(1)
//    {
        printf("u32PktLen =%d\r\n",u32PktLen);
        // Check if there's any packets available
        if(EMAC_RecvPkt(auPkt, &u32PktLen) == 0)
            return;
            //break;
        // Process receive packet
        process_rx_packet(auPkt, u32PktLen);
        for(i=0;i<u32PktLen;i++)
        {
            if(auPkt[i]==192)
            {
                printf("IP address: %d.%d.%d.%d\n", auPkt[i], auPkt[i+1],auPkt[i+2], auPkt[i+3]);
            }
        }
        // Clean up Rx resource occupied by previous received packet
        EMAC_RecvPktDone();
//    }
}

void SYS_Init(void)
{

    /* Unlock protected registers */
    SYS_UnlockReg();

    /* Set XT1_OUT(PF.2) and XT1_IN(PF.3) to input mode */
    PF->MODE &= ~(GPIO_MODE_MODE2_Msk | GPIO_MODE_MODE3_Msk);

    /* Enable External XTAL (4~24 MHz) */
    CLK_EnableXtalRC(CLK_PWRCTL_HXTEN_Msk);

    /* Waiting for 12MHz clock ready */
    CLK_WaitClockReady( CLK_STATUS_HXTSTB_Msk);

    /* Switch HCLK clock source to HXT */
    CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_HXT,CLK_CLKDIV0_HCLK(1));

    /* Set core clock as PLL_CLOCK from PLL */
    CLK_SetCoreClock(FREQ_192MHZ);

    /* Set both PCLK0 and PCLK1 as HCLK/2 */
    CLK->PCLKDIV = CLK_PCLKDIV_APB0DIV_DIV2 | CLK_PCLKDIV_APB1DIV_DIV2;

    /* Enable IP clock */
    CLK_EnableModuleClock(UART0_MODULE);
    CLK_EnableModuleClock(EMAC_MODULE);

    /* Select IP clock source */
    CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART0SEL_HXT, CLK_CLKDIV0_UART0(1));

    // Configure MDC clock rate to HCLK / (127 + 1) = 1.5 MHz if system is running at 192 MHz
    CLK_SetModuleClock(EMAC_MODULE, 0, CLK_CLKDIV3_EMAC(127));

    /* Update System Core Clock */
    /* User can use SystemCoreClockUpdate() to calculate SystemCoreClock. */
    SystemCoreClockUpdate();

    /* Set GPB multi-function pins for UART0 RXD and TXD */
    SYS->GPB_MFPH &= ~(SYS_GPB_MFPH_PB12MFP_Msk | SYS_GPB_MFPH_PB13MFP_Msk);
    SYS->GPB_MFPH |= (SYS_GPB_MFPH_PB12MFP_UART0_RXD | SYS_GPB_MFPH_PB13MFP_UART0_TXD);
    // Configure RMII pins
    SYS->GPA_MFPL |= SYS_GPA_MFPL_PA6MFP_EMAC_RMII_RXERR | SYS_GPA_MFPL_PA7MFP_EMAC_RMII_CRSDV;
    SYS->GPC_MFPL |= SYS_GPC_MFPL_PC6MFP_EMAC_RMII_RXD1 | SYS_GPC_MFPL_PC7MFP_EMAC_RMII_RXD0;
    SYS->GPC_MFPH |= SYS_GPC_MFPH_PC8MFP_EMAC_RMII_REFCLK;
    SYS->GPE_MFPH |= SYS_GPE_MFPH_PE8MFP_EMAC_RMII_MDC |
                     SYS_GPE_MFPH_PE9MFP_EMAC_RMII_MDIO |
                     SYS_GPE_MFPH_PE10MFP_EMAC_RMII_TXD0 |
                     SYS_GPE_MFPH_PE11MFP_EMAC_RMII_TXD1 |
                     SYS_GPE_MFPH_PE12MFP_EMAC_RMII_TXEN;

    // Enable high slew rate on all RMII TX output pins
    PE->SLEWCTL = (GPIO_SLEWCTL_HIGH << GPIO_SLEWCTL_HSREN10_Pos) |
                  (GPIO_SLEWCTL_HIGH << GPIO_SLEWCTL_HSREN11_Pos) |
                  (GPIO_SLEWCTL_HIGH << GPIO_SLEWCTL_HSREN12_Pos);

    /* Lock protected registers */
    SYS_LockReg();

}


// This sample application can response to ICMP ECHO packets (ping)
// IP address is configure with DHCP, but if a lease cannot be acquired, a static IP will be used.
int main(void)
{

    SYS_Init();
    UART_Open(UART0, 115200);
    Led_init();
    printf("led init \r\n");
    key_init();
    //TIM_init();

    // Select RMII interface by default
    EMAC_Open(g_au8MacAddr);
    printf("EMAC open macaddr seccused\r\n");
    // Init phy
    EMAC_PhyInit();

    NVIC_EnableIRQ(EMAC_TX_IRQn);
    NVIC_EnableIRQ(EMAC_RX_IRQn);

    EMAC_ENABLE_RX();
    EMAC_ENABLE_TX();
    printf("emaec enable rx dfksa \r\n");
    if (dhcp_start() < 0)
    {
        // Cannot get a DHCP lease, use static IP.
        printf("DHCP failed, use static IP 192.168.10.10\n");
        g_au8IpAddr[0] = 0xC0;
        g_au8IpAddr[1] = 0xA8;
        g_au8IpAddr[2] = 0x0A;
        g_au8IpAddr[3] = 0x0A;
    }
   
    while(1);

}


/*** (C) COPYRIGHT 2016 Nuvoton Technology Corp. ***/

使用特权

评论回复
9
fst20220608|  楼主 | 2022-6-11 19:47 | 只看该作者
我读取EMAC_MdioRead(PHY_STATUS_REG, EMAC_PHY_ADDR);以太网连不上的时候,值是0x00007849,以太网连接成功的时候,值是0x0000786d,不懂为啥就是连不上

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

4

主题

11

帖子

0

粉丝