/******************************************************************************
  Function:
    void DRV_NVM_SST25VF064_WriteStatusRegister(uint8_t newStatus)

  Summary:
    Programs the status register.
    
  Description:
    This routine programs the status register of the flash device
    with the new value specified by newStatus. Only bits that are
    actuall writable will be modified.

  Parameters:
    newStatus - the new status that will be used.

  Returns:
    None
******************************************************************************/
void DRV_NVM_SST25VF064_WriteStatusRegister(uint8_t newStatus)
{

    while(!SPILOCK(spiInitData.channel));
    
    SPIINITIALIZE((DRV_SPI_INIT_DATA *)&spiInitData);

    // this is the sequence of the status register write
    // SST25_CMD_EWSR must be followed by SST25_CMD_WRSR
    SST25CSLow();
    
    // send write enable command
    PUTSPIBYTE(spiInitData.channel, SST25_CMD_EWSR);
	
    SST25CSHigh();

    SST25CSLow();

    // send write status register command
    PUTSPIBYTE(spiInitData.channel, SST25_CMD_WRSR);

    // program the new status bits
    PUTSPIBYTE(spiInitData.channel, newStatus);

    SST25CSHigh();

    // Wait for write end
    while(DRV_NVM_SST25VF064_IsWriteBusy());

    SPIUNLOCK(spiInitData.channel);
}
/******************************************************************************
  Function:
    void DRV_NVM_M25P80_WriteStatusRegister(uint8_t newStatus)

  Summary:
    Programs the status register.

  Description:
    This routine programs the status register of the flash device
    with the new value specified by newStatus. Only bits that are
    actuall writable will be modified.

  Parameters:
    newStatus - the new status that will be used.

  Returns:
    None
******************************************************************************/
void DRV_NVM_M25P80_WriteStatusRegister(uint8_t newStatus)
{

    while(!SPILOCK(spiInitData.channel));

    SPIINITIALIZE((DRV_SPI_INIT_DATA *)&spiInitData);

    // this is the sequence of the status register write
    // M25P80_CMD_WREN must be followed by SST25_CMD_WRSR
    DRV_NVM_M25P80_ChipSelectEnable();

    // send write enable command
    PUTSPIBYTE(spiInitData.channel, M25P80_CMD_WREN);

    DRV_NVM_M25P80_ChipSelectDisable();

    DRV_NVM_M25P80_ChipSelectEnable();

    // send write status register command
    PUTSPIBYTE(spiInitData.channel, M25P80_CMD_WRSR);

    // program the new status bits
    PUTSPIBYTE(spiInitData.channel, newStatus);

    DRV_NVM_M25P80_ChipSelectDisable();

    // Wait for write end
    while(DRV_NVM_M25P80_IsWriteBusy());

    SPIUNLOCK(spiInitData.channel);
}
/******************************************************************************
  Function:
    void DRV_NVM_SST25VF064_SectorErase( uint32_t address )

  Summary:
    Erase the sector specified by the given address.
    
  Description:
    This routine erases the sector of where the given address resides.
    
  Parameters:
    None

  Returns:
    None
******************************************************************************/
void DRV_NVM_SST25VF064_SectorErase(uint32_t address)
{
    // Note: This sector erase do not check for block protection (BP [3:0]).
    //       This function undo the block protection and erases
    //       the sector of the given address.
    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_SER);
    PUTSPIBYTE(spiInitData.channel, ((SST25_ADDRESS)address).uint8Address[2]);
    PUTSPIBYTE(spiInitData.channel, ((SST25_ADDRESS)address).uint8Address[1]);
    PUTSPIBYTE(spiInitData.channel, ((SST25_ADDRESS)address).uint8Address[0]);

    SST25CSHigh();
    
    // Wait for write end
    while(DRV_NVM_SST25VF064_IsWriteBusy());

    SPIUNLOCK(spiInitData.channel);

}
/******************************************************************************
  Function:
    void DRV_NVM_M25P80_SectorErase( uint32_t address )

  Summary:
    Erase the sector specified by the given address.

  Description:
    This routine erases the sector of where the given address resides.

  Parameters:
    None

  Returns:
    None
******************************************************************************/
void DRV_NVM_M25P80_SectorErase(uint32_t address)
{
    // Note: This sector erase do not check for block protection (BP [3:0]).
    //       This function undo the block protection and erases
    //       the sector of the given address.
    DRV_NVM_M25P80_WriteStatusRegister(0x00);

    while(!SPILOCK(spiInitData.channel));

    DRV_SPI_Initialize((DRV_SPI_INIT_DATA *)&spiInitData);

    DRV_NVM_M25P80_WriteEnable();

    DRV_NVM_M25P80_ChipSelectEnable();

    PUTSPIBYTE(spiInitData.channel, M25P80_CMD_SER);
    PUTSPIBYTE(spiInitData.channel, ((M25P80_ADDRESS) address).uint8Address[2]);
    PUTSPIBYTE(spiInitData.channel, ((M25P80_ADDRESS) address).uint8Address[1]);
    PUTSPIBYTE(spiInitData.channel, ((M25P80_ADDRESS) address).uint8Address[0]);

    DRV_NVM_M25P80_ChipSelectDisable();

    SPIUNLOCK(spiInitData.channel);

    // Wait for write end
    while(DRV_NVM_M25P80_IsWriteBusy());
}
/******************************************************************************
  Function:
    void DRV_NVM_M25P80_ChipErase( )

  Summary:
    Erase the flash device.

  Description:
    This routine erases the whole memory of the flash device.

  Parameters:
    None

  Returns:
    None
 ******************************************************************************/
void DRV_NVM_M25P80_ChipErase(void)
{
    // reset the BPL bits to be non-write protected, in case they are
    // write protected
    DRV_NVM_M25P80_WriteStatusRegister(0x00);

    while(!SPILOCK(spiInitData.channel));

    SPIINITIALIZE((DRV_SPI_INIT_DATA *)&spiInitData);

    // send write enable command
    DRV_NVM_M25P80_WriteEnable();

    DRV_NVM_M25P80_ChipSelectEnable();
    PUTSPIBYTE(spiInitData.channel, M25P80_CMD_ERASE);
    DRV_NVM_M25P80_ChipSelectDisable();

    // wait for BULK ERASE to be done
    while(DRV_NVM_M25P80_CheckWriteInProgress() == 1);

    SPIUNLOCK(spiInitData.channel);

    // Wait for write end
    while(DRV_NVM_M25P80_IsWriteBusy());

}
/******************************************************************************
  Function:
    void DRV_NVM_SST25VF064_ReadStatusRegister( void )
    
  Summary:
    Reads the status register of the device.
    
  Description:
    This function reads the status register of the device.


  Parameters:
    None

  Returns:
    The current value of the status register.
******************************************************************************/
uint8_t DRV_NVM_SST25VF064_ReadStatusRegister(void)
{
    uint8_t    temp;

    while(!SPILOCK(spiInitData.channel));

    SPIINITIALIZE((DRV_SPI_INIT_DATA *)&spiInitData);
    temp =  ReadStatusRegister();

    SPIUNLOCK(spiInitData.channel);
    return temp;

}
/******************************************************************************
  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:
    void DRV_NVM_M25P80_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_M25P80_Read(uint32_t address, uint8_t *pData, uint16_t nCount)
{
    while(!SPILOCK(spiInitData.channel));

    DRV_SPI_Initialize((DRV_SPI_INIT_DATA *)&spiInitData);

    DRV_NVM_M25P80_ChipSelectEnable();

    PUTSPIBYTE(spiInitData.channel, M25P80_CMD_READ);
    PUTSPIBYTE(spiInitData.channel, ((M25P80_ADDRESS) address).uint8Address[2]);
    PUTSPIBYTE(spiInitData.channel, ((M25P80_ADDRESS) address).uint8Address[1]);
    PUTSPIBYTE(spiInitData.channel, ((M25P80_ADDRESS) address).uint8Address[0]);

    DRV_SPI_GetBuffer(spiInitData.channel, pData, nCount);

    DRV_NVM_M25P80_ChipSelectDisable();
    SPIUNLOCK(spiInitData.channel);
}
/******************************************************************************
  Function:
    uint8_t DRV_NVM_M25P80_Write(   uint32_t address,
                                    uint8_t *pData,
                                    uint16_t nCount )

  Summary:
    Writes an array of bytes to a specified address.

  Description:
    This routine writes the array of bytes from the location pointed to by
    pData to the given address. The number of bytes to be written is specified
    by nCount.

  Parameters:
    address - starting address of the array to be written
    pData   - pointer to the source of the array
    nCount  - specifies the number of bytes to be written

  Returns:
    1 - if the write is successful
    0 - if the write was not successful
******************************************************************************/
uint8_t DRV_NVM_M25P80_Write(uint32_t address, uint8_t *pData, uint16_t nCount)
{

    uint32_t    addr;
    uint8_t     *pD;
    uint16_t    counter, sendCount, ret = 1;

    while(!SPILOCK(spiInitData.channel));

    DRV_SPI_Initialize((DRV_SPI_INIT_DATA *)&spiInitData);

    addr = address;
    pD = pData;

    // check in case previous erase or write is still ongoing
    while(DRV_NVM_M25P80_CheckWriteInProgress() == 1);
    // send write enable command
    DRV_NVM_M25P80_WriteEnable();

    for (counter = 0; counter < nCount; )
    {
        sendCount = 256 - (addr & 0xFF);
        if (sendCount > (nCount - counter))
            sendCount = (nCount - counter);

        ret = DRV_NVM_M25P80_WriteSector(addr, pD, sendCount);
        if (ret == 0)
            break;
        else
        {
            addr    += sendCount;
            pD      += sendCount;
            counter += sendCount;
        }

    }

    SPIUNLOCK(spiInitData.channel);

    return (ret);

}
/******************************************************************************
  Function:
    void DRV_NVM_M25P80_ReadIDRegister( void )

  Summary:
    Reads the ID register of the device.

  Description:
    This function reads the ID of the device.

  Parameters:
    Pointer to the buffer for the device ID information.

  Returns:
    None
******************************************************************************/
void DRV_NVM_M25P80_ReadIDRegister(uint8_t* pBuffer)
{
    uint8_t     deviceUniqueIDLen = 21;

    while(!SPILOCK(spiInitData.channel));

    SPIINITIALIZE((DRV_SPI_INIT_DATA *)&spiInitData);

    DRV_NVM_M25P80_ChipSelectEnable();
    PUTSPIBYTE(spiInitData.channel, M25P80_CMD_RDID);

    while(deviceUniqueIDLen)
    {
        deviceUniqueIDLen--;
        GETSPIBYTE(spiInitData.channel, *pBuffer++);
    }

    DRV_NVM_M25P80_ChipSelectDisable();

    SPIUNLOCK(spiInitData.channel);

}
/******************************************************************************
  Function:
    uint8_t DRV_NVM_SST25VF064_Write(   uint32_t address,
                                        uint8_t *pData, 
                                        uint16_t nCount )

  Summary:
    Writes an array of bytes to a specified address.
    
  Description:
    This routine writes the array of bytes from the location pointed to by 
    pData to the given address. The number of bytes to be written is specified
    by nCount.

  Parameters:
    address - starting address of the array to be written
    pData   - pointer to the source of the array
    nCount  - specifies the number of bytes to be written

  Returns:
    1 - if the write is successful 
    0 - if the write was not successful
******************************************************************************/
uint8_t DRV_NVM_SST25VF064_Write(uint32_t address, uint8_t *pData, uint16_t nCount)
{

    uint32_t    addr;
    uint8_t     *pD;
    uint16_t    counter, sendCount, ret;

    while(!SPILOCK(spiInitData.channel));

    DRV_SPI_Initialize((DRV_SPI_INIT_DATA *)&spiInitData);

    addr = address;
    pD = pData;

    for (counter = 0; counter < nCount; )
    {
        sendCount = 256 - (addr & 0xFF);
        if (sendCount > (nCount - counter))
            sendCount = (nCount - counter);

        ret = DRV_NVM_SST25VF064_WriteSector(addr, pD, sendCount);
        if (ret == 0)
            break;
        else
        {
            addr    += sendCount;
            pD      += sendCount;
            counter += sendCount;
        }

    }

    SPIUNLOCK(spiInitData.channel);

    return (ret);

}
/******************************************************************************
  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;
}