summaryrefslogtreecommitdiff
path: root/bsp/hpmicro/libraries/drivers/drv_enet.c
blob: 6ab01d76d2040811f10f49c6b3c7b0d9ccbd15ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
/*
 * Copyright (c) 2021-2025 HPMicro
 *
 * SPDX-License-Identifier: BSD-3-Clause
 *
 * Change Logs:
 * Date         Author      Notes
 * 2022-01-11   Jiading     First version
 * 2022-07-10   Jiading     Driver optimization for multiple instances
 * 2025-04-15   Jiading     Fixed an issue that received data is probabilistically overwritten
 * 2025-12-01   Jiading     Optimized the driver for PTP initialization
 * 2026-07-18   Jiading     Align ENET HW checksum with RT_LWIP_USING_HW_CHECKSUM
 * 2026-07-18   Jiading     Align ENET ISR and MMC interrupt init with hpm_sdk v1.12.0
 * 2026-10-08   Jiading     Use enet_set_hw_checksum_config() instead of register poke
 */

#include <rtdevice.h>

#ifdef BSP_USING_ETH
#ifdef LWIP_NO_RX_THREAD
#error "LWIP_NO_RX_THREAD is not supported in current implementation!"
#endif
#include <rtdbg.h>
#include "rtconfig.h"
#include "drv_enet.h"
#include "hpm_otp_drv.h"

#if defined(LWIP_PTP) && LWIP_PTP
#include "lwip_ptp_tx_ts.h"
#include "hpm_soc.h"
#endif

#ifdef BSP_USING_ETH0

ATTR_PLACE_AT_NONCACHEABLE_WITH_ALIGNMENT(ENET_SOC_DESC_ADDR_ALIGNMENT)
__RW enet_rx_desc_t enet0_dma_rx_desc_tab[ENET0_RX_BUFF_COUNT]; /* Ethernet0 Rx DMA Descriptor */

ATTR_PLACE_AT_NONCACHEABLE_WITH_ALIGNMENT(ENET_SOC_DESC_ADDR_ALIGNMENT)
__RW enet_tx_desc_t enet0_dma_tx_desc_tab[ENET0_TX_BUFF_COUNT]; /* Ethernet0 Tx DMA Descriptor */

ATTR_PLACE_AT_WITH_ALIGNMENT(".fast_ram", ENET_SOC_BUFF_ADDR_ALIGNMENT)
__RW uint8_t enet0_rx_buff[ENET0_RX_BUFF_COUNT][ENET0_RX_BUFF_SIZE]; /* Ethernet0 Receive Buffer */

ATTR_PLACE_AT_WITH_ALIGNMENT(".fast_ram", ENET_SOC_BUFF_ADDR_ALIGNMENT)
__RW uint8_t enet0_tx_buff[ENET0_TX_BUFF_COUNT][ENET0_TX_BUFF_SIZE]; /* Ethernet0 Transmit Buffer */

LWIP_MEMPOOL_DECLARE(enet0_rx_pool, ENET0_RX_BUFF_COUNT, sizeof(my_custom_pbuf_t), "Custom RX PBUF pool");
static enet_frame_t enet0_frame[ENET0_RX_BUFF_COUNT] = {0};

struct eth_device eth0_dev;
static enet_device enet0_dev;
static enet_buff_config_t enet0_rx_buff_cfg = {.buffer = (uint32_t)enet0_rx_buff,
                                               .count  = ENET0_RX_BUFF_COUNT,
                                               .size   = ENET0_RX_BUFF_SIZE
                                              };

static enet_buff_config_t enet0_tx_buff_cfg = {.buffer = (uint32_t)enet0_tx_buff,
                                               .count  = ENET0_TX_BUFF_COUNT,
                                               .size   = ENET0_TX_BUFF_SIZE
                                              };

#if defined(__USE_ENET_PTP) && __USE_ENET_PTP
static enet_ptp_ts_update_t ptp_timestamp0 = {0, 0};
static enet_ptp_config_t ptp_config0 = {.update_method = enet_ptp_time_fine_update};
#endif

static hpm_enet_t enet0 = {.name            = "E0",
                           .base            = HPM_ENET0,
                           .clock_name      = clock_eth0,
                           .irq_num         = IRQn_ENET0,
                           .inf             = BOARD_ENET0_INF,
                           .eth_dev         = &eth0_dev,
                           .enet_dev        = &enet0_dev,
                           .rx_buff_cfg     = &enet0_rx_buff_cfg,
                           .tx_buff_cfg     = &enet0_tx_buff_cfg,
                           .dma_rx_desc_tab = enet0_dma_rx_desc_tab,
                           .dma_tx_desc_tab = enet0_dma_tx_desc_tab,
                           .frame           = enet0_frame,
#if !BOARD_ENET0_INF
                           .int_refclk      = BOARD_ENET0_INT_REF_CLK,
#else
                           .tx_delay        = BOARD_ENET0_TX_DLY,
                           .rx_delay        = BOARD_ENET0_RX_DLY,
#endif

#if defined(__USE_ENET_PTP) && __USE_ENET_PTP
                           .ptp_clk_src     = BOARD_ENET0_PTP_CLOCK,
                           .ptp_config      = &ptp_config0,
                           .ptp_timestamp   = &ptp_timestamp0
#endif
                          };
#endif

mac_init_t mac_init[] = {
    {MAC0_ADDR0, MAC0_ADDR1, MAC0_ADDR2, MAC0_ADDR3, MAC0_ADDR4, MAC0_ADDR5},
    {MAC1_ADDR0, MAC1_ADDR1, MAC1_ADDR2, MAC1_ADDR3, MAC1_ADDR4, MAC1_ADDR5}
};

#ifdef BSP_USING_ETH1

ATTR_PLACE_AT_NONCACHEABLE_WITH_ALIGNMENT(ENET_SOC_DESC_ADDR_ALIGNMENT)
__RW enet_rx_desc_t enet1_dma_rx_desc_tab[ENET1_RX_BUFF_COUNT]; /* Ethernet1 Rx DMA Descriptor */

ATTR_PLACE_AT_NONCACHEABLE_WITH_ALIGNMENT(ENET_SOC_DESC_ADDR_ALIGNMENT)
__RW enet_tx_desc_t enet1_dma_tx_desc_tab[ENET1_TX_BUFF_COUNT]; /* Ethernet1 Tx DMA Descriptor */

ATTR_PLACE_AT_WITH_ALIGNMENT(".fast_ram", ENET_SOC_BUFF_ADDR_ALIGNMENT)
__RW uint8_t enet1_rx_buff[ENET1_RX_BUFF_COUNT][ENET1_RX_BUFF_SIZE]; /* Ethernet1 Receive Buffer */

ATTR_PLACE_AT_WITH_ALIGNMENT(".fast_ram", ENET_SOC_BUFF_ADDR_ALIGNMENT)
__RW uint8_t enet1_tx_buff[ENET1_TX_BUFF_COUNT][ENET1_TX_BUFF_SIZE]; /* Ethernet1 Transmit Buffer */

LWIP_MEMPOOL_DECLARE(enet1_rx_pool, ENET1_RX_BUFF_COUNT, sizeof(my_custom_pbuf_t), "Custom RX PBUF pool");
static enet_frame_t enet1_frame[ENET1_RX_BUFF_COUNT] = {0};

struct eth_device eth1_dev;
static enet_device enet1_dev;
static enet_buff_config_t enet1_rx_buff_cfg = {.buffer = (uint32_t)enet1_rx_buff,
                                               .count  = ENET1_RX_BUFF_COUNT,
                                               .size   = ENET1_RX_BUFF_SIZE
                                              };

static enet_buff_config_t enet1_tx_buff_cfg = {.buffer = (uint32_t)enet1_tx_buff,
                                               .count  = ENET1_TX_BUFF_COUNT,
                                               .size   = ENET1_TX_BUFF_SIZE
                                              };

#if defined(__USE_ENET_PTP) && __USE_ENET_PTP
static enet_ptp_ts_update_t ptp_timestamp1 = {0, 0};
static enet_ptp_config_t ptp_config1 = {.update_method = enet_ptp_time_fine_update};
#endif

static hpm_enet_t enet1 = {.name            = "E1",
                           .base            = HPM_ENET1,
                           .clock_name      = clock_eth1,
                           .irq_num         = IRQn_ENET1,
                           .inf             = BOARD_ENET1_INF,
                           .eth_dev         = &eth1_dev,
                           .enet_dev        = &enet1_dev,
                           .rx_buff_cfg     = &enet1_rx_buff_cfg,
                           .tx_buff_cfg     = &enet1_tx_buff_cfg,
                           .dma_rx_desc_tab = enet1_dma_rx_desc_tab,
                           .dma_tx_desc_tab = enet1_dma_tx_desc_tab,
                           .frame           = enet1_frame,

#if !BOARD_ENET1_INF
                           .int_refclk      = BOARD_ENET1_INT_REF_CLK,
#else
                           .tx_delay        = BOARD_ENET1_TX_DLY,
                           .rx_delay        = BOARD_ENET1_RX_DLY,
#endif

#if defined(__USE_ENET_PTP) && __USE_ENET_PTP
                           .ptp_clk_src     = BOARD_ENET1_PTP_CLOCK,
                           .ptp_config      = &ptp_config1,
                           .ptp_timestamp   = &ptp_timestamp1
#endif
                          };
#endif

static hpm_enet_t *s_geths[] = {
#ifdef BSP_USING_ETH0
       &enet0,
#endif

#ifdef BSP_USING_ETH1
       &enet1
#endif
};

void free_rx_dma_descriptor(void *p)
{
    enet_frame_t *frame;

    /* Release descriptors to DMA */
    frame = (enet_frame_t *)p;

    /* Set Own bit in Rx descriptors: gives the buffers back to DMA */
    enet_rx_desc_t *dma_rx_desc = frame->rx_desc;

    for (uint32_t i = 0; i < frame->seg; i++) {
        dma_rx_desc->rdes0_bm.own = 1;
        dma_rx_desc = (enet_rx_desc_t *)(dma_rx_desc->rdes3_bm.next_desc);
    }

    /* Clear Segment_Count */
    frame->seg = 0;
    frame->used = 0;
}

#ifdef BSP_USING_ETH0
void enet0_pbuf_free_custom(struct pbuf *p)
{
    SYS_ARCH_DECL_PROTECT(old_level);
    my_custom_pbuf_t *my_pbuf = (my_custom_pbuf_t *)p;

    SYS_ARCH_PROTECT(old_level);
    free_rx_dma_descriptor((void *)my_pbuf->dma_descriptor);

    LWIP_MEMPOOL_FREE(enet0_rx_pool, my_pbuf);
    SYS_ARCH_UNPROTECT(old_level);
}
#endif

#ifdef BSP_USING_ETH1
void enet1_pbuf_free_custom(struct pbuf *p)
{
    SYS_ARCH_DECL_PROTECT(old_level);
    my_custom_pbuf_t *my_pbuf = (my_custom_pbuf_t *)p;

    SYS_ARCH_PROTECT(old_level);
    free_rx_dma_descriptor((void *)my_pbuf->dma_descriptor);

    LWIP_MEMPOOL_FREE(enet1_rx_pool, my_pbuf);
    SYS_ARCH_UNPROTECT(old_level);
}
#endif

ATTR_WEAK uint8_t enet_get_mac_address(ENET_Type *ptr, uint8_t *mac)
{
    uint32_t macl, mach;
    uint8_t i;

    i = (ptr == HPM_ENET0) ? 0 : 1;

        if (mac == NULL) {
            return ENET_MAC_ADDR_PARA_ERROR;
        }

        /* load mac address from OTP MAC area */
        if (i == 0) {
            macl = otp_read_from_shadow(OTP_SOC_MAC0_IDX);
            mach = otp_read_from_shadow(OTP_SOC_MAC0_IDX + 1);

            mac[0] = (macl >>  0) & 0xff;
            mac[1] = (macl >>  8) & 0xff;
            mac[2] = (macl >> 16) & 0xff;
            mac[3] = (macl >> 24) & 0xff;
            mac[4] = (mach >>  0) & 0xff;
            mac[5] = (mach >>  8) & 0xff;
        } else {
            macl = otp_read_from_shadow(OTP_SOC_MAC0_IDX + 1);
            mach = otp_read_from_shadow(OTP_SOC_MAC0_IDX + 2);

            mac[0] = (macl >> 16) & 0xff;
            mac[1] = (macl >> 24) & 0xff;
            mac[2] = (mach >>  0) & 0xff;
            mac[3] = (mach >>  8) & 0xff;
            mac[4] = (mach >> 16) & 0xff;
            mac[5] = (mach >> 24) & 0xff;
        }

        if (!IS_MAC_INVALID(mac)) {
            return ENET_MAC_ADDR_FROM_OTP_MAC;
        }

        /* load MAC address from MACRO definitions */
        memcpy(mac, &mac_init[i], ENET_MAC_SIZE);
        return ENET_MAC_ADDR_FROM_MACRO;
}

static rt_err_t hpm_enet_init(enet_device *init)
{
    enet_hw_checksum_config_t hw_cksum_cfg;

    if (init->media_interface == enet_inf_rmii)
    {
        /* Initialize reference clock */
        board_init_enet_rmii_reference_clock(init->instance, init->int_refclk);
    }

    /* Set RGMII clock delay */
   if (init->media_interface == enet_inf_rgmii)
   {
        clock_add_to_group(init->clock_name, BOARD_RUNNING_CORE & 0x1);
        enet_rgmii_set_clock_delay(init->instance, init->tx_delay, init->rx_delay);
   }

    /* Get the default interrupt config */
    enet_get_default_interrupt_config(init->instance, &init->int_config);

    /* Initialize eth controller */
    enet_controller_init(init->instance, init->media_interface, &init->desc, &init->mac_config, &init->int_config);

    enet_get_default_tx_control_config(init->instance, &init->desc.tx_control_config);

#ifdef RT_LWIP_USING_HW_CHECKSUM
    enet_get_default_hw_checksum_config(&hw_cksum_cfg, true);
#else
    enet_get_default_hw_checksum_config(&hw_cksum_cfg, false);
#endif
    enet_set_hw_checksum_config(init->instance, &init->desc.tx_control_config, &hw_cksum_cfg);

    /* Disable LPI interrupt */
    enet_disable_lpi_interrupt(init->instance);

#if defined(__USE_ENET_PTP) && __USE_ENET_PTP
   /* initialize PTP Clock */
   board_init_enet_ptp_clock(init->instance);

   /* initialize ptp timer, the config is prepared in rt_hw_eth_init() */
   enet_init_ptp(init->instance, &init->ptp_config);
#endif

   /* enable irq */
   intc_m_enable_irq(init->irq_number);

   return RT_EOK;
}

static rt_err_t rt_hpm_eth_init(rt_device_t dev)
{
    uint8_t mac[ENET_MAC_SIZE];

    enet_device *enet_dev = (enet_device *)dev->user_data;

    /* Initialize GPIOs */
    board_init_enet_pins(enet_dev->instance);

    /* Reset an enet PHY */
    board_reset_enet_phy(enet_dev->instance);

    /* Get MAC address */
    enet_get_mac_address(enet_dev->instance, mac);

    /* Set mac0 address */
    enet_dev->mac_config.mac_addr_high[0] = mac[5] << 8 | mac[4];
    enet_dev->mac_config.mac_addr_low[0] = mac[3] << 24 | mac[2] << 16 | mac[1] << 8 | mac[0];
    enet_dev->mac_config.valid_max_count = 1;

    /* Initialize MAC and DMA */
    if (hpm_enet_init(enet_dev) == 0)
    {
        LOG_D("Ethernet control initialize successfully\n");
        return RT_EOK;
    }
    else
    {
        LOG_D("Ethernet control initialize unsuccessfully\n");
        return -RT_ERROR;
    }
}

static rt_err_t rt_hpm_eth_open(rt_device_t dev, rt_uint16_t oflag)
{
    return RT_EOK;
}

static rt_err_t rt_hpm_eth_close(rt_device_t dev)
{
    return RT_EOK;
}

static rt_ssize_t rt_hpm_eth_read(rt_device_t dev, rt_off_t pos, void * buffer, rt_size_t size)
{
    return 0;
}

static rt_ssize_t rt_hpm_eth_write(rt_device_t dev, rt_off_t pos, const void * buffer, rt_size_t size)
{
    return 0;
}

static rt_err_t rt_hpm_eth_control(rt_device_t dev, int cmd, void * args)
{
    uint8_t *mac = (uint8_t *)args;
    enet_device *enet_dev = (enet_device *)dev->user_data;

    switch (cmd)
    {
    case NIOCTL_GADDR:
        if (args != NULL)
        {
            enet_get_mac_address(enet_dev->instance, (uint8_t *)mac);
            SMEMCPY(args, mac, ENET_MAC_SIZE);
        }
        else
        {
            return -RT_ERROR;
        }
        break;
    default:
        break;
    }

    return RT_EOK;
}

