I want to interface W25M512JV (16 PIN Package)with ATMEGA 32 (operating at 3.3V,8Mhz, compiler winavr, software based SPI). This is my code.
I want to write data on 200 location of rom. and reading same location and coping data on variable "rom_data".But I always get high on MISO of rom. (Reset pin is connected with VCC and I using PIN 8 FOR MISO, PIN 15 FOR MOSI, PIN 16 FOR CLK).
unsigned char rom_data;
int main(void)
{
unsigned char spi_counter=0XAA;
unsigned char rom_address=200;
while(1)
{
ms(250);
FlashWriteData(rom_address,&spi_counter,1); //for writing data on 200 location
ms(250);
FlashReadData(rom_address,&rom_data,1); //For reading data
}
// Function for writing data on flash
//Input address, data, no of byte to write
void FlashWriteData (unsigned long address, unsigned char const *data,int size)
{
unsigned char command[4];
unsigned char data_var;
data_var=*data;
WriteEnable(); us(550); //Send command to disable write protection send command 0X06
WriteUnprotect(); us(550); //Global block unclock --0X98 command
/* This section writes data. The first character is the write command */
CS_L;
us(550);
command[0] = 0x02; //write command
command[1] = (unsigned char)(address >> 16);
command[2] = (unsigned char)(address >> 8);
command[3] = (unsigned char)(address >> 0);
soft_spi_data_write(command,4); //send write command
soft_spi_data_write(&data_var,1); //read data form rom
us(550);
CS_H;
while(FlashReadStatus() == 0x03); //this ensure read operation are completed
us(200);
WriteProtect(); //after writing data set lock flag
}
//Function for read data form memory
//Input- address, size(not used yet), unsigned char pointer for returning data.
unsigned char FlashReadData (unsigned long address, unsigned char *data,unsigned long size)
{
unsigned char command[4];
CS_L;
us(550);
/*Initialize peripheral for SPI */
/*This section reads back data. The first character is the read command */
command[0] = 0x03; //send read command
command[1] = (unsigned char)(address >> 16);
command[2] = (unsigned char)(address >> 8);
command[3] = (unsigned char)(address >> 0);
soft_spi_data_write(command,4); //send read command
us(100);
*data=soft_spi_data_read();
us(550);
CS_H;
return(0);
}
void WriteUnprotect (void)
{
unsigned char command[2];
/* send write enable command */
//Erase_Flash_Chip();
GlobalBlockProtectionUnlock();
ms(10);
CS_L;
ms(5);
command[0] = 0x01;
command[1] = 0x00;
soft_spi_data_write(command,2);
ms(5);
CS_H;
}
void WriteEnable (void)
{
/* send write enable command */
unsigned char command = 0x06;
CS_L;
us(500);
//do{
soft_spi_data_write(&command, 1);
//loop++;
//}while ((FlashReadStatus() & FLASH_WEL) != FLASH_WEL);
us(500);
CS_H;
}
void GlobalBlockProtectionUnlock (void)
{
/* send gloal block unlock command */
unsigned char command = 0x98;
/* Initialize peripheral for SPI */
CS_L;
us(500);
soft_spi_data_write(&command,1);
us(500);
CS_H;
return;
}
/*write data in 00 mode*/
void soft_spi_data_write(unsigned char *data,int no){
unsigned char i, maskdata = 0X80;
int loop;
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;
ms(1)
clk_l;
ms(1);
}
}
d_out_l;
return;
}
/*read data in 00 mode*/
unsigned char soft_spi_data_read(void)
{
unsigned char i=0;
_Bool pin;
data_sp=0;
while(i<8)
{
d_out_h;
clk_h;
ms(1);
clk_l;
ms(1);
pin=read_spi;
if(pin==true)
{
data_ty=0X01;
}
else
{
data_ty=0X00;
}
data_sp=data_sp<<1;
data_sp|=data_ty;
i++;
}
return(data_sp);
}