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
| #include "phy_smi.h" #include "dwt_delay.h"
static void MDIO_Set(uint8_t value) { HAL_GPIO_WritePin(MDIO_PORT, MDIO_PIN, value ? GPIO_PIN_SET : GPIO_PIN_RESET); }
static uint8_t MDIO_Get(void) { return HAL_GPIO_ReadPin(MDIO_PORT, MDIO_PIN); }
static void MDC_Toggle(GPIO_TypeDef *port, uint16_t pin) { HAL_GPIO_WritePin(port, pin, GPIO_PIN_SET); DWT_Delay(MDC_DELAY_NS); HAL_GPIO_WritePin(port, pin, GPIO_PIN_RESET); DWT_Delay(MDC_DELAY_NS); }
static void MDIO_WriteBit(uint8_t bit, GPIO_TypeDef *mdc_port, uint16_t mdc_pin) { MDIO_Set(bit); MDC_Toggle(mdc_port, mdc_pin); }
static uint8_t MDIO_ReadBit(GPIO_TypeDef *mdc_port, uint16_t mdc_pin) { MDC_Toggle(mdc_port, mdc_pin); return MDIO_Get(); }
void PHY_Write(uint8_t phyAddr, uint8_t regAddr, uint16_t data) { GPIO_TypeDef *mdc_ports[] = {MDC_PORT_1, MDC_PORT_2, MDC_PORT_3, MDC_PORT_4, MDC_PORT_5, MDC_PORT_6, MDC_PORT_7}; uint16_t mdc_pins[] = {MDC_PIN_1, MDC_PIN_2, MDC_PIN_3, MDC_PIN_4, MDC_PIN_5, MDC_PIN_6, MDC_PIN_7};
GPIO_TypeDef *mdc_port = mdc_ports[phyAddr]; uint16_t mdc_pin = mdc_pins[phyAddr];
for (int i = 0; i < 32; i++) { MDIO_WriteBit(1, mdc_port, mdc_pin); }
MDIO_WriteBit(0, mdc_port, mdc_pin); MDIO_WriteBit(1, mdc_port, mdc_pin); MDIO_WriteBit(0, mdc_port, mdc_pin); MDIO_WriteBit(1, mdc_port, mdc_pin); for (int i = 4; i >= 0; i--) { MDIO_WriteBit((phyAddr >> i) & 0x01, mdc_port, mdc_pin); } for (int i = 4; i >= 0; i--) { MDIO_WriteBit((regAddr >> i) & 0x01, mdc_port, mdc_pin); } MDIO_WriteBit(1, mdc_port, mdc_pin); MDIO_WriteBit(0, mdc_port, mdc_pin);
for (int i = 15; i >= 0; i--) { MDIO_WriteBit((data >> i) & 0x01, mdc_port, mdc_pin); }
MDIO_Set(0); }
uint16_t PHY_Read(uint8_t phyAddr, uint8_t regAddr) { GPIO_TypeDef *mdc_ports[] = {MDC_PORT_1, MDC_PORT_2, MDC_PORT_3, MDC_PORT_4, MDC_PORT_5, MDC_PORT_6, MDC_PORT_7}; uint16_t mdc_pins[] = {MDC_PIN_1, MDC_PIN_2, MDC_PIN_3, MDC_PIN_4, MDC_PIN_5, MDC_PIN_6, MDC_PIN_7};
GPIO_TypeDef *mdc_port = mdc_ports[phyAddr]; uint16_t mdc_pin = mdc_pins[phyAddr];
uint16_t data = 0;
for (int i = 0; i < 32; i++) { MDIO_WriteBit(1, mdc_port, mdc_pin); }
MDIO_WriteBit(0, mdc_port, mdc_pin); MDIO_WriteBit(1, mdc_port, mdc_pin); MDIO_WriteBit(1, mdc_port, mdc_pin); MDIO_WriteBit(0, mdc
_port, mdc_pin); for (int i = 4; i >= 0; i--) { MDIO_WriteBit((phyAddr >> i) & 0x01, mdc_port, mdc_pin); } for (int i = 4; i >= 0; i--) { MDIO_WriteBit((regAddr >> i) & 0x01, mdc_port, mdc_pin); }
GPIO_InitTypeDef GPIO_InitStruct = {0}; GPIO_InitStruct.Pin = MDIO_PIN; GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(MDIO_PORT, &GPIO_InitStruct);
MDIO_ReadBit(mdc_port, mdc_pin); DWT_Delay(MDC_DELAY_NS);
for (int i = 15; i >= 0; i--) { data |= (MDIO_ReadBit(mdc_port, mdc_pin) << i); }
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; HAL_GPIO_Init(MDIO_PORT, &GPIO_InitStruct);
MDIO_Set(0);
return data; }
void PHY_WriteAll(uint8_t regAddr, uint16_t data) { for (uint8_t phyAddr = 0; phyAddr < 7; phyAddr++) { PHY_Write(phyAddr, regAddr, data); } }
void PHY_ReadAll(uint8_t regAddr, uint16_t* data) { for (uint8_t phyAddr = 0; phyAddr < 7; phyAddr++) { data[phyAddr] = PHY_Read(phyAddr, regAddr); } }
|