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
45
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
74
75
76
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
115
116
117
120
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
196
197
198
199
200
204
208
212
216
220
221
222
223
224
225
226
227
228
229
230
231
232
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
275
276
280
281
282
283
284
285
286
287
288
289
290
291
292
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
317
318
319
320
321
327
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
371
372
373
378
379
380
381
382
383
387
388
389
390
391
392
393
394
395
396
401
412
413
419
423
424
427
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
488
492
493
498
502
503
508
515
516
519
524
525
526
529
530
531
532
533
534
535
536
541
545
546
553
562
563
570
579
580
584
588
589
594
595
596
597
598
599
600
601
602
603
604
605
606
612
613
614
615
616
621
626
631
636
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
668
669
670
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
695
700
701
702
704
708
709
710
714
715
720
721
722
723
724
725
726
727
732
733
734
735
736
737
738
739
740
745
746
/* ... */
#include "stm32f4xx_hal.h"
#include "lwip/timeouts.h"
#include "netif/ethernet.h"
#include "netif/etharp.h"
#include "lwip/stats.h"
#include "lwip/snmp.h"
#include "lwip/tcpip.h"
#include "ethernetif.h"
#include "../Components/lan8742/lan8742.h"
#include <string.h>
10 includes
Includes
#define TIME_WAITING_FOR_INPUT ( osWaitForever )
#define INTERFACE_THREAD_STACK_SIZE ( 512 )
#define IFNAME0 's'
#define IFNAME1 't'
#define ETH_DMA_TRANSMIT_TIMEOUT (20U)
#define ETH_RX_BUFFER_CNT 10
/* ... */
#define ETH_TX_BUFFER_MAX ((ETH_TX_DESC_CNT) * 2)
/* ... */
Private define
/* ... */
typedef enum
{
RX_ALLOC_OK = 0x00,
RX_ALLOC_ERROR = 0x01
...} RxAllocStatusTypeDef;
typedef struct
{
struct pbuf_custom pbuf_custom;
uint8_t buff[(ETH_RX_BUF_SIZE + 31) & ~31] __ALIGNED(32);
...} RxBuff_t;
ETH_DMADescTypeDef DMARxDscrTab[ETH_RX_DESC_CNT];
ETH_DMADescTypeDef DMATxDscrTab[ETH_TX_DESC_CNT];
LWIP_MEMPOOL_DECLARE(RX_POOL, ETH_RX_BUFFER_CNT, sizeof(RxBuff_t), "Zero-copy RX PBUF pool");
static uint8_t RxAllocStatus;
osSemaphoreId RxPktSemaphore = NULL;
TaskHandle_t EthIfThread;
osSemaphoreId TxPktSemaphore = NULL;
ETH_HandleTypeDef EthHandle;
ETH_TxPacketConfig TxConfig;
lan8742_Object_t LAN8742;
Private variables
static void ethernetif_input( void const * argument );
static void RMII_Thread( void const * argument );
int32_t ETH_PHY_IO_Init(void);
int32_t ETH_PHY_IO_DeInit (void);
int32_t ETH_PHY_IO_ReadReg(uint32_t DevAddr, uint32_t RegAddr, uint32_t *pRegVal);
int32_t ETH_PHY_IO_WriteReg(uint32_t DevAddr, uint32_t RegAddr, uint32_t RegVal);
int32_t ETH_PHY_IO_GetTick(void);
void pbuf_free_custom(struct pbuf *p);
lan8742_IOCtx_t LAN8742_IOCtx = {ETH_PHY_IO_Init,
ETH_PHY_IO_DeInit,
ETH_PHY_IO_WriteReg,
ETH_PHY_IO_ReadReg,
ETH_PHY_IO_GetTick...};
Private function prototypes
/* ... */
/* ... */
static void low_level_init(struct netif *netif)
{
uint32_t duplex, speed = 0;
int32_t PHYLinkState = 0;
ETH_MACConfigTypeDef MACConf = {0};
uint8_t macaddress[6]= {ETH_MAC_ADDR0, ETH_MAC_ADDR1, ETH_MAC_ADDR2, ETH_MAC_ADDR3, ETH_MAC_ADDR4, ETH_MAC_ADDR5};
EthHandle.Instance = ETH;
EthHandle.Init.MACAddr = macaddress;
EthHandle.Init.MediaInterface = HAL_ETH_RMII_MODE;
EthHandle.Init.RxDesc = DMARxDscrTab;
EthHandle.Init.TxDesc = DMATxDscrTab;
EthHandle.Init.RxBuffLen = ETH_RX_BUF_SIZE;
HAL_ETH_Init(&EthHandle);
netif->hwaddr_len = ETH_HWADDR_LEN;
netif->hwaddr[0] = ETH_MAC_ADDR0;
netif->hwaddr[1] = ETH_MAC_ADDR1;
netif->hwaddr[2] = ETH_MAC_ADDR2;
netif->hwaddr[3] = ETH_MAC_ADDR3;
netif->hwaddr[4] = ETH_MAC_ADDR4;
netif->hwaddr[5] = ETH_MAC_ADDR5;
netif->mtu = ETH_MAX_PAYLOAD;
netif->flags |= NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP;
LWIP_MEMPOOL_INIT(RX_POOL);
memset(&TxConfig, 0 , sizeof(ETH_TxPacketConfig));
TxConfig.Attributes = ETH_TX_PACKETS_FEATURES_CSUM | ETH_TX_PACKETS_FEATURES_CRCPAD;
TxConfig.ChecksumCtrl = ETH_CHECKSUM_IPHDR_PAYLOAD_INSERT_PHDR_CALC;
TxConfig.CRCPadCtrl = ETH_CRC_PAD_INSERT;
RxPktSemaphore = xSemaphoreCreateBinary();
TxPktSemaphore = xSemaphoreCreateBinary();
osThreadDef(EthIf, ethernetif_input, osPriorityRealtime, 0, INTERFACE_THREAD_STACK_SIZE);
osThreadCreate (osThread(EthIf), netif);
LAN8742_RegisterBusIO(&LAN8742, &LAN8742_IOCtx);
LAN8742_Init(&LAN8742);
PHYLinkState = LAN8742_GetLinkState(&LAN8742);
if(PHYLinkState <= LAN8742_STATUS_LINK_DOWN)
{
netif_set_link_down(netif);
netif_set_down(netif);
}if (PHYLinkState <= LAN8742_STATUS_LINK_DOWN) { ... }
else
{
switch (PHYLinkState)
{
case LAN8742_STATUS_100MBITS_FULLDUPLEX:
duplex = ETH_FULLDUPLEX_MODE;
speed = ETH_SPEED_100M;
break;case LAN8742_STATUS_100MBITS_FULLDUPLEX:
case LAN8742_STATUS_100MBITS_HALFDUPLEX:
duplex = ETH_HALFDUPLEX_MODE;
speed = ETH_SPEED_100M;
break;case LAN8742_STATUS_100MBITS_HALFDUPLEX:
case LAN8742_STATUS_10MBITS_FULLDUPLEX:
duplex = ETH_FULLDUPLEX_MODE;
speed = ETH_SPEED_10M;
break;case LAN8742_STATUS_10MBITS_FULLDUPLEX:
case LAN8742_STATUS_10MBITS_HALFDUPLEX:
duplex = ETH_HALFDUPLEX_MODE;
speed = ETH_SPEED_10M;
break;case LAN8742_STATUS_10MBITS_HALFDUPLEX:
default:
duplex = ETH_FULLDUPLEX_MODE;
speed = ETH_SPEED_100M;
break;default
}switch (PHYLinkState) { ... }
HAL_ETH_GetMACConfig(&EthHandle, &MACConf);
MACConf.DuplexMode = duplex;
MACConf.Speed = speed;
HAL_ETH_SetMACConfig(&EthHandle, &MACConf);
HAL_ETH_Start_IT(&EthHandle);
netif_set_up(netif);
netif_set_link_up(netif);
}else { ... }
if(HAL_GetREVID() == 0x1000)
{
/* ... */
osThreadDef(RMII_Watchdog, RMII_Thread, osPriorityRealtime, 0, configMINIMAL_STACK_SIZE);
osThreadCreate (osThread(RMII_Watchdog), NULL);
}if (HAL_GetREVID() == 0x1000) { ... }
}{ ... }
/* ... */
static err_t low_level_output(struct netif *netif, struct pbuf *p)
{
uint32_t i = 0U;
struct pbuf *q = NULL;
err_t errval = ERR_OK;
ETH_BufferTypeDef Txbuffer[ETH_TX_DESC_CNT] = {0};
memset(Txbuffer, 0 , ETH_TX_DESC_CNT*sizeof(ETH_BufferTypeDef));
for(q = p; q != NULL; q = q->next)
{
if(i >= ETH_TX_DESC_CNT)
return ERR_IF;
Txbuffer[i].buffer = q->payload;
Txbuffer[i].len = q->len;
if(i>0)
{
Txbuffer[i-1].next = &Txbuffer[i];
}if (i>0) { ... }
if(q->next == NULL)
{
Txbuffer[i].next = NULL;
}if (q->next == NULL) { ... }
i++;
}for (q = p; q != NULL; q = q->next) { ... }
TxConfig.Length = p->tot_len;
TxConfig.TxBuffer = Txbuffer;
TxConfig.pData = p;
pbuf_ref(p);
HAL_ETH_Transmit_IT(&EthHandle, &TxConfig);
while(osSemaphoreWait(TxPktSemaphore, TIME_WAITING_FOR_INPUT)!=osOK)
{
}while (osSemaphoreWait(TxPktSemaphore, TIME_WAITING_FOR_INPUT)!=osOK) { ... }
HAL_ETH_ReleaseTxPacket(&EthHandle);
return errval;
}{ ... }
/* ... */
static struct pbuf * low_level_input(struct netif *netif)
{
struct pbuf *p = NULL;
if(RxAllocStatus == RX_ALLOC_OK)
{
HAL_ETH_ReadData(&EthHandle, (void **)&p);
}if (RxAllocStatus == RX_ALLOC_OK) { ... }
return p;
}{ ... }
/* ... */
static void ethernetif_input( void const * argument )
{
struct pbuf *p = NULL;
struct netif *netif = (struct netif *) argument;
for( ;; )
{
if (osSemaphoreWait( RxPktSemaphore, TIME_WAITING_FOR_INPUT)==osOK)
{
do
{
p = low_level_input( netif );
if (p != NULL)
{
if (netif->input( p, netif) != ERR_OK )
{
pbuf_free(p);
}if (netif->input( p, netif) != ERR_OK) { ... }
}if (p != NULL) { ... }
...}while(p!=NULL);
}if (osSemaphoreWait( RxPktSemaphore, TIME_WAITING_FOR_INPUT)==osOK) { ... }
}for (;;) { ... }
}{ ... }
/* ... */
err_t ethernetif_init(struct netif *netif)
{
LWIP_ASSERT("netif != NULL", (netif != NULL));
#if LWIP_NETIF_HOSTNAME
netif->hostname = "lwip";/* ... */
#endif
/* ... */
MIB2_INIT_NETIF(netif, snmp_ifType_ethernet_csmacd, LINK_SPEED_OF_YOUR_NETIF_IN_BPS);
netif->name[0] = IFNAME0;
netif->name[1] = IFNAME1;
/* ... */
netif->output = etharp_output;
netif->linkoutput = low_level_output;
low_level_init(netif);
return ERR_OK;
}{ ... }
/* ... */
void pbuf_free_custom(struct pbuf *p)
{
struct pbuf_custom* custom_pbuf = (struct pbuf_custom*)p;
LWIP_MEMPOOL_FREE(RX_POOL, custom_pbuf);
if (RxAllocStatus == RX_ALLOC_ERROR)
{
RxAllocStatus = RX_ALLOC_OK;
osSemaphoreRelease(RxPktSemaphore);
}if (RxAllocStatus == RX_ALLOC_ERROR) { ... }
}{ ... }
/* ... */
u32_t sys_now(void)
{
return HAL_GetTick();
}{ ... }
/* ... */
/* ... */
void HAL_ETH_MspInit(ETH_HandleTypeDef *heth)
{
GPIO_InitTypeDef GPIO_InitStructure = {0};
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOG_CLK_ENABLE();
/* ... */
GPIO_InitStructure.Speed = GPIO_SPEED_HIGH;
GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
GPIO_InitStructure.Pull = GPIO_NOPULL;
GPIO_InitStructure.Alternate = GPIO_AF11_ETH;
GPIO_InitStructure.Pin = GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_7;
HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.Pin = GPIO_PIN_13;
HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.Pin = GPIO_PIN_1 | GPIO_PIN_4 | GPIO_PIN_5;
HAL_GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.Pin = GPIO_PIN_11 | GPIO_PIN_13;
HAL_GPIO_Init(GPIOG, &GPIO_InitStructure);
HAL_NVIC_SetPriority(ETH_IRQn, 0x7, 0);
HAL_NVIC_EnableIRQ(ETH_IRQn);
__HAL_RCC_ETH_CLK_ENABLE();
}{ ... }
/* ... */
void HAL_ETH_RxCpltCallback(ETH_HandleTypeDef *heth)
{
osSemaphoreRelease(RxPktSemaphore);
}{ ... }
/* ... */
void HAL_ETH_TxCpltCallback(ETH_HandleTypeDef *heth)
{
osSemaphoreRelease(TxPktSemaphore);
}{ ... }
/* ... */
void HAL_ETH_ErrorCallback(ETH_HandleTypeDef *heth)
{
if((HAL_ETH_GetDMAError(heth) & ETH_DMASR_RBUS) == ETH_DMASR_RBUS)
{
osSemaphoreRelease(RxPktSemaphore);
}if ((HAL_ETH_GetDMAError(heth) & ETH_DMASR_RBUS) == ETH_DMASR_RBUS) { ... }
}{ ... }
/* ... */
/* ... */
int32_t ETH_PHY_IO_Init(void)
{
/* ... */
HAL_ETH_SetMDIOClockRange(&EthHandle);
return 0;
}{ ... }
/* ... */
int32_t ETH_PHY_IO_DeInit (void)
{
return 0;
}{ ... }
/* ... */
int32_t ETH_PHY_IO_ReadReg(uint32_t DevAddr, uint32_t RegAddr, uint32_t *pRegVal)
{
if(HAL_ETH_ReadPHYRegister(&EthHandle, DevAddr, RegAddr, pRegVal) != HAL_OK)
{
return -1;
}if (HAL_ETH_ReadPHYRegister(&EthHandle, DevAddr, RegAddr, pRegVal) != HAL_OK) { ... }
return 0;
}{ ... }
/* ... */
int32_t ETH_PHY_IO_WriteReg(uint32_t DevAddr, uint32_t RegAddr, uint32_t RegVal)
{
if(HAL_ETH_WritePHYRegister(&EthHandle, DevAddr, RegAddr, RegVal) != HAL_OK)
{
return -1;
}if (HAL_ETH_WritePHYRegister(&EthHandle, DevAddr, RegAddr, RegVal) != HAL_OK) { ... }
return 0;
}{ ... }
/* ... */
int32_t ETH_PHY_IO_GetTick(void)
{
return HAL_GetTick();
}{ ... }
/* ... */
void ethernet_link_thread( void const * argument )
{
ETH_MACConfigTypeDef MACConf = {0};
int32_t PHYLinkState = 0U;
uint32_t linkchanged = 0U, speed = 0U, duplex = 0U;
struct netif *netif = (struct netif *) argument;
for(;;)
{
PHYLinkState = LAN8742_GetLinkState(&LAN8742);
if(netif_is_link_up(netif) && (PHYLinkState <= LAN8742_STATUS_LINK_DOWN))
{
HAL_ETH_Stop_IT(&EthHandle);
netif_set_down(netif);
netif_set_link_down(netif);
}if (netif_is_link_up(netif) && (PHYLinkState <= LAN8742_STATUS_LINK_DOWN)) { ... }
else if(!netif_is_link_up(netif) && (PHYLinkState > LAN8742_STATUS_LINK_DOWN))
{
switch (PHYLinkState)
{
case LAN8742_STATUS_100MBITS_FULLDUPLEX:
duplex = ETH_FULLDUPLEX_MODE;
speed = ETH_SPEED_100M;
linkchanged = 1;
break;case LAN8742_STATUS_100MBITS_FULLDUPLEX:
case LAN8742_STATUS_100MBITS_HALFDUPLEX:
duplex = ETH_HALFDUPLEX_MODE;
speed = ETH_SPEED_100M;
linkchanged = 1;
break;case LAN8742_STATUS_100MBITS_HALFDUPLEX:
case LAN8742_STATUS_10MBITS_FULLDUPLEX:
duplex = ETH_FULLDUPLEX_MODE;
speed = ETH_SPEED_10M;
linkchanged = 1;
break;case LAN8742_STATUS_10MBITS_FULLDUPLEX:
case LAN8742_STATUS_10MBITS_HALFDUPLEX:
duplex = ETH_HALFDUPLEX_MODE;
speed = ETH_SPEED_10M;
linkchanged = 1;
break;case LAN8742_STATUS_10MBITS_HALFDUPLEX:
default:
break;default
}switch (PHYLinkState) { ... }
if(linkchanged)
{
HAL_ETH_GetMACConfig(&EthHandle, &MACConf);
MACConf.DuplexMode = duplex;
MACConf.Speed = speed;
HAL_ETH_SetMACConfig(&EthHandle, &MACConf);
HAL_ETH_Start_IT(&EthHandle);
netif_set_up(netif);
netif_set_link_up(netif);
}if (linkchanged) { ... }
}else if (!netif_is_link_up(netif) && (PHYLinkState > LAN8742_STATUS_LINK_DOWN)) { ... }
osDelay(100);
}for (;;) { ... }
}{ ... }
void HAL_ETH_RxAllocateCallback(uint8_t **buff)
{
struct pbuf_custom *p = LWIP_MEMPOOL_ALLOC(RX_POOL);
if (p)
{
*buff = (uint8_t *)p + offsetof(RxBuff_t, buff);
p->custom_free_function = pbuf_free_custom;
/* ... */
pbuf_alloced_custom(PBUF_RAW, 0, PBUF_REF, p, *buff, ETH_RX_BUF_SIZE);
}if (p) { ... }
else
{
RxAllocStatus = RX_ALLOC_ERROR;
*buff = NULL;
}else { ... }
}{ ... }
void HAL_ETH_RxLinkCallback(void **pStart, void **pEnd, uint8_t *buff, uint16_t Length)
{
struct pbuf **ppStart = (struct pbuf **)pStart;
struct pbuf **ppEnd = (struct pbuf **)pEnd;
struct pbuf *p = NULL;
p = (struct pbuf *)(buff - offsetof(RxBuff_t, buff));
p->next = NULL;
p->tot_len = 0;
p->len = Length;
if (!*ppStart)
{
*ppStart = p;
}if (!*ppStart) { ... }
else
{
(*ppEnd)->next = p;
}else { ... }
*ppEnd = p;
/* ... */
for (p = *ppStart; p != NULL; p = p->next)
{
p->tot_len += Length;
}for (p = *ppStart; p != NULL; p = p->next) { ... }
}{ ... }
void HAL_ETH_TxFreeCallback(uint32_t * buff)
{
pbuf_free((struct pbuf *)buff);
}{ ... }
/* ... */
void RMII_Thread( void const * argument )
{
(void) argument;
for(;;)
{
if(EthHandle.Instance->MMCRGUFCR > 0U)
{
osThreadTerminate(NULL);
}if (EthHandle.Instance->MMCRGUFCR > 0U) { ... }
else if(EthHandle.Instance->MMCRFCECR > 10U)
{
SYSCFG->PMC &= ~SYSCFG_PMC_MII_RMII_SEL;
SYSCFG->PMC |= SYSCFG_PMC_MII_RMII_SEL;
EthHandle.Instance->MMCCR |= ETH_MMCCR_CR;
}else if (EthHandle.Instance->MMCRFCECR > 10U) { ... }
else
{
osDelay(200);
}else { ... }
}for (;;) { ... }
}{ ... }