📄 c2195.c
字号:
// Step 2: Check memory space available in 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
ucRemainder = udNrOfElementsInArray;
while(ucRemainder>0)
{
if(ucMargin != FLASH_WRITE_BUFFER_SIZE)
{
bytes_to_write = ucMargin;
ucMargin = FLASH_WRITE_BUFFER_SIZE;
}
else
bytes_to_write = FLASH_WRITE_BUFFER_SIZE;
if(ucRemainder <= bytes_to_write)
bytes_to_write = ucRemainder;
typeReturn = FlashPageProgram(udAddr, pArray, bytes_to_write);
if(Flash_Success != typeReturn)
return typeReturn; // return immediately if Not successful
udAddr += bytes_to_write;
pArray += bytes_to_write;
ucRemainder -= bytes_to_write;
}
return typeReturn;
} /* End of FlashProgram*/
#if defined(USE_M25PE80)
/*******************************************************************************
Function: ReturnType FlashBulkErase( void )
Arguments: None.
Return Values:
Flash_OperationOngoing
Flash_OperationTimeOut
Flash_Success
Description: This function erases the whole flash
by sending an SPI_FLASH_INS_BE instruction.
Once erase has completed the Flash_Success value
is returned.
Pseudo Code:
Step 1: Check whether any previous Write, Program or Erase cycle is still on-going
Step 2: Disable 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 pIns_Addr[1];
// Step 1: Check whether any previous Write, Program or Erase cycle is still 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 = pIns_Addr;
pIns_Addr[0] = SPI_FLASH_INS_BE;
// Step 3: 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;
} /* EndFunction FlashBulkErase */
/*******************************************************************************
Function: ST_uint8 FlashReadLockRegister( ST_uint32 udAddr ,ST_uint8 *ucpLockRegister )
Arguments: udAddr, address to read
ucpLockRegister, 8-bit buffer to hold the lock register read from the memory
Return Values:
Flash_AddressInvalid
Flash_Success
Description: This function is used to read the Sector/Sub-sector Lock register.
Pseudo Code:
Step 1: Check Range of address
Step 2: Initialize the data (i.e. instruction) packet to be sent serially
Step 3: Send the packet serially, and fill the buffer with the data being returned
*******************************************************************************/
ReturnType FlashReadLockRegister( ST_uint32 udAddr, ST_uint8 *ucpLockRegister )
{
CharStream char_stream_send,char_stream_recv;
ST_uint8 pIns_Addr[4];
// Step 1: Validate address
if(!(udAddr < FLASH_SIZE)) return Flash_AddressInvalid;
// Step 2: Initialize the data (i.e. instruction) packet to be sent serially
char_stream_send.length = 4;
char_stream_send.pChar = pIns_Addr;
pIns_Addr[0] = SPI_FLASH_INS_RDLR;
pIns_Addr[1] = udAddr>>16;
pIns_Addr[2] = udAddr>>8;
pIns_Addr[3] = udAddr;
char_stream_recv.length = 1;
char_stream_recv.pChar = ucpLockRegister;
// Step 3: Send the packet serially, and fill the buffer with the data being returned
Serialize(&char_stream_send,
&char_stream_recv,
enumEnableTansRecv_SelectSlave,
enumDisableTansRecv_DeSelectSlave
);
return Flash_Success;
} /* EndFunction FlashReadLockRegister */
/*******************************************************************************
Function: ST_uint8 FlashWriteLockReg( ST_uint32 udAddr, ST_uint8 ucLockRegister )
Arguments: udAddr , address to write
ucLockRegister, 8-bit value to write into the lock register
Return Values:
Flash_AddressInvalid
Flash_OperationOngoing
Flash_Success
Description: This function is used to write the Sector/Sub-sector Lock register.
Pseudo Code:
Step 1: Check Range of address
Step 2: Check whether any previous Write, Program or Erase cycle is still on-going
Step 3: Disable Write protection
Step 4: Initialize the data (i.e. instruction) packet to be sent serially
Step 5: Send the packet serially, and fill the buffer with the data being returned
*******************************************************************************/
ReturnType FlashWriteLockRegister( ST_uint32 udAddr, ST_uint8 ucLockRegister )
{
CharStream char_stream_send;
ST_uint8 pIns_Addr[5];
// Step 1: Validate address
if(!(udAddr < FLASH_SIZE)) return Flash_AddressInvalid;
// Step 2: Check whether any previous Write, Program or Erase cycle is still on-going
if (IsFlashBusy()) return Flash_OperationOngoing;
// Step 3: Disable Write protection
FlashWriteEnable();
// Step 4: Initialize the data (i.e. instruction) packet to be sent serially
char_stream_send.length = 5;
char_stream_send.pChar = pIns_Addr;
pIns_Addr[0] = SPI_FLASH_INS_WRLR;
pIns_Addr[1] = udAddr>>16;
pIns_Addr[2] = udAddr>>8;
pIns_Addr[3] = udAddr;
pIns_Addr[4] = ucLockRegister;
// Step 5: Send the packet serially, and fill the buffer with the data being returned
Serialize(&char_stream_send,
ptrNull,
enumEnableTansRecv_SelectSlave,
enumDisableTansRecv_DeSelectSlave
);
return Flash_Success;
} /* EndFunction FlashWriteLockRegister */
#endif
/*******************************************************************************
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 giving a TimeOut
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, then timeout occurs
} EndWhile
*******************************************************************************/
#ifdef TIME_H_EXISTS
/*-----------------------------------------------------------------------------
Description: This function provides a timeout for Flash memory polling actions or
other operations that would otherwise never terminate.
The Routine uses the clock() function inside the 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 memory polling actions or
other operations that would otherwise never terminate.
The Routine uses COUNT_FOR_A_SECOND that gives the number of times a loop has to
be repeated to generate a one second delay. This value 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))
{
return Flash_OperationTimeOut;
} else {
udCounter++;
return Flash_OperationOngoing;
} /* Endif */
} /* EndFunction FlashTimeOut */
#endif /* TIME_H_EXISTS */
/*******************************************************************************
End of C2195.c
*******************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -