打印
[STM32F7]

W5300调试UDP通信时,发送数据正常,接收数据丢失最高位

[复制链接]
1041|5
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
chenwang1996|  楼主 | 2020-9-27 15:13 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
W5300调试UDP通信时,发送数据正常,接收数据丢失最高位。STM32F767芯片接W5300,另一端接PC网络调试助手。采用8位数据模式。STM32发送0XFF或0XAA,PC网络调试助手能收到0XFF或0XAA,但是网络调试助手发送0XFF或0XAA,STM32从W5300的Sn_RX_FIFOR读出来是0X7F或0X2A。STM32程序如下:
void LCD_Init(void)
{           
    GPIO_InitTypeDef GPIO_Initure;
        FMC_NORSRAM_TimingTypeDef FSMC_ReadWriteTim;
    FMC_NORSRAM_TimingTypeDef FSMC_WriteTim;


    LCD_MPU_Config();                       //ʹÄÜMPU±£»¤LCDÇøÓò
    SRAM_Handler.Instance=FMC_NORSRAM_DEVICE;      
    SRAM_Handler.Extended=FMC_NORSRAM_EXTENDED_DEVICE;      
   
    SRAM_Handler.Init.NSBank=FMC_NORSRAM_BANK3;     //ʹÓÃNE1
    SRAM_Handler.Init.DataAddressMux=FMC_DATA_ADDRESS_MUX_DISABLE; //²»¸´ÓÃÊý¾ÝÏß
    SRAM_Handler.Init.MemoryType=FMC_MEMORY_TYPE_SRAM;   //SRAM
    SRAM_Handler.Init.MemoryDataWidth=FMC_NORSRAM_MEM_BUS_WIDTH_8; //16λÊý¾Ý¿í¶È
    SRAM_Handler.Init.BurstAccessMode=FMC_BURST_ACCESS_MODE_DISABLE; //ÊÇ·ñʹÄÜÍ»·¢·ÃÎÊ,½ö¶Ôͬ²½Í»·¢´æ´¢Æ÷ÓÐЧ,´Ë´¦Î´Óõ½
    SRAM_Handler.Init.WaitSignalPolarity=FMC_WAIT_SIGNAL_POLARITY_LOW;//µÈ´ýÐźŵļ«ÐÔ,½öÔÚÍ»·¢Ä£Ê½·ÃÎÊÏÂÓÐÓÃ
    SRAM_Handler.Init.WaitSignalActive=FMC_WAIT_TIMING_DURING_WS;//FMC_WAIT_TIMING_BEFORE_WS;   //´æ´¢Æ÷ÊÇÔڵȴýÖÜÆÚ֮ǰµÄÒ»¸öʱÖÓÖÜÆÚ»¹ÊǵȴýÖÜÆÚÆÚ¼äʹÄÜNWAIT
    SRAM_Handler.Init.WriteOperation=FMC_WRITE_OPERATION_ENABLE;    //´æ´¢Æ÷дʹÄÜ
    SRAM_Handler.Init.WaitSignal=FMC_WAIT_SIGNAL_DISABLE;           //µÈ´ýʹÄÜλ,´Ë´¦Î´Óõ½
    SRAM_Handler.Init.ExtendedMode=FMC_EXTENDED_MODE_ENABLE;        //¶ÁдʹÓò»Í¬µÄʱÐò
    SRAM_Handler.Init.AsynchronousWait=FMC_ASYNCHRONOUS_WAIT_DISABLE;//ÊÇ·ñʹÄÜͬ²½´«ÊäģʽϵĵȴýÐźÅ,´Ë´¦Î´Óõ½
    SRAM_Handler.Init.WriteBurst=FMC_WRITE_BURST_DISABLE;           //½ûֹͻ·¢Ð´
    SRAM_Handler.Init.ContinuousClock=FMC_CONTINUOUS_CLOCK_SYNC_ASYNC;

//          FSMC_ReadWriteTim.AddressSetupTime=0x15;    //µØÖ·½¨Á¢Ê±¼ä(ADDSET)Ϊ17¸öHCLK 1/216M=4.6ns*17=78ns//01
//    FSMC_ReadWriteTim.AddressHoldTime=0x01;
//    FSMC_ReadWriteTim.DataSetupTime=0x15;       //Êý¾Ý±£´æʱ¼ä(DATAST)Ϊ85¸öHCLK        =4.6*85=391ns
          FSMC_ReadWriteTim.AddressSetupTime=0x15;    //µØÖ·½¨Á¢Ê±¼ä(ADDSET)Ϊ17¸öHCLK 1/216M=4.6ns*17=78ns//01
    FSMC_ReadWriteTim.AddressHoldTime=0x01;
    FSMC_ReadWriteTim.DataSetupTime=0x15;       //Êý¾Ý±£´æʱ¼ä(DATAST)Ϊ85¸öHCLK        =4.6*85=391ns
//         FSMC_ReadWriteTim.BusTurnAroundDuration = 0;
//          FSMC_ReadWriteTim.CLKDivision = 0;
//          FSMC_ReadWriteTim.DataLatency = 0;
       
    FSMC_ReadWriteTim.AccessMode=FMC_ACCESS_MODE_B; //ģʽA
    //FSMCдʱÐò¿ØÖƼĴæÆ÷
    FSMC_WriteTim.AddressSetupTime=0x15;        //µØÖ·½¨Á¢Ê±¼ä(ADDSET)Ϊ21¸öHCLK=96ns
    FSMC_WriteTim.AddressHoldTime=0x00;
    FSMC_WriteTim.DataSetupTime=0x55;           //Êý¾Ý±£´æʱ¼ä(DATAST)Ϊ4.6ns*21¸öHCLK=96ns//10
    FSMC_WriteTim.AccessMode=FMC_ACCESS_MODE_A;     //ģʽA
        HAL_SRAM_Init(&SRAM_Handler,&FSMC_ReadWriteTim,&FSMC_WriteTim);       
        delay_ms(50); // delay 50 ms
}  
void W5300_Config(void)
{
        unsigned char *ptr;
//        unsigned char *ptr;
   HAL_GPIO_WritePin(GPIOB,W5300_RST,GPIO_PIN_SET);        //PB1ÖÃ0
        //GPIO_SetBits(GPIOE, W5300_RST);
        /* Wait Ethernet Link */
        //while(HAL_GPIO_ReadPin(GPIOG, L_LINK)==1);
  delay_ms(10); // delay 50 ms
/*        ptr=(unsigned char*)MR;
        ip=*ptr;
        */
        UDP_OK=0;
        ptr=(unsigned char*)MR1;
        *ptr=0x80;
         delay_ms(15);

        ptr=(unsigned char*)MR1;
        *ptr=0x00;
        ptr=(unsigned char*)MR;
        *ptr=0x38;
//         delay_ms(10);
//        ptr=(unsigned char*)MR1;
//        ip=*ptr;
  delay_ms(10);
//ÅäÖÃÖжÏ
        ptr=(unsigned char*)IR;
        *ptr=0x00;
        ptr=(unsigned char*)IR1;
        *ptr=0x00;
        /* Set Gateway address as "192.168.0.1" */
        ptr=(unsigned char*)GAR;                                                /* Set Gateway IP */
  *ptr=0xc0;
        ptr=(unsigned char*)GAR1;                                                /* #define GAR2            (GAR + 2)      Set Gateway IP */
        *ptr=0xa8;
        ptr=(unsigned char*)GAR2;                                                /* Set Gateway IP */
  *ptr=0x01;
        ptr=(unsigned char*)GAR3;                                                /* #define GAR2            (GAR + 2)      Set Gateway IP */
        *ptr=0x01;

        /* Set IP address as "192.168.0.20" */
        ptr=(unsigned char*)SIPR;
        *ptr=0xc0;
        ptr=(unsigned char*)SIPR1;
        *ptr=0xa8;
        ptr=(unsigned char*)SIPR2;
        *ptr=0x01;
        ptr=(unsigned char*)SIPR3;
        *ptr=0x14;
        /* Set Subnet mask as "255.255.255.0" */
        ptr=(unsigned char*)SUBR;
        /**ptr++=0xffff;
        *ptr=0xff00;
        */
        *ptr=0xFF;
        ptr=(unsigned char*)SUBR1;
        *ptr=0xff;
        ptr=(unsigned char*)SUBR2;
        *ptr=0xff;
        ptr=(unsigned char*)SUBR3;
        *ptr=0x00;
        ptr=(unsigned char*)SUBR2;                                                /* Set Gateway IP */
        ip=*ptr;
        delay_ms(15);
        /* Set MAC address as "48 53 00 31 30 33"*/
        ptr=(unsigned char*)SHAR;
        *ptr=0x00;
        ptr=(unsigned char*)SHAR1;
        *ptr=0x08;
        ptr=(unsigned char*)SHAR2;
        *ptr=0xDC;
        ptr=(unsigned char*)SHAR3;
        *ptr=0x01;
        ptr=(unsigned char*)SHAR4;
        *ptr=0x02;
        ptr=(unsigned char*)SHAR4;
        *ptr=0x03;
        //ÉèÖÃÄÚ´æ´óС
                ptr=(unsigned char*)TMS01R;
         *ptr=0x04;
         ptr=(unsigned char*)TMSR1;
         *ptr=0x10;
          ptr=(unsigned char*)TMSR2;
         *ptr=0x01;
          ptr=(unsigned char*)TMSR3;
         *ptr=0x14;
          ptr=(unsigned char*)TMSR4;
         *ptr=0x00;
          ptr=(unsigned char*)TMSR5;
         *ptr=0x07;
          ptr=(unsigned char*)TMSR6;
         *ptr=0x0c;
          ptr=(unsigned char*)TMSR7;
         *ptr=0x0c;
         //
          ptr=(unsigned char*)RMS01R;
         *ptr=0x11;
                 ptr=(unsigned char*)RMSR1;
         *ptr=0x03;
                 ptr=(unsigned char*)RMSR2;
         *ptr=0x05;
                 ptr=(unsigned char*)RMSR3;
         *ptr=0x10;
                 ptr=(unsigned char*)RMSR4;
         *ptr=0x03;
                 ptr=(unsigned char*)RMSR5;
         *ptr=0x04;
                 ptr=(unsigned char*)RMSR6;
         *ptr=0x04;
                 ptr=(unsigned char*)RMSR7;
         *ptr=0x04;
        //ÉèÖÃÄÚ´æ
         ptr=(unsigned char*)MTYPER0;
        *ptr=0x01;
         ptr=(unsigned char*)MTYPER1;
        *ptr=0xff;
        /* Set interrupt mask */
                ptr=(unsigned char*)IMR;
        *ptr=IR_CONFLICT;
        ptr=(unsigned char*)IMR1;
        *ptr=0x01;
         delay_ms(10); // delay 50 ms
                 ptr=(unsigned char*)IMR1;                                       
     ip=*ptr;
                  delay_ms(10); // delay 50 ms
}

unsigned int Socket0_UDP(void)
{
        unsigned char *ptr;
        unsigned char i;

        /* Set Socket Port Number as 5000*/
/*        ptr=(unsigned short*)Sn_PORTR(0);
        *ptr=5000;*/
        ptr=(unsigned char*)Sn_PORTR(0);
  *ptr=0x13;
        ptr=(unsigned char*)Sn_PORTR1(0);
        *ptr=0x88;//0x1388
        /* Set Destination IP  as "192.168.0.30" */

        ptr=(unsigned char*)Sn_DIPR(0);
  *ptr=0xc0;
        ptr=(unsigned char*)Sn_DIPR1(0);
        *ptr=0xA8;
        ptr=(unsigned char*)Sn_DIPR2(0);
        *ptr=0x01;
        ptr=(unsigned char*)Sn_DIPR3(0);
        *ptr=0x16;
       
       
        //ÎïÀíµØÖ·
                ptr=(unsigned char*)Sn_DHAR(0);
        *ptr=0x28;
        ptr=(unsigned char*)Sn_DHAR1(0);
        *ptr=0xd2;
        ptr=(unsigned char*)Sn_DHAR2(0);
        *ptr=0x44;
        ptr=(unsigned char*)Sn_DHAR3(0);
        *ptr=0x3e;
        ptr=(unsigned char*)Sn_DHAR4(0);
        *ptr=0x77;
        ptr=(unsigned char*)Sn_DHAR5(0);
        *ptr=0x3a;
       
       
       
        /* Set Destination Port number as 5000 */
        /*ptr=(unsigned short*)Sn_DPORTR(0);
        *ptr=5000;*/
        ptr=(unsigned char*)Sn_DPORTR(0);
  *ptr=0x13;
        ptr=(unsigned char*)Sn_DPORTR(0);
        *ptr=0x88;//0x1388
   ptr=(unsigned char*)Sn_MSSR(0);
        *ptr=0x05;
         ptr=(unsigned char*)Sn_MSSR1(0);
        *ptr=0xb4;//0x5b4
        /* Set Socket0 in UDP mode */
        ptr=(unsigned char*)Sn_MR1(0);
        *ptr=0x02;//

//                ptr=(unsigned char*)Sn_CR1(0);
//                *ptr=Sn_CR_CLOSE;
//        delay_ms(10);
        /* Open Socket0 */
        ptr=(unsigned char*)Sn_CR1(0);
        *ptr=Sn_CR_OPEN;
       
        delay_ms(10);
        ptr=(unsigned char*)Sn_SSR1(0);
        i=*ptr;
        ip=i;
        if((i&0xff)!=SOCK_UDP)                /* If failed to open Socket0, close Socket0 and reurn false */
        {
                ptr=(unsigned char*)Sn_CR1(0);
                *ptr=Sn_CR_CLOSE;
                delay_ms(10);
                return FALSE;
        }
        S0_SendOK=1;
        //GPIO_ResetBits(GPIOF, D_OUTPUT2);                /* If UDP Ready, light LED2*/
        return TRUE;
}