static rt_err_t rt_hpm_eth_tx(rt_device_t dev, struct pbuf * p)
{
    rt_err_t ret = RT_ERROR;
    uint32_t status;
    enet_device *enet_dev = (enet_device *)dev->user_data;
    uint32_t tx_buff_size = enet_dev->desc.tx_buff_cfg.size;
    struct pbuf *q;
    uint8_t *buffer;
    __IO enet_tx_desc_t *dma_tx_desc;
    uint32_t frame_length = 0;
    uint32_t buffer_offset = 0;
    uint32_t bytes_left_to_copy = 0;
    uint32_t payload_offset = 0;
    enet_tx_desc_t  *tx_desc_list_cur = enet_dev->desc.tx_desc_list_cur;

    dma_tx_desc = tx_desc_list_cur;
    buffer = (uint8_t *)(dma_tx_desc->tdes2_bm.buffer1);
    buffer_offset = 0;
    rt_tick_t t_start;

    /* copy frame from pbufs to driver buffers */
    for (q = p; q != NULL; q = q->next)
    {
        /* Get bytes in current lwIP buffer */
        bytes_left_to_copy = q->len;
        payload_offset = 0;

        /* Check if the length of data to copy is bigger than Tx buffer size*/
        while ((bytes_left_to_copy + buffer_offset) > tx_buff_size)
        {
            /* check DMA own status within timeout */
            t_start = rt_tick_get();
            while (dma_tx_desc->tdes0_bm.own)
            {
                if (rt_tick_get() - t_start > RT_TICK_PER_SECOND / 100)
                {
                    return ERR_TIMEOUT;
                }
            }

            /* Copy data to Tx buffer*/
            SMEMCPY((uint8_t *)((uint8_t *)buffer + buffer_offset),
                    (uint8_t *)((uint8_t *)q->payload + payload_offset),
                    tx_buff_size - buffer_offset);

            /* Point to next descriptor */
            dma_tx_desc = (enet_tx_desc_t *)(dma_tx_desc->tdes3_bm.next_desc);

            /* Check if the buffer is available */
            if (dma_tx_desc->tdes0_bm.own != 0)
            {
                LOG_E("DMA tx desc buffer is not valid\n");
                return ERR_INPROGRESS;
            }

            buffer = (uint8_t *)(dma_tx_desc->tdes2_bm.buffer1);

            bytes_left_to_copy = bytes_left_to_copy - (tx_buff_size - buffer_offset);
            payload_offset = payload_offset + (tx_buff_size - buffer_offset);
            frame_length = frame_length + (tx_buff_size - buffer_offset);
            buffer_offset = 0;
        }

        /* check DMA own status within timeout */
        t_start = rt_tick_get();
        while (dma_tx_desc->tdes0_bm.own)
        {
            if (rt_tick_get() - t_start > RT_TICK_PER_SECOND / 100)
            {
                return ERR_TIMEOUT;
            }
        }

        /* Copy the remaining bytes */
        buffer = (void *)sys_address_to_core_local_mem(BOARD_RUNNING_CORE, (uint32_t)buffer);
        SMEMCPY((uint8_t *)((uint8_t *)buffer + buffer_offset),
                (uint8_t *)((uint8_t *)q->payload + payload_offset),
                bytes_left_to_copy);

        buffer_offset = buffer_offset + bytes_left_to_copy;
        frame_length = frame_length + bytes_left_to_copy;
    }

    /* Prepare transmit descriptors to give to DMA */
    LOG_D("The length of the transmitted frame: %d\n", frame_length);

    frame_length += 4;
#if defined(LWIP_PTP) && LWIP_PTP
    {
        enet_ptp_ts_system_t timestamp;
        enet_tx_control_config_t tx_cfg = enet_dev->desc.tx_control_config;

        tx_cfg.enable_ttse = lwip_ptp_frame_needs_tx_hw_timestamp(p);
        status = enet_prepare_tx_desc_with_ts_record(enet_dev->instance,
                                                     &enet_dev->desc.tx_desc_list_cur,
                                                     &tx_cfg,
                                                     frame_length,
                                                     enet_dev->desc.tx_buff_cfg.size,
                                                     &timestamp);
        if (tx_cfg.enable_ttse) {
            p->time_sec = timestamp.sec;
            p->time_nsec = timestamp.nsec;
        }
    }
#else
    status = enet_prepare_tx_desc(enet_dev->instance,
                                  &enet_dev->desc.tx_desc_list_cur,
                                  &enet_dev->desc.tx_control_config,
                                  frame_length,
                                  enet_dev->desc.tx_buff_cfg.size);
#endif
    if (status != ENET_SUCCESS)
    {
        LOG_E("Ethernet controller transmit unsuccessfully: %d\n", status);
    }

    return ERR_OK;
}

static struct pbuf *rt_hpm_eth_rx(rt_device_t dev)
{
    struct pbuf *p = NULL, *q = NULL;
    enet_device *enet_dev = (enet_device *)dev->user_data;
    uint32_t rx_buff_size = enet_dev->desc.rx_buff_cfg.size;
    uint16_t len = 0;
    uint8_t *buffer;
    enet_rx_desc_t *dma_rx_desc;
    uint32_t buffer_offset = 0;
    uint32_t payload_offset = 0;
    uint32_t bytes_left_to_copy = 0;
    uint32_t i = 0;

    /* Get a received frame */
    RT_ASSERT(!enet_dev->frame[enet_dev->cnt].used);
    if (enet_dev->frame[enet_dev->cnt].used == 0) {
        enet_dev->frame[enet_dev->cnt] = enet_get_received_frame_interrupt(&enet_dev->desc.rx_desc_list_cur, &enet_dev->desc.rx_frame_info, enet_dev->desc.rx_buff_cfg.count);
    } else {
        return p;
    }

    /* Obtain the size of the packet and put it into the "len" variable. */
    len = enet_dev->frame[enet_dev->cnt].length;
    buffer = (uint8_t *)sys_address_to_core_local_mem(BOARD_RUNNING_CORE, enet_dev->frame[enet_dev->cnt].buffer);
    LOG_D("The current received frame length : %d\n", len);

    if (len > 0)
    {
        enet_dev->frame[enet_dev->cnt].used = 1;

#ifdef BSP_USING_ETH0
        if (enet_dev->instance == HPM_ENET0) {
            my_custom_pbuf_t *my_pbuf = (my_custom_pbuf_t *)LWIP_MEMPOOL_ALLOC(enet0_rx_pool);
            my_pbuf->p.custom_free_function = enet0_pbuf_free_custom;
            my_pbuf->dma_descriptor = (void *)&enet_dev->frame[enet_dev->cnt];
            p = pbuf_alloced_custom(PBUF_RAW, enet_dev->frame[enet_dev->cnt].length, PBUF_REF, &my_pbuf->p, buffer, enet_dev->desc.rx_buff_cfg.size);
#if defined(LWIP_PTP) && LWIP_PTP
            if (p != NULL) {
                enet_rx_desc_t *rx_desc = enet_dev->frame[enet_dev->cnt].rx_desc;

                if (rx_desc != NULL) {
                    p->time_sec = rx_desc->rdes7_bm.rtsh;
                    p->time_nsec = rx_desc->rdes6_bm.rtsl;
                }
            }
#endif
            enet_dev->cnt = ++enet_dev->cnt % enet_dev->desc.rx_buff_cfg.count;
        }
#endif

#ifdef BSP_USING_ETH1
        if (enet_dev->instance == HPM_ENET1) {
            my_custom_pbuf_t *my_pbuf = (my_custom_pbuf_t *)LWIP_MEMPOOL_ALLOC(enet1_rx_pool);
            my_pbuf->p.custom_free_function = enet1_pbuf_free_custom;
            my_pbuf->dma_descriptor = (void *)&enet_dev->frame[enet_dev->cnt];
            p = pbuf_alloced_custom(PBUF_RAW, enet_dev->frame[enet_dev->cnt].length, PBUF_REF, &my_pbuf->p, buffer, enet_dev->desc.rx_buff_cfg.size);
#if defined(LWIP_PTP) && LWIP_PTP
            if (p != NULL) {
                enet_rx_desc_t *rx_desc = enet_dev->frame[enet_dev->cnt].rx_desc;

                if (rx_desc != NULL) {
                    p->time_sec = rx_desc->rdes7_bm.rtsh;
                    p->time_nsec = rx_desc->rdes6_bm.rtsl;
                }
            }
#endif
            enet_dev->cnt = ++enet_dev->cnt % enet_dev->desc.rx_buff_cfg.count;
        }
#endif
        /* Clear Segment_Count */
        enet_dev->desc.rx_frame_info.seg_count = 0;
    }

    /* Resume Rx Process */
    enet_rx_resume(enet_dev->instance);

