提交 5fd89e44 编写于 作者: T tanek liang

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

上级 049ed504
...@@ -36,16 +36,17 @@ ...@@ -36,16 +36,17 @@
#define PHY_ADDRESS 0x02u #define PHY_ADDRESS 0x02u
/* debug option */ /* debug option */
//#define DEBUG
//#define ETH_RX_DUMP //#define ETH_RX_DUMP
//#define ETH_TX_DUMP //#define ETH_TX_DUMP
#ifdef DEBUG #define DBG_ENABLE
#define ETH_PRINTF rt_kprintf #define DBG_SECTION_NAME "[ETH]"
#else #define DBG_COLOR
#define ETH_PRINTF(...) #define DBG_LEVEL DBG_INFO
#endif #include <rtdbg.h>
#define MAX_ADDR_LEN 6 #define MAX_ADDR_LEN 6
...@@ -380,13 +381,11 @@ static void _enet_config(void) ...@@ -380,13 +381,11 @@ static void _enet_config(void)
/* Set SMI to get PHY link status. */ /* Set SMI to get PHY link status. */
sysClock = CLOCK_GetFreq(kCLOCK_AhbClk); sysClock = CLOCK_GetFreq(kCLOCK_AhbClk);
status = PHY_Init(imxrt_eth_device.enet_base, PHY_ADDRESS, sysClock); 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);
}
if (status == kStatus_Success)
{
PHY_GetLinkStatus(imxrt_eth_device.enet_base, PHY_ADDRESS, &link); PHY_GetLinkStatus(imxrt_eth_device.enet_base, PHY_ADDRESS, &link);
if (link) if (link)
{ {
...@@ -397,6 +396,18 @@ static void _enet_config(void) ...@@ -397,6 +396,18 @@ static void _enet_config(void)
config.miiDuplex = (enet_mii_duplex_t)duplex; 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); ENET_Init(imxrt_eth_device.enet_base, &imxrt_eth_device.enet_handle, &config, &buffConfig, &imxrt_eth_device.dev_addr[0], sysClock);
ENET_SetCallback(&imxrt_eth_device.enet_handle, _enet_callback, &imxrt_eth_device); ENET_SetCallback(&imxrt_eth_device.enet_handle, _enet_callback, &imxrt_eth_device);
ENET_ActiveRead(imxrt_eth_device.enet_base); ENET_ActiveRead(imxrt_eth_device.enet_base);
...@@ -416,33 +427,33 @@ static rt_err_t rt_imxrt_eth_init(rt_device_t dev) ...@@ -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) 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; return RT_EOK;
} }
static rt_err_t rt_imxrt_eth_close(rt_device_t dev) 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; 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) 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); rt_set_errno(-RT_ENOSYS);
return 0; 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) 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); rt_set_errno(-RT_ENOSYS);
return 0; return 0;
} }
static rt_err_t rt_imxrt_eth_control(rt_device_t dev, int cmd, void *args) 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) switch(cmd)
{ {
case NIOCTL_GADDR: case NIOCTL_GADDR:
...@@ -468,7 +479,7 @@ rt_err_t rt_imxrt_eth_tx( rt_device_t dev, struct pbuf* p) ...@@ -468,7 +479,7 @@ rt_err_t rt_imxrt_eth_tx( rt_device_t dev, struct pbuf* p)
RT_ASSERT(p != NULL); RT_ASSERT(p != NULL);
RT_ASSERT(enet_handle != RT_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 #ifdef ETH_TX_DUMP
{ {
...@@ -477,11 +488,11 @@ rt_err_t rt_imxrt_eth_tx( rt_device_t dev, struct pbuf* p) ...@@ -477,11 +488,11 @@ rt_err_t rt_imxrt_eth_tx( rt_device_t dev, struct pbuf* p)
buf = (uint8_t *)p->payload; buf = (uint8_t *)p->payload;
for (i = 0; i < p->len; i++) for (i = 0; i < p->len; i++)
{ {
ETH_PRINTF("%02X ", buf[i]); dbg_log(DBG_LOG, "%02X ", buf[i]);
if (i % 16 == 15) if (i % 16 == 15)
ETH_PRINTF("\n"); dbg_log(DBG_LOG, "\n");
} }
ETH_PRINTF("\n"); dbg_log(DBG_LOG, "\n");
} }
#endif #endif
...@@ -532,28 +543,28 @@ struct pbuf *rt_imxrt_eth_rx(rt_device_t dev) ...@@ -532,28 +543,28 @@ struct pbuf *rt_imxrt_eth_rx(rt_device_t dev)
buf = (uint8_t *)p->payload; buf = (uint8_t *)p->payload;
for (i = 0; i < p->len; i++) for (i = 0; i < p->len; i++)
{ {
ETH_PRINTF("%02X ", buf[i]); dbg_log(DBG_LOG, "%02X ", buf[i]);
if (i % 16 == 15) if (i % 16 == 15)
ETH_PRINTF("\n"); dbg_log(DBG_LOG, "\n");
} }
ETH_PRINTF("\n"); dbg_log(DBG_LOG, "\n");
#endif #endif
return p; return p;
} }
else else
{ {
ETH_PRINTF(" A frame read failed\n"); dbg_log(DBG_LOG, " A frame read failed\n");
pbuf_free(p); pbuf_free(p);
} }
} }
else else
{ {
ETH_PRINTF(" pbuf_alloc faild\n"); dbg_log(DBG_LOG, " pbuf_alloc faild\n");
} }
} }
else if (status == kStatus_ENET_RxFrameError) 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. */ /* Update the received buffer when error happened. */
/* Get the error information of the received g_frame. */ /* Get the error information of the received g_frame. */
ENET_GetRxErrBeforeReadFrame(enet_handle, error_statistic); ENET_GetRxErrBeforeReadFrame(enet_handle, error_statistic);
...@@ -591,20 +602,20 @@ static int rt_hw_imxrt_eth_init(void) ...@@ -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_rx = rt_imxrt_eth_rx;
imxrt_eth_device.parent.eth_tx = rt_imxrt_eth_tx; 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 */ /* init tx semaphore */
rt_sem_init(&imxrt_eth_device.tx_wait, "tx_wait", 0, RT_IPC_FLAG_FIFO); rt_sem_init(&imxrt_eth_device.tx_wait, "tx_wait", 0, RT_IPC_FLAG_FIFO);
/* register eth device */ /* 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"); state = eth_device_init(&(imxrt_eth_device.parent), "e0");
if (RT_EOK == state) if (RT_EOK == state)
{ {
ETH_PRINTF("eth_device_init success\r\n"); dbg_log(DBG_LOG, "eth_device_init success\r\n");
} }
else 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; return state;
} }
......
...@@ -29,12 +29,13 @@ ...@@ -29,12 +29,13 @@
*/ */
#include "fsl_phy.h" #include "fsl_phy.h"
#include <rtthread.h>
/******************************************************************************* /*******************************************************************************
* Definitions * Definitions
******************************************************************************/ ******************************************************************************/
/*! @brief Defines the timeout macro. */ /*! @brief Defines the timeout macro. */
#define PHY_TIMEOUT_COUNT 0x3FFFFFFU #define PHY_TIMEOUT_COUNT 0xFFFFU
/******************************************************************************* /*******************************************************************************
* Prototypes * Prototypes
...@@ -90,7 +91,7 @@ status_t PHY_Init(ENET_Type *base, uint32_t phyAddr, uint32_t srcClock_Hz) ...@@ -90,7 +91,7 @@ status_t PHY_Init(ENET_Type *base, uint32_t phyAddr, uint32_t srcClock_Hz)
} }
/* Reset PHY. */ /* Reset PHY. */
counter = PHY_TIMEOUT_COUNT; counter = 6;
result = PHY_Write(base, phyAddr, PHY_BASICCONTROL_REG, PHY_BCTL_RESET_MASK); result = PHY_Write(base, phyAddr, PHY_BASICCONTROL_REG, PHY_BCTL_RESET_MASK);
if (result == kStatus_Success) if (result == kStatus_Success)
{ {
...@@ -137,6 +138,9 @@ status_t PHY_Init(ENET_Type *base, uint32_t phyAddr, uint32_t srcClock_Hz) ...@@ -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) if (!counter)
{ {
return kStatus_PHY_AutoNegotiateFail; return kStatus_PHY_AutoNegotiateFail;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册