-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstm32-bsp-eth-dhcp-files.patch
1380 lines (1377 loc) · 41.2 KB
/
stm32-bsp-eth-dhcp-files.patch
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
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
From af0c1609df44eeafb3f39f8fe64c85c07a0c7ef3 Mon Sep 17 00:00:00 2001
From: Robin Mueller <[email protected]>
Date: Sun, 24 Oct 2021 12:38:42 +0200
Subject: [PATCH] Added STM32 files which are added via patch
---
lwip/ports/drivers/stm32h7/app_dhcp.c | 208 +++++
lwip/ports/drivers/stm32h7/app_ethernet.c | 191 +++++
lwip/ports/drivers/stm32h7/ethernetif.c | 748 ++++++++++++++++++
.../stm32h7/include/rtems_lwip/app_dhcp.h | 51 ++
.../stm32h7/include/rtems_lwip/app_ethernet.h | 64 ++
.../stm32h7/include/rtems_lwip/ethernetif.h | 59 ++
6 files changed, 1321 insertions(+)
create mode 100644 lwip/ports/drivers/stm32h7/app_dhcp.c
create mode 100644 lwip/ports/drivers/stm32h7/app_ethernet.c
create mode 100644 lwip/ports/drivers/stm32h7/ethernetif.c
create mode 100644 lwip/ports/drivers/stm32h7/include/rtems_lwip/app_dhcp.h
create mode 100644 lwip/ports/drivers/stm32h7/include/rtems_lwip/app_ethernet.h
create mode 100644 lwip/ports/drivers/stm32h7/include/rtems_lwip/ethernetif.h
diff --git a/lwip/ports/drivers/stm32h7/app_dhcp.c b/lwip/ports/drivers/stm32h7/app_dhcp.c
new file mode 100644
index 0000000..0685670
--- /dev/null
+++ b/lwip/ports/drivers/stm32h7/app_dhcp.c
@@ -0,0 +1,208 @@
+/* Includes ------------------------------------------------------------------*/
+#include "app_dhcp.h"
+#include "port_conf.h"
+#include "ethernetif.h"
+#include "stm32h7xx_hal.h"
+#include "lwip/opt.h"
+#include "lwip/dhcp.h"
+#include "lwip/ip_addr.h"
+#include "rtems_lwip.h"
+#include "rtems_lwip_conf.h"
+
+#if NO_SYS == 0
+#include "lwip/sys.h"
+#endif
+#include <sys/unistd.h>
+
+#if LWIP_DHCP
+#ifndef MAX_DHCP_TRIES
+#define MAX_DHCP_TRIES 4
+#endif /* MAX_DHCP_TRIES */
+
+
+#if NO_SYS == 1
+static uint32_t DHCPfineTimer = 0;
+
+#endif /* NO_SYS == 1*/
+
+static void dhcp_thread(void* argument);
+
+LwipThreadArgs dhcp_args;
+uint8_t DHCP_state = DHCP_START;
+
+#if NO_SYS == 0
+void rtems_lwip_start_dhcp_thread(uint32_t task_interval_ms,
+ size_t task_stack, uint8_t task_priority) {
+ dhcp_args.netif = rtems_lwip_get_netif(0);
+ dhcp_args.task_interval_ms = task_interval_ms;
+ sys_thread_new("DHCP", dhcp_thread, (void*) &dhcp_args, task_stack, task_priority);
+}
+#endif /* NO_SYS == 0 */
+
+/**
+ * @brief This is the thread dedicated to DHCP handling
+ * @param argument: network interface
+ * @retval None
+ */
+void dhcp_thread(void* argument)
+{
+ ip_addr_t ipaddr;
+ ip_addr_t netmask;
+ ip_addr_t gw;
+ struct dhcp *dhcp = NULL;
+ LwipThreadArgs* args = (LwipThreadArgs*) argument;
+ if(args == NULL) {
+ printf("dhcp_thread: Passed arguments are invalid!\n\r");
+ exit(1);
+ }
+
+ struct netif* netif = args->netif;
+ if(netif == NULL) {
+ printf("dhcp_thread: Passed netif is invalid!\n\r");
+ exit(1);
+ }
+
+ if(args->task_interval_ms == 0) {
+ printf("dhcp_thread: Invalid task interval!\n\r");
+ exit(1);
+ }
+
+ for (;;) {
+ switch (DHCP_state) {
+ case DHCP_START: {
+ ip_addr_set_zero_ip4(&netif->ip_addr);
+ ip_addr_set_zero_ip4(&netif->netmask);
+ ip_addr_set_zero_ip4(&netif->gw);
+ DHCP_state = DHCP_WAIT_ADDRESS;
+
+ dhcp_start(netif);
+ }
+ break;
+ case DHCP_WAIT_ADDRESS: {
+ if (dhcp_supplied_address(netif)) {
+ DHCP_state = DHCP_ADDRESS_ASSIGNED;
+#if RTEMS_LWIP_DHCP_PRINTOUT == 1
+ printf("IP address assigned by a DHCP server: %s\n\r",
+ ip4addr_ntoa(netif_ip4_addr(netif)));
+#endif
+ }
+ else {
+ dhcp = (struct dhcp *)netif_get_client_data(netif, LWIP_NETIF_CLIENT_DATA_INDEX_DHCP);
+
+ /* DHCP timeout */
+ if (dhcp->tries > MAX_DHCP_TRIES) {
+ DHCP_state = DHCP_TIMEOUT;
+#if RTEMS_LWIP_DHCP_PRINTOUT == 1
+ uint8_t iptxt[20];
+ sprintf((char *)iptxt, "%s", ip4addr_ntoa(netif_ip4_addr(netif)));
+ printf("DHCP timeout\n\r");
+ printf("Setting static IP address %s..\n\r", iptxt);
+#endif
+ /* Static address used */
+ rtems_lwip_determine_static_ipv4_address(&ipaddr, &netmask, &gw);
+ netif_set_addr(netif, ip_2_ip4(&ipaddr), ip_2_ip4(&netmask), ip_2_ip4(&gw));
+ }
+ }
+ }
+ break;
+ case DHCP_LINK_DOWN: {
+ DHCP_state = DHCP_OFF;
+ }
+ break;
+ default: break;
+ }
+
+ usleep(args->task_interval_ms * 1000);
+ }
+}
+
+#if NO_SYS == 1
+
+void DHCP_Process(struct netif *netif);
+
+/**
+ * @brief DHCP periodic check
+ * @param netif
+ * @retval None
+ */
+void dhcp_periodic_handle(struct netif *netif)
+{
+ /* Fine DHCP periodic process every 500ms */
+ if (HAL_GetTick() - DHCPfineTimer >= RTEMS_LWIP_DHCP_TASK_INTERVAL_MS) {
+ DHCPfineTimer = HAL_GetTick();
+ /* process DHCP state machine */
+ DHCP_Process(netif);
+ }
+}
+
+/**
+ * @brief DHCP_Process_Handle
+ * @param None
+ * @retval None
+ */
+void DHCP_Process(struct netif *netif)
+{
+ ip_addr_t ipaddr;
+ ip_addr_t netmask;
+ ip_addr_t gw;
+ struct dhcp *dhcp = NULL;
+ switch (DHCP_state) {
+ case DHCP_START: {
+ ip_addr_set_zero_ip4(&netif->ip_addr);
+ ip_addr_set_zero_ip4(&netif->netmask);
+ ip_addr_set_zero_ip4(&netif->gw);
+ dhcp_start(netif);
+ DHCP_state = DHCP_WAIT_ADDRESS;
+ }
+ break;
+
+ case DHCP_WAIT_ADDRESS: {
+ if (dhcp_supplied_address(netif)) {
+ DHCP_state = DHCP_ADDRESS_ASSIGNED;
+ printf("IP address assigned by a DHCP server: %s\n\r", ip4addr_ntoa(netif_ip4_addr(netif)));
+ }
+ else
+ {
+ dhcp = (struct dhcp *)netif_get_client_data(netif, LWIP_NETIF_CLIENT_DATA_INDEX_DHCP);
+
+ /* DHCP timeout */
+ if (dhcp->tries > MAX_DHCP_TRIES)
+ {
+ DHCP_state = DHCP_TIMEOUT;
+
+ uint8_t iptxt[20];
+ sprintf((char *)iptxt, "%s", ip4addr_ntoa(netif_ip4_addr(netif)));
+ printf("DHCP timeout\n\r");
+ printf("Setting static IP address %s..\n\r", iptxt);
+ /* Static address used */
+ rtems_lwip_determine_static_ipv4_address(&ipaddr, &netmask, &gw);
+ netif_set_addr(netif, &ipaddr, &netmask, &gw);
+
+ }
+ }
+ }
+ break;
+ case DHCP_LINK_DOWN: {
+ /* Stop DHCP */
+ //dhcp_release_and_stop(netif);
+ DHCP_state = DHCP_OFF;
+ }
+ break;
+ default: break;
+ }
+}
+
+#endif /* NO_SYS == 1 */
+
+void set_dhcp_state(uint8_t new_state)
+{
+ DHCP_state = new_state;
+}
+
+uint8_t get_dhcp_state()
+{
+ return DHCP_state;
+}
+
+#endif /* LWIP_DHCP */
+
diff --git a/lwip/ports/drivers/stm32h7/app_ethernet.c b/lwip/ports/drivers/stm32h7/app_ethernet.c
new file mode 100644
index 0000000..0536886
--- /dev/null
+++ b/lwip/ports/drivers/stm32h7/app_ethernet.c
@@ -0,0 +1,191 @@
+/**
+ ******************************************************************************
+ * @file LwIP/LwIP_HTTP_Server_Netconn_RTOS/Src/app_ethernet.c
+ * @author MCD Application Team
+ * @brief Ethernet specefic module
+ ******************************************************************************
+ * @attention
+ *
+ * <h2><center>© Copyright (c) 2017 STMicroelectronics.
+ * All rights reserved.</center></h2>
+ *
+ * This software component is licensed by ST under Ultimate Liberty license
+ * SLA0044, the "License"; You may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at:
+ * www.st.com/SLA0044
+ *
+ ******************************************************************************
+ */
+/* Includes ------------------------------------------------------------------*/
+#include "rtems_lwip_conf.h"
+#include "rtems_lwip.h"
+#include "stm32h7/lan8742.h"
+#include "stm32h7xx_hal.h"
+#include "lwip/opt.h"
+#if LWIP_DHCP
+#include "lwip/dhcp.h"
+#endif
+
+#if NO_SYS == 0
+#include "lwip/sys.h"
+#endif
+
+#include "app_ethernet.h"
+#include "app_dhcp.h"
+#include "ethernetif.h"
+
+/* Private typedef -----------------------------------------------------------*/
+LwipThreadArgs linkArgs;
+
+/* Private define ------------------------------------------------------------*/
+/* Private macro -------------------------------------------------------------*/
+/* Private variables ---------------------------------------------------------*/
+#if NO_SYS == 1
+uint32_t ethernet_link_timer = 0;
+#endif
+
+/* Private function prototypes -----------------------------------------------*/
+#if LWIP_DHCP
+void set_dhcp_state(uint8_t new_state);
+#endif
+static void ethernet_link_thread( void* argument );
+lan8742_Object_t* get_lan_phy_handle();
+ETH_HandleTypeDef* get_eth_handle();
+
+/* Private functions ---------------------------------------------------------*/
+/**
+ * @brief Notify the User about the nework interface config status
+ * @param netif: the network interface
+ * @retval None
+ */
+void ethernet_link_status_updated(struct netif *netif)
+{
+ if (netif_is_up(netif)) {
+#if LWIP_DHCP
+ printf("Ethernet link up. Setting DHCP state..\n\r");
+ /* Update DHCP state machine */
+ set_dhcp_state(DHCP_START);
+#endif /* LWIP_DHCP */
+ }
+ else {
+#if LWIP_DHCP
+ printf("Ethernet link down. Setting DHCP state..\n\r");
+ /* Update DHCP state machine */
+ set_dhcp_state(DHCP_LINK_DOWN);
+#endif /* LWIP_DHCP */
+ }
+}
+
+#if LWIP_NETIF_LINK_CALLBACK == 1 && NO_SYS == 1
+/**
+ * @brief Ethernet Link periodic check
+ * @param netif
+ * @retval None
+ */
+void ethernet_link_periodic_handle(struct netif *netif)
+{
+ uint32_t time_now = HAL_GetTick();
+ /* Ethernet Link every 100ms */
+ if (time_now - ethernet_link_timer >= 100) {
+ ethernet_link_timer = time_now;
+ ethernet_link_check_state(netif);
+ }
+}
+#endif
+
+#if NO_SYS == 0
+
+void rtems_lwip_start_link_thread(uint32_t task_interval_ms,
+ size_t task_stack, uint8_t task_priority) {
+ linkArgs.netif = rtems_lwip_get_netif(0);
+ linkArgs.task_interval_ms = task_interval_ms;
+ sys_thread_new("LINK", ethernet_link_thread, (void*) &linkArgs, task_stack, task_priority);
+}
+
+#endif
+
+/**
+ * @brief Check the ETH link state and update netif accordingly.
+ * @param argument: netif
+ * @retval None
+ */
+void ethernet_link_thread( void* argument )
+{
+ ETH_MACConfigTypeDef MACConf;
+ int32_t PHYLinkState;
+ uint32_t linkchanged = 0, speed = 0, duplex =0;
+ LwipThreadArgs* args = (LwipThreadArgs*) argument;
+ if(args == NULL) {
+ printf("ethernet_link_thread: Passed arguments are invalid!\n\r");
+ exit(1);
+ }
+
+ struct netif *netif = args->netif;
+ if(netif == NULL) {
+ printf("ethernet_link_thread: Passed netif is invalid!\n\r");
+ exit(1);
+ }
+
+ if(args->task_interval_ms == 0) {
+ printf("ethernet_link_thread: Invalid task interval!\n\r");
+ exit(1);
+ }
+
+ lan8742_Object_t* lan = get_lan_phy_handle();
+ ETH_HandleTypeDef* eth = get_eth_handle();
+ for(;;) {
+
+ PHYLinkState = LAN8742_GetLinkState(lan);
+
+ if(netif_is_link_up(netif) && (PHYLinkState <= LAN8742_STATUS_LINK_DOWN)) {
+ HAL_ETH_Stop_IT(eth);
+ netif_set_down(netif);
+ netif_set_link_down(netif);
+ }
+ 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_HALFDUPLEX: {
+ duplex = ETH_HALFDUPLEX_MODE;
+ speed = ETH_SPEED_100M;
+ linkchanged = 1;
+ break;
+ }
+ case LAN8742_STATUS_10MBITS_FULLDUPLEX: {
+ duplex = ETH_FULLDUPLEX_MODE;
+ speed = ETH_SPEED_10M;
+ linkchanged = 1;
+ break;
+ }
+ case LAN8742_STATUS_10MBITS_HALFDUPLEX: {
+ duplex = ETH_HALFDUPLEX_MODE;
+ speed = ETH_SPEED_10M;
+ linkchanged = 1;
+ break;
+ }
+ default:
+ break;
+ }
+
+ if(linkchanged) {
+ /* Get MAC Config MAC */
+ HAL_ETH_GetMACConfig(eth, &MACConf);
+ MACConf.DuplexMode = duplex;
+ MACConf.Speed = speed;
+ HAL_ETH_SetMACConfig(eth, &MACConf);
+ HAL_ETH_Start_IT(eth);
+ netif_set_up(netif);
+ netif_set_link_up(netif);
+ }
+ }
+
+ usleep(args->task_interval_ms * 1000);
+ }
+}
+
+/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
diff --git a/lwip/ports/drivers/stm32h7/ethernetif.c b/lwip/ports/drivers/stm32h7/ethernetif.c
new file mode 100644
index 0000000..61014df
--- /dev/null
+++ b/lwip/ports/drivers/stm32h7/ethernetif.c
@@ -0,0 +1,748 @@
+/**
+ ******************************************************************************
+ * @file LwIP/LwIP_HTTP_Server_Netconn_RTOS/Src/ethernetif.c
+ * @author MCD Application Team
+ * @brief This file implements Ethernet network interface drivers for lwIP
+ ******************************************************************************
+ * @attention
+ *
+ * <h2><center>© Copyright (c) 2017 STMicroelectronics.
+ * All rights reserved.</center></h2>
+ *
+ * This software component is licensed by ST under Ultimate Liberty license
+ * SLA0044, the "License"; You may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at:
+ * www.st.com/SLA0044
+ *
+ ******************************************************************************
+ */
+
+/* Includes ------------------------------------------------------------------*/
+#include "stm32h7xx_hal.h"
+#include "port_conf.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 "lan8742.h"
+#include <string.h>
+
+/* Private typedef -----------------------------------------------------------*/
+/* Private define ------------------------------------------------------------*/
+#define DMA_DESCRIPTOR_ALIGNMENT ( 0x20 )
+
+/* Define those to better describe your network interface. */
+#define IFNAME0 's'
+#define IFNAME1 't'
+
+/* Private macro -------------------------------------------------------------*/
+/* Private variables ---------------------------------------------------------*/
+/*
+@Note: This interface is implemented to operate in zero-copy mode only:
+ - Rx buffers are allocated statically and passed directly to the LwIP stack,
+ they will return back to ETH DMA after been processed by the stack.
+ - Tx Buffers will be allocated from LwIP stack memory heap,
+ then passed to ETH HAL driver.
+
+@Notes:
+ 1.a. ETH DMA Rx descriptors must be contiguous, the default count is 4,
+ to customize it please redefine ETH_RX_DESC_CNT in stm32xxxx_hal_conf.h
+ 1.b. ETH DMA Tx descriptors must be contiguous, the default count is 4,
+ to customize it please redefine ETH_TX_DESC_CNT in stm32xxxx_hal_conf.h
+
+ 2.a. Rx Buffers number must be between ETH_RX_DESC_CNT and 2*ETH_RX_DESC_CNT
+ 2.b. Rx Buffers must have the same size: ETH_RX_BUFFER_SIZE, this value must
+ passed to ETH DMA in the init field (EthHandle.Init.RxBuffLen)
+ 2.c The RX Ruffers addresses and sizes must be properly defined to be aligned
+ to L1-CACHE line size (32 bytes).
+*/
+
+#ifdef __rtems__
+netif_init_fn eth_lwip_init_fnc = ðernetif_init;
+
+/* Put into special RTEMS section and align correctly */
+ETH_DMADescTypeDef DMARxDscrTab[ETH_RX_DESC_CNT] __attribute__((section(".bsp_nocache"), __aligned__(DMA_DESCRIPTOR_ALIGNMENT))); /* Ethernet Rx DMA Descriptors */
+/* Put into special RTEMS section and align correctly */
+ETH_DMADescTypeDef DMATxDscrTab[ETH_TX_DESC_CNT] __attribute__((section(".bsp_nocache"), __aligned__(DMA_DESCRIPTOR_ALIGNMENT))); /* Ethernet Tx DMA Descriptors */
+/* Ethernet Receive Buffers. Just place somewhere is BSS instead of explicitely placing it */
+uint8_t Rx_Buff[ETH_RX_DESC_CNT][ETH_RX_BUFFER_SIZE] RTEMS_ALIGNED(DMA_DESCRIPTOR_ALIGNMENT);
+#endif /* __rtems__ */
+
+ETH_HandleTypeDef EthHandle;
+ETH_TxPacketConfig TxConfig;
+
+lan8742_Object_t LAN8742;
+
+#if NO_SYS == 0
+sys_sem_t RxPktSemaphore; /* Semaphore to signal incoming packets */
+#endif
+
+/* Private function prototypes -----------------------------------------------*/
+#if !STATIC_IP_ADDRESS && NO_SYS == 0 && LWIP_NETIF_API == 1
+void set_dhcp_state(uint8_t new_state);
+#endif
+
+uint8_t determine_mpu_region_size_select(size_t lwip_heap_size);
+static void low_level_init(struct netif *netif);
+static err_t low_level_output(struct netif *netif, struct pbuf *p);
+void ethernet_link_check_state(struct netif *netif);
+
+void pbuf_free_custom(struct pbuf *p);
+
+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);
+
+
+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};
+
+LWIP_MEMPOOL_DECLARE(RX_POOL, 10, sizeof(struct pbuf_custom), "Zero-copy RX PBUF pool");
+
+/* Private functions ---------------------------------------------------------*/
+/*******************************************************************************
+ LL Driver Interface ( LwIP stack --> ETH)
+*******************************************************************************/
+
+/**
+ * @brief Should be called at the beginning of the program to set up the
+ * network interface. It calls the function low_level_init() to do the
+ * actual setup of the hardware.
+ *
+ * This function should be passed as a parameter to netif_add().
+ *
+ * @param netif the lwip network interface structure for this ethernetif
+ * @return ERR_OK if the loopif is initialized
+ * ERR_MEM if private data couldn't be allocated
+ * any other err_t on error
+ */
+err_t ethernetif_init(struct netif *netif)
+{
+ LWIP_ASSERT("netif != NULL", (netif != NULL));
+
+#ifdef __rtems__
+#if RTEMS_LWIP_PROTECT_LWIP_HEAP_WITH_MPU == 1
+ mpu_config_tx_buffers(LWIP_RAM_HEAP_POINTER, MEM_SIZE);
+#endif /* RTEMS_LWIP_PROTECT_LWIP_HEAP_WITH_MPU == 1 */
+#endif /* __rtems__ */
+
+#if LWIP_NETIF_HOSTNAME
+ /* Initialize interface hostname */
+ netif->hostname = "lwip";
+#endif /* LWIP_NETIF_HOSTNAME */
+
+#if NO_SYS == 0
+ /*
+ * Initialize the snmp variables and counters inside the struct netif.
+ * The last argument should be replaced with your link speed, in units
+ * of bits per second.
+ */
+ MIB2_INIT_NETIF(netif, snmp_ifType_ethernet_csmacd, LINK_SPEED_OF_YOUR_NETIF_IN_BPS);
+#endif
+
+ netif->name[0] = IFNAME0;
+ netif->name[1] = IFNAME1;
+
+ /* We directly use etharp_output() here to save a function call.
+ * You can instead declare your own function an call etharp_output()
+ * from it if you have to do some checks before sending (e.g. if link
+ * is available...) */
+ netif->output = etharp_output;
+ netif->linkoutput = low_level_output;
+
+ /* This is already done by the generic driver, so the internal state machine is set accordingly */
+#if !STATIC_IP_ADDRESS && NO_SYS == 0 && LWIP_NETIF_API == 1
+ set_dhcp_state(DHCP_WAIT_ADDRESS);
+#endif
+
+ /* initialize the hardware */
+ low_level_init(netif);
+
+ return ERR_OK;
+}
+
+/**
+ * @brief In this function, the hardware should be initialized.
+ * Called from ethernetif_init().
+ *
+ * @param netif the already initialized lwip network interface structure
+ * for this ethernetif
+ */
+static void low_level_init(struct netif *netif)
+{
+#if NO_SYS == 0
+ int32_t PHYLinkState;
+ ETH_MACConfigTypeDef MACConf;
+ uint32_t duplex, speed = 0;
+#endif
+ uint32_t idx = 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_BUFFER_SIZE;
+
+ /* configure ethernet peripheral (GPIOs, clocks, MAC, DMA) */
+ HAL_ETH_Init(&EthHandle);
+
+#if NO_SYS == 0
+ /* Enable the Ethernet global Interrupt */
+ HAL_NVIC_SetPriority(ETH_IRQn, 0x7, 0);
+ HAL_NVIC_EnableIRQ(ETH_IRQn);
+#endif
+
+ /* set MAC hardware address length */
+ netif->hwaddr_len = ETH_HWADDR_LEN;
+
+ /* set MAC hardware address */
+ 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;
+
+ /* maximum transfer unit */
+ netif->mtu = ETH_MAX_PAYLOAD;
+
+ /* device capabilities */
+ /* don't set NETIF_FLAG_ETHARP if this device is not an ethernet one */
+ netif->flags |= NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP;
+
+ for(idx = 0; idx < ETH_RX_DESC_CNT; idx ++)
+ {
+ HAL_ETH_DescAssignMemory(&EthHandle, idx, Rx_Buff[idx], NULL);
+ }
+
+ /* Initialize the RX POOL */
+ 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;
+
+#ifdef __rtems__
+#if NO_SYS == 0
+ /* create a binary semaphore used for informing ethernetif of frame reception */
+ sys_sem_new(&RxPktSemaphore, 1);
+
+ /* create the task that handles the ETH_MAC */
+ sys_thread_new(
+ "ETHM",
+ (lwip_thread_fn) ethernetif_input,
+ netif,
+ RTEMS_LWIP_INTERFACE_THREAD_STACK_SIZE,
+ RTEMS_LWIP_INTERFACE_THREAD_PRIORITY
+ );
+#endif /* NO_SYS == 0 */
+#endif /* __rtems__ */
+
+ /* Set PHY IO functions */
+ LAN8742_RegisterBusIO(&LAN8742, &LAN8742_IOCtx);
+
+ /* Initialize the LAN8742 ETH PHY */
+ LAN8742_Init(&LAN8742);
+
+#if NO_SYS == 0
+ PHYLinkState = LAN8742_GetLinkState(&LAN8742);
+
+ /* Get link state */
+ if(PHYLinkState <= LAN8742_STATUS_LINK_DOWN)
+ {
+ netif_set_link_down(netif);
+ netif_set_down(netif);
+ }
+ else
+ {
+ switch (PHYLinkState)
+ {
+ case LAN8742_STATUS_100MBITS_FULLDUPLEX:
+ duplex = ETH_FULLDUPLEX_MODE;
+ speed = ETH_SPEED_100M;
+ break;
+ case LAN8742_STATUS_100MBITS_HALFDUPLEX:
+ duplex = ETH_HALFDUPLEX_MODE;
+ speed = ETH_SPEED_100M;
+ break;
+ case LAN8742_STATUS_10MBITS_FULLDUPLEX:
+ duplex = ETH_FULLDUPLEX_MODE;
+ speed = ETH_SPEED_10M;
+ break;
+ case LAN8742_STATUS_10MBITS_HALFDUPLEX:
+ duplex = ETH_HALFDUPLEX_MODE;
+ speed = ETH_SPEED_10M;
+ break;
+ default:
+ duplex = ETH_FULLDUPLEX_MODE;
+ speed = ETH_SPEED_100M;
+ break;
+ }
+
+ /* Get MAC Config MAC */
+ HAL_ETH_GetMACConfig(&EthHandle, &MACConf);
+ MACConf.DuplexMode = duplex;
+ MACConf.Speed = speed;
+ HAL_ETH_SetMACConfig(&EthHandle, &MACConf);
+ HAL_ETH_Start_IT(&EthHandle);
+#ifdef __rtems__
+ /* Install the ETH IRQ for RTEMS */
+ rtems_interrupt_handler_install(ETH_IRQn, NULL, RTEMS_INTERRUPT_UNIQUE,
+ (rtems_interrupt_handler) &HAL_ETH_IRQHandler, &EthHandle);
+#endif
+ netif_set_up(netif);
+ netif_set_link_up(netif);
+ }
+#endif
+
+ ethernet_link_check_state(netif);
+}
+
+/**
+ * @brief This function should do the actual transmission of the packet. The packet is
+ * contained in the pbuf that is passed to the function. This pbuf
+ * might be chained.
+ *
+ * @param netif the lwip network interface structure for this ethernetif
+ * @param p the MAC packet to send (e.g. IP packet including MAC addresses and type)
+ * @return ERR_OK if the packet could be sent
+ * an err_t value if the packet couldn't be sent
+ *
+ * @note Returning ERR_MEM here if a DMA queue of your MAC is full can lead to
+ * strange results. You might consider waiting for space in the DMA queue
+ * to become available since the stack doesn't retry to send a packet
+ * dropped because of memory failure (except for the TCP timers).
+ */
+static err_t low_level_output(struct netif *netif, struct pbuf *p)
+{
+ uint32_t i=0;
+ struct pbuf *q;
+ err_t errval = ERR_OK;
+ ETH_BufferTypeDef Txbuffer[ETH_TX_DESC_CNT];
+
+ 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;
+
+#ifdef __rtems__
+ /* For the socket API, a custom send buffer might be used which is not protected by the
+ MPU. Therefore, clean DCache here as well */
+#if RTEMS_LWIP_PROTECT_LWIP_HEAP_WITH_MPU == 0 || LWIP_SOCKET == 1
+ /* Cleaning cache isn't necessary if the tx buffers are in a not-cacheable MPU region. */
+ uint8_t *dataStart = q->payload;
+ uint8_t *lineStart = (uint8_t *)((uint32_t)dataStart & ~31);
+ SCB_CleanDCache_by_Addr((uint32_t *)lineStart, q->len + (dataStart - lineStart));
+#endif
+#endif
+
+ if(i>0)
+ {
+ Txbuffer[i-1].next = &Txbuffer[i];
+ }
+
+ if(q->next == NULL)
+ {
+ Txbuffer[i].next = NULL;
+ }
+
+ i++;
+ }
+
+ TxConfig.Length = p->tot_len;
+ TxConfig.TxBuffer = Txbuffer;
+
+ HAL_ETH_Transmit(&EthHandle, &TxConfig, RTEMS_LWIP_ETH_DMA_TRANSMIT_TIMEOUT);
+
+ return errval;
+}
+
+/**
+ * @brief Should allocate a pbuf and transfer the bytes of the incoming
+ * packet from the interface into the pbuf.
+ *
+ * @param netif the lwip network interface structure for this ethernetif
+ * @return a pbuf filled with the received packet (including MAC header)
+ * NULL on memory error
+ */
+static struct pbuf * low_level_input(struct netif *netif)
+{
+ struct pbuf *p = NULL;
+ ETH_BufferTypeDef RxBuff[ETH_RX_DESC_CNT];
+ uint32_t framelength = 0, i = 0;;
+ struct pbuf_custom* custom_pbuf;
+
+ memset(RxBuff, 0 , ETH_RX_DESC_CNT*sizeof(ETH_BufferTypeDef));
+
+ for(i = 0; i < ETH_RX_DESC_CNT -1; i++)
+ {
+ RxBuff[i].next=&RxBuff[i+1];
+ }
+
+#if NO_SYS == 0
+ if(HAL_ETH_GetRxDataBuffer(&EthHandle, RxBuff) == HAL_OK) {
+#else
+ if (HAL_ETH_IsRxDataAvailable(&EthHandle)) {
+#endif /* NO_SYS != 0 */
+
+#if NO_SYS == 1
+ HAL_ETH_GetRxDataBuffer(&EthHandle, RxBuff);
+#endif /* NO_SYS == 1 */
+
+ HAL_ETH_GetRxDataLength(&EthHandle, &framelength);
+
+ /* Build Rx descriptor to be ready for next data reception */
+ HAL_ETH_BuildRxDescriptors(&EthHandle);
+
+ /* Invalidate data cache for ETH Rx Buffers */
+ SCB_InvalidateDCache_by_Addr((uint32_t *)RxBuff->buffer, framelength);
+
+ custom_pbuf = (struct pbuf_custom*)LWIP_MEMPOOL_ALLOC(RX_POOL);
+ if(custom_pbuf != NULL)
+ {
+ custom_pbuf->custom_free_function = pbuf_free_custom;
+
+ p = pbuf_alloced_custom(PBUF_RAW, framelength, PBUF_REF, custom_pbuf, RxBuff->buffer, framelength);
+ }
+
+ }
+
+ return p;
+}
+
+#if NO_SYS == 0
+/**
+ * @brief This function is the ethernetif_input task, it is processed when a packet
+ * is ready to be read from the interface. It uses the function low_level_input()
+ * that should handle the actual reception of bytes from the network
+ * interface. Then the type of the received packet is determined and
+ * the appropriate input function is called.
+ *
+ * @param netif the lwip network interface structure for this ethernetif
+ */
+void ethernetif_input(struct netif *netif)
+{
+ struct pbuf *p;
+
+ for( ;; )
+ {
+ if (sys_arch_sem_wait(&RxPktSemaphore, 0) != SYS_ARCH_TIMEOUT)
+ {
+ do
+ {
+ p = low_level_input( netif );
+ if (p != NULL)
+ {
+ if (netif->input( p, netif) != ERR_OK )
+ {
+ pbuf_free(p);
+ }
+ }
+
+ }while(p!=NULL);
+ }
+ }
+}
+#else
+/**
+ * @brief This function is the ethernetif_input task, it is processed when a packet
+ * is ready to be read from the interface. It uses the function low_level_input()
+ * that should handle the actual reception of bytes from the network
+ * interface. Then the type of the received packet is determined and
+ * the appropriate input function is called.
+ *
+ * @param netif the lwip network interface structure for this ethernetif
+ */
+void ethernetif_input(struct netif *netif)
+{
+ err_t err;
+ struct pbuf *p;
+
+ /* move received packet into a new pbuf */
+ p = low_level_input(netif);
+
+ /* no packet could be read, silently ignore this */
+ if (p == NULL) return;
+
+ /* entry point to the LwIP stack */
+ err = netif->input(p, netif);
+
+ if (err != ERR_OK)
+ {
+ LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_input: IP input error\n"));
+ pbuf_free(p);
+ p = NULL;
+ }
+}
+#endif /* NO_SYS == 1 */
+
+/**
+ * @brief Custom Rx pbuf free callback
+ * @param pbuf: pbuf to be freed
+ * @retval None
+ */
+void pbuf_free_custom(struct pbuf *p)
+{
+ struct pbuf_custom* custom_pbuf = (struct pbuf_custom*)p;
+ LWIP_MEMPOOL_FREE(RX_POOL, custom_pbuf);
+}
+
+
+/*******************************************************************************
+ Ethernet MSP Routines
+*******************************************************************************/
+/**
+ * @brief Note: HAL_ETH_MspInit of RTEMS BSP is used.
+ * @param heth: ETH handle
+ * @retval None
+*/
+
+#if NO_SYS == 0
+/**
+ * @brief Ethernet Rx Transfer completed callback
+ * @param heth: ETH handle
+ * @retval None
+ */
+void HAL_ETH_RxCpltCallback(ETH_HandleTypeDef *heth)
+{
+ sys_sem_signal(&RxPktSemaphore);
+}
+#endif
+
+/*******************************************************************************
+ PHI IO Functions
+*******************************************************************************/
+/**
+ * @brief Initializes the MDIO interface GPIO and clocks.
+ * @param None
+ * @retval 0 if OK, -1 if ERROR
+ */
+int32_t ETH_PHY_IO_Init(void)
+{
+ /* We assume that MDIO GPIO configuration is already done
+ in the ETH_MspInit() else it should be done here
+ */
+
+ /* Configure the MDIO Clock */
+ HAL_ETH_SetMDIOClockRange(&EthHandle);
+
+ return 0;
+}
+
+/**
+ * @brief De-Initializes the MDIO interface .
+ * @param None
+ * @retval 0 if OK, -1 if ERROR
+ */
+int32_t ETH_PHY_IO_DeInit (void)
+{
+ return 0;
+}
+
+/**
+ * @brief Read a PHY register through the MDIO interface.
+ * @param DevAddr: PHY port address
+ * @param RegAddr: PHY register address
+ * @param pRegVal: pointer to hold the register value