1
+ #include <string.h>
2
+ #include "stm32f4xx_hal.h"
3
+ #include "mbed_toolchain.h"
4
+
5
+ #define C029_OTP_START_ADDRESS (0x1FFF7800U)
6
+ #define C029_OTP_END_ADDRESS (C029_OTP_START_ADDRESS + (16*32))
7
+ #define C029_MAC_ETHERNET_ID (3)
8
+
9
+ typedef MBED_PACKED (struct ) C029_OTP_Header {
10
+ uint8_t id ;
11
+ uint8_t len ;
12
+ uint8_t data [];
13
+ } C029_OTP_Header ;
14
+
15
+ static int _macRetrieved = 0 ;
16
+ static char _macAddr [6 ] = { 0x02 , 0x02 , 0xF7 , 0xF0 , 0x00 , 0x00 };
17
+
18
+ static C029_OTP_Header * increment (C029_OTP_Header * pTemp )
19
+ {
20
+ uint8_t len = 0 ;
21
+ uint8_t id = 0 ;
22
+ uint8_t * p = (uint8_t * )pTemp ;
23
+
24
+ memcpy ((void * )& id , (void * )pTemp , 1 );
25
+
26
+ if (id == 0xFF ){
27
+ p ++ ;
28
+ } else {
29
+ p ++ ;
30
+ memcpy ((void * )& len , (void * )p ++ , 1 );
31
+ p += len ;
32
+ }
33
+ return (C029_OTP_Header * )p ;
34
+ }
35
+
36
+ /**
37
+ * Override HAL Eth Init function
38
+ */
39
+ void HAL_ETH_MspInit (ETH_HandleTypeDef * heth )
40
+ {
41
+ GPIO_InitTypeDef GPIO_InitStructure ;
42
+ if (heth -> Instance == ETH ) {
43
+
44
+ /* Enable GPIOs clocks */
45
+ __HAL_RCC_GPIOA_CLK_ENABLE ();
46
+ __HAL_RCC_GPIOB_CLK_ENABLE ();
47
+ __HAL_RCC_GPIOC_CLK_ENABLE ();
48
+
49
+ /** ETH GPIO Configuration
50
+ RMII_REF_CLK ----------------------> PA1
51
+ RMII_MDIO -------------------------> PA2
52
+ RMII_MDC --------------------------> PC1
53
+ RMII_MII_CRS_DV -------------------> PA7
54
+ RMII_MII_RXD0 ---------------------> PC4
55
+ RMII_MII_RXD1 ---------------------> PC5
56
+ RMII_MII_RXER ---------------------> PG2
57
+ RMII_MII_TX_EN --------------------> PB11
58
+ RMII_MII_TXD0 ---------------------> PB12
59
+ RMII_MII_TXD1 ---------------------> PB13
60
+ */
61
+ /* Configure PA1, PA2 and PA7 */
62
+ GPIO_InitStructure .Speed = GPIO_SPEED_HIGH ;
63
+ GPIO_InitStructure .Mode = GPIO_MODE_AF_PP ;
64
+ GPIO_InitStructure .Pull = GPIO_PULLUP ;
65
+ GPIO_InitStructure .Pin = GPIO_PIN_2 | GPIO_PIN_7 ;
66
+ GPIO_InitStructure .Alternate = GPIO_AF11_ETH ;
67
+ HAL_GPIO_Init (GPIOA , & GPIO_InitStructure );
68
+
69
+ GPIO_InitStructure .Pull = GPIO_NOPULL ;
70
+ GPIO_InitStructure .Pin = GPIO_PIN_1 ;
71
+ HAL_GPIO_Init (GPIOA , & GPIO_InitStructure );
72
+
73
+ /* Configure PB13 */
74
+ GPIO_InitStructure .Pin = GPIO_PIN_13 | GPIO_PIN_11 | GPIO_PIN_12 ;
75
+ HAL_GPIO_Init (GPIOB , & GPIO_InitStructure );
76
+
77
+ /* Configure PC1, PC4 and PC5 */
78
+ GPIO_InitStructure .Pin = GPIO_PIN_1 | GPIO_PIN_4 | GPIO_PIN_5 ;
79
+ HAL_GPIO_Init (GPIOC , & GPIO_InitStructure );
80
+
81
+ /* Enable the Ethernet global Interrupt */
82
+ HAL_NVIC_SetPriority (ETH_IRQn , 0x7 , 0 );
83
+ HAL_NVIC_EnableIRQ (ETH_IRQn );
84
+
85
+ /* Enable ETHERNET clock */
86
+ __HAL_RCC_ETH_CLK_ENABLE ();
87
+ }
88
+ }
89
+
90
+ /**
91
+ * Override HAL Eth DeInit function
92
+ */
93
+ void HAL_ETH_MspDeInit (ETH_HandleTypeDef * heth )
94
+ {
95
+ if (heth -> Instance == ETH ) {
96
+ /* Peripheral clock disable */
97
+ __HAL_RCC_ETH_CLK_DISABLE ();
98
+
99
+ /** ETH GPIO Configuration
100
+ RMII_REF_CLK ----------------------> PA1
101
+ RMII_MDIO -------------------------> PA2
102
+ RMII_MDC --------------------------> PC1
103
+ RMII_MII_CRS_DV -------------------> PA7
104
+ RMII_MII_RXD0 ---------------------> PC4
105
+ RMII_MII_RXD1 ---------------------> PC5
106
+ RMII_MII_RXER ---------------------> PG2
107
+ RMII_MII_TX_EN --------------------> PB11
108
+ RMII_MII_TXD0 ---------------------> PB12
109
+ RMII_MII_TXD1 ---------------------> PB13
110
+ */
111
+ HAL_GPIO_DeInit (GPIOA , GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_7 );
112
+ HAL_GPIO_DeInit (GPIOB , GPIO_PIN_13 | GPIO_PIN_11 | GPIO_PIN_12 );
113
+ HAL_GPIO_DeInit (GPIOC , GPIO_PIN_1 | GPIO_PIN_4 | GPIO_PIN_5 );
114
+
115
+ /* Disable the Ethernet global Interrupt */
116
+ NVIC_DisableIRQ (ETH_IRQn );
117
+ }
118
+ }
119
+
120
+ uint8_t mbed_otp_mac_address (char * mac )
121
+ {
122
+ C029_OTP_Header * pFound = NULL ;
123
+ C029_OTP_Header * pTemp = (C029_OTP_Header * )C029_OTP_START_ADDRESS ;
124
+ C029_OTP_Header temp ;
125
+
126
+ if (_macRetrieved == 0 ) {
127
+ while ((pTemp >= (C029_OTP_Header * )C029_OTP_START_ADDRESS ) && (pTemp < (C029_OTP_Header * )C029_OTP_END_ADDRESS )){
128
+ memcpy ((void * )& temp , (void * )pTemp , sizeof (temp ));
129
+ if (temp .id == C029_MAC_ETHERNET_ID ){
130
+ pFound = pTemp ;
131
+ break ;
132
+ }
133
+ pTemp = increment (pTemp );
134
+ }
135
+ if (pFound != NULL ) {
136
+ memcpy (_macAddr , pFound -> data , 6 );
137
+ _macRetrieved = 1 ;
138
+ }
139
+ }
140
+ memcpy (mac , _macAddr , 6 );
141
+
142
+ return 1 ;
143
+ }
0 commit comments