Beispiel #1
0
/************************************************************************
* Function: BYTE SST39WriteByte(DWORD address, BYTE data)
*                                                                       
* Overview: this function writes one byte
*                                                                       
* Input: address and byte to be written
*                                                                       
* Output: non-zero if it's successful
*
* Notes: none
*                                                                       
************************************************************************/
BYTE SST39WriteByte(DWORD address, BYTE data)
{
    SST39SetAddress(0x55555555);
    SST39_CS_LAT = 0;
    PMDIN1 = 0xaa;
    SST39PMPWaitBusy();
    SST39_CS_LAT = 1;

    SST39SetAddress(0xaaaaaaaa);
    SST39_CS_LAT = 0;
    PMDIN1 = 0x55;
    SST39PMPWaitBusy();
    SST39_CS_LAT = 1;

    SST39SetAddress(0x55555555);
    SST39_CS_LAT = 0;
    PMDIN1 = 0xa0;
    SST39PMPWaitBusy();
    SST39_CS_LAT = 1;

    SST39SetAddress(address);
    SST39_CS_LAT = 0;
    PMDIN1 = data;
    SST39PMPWaitBusy();
    SST39_CS_LAT = 1;

    SST39WaitProgram();

    if(SST39ReadByte(address) == data)
        if(SST39ReadByte(address) == data)
            return (1);

    return (0); // failed
}
/******************************************************************
 *  SST39WriteIncrementalArray(BYTE* vData, WORD wLen);
 * write an array erasing the sector if required
 *****************************************************************/
void SST39WriteIncrementalArray(BYTE* vData, WORD wLen)
{
	while (wLen > 0) {
		// wait for any existing transaction to finish
		SST39WaitProgram();
		
		// clear the sector if on a sector boundary
		if ((dwWriteAddr & SST39_FLASH_SECTOR_MASK) == 0) {
			SST39SectorErase(dwWriteAddr);
			SST39WaitProgram();
		}
			
		// write the byte of data
		SST39WriteByte(dwWriteAddr, *vData);
		// dwWriteAddr is incremented in WriteByte
		vData++;
		wLen--;
	}
}
/************************************************************************
* Function: void SST39SectorErase(DWORD address)                                 
*
* Overview: erase whole chip
*
* Input: none
*            
* Output: none
*
************************************************************************/
void SST39SectorErase(DWORD address)
{
    SST39SetAddress(0x00005555);
    SST39_CS_LAT = 0;
    PMDIN1=0xaa;
    SST39PMPWaitBusy();
    SST39_CS_LAT = 1;

    SST39SetAddress(0x00002AAA);
    SST39_CS_LAT = 0;
    PMDIN1=0x55;
    SST39PMPWaitBusy();
    SST39_CS_LAT = 1;
    
    SST39SetAddress(0x00005555);
    SST39_CS_LAT = 0;
    PMDIN1=0x80;
    SST39PMPWaitBusy();
    SST39_CS_LAT = 1;

    SST39SetAddress(0x00005555);
    SST39_CS_LAT = 0;
    PMDIN1=0xaa;
    SST39PMPWaitBusy();
    SST39_CS_LAT = 1;

    SST39SetAddress(0x00002AAA);
    SST39_CS_LAT = 0;
    PMDIN1=0x55;
    SST39PMPWaitBusy();
    SST39_CS_LAT = 1;
    
    // set the sector address
    SST39SetAddress(address);
    SST39_CS_LAT = 0;
    PMDIN1=0x30;
    SST39PMPWaitBusy();
    SST39_CS_LAT = 1;

    SST39WaitProgram();
}
/************************************************************************
* Function: void SST39ChipErase(void)                                 
*
* Overview: erase whole chip
*
* Input: none
*            
* Output: none
*
************************************************************************/
void SST39ChipErase(void)
{
    SST39SetAddress(0x00005555);
    SST39_CS_LAT = 0;
    PMDIN1=0xaa;
    SST39PMPWaitBusy();
    SST39_CS_LAT = 1;

    SST39SetAddress(0x00002AAA);
    SST39_CS_LAT = 0;
    PMDIN1=0x55;
    SST39PMPWaitBusy();
    SST39_CS_LAT = 1;
    
    SST39SetAddress(0x00005555);
    SST39_CS_LAT = 0;
    PMDIN1=0x80;
    SST39PMPWaitBusy();
    SST39_CS_LAT = 1;

    SST39SetAddress(0x00005555);
    SST39_CS_LAT = 0;
    PMDIN1=0xaa;
    SST39PMPWaitBusy();
    SST39_CS_LAT = 1;

    SST39SetAddress(0x00002AAA);
    SST39_CS_LAT = 0;
    PMDIN1=0x55;
    SST39PMPWaitBusy();
    SST39_CS_LAT = 1;
    
    SST39SetAddress(0x00005555);
    SST39_CS_LAT = 0;
    PMDIN1=0x10;
    SST39PMPWaitBusy();
    SST39_CS_LAT = 1;

    SST39WaitProgram();
}
/************************************************************************
* Function: BYTE SST39WriteByte(DWORD address, BYTE data)
*                                                                       
* Overview: this function writes one byte
*                                                                       
* Input: address and byte to be written
*                                                                       
* Output: non-zero if it's successful
*
* Notes: none
*                                                                       
************************************************************************/
BYTE SST39WriteByte(DWORD address, BYTE data)
{
	BYTE d1, d2;
	
    SST39SetAddress(0x00005555);
    SST39_CS_LAT = 0;
    PMDIN1 = 0xAA;
    SST39PMPWaitBusy();
    SST39_CS_LAT = 1;

    SST39SetAddress(0x00002AAA);
    SST39_CS_LAT = 0;
    PMDIN1 = 0x55;
    SST39PMPWaitBusy();
    SST39_CS_LAT = 1;
    
    SST39SetAddress(0x00005555);
    SST39_CS_LAT = 0;
    PMDIN1 = 0xA0;
    SST39PMPWaitBusy();
    SST39_CS_LAT = 1;
    
    SST39SetAddress(address);
    SST39_CS_LAT = 0;
    PMDIN1 = data;
    SST39PMPWaitBusy();
    SST39_CS_LAT = 1;
    
	dwWriteAddr++;
    SST39WaitProgram();

	d1 = SST39ReadByte(address);
	d2 = SST39ReadByte(address);
	if ((d1 == data) && (d2 == data))
		return 1;
 
    return 0; // failed
}