    return p;
}

static void eth_rx_callback(struct eth_device* dev)
{
    rt_err_t result;
    result = eth_device_ready(dev);
    if (result != RT_EOK)
    {
        LOG_I("Receive callback error = %d\n", result);
    }
}

#ifndef HPM_USING_RTTHREAD_INTERRUPT_FRAMEWORK
void isr_enet(hpm_enet_t *obj)
#else
void isr_enet(int vector, hpm_enet_t *obj)
#endif /* HPM_USING_RTTHREAD_INTERRUPT_FRAMEWORK */
{
    uint32_t status;
    uint32_t rxgbfrmis;
    uint32_t intr_status;
    ENET_Type *ptr = obj->base;

    status = ptr->DMA_STATUS;
    rxgbfrmis = ptr->MMC_INTR_RX;
    intr_status = ptr->INTR_STATUS;

    if (ENET_DMA_STATUS_GLPII_GET(status)) {
        /* read LPI_CSR to clear interrupt status */
        ptr->LPI_CSR;
    }

    if (ENET_INTR_STATUS_RGSMIIIS_GET(intr_status)) {
        /* read XMII_CSR to clear interrupt status */
        ptr->XMII_CSR;
    }

    if (ENET_DMA_STATUS_RI_GET(status)) {
        eth_rx_callback(obj->eth_dev);
        ptr->DMA_STATUS |= ENET_DMA_STATUS_RI_MASK;
    }

    if (ENET_MMC_INTR_RX_RXCTRLFIS_GET(rxgbfrmis)) {
        ptr->RXFRAMECOUNT_GB;
    }
}

#ifndef HPM_USING_RTTHREAD_INTERRUPT_FRAMEWORK
#ifdef BSP_USING_ETH0
SDK_DECLARE_EXT_ISR_M(IRQn_ENET0, isr_enet0)
void isr_enet0(void)
{
    isr_enet(&enet0);
}
#endif

#ifdef BSP_USING_ETH1
SDK_DECLARE_EXT_ISR_M(IRQn_ENET1, isr_enet1)
void isr_enet1(void)
{
    isr_enet(&enet1);
}
#endif
#endif /* HPM_USING_RTTHREAD_INTERRUPT_FRAMEWORK */

