/************************************************************************ * Function: BYTE SST25ReadByte(DWORD address) * * Overview: this function reads a byte from the address specified * * Input: address * * Output: data read * ************************************************************************/ BYTE SST25ReadByte(DWORD address) { BYTE temp; while(!SPILock(spiInitData.channel)) ; DRV_SPI_Initialize(spiInitData.channel, (DRV_SPI_INIT_DATA *)&spiInitData); SST25CSLow(); SPIPut(spiInitData.channel, SST25_CMD_READ); SPIGet(spiInitData.channel); SPIPut(spiInitData.channel, ((DWORD_VAL) address).v[2]); SPIGet(spiInitData.channel); SPIPut(spiInitData.channel, ((DWORD_VAL) address).v[1]); SPIGet(spiInitData.channel); SPIPut(spiInitData.channel, ((DWORD_VAL) address).v[0]); SPIGet(spiInitData.channel); SPIPut(spiInitData.channel, 0); temp = SPIGet(spiInitData.channel); SST25CSHigh(); SPIUnLock(spiInitData.channel); return (temp); }
/************************************************************************ * Function: void SST25WriteByte(BYTE data, DWORD address) * * Overview: this function writes a byte to the address specified * * Input: data to be written and address * * Output: none * ************************************************************************/ void SST25WriteByte(BYTE data, DWORD address) { SST25WriteEnable(); while(!SPILock(spiInitData.channel)) ; DRV_SPI_Initialize(spiInitData.channel, (DRV_SPI_INIT_DATA *)&spiInitData); SST25CSLow(); SPIPut(spiInitData.channel, SST25_CMD_WRITE); SPIGet(spiInitData.channel); SPIPut(spiInitData.channel, ((DWORD_VAL) address).v[2]); SPIGet(spiInitData.channel); SPIPut(spiInitData.channel, ((DWORD_VAL) address).v[1]); SPIGet(spiInitData.channel); SPIPut(spiInitData.channel, ((DWORD_VAL) address).v[0]); SPIGet(spiInitData.channel); SPIPut(spiInitData.channel, data); SPIGet(spiInitData.channel); SST25CSHigh(); SPIUnLock(spiInitData.channel); // Wait for write end while(SST25IsWriteBusy()); }
/************************************************************************ * Function: SST25WriteEnable() * * Overview: this function allows write/erase SST25. Must be called * before every write/erase command. * * Input: none * * Output: none * ************************************************************************/ void SST25WriteEnable(void) { SST25CSLow(); SPIPut(spiInitData.spiId, SST25_CMD_WREN); PLIB_SPI_BufferRead(spiInitData.spiId); SST25CSHigh(); }
/************************************************************************ * Function: SST25WriteEnable() * * Overview: this function allows write/erase SST25. Must be called * before every write/erase command. * * Input: none * * Output: none * ************************************************************************/ void SST25WriteEnable(void) { while(!SPILock(spiInitData.channel)) ; DRV_SPI_Initialize(spiInitData.channel, (DRV_SPI_INIT_DATA *)&spiInitData); SST25CSLow(); SPIPut(spiInitData.channel, SST25_CMD_WREN); SPIGet(spiInitData.channel); SST25CSHigh(); SPIUnLock(spiInitData.channel); }
/****************************************************************************** Function: static inline __attribute__((__always_inline__)) uint8_t ReadStatusRegister(void) Summary: Reads the status register of the device. Description: This is an internal inline function called within the module to read the status register of the device. Parameters: None Returns: Returns a byte indicating the status of the device. ******************************************************************************/ static inline __attribute__((__always_inline__)) uint8_t ReadStatusRegister(void) { uint8_t temp; SST25CSLow(); PUTSPIBYTE(spiInitData.channel, SST25_CMD_RDSR); GETSPIBYTE(spiInitData.channel, temp); SST25CSHigh(); return temp; }
/************************************************************************ * Function: BYTE SST25IsWriteBusy(void) * * Overview: this function reads status register and chek BUSY bit for write operation * * Input: none * * Output: non zero if busy * ************************************************************************/ uint8_t SST25IsWriteBusy(void) { uint8_t temp; SST25CSLow(); SPIPut(spiInitData.spiId, SST25_CMD_RDSR); PLIB_SPI_BufferRead(spiInitData.spiId); SPIPut(spiInitData.spiId, 0); temp = PLIB_SPI_BufferRead(spiInitData.spiId); SST25CSHigh(); return (temp & 0x01); }
/************************************************************************ * Function: BYTE SST25WriteArray(DWORD address, BYTE* pData, nCount) * * Overview: this function writes a data array at the address specified * * Input: flash memory address, pointer to the data array, data number * * Output: return 1 if the operation was successfull * ************************************************************************/ BYTE SST25WriteArray(DWORD address, BYTE *pData, WORD nCount) { DWORD addr; BYTE *pD; WORD counter; #if defined(USE_M25P80) BYTE status; #endif addr = address; pD = pData; // WRITE for(counter = 0; counter < nCount; counter++) { SST25WriteByte(*pD++, addr++); } #if defined(USE_M25P80) // check status of Write in Progress // wait for BULK ERASE to be done SST25CSLow(); SPIPut(spiInitData.channel, SST25_CMD_RDSR); SPIGet(spiInitData.channel); while(1) { SPIPut(spiInitData.channel, 0); status = (SPIGet(spiInitData.channel)& SST25_WIP_STATUS); if ((status & SST25_WIP_STATUS) == 0) break; } SST25CSHigh(); #endif // VERIFY for(counter = 0; counter < nCount; counter++) { if(*pData != SST25ReadByte(address)) return (0); pData++; address++; } return (1); }
/************************************************************************ * Function: BYTE SST25WriteArray(DWORD address, BYTE* pData, nCount) * * Overview: this function writes a data array at the address specified * * Input: flash memory address, pointer to the data array, data number * * Output: return 1 if the operation was successfull * ************************************************************************/ uint8_t SST25WriteArray(uint32_t address, uint8_t *pData, uint16_t nCount) { uint32_t addr; uint8_t *pD; uint16_t counter; #if defined(USE_M25P80) uint8_t status; #endif addr = address; pD = pData; // WRITE for(counter = 0; counter < nCount; counter++) { SST25WriteByte(*pD++, addr++); } #if defined(USE_M25P80) // check status of Write in Progress // wait for BULK ERASE to be done SST25CSLow(); SPIPut(spiInitData.spiId, SST25_CMD_RDSR); SPIGet(spiInitData.spiId); while(1) { SPIPut(spiInitData.spiId, 0); status = (SPIGet(spiInitData.spiId)& SST25_WIP_STATUS); if ((status & SST25_WIP_STATUS) == 0) break; } SST25CSHigh(); #endif // VERIFY for(counter = 0; counter < nCount; counter++) { if(*pData != SST25ReadByte(address)) return (0); pData++; address++; } return (1); }
/****************************************************************************** Function: uint8_t DRV_NVM_SST25VF064_ReadByte( uint32_t address ) Summary: Reads a byte from the specified address. Description: This routine is internal to this module. This reads a a byte from the given address. Parameters: address - location byte to be read Returns: read byte ******************************************************************************/ uint8_t DRV_NVM_SST25VF064_ReadByte(uint32_t address) { uint8_t temp; SST25CSLow(); PUTSPIBYTE(spiInitData.channel, SST25_CMD_READ); PUTSPIBYTE(spiInitData.channel, ((SST25_ADDRESS)address).uint8Address[2]); PUTSPIBYTE(spiInitData.channel, ((SST25_ADDRESS)address).uint8Address[1]); PUTSPIBYTE(spiInitData.channel, ((SST25_ADDRESS)address).uint8Address[0]); GETSPIBYTE(spiInitData.channel, temp); SST25CSHigh(); return (temp); }
/****************************************************************************** Function: void DRV_NVM_SST25VF064_WriteByte( uint8_t data, uint32_t address ) Summary: Writes a byte to the specified address. Description: This routine is internal to this module. This writes a byte to the given address. Parameters: data - byte to be written address - location of the programmed byte Returns: None. ******************************************************************************/ void DRV_NVM_SST25VF064_WriteByte(uint8_t data, uint32_t address) { DRV_NVM_SST25VF064_WriteEnable(); SST25CSLow(); PUTSPIBYTE(spiInitData.channel, SST25_CMD_WRITE); PUTSPIBYTE(spiInitData.channel, ((SST25_ADDRESS)address).uint8Address[2]); PUTSPIBYTE(spiInitData.channel, ((SST25_ADDRESS)address).uint8Address[1]); PUTSPIBYTE(spiInitData.channel, ((SST25_ADDRESS)address).uint8Address[0]); PUTSPIBYTE(spiInitData.channel, data); SST25CSHigh(); // Wait for write end while(DRV_NVM_SST25VF064_IsWriteBusy()); }
/****************************************************************************** Function: void DRV_NVM_SST25VF064_Read( uint32_t address, uint8_t *pData, uint16_t nCount ) Summary: Reads an array of bytes from the specified address. Description: This routine reads an array of bytes from the specified address location. The read array is saved to the location pointed to by pData. The number of bytes to be read is specified by nCount. Parameters: address - starting address of the array to be read pData - pointer to the destination of the read array nCount - specifies the number of bytes to be read Returns: None ******************************************************************************/ void DRV_NVM_SST25VF064_Read(uint32_t address, uint8_t *pData, uint16_t nCount) { while(!SPILOCK(spiInitData.channel)); SPIINITIALIZE((DRV_SPI_INIT_DATA *)&spiInitData); SST25CSLow(); PUTSPIBYTE(spiInitData.channel, SST25_CMD_READ); PUTSPIBYTE(spiInitData.channel, ((SST25_ADDRESS)address).uint8Address[2]); PUTSPIBYTE(spiInitData.channel, ((SST25_ADDRESS)address).uint8Address[1]); PUTSPIBYTE(spiInitData.channel, ((SST25_ADDRESS)address).uint8Address[0]); DRV_SPI_GetBuffer(spiInitData.channel, pData, nCount); SST25CSHigh(); SPIUNLOCK(spiInitData.channel); }
/************************************************************************ * Function: BYTE SST25IsWriteBusy(void) * * Overview: this function reads status register and chek BUSY bit for write operation * * Input: none * * Output: non zero if busy * ************************************************************************/ BYTE SST25IsWriteBusy(void) { BYTE temp; while(!SPILock(spiInitData.channel)) ; DRV_SPI_Initialize(spiInitData.channel, (DRV_SPI_INIT_DATA *)&spiInitData); SST25CSLow(); SPIPut(spiInitData.channel, SST25_CMD_RDSR); SPIGet(spiInitData.channel); SPIPut(spiInitData.channel, 0); temp = SPIGet(spiInitData.channel); SST25CSHigh(); SPIUnLock(spiInitData.channel); return (temp & 0x01); }
/****************************************************************************** Function: void DRV_NVM_SST25VF064_ChipErase( ) Summary: Erase the flash device. Description: This routine erases the whole memory of the flash device. Parameters: None Returns: None ******************************************************************************/ void DRV_NVM_SST25VF064_ChipErase(void) { // reset the BPL bits to be non-write protected, in case they are // write protected DRV_NVM_SST25VF064_WriteStatusRegister(0x00); while(!SPILOCK(spiInitData.channel)); SPIINITIALIZE((DRV_SPI_INIT_DATA *)&spiInitData); DRV_NVM_SST25VF064_WriteEnable(); SST25CSLow(); PUTSPIBYTE(spiInitData.channel, SST25_CMD_ERASE); SST25CSHigh(); // Wait for write end while(DRV_NVM_SST25VF064_IsWriteBusy()); SPIUNLOCK(spiInitData.channel); return; }
/************************************************************************ * Function: void SST25ChipErase(void) * * Overview: chip erase * * Input: none * ************************************************************************/ void SST25ChipErase(void) { #if defined(USE_M25P80) BYTE status; #endif #if defined(USE_SST25VF016) SST25WriteEnable(); #endif while(!SPILock(spiInitData.channel)) ; DRV_SPI_Initialize(spiInitData.channel, (DRV_SPI_INIT_DATA *)&spiInitData); SST25CSLow(); #if defined(USE_M25P80) // send write enable command SPIPut(spiInitData.channel, SST25_CMD_EWSR); SPIGet(spiInitData.channel); SST25CSHigh(); Nop(); SST25CSLow(); // verify if the WEL bit is set high while(1) { SPIPut(spiInitData.channel, SST25_CMD_RDSR); SPIGet(spiInitData.channel); status = 0xFF; while((status & SST25_STATUS_MASK) > 0) { SPIPut(spiInitData.channel, 0); status = SPIGet(spiInitData.channel); } if ((status & SST25_WEL_STATUS) == SST25_WEL_STATUS) break; } SST25CSHigh(); #endif SST25CSLow(); SPIPut(spiInitData.channel, SST25_CMD_ERASE); SPIGet(spiInitData.channel); SST25CSHigh(); #if defined (USE_M25P80) // wait for BULK ERASE to be done SST25CSLow(); SPIPut(spiInitData.channel, SST25_CMD_RDSR); SPIGet(spiInitData.channel); while(1) { SPIPut(spiInitData.channel, 0); status = (SPIGet(spiInitData.channel)& SST25_WIP_STATUS); if ((status & SST25_WIP_STATUS) == 0) break; } SST25CSHigh(); #endif SPIUnLock(spiInitData.channel); // Wait for write end while(SST25IsWriteBusy()); }
/****************************************************************************** Function: void DRV_NVM_SST25VF064_WriteEnable( void ) Summary: Enables the device for writes. Description: This is an internal function called within the module to enable the device for writes. Parameters: None Returns: None ******************************************************************************/ void DRV_NVM_SST25VF064_WriteEnable(void) { SST25CSLow(); PUTSPIBYTE(spiInitData.channel, SST25_CMD_WREN); SST25CSHigh(); }
/************************************************************************ * Function: void SST25ChipErase(void) * * Overview: chip erase * * Input: none * ************************************************************************/ void SST25ChipErase(void) { #if defined(USE_M25P80) uint8_t status; #endif #if defined(USE_SST25VF016) SST25WriteEnable(); #endif // while(!SPILock(spiInitData.spiId)); SST25CSLow(); #if defined(USE_M25P80) // send write enable command SPIPut(spiInitData.spiId, SST25_CMD_EWSR); SPIGet(spiInitData.spiId); SST25CSHigh(); Nop(); SST25CSLow(); // verify if the WEL bit is set high while(1) { SPIPut(spiInitData.spiId, SST25_CMD_RDSR); SPIGet(spiInitData.spiId); status = 0xFF; while((status & SST25_STATUS_MASK) > 0) { SPIPut(spiInitData.spiId, 0); status = SPIGet(spiInitData.spiId); } if ((status & SST25_WEL_STATUS) == SST25_WEL_STATUS) break; } SST25CSHigh(); #endif SST25CSLow(); SPIPut(spiInitData.spiId, SST25_CMD_ERASE); PLIB_SPI_BufferRead(spiInitData.spiId); SST25CSHigh(); #if defined (USE_M25P80) // wait for BULK ERASE to be done SST25CSLow(); SPIPut(spiInitData.spiId, SST25_CMD_RDSR); SPIGet(spiInitData.spiId); while(1) { SPIPut(spiInitData.spiId, 0); status = (SPIGet(spiInitData.spiId)& SST25_WIP_STATUS); if ((status & SST25_WIP_STATUS) == 0) break; } SST25CSHigh(); #endif // SPIUnLock(spiInitData.spiId); // Wait for write end while(SST25IsWriteBusy()); }