完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
因为F407不支持op code的设置,所以采用gpio模拟smi接口操作,但是读出来的数据有问题,全是0xffff,找不出问题,是delay_us(1)延时太长吗?
[C] 纯文本查看 复制代码 //IO方向#define MDIO_IN() {GPIOA->MODER&=~(3<<(2*2));GPIOA->MODER|=0<<2*2;}//PA2#define MDIO_OUT() {GPIOA->MODER&=~(3<<(2*2));GPIOA->MODER|=1<<2*2;} //PA2#define MDIO PAout(2) //SCL#define MDC PCout(1) //SDA #define READ_MDIO PCin(1) //SDAvoid SMI_Init(void) {//配置PA2 PC1为推完输出,GPIO模拟SMIGPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推完输出GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;//GPIO_PuPd_UP GPIO_PuPd_NOPULLGPIO_Init(GPIOA, &GPIO_InitStructure);//PA2 MDIO GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;//GPIO_PuPd_UP GPIO_PuPd_NOPULLGPIO_Init(GPIOC, &GPIO_InitStructure);//PC1 MDC ,不需要上拉 MDIO = 1; MDC = 0;}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; //上升沿传输数据delay_us(1);if(READ_MDIO) receive|= 0x01; //上升沿,高电平期间读数据MDC=0;//时钟恢复低电平 } return receive;}u8 SMI_Read_2Bit(void){ u8 i, receive=0;// MDIO_IN(); for(i = 0; i < 2; i++) { receive<<=1; MDC=0;//拉低时钟delay_us(1);MDC=1; //上升沿传输数据delay_us(1); if(READ_MDIO) receive|= 0x01; //上升沿,高电平期间读数据 MDC=0;//恢复低电平 } return receive;}void SMI_Write_One_Byte(u8 data){ u8 i;// MDIO_OUT(); for(i = 0; i < 8; i++) { MDC=0; MDIO=(data&0x80)>>7;//准备数据 delay_us(1); MDC=1; //上升沿传输数据 delay_us(1); //保持一段时间 MDC = 0;//恢复低电平 data<<=1; }}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 0x2#define WRITE_OP_CODE_2bit 0x1#define SMI_OP_CODE_2bit 0x00#define SMI_TA 0x02void SMI_Write_Frame(u16 PHYAddress, u16 PHYReg, u16 PHYValue){ u8 addr; addr = (PHYAddress & 0x7) <<5 | (PHYReg & 0x1F); 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_2Bit(0x00); //dir: 0x0 is write SMI_Write_One_Byte(addr); SMI_Write_2Bit(SMI_TA); SMI_Write_One_Byte(0xFF); SMI_Write_One_Byte(PHYValue); MDIO_IN();//恢复输入高阻态}u16 SMI_Read_Reg(u16 PHYAddress, u16 PHYReg){ u8 addr; u16 data; addr = (PHYAddress & 0x7)<<5 | (PHYReg & 0x1F); ////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_2Bit(0x0); //PHYAD[4:3] decide dir: 0x2/0x3 is write SMI_Write_One_Byte(addr); 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;}u8 KSZ8863_Get_Reg_Value(u8 port, u8 reg_num){u16 reg;//reg = ETH_ReadPHYRegister(port, reg_num); //从port1的1号寄存器中读取网络速度和双工模式reg = SMI_Read_Reg(port, reg_num);printf("port %d, reg 0x%x = 0x%x rn", port, reg_num, reg);return reg;}void KSZ8863_Print_Reg_Value(void){u8 i, port;for(port = 1; port < 4; port++){for(i = 0; i < 6; i++){KSZ8863_Get_Reg_Value(port, i);delay_ms(100);}}} |
|
相关推荐
6个回答
|
|
|
|
|
|
|
|
谢谢原子哥,网友博客的分析文章 http://blog.csdn.net/jasonchen_gbd/article/details/51628992
中提到“当PHY向MDIO写数据时,PHY要先发出一个MDC的上升沿,然后等待0-300ns” , 这句话意思是,mac读数据时,mdc时钟是由phy提供的???是这样子么? |
|
|
|
|
|
|
|
楼主方便联系吗?我ID就是QQ
|
|
|
|
我在这个贴看到你的,LZ估计联系不上啦,看到你登录时间最近
|
|
|
|
只有小组成员才能发言,加入小组>>
如何使用STM32+nrf24l01架构把有线USB设备无线化?
2547 浏览 7 评论
请问能利用51单片机和nRF24L01模块实现实时语音无线传输吗?
2332 浏览 5 评论
3161 浏览 3 评论
2804 浏览 8 评论
为什么ucosii上移植lwip后系统进入了HardFault_Handler?
2762 浏览 4 评论
请教各位大咖:有没有接收频率32M左右的芯片推荐的?先感谢啦!
611浏览 1评论
857浏览 0评论
971浏览 0评论
625浏览 0评论
452浏览 0评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-25 05:16 , Processed in 1.062640 second(s), Total 58, Slave 51 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号