/* Read data from RX buffer and dump them to S_buffer */
unsigned char S0_udprx_process(void)
{
        unsigned char*ptr;
        unsigned short rx_size;
        unsigned int i,j;


        ptr=(unsigned char*)Sn_RX_RSR0(0);
        ip=*ptr;
        ptr=(unsigned char*)Sn_RX_RSR1(0);
        ip=*ptr;
        ptr=(unsigned char*)Sn_RX_RSR2(0);
        ip=*ptr;
        ptr=(unsigned char*)Sn_RX_RSR3(0);
        ip=*ptr;
//ip=rx_size;
                 delay_us(10);
        if(ip==0)                
                return 0;
//ip
        ptr=(unsigned char*)Sn_RX_FIFOR(0);
        IP_Buffer[0]=*ptr;
        ptr=(unsigned char*)Sn_RX_FIFOR1(0);
        IP_Buffer[1]=*ptr;
         delay_us(30);
//        rx_size=S_Buffer[0]<<8+S_Buffer[1];
        ptr=(unsigned char*)Sn_RX_FIFOR(0);
        IP_Buffer[2]=*ptr;
        ptr=(unsigned char*)Sn_RX_FIFOR1(0);
        IP_Buffer[3]=*ptr;
         delay_us(30);
        //¶Ë¿Ú
        ptr=(unsigned char*)Sn_RX_FIFOR(0);
        IP_Buffer[4]=*ptr;
        ptr=(unsigned char*)Sn_RX_FIFOR1(0);
        IP_Buffer[5]=*ptr;
         delay_us(30);
  //Êý¾Ý
                ptr=(unsigned char*)Sn_RX_FIFOR(0);
        IP_Buffer[6]=*ptr;
        ptr=(unsigned char*)Sn_RX_FIFOR1(0);
        IP_Buffer[7]=*ptr;
         delay_us(30);
         //Êý¾Ý´¦Àí
        i=(IP_Buffer[6]<<8)+IP_Buffer[7];
        if(i%2!=0) i++;
        //i=i/2;
  for(j=0;j<i;j++)
        {
        ptr=(unsigned char*)Sn_RX_FIFOR(0);
        R_Buffer[j]=*ptr;
                j++;
        ptr=(unsigned char*)Sn_RX_FIFOR1(0);
        R_Buffer[j]=*ptr;
         delay_us(30);
        }
        ptr=(unsigned char*)Sn_CR(0);                /* Set RECV command */
        *ptr=Sn_CR_RECV;

        return i;
}


