📄 c2082.c
字号:
#ifdef USE_JEDEC_STANDARD_TWO_BYTE_SIGNATURE
/*******************************************************************************
Function: FlashReadManufacturerIdentification( ST_uint8 *ucpManufactureIdentification)
Arguments: ucpManufacturerIdentification, 8-bit buffer to hold the manufacturer identification
being read from the memory
Return Value:
Flash_WrongType: if any value other than MANUFACTURER_ST(0x20) is returned
Flash_Success : if MANUFACTURER_ST(0x20) is correctly returned
Description: This function returns the Manufacturer Identification(0x20) by sending an
SPI_FLASH_INS_RDID Instruction.
After retrieving the Manufacturer Identification, the routine checks if the device is
an ST memory product. If not, Flash_WrongType is returned.
Note: The availability of this function should be checked in the appropriate datasheet
for each memory.
Pseudo Code:
Step 1: Initialize the data (i.e. Instruction) packet to be sent serially
Step 2: Send the packet serially
Step 3: get the Manufacturer Identification
*******************************************************************************/
ReturnType FlashReadManufacturerIdentification( ST_uint8 *ucpManufacturerIdentification)
{
CharStream char_stream_send;
CharStream char_stream_recv;
ST_uint8 cRDID = SPI_FLASH_INS_RDID;
ST_uint8 pIdentification[3];
// Step 1: Initialize the data (i.e. Instruction) packet to be sent serially
char_stream_send.length = 1;
char_stream_send.pChar = &cRDID;
char_stream_recv.length = 3;
char_stream_recv.pChar = &pIdentification[0];
// Step 2: Send the packet serially
Serialize(&char_stream_send,
&char_stream_recv,
enumEnableTansRecv_SelectSlave,
enumDisableTansRecv_DeSelectSlave
);
// Step 3: get the Manufacturer Identification
*ucpManufacturerIdentification = pIdentification[0];
if(MANUFACTURER_ST == *ucpManufacturerIdentification)
{
return Flash_Success;
}
else
{
return Flash_WrongType;
}
}
#endif // end of #ifdef USE_JEDEC_STANDARD_TWO_BYTE_SIGNATURE
/*******************************************************************************
Function: FlashReadStatusRegister( ST_uint8 *ucpStatusRegister)
Arguments: ucpStatusRegister, 8-bit buffer to hold the Status Register value read
from the memory
Return Value:
Flash_Success
Description: This function reads the Status Register by sending an
SPI_FLASH_INS_RDSR Instruction.
Pseudo Code:
Step 1: Initialize the data (i.e. Instruction) packet to be sent serially
Step 2: Send the packet serially, get the Status Register content
*******************************************************************************/
ReturnType FlashReadStatusRegister( ST_uint8 *ucpStatusRegister)
{
CharStream char_stream_send;
CharStream char_stream_recv;
ST_uint8 cRDSR = SPI_FLASH_INS_RDSR;
// Step 1: Initialize the data (i.e. Instruction) packet to be sent serially
char_stream_send.length = 1;
char_stream_send.pChar = &cRDSR;
char_stream_recv.length = 1;
char_stream_recv.pChar = ucpStatusRegister;
// Step 2: Send the packet serially, get the Status Register content
Serialize(&char_stream_send,
&char_stream_recv,
enumEnableTansRecv_SelectSlave,
enumDisableTansRecv_DeSelectSlave
);
return Flash_Success;
}
/*******************************************************************************
Function: FlashWriteStatusRegister( ST_uint8 ucStatusRegister)
Arguments: ucStatusRegister, an 8-bit new value to be written to the Status Register
Return Value:
Flash_Success
Description: This function modifies the Status Register by sending an
SPI_FLASH_INS_WRSR Instruction.
The Write Status Register (WRSR) Instruction has no effect
on b6, b5, b1(WEL) and b0(WIP) of the Status Register.b6 and b5 are
always read as 0.
Pseudo Code:
Step 1: Disable Write protection
Step 2: Initialize the data (i.e. Instruction & value) packet to be sent serially
Step 3: Send the packet serially
Step 4: Wait until the operation completes or a timeout occurs.
*******************************************************************************/
ReturnType FlashWriteStatusRegister( ST_uint8 ucStatusRegister)
{
CharStream char_stream_send;
ST_uint8 pIns_Val[2];
// Step 1: Disable Write protection
FlashWriteEnable();
// Step 2: Initialize the data (i.e. Instruction & value) packet to be sent serially
char_stream_send.length = 2;
char_stream_send.pChar = pIns_Val;
pIns_Val[0] = SPI_FLASH_INS_WRSR;
pIns_Val[1] = ucStatusRegister;
// Step 3: Send the packet serially
Serialize(&char_stream_send,
ptrNull,
enumEnableTransOnly_SelectSlave,
enumDisableTransOnly_DeSelectSlave
);
// Step 4: Wait until the operation completes or a timeout occurs.
WAIT_TILL_Instruction_EXECUTION_COMPLETE(1)
return Flash_Success;
}
/*******************************************************************************
Function: FlashRead( ST_uint32 udAddr, ST_uint8 *ucpElements, ST_uint32 udNrOfElementsToRead)
Arguments: udAddr, start address to read from
ucpElements, buffer to hold the elements to be returned
udNrOfElementsToRead, number of elements to be returned, counted in bytes.
Return Value:
Flash_AddressInvalid
Flash_Success
Description: This function reads the Flash memory by sending an
SPI_FLASH_INS_READ Instruction.
by design, the whole Flash memory space can be read with one READ Instruction
by incrementing the start address and rolling to 0x0 automatically,
that is, this function is across pages and sectors.
Pseudo Code:
Step 1: Validate address input
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 FlashRead( uAddrType udAddr, ST_uint8 *ucpElements, ST_uint32 udNrOfElementsToRead)
{
CharStream char_stream_send;
CharStream char_stream_recv;
ST_uint8 pIns_Addr[4];
// Step 1: Validate address input
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_READ;
pIns_Addr[1] = udAddr>>16;
pIns_Addr[2] = udAddr>>8;
pIns_Addr[3] = udAddr;
char_stream_recv.length = udNrOfElementsToRead;
char_stream_recv.pChar = ucpElements;
// 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;
}
/*******************************************************************************
Function: FlashFastRead( ST_uint32 udAddr, ST_uint8 *ucpElements, ST_uint32 udNrOfElementsToRead)
Arguments: udAddr, start address to read from
ucpElements, buffer to hold the elements to be returned
udNrOfElementsToRead, number of elements to be returned, counted in bytes.
Return Value:
Flash_AddressInvalid
Flash_Success
Description: This function reads the Flash memory by sending an
SPI_FLASH_INS_FAST_READ Instruction.
by design, the whole Flash memory space can be read with one FAST_READ Instruction
by incrementing the start address and rolling to 0x0 automatically,
that is, this function is across pages and sectors.
Pseudo Code:
Step 1: Validate address input
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 FlashFastRead( uAddrType udAddr, ST_uint8 *ucpElements, ST_uint32 udNrOfElementsToRead)
{
CharStream char_stream_send;
CharStream char_stream_recv;
ST_uint8 pIns_Addr[5];
// Step 1: Validate address input
if(!(udAddr < FLASH_SIZE)) return Flash_AddressInvalid;
// Step 2: 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_FAST_READ;
pIns_Addr[1] = udAddr>>16;
pIns_Addr[2] = udAddr>>8;
pIns_Addr[3] = udAddr;
pIns_Addr[4] = SPI_FLASH_INS_DUMMY;
char_stream_recv.length = udNrOfElementsToRead;
char_stream_recv.pChar = ucpElements;
// 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;
}
/*******************************************************************************
Function: FlashPageProgram( ST_uint32 udAddr, ST_uint8 *pArray, ST_uint32 udNrOfElementsInArray)
Arguments: udAddr, start address to write to
pArray, buffer to hold the elements to be programmed
udNrOfElementsInArray, number of elements to be programmed, counted in bytes
Return Value:
Flash_AddressInvalid
Flash_OperationOngoing
Flash_OperationTimeOut
Flash_Success
Description: This function writes a maximum of 256 bytes of data into the memory by sending an
SPI_FLASH_INS_PP Instruction.
by design, the PP Instruction is effective WITHIN ONE page,i.e. 0xXX00 - 0xXXff.
when 0xXXff is reached, the address rolls over to 0xXX00 automatically.
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 the setup of a proper ucStatusRegister value.
Pseudo Code:
Step 1: Validate address input
Step 2: Check whether any previous Write, Program or Erase cycle is 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 only) packet to be sent serially
Step 5: Send the packet (Instruction & address only) serially
Step 6: Initialize the data (data to be programmed) packet to be sent serially
Step 7: Send the packet (data to be programmed) serially
Step 8: Wait until the operation completes or a timeout occurs.
*******************************************************************************/
ReturnType FlashPageProgram( uAddrType udAddr, ST_uint8 *pArray , ST_uint16 udNrOfElementsInArray)
{
CharStream char_stream_send;
ST_uint8 pIns_Addr[4];
// Step 1: Validate address input
if(!(udAddr < FLASH_SIZE)) return Flash_AddressInvalid;
// Step 2: Check whether any previous Write, Program or Erase cycle is on-going
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,
ptrNull,
enumNull,
enumDisableTransOnly_DeSelectSlave
);
// Step 8: Wait until the operation completes or a timeout occurs.
WAIT_TILL_Instruction_EXECUTION_COMPLETE(1)
return Flash_Success;
}
/*******************************************************************************
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 status
Flash_Success is returned.
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 to set a proper ucStatusRegister value.
Pseudo Code:
Step 1: Validate the sector number input
Step 2: Check whether any previous Write, Program or Erase cycle is 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 the sector number input
if(!(uscSectorNr < FLASH_SECTOR_COUNT)) return Flash_SectorNrInvalid;
// Step 2: Check whether any previous Write, Program or Erase cycle is 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[0];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -