Exemplo n.º 1
0
/*
**  This function will send a sector erase command and will erase a sector of 
**  Flash.
*/
static void FlashSectorErase(void)
{
    unsigned int dummy = 0;
    unsigned short length = 0;

    txBuffer[0] = FLASH_SECTOR_ERASE;
    txBuffer[1] = FLASH_SECTOR_ADD_HIGH;
    txBuffer[2] = FLASH_SECTOR_ADD_MID;
    txBuffer[3] = FLASH_SECTOR_ADD_LOW;

    length = 4;

    /* Configure the Sector erase parameters for Edma transmit.*/
    McSpiTxEdmaParamSet(MCSPI_TX_EVENT, MCSPI_TX_EVENT, txBuffer, 
                        length);

    /* Configure the sector erase parameters for Edma receive.*/
    McSpiRxEdmaParamSet(MCSPI_RX_EVENT, MCSPI_RX_EVENT, (unsigned char *)dummy, 
                        length, FALSE);

    /* Register the call-back function for McSPI Tx/Rx events.*/
    cb_Fxn[MCSPI_TX_EVENT] = &CallBack;
    cb_Fxn[MCSPI_RX_EVENT] = &CallBack;

    McSPITransfer(length);

    IsFlashBusy();

}
Exemplo n.º 2
0
/*
**    This function will read data of 1 page size from a page of flash.
*/
static void ReadFromFlash(void)
{
    unsigned int index = 0;
    unsigned short length = 0;

    txBuffer[0] = FLASH_DATA_READ;
    txBuffer[1] = FLASH_SECTOR_ADD_HIGH;
    txBuffer[2] = FLASH_SECTOR_ADD_MID;
    txBuffer[3] = FLASH_SECTOR_ADD_LOW;

    for(index = 4; index < 260; index++)
    {
        txBuffer[index] = 0;
    }

    length = 260;

    /* Configure the read data parameters of McSPI for Edma transmit.*/
    McSpiTxEdmaParamSet(MCSPI_TX_EVENT, MCSPI_TX_EVENT, txBuffer, 
                        length);

    /* Configure the read data parameters of McSPI for Edma receive.*/
    McSpiRxEdmaParamSet(MCSPI_RX_EVENT, MCSPI_RX_EVENT, rxBuffer, 
                        length, TRUE);

    /* Register the call-back function for Tx/Rx edma events of McSPI.*/
    cb_Fxn[MCSPI_TX_EVENT] = &CallBack;
    cb_Fxn[MCSPI_RX_EVENT] = &CallBack;

    McSPITransfer(length);

    /* Check whether flash is busy in reading data from flash. */
    IsFlashBusy();
}
Exemplo n.º 3
0
/*
**  This function will send the page program command to the flash device.
*/
static void FlashPageProgram(void)
{
    unsigned int index = 0;
    unsigned int dummy = 0;
    unsigned short length = 0;

    txBuffer[0] = FLASH_PAGE_PROGRAM;
    txBuffer[1] = FLASH_SECTOR_ADD_HIGH;
    txBuffer[2] = FLASH_SECTOR_ADD_MID;
    txBuffer[3] = FLASH_SECTOR_ADD_LOW;

    for(index = 0; index < 256; index++)
    {
        txBuffer[index + 4] = (unsigned char) index;
        vrfyData[index] = (unsigned char) index;
    }

    length = 260;

    /* Configure the Page-program parameters for Edma transmit.*/
    McSpiTxEdmaParamSet(MCSPI_TX_EVENT, MCSPI_TX_EVENT, txBuffer, 
                        length);

    /* Configure the Page-program parameters for Edma receive.*/
    McSpiRxEdmaParamSet(MCSPI_RX_EVENT, MCSPI_RX_EVENT, (unsigned char *)dummy, 
                        length, FALSE);

    /* Register the call-back function for McSPI Tx/Rx events.*/
    cb_Fxn[MCSPI_TX_EVENT] = &CallBack;
    cb_Fxn[MCSPI_RX_EVENT] = &CallBack;

    McSPITransfer(length);

    IsFlashBusy();
}
Exemplo n.º 4
0
/************************************************
使能Flash写命令
入口参数: 无
出口参数: 无
************************************************/
void FlashWriteEnable()
{
    while (IsFlashBusy());                      //Flash忙检测
    CS_PM25 = 0;
    WriteReadByte(SFC_WREN);                         //发送写使能命令
    CS_PM25 = 1;
}
Exemplo n.º 5
0
/*******************************************************************************
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;
}
Exemplo n.º 6
0
/*
** This function will erase a sector of flash.
*/
static void SectorErase(void)
{
    txBuffer[0] = FLASH_SECTOR_ERASE;
    txBuffer[1] = FLASH_SECTOR_ADD_HIGH;
    txBuffer[2] = FLASH_SECTOR_ADD_MID;
    txBuffer[3] = FLASH_SECTOR_ADD_LOW;

    length = 4;

    McSPITransfer();

    IsFlashBusy();
}
Exemplo n.º 7
0
/*******************************************************************************
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
	enable_cs();
    Serialize_nf(&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_nf(&char_stream_send, 
              ptrNull,
              enumNull,
              enumDisableTransOnly_DeSelectSlave
              );
	disable_cs();

    // Step 8: Wait until the operation completes or a timeout occurs.
    WAIT_TILL_Instruction_EXECUTION_COMPLETE(1)

    return Flash_Success;
}
Exemplo n.º 8
0
/*******************************************************************************
Function:     ReturnType FlashSubSectorErase( uSectorType uscSectorNr )
Arguments:    uSectorType is the number of the SubSector to be erased.

Return Values:
   Flash_SectorNrInvalid
   Flash_OperationOngoing
   Flash_OperationTimeOut
   Flash_Success           

Description:  This function erases the SubSector 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  FlashSubSectorErase( unsigned int uscSectorNr )
{
	CharStream char_stream_send;
	ST_uint8  pIns_Addr[4]; 

    // Step 1: Validate the sector number input
	if(!(uscSectorNr < FLASH_SUBSECTOR_COUNT)) return Flash_SectorNrInvalid;
	
	uscSectorNr = uscSectorNr * 0x1000;

    // 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];
	pIns_Addr[0]              = SPI_FLASH_INS_SSE;
#ifdef FLASH_SMALLER_SECTOR_SIZE
	pIns_Addr[1]              = uscSectorNr>>1;
	pIns_Addr[2]              = uscSectorNr<<7;
	pIns_Addr[3]              = 0;
#else
	pIns_Addr[1]              = (uscSectorNr&0x00FF0000)>>16;
	pIns_Addr[2]              = (uscSectorNr&0x0000FF00)>>8;
	pIns_Addr[3]              = (uscSectorNr&0x000000FF);
#endif

    // 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;
}
Exemplo n.º 9
0
/************************************************
从Flash中读取数据
入口参数:
    addr   : 地址参数
    size   : 数据块大小
    buffer : 缓冲从Flash中读取的数据
出口参数:
    无
************************************************/
void FlashRead(ulong addr, ulong size, uchar *buffer)
{
    if (g_fFlashOK)
    {
        while (IsFlashBusy());                  //Flash忙检测
        CS_PM25 = 0;
        WriteReadByte(SFC_FASTREAD);                 //使用快速读取命令
        WriteReadByte(((uchar *)&addr)[1]);           //设置起始地址
        WriteReadByte(((uchar *)&addr)[2]);
        WriteReadByte(((uchar *)&addr)[3]);
        WriteReadByte(0);                            //需要空读一个字节
        while (size)
        {
            *buffer = WriteReadByte(0);              //自动连续读取并保存
            addr++;
            buffer++;
            size--;
        }
        CS_PM25 = 1;
    }
}
Exemplo n.º 10
0
/*******************************************************************************
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;
}