write_chip,read_chip

int write_chip(UINT32 addr, UINT32 data)

{

    if(0 == fpgaRWMode)   /* localbus mode */

    {

        UINT16 datah, datal;

        UINT32 dataread;

    

        datah = (UINT16)(data >> 16);

        FPGA_REG(addr) = datah;

        rwWait(60);



        datal = (UINT16)(data & 0xFFFF);

        FPGA_REG(addr) = datal;

        rwWait(60);

    	

    #if 0

        read_chip(addr, &dataread);

        if (data != dataread)

        {

            printf("ERROR:write 0x%08X; read 0x%08X\n", data, dataread);

            return OK;

        }

    #endif

        read_chip(addr, (UINT32 *)(&dataread));

        if(print_level == 1)

            printf("spi write 0x%05X: 0x%08X\n", addr, data);

        return OK;

    }

    else if(1 == fpgaRWMode) /* i2c mode */

    {

        int ret = 0;

        

        ret = fpgaMAC_i2c_write(0,0,0,addr,(unsigned char *)(&data),4);



    	  if(print_level == 1)

    	  {

    	      if(ret != 0)

    	          printf("spi write failed! read 0x%05x: 0x%08x\n", addr, data);

    	      else

                printf("spi write 0x%05x: 0x%08x\n", addr, data);

        }



    	  if(!ret)

            return OK;

        else

            return ERROR;

    }

    else if(2 == fpgaRWMode) /* spi mode */

    {

        int ret = 0;



		unsigned char *wtBuff = (unsigned char *)malloc(8);

		if(NULL == wtBuff)

		{

			printf("No memory!\n");

			return -1;

		}



		*wtBuff 	= (unsigned char)((data>>24) & 0xff);

		*(wtBuff+1) = (unsigned char)((data>>16) & 0xff);

		*(wtBuff+2) = (unsigned char)((data>>8 ) & 0xff);

		*(wtBuff+3) = (unsigned char)((data    ) & 0xff);

		

        ret = fpga_spi_write(addr,(unsigned char *)(wtBuff),4);

		

		

    	  if(print_level == 1)

    	  {

    	      if(ret != 0)

    	          printf("spi write failed! read 0x%05x: 0x%08x\n", addr, data);

    	      else

                printf("spi write 0x%05x: 0x%08x\n", addr, data);

         }



		free(wtBuff);  



    	  if(!ret)

            return OK;

        else

            return ERROR;

    }

    else

    {

        return ERROR;

    }

}



int read_chip(UINT32 addr, UINT32 *data)

{

    if(data == NULL)

        return ERROR;



    if(0 == fpgaRWMode)   /* localbus mode */

    {

        UINT16 datah, datal;

        

        datah = FPGA_REG(addr);

        rwWait(60);

        

        datal = FPGA_REG(addr);

        rwWait(60);

    

        *data = datal + (((UINT32)datah) << 16);



    	  if(print_level == 1)

        printf("localbus read 0x%05X: 0x%08X\n", addr, *data);



    	  return OK;

    }

    else if(1 == fpgaRWMode)  /* i2c mode */

    {

        int ret = 0;



        ret = fpgaMAC_i2c_read(0,0,0,addr,(unsigned char *)data,4);

        

    	  if(print_level == 1)

    	  {

    	      if(ret != 0)

    	          printf("i2c read failed! read 0x%05x: 0x%08x\n", addr, *data);

    	      else

                printf("i2c read 0x%05x: 0x%08x\n", addr, *data);

        }



    	  if(!ret)

            return OK;

        else

            return ERROR;

    }

    else if(2 == fpgaRWMode)  /* spi mode */

    {

        int ret = 0;



        ret = fpga_spi_read(addr,(unsigned char *)data,4);

        

    	  if(print_level == 1)

    	  {

    	      if(ret != 0)

    	          printf("spi read failed! read 0x%05x: 0x%08x\n", addr, *data);

    	      else

                printf("spi read 0x%05x: 0x%08x\n", addr, *data);

        }



    	  if(!ret)

            return OK;

        else

            return ERROR;

    }

    else

    {

        return ERROR;

    }

}

  

你可能感兴趣的:(write)