Estoy intentando guardar algunas cadenas en Winbond Spi Flash con el microcontrolador atmega 128. Hasta ahora soy capaz de obtener ID de manufactura. y guardar la cadena hasta 256 bytes, pero cuando intento leerlo, se pone en blanco. Estoy submitiendo el código para leer y escribir.
void RD_JEDEC_ID(void)
{
uint8_t manuf;
uint16_t id;//total sector linearly from 0 to 4194303.
CSF_EN;
spi_transfer(R_JEDEC_ID);
manuf=spi_transfer(0x00);
id = spi_transfer(0x00) << 8;
id |= spi_transfer(0x00);
CSF_DS;
if (manuf==0xEF){ENB_PC();U0TX_SRAM("FLASH : WINBOND ");}
else if (manuf==0x37){ENB_PC();U0TX_SRAM("FLASH : AMIC ");}
else{ENB_PC();U0TX_SRAM("FLASH : UNIDENTIFIED\r\n");}
char LNTH[7];itoa(id,LNTH,16);
ENB_PC();U0TX_SRAM("ID_CAP = ");U0TX_SRAM(&LNTH[0]);U0TX_SRAM("\r\n");
}
uint8_t GT_STSTREG1()
{
CSF_EN;
spi_transfer(R_SR1);
uint8_t sr1 = spi_transfer(0x00);
CSF_DS;wdt_reset();
//char LTH[5];itoa(sr1,LTH,16);
//ENB_PC();U0TX_SRAM("STATUS REG VALUE = ");U0TX_SRAM(<H[0]);U0TX_SRAM("\r\n");
return sr1;
//BUSSY=sr1 & 0x01;=1
//WEL= sr1 & 0x02;=2
}
void CHK_BUSSY()
{
//ENB_PC();U0TX_SRAM("CHECKING BUSSY\r\n");
uint8_t stat;
mm : stat= GT_STSTREG1();
if ((stat & 0x01)!=0)
{
goto mm;
}
}
void Write_EN()
{
//ENB_PC();U0TX_SRAM("ENABLING WRITE COMMAND\r\n");
CSF_EN;
spi_transfer(W_EN);
CSF_DS;
uint8_t wel;
pp : wel= GT_STSTREG1();
if ((wel & 0x02)!= 2)
{
goto pp;
}
}
void write_DS()
{
//ENB_PC();U0TX_SRAM("DISANABLING WRITE COMMAND\r\n");
CSF_EN;
spi_transfer(W_DE);
CSF_DS;
uint8_t wez;
vv : wez= GT_STSTREG1();
if ((wez & 0x01)!= 0)
{
goto vv;
}
}
char RD_FLASH_CHAR(uint32_t addr)
{
CHK_BUSSY();
CSF_EN;
spi_transfer(READ);
spi_transfer(addr >> 16);
spi_transfer(addr >> 8);
spi_transfer(addr);
unsigned char result = spi_transfer(0x00);
CSF_DS;
return result;
}
void WR_FLASH_CHAR(uint32_t addr, char byt)
{
CHK_BUSSY();Write_EN();
CSF_EN;
spi_transfer(PAGE_PGM);
spi_transfer(addr >> 16);
spi_transfer(addr >> 8);
spi_transfer(addr);
spi_transfer(byt);
CSF_DS;
CHK_BUSSY();write_DS();
}
void FLASH_ERASE()
{
CHK_BUSSY();Write_EN();
CSF_EN;
spi_transfer(CHIP_ERASE);
CSF_DS;
CHK_BUSSY();write_DS();
ENB_PC();U0TX_SRAM("EXTERNAL FLASH IS ERASED\r\n");
}
void SECTOR_ERASE(uint32_t addrs)
{
CHK_BUSSY();Write_EN();
CSF_EN;
spi_transfer(SECTOR_E);
spi_transfer(addrs >> 16);
spi_transfer(addrs >> 8);
spi_transfer(addrs);
CSF_DS;
CHK_BUSSY();write_DS();
ENB_PC();U0TX_SRAM("4KB SECTOR ERASED\r\n");
}
int WR_FLASH_DATA(uint32_t addres,unsigned char *buff)
{
//ENB_PC();U0TX_SRAM("WRITING FLASH DATA\r\n");
int adcnt=0;
char k=*buff; k=*buff++;
while (k!='void RD_JEDEC_ID(void)
{
uint8_t manuf;
uint16_t id;//total sector linearly from 0 to 4194303.
CSF_EN;
spi_transfer(R_JEDEC_ID);
manuf=spi_transfer(0x00);
id = spi_transfer(0x00) << 8;
id |= spi_transfer(0x00);
CSF_DS;
if (manuf==0xEF){ENB_PC();U0TX_SRAM("FLASH : WINBOND ");}
else if (manuf==0x37){ENB_PC();U0TX_SRAM("FLASH : AMIC ");}
else{ENB_PC();U0TX_SRAM("FLASH : UNIDENTIFIED\r\n");}
char LNTH[7];itoa(id,LNTH,16);
ENB_PC();U0TX_SRAM("ID_CAP = ");U0TX_SRAM(&LNTH[0]);U0TX_SRAM("\r\n");
}
uint8_t GT_STSTREG1()
{
CSF_EN;
spi_transfer(R_SR1);
uint8_t sr1 = spi_transfer(0x00);
CSF_DS;wdt_reset();
//char LTH[5];itoa(sr1,LTH,16);
//ENB_PC();U0TX_SRAM("STATUS REG VALUE = ");U0TX_SRAM(<H[0]);U0TX_SRAM("\r\n");
return sr1;
//BUSSY=sr1 & 0x01;=1
//WEL= sr1 & 0x02;=2
}
void CHK_BUSSY()
{
//ENB_PC();U0TX_SRAM("CHECKING BUSSY\r\n");
uint8_t stat;
mm : stat= GT_STSTREG1();
if ((stat & 0x01)!=0)
{
goto mm;
}
}
void Write_EN()
{
//ENB_PC();U0TX_SRAM("ENABLING WRITE COMMAND\r\n");
CSF_EN;
spi_transfer(W_EN);
CSF_DS;
uint8_t wel;
pp : wel= GT_STSTREG1();
if ((wel & 0x02)!= 2)
{
goto pp;
}
}
void write_DS()
{
//ENB_PC();U0TX_SRAM("DISANABLING WRITE COMMAND\r\n");
CSF_EN;
spi_transfer(W_DE);
CSF_DS;
uint8_t wez;
vv : wez= GT_STSTREG1();
if ((wez & 0x01)!= 0)
{
goto vv;
}
}
char RD_FLASH_CHAR(uint32_t addr)
{
CHK_BUSSY();
CSF_EN;
spi_transfer(READ);
spi_transfer(addr >> 16);
spi_transfer(addr >> 8);
spi_transfer(addr);
unsigned char result = spi_transfer(0x00);
CSF_DS;
return result;
}
void WR_FLASH_CHAR(uint32_t addr, char byt)
{
CHK_BUSSY();Write_EN();
CSF_EN;
spi_transfer(PAGE_PGM);
spi_transfer(addr >> 16);
spi_transfer(addr >> 8);
spi_transfer(addr);
spi_transfer(byt);
CSF_DS;
CHK_BUSSY();write_DS();
}
void FLASH_ERASE()
{
CHK_BUSSY();Write_EN();
CSF_EN;
spi_transfer(CHIP_ERASE);
CSF_DS;
CHK_BUSSY();write_DS();
ENB_PC();U0TX_SRAM("EXTERNAL FLASH IS ERASED\r\n");
}
void SECTOR_ERASE(uint32_t addrs)
{
CHK_BUSSY();Write_EN();
CSF_EN;
spi_transfer(SECTOR_E);
spi_transfer(addrs >> 16);
spi_transfer(addrs >> 8);
spi_transfer(addrs);
CSF_DS;
CHK_BUSSY();write_DS();
ENB_PC();U0TX_SRAM("4KB SECTOR ERASED\r\n");
}
int WR_FLASH_DATA(uint32_t addres,unsigned char *buff)
{
//ENB_PC();U0TX_SRAM("WRITING FLASH DATA\r\n");
int adcnt=0;
char k=*buff; k=*buff++;
while (k!='%pre%')
{
WR_FLASH_CHAR(addres,k);k=*buff++;
addres++;adcnt++;
}
char dlen[3]="%pre%%pre%%pre%";
itoa(adcnt,dlen,10);
ENB_PC();U0TX_SRAM("FLASH WRITE COUNT : ");U0TX_SRAM(&dlen[0]);U0TX_SRAM("\r\n");
//ENB_PC();U0TX_SRAM("WRITING FLASH DATA COMPLETED\r\n");
return adcnt;
}
void RD_FLASH_DATA(uint32_t addr,int cnt)
{
ENB_PC();U0TX_SRAM("READING FLASH DATA\r\n");
unsigned char reslt;
for (int j=addr;j<=cnt;j++)
{
reslt = RD_FLASH_CHAR(addr++);FDATA[j]=reslt;
}
ENB_PC();U0TX_SRAM(&FDATA[0]);U0TX_SRAM("\r\n");
}
')
{
WR_FLASH_CHAR(addres,k);k=*buff++;
addres++;adcnt++;
}
char dlen[3]="%pre%%pre%%pre%";
itoa(adcnt,dlen,10);
ENB_PC();U0TX_SRAM("FLASH WRITE COUNT : ");U0TX_SRAM(&dlen[0]);U0TX_SRAM("\r\n");
//ENB_PC();U0TX_SRAM("WRITING FLASH DATA COMPLETED\r\n");
return adcnt;
}
void RD_FLASH_DATA(uint32_t addr,int cnt)
{
ENB_PC();U0TX_SRAM("READING FLASH DATA\r\n");
unsigned char reslt;
for (int j=addr;j<=cnt;j++)
{
reslt = RD_FLASH_CHAR(addr++);FDATA[j]=reslt;
}
ENB_PC();U0TX_SRAM(&FDATA[0]);U0TX_SRAM("\r\n");
}
¿Puede alguien decirme qué está mal?