📄 c2082.c
字号:
pIns_Addr[0] = SPI_FLASH_INS_SE;
#ifdef FLASH_SMALLER_SECTOR_SIZE
pIns_Addr[1] = uscSectorNr>>1;
pIns_Addr[2] = uscSectorNr<<7;
#else
pIns_Addr[1] = uscSectorNr;
pIns_Addr[2] = 0;
#endif
pIns_Addr[3] = 0;
// Step 5: Send the packet (Instruction & address) serially
Serialize(&char_stream_send,
ptrNull,
enumEnableTransOnly_SelectSlave,
enumDisableTansRecv_DeSelectSlave
);
// Step 6: Wait until the operation completes or a timeout occurs.
WAIT_TILL_Instruction_EXECUTION_COMPLETE(3)
return Flash_Success;
}
/*******************************************************************************
Function: ReturnType FlashBulkErase( void )
Arguments: none
Return Values:
Flash_OperationOngoing
Flash_OperationTimeOut
Flash_Success
Description: This function erases the whole Flash memory by sending an
SPI_FLASH_INS_BE Instruction.
Note:
This function does not check whether the target memory area (or part of it)
is in a Software Protection Mode(SPM) or Hardware Protection Mode(HPM),
in which case the PP Instruction will be ignored.
The function assumes that the target memory area has previously been unprotected at both
the hardware and software levels.
To unprotect the memory, please call FlashWriteStatusRegister(ST_uint8 ucStatusRegister),
and refer to the datasheet to set a proper ucStatusRegister value.
Pseudo Code:
Step 1: Check whether any previous Write, Program or Erase cycle is on going
Step 2: Disable the Write protection (the Flash memory will automatically enable it
again after the execution of the Instruction)
Step 3: Initialize the data (Instruction & address) packet to be sent serially
Step 4: Send the packet (Instruction & address) serially
Step 5: Wait until the operation completes or a timeout occurs.
*******************************************************************************/
ReturnType FlashBulkErase( void )
{
CharStream char_stream_send;
ST_uint8 cBE = SPI_FLASH_INS_BE;
// Step 1: Check whether any previous Write, Program or Erase cycle is on going
if(IsFlashBusy()) return Flash_OperationOngoing;
// Step 2: Disable Write protection
FlashWriteEnable();
// Step 3: Initialize the data(Instruction & address) packet to be sent serially
char_stream_send.length = 1;
char_stream_send.pChar = &cBE;
// Step 4: Send the packet(Instruction & address) serially
Serialize(&char_stream_send,
ptrNull,
enumEnableTransOnly_SelectSlave,
enumDisableTansRecv_DeSelectSlave
);
// Step 5: Wait until the operation completes or a timeout occurs.
WAIT_TILL_Instruction_EXECUTION_COMPLETE(BE_TIMEOUT)
return Flash_Success;
}
#ifndef NO_DEEP_POWER_DOWN_SUPPORT
/*******************************************************************************
Function: FlashDeepPowerDown( void )
Arguments: void
Return Value:
Flash_OperationOngoing
Flash_Success
Description: This function puts the device in the lowest consumption
mode (the Deep Power-down mode) by sending an SPI_FLASH_INS_DP.
After calling this routine, the Flash memory will not respond to any
subsequent Instruction except for the RDP Instruction.
Pseudo Code:
Step 1: Initialize the data (i.e. Instruction) packet to be sent serially
Step 2: Check whether any previous Write, Program or Erase cycle is on going
Step 3: Send the packet serially
*******************************************************************************/
ReturnType FlashDeepPowerDown( void )
{
CharStream char_stream_send;
ST_uint8 cDP = SPI_FLASH_INS_DP;
// Step 1: Initialize the data (i.e. Instruction) packet to be sent serially
char_stream_send.length = 1;
char_stream_send.pChar = &cDP;
// Step 2: Check whether any previous Write, Program or Erase cycle is on going
if(IsFlashBusy()) return Flash_OperationOngoing;
// Step 3: Send the packet serially
Serialize(&char_stream_send,
ptrNull,
enumEnableTransOnly_SelectSlave,
enumDisableTransOnly_DeSelectSlave
);
return Flash_Success;
}
/*******************************************************************************
Function: FlashReleaseFromDeepPowerDown( void )
Arguments: void
Return Value:
Flash_Success
Description: This function takes the device out of the Deep Power-down
mode by sending an SPI_FLASH_INS_RES.
Pseudo Code:
Step 1: Initialize the data (i.e. Instruction) packet to be sent serially
Step 2: Send the packet serially
*******************************************************************************/
ReturnType FlashReleaseFromDeepPowerDown( void )
{
CharStream char_stream_send;
ST_uint8 cRES = SPI_FLASH_INS_RES;
// Step 1: Initialize the data (i.e. Instruction) packet to be sent serially
char_stream_send.length = 1;
char_stream_send.pChar = &cRES;
// Step 2: Send the packet serially
Serialize(&char_stream_send,
ptrNull,
enumEnableTransOnly_SelectSlave,
enumDisableTransOnly_DeSelectSlave
);
return Flash_Success;
}
#endif // end of #ifndef NO_DEEP_POWER_DOWN_SUPPORT
/*******************************************************************************
Function: FlashProgram( ST_uint32 udAddr, ST_uint8 *pArray, ST_uint32 udNrOfElementsInArray )
Arguments: udAddr, start address to program
pArray, address of the buffer that holds the elements to be programmed
udNrOfElementsInArray, number of elements to be programmed, counted in bytes
Return Value:
Flash_AddressInvalid
Flash_MemoryOverflow
Flash_OperationTimeOut
Flash_Success
Description: This function programs a chunk of data into the memory at one go.
If the start address and the available space are checked successfully,
this function programs data from the buffer(pArray) to the memory sequentially by
invoking FlashPageProgram(). This function automatically handles page boundary
crossing, if any.
Like FlashPageProgram(), this function assumes that the memory to be programmed
has been previously erased or that bits are only changed from 1 to 0.
Note:
This function does not check whether the target memory area is in a Software
Protection Mode(SPM) or Hardware Protection Mode(HPM), in which case the PP
Instruction will be ignored.
The function assumes that the target memory area has previously been unprotected at both
the hardware and software levels.
To unprotect the memory, please call FlashWriteStatusRegister(ST_uint8 ucStatusRegister),
and refer to the datasheet for a proper ucStatusRegister value.
Pseudo Code:
Step 1: Validate address input
Step 2: Check memory space available on the whole memory
Step 3: calculte memory space available within the page containing the start address(udAddr)
Step 3-1: if the page boundary is crossed, invoke FlashPageProgram() repeatedly
Step 3-2: if the page boundary is not crossed, invoke FlashPageProgram() once only
*******************************************************************************/
ReturnType FlashProgram( ST_uint32 udAddr, ST_uint8 *pArray, ST_uint32 udNrOfElementsInArray )
{
ST_uint16 ucMargin;
ST_uint16 ucPageCount, ucRemainder;
ReturnType typeReturn;
// Step 1: Validate address input
if(!(udAddr < FLASH_SIZE)) return Flash_AddressInvalid;
// Step 2: Check memory space available on the whole memory
if(udAddr + udNrOfElementsInArray > FLASH_SIZE) return Flash_MemoryOverflow;
// Step 3: calculte memory space available within the page containing the start address(udAddr)
ucMargin = (ST_uint8)(~udAddr) + 1;
// Step 3-1: if the page boundary is crossed, invoke FlashPageWrite() repeatedly
if(udNrOfElementsInArray > ucMargin)
{
typeReturn = FlashPageProgram(udAddr, pArray, ucMargin);
if(Flash_Success != typeReturn) return typeReturn; // return immediately if Not successful
udNrOfElementsInArray -= ucMargin; // re-calculate the number of elements
pArray += ucMargin; // modify the pointer to the buffer
udAddr += ucMargin; // modify the start address in the memory
ucPageCount = udNrOfElementsInArray / FLASH_WRITE_BUFFER_SIZE; // calculate the number of pages to be programmed
ucRemainder = udNrOfElementsInArray % FLASH_WRITE_BUFFER_SIZE; // calculate the remainder after filling up one or more whole pages
while(ucPageCount--)
{
typeReturn = FlashPageProgram(udAddr, pArray, FLASH_WRITE_BUFFER_SIZE);
if(Flash_Success != typeReturn) return typeReturn; // return immediately if Not successful
pArray += FLASH_WRITE_BUFFER_SIZE;
udAddr += FLASH_WRITE_BUFFER_SIZE;
};
return FlashPageProgram(udAddr, pArray, ucRemainder);
}
// Step 3-2: if the page boundary is not crossed, invoke FlashPageWrite() once only
else
{
return FlashPageProgram(udAddr, pArray, udNrOfElementsInArray);
}
}
/*******************************************************************************
Function: IsFlashBusy( )
Arguments: none
Return Value:
TRUE
FALSE
Description: This function checks the Write In Progress (WIP) bit to determine whether
the Flash memory is busy with a Write, Program or Erase cycle.
Pseudo Code:
Step 1: Read the Status Register.
Step 2: Check the WIP bit.
*******************************************************************************/
BOOL IsFlashBusy()
{
ST_uint8 ucSR;
// Step 1: Read the Status Register.
FlashReadStatusRegister(&ucSR);
// Step 2: Check the WIP bit.
if(ucSR & SPI_FLASH_WIP)
return TRUE;
else
return FALSE;
}
#ifdef VERBOSE
/*******************************************************************************
Function: FlashErrorStr( ReturnType rErrNum );
Arguments: rErrNum is the error number returned from other Flash memory Routines
Return Value: A pointer to a string with the error message
Description: This function is used to generate a text string describing the
error from the Flash memory. Call with the return value from other Flash memory routines.
Pseudo Code:
Step 1: Return the correct string.
*******************************************************************************/
ST_sint8 *FlashErrorStr( ReturnType rErrNum )
{
switch(rErrNum)
{
case Flash_AddressInvalid:
return "Flash - Address is out of Range";
case Flash_MemoryOverflow:
return "Flash - Memory Overflows";
case Flash_PageEraseFailed:
return "Flash - Page Erase failed";
case Flash_PageNrInvalid:
return "Flash - Page Number is out of Range";
case Flash_SectorNrInvalid:
return "Flash - Sector Number is out of Range";
case Flash_FunctionNotSupported:
return "Flash - Function not supported";
case Flash_NoInformationAvailable:
return "Flash - No Additional Information Available";
case Flash_OperationOngoing:
return "Flash - Operation ongoing";
case Flash_OperationTimeOut:
return "Flash - Operation TimeOut";
case Flash_ProgramFailed:
return "Flash - Program failed";
case Flash_Success:
return "Flash - Success";
case Flash_WrongType:
return "Flash - Wrong Type";
default:
return "Flash - Undefined Error Value";
} /* EndSwitch */
} /* EndFunction FlashErrorString */
#endif /* VERBOSE Definition */
/*******************************************************************************
Function: FlashTimeOut(ST_uint32 udSeconds)
Arguments: udSeconds holds the number of seconds before TimeOut occurs
Return Value:
Flash_OperationTimeOut
Flash_OperationOngoing
Example: FlashTimeOut(0) // Initializes the Timer
While(1) {
...
If (FlashTimeOut(5) == Flash_OperationTimeOut) break;
// The loop is executed for 5 Seconds before the operation is aborted
} EndWhile
*******************************************************************************/
#ifdef TIME_H_EXISTS
/*-----------------------------------------------------------------------------
Description: This function provides a timeout for Flash polling actions or
other operations which would otherwise never return.
The Routine uses the function clock() inside ANSI C library "time.h".
-----------------------------------------------------------------------------*/
ReturnType FlashTimeOut(ST_uint32 udSeconds){
static clock_t clkReset,clkCount;
if (udSeconds == 0) { /* Set Timeout to 0 */
clkReset=clock();
} /* EndIf */
clkCount = clock() - clkReset;
if (clkCount<(CLOCKS_PER_SEC*(clock_t)udSeconds))
return Flash_OperationOngoing;
else
return Flash_OperationTimeOut;
}/* EndFunction FlashTimeOut */
#else
/*-----------------------------------------------------------------------------
Description: This function provides a timeout for Flash polling actions or
other operations which would otherwise never return.
The Routine uses COUNT_FOR_A_SECOND which is considered to be a loop that
counts for one second. It needs to be adapted to the target Hardware.
-----------------------------------------------------------------------------*/
ReturnType FlashTimeOut(ST_uint32 udSeconds) {
static ST_uint32 udCounter = 0;
if (udSeconds == 0) { /* Set Timeout to 0 */
udCounter = 0;
} /* EndIf */
if (udCounter == (udSeconds * COUNT_FOR_A_SECOND)) {
udCounter = 0;
return Flash_OperationTimeOut;
} else {
udCounter++;
return Flash_OperationOngoing;
} /* Endif */
} /* EndFunction FlashTimeOut */
#endif /* TIME_H_EXISTS */
/*******************************************************************************
End of c2082.c
*******************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -