[KSZ8081RND] simplify read/write functions

This commit is contained in:
ІО-23 Шмуляр Олег 2025-05-10 13:44:32 +03:00
parent 72f04af872
commit b1e57d125b
1 changed files with 54 additions and 60 deletions

View File

@ -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;
@ -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;
@ -92,33 +92,29 @@ int KSZ8081RND_run_test(void)
int ReadRegister(uint32_t reg, uint16_t *value)
{
uint16_t tmpreg1;
uint32_t tickstart;
tmpreg1 = (reg << 6);
tmpreg1 |= 1;
ETH->MACMIIAR = tmpreg1;
tickstart = HAL_GetTick();
HAL_TIM_Base_Start(&htim2);
while ((tmpreg1 & 1) == ETH_MACMIIAR_MB)
{
if ((HAL_GetTick() - tickstart) > PHY_READ_TO)
{
return HAL_ERROR;
}
tmpreg1 = ETH->MACMIIAR;
while (ETH->MACMIIAR & 1) {
if (TIM2->CNT > (RW_TIMEOUT_US << 4)) {
HAL_TIM_Base_Stop(&htim2);
return 1;
}
}
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;
tmpreg1 = (reg << 6);
tmpreg1 |= 3;
@ -126,17 +122,15 @@ int WriteRegister(uint32_t reg, uint16_t value)
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;
}