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