[bsp] update eth driver: fix phy init never timeout bug

This commit is contained in:
tanek liang 2017-12-07 13:14:41 +08:00
parent 049ed5047f
commit 5fd89e4461
2 changed files with 94 additions and 79 deletions

View File

@ -36,16 +36,17 @@
#define PHY_ADDRESS 0x02u
/* debug option */
//#define DEBUG
//#define ETH_RX_DUMP
//#define ETH_TX_DUMP
#ifdef DEBUG
#define ETH_PRINTF rt_kprintf
#else
#define ETH_PRINTF(...)
#endif
#define DBG_ENABLE
#define DBG_SECTION_NAME "[ETH]"
#define DBG_COLOR
#define DBG_LEVEL DBG_INFO
#include <rtdbg.h>
#define MAX_ADDR_LEN 6
@ -380,21 +381,31 @@ static void _enet_config(void)
/* Set SMI to get PHY link status. */
sysClock = CLOCK_GetFreq(kCLOCK_AhbClk);
status = PHY_Init(imxrt_eth_device.enet_base, PHY_ADDRESS, sysClock);
while (status != kStatus_Success)
{
ETH_PRINTF("\r\nPHY Auto-negotiation failed. Please check the cable connection and link partner setting.\r\n");
status = PHY_Init(imxrt_eth_device.enet_base, PHY_ADDRESS, sysClock);
}
PHY_GetLinkStatus(imxrt_eth_device.enet_base, PHY_ADDRESS, &link);
if (link)
status = PHY_Init(imxrt_eth_device.enet_base, PHY_ADDRESS, sysClock);
if (status == kStatus_Success)
{
/* Get the actual PHY link speed. */
PHY_GetLinkSpeedDuplex(imxrt_eth_device.enet_base, PHY_ADDRESS, &speed, &duplex);
/* Change the MII speed and duplex for actual link status. */
config.miiSpeed = (enet_mii_speed_t)speed;
config.miiDuplex = (enet_mii_duplex_t)duplex;
PHY_GetLinkStatus(imxrt_eth_device.enet_base, PHY_ADDRESS, &link);
if (link)
{
/* Get the actual PHY link speed. */
PHY_GetLinkSpeedDuplex(imxrt_eth_device.enet_base, PHY_ADDRESS, &speed, &duplex);
/* Change the MII speed and duplex for actual link status. */
config.miiSpeed = (enet_mii_speed_t)speed;
config.miiDuplex = (enet_mii_duplex_t)duplex;
}
dbg_log(DBG_LOG, "PHY Auto-negotiation success.\n");
eth_device_linkchange(&imxrt_eth_device.parent, RT_TRUE);
}
else
{
config.miiSpeed = kENET_MiiSpeed10M;
config.miiDuplex = kENET_MiiHalfDuplex;
dbg_log(DBG_WARNING, "PHY Auto-negotiation failed. Please check the cable connection and link partner setting.\n");
eth_device_linkchange(&imxrt_eth_device.parent, RT_FALSE);
}
ENET_Init(imxrt_eth_device.enet_base, &imxrt_eth_device.enet_handle, &config, &buffConfig, &imxrt_eth_device.dev_addr[0], sysClock);
@ -416,33 +427,33 @@ static rt_err_t rt_imxrt_eth_init(rt_device_t dev)
static rt_err_t rt_imxrt_eth_open(rt_device_t dev, rt_uint16_t oflag)
{
ETH_PRINTF("rt_imxrt_eth_open...\n");
dbg_log(DBG_LOG, "rt_imxrt_eth_open...\n");
return RT_EOK;
}
static rt_err_t rt_imxrt_eth_close(rt_device_t dev)
{
ETH_PRINTF("rt_imxrt_eth_close...\n");
dbg_log(DBG_LOG, "rt_imxrt_eth_close...\n");
return RT_EOK;
}
static rt_size_t rt_imxrt_eth_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size)
{
ETH_PRINTF("rt_imxrt_eth_read...\n");
dbg_log(DBG_LOG, "rt_imxrt_eth_read...\n");
rt_set_errno(-RT_ENOSYS);
return 0;
}
static rt_size_t rt_imxrt_eth_write (rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size)
{
ETH_PRINTF("rt_imxrt_eth_write...\n");
dbg_log(DBG_LOG, "rt_imxrt_eth_write...\n");
rt_set_errno(-RT_ENOSYS);
return 0;
}
static rt_err_t rt_imxrt_eth_control(rt_device_t dev, int cmd, void *args)
{
ETH_PRINTF("rt_imxrt_eth_control...\n");
dbg_log(DBG_LOG, "rt_imxrt_eth_control...\n");
switch(cmd)
{
case NIOCTL_GADDR:
@ -468,7 +479,7 @@ rt_err_t rt_imxrt_eth_tx( rt_device_t dev, struct pbuf* p)
RT_ASSERT(p != NULL);
RT_ASSERT(enet_handle != RT_NULL);
ETH_PRINTF("rt_imxrt_eth_tx: %d\n", p->len);
dbg_log(DBG_LOG, "rt_imxrt_eth_tx: %d\n", p->len);
#ifdef ETH_TX_DUMP
{
@ -477,11 +488,11 @@ rt_err_t rt_imxrt_eth_tx( rt_device_t dev, struct pbuf* p)
buf = (uint8_t *)p->payload;
for (i = 0; i < p->len; i++)
{
ETH_PRINTF("%02X ", buf[i]);
dbg_log(DBG_LOG, "%02X ", buf[i]);
if (i % 16 == 15)
ETH_PRINTF("\n");
dbg_log(DBG_LOG, "\n");
}
ETH_PRINTF("\n");
dbg_log(DBG_LOG, "\n");
}
#endif
@ -532,28 +543,28 @@ struct pbuf *rt_imxrt_eth_rx(rt_device_t dev)
buf = (uint8_t *)p->payload;
for (i = 0; i < p->len; i++)
{
ETH_PRINTF("%02X ", buf[i]);
dbg_log(DBG_LOG, "%02X ", buf[i]);
if (i % 16 == 15)
ETH_PRINTF("\n");
dbg_log(DBG_LOG, "\n");
}
ETH_PRINTF("\n");
dbg_log(DBG_LOG, "\n");
#endif
return p;
}
else
{
ETH_PRINTF(" A frame read failed\n");
dbg_log(DBG_LOG, " A frame read failed\n");
pbuf_free(p);
}
}
else
{
ETH_PRINTF(" pbuf_alloc faild\n");
dbg_log(DBG_LOG, " pbuf_alloc faild\n");
}
}
else if (status == kStatus_ENET_RxFrameError)
{
ETH_PRINTF("ENET_GetRxFrameSize: kStatus_ENET_RxFrameError\n");
dbg_log(DBG_WARNING, "ENET_GetRxFrameSize: kStatus_ENET_RxFrameError\n");
/* Update the received buffer when error happened. */
/* Get the error information of the received g_frame. */
ENET_GetRxErrBeforeReadFrame(enet_handle, error_statistic);
@ -591,20 +602,20 @@ static int rt_hw_imxrt_eth_init(void)
imxrt_eth_device.parent.eth_rx = rt_imxrt_eth_rx;
imxrt_eth_device.parent.eth_tx = rt_imxrt_eth_tx;
ETH_PRINTF("sem init: tx_wait\r\n");
dbg_log(DBG_LOG, "sem init: tx_wait\r\n");
/* init tx semaphore */
rt_sem_init(&imxrt_eth_device.tx_wait, "tx_wait", 0, RT_IPC_FLAG_FIFO);
/* register eth device */
ETH_PRINTF("eth_device_init start\r\n");
dbg_log(DBG_LOG, "eth_device_init start\r\n");
state = eth_device_init(&(imxrt_eth_device.parent), "e0");
if (RT_EOK == state)
{
ETH_PRINTF("eth_device_init success\r\n");
dbg_log(DBG_LOG, "eth_device_init success\r\n");
}
else
{
ETH_PRINTF("eth_device_init faild: %d\r\n", state);
dbg_log(DBG_LOG, "eth_device_init faild: %d\r\n", state);
}
return state;
}

View File

@ -29,12 +29,13 @@
*/
#include "fsl_phy.h"
#include <rtthread.h>
/*******************************************************************************
* Definitions
******************************************************************************/
/*! @brief Defines the timeout macro. */
#define PHY_TIMEOUT_COUNT 0x3FFFFFFU
#define PHY_TIMEOUT_COUNT 0xFFFFU
/*******************************************************************************
* Prototypes
@ -90,7 +91,7 @@ status_t PHY_Init(ENET_Type *base, uint32_t phyAddr, uint32_t srcClock_Hz)
}
/* Reset PHY. */
counter = PHY_TIMEOUT_COUNT;
counter = 6;
result = PHY_Write(base, phyAddr, PHY_BASICCONTROL_REG, PHY_BCTL_RESET_MASK);
if (result == kStatus_Success)
{
@ -137,6 +138,9 @@ status_t PHY_Init(ENET_Type *base, uint32_t phyAddr, uint32_t srcClock_Hz)
}
}
rt_kprintf("[PHY] wait autonegotiation complete...\n");
rt_thread_delay(RT_TICK_PER_SECOND);
if (!counter)
{
return kStatus_PHY_AutoNegotiateFail;