[bsp][imxrt1052-evk] update eth driver

- fixed eth send frame bug
- code clean
This commit is contained in:
liang yongxiang 2018-04-08 17:16:49 +08:00
parent 9efc027792
commit ae1536b2a0
2 changed files with 331 additions and 306 deletions

View File

@ -1345,7 +1345,13 @@ status_t ENET_SendFrame(ENET_Type *base, enet_handle_t *handle, const uint8_t *d
#else
address = (uint32_t)curBuffDescrip->buffer;
#endif /* FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET */
memcpy((void *)address, data, length);
{
// Change SDK to reduce memory copy
extern void pbuf2mem(const uint8_t *data, void *dataptr, uint32_t len);
pbuf2mem(data, (void *)address, length);
}
//memcpy((void *)address, data, length);
/* Set data length. */
curBuffDescrip->length = length;
#ifdef ENET_ENHANCEDBUFFERDESCRIPTOR_MODE

View File

@ -16,7 +16,7 @@
#include <rtdevice.h>
#ifdef RT_USING_FINSH
#include <finsh.h>
#include <finsh.h>
#endif
#include "fsl_enet.h"
@ -73,18 +73,18 @@ ALIGN(ENET_BUFF_ALIGNMENT) rt_uint8_t g_rxDataBuff[ENET_RXBD_NUM][RT_ALIGN(ENET_
static struct rt_imxrt_eth imxrt_eth_device;
void _enet_rx_callback(struct rt_imxrt_eth * eth)
void _enet_rx_callback(struct rt_imxrt_eth *eth)
{
rt_err_t result;
ENET_DisableInterrupts(eth->enet_base, kENET_RxFrameInterrupt);
result = eth_device_ready(&(eth->parent));
if( result != RT_EOK )
rt_kprintf("RX err =%d\n", result );
if (result != RT_EOK)
rt_kprintf("RX err =%d\n", result);
}
void _enet_tx_callback(struct rt_imxrt_eth * eth)
void _enet_tx_callback(struct rt_imxrt_eth *eth)
{
if (eth->tx_is_waiting == RT_TRUE)
{
@ -95,7 +95,7 @@ void _enet_tx_callback(struct rt_imxrt_eth * eth)
void _enet_callback(ENET_Type *base, enet_handle_t *handle, enet_event_t event, void *userData)
{
switch(event)
switch (event)
{
case kENET_RxEvent:
@ -314,8 +314,6 @@ static void _enet_io_init(void)
Pull / Keep Select Field: Pull
Pull Up / Down Config. Field: 100K Ohm Pull Up
Hyst. Enable Field: Hysteresis Disabled */
}
static void _enet_clk_init(void)
@ -353,7 +351,8 @@ static void _enet_config(void)
uint32_t sysClock;
/* prepare the buffer configuration. */
enet_buffer_config_t buffConfig = {
enet_buffer_config_t buffConfig =
{
ENET_RXBD_NUM,
ENET_TXBD_NUM,
SDK_SIZEALIGN(ENET_RXBUFF_SIZE, ENET_BUFF_ALIGNMENT),
@ -390,6 +389,43 @@ static void _enet_config(void)
ENET_ActiveRead(imxrt_eth_device.enet_base);
}
#if defined(ETH_RX_DUMP) || defined(ETH_TX_DUMP)
static void packet_dump(const char *msg, const struct pbuf *p)
{
const struct pbuf *q;
rt_uint32_t i, j;
rt_uint8_t *ptr;
rt_kprintf("%s %d byte\n", msg, p->tot_len);
i = 0;
for (q = p; q != RT_NULL; q = q->next)
{
ptr = q->payload;
for (j = 0; j < q->len; j++)
{
if ((i % 8) == 0)
{
rt_kprintf(" ");
}
if ((i % 16) == 0)
{
rt_kprintf("\r\n");
}
rt_kprintf("%02x ", *ptr);
i++;
ptr++;
}
}
rt_kprintf("\n\n");
}
#else
#define packet_dump(...)
#endif /* dump */
/* initialize the interface */
static rt_err_t rt_imxrt_eth_init(rt_device_t dev)
{
@ -411,14 +447,14 @@ static rt_err_t rt_imxrt_eth_close(rt_device_t dev)
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)
{
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)
static rt_size_t rt_imxrt_eth_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
{
dbg_log(DBG_LOG, "rt_imxrt_eth_write...\n");
rt_set_errno(-RT_ENOSYS);
@ -428,11 +464,11 @@ static rt_size_t rt_imxrt_eth_write (rt_device_t dev, rt_off_t pos, const void*
static rt_err_t rt_imxrt_eth_control(rt_device_t dev, int cmd, void *args)
{
dbg_log(DBG_LOG, "rt_imxrt_eth_control...\n");
switch(cmd)
switch (cmd)
{
case NIOCTL_GADDR:
/* get mac address */
if(args) rt_memcpy(args, imxrt_eth_device.dev_addr, 6);
if (args) rt_memcpy(args, imxrt_eth_device.dev_addr, 6);
else return -RT_ERROR;
break;
@ -445,7 +481,7 @@ static rt_err_t rt_imxrt_eth_control(rt_device_t dev, int cmd, void *args)
/* ethernet device interface */
/* transmit packet. */
rt_err_t rt_imxrt_eth_tx( rt_device_t dev, struct pbuf* p)
rt_err_t rt_imxrt_eth_tx(rt_device_t dev, struct pbuf *p)
{
rt_err_t result = RT_EOK;
enet_handle_t * enet_handle = &imxrt_eth_device.enet_handle;
@ -456,42 +492,38 @@ rt_err_t rt_imxrt_eth_tx( rt_device_t dev, struct pbuf* p)
dbg_log(DBG_LOG, "rt_imxrt_eth_tx: %d\n", p->len);
#ifdef ETH_TX_DUMP
{
int i;
uint8_t * buf;
buf = (uint8_t *)p->payload;
for (i = 0; i < p->len; i++)
{
dbg_log(DBG_LOG, "%02X ", buf[i]);
if (i % 16 == 15)
dbg_log(DBG_LOG, "\n");
}
dbg_log(DBG_LOG, "\n");
}
packet_dump("send", p);
#endif
do
{
result = ENET_SendFrame(imxrt_eth_device.enet_base, enet_handle, p->payload, p->len);
result = ENET_SendFrame(imxrt_eth_device.enet_base, enet_handle, (const uint8_t *)p, p->tot_len);
if (result == kStatus_ENET_TxFrameBusy)
{
imxrt_eth_device.tx_is_waiting = RT_TRUE;
rt_sem_take(&imxrt_eth_device.tx_wait, RT_WAITING_FOREVER);
}
} while (result == kStatus_ENET_TxFrameBusy);
}
while (result == kStatus_ENET_TxFrameBusy);
return RT_EOK;
}
void pbuf2mem(const uint8_t *data, void *dataptr, uint32_t len)
{
pbuf_copy_partial((const struct pbuf *)data, dataptr, len, 0);
}
/* reception packet. */
struct pbuf *rt_imxrt_eth_rx(rt_device_t dev)
{
uint32_t length = 0;
status_t status;
struct pbuf* p = RT_NULL;
enet_handle_t * enet_handle = &imxrt_eth_device.enet_handle;
struct pbuf *p = RT_NULL;
enet_handle_t *enet_handle = &imxrt_eth_device.enet_handle;
ENET_Type *enet_base = imxrt_eth_device.enet_base;
enet_data_error_stats_t *error_statistic = &imxrt_eth_device.error_statistic;
@ -509,20 +541,9 @@ struct pbuf *rt_imxrt_eth_rx(rt_device_t dev)
status = ENET_ReadFrame(enet_base, enet_handle, p->payload, length);
if (status == kStatus_Success)
{
#ifdef ETH_RX_DUMP
uint8_t *buf;
int i;
ETH_PRINTF("A frame received. the length:%d\n", p->len);
buf = (uint8_t *)p->payload;
for (i = 0; i < p->len; i++)
{
dbg_log(DBG_LOG, "%02X ", buf[i]);
if (i % 16 == 15)
dbg_log(DBG_LOG, "\n");
}
dbg_log(DBG_LOG, "\n");
#endif
#ifdef ETH_RX_DUMP
packet_dump("recv", p);
#endif
return p;
}
else
@ -552,7 +573,6 @@ struct pbuf *rt_imxrt_eth_rx(rt_device_t dev)
static void phy_monitor_thread_entry(void *parameter)
{
phy_speed_t speed;
phy_duplex_t duplex;
bool link = false;
@ -629,12 +649,12 @@ static int rt_hw_imxrt_eth_init(void)
/* OUI 00-80-E1 STMICROELECTRONICS. */
imxrt_eth_device.dev_addr[0] = 0x00;
imxrt_eth_device.dev_addr[1] = 0x80;
imxrt_eth_device.dev_addr[2] = 0xE1;
imxrt_eth_device.dev_addr[1] = 0x04;
imxrt_eth_device.dev_addr[2] = 0x9F;
/* generate MAC addr from 96bit unique ID (only for test). */
imxrt_eth_device.dev_addr[3] = 0x12;
imxrt_eth_device.dev_addr[4] = 0x34;
imxrt_eth_device.dev_addr[5] = 0x56;
imxrt_eth_device.dev_addr[3] = 0x05;
imxrt_eth_device.dev_addr[4] = 0x44;
imxrt_eth_device.dev_addr[5] = 0xE5;
imxrt_eth_device.speed = kENET_MiiSpeed100M;
imxrt_eth_device.duplex = kENET_MiiFullDuplex;
@ -891,7 +911,6 @@ void enet_rx_stat(void)
void enet_buf_info(void)
{
int i = 0;
for (i = 0; i < ENET_RXBD_NUM; i++)
{