[KSZ8081RND] simplify read/write functions
This commit is contained in:
parent
72f04af872
commit
b1e57d125b
|
@ -2,7 +2,7 @@
|
|||
#include "lcd.h"
|
||||
#include "KSZ8081RND.h"
|
||||
|
||||
#define ETH_MACMIIAR_CR_MASK 0xFFFFFFE3U
|
||||
#define RW_TIMEOUT_US 500000
|
||||
|
||||
extern TIM_HandleTypeDef htim2;
|
||||
|
||||
|
@ -13,13 +13,13 @@ int KSZ8081RND_run_test(void)
|
|||
DISPLAY_SET_CURSOR(1, 4);
|
||||
|
||||
// enable clocks
|
||||
__HAL_RCC_ETH_CLK_ENABLE();
|
||||
__HAL_RCC_ETH_CLK_ENABLE();
|
||||
|
||||
__HAL_RCC_GPIOC_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOA_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOB_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOC_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOA_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOB_CLK_ENABLE();
|
||||
|
||||
// configure pins
|
||||
// configure pins
|
||||
GPIO_InitTypeDef GPIO_InitStruct = {};
|
||||
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_1|GPIO_PIN_4|GPIO_PIN_5;
|
||||
|
@ -29,27 +29,27 @@ int KSZ8081RND_run_test(void)
|
|||
GPIO_InitStruct.Alternate = GPIO_AF11_ETH;
|
||||
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
|
||||
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_7;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
|
||||
GPIO_InitStruct.Alternate = GPIO_AF11_ETH;
|
||||
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_7;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
|
||||
GPIO_InitStruct.Alternate = GPIO_AF11_ETH;
|
||||
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
||||
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_11|GPIO_PIN_12|GPIO_PIN_13;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
|
||||
GPIO_InitStruct.Alternate = GPIO_AF11_ETH;
|
||||
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_11|GPIO_PIN_12|GPIO_PIN_13;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
|
||||
GPIO_InitStruct.Alternate = GPIO_AF11_ETH;
|
||||
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
|
||||
|
||||
// hardware reset KSZ8081RND
|
||||
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_10, GPIO_PIN_RESET);
|
||||
HAL_Delay(10);
|
||||
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_10, GPIO_PIN_SET);
|
||||
HAL_Delay(10);
|
||||
// hardware reset KSZ8081RND
|
||||
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_10, GPIO_PIN_RESET);
|
||||
HAL_Delay(10);
|
||||
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_10, GPIO_PIN_SET);
|
||||
HAL_Delay(10);
|
||||
|
||||
// enable PLL and REF_CLK output from KSZ8081RND
|
||||
// enable PLL and REF_CLK output from KSZ8081RND
|
||||
uint16_t r;
|
||||
|
||||
if (ReadRegister(0x1F, &r)) {
|
||||
|
@ -64,7 +64,7 @@ int KSZ8081RND_run_test(void)
|
|||
return 1;
|
||||
}
|
||||
|
||||
// switch to RMII interface
|
||||
// switch MAC to RMII interface
|
||||
__HAL_RCC_SYSCFG_CLK_ENABLE();
|
||||
SYSCFG->PMC |= (1 << 23);
|
||||
(void)SYSCFG->PMC;
|
||||
|
@ -77,7 +77,7 @@ int KSZ8081RND_run_test(void)
|
|||
|
||||
while (ETH->DMABMR & 1) {
|
||||
if (TIM2->CNT > (500000 << 4)) {
|
||||
// MAC software reset timed out -> no REF_CLK output?
|
||||
// MAC software reset timed out -> no REF_CLK output from PHY?
|
||||
HAL_TIM_Base_Stop(&htim2);
|
||||
display_write_data_seq("SR ERROR");
|
||||
return 1;
|
||||
|
@ -91,52 +91,46 @@ int KSZ8081RND_run_test(void)
|
|||
|
||||
int ReadRegister(uint32_t reg, uint16_t *value)
|
||||
{
|
||||
uint16_t tmpreg1;
|
||||
uint32_t tickstart;
|
||||
uint16_t tmpreg1;
|
||||
|
||||
tmpreg1 = (reg << 6);
|
||||
tmpreg1 |= 1;
|
||||
ETH->MACMIIAR = tmpreg1;
|
||||
tmpreg1 = (reg << 6);
|
||||
tmpreg1 |= 1;
|
||||
|
||||
tickstart = HAL_GetTick();
|
||||
ETH->MACMIIAR = tmpreg1;
|
||||
|
||||
while ((tmpreg1 & 1) == ETH_MACMIIAR_MB)
|
||||
{
|
||||
if ((HAL_GetTick() - tickstart) > PHY_READ_TO)
|
||||
{
|
||||
return HAL_ERROR;
|
||||
}
|
||||
HAL_TIM_Base_Start(&htim2);
|
||||
|
||||
tmpreg1 = ETH->MACMIIAR;
|
||||
}
|
||||
while (ETH->MACMIIAR & 1) {
|
||||
if (TIM2->CNT > (RW_TIMEOUT_US << 4)) {
|
||||
HAL_TIM_Base_Stop(&htim2);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
*value = (uint16_t) (ETH->MACMIIDR);
|
||||
|
||||
return HAL_OK;
|
||||
HAL_TIM_Base_Stop(&htim2);
|
||||
*value = (uint16_t) (ETH->MACMIIDR);
|
||||
return HAL_OK;
|
||||
}
|
||||
|
||||
int WriteRegister(uint32_t reg, uint16_t value)
|
||||
{
|
||||
uint32_t tmpreg1;
|
||||
uint32_t tickstart;
|
||||
uint32_t tmpreg1;
|
||||
|
||||
tmpreg1 = (reg << 6);
|
||||
tmpreg1 |= 3;
|
||||
tmpreg1 = (reg << 6);
|
||||
tmpreg1 |= 3;
|
||||
|
||||
ETH->MACMIIDR = value;
|
||||
ETH->MACMIIAR = tmpreg1;
|
||||
ETH->MACMIIDR = value;
|
||||
ETH->MACMIIAR = tmpreg1;
|
||||
|
||||
tickstart = HAL_GetTick();
|
||||
HAL_TIM_Base_Start(&htim2);
|
||||
|
||||
while ((tmpreg1 & ETH_MACMIIAR_MB) == ETH_MACMIIAR_MB)
|
||||
{
|
||||
if ((HAL_GetTick() - tickstart) > PHY_WRITE_TO)
|
||||
{
|
||||
return HAL_ERROR;
|
||||
}
|
||||
while (ETH->MACMIIAR & 1) {
|
||||
if (TIM2->CNT > (RW_TIMEOUT_US << 4)) {
|
||||
HAL_TIM_Base_Stop(&htim2);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
tmpreg1 = ETH->MACMIIAR;
|
||||
}
|
||||
|
||||
return HAL_OK;
|
||||
HAL_TIM_Base_Stop(&htim2);
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue