1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
70
71
74
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
154
155
156
161
165
166
171
175
176
179
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
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
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
427
428
429
430
431
432
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
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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
522
527
528
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
556
560
561
562
563
564
565
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
600
601
602
603
604
605
610
615
616
621
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
650
651
652
653
654
659
665
666
/* ... */
#include "stm32f4xx_hal.h"
#include "lwip/opt.h"
#include "lwip/timeouts.h"
#include "lwip/tcpip.h"
#include "netif/etharp.h"
#include "ethernetif.h"
#include <string.h>
7 includes
Includes
#define TIME_WAITING_FOR_INPUT ( 100 )
#define INTERFACE_THREAD_STACK_SIZE ( 350 )
#define IFNAME0 's'
#define IFNAME1 't'
Private define
#if defined ( __ICCARM__ )
#pragma data_alignment=4
#endif
__ALIGN_BEGIN ETH_DMADescTypeDef DMARxDscrTab[ETH_RXBUFNB] __ALIGN_END;
#if defined ( __ICCARM__ )
#pragma data_alignment=4
#endif
__ALIGN_BEGIN ETH_DMADescTypeDef DMATxDscrTab[ETH_TXBUFNB] __ALIGN_END;
#if defined ( __ICCARM__ )
#pragma data_alignment=4
#endif
__ALIGN_BEGIN uint8_t Rx_Buff[ETH_RXBUFNB][ETH_RX_BUF_SIZE] __ALIGN_END;
#if defined ( __ICCARM__ )
#pragma data_alignment=4
#endif
__ALIGN_BEGIN uint8_t Tx_Buff[ETH_TXBUFNB][ETH_TX_BUF_SIZE] __ALIGN_END;
osSemaphoreId s_xSemaphore = NULL;
ETH_HandleTypeDef EthHandle;
Private variables
static void ethernetif_input( void const * argument );
static void arp_timer(void *arg);
Private function prototypes
/* ... */
/* ... */
void HAL_ETH_MspInit(ETH_HandleTypeDef *heth)
{
GPIO_InitTypeDef GPIO_InitStructure;
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOF_CLK_ENABLE();
__HAL_RCC_GPIOG_CLK_ENABLE();
__HAL_RCC_GPIOH_CLK_ENABLE();
__HAL_RCC_GPIOI_CLK_ENABLE();
/* ... */
GPIO_InitStructure.Pin = GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_7 | GPIO_PIN_8;
GPIO_InitStructure.Speed = GPIO_SPEED_HIGH;
GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
GPIO_InitStructure.Pull = GPIO_NOPULL;
GPIO_InitStructure.Alternate = GPIO_AF11_ETH;
HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.Pin = GPIO_PIN_5 | GPIO_PIN_8;
HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.Pin = GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_5;
HAL_GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.Pin = GPIO_PIN_11 | GPIO_PIN_13 | GPIO_PIN_14;
HAL_GPIO_Init(GPIOG, &GPIO_InitStructure);
GPIO_InitStructure.Pin = GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_6 | GPIO_PIN_7;
HAL_GPIO_Init(GPIOH, &GPIO_InitStructure);
GPIO_InitStructure.Pin = GPIO_PIN_10;
HAL_GPIO_Init(GPIOI, &GPIO_InitStructure);
HAL_NVIC_SetPriority(ETH_IRQn, 0x7, 0);
HAL_NVIC_EnableIRQ(ETH_IRQn);
__HAL_RCC_ETH_CLK_ENABLE();
if (heth->Init.MediaInterface == ETH_MEDIA_INTERFACE_MII)
{
HAL_RCC_MCOConfig(RCC_MCO1, RCC_MCO1SOURCE_HSE, RCC_MCODIV_1);
}if (heth->Init.MediaInterface == ETH_MEDIA_INTERFACE_MII) { ... }
}{ ... }
/* ... */
void HAL_ETH_RxCpltCallback(ETH_HandleTypeDef *heth)
{
osSemaphoreRelease(s_xSemaphore);
}{ ... }
/* ... */
void ETHERNET_IRQHandler(void)
{
HAL_ETH_IRQHandler(&EthHandle);
}{ ... }
/* ... */
/* ... */
static void low_level_init(struct netif *netif)
{
uint32_t regvalue = 0;
uint8_t macaddress[6]= { MAC_ADDR0, MAC_ADDR1, MAC_ADDR2, MAC_ADDR3, MAC_ADDR4, MAC_ADDR5 };
EthHandle.Instance = ETH;
EthHandle.Init.MACAddr = macaddress;
EthHandle.Init.AutoNegotiation = ETH_AUTONEGOTIATION_ENABLE;
EthHandle.Init.Speed = ETH_SPEED_100M;
EthHandle.Init.DuplexMode = ETH_MODE_FULLDUPLEX;
EthHandle.Init.MediaInterface = ETH_MEDIA_INTERFACE_MII;
EthHandle.Init.RxMode = ETH_RXINTERRUPT_MODE;
EthHandle.Init.ChecksumMode = ETH_CHECKSUM_BY_HARDWARE;
EthHandle.Init.PhyAddress = DP83848_PHY_ADDRESS;
if (HAL_ETH_Init(&EthHandle) == HAL_OK)
{
netif->flags |= NETIF_FLAG_LINK_UP;
}if (HAL_ETH_Init(&EthHandle) == HAL_OK) { ... }
HAL_ETH_DMATxDescListInit(&EthHandle, DMATxDscrTab, &Tx_Buff[0][0], ETH_TXBUFNB);
HAL_ETH_DMARxDescListInit(&EthHandle, DMARxDscrTab, &Rx_Buff[0][0], ETH_RXBUFNB);
netif->hwaddr_len = ETHARP_HWADDR_LEN;
netif->hwaddr[0] = MAC_ADDR0;
netif->hwaddr[1] = MAC_ADDR1;
netif->hwaddr[2] = MAC_ADDR2;
netif->hwaddr[3] = MAC_ADDR3;
netif->hwaddr[4] = MAC_ADDR4;
netif->hwaddr[5] = MAC_ADDR5;
netif->mtu = 1500;
netif->flags |= NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP;
osSemaphoreDef(SEM);
s_xSemaphore = osSemaphoreCreate(osSemaphore(SEM) , 1 );
osThreadDef(Eth_if, ethernetif_input, osPriorityRealtime, 0, INTERFACE_THREAD_STACK_SIZE);
osThreadCreate (osThread(Eth_if), netif);
HAL_ETH_Start(&EthHandle);
HAL_ETH_ReadPHYRegister(&EthHandle, PHY_MICR, ®value);
regvalue |= (PHY_MICR_INT_EN | PHY_MICR_INT_OE);
HAL_ETH_WritePHYRegister(&EthHandle, PHY_MICR, regvalue );
HAL_ETH_ReadPHYRegister(&EthHandle, PHY_MISR, ®value);
regvalue |= PHY_MISR_LINK_INT_EN;
HAL_ETH_WritePHYRegister(&EthHandle, PHY_MISR, regvalue);
}{ ... }
/* ... */
static err_t low_level_output(struct netif *netif, struct pbuf *p)
{
err_t errval;
struct pbuf *q;
uint8_t *buffer = (uint8_t *)(EthHandle.TxDesc->Buffer1Addr);
__IO ETH_DMADescTypeDef *DmaTxDesc;
uint32_t framelength = 0;
uint32_t bufferoffset = 0;
uint32_t byteslefttocopy = 0;
uint32_t payloadoffset = 0;
DmaTxDesc = EthHandle.TxDesc;
bufferoffset = 0;
for(q = p; q != NULL; q = q->next)
{
if((DmaTxDesc->Status & ETH_DMATXDESC_OWN) != (uint32_t)RESET)
{
errval = ERR_USE;
goto error;
}if ((DmaTxDesc->Status & ETH_DMATXDESC_OWN) != (uint32_t)RESET) { ... }
byteslefttocopy = q->len;
payloadoffset = 0;
while( (byteslefttocopy + bufferoffset) > ETH_TX_BUF_SIZE )
{
memcpy( (uint8_t*)((uint8_t*)buffer + bufferoffset), (uint8_t*)((uint8_t*)q->payload + payloadoffset), (ETH_TX_BUF_SIZE - bufferoffset) );
DmaTxDesc = (ETH_DMADescTypeDef *)(DmaTxDesc->Buffer2NextDescAddr);
if((DmaTxDesc->Status & ETH_DMATXDESC_OWN) != (uint32_t)RESET)
{
errval = ERR_USE;
goto error;
}if ((DmaTxDesc->Status & ETH_DMATXDESC_OWN) != (uint32_t)RESET) { ... }
buffer = (uint8_t *)(DmaTxDesc->Buffer1Addr);
byteslefttocopy = byteslefttocopy - (ETH_TX_BUF_SIZE - bufferoffset);
payloadoffset = payloadoffset + (ETH_TX_BUF_SIZE - bufferoffset);
framelength = framelength + (ETH_TX_BUF_SIZE - bufferoffset);
bufferoffset = 0;
}while ((byteslefttocopy + bufferoffset) > ETH_TX_BUF_SIZE) { ... }
memcpy( (uint8_t*)((uint8_t*)buffer + bufferoffset), (uint8_t*)((uint8_t*)q->payload + payloadoffset), byteslefttocopy );
bufferoffset = bufferoffset + byteslefttocopy;
framelength = framelength + byteslefttocopy;
}for (q = p; q != NULL; q = q->next) { ... }
HAL_ETH_TransmitFrame(&EthHandle, framelength);
errval = ERR_OK;
error:
if ((EthHandle.Instance->DMASR & ETH_DMASR_TUS) != (uint32_t)RESET)
{
EthHandle.Instance->DMASR = ETH_DMASR_TUS;
EthHandle.Instance->DMATPDR = 0;
}if ((EthHandle.Instance->DMASR & ETH_DMASR_TUS) != (uint32_t)RESET) { ... }
return errval;
}{ ... }
/* ... */
static struct pbuf * low_level_input(struct netif *netif)
{
struct pbuf *p, *q;
u16_t len = 0;
uint8_t *buffer;
__IO ETH_DMADescTypeDef *dmarxdesc;
uint32_t bufferoffset = 0;
uint32_t payloadoffset = 0;
uint32_t byteslefttocopy = 0;
uint32_t i=0;
p = NULL;
if(HAL_ETH_GetReceivedFrame_IT(&EthHandle) != HAL_OK)
return NULL;
len = EthHandle.RxFrameInfos.length;
buffer = (uint8_t *)EthHandle.RxFrameInfos.buffer;
if (len > 0)
{
p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL);
}if (len > 0) { ... }
if (p != NULL)
{
dmarxdesc = EthHandle.RxFrameInfos.FSRxDesc;
bufferoffset = 0;
for(q = p; q != NULL; q = q->next)
{
byteslefttocopy = q->len;
payloadoffset = 0;
while( (byteslefttocopy + bufferoffset) > ETH_RX_BUF_SIZE )
{
memcpy( (u8_t*)((u8_t*)q->payload + payloadoffset), (u8_t*)((u8_t*)buffer + bufferoffset), (ETH_RX_BUF_SIZE - bufferoffset));
dmarxdesc = (ETH_DMADescTypeDef *)(dmarxdesc->Buffer2NextDescAddr);
buffer = (unsigned char *)(dmarxdesc->Buffer1Addr);
byteslefttocopy = byteslefttocopy - (ETH_RX_BUF_SIZE - bufferoffset);
payloadoffset = payloadoffset + (ETH_RX_BUF_SIZE - bufferoffset);
bufferoffset = 0;
}while ((byteslefttocopy + bufferoffset) > ETH_RX_BUF_SIZE) { ... }
memcpy( (u8_t*)((u8_t*)q->payload + payloadoffset), (u8_t*)((u8_t*)buffer + bufferoffset), byteslefttocopy);
bufferoffset = bufferoffset + byteslefttocopy;
}for (q = p; q != NULL; q = q->next) { ... }
}if (p != NULL) { ... }
dmarxdesc = EthHandle.RxFrameInfos.FSRxDesc;
for (i=0; i< EthHandle.RxFrameInfos.SegCount; i++)
{
dmarxdesc->Status |= ETH_DMARXDESC_OWN;
dmarxdesc = (ETH_DMADescTypeDef *)(dmarxdesc->Buffer2NextDescAddr);
}for (i=0; i< EthHandle.RxFrameInfos.SegCount; i++) { ... }
EthHandle.RxFrameInfos.SegCount =0;
if ((EthHandle.Instance->DMASR & ETH_DMASR_RBUS) != (uint32_t)RESET)
{
EthHandle.Instance->DMASR = ETH_DMASR_RBUS;
EthHandle.Instance->DMARPDR = 0;
}if ((EthHandle.Instance->DMASR & ETH_DMASR_RBUS) != (uint32_t)RESET) { ... }
return p;
}{ ... }
/* ... */
void ethernetif_input( void const * argument )
{
struct pbuf *p;
struct netif *netif = (struct netif *) argument;
for( ;; )
{
if (osSemaphoreWait( s_xSemaphore, TIME_WAITING_FOR_INPUT)==osOK)
{
do
{
LOCK_TCPIP_CORE();
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) { ... }
UNLOCK_TCPIP_CORE();
...}while(p!=NULL);
}if (osSemaphoreWait( s_xSemaphore, 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
netif->name[0] = IFNAME0;
netif->name[1] = IFNAME1;
netif->output = etharp_output;
netif->linkoutput = low_level_output;
low_level_init(netif);
etharp_init();
sys_timeout(ARP_TMR_INTERVAL, arp_timer, NULL);
return ERR_OK;
}{ ... }
/* ... */
static void arp_timer(void *arg)
{
etharp_tmr();
sys_timeout(ARP_TMR_INTERVAL, arp_timer, NULL);
}{ ... }
/* ... */
void ethernetif_set_link(void const *argument)
{
uint32_t regvalue = 0;
struct link_str *link_arg = (struct link_str *)argument;
for(;;)
{
if (osSemaphoreWait( link_arg->semaphore, 100)== osOK)
{
HAL_ETH_ReadPHYRegister(&EthHandle, PHY_MISR, ®value);
if((regvalue & PHY_LINK_INTERRUPT) != (uint16_t)RESET)
{
HAL_ETH_ReadPHYRegister(&EthHandle, PHY_SR, ®value);
if((regvalue & PHY_LINK_STATUS)!= (uint16_t)RESET)
{
netif_set_link_up(link_arg->netif);
}if ((regvalue & PHY_LINK_STATUS)!= (uint16_t)RESET) { ... }
else
{
netif_set_link_down(link_arg->netif);
}else { ... }
}if ((regvalue & PHY_LINK_INTERRUPT) != (uint16_t)RESET) { ... }
}if (osSemaphoreWait( link_arg->semaphore, 100)== osOK) { ... }
}for (;;) { ... }
}{ ... }
/* ... */
void ethernetif_update_config(struct netif *netif)
{
__IO uint32_t tickstart = 0;
uint32_t regvalue = 0;
if(netif_is_link_up(netif))
{
if(EthHandle.Init.AutoNegotiation != ETH_AUTONEGOTIATION_DISABLE)
{
HAL_ETH_WritePHYRegister(&EthHandle, PHY_BCR, PHY_AUTONEGOTIATION);
tickstart = HAL_GetTick();
do
{
HAL_ETH_ReadPHYRegister(&EthHandle, PHY_BSR, ®value);
if((HAL_GetTick() - tickstart ) > 1000)
{
goto error;
}if ((HAL_GetTick() - tickstart ) > 1000) { ... }
...} while (((regvalue & PHY_AUTONEGO_COMPLETE) != PHY_AUTONEGO_COMPLETE));
HAL_ETH_ReadPHYRegister(&EthHandle, PHY_SR, ®value);
if((regvalue & PHY_DUPLEX_STATUS) != (uint32_t)RESET)
{
EthHandle.Init.DuplexMode = ETH_MODE_FULLDUPLEX;
}if ((regvalue & PHY_DUPLEX_STATUS) != (uint32_t)RESET) { ... }
else
{
EthHandle.Init.DuplexMode = ETH_MODE_HALFDUPLEX;
}else { ... }
if(regvalue & PHY_SPEED_STATUS)
{
EthHandle.Init.Speed = ETH_SPEED_10M;
}if (regvalue & PHY_SPEED_STATUS) { ... }
else
{
EthHandle.Init.Speed = ETH_SPEED_100M;
}else { ... }
}if (EthHandle.Init.AutoNegotiation != ETH_AUTONEGOTIATION_DISABLE) { ... }
else
{
error :
assert_param(IS_ETH_SPEED(EthHandle.Init.Speed));
assert_param(IS_ETH_DUPLEX_MODE(EthHandle.Init.DuplexMode));
HAL_ETH_WritePHYRegister(&EthHandle, PHY_BCR, ((uint16_t)(EthHandle.Init.DuplexMode >> 3) |
(uint16_t)(EthHandle.Init.Speed >> 1)));
}else { ... }
HAL_ETH_ConfigMAC(&EthHandle, (ETH_MACInitTypeDef *) NULL);
HAL_ETH_Start(&EthHandle);
}if (netif_is_link_up(netif)) { ... }
else
{
HAL_ETH_Stop(&EthHandle);
}else { ... }
ethernetif_notify_conn_changed(netif);
}{ ... }
/* ... */
__weak void ethernetif_notify_conn_changed(struct netif *netif)
{
/* ... */
}{ ... }
u32_t sys_now(void)
{
return HAL_GetTick();
}{ ... }