📄 c2195.c
字号:
// Step 2: Send the packet serially
Serialize(&char_stream_send,
&char_stream_recv,
enumEnableTansRecv_SelectSlave,
enumDisableTansRecv_DeSelectSlave
);
// Step 3: get the returned Device Identification (memory type + memory capacity)
*uwpDeviceIdentification = char_stream_recv.pChar[1];
*uwpDeviceIdentification <<= 8;
*uwpDeviceIdentification |= char_stream_recv.pChar[2];
if(EXPECTED_DEVICE == *uwpDeviceIdentification)
return Flash_Success;
else
return Flash_WrongType;
} /* EndFunction FlashReadDeviceIdentification */
/*******************************************************************************
Function: FlashReadManufacturerIdentification( ST_uint8 *ucpManufacturerIdentification)
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.
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 pID[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 = &pID[0];
// Step 2: Send the packet serially
Serialize(&char_stream_send,
&char_stream_recv,
enumEnableTansRecv_SelectSlave,
enumDisableTansRecv_DeSelectSlave
);
// Step 3: get the returned Manufacturer Identification
*ucpManufacturerIdentification = pID[0];
if(MANUFACTURER_ST == *ucpManufacturerIdentification)
{
return Flash_Success;
}
else
{
return Flash_WrongType;
}
} /* EndFunction FlashReadManufacturerIdentification */
/*******************************************************************************
Function: FlashReadStatusRegister( ST_uint8 *ucpStatusRegister)
Arguments: ucpStatusRegister, 8-bit buffer to hold the status(WEL + WIP) 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
Step 3: get the returned status(WEL + WIP)
*******************************************************************************/
ReturnType FlashReadStatusRegister( ST_uint8 *ucpStatusRegister)
{
CharStream char_stream_send;
CharStream char_stream_recv;
ST_uint8 cRDSR = SPI_FLASH_INS_RDSR;
ST_uint8 cSR;
// 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 = &cSR;
// Step 2: Send the packet serially
Serialize(&char_stream_send,
&char_stream_recv,
enumEnableTansRecv_SelectSlave,
enumDisableTansRecv_DeSelectSlave
);
// Step 3: get the returned status(WEL + WIP)
*ucpStatusRegister = cSR;
return Flash_Success;
} /* EndFunction FlashReadStatusRegister */
/*******************************************************************************
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 0h automatically,
that is, this function goes 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;
} /* EndFunction FlashRead */
/*******************************************************************************
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 READ instruction
by incrementing the start address and rolling to 0h automatically,
that is, this function goes 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 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;
} /* EndFunction FlashFastRead */
/*******************************************************************************
Function: FlashPageWrite( ST_uint32 udAddr, ST_uint8 *pArray, ST_uint32 udNrOfElementsInArray)
Arguments: udAddr, start address to write to
pArray, buffer to hold the elements to be written
udNrOfElementsInArray, number of elements to be written, counted in bytes
Return Value:
Flash_AddressInvalid
Flash_OperationOngoing
Flash_OperationTimeOut
Flash_Success
Description: This function writes 256 bytes or less of data into the memory by sending an
SPI_FLASH_INS_PW instruction.
by design, the PW instruction is effective WITHIN ONE page,i.e. 0xXX00 - 0xXXFF.
when 0xXXFF is reached, the address rolls over to 0xXX00 automatically.
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 written) packet to be sent serially
Step 7: Send the packet (data to be written) serially
Step 8: Wait until the operation completes or a timeout occurs.
*******************************************************************************/
ReturnType FlashPageWrite( 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 still 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_PW;
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 written) packet to be sent serially
char_stream_send.length = udNrOfElementsInArray;
char_stream_send.pChar = pArray;
// Step 7: Send the packet (data to be written) 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(PW_TIMEOUT)
return Flash_Success;
} /* EndFunction FlashPageWrite */
/*******************************************************************************
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 256 bytes or less 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.
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 a Write, Program or Erase cycle is still on-going
if(IsFlashBusy()) return Flash_OperationOngoing;
// Step 3: Disable Write protection
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -