137 lines
2.9 KiB
C
137 lines
2.9 KiB
C
#include "main.h"
|
|
#include "lcd.h"
|
|
#include "KSZ8081RND.h"
|
|
|
|
#define RW_TIMEOUT_US 500000
|
|
|
|
extern TIM_HandleTypeDef htim2;
|
|
|
|
int KSZ8081RND_run_test(void)
|
|
{
|
|
DISPLAY_CLEAR;
|
|
display_write_data_seq("KSZ8081RND ETH");
|
|
DISPLAY_SET_CURSOR(1, 4);
|
|
|
|
// enable clocks
|
|
__HAL_RCC_ETH_CLK_ENABLE();
|
|
|
|
__HAL_RCC_GPIOC_CLK_ENABLE();
|
|
__HAL_RCC_GPIOA_CLK_ENABLE();
|
|
__HAL_RCC_GPIOB_CLK_ENABLE();
|
|
|
|
// configure pins
|
|
GPIO_InitTypeDef GPIO_InitStruct = {};
|
|
|
|
GPIO_InitStruct.Pin = GPIO_PIN_1|GPIO_PIN_4|GPIO_PIN_5;
|
|
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(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_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);
|
|
|
|
// enable PLL and REF_CLK output from KSZ8081RND
|
|
uint16_t r;
|
|
|
|
if (ReadRegister(0x1F, &r)) {
|
|
display_write_data_seq("READ ERROR");
|
|
return 1;
|
|
}
|
|
|
|
r |= (1 << 7);
|
|
|
|
if (WriteRegister(0x1F, r)) {
|
|
display_write_data_seq("WRITE ERROR");
|
|
return 1;
|
|
}
|
|
|
|
// switch MAC to RMII interface
|
|
__HAL_RCC_SYSCFG_CLK_ENABLE();
|
|
SYSCFG->PMC |= (1 << 23);
|
|
(void)SYSCFG->PMC;
|
|
|
|
// check if software reset happens on MAC
|
|
TIM2->CNT = 0;
|
|
|
|
ETH->DMABMR |= 1; // assert software reset
|
|
HAL_TIM_Base_Start(&htim2);
|
|
|
|
while (ETH->DMABMR & 1) {
|
|
if (TIM2->CNT > (500000 << 4)) {
|
|
// MAC software reset timed out -> no REF_CLK output from PHY?
|
|
HAL_TIM_Base_Stop(&htim2);
|
|
display_write_data_seq("SR ERROR");
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
HAL_TIM_Base_Stop(&htim2);
|
|
display_write_data_seq("OK");
|
|
return 0;
|
|
}
|
|
|
|
int ReadRegister(uint32_t reg, uint16_t *value)
|
|
{
|
|
uint16_t tmpreg1;
|
|
|
|
tmpreg1 = (reg << 6);
|
|
tmpreg1 |= 1;
|
|
|
|
ETH->MACMIIAR = tmpreg1;
|
|
|
|
HAL_TIM_Base_Start(&htim2);
|
|
|
|
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;
|
|
|
|
tmpreg1 = (reg << 6);
|
|
tmpreg1 |= 3;
|
|
|
|
ETH->MACMIIDR = value;
|
|
ETH->MACMIIAR = tmpreg1;
|
|
|
|
HAL_TIM_Base_Start(&htim2);
|
|
|
|
while (ETH->MACMIIAR & 1) {
|
|
if (TIM2->CNT > (RW_TIMEOUT_US << 4)) {
|
|
HAL_TIM_Base_Stop(&htim2);
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
HAL_TIM_Base_Stop(&htim2);
|
|
return 0;
|
|
}
|