+ Post New Thread
Results 1 to 2 of 2
  1. #1
    Member level 3
    Points: 2,007, Level: 10
    Achievements:
    7 years registered

    Join Date
    Dec 2009
    Location
    indore
    Posts
    59
    Helped
    0 / 0
    Points
    2,007
    Level
    10

    Interfacing w25m512 with micrcontroller

    Hello,

    I am interfacing W25M512 IC with the microcontroller and devise ID(By sending 0X90 i get 0XEF) is read successfully. But when I try to write and read data on rom then I always get 0XFF.

    I am using a soft SPI lib for communication and I think it is working fine. My code is given below. So please help me to solve the problem.


    Code:
    int main(void)
    {
      unsigned char data =0XAA,rom_data;
      unsigned char address=100;
    
      FlashWriteData(address,&data,1);
    
      while(1)
      {
        FlashReadData(address,&rom_data,1);
        ms(25);
      }
    }
    
    
    /***************************Read data form memory*****************************/
    
    unsigned char tyt;                       //Global variable to monitor data on debugger  
     unsigned char FlashReadData (unsigned long address, unsigned char *data,unsigned long size)
     {
      unsigned char command[5];
    
      while(1){                                     //Check the status of the rom 
        	 status= read_status();
        	 if(status!=1)break;
         }
    
      WriteEnable();	  us(550);
    
      CS_L;
      us(550);
    
      	   /*Initialize peripheral for SPI */
       	   /*This section reads back data.  The first character is the read command */
    
              command[0] = 0x03;
              command[1] = ((unsigned char)(address >> 16) & 0XFF);
       	  command[2] = ((unsigned char)(address >>  8) & 0XFF);
      	  command[3] = ((unsigned char)(address >>  0) & 0XFF);
    
      	 soft_spi_data_write(command,1);
      	 soft_spi_data_write(command+1,1);
      	 soft_spi_data_write(command+2,1);
      	 soft_spi_data_write(command+3,1);
    
       tyt=soft_spi_data_read();
       *data=tyt;
       us(550);
    
       CS_H;
      return(0);
     }
    
    /***************************Write data on memory*******************************/
    /*
    Input--Address, *data, size--no of byte to write 
    */
    void FlashWriteData (unsigned long address, unsigned char const *data,int size)
     {
      unsigned char command[9];
      unsigned char data_var;
      data_var=*data;
                                                           
      WriteEnable();	  us(550);                                          //Send 0X06 command for write enable 
      WriteUnprotect();	  us(550);  				//Allow memory to be modified
    
    
            while(1){                                       //read status register 1 and wait till any write operation is finished
            	 status= read_status();
            	 if(status!=1)break;
             }
    
         CS_L;
         us(550);
    
      	 command[0] = 0x02;                                                             //Write command
      	 command[1] = ((unsigned char)(address >> 16) & 0XFF);     //Send address in three byte address mode
      	 command[2] = ((unsigned char)(address >>  8) & 0XFF);
      	 command[3] = ((unsigned char)(address >>  0) & 0XFF);
                                                              
         soft_spi_data_write(command,4);                                              //Send command 0X02 and then address 
         soft_spi_data_write(&data_var,1);                                              //Send data on eeprom
        
         us(550);
         CS_H;
    
     //  WriteProtect();
    }
    
    /*****************************************************************************************/
     void WriteProtect (void)
     {
       unsigned char command[2];
    
        CS_L;
    
       /*  This section writes data.  The first character is the write command */
       command[0] = 0x01;                             //Write on status regidter
       command[1] = 0x80;
    
       soft_spi_data_write(command, 2);
       //commented
    
       /* close peripheral for SPI */
       CS_H;
     }
    
    /***********************Disable globel protect*************************************************/
    void WriteUnprotect (void)
     {
       unsigned char command[2];
    
     /* send write enable command */
      	
       GlobalBlockProtectionUnlock();
    
       ms(1);
    
       CS_L;
       us(500);
    
     	  command[0] = 0x01;
      	  command[1] = 0x00;
    
       soft_spi_data_write(command,2);
     //  GlobalBlockProtectionUnlock();
    
       us(500);
       CS_H;
     }
    
    /*******************************************************************/
     void GlobalBlockProtectionUnlock (void)
     {
      /* send write enable command */
       unsigned char command = 0x98;
    
       /* Initialize peripheral for SPI */
        CS_L;
        us(500);
      	   soft_spi_data_write(&command,1);
        us(500);
        CS_H;
    
        return;
     }
    
    /***********Read the status register 1 of memory**************************/
    
    unsigned char FlashReadStatus (void)
     {
       unsigned char command,response;
    
      /* Initialize peripheral for SPI */
    
       CS_L;
       ms(1);
    
       command = 0x05;
    
      	  soft_spi_data_write(&command, 1);
      	  us(10);
      	  response=soft_spi_data_read();
    
       ms(1);
       CS_H;
    
       return(response);
      }
    
    
    
    
    
    /****************Reading data form SPI************************/
    
    unsigned char soft_spi_data_read(void)
    {
    	 unsigned char i=0;
    	  _Bool pin;
    	  data_sp=0;
    
    	  for(i=0; i<8; i++)
    	    {
    
    	 	 clk_h;
    	 	 us(100);
    
    	 	 pin=read_spi;
    
     	  	 if(pin==true)
          	 {
     	  		 data_sp=(data_sp | (0x80>>i));
          	 }
    
     	  	 clk_l;
      	   	 us(100);
        }
    
       return(data_sp);
    }
    
    
    /**************************For writing data or command on SPI**********************************/
    void soft_spi_data_write(unsigned char *data,int no){
       unsigned char i, maskdata = 0X80;  //0x80;
       int loop;
       unsigned char d;
    
       d=*data;
    
          for(loop=0;loop<no;loop++)
          {
    
        	  for(i= 0;i<8;i++)
        	  {
       		  	  if(*(data+loop) & maskdata)
       		  	  {
       		  		  d_out_h;
       		  	  }
       		  	  else
       		  	  {
       		  		  d_out_l;
       		  	  }
       		  	  maskdata >>= 1;
       		  	  clk_h;
       		  	  us(100)
       		  	  clk_l;
       		  	  us(100);
    
             }
          }
         d_out_l;
         clk_h;
         return;
    }
    Last edited by KlausST; 24th July 2018 at 08:37. Reason: added code tags

    •   AltAdvertisement

        
       

  2. #2
    Full Member level 1
    Points: 2,388, Level: 11
    Achievements:
    7 years registered

    Join Date
    Jan 2009
    Posts
    105
    Helped
    16 / 16
    Points
    2,388
    Level
    11

    Re: Interfacing w25m512 with micrcontroller

    Sth with your routines looks fishy.
    For example, where is the "WriteEnable()" function?
    If write was not enabled, it's normal that you will readback 0xFF, as written data is not flashed internally.
    Please recheck your code, and also the register adresses & commands against the device spec



--[[ ]]--