一、时序介绍
使用MDC 、MDIO两只引脚进行数据配置。时序为:
二、底层驱动代码
1、读PHY寄存器地址:
u16 PHY_WAN_Reg_Read( u8 PhyAddress, u8 RegisterAddress )
{
u8 i;
u8 bValue;
u16 wValue;
PHY_WAN_MDIO_Out();
//Preamble
for( i = 0; i < 32; i ++ )
{
PHY_WAN_WRITE_BIT(1);
}
//Start
PHY_WAN_WRITE_BIT(0);
PHY_WAN_WRITE_BIT(1);
//Opcode
PHY_WAN_WRITE_BIT(1);
PHY_WAN_WRITE_BIT(0);
//PHY Address
for( i = 0; i < 5; i ++ )
{
if( 0x10 & PhyAddress )
{
PHY_WAN_WRITE_BIT(1);
}
else
{
PHY_WAN_WRITE_BIT(0);
}
PhyAddress <<= 1;
}
MDC_DELAY;
MDC_DELAY;
//Register Address
for( i = 0; i < 5; i ++ )
{
if( 0x10 & RegisterAddress )
{
PHY_WAN_WRITE_BIT(1);
}
else
{
PHY_WAN_WRITE_BIT(0);
}
RegisterAddress <<= 1;
}
MDC_DELAY;
MDC_DELAY;
//TA: change MDIO to INPUT
/* tri-state our MDIO I/O pin so we can read */
PHY_WAN_MDC(0);PHY_WAN_MDIO_In();MDC_DELAY;PHY_WAN_MDC(1);MDC_DELAY;
/* check the turnaround bit: the PHY should be driving it to zero */
PHY_WAN_MDIO_READ(bValue);
if( 0 != bValue )
{
//PHY_ASSERT;
}
//<READ> Register Data
wValue = 0;
for( i = 0; i < 16; i ++ )
{
wValue <<= 1;
PHY_WAN_READ_BIT(bValue);
if( bValue )
{
wValue |= 0x0001;
}
}
//Idle: change MDIO to OUTPUT
PHY_WAN_MDC(0);PHY_WAN_MDIO_Out();MDC_DELAY;PHY_WAN_MDC(1);MDC_DELAY;
for( i = 0; i < 32; i ++ )
{
PHY_WAN_WRITE_BIT(1);
}
return wValue;
}
2、写寄存器
void PHY_WAN_Reg_Write( u8 PhyAddress, u8 RegisterAddress, u16 RegisterData )
{
u8 i;
PHY_WAN_MDIO_Out();
//Preamble
for( i = 0; i < 32; i ++ )
{
PHY_WAN_WRITE_BIT(1);
}
//Start
PHY_WAN_WRITE_BIT(0);
PHY_WAN_WRITE_BIT(1);
//Opcode
PHY_WAN_WRITE_BIT(0);
PHY_WAN_WRITE_BIT(1);
//PHY Address
for( i = 0; i < 5; i ++ )
{
if( 0x10 & PhyAddress )
{
PHY_WAN_WRITE_BIT(1);
}
else
{
PHY_WAN_WRITE_BIT(0);
}
PhyAddress <<= 1;
}
//Register Address
for( i = 0; i < 5; i ++ )
{
if( 0x10 & RegisterAddress )
{
PHY_WAN_WRITE_BIT(1);
}
else
{
PHY_WAN_WRITE_BIT(0);
}
RegisterAddress <<= 1;
}
//TA
PHY_WAN_WRITE_BIT(1);
PHY_WAN_WRITE_BIT(0);
//<WRITE> Register Data
for( i = 0; i < 16; i ++ )
{
if( 0x8000 & RegisterData )
{
PHY_WAN_WRITE_BIT(1);
}
else
{
PHY_WAN_WRITE_BIT(0);
}
RegisterData <<= 1;
}
//Idle
PHY_WAN_MDC(0);PHY_WAN_MDIO_Out();MDC_DELAY;PHY_WAN_MDC(1);MDC_DELAY;
for( i = 0; i < 32; i ++ )
{
PHY_WAN_WRITE_BIT(1);
}
}
3、说明
其中 PhyAddress 为PHY芯片的 SMI Device Address。RegisterAddress 为PHY芯片的寄存器地址。