高级会员
- 积分
- 873
- 金钱
- 873
- 注册时间
- 2018-2-9
- 在线时间
- 76 小时
|
1金钱
下面是我的代码。delay和串口没有放上去。串口是可以正常通的
[mw_shl_code=c,true]#include "me_dio.h"
#define MDIO_IN() {GPIOA->CRH &= ~(15<<16);GPIOA->CRH |= 8<<16;GPIOA->ODR |= 1<<12;}
#define MDIO_OUT() {GPIOA->CRH &= ~(15<<16);GPIOA->CRH |= 3<<16;}
#define MDIO PAout(12) //SCL
#define MDC PAout(11) //SDA
#define READ_MDIO PAin(12) //SDA
/*******************************************************************************
* Function Name : Mdio_Init
* Description : 1ü½ÅÅäÖÃ
* Param : None
* Attention : None
*******************************************************************************/
void Mdio_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Configure Mdio pins: PA11 -> MDC and PA12 -> MDIO */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //íÆíêêä3ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_Init(GPIOA, &GPIO_InitStructure);
MDIO = 1;
MDC = 0;
}
/*******************************************************************************
* Function Name : SMI_Read_One_Byte
* Description : ¶áè¡ò»¸ö×Ö½ú
* param : None
* Return : None
* Attention : None
*******************************************************************************/
u8 SMI_Read_One_Byte(void)
{
u8 i, receive=0;
// MDIO_IN();
for(i = 0; i < 8; i++)
{
receive<<=1;
MDC = 0;//à-μíê±Öó
delay_us(1);
MDC = 1; //éÏéyÑØ′«êäêy¾Y
delay_us(1);
if(READ_MDIO)
{receive |= 0x01;} //éÏéyÑظßμçƽÆú¼ä¶áè¡êy¾Y
//delay_us(1);
MDC = 0;//
}
return receive;
}
/*****************************************************************
* Function Name : SMI_Read_2Bit
* Description : ¶áè¡á½¸öbit
* param : None
* Return : None
* Attention : None
******************************************************************/
u8 SMI_Read_2Bit(void)
{
u8 i, receive=0;
for(i = 0; i < 2; i++)
{
receive<<=1;
MDC = 0;//à-μíê±Öó
delay_us(1);
MDC = 1; //éÏéyÑØ′«êäêy¾Y
delay_us(1);
if(READ_MDIO)
{receive |= 0x01;} //éÏéyÑظßμçƽÆú¼ä¶áêy¾Y
MDC = 0;//»Ö¸′μíμçƽ
}
return receive;
}
/*****************************************************************
* Function Name : SMI_Write_One_Byte
* Description : D′èëò»¸ö×Ö½ú
* param : òaD′èëμÄêy¾Y
* Return : None
* Attention : None
******************************************************************/
void SMI_Write_One_Byte(u8 data)
{
u8 i;
for(i = 0; i < 8; i++)
{
MDC = 0;
MDIO = ((data&0x80)>>7);
delay_us(1);
MDC = 1;
delay_us(1);
MDC = 0;
data<<=1;
}
}
/*****************************************************************
* Function Name : SMI_Write_2Bit
* Description : D′èëὸöBit
* param : òaD′èëμÄêy¾Y
* Return : None
* Attention : None
******************************************************************/
void SMI_Write_2Bit(u8 data)
{
u8 i;
MDIO_OUT();
for(i = 0; i < 2; i++)
{
MDC = 0;
MDIO = (data&0x2)>>1;
delay_us(1);
MDC = 1;
delay_us(1);
MDC = 0;
data<<=1;
}
}
#define START_OF_FRAME_2bit 0x01
#define READ_OP_CODE_2bit 0x02
#define WRITE_OP_CODE_2bit 0x01
#define SMI_OP_CODE_2bit 0x00
#define SMI_TA 0x02
/*****************************************************************
* Function Name : SMI_Write_Frame
* Description : D′èëò»¸öêy¾YÖ¡
* PHYAddress : PHYμÄμØÖ·
* RHYReg : PHYμļÄ′æÆ÷μØÖ·
* PHYValue : òaD′èë¼Ä′æÆ÷μÄêy¾Y
* Attention : None
******************************************************************/
void SMI_Write_Frame(u16 PHYAddress, u16 PHYReg, u16 PHYValue)
{
u8 addr;
addr = (PHYAddress & 0xFF) <<3 | (PHYReg & 0xFF)>>5;
MDIO_OUT();
////32 bit Preamble
SMI_Write_One_Byte(0xFF);
SMI_Write_One_Byte(0xFF);
SMI_Write_One_Byte(0xFF);
SMI_Write_One_Byte(0xFF);
SMI_Write_One_Byte(0xFF);
SMI_Write_One_Byte(0xFF);
SMI_Write_One_Byte(0xFF);
SMI_Write_One_Byte(0xFF);
SMI_Write_2Bit(START_OF_FRAME_2bit);
SMI_Write_2Bit(WRITE_OP_CODE_2bit);
SMI_Write_One_Byte(addr);
SMI_Write_2Bit(PHYReg & 0x03);
SMI_Write_2Bit(SMI_TA);
SMI_Write_One_Byte(0xFF);
SMI_Write_One_Byte(PHYValue);
MDIO_IN();//»Ö¸′¸ß×è
}
/*****************************************************************
* Function Name : SMI_read_Reg
* Description : ¶áè¡êy¾YμÄò»¸ö¼Ä′æÆ÷
* PHYAddress : PHYD¾Æ¬μÄμØÖ·
* RHYReg : PHYμļÄ′æÆ÷μØÖ·
* Attention : None
******************************************************************/
u16 SMI_Read_Reg(u16 PHYAddress, u16 PHYReg)
{
u8 addr;
u16 data;
addr = ((PHYAddress & 0xFF)<<3) | ((PHYReg & 0xFF)>>5);
////32 bit Preamble
MDIO_OUT();
SMI_Write_One_Byte(0xFF);
SMI_Write_One_Byte(0xFF);
SMI_Write_One_Byte(0xFF);
SMI_Write_One_Byte(0xFF);
SMI_Write_2Bit(START_OF_FRAME_2bit);
SMI_Write_2Bit(READ_OP_CODE_2bit);
SMI_Write_One_Byte(addr);
SMI_Write_2Bit(PHYReg & 0x03);
MDIO_IN();
data = SMI_Read_2Bit();//SMI_TA should be Z0
data = SMI_Read_One_Byte();
data = data << 8;
data = data | SMI_Read_One_Byte();
return data;
}
/*****************************************************************
* Function Name : Get_Reg_Value
* Description : »ñè¡ò»¸ö¼Ä′æÆ÷μÄÖμ
* port : Ö¸¶¨PHY¶Ë¿úoÅ
* PHYAddress : Ö¸¶¨¼Ä′æÆ÷μıêoÅ
* Return : ·μ»Ø»ñè¡μ½μÄêy¾Y16λ
* Attention : None
******************************************************************/
u16 Get_Reg_Value(u8 port, u8 reg_num)
{
u16 reg;
reg = SMI_Read_Reg(port, reg_num);
return reg;
}
/*****************************************************************
* Function Name : Print_Reg_Value
* Description : ½«»ñè¡μ½μļÄ′æÆ÷Öμ·¢Ëíμ½′®¿ú
* Attention : None
******************************************************************/
void Print_Reg_Value(void)
{
u16 send_data;
u8 i,port;
for(port = 0; port < 31; port++){
for(i = 0; i < 31; i++){
send_data = Get_Reg_Value(port, 0);
USART_SendData(USART2, send_data);
delay_ms(10);
USART_SendData(USART2, port);
delay_ms(10);
USART_SendData(USART2, i);
delay_ms(10);
}
}
}
/*****************************************************************
* Function Name : main
* Description : Ö÷oˉêy
* Attention : 0
******************************************************************/
int main(void)
{
delay_init(); //Ñóê±oˉêy3õê¼»ˉ
uart_init(115200); //′®¿ú3õê¼»ˉÎa115200
Mdio_Init();
while(1)
{
Print_Reg_Value();
}
return 0;
}[/mw_shl_code]
|
|