使用特权

评论回复
沙发
chenwang1996|  楼主 | 2020-9-27 15:39 | 只看该作者
大神们,帮我看看是哪里的问题,STM32从W5300读其他寄存器时,也能正常读出正确的数据,比如读子网掩码寄存器SUBR2,也能读出0XFF。读Sn_RX_FIFOR就丢失最高位。

        ptr=(unsigned char*)SUBR2;
        *ptr=0xff;
        ptr=(unsigned char*)SUBR3;
        *ptr=0x00;
        ptr=(unsigned char*)SUBR2;                                                /* Set Gateway IP */
        ip=*ptr;
        delay_ms(15);

使用特权

评论回复
板凳
观海| | 2020-10-12 08:29 | 只看该作者
通讯速度是多少啊

使用特权

评论回复
地板
guanjiaer| | 2020-10-12 08:29 | 只看该作者
楼主找到问题了吗

使用特权

评论回复
5
chenwang1996|  楼主 | 2020-10-12 16:07 | 只看该作者
guanjiaer 发表于 2020-10-12 08:29
楼主找到问题了吗

找到了,FMC_D7口被其他电路影响,电平被强制拉低了

使用特权

评论回复
6
chenwang1996|  楼主 | 2020-10-12 16:19 | 只看该作者
观海 发表于 2020-10-12 08:29
通讯速度是多少啊

通讯速度不清楚,但是问题已解决,电路问题,FMC_D7口接到了另一个模块上,另一个模块没关闭,导致FMC_D7无法拉高。

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1

主题

4

帖子

0

粉丝