1+ /* Copyright (c) 2017 ARM Limited
2+ *
3+ * Licensed under the Apache License, Version 2.0 (the "License");
4+ * you may not use this file except in compliance with the License.
5+ * You may obtain a copy of the License at
6+ *
7+ * http://www.apache.org/licenses/LICENSE-2.0
8+ *
9+ * Unless required by applicable law or agreed to in writing, software
10+ * distributed under the License is distributed on an "AS IS" BASIS,
11+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+ * See the License for the specific language governing permissions and
13+ * limitations under the License.
14+ */
15+
16+
17+ #include <string.h>
18+ #include "stm32f4xx_hal.h"
19+ #include "mbed_toolchain.h"
20+
21+ #define C029_OTP_START_ADDRESS (0x1FFF7800U)
22+ #define C029_OTP_END_ADDRESS (C029_OTP_START_ADDRESS + (16*32))
23+ #define C029_MAC_ETHERNET_ID (3)
24+
25+ typedef MBED_PACKED (struct ) C029_OTP_Header {
26+ uint8_t id ;
27+ uint8_t len ;
28+ uint8_t data [];
29+ } C029_OTP_Header ;
30+
31+ static int _macRetrieved = 0 ;
32+ static char _macAddr [6 ] = { 0x02 , 0x02 , 0xF7 , 0xF0 , 0x00 , 0x00 };
33+
34+ static C029_OTP_Header * increment (C029_OTP_Header * pTemp )
35+ {
36+ uint8_t len = 0 ;
37+ uint8_t id = 0 ;
38+ uint8_t * p = (uint8_t * )pTemp ;
39+
40+ memcpy ((void * )& id , (void * )pTemp , 1 );
41+
42+ if (id == 0xFF ){
43+ p ++ ;
44+ } else {
45+ p ++ ;
46+ memcpy ((void * )& len , (void * )p ++ , 1 );
47+ p += len ;
48+ }
49+ return (C029_OTP_Header * )p ;
50+ }
51+
52+ /**
53+ * Override HAL Eth Init function
54+ */
55+ void HAL_ETH_MspInit (ETH_HandleTypeDef * heth )
56+ {
57+ GPIO_InitTypeDef GPIO_InitStructure ;
58+ if (heth -> Instance == ETH ) {
59+
60+ /* Enable GPIOs clocks */
61+ __HAL_RCC_GPIOA_CLK_ENABLE ();
62+ __HAL_RCC_GPIOB_CLK_ENABLE ();
63+ __HAL_RCC_GPIOC_CLK_ENABLE ();
64+
65+ /** ETH GPIO Configuration
66+ RMII_REF_CLK ----------------------> PA1
67+ RMII_MDIO -------------------------> PA2
68+ RMII_MDC --------------------------> PC1
69+ RMII_MII_CRS_DV -------------------> PA7
70+ RMII_MII_RXD0 ---------------------> PC4
71+ RMII_MII_RXD1 ---------------------> PC5
72+ RMII_MII_RXER ---------------------> PG2
73+ RMII_MII_TX_EN --------------------> PB11
74+ RMII_MII_TXD0 ---------------------> PB12
75+ RMII_MII_TXD1 ---------------------> PB13
76+ */
77+ /* Configure PA1, PA2 and PA7 */
78+ GPIO_InitStructure .Speed = GPIO_SPEED_HIGH ;
79+ GPIO_InitStructure .Mode = GPIO_MODE_AF_PP ;
80+ GPIO_InitStructure .Pull = GPIO_PULLUP ;
81+ GPIO_InitStructure .Pin = GPIO_PIN_2 | GPIO_PIN_7 ;
82+ GPIO_InitStructure .Alternate = GPIO_AF11_ETH ;
83+ HAL_GPIO_Init (GPIOA , & GPIO_InitStructure );
84+
85+ GPIO_InitStructure .Pull = GPIO_NOPULL ;
86+ GPIO_InitStructure .Pin = GPIO_PIN_1 ;
87+ HAL_GPIO_Init (GPIOA , & GPIO_InitStructure );
88+
89+ /* Configure PB13 */
90+ GPIO_InitStructure .Pin = GPIO_PIN_13 | GPIO_PIN_11 | GPIO_PIN_12 ;
91+ HAL_GPIO_Init (GPIOB , & GPIO_InitStructure );
92+
93+ /* Configure PC1, PC4 and PC5 */
94+ GPIO_InitStructure .Pin = GPIO_PIN_1 | GPIO_PIN_4 | GPIO_PIN_5 ;
95+ HAL_GPIO_Init (GPIOC , & GPIO_InitStructure );
96+
97+ /* Enable the Ethernet global Interrupt */
98+ HAL_NVIC_SetPriority (ETH_IRQn , 0x7 , 0 );
99+ HAL_NVIC_EnableIRQ (ETH_IRQn );
100+
101+ /* Enable ETHERNET clock */
102+ __HAL_RCC_ETH_CLK_ENABLE ();
103+ }
104+ }
105+
106+ /**
107+ * Override HAL Eth DeInit function
108+ */
109+ void HAL_ETH_MspDeInit (ETH_HandleTypeDef * heth )
110+ {
111+ if (heth -> Instance == ETH ) {
112+ /* Peripheral clock disable */
113+ __HAL_RCC_ETH_CLK_DISABLE ();
114+
115+ /** ETH GPIO Configuration
116+ RMII_REF_CLK ----------------------> PA1
117+ RMII_MDIO -------------------------> PA2
118+ RMII_MDC --------------------------> PC1
119+ RMII_MII_CRS_DV -------------------> PA7
120+ RMII_MII_RXD0 ---------------------> PC4
121+ RMII_MII_RXD1 ---------------------> PC5
122+ RMII_MII_RXER ---------------------> PG2
123+ RMII_MII_TX_EN --------------------> PB11
124+ RMII_MII_TXD0 ---------------------> PB12
125+ RMII_MII_TXD1 ---------------------> PB13
126+ */
127+ HAL_GPIO_DeInit (GPIOA , GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_7 );
128+ HAL_GPIO_DeInit (GPIOB , GPIO_PIN_13 | GPIO_PIN_11 | GPIO_PIN_12 );
129+ HAL_GPIO_DeInit (GPIOC , GPIO_PIN_1 | GPIO_PIN_4 | GPIO_PIN_5 );
130+
131+ /* Disable the Ethernet global Interrupt */
132+ NVIC_DisableIRQ (ETH_IRQn );
133+ }
134+ }
135+
136+ uint8_t mbed_otp_mac_address (char * mac )
137+ {
138+ C029_OTP_Header * pFound = NULL ;
139+ C029_OTP_Header * pTemp = (C029_OTP_Header * )C029_OTP_START_ADDRESS ;
140+ C029_OTP_Header temp ;
141+
142+ if (_macRetrieved == 0 ) {
143+ while ((pTemp >= (C029_OTP_Header * )C029_OTP_START_ADDRESS ) && (pTemp < (C029_OTP_Header * )C029_OTP_END_ADDRESS )){
144+ memcpy ((void * )& temp , (void * )pTemp , sizeof (temp ));
145+ if (temp .id == C029_MAC_ETHERNET_ID ){
146+ pFound = pTemp ;
147+ break ;
148+ }
149+ pTemp = increment (pTemp );
150+ }
151+ if (pFound != NULL ) {
152+ memcpy (_macAddr , pFound -> data , 6 );
153+ _macRetrieved = 1 ;
154+ }
155+ }
156+ memcpy (mac , _macAddr , 6 );
157+
158+ return 1 ;
159+ }
0 commit comments