int rt_hw_eth_init(void)
{
    rt_err_t err = RT_ERROR;

#ifdef BSP_USING_ETH0
    LWIP_MEMPOOL_INIT(enet0_rx_pool);
#endif

#ifdef BSP_USING_ETH1
    LWIP_MEMPOOL_INIT(enet1_rx_pool);
#endif

    for (uint32_t i = 0; i < ARRAY_SIZE(s_geths); i++)
    {
        /* Clear memory */
        memset((uint8_t *)s_geths[i]->dma_rx_desc_tab, 0x00, sizeof(enet_rx_desc_t) * s_geths[i]->rx_buff_cfg->count);
        memset((uint8_t *)s_geths[i]->dma_tx_desc_tab, 0x00, sizeof(enet_tx_desc_t) * s_geths[i]->tx_buff_cfg->count);

        memset((uint8_t *)s_geths[i]->rx_buff_cfg->buffer, 0x00, sizeof(s_geths[i]->rx_buff_cfg->size));
        memset((uint8_t *)s_geths[i]->tx_buff_cfg->buffer, 0x00, sizeof(s_geths[i]->tx_buff_cfg->size));

        /* Set list heads */
        s_geths[i]->enet_dev->desc.tx_desc_list_head = (enet_tx_desc_t *)core_local_mem_to_sys_address(BOARD_RUNNING_CORE, (uint32_t)s_geths[i]->dma_tx_desc_tab);
        s_geths[i]->enet_dev->desc.rx_desc_list_head = (enet_rx_desc_t *)core_local_mem_to_sys_address(BOARD_RUNNING_CORE, (uint32_t)s_geths[i]->dma_rx_desc_tab);

        s_geths[i]->enet_dev->desc.tx_buff_cfg.buffer = core_local_mem_to_sys_address(BOARD_RUNNING_CORE, s_geths[i]->tx_buff_cfg->buffer);
        s_geths[i]->enet_dev->desc.tx_buff_cfg.count = s_geths[i]->tx_buff_cfg->count;
        s_geths[i]->enet_dev->desc.tx_buff_cfg.size = s_geths[i]->tx_buff_cfg->size;

        s_geths[i]->enet_dev->desc.rx_buff_cfg.buffer = core_local_mem_to_sys_address(BOARD_RUNNING_CORE, s_geths[i]->rx_buff_cfg->buffer);
        s_geths[i]->enet_dev->desc.rx_buff_cfg.count = s_geths[i]->rx_buff_cfg->count;
        s_geths[i]->enet_dev->desc.rx_buff_cfg.size = s_geths[i]->rx_buff_cfg->size;

        /* Set DMA PBL */
        s_geths[i]->enet_dev->mac_config.dma_pbl = board_get_enet_dma_pbl(s_geths[i]->base);

        /* Set instance */
        s_geths[i]->enet_dev->instance = s_geths[i]->base;

        /* Set clock name */
        s_geths[i]->enet_dev->clock_name = s_geths[i]->clock_name;

        /* Set media interface */
        s_geths[i]->enet_dev->media_interface = s_geths[i]->inf ? enet_inf_rgmii : enet_inf_rmii;

        if (s_geths[i]->enet_dev->media_interface == enet_inf_rmii)
        {
            /* Set refclk  */
            s_geths[i]->enet_dev->int_refclk = s_geths[i]->int_refclk;
        } else {
            /* Set TX/RX delay */
            s_geths[i]->enet_dev->tx_delay = s_geths[i]->tx_delay;
            s_geths[i]->enet_dev->rx_delay = s_geths[i]->rx_delay;
        }


#if defined(__USE_ENET_PTP) && __USE_ENET_PTP
        /* Set PTP function */
        s_geths[i]->enet_dev->ptp_clk_src   = s_geths[i]->ptp_clk_src;
        s_geths[i]->enet_dev->ptp_timestamp = *s_geths[i]->ptp_timestamp;

        /* Runtime defaults: ssinc is derived from the real PTP clock frequency */
        enet_get_default_ptp_config(s_geths[i]->enet_dev->instance,
                                    clock_get_frequency(s_geths[i]->ptp_clk_src),
                                    &s_geths[i]->enet_dev->ptp_config);
        /* Board config overrides the runtime default */
        s_geths[i]->enet_dev->ptp_config.update_method = s_geths[i]->ptp_config->update_method;
        /* enet_init_ptp() requires a valid timestamp pointer */
        s_geths[i]->enet_dev->ptp_config.ptp_timestamp = &s_geths[i]->enet_dev->ptp_timestamp;
#endif

        /* Set the irq number */
        s_geths[i]->enet_dev->irq_number = s_geths[i]->irq_num;

        /* Set the frame buffer and counter */
        s_geths[i]->enet_dev->frame = s_geths[i]->frame;
        s_geths[i]->enet_dev->cnt = 0;

        /* Set the parent parameters */
        s_geths[i]->eth_dev->parent.init      = rt_hpm_eth_init;
        s_geths[i]->eth_dev->parent.open      = rt_hpm_eth_open;
        s_geths[i]->eth_dev->parent.close     = rt_hpm_eth_close;
        s_geths[i]->eth_dev->parent.read      = rt_hpm_eth_read;
        s_geths[i]->eth_dev->parent.write     = rt_hpm_eth_write;
        s_geths[i]->eth_dev->parent.control   = rt_hpm_eth_control;

        s_geths[i]->eth_dev->parent.user_data = s_geths[i]->enet_dev;

        s_geths[i]->eth_dev->eth_rx = rt_hpm_eth_rx;
        s_geths[i]->eth_dev->eth_tx = rt_hpm_eth_tx;

#ifdef HPM_USING_RTTHREAD_INTERRUPT_FRAMEWORK
        /* Register ENET device to irq table */
        rt_hw_interrupt_install(s_geths[i]->irq_num, (rt_isr_handler_t)isr_enet, s_geths[i], s_geths[i]->name);
#endif /* HPM_USING_RTTHREAD_INTERRUPT_FRAMEWORK */

        err = eth_device_init(s_geths[i]->eth_dev, s_geths[i]->name);

        if (RT_EOK == err)
        {
            LOG_D("Ethernet device %d initialize successfully!\n", i);
        }
        else
        {
            LOG_D("Ethernet device %d initialize unsuccessfully!\n");
            return err;
        }
    }

    return err;

}
INIT_DEVICE_EXPORT(rt_hw_eth_init);
#endif /* BSP_USING_ETH */