📄 dataflash.c
字号:
if(IsFlashBusy()) return Flash_OperationOngoing;
// Step 3: Disable Write protection
FlashWriteEnable();
// Step 4: Initialize the data (instruction & address only) packet to be sent serially
char_stream_send.length = 4;
char_stream_send.pChar = pIns_Addr;
pIns_Addr[0] = SPI_FLASH_INS_PP;
pIns_Addr[1] = udAddr>>16;
pIns_Addr[2] = udAddr>>8;
pIns_Addr[3] = udAddr;
// Step 5: Send the packet (instruction & address only) serially
Serialize(&char_stream_send,
ptrNull,
enumEnableTransOnly_SelectSlave,
enumNull
);
// Step 6: Initialize the data (data to be programmed) packet to be sent serially
char_stream_send.length = udNrOfElementsInArray;
char_stream_send.pChar = pArray;
// Step 7: Send the packet(data to be programmed) serially
Serialize(&char_stream_send,
0,
enumNull,
enumDisableTransOnly_DeSelectSlave
);
// Step 8: Wait until the operation completes or a timeout occurs.
WAIT_TILL_INSTRUCTION_EXECUTION_COMPLETE(PP_TIMEOUT)
return Flash_Success;
} /* EndFunction FlashPageProgram */
#ifdef FULL_DATAFLASH_FUNCTION
/*******************************************************************************
Function: ReturnType FlashPageErase( uPageType upgPageNr )
Arguments: upgPageNr is the number of the Page to be erased.
Return Values:
Flash_PageNrInvalid
Flash_OperationOngoing
Flash_OperationTimeOut
Flash_Success
Description: This function erases the Page specified in upgPageNr
by sending an SPI_FLASH_INS_PE instruction.
The function checks that the Page number is within the valid range before issuing
the erase instruction. Once erase has completed the Flash_Success value
is returned.
Pseudo Code:
Step 1: Validate page number input
Step 2: Check whether any previous Write, Program or Erase cycle is still on-going
Step 3: Disable Write protection (the Flash memory will automatically enable it again after
the execution of the instruction)
Step 4: Initialize the data (instruction & address) packet to be sent serially
Step 5: Send the packet (data to be programmed) serially
Step 6: Wait until the operation completes or a timeout occurs.
*******************************************************************************/
ReturnType FlashPageErase( uPageType upgPageNr )
{
CharStream char_stream_send;
ST_uint8 pIns_Addr[4];
// Step 1: Validate page number input
if(!(upgPageNr < FLASH_PAGE_COUNT)) return Flash_PageNrInvalid;
// 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 (instruction & address) packet to be sent serially
char_stream_send.length = 4;
char_stream_send.pChar = pIns_Addr;
pIns_Addr[0] = SPI_FLASH_INS_PE;
pIns_Addr[1] = upgPageNr>>8;
pIns_Addr[2] = upgPageNr;
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(PE_TIMEOUT)
return Flash_Success;
} /* EndFunction FlashPageErase */
#endif
#ifdef FULL_DATAFLASH_FUNCTION
/*******************************************************************************
Function: ReturnType FlashSectorErase( uSectorType uscSectorNr )
Arguments: uSectorType is the number of the Sector to be erased.
Return Values:
Flash_SectorNrInvalid
Flash_OperationOngoing
Flash_OperationTimeOut
Flash_Success
Description: This function erases the Sector specified in uscSectorNr
by sending an SPI_FLASH_INS_SE instruction.
The function checks that the sector number is within the valid range before issuing
the erase instruction. Once erase has completed the Flash_Success value
is returned.
Pseudo Code:
Step 1: Validate sector number input
Step 2: Check whether any previous Write, Program or Erase cycle is still on-going
Step 3: Disable Write protection (the Flash memory will automatically enable it
again after the execution of the instruction)
Step 4: Initialize the data (instruction & address) packet to be sent serially
Step 5: Send the packet (instruction & address) serially
Step 6: Wait until the operation completes or a timeout occurs.
*******************************************************************************/
ReturnType FlashSectorErase( uSectorType uscSectorNr )
{
CharStream char_stream_send;
ST_uint8 pIns_Addr[4];
// Step 1: Validate sector number input
if(!(uscSectorNr < FLASH_SECTOR_COUNT)) return Flash_SectorNrInvalid;
// 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 (instruction & address) packet to be sent serially
char_stream_send.length = 4;
char_stream_send.pChar = pIns_Addr;
pIns_Addr[0] = SPI_FLASH_INS_SE;
pIns_Addr[1] = uscSectorNr;
pIns_Addr[2] = 0;
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(SE_TIMEOUT)
return Flash_Success;
} /* EndFunction FlashSectorErase */
#endif
#ifdef FULL_DATAFLASH_FUNCTION
/*******************************************************************************
Function: FlashDeepPowerDown( void )
Arguments: void
Return Value:
Flash_OperationOngoing
Flash_Success
Description: This function puts the device in the lowest power 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 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 still 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 still 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;
}/* EndFunction FlashDeepPowerDown */
/*******************************************************************************
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_RDP.
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 cRDP = SPI_FLASH_INS_RDP;
// Step 1: Initialize the data (i.e. instruction) packet to be sent serially
char_stream_send.length = 1;
char_stream_send.pChar = &cRDP;
// Step 2: Send the packet serially
Serialize(&char_stream_send,
ptrNull,
enumEnableTransOnly_SelectSlave,
enumDisableTransOnly_DeSelectSlave
);
return Flash_Success;
} /* EndFunction FlashReleaseFromDeepPowerDown */
#endif
/*******************************************************************************
Function: FlashWrite( ST_uint32 udAddr, ST_uint8 *pArray, ST_uint32 udNrOfElementsInArray)
Arguments: udAddr, start address to write
pArray, address of the buffer that holds the elements to be written
udNrOfElementsInArray, counter of elements to be written, counted in bytes
Return Value:
Flash_OperationOngoing
Flash_AddressInvalid
Flash_MemoryOverflow
Flash_OperationTimeOut
Flash_Success
Description: This function writes a chunk of data into the memory at one go.
If verifying the start address and checking the available space are successful,
this function writes data from the buffer(pArray) to the memory sequentially by
invoking FlashPageWrite(). This function automatically handles page boundary
crosses, if any.
Pseudo Code:
Step 1: Validate address input
Step 2: Check memory space available on the whole memory
Step 3: calculate memory space available within the page containing the start address(udAddr)
Step 3-1: if the page boundary is crossed, invoke FlashPageWrite() repeatedly
*******************************************************************************/
ReturnType FlashWrite( ST_uint32 udAddr, ST_uint8 *pArray, ST_uint32 udNrOfElementsInArray )
{
ST_uint16 ucMargin;
ST_uint16 bytes_to_write;
ST_uint32 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
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 = FlashPageWrite(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;
} /* EndFunction FlashWrite */
#ifdef FULL_DATAFLASH_FUNCTION
/*******************************************************************************
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 verifying the start address and checking the available space is successful,
this function programs data from the buffer(pArray) to the memory sequentially by
invoking FlashPageProgram(). This function automatically handles page boundary
crosses, if any.
Like FlashPageProgram(), this function assumes that the memory to be programmed
has previously been erased or that bits are only changed from 1 to 0.
Pseudo Code:
Step 1: Validate address input
Step 2: Check memory space available in 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
*******************************************************************************/
ReturnType FlashProgram( ST_uint32 udAddr, ST_uint8 *pArray, ST_uint32 udNrOfElementsInArray )
{
ST_uint16 ucMargin;
ST_uint16 bytes_to_write;
ST_uint32 ucRemainder;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -