⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 flashloader.c

📁 linked list construct to support any number of Flash banks.
💻 C
📖 第 1 页 / 共 5 页
字号:
        case STFLASH_ACCESS_16_BITS:

            /* Clear Status Register (precautionary) */
            *EraseAddr16 = CLEAR_SR16;              /* "Don't Care" address in Flash */

            /* commence Erase Command Sequence */
            *EraseAddr16 = ERASE_SU16;              /* "Don't Care" address in Flash */
            *EraseAddr16 = ERASE_CR16;              /* Confirm at Flash block base   */

            StartTime = time_now();                 /* ticks at Erase start */

            /* commence Read Status Register Command Sequence */
            *EraseAddr16 = READ_SR16;               /* "Don't Care" address in Flash */

            /* poll for completion of Erasure on both chips */
            while( DoingErase )
            {
                if( ( *EraseAddr16 &
                    COM_STAT16 ) == COM_STAT16 )
                {
                    DoingErase = FALSE;        /* Erase sequence completed */

                    /* check for Erase sequence failure on either chip */
                    if( ( *EraseAddr16 & ERA_STAT16 ) != 0 )
                    {
                        RetValue = STFLASH_ERROR_ERASE;
                    }
                    else
                    {
                        /* check for Vpp below minimum on either chip */
                        if( ( *EraseAddr16 & VPP_STAT16 ) != 0 )
                        {
                            RetValue = STFLASH_ERROR_VPP_LOW;
                        }
                    }
                }
                else
                {
                    Curr_Time = time_now();
                    if( time_minus( Curr_Time,
                        StartTime ) >
                            (clock_t)ERASE_TIMEOUT )
                    {
                        DoingErase = FALSE;    /* Erase sequence timed out */
                        RetValue = ST_ERROR_TIMEOUT;
                    }
                }
            }

            break;


        case STFLASH_ACCESS_32_BITS:

            /* Clear Status Register (precautionary) */
            *EraseAddr32 = CLEAR_SR32;              /* "Don't Care" address in Flash */

            /* commence Erase Command Sequence */
            *EraseAddr32 = ERASE_SU32;              /* "Don't Care" address in Flash */
            *EraseAddr32 = ERASE_CR32;              /* Confirm at Flash block base   */

            StartTime = time_now();                 /* ticks at Erase start */

            /* commence Read Status Register Command Sequence */
            *EraseAddr32 = READ_SR32;               /* "Don't Care" address in Flash */

            /* poll for completion of Erasure on all chips */
            while( DoingErase )
            {
                if( ( *EraseAddr32 &
                    COM_STAT32 ) == COM_STAT32 )
                {
                    DoingErase = FALSE;        /* Erase sequence completed */

                    /* check for Erase sequence failure on any chip */
                    if( ( *EraseAddr32 & ERA_STAT32 ) != 0 )
                    {
                        RetValue = STFLASH_ERROR_ERASE;
                    }
                    else
                    {
                        /* check for Vpp below minimum on any chip */
                        if( ( *EraseAddr32 & VPP_STAT32 ) != 0 )
                        {
                            RetValue = STFLASH_ERROR_VPP_LOW;
                        }
                    }
                }
                else
                {
                    Curr_Time = time_now();
                    if( time_minus( Curr_Time,
                        StartTime ) >
                            (clock_t)ERASE_TIMEOUT )
                    {
                        DoingErase = FALSE;    /* Erase sequence timed out */
                        RetValue = ST_ERROR_TIMEOUT;
                    }
                }
            }

            break;

        default:
            RetValue = ST_ERROR_BAD_PARAMETER;
            break;
    }

    VppDisable( ThisElem->VppAddress );
  }
   
    /* restore device to read mode */
    switch( ThisElem->MaxAccessWidth )
    {
        case STFLASH_ACCESS_08_BITS:
            *EraseAddr08 = READ_MA08;
            break;

        case STFLASH_ACCESS_16_BITS:
            *EraseAddr16 = READ_MA16;
            break;

        case STFLASH_ACCESS_32_BITS:
            *EraseAddr32 = READ_MA32;
            break;

        default:
            break;
    }


    return( RetValue );
}


/****************************************************************************
Name         : STFLASH_Write()

Description  : Performs a direct write of the number of bytes requested
               to the specified Flash bank.  The maximum access width is
               used for as much of the operation as possible, provided
               that BOTH pointers are aligned onto boundaries of it.
               The Vpp programming voltage is enabled throughout to
               minimise the settling time overhead.  The Vpp Low error is
               not considered fatal; if raised it will be returned at the
               end when all locations have been Programmed, provided no
               Write Sequence failures are earlier determined.

Parameters   : STFLASH_Handle_t Handle identifies the Flash bank,
               U32 Offset (from device BaseAddress) to write to,
               U8 Buffer pointer from which data will be read,
               U32 Number of bytes to be written, followed by
               U32* Number of bytes actually written (return).

Return Value : ST_ErrorCode_t specified as
               ST_NO_ERROR                 No errors occurred
               ST_ERROR_INVALID_HANDLE     Rogue Flash bank indicator
               ST_ERROR_BAD_PARAMETER      Invalid parameter(s)
               ST_ERROR_TIMEOUT            Timeout error during write
               STFLASH_ERROR_WRITE         Error during write
               STFLASH_ERROR_VPP_LOW       Low Vpp detected during write

See Also     : STFLASH_Init()
               STFLASH_Erase()
               STFLASH_Write()
 ****************************************************************************/

/* HCX 修改8M/16M FLASH 写操作的驱动 2002。12。27 */

ST_ErrorCode_t STFLASH_Write( STFLASH_Handle_t  Handle,
                              U32				Offset[],
                              U8				*Buffer[],
                              U32				NumberToWrite[],
				     U32				iBlockNumToWrite,
                              U32               		*NumberActuallyWritten )
{
    #pragma ST_device( WriteAddr08 )
    #pragma ST_device( WriteAddr16 )
    #pragma ST_device( WriteAddr32 )

    stflash_Inst_t  *ThisElem;

    U32             LastRiteP1;         /* last address to be written plus one */
    ST_ErrorCode_t  RetValue;

    U8              *TempAddress;       /* for recaste */
    U8              *Access_Addr;       /* BaseAddress + Offset */
    volatile U8     *WriteAddr08;       /*  8 bit write access address */
    volatile U16    *WriteAddr16;       /* 16 bit write access address */
    volatile U32    *WriteAddr32;       /* 32 bit write access address */

	U32             iOffset;
	U32             iNumberToWrite;
	U8              *ucBuffer;


    /* Perform necessary validity checks */

    ThisElem = (stflash_Inst_t *)Handle;

    if( ThisElem->MagicNumber != MAGIC_NUMBER ) /* rogue Handle value? */
    {
        *NumberActuallyWritten = 0;
        return( ST_ERROR_INVALID_HANDLE );
    }

    if( NumberToWrite[0] == 0 )                    /* no bytes to write? */
    {
        *NumberActuallyWritten = 0;
        return ST_NO_ERROR;                     /* nothing to do, OK */
    }

    LastRiteP1 = Offset[0] + NumberToWrite[0];        /* compute last address + 1 */
    if( ( Buffer[0] == NULL ) ||                   /* no buffer address? */
        ( LastRiteP1 > ThisElem->LastOffsP1 ) ) /* fallen off end of block? */
    {
        *NumberActuallyWritten = 0;
        return( ST_ERROR_BAD_PARAMETER );
    }

    /* Write operations differ significantly between
       the two devices so there are two functions. */
    switch( ThisElem->DeviceType )
    {
        case STFLASH_M29W800T:
        case STFLASH_M29W800B:
        case STFLASH_M29W1600T:
        case STFLASH_M29W1600B:
            RetValue = m29w800t_Write( ThisElem,
                                       Offset,
                                       Buffer,
                                       NumberToWrite,
						 iBlockNumToWrite,
                                       NumberActuallyWritten );
            break;
 
        default:
            *NumberActuallyWritten = 0;
            return ST_ERROR_FEATURE_NOT_SUPPORTED;
    }

    TempAddress = (U8*)ThisElem->BaseAddress;       /* cast */
    Access_Addr = TempAddress + Offset[0];             /* user offset */

    /* restore device to read mode */
    switch( ThisElem->MaxAccessWidth )
    {
        case STFLASH_ACCESS_08_BITS:
            WriteAddr08 = Access_Addr;
            *WriteAddr08 = READ_MA08;
            break;

        case STFLASH_ACCESS_16_BITS:
            WriteAddr16 = (U16*)Access_Addr;
            *WriteAddr16 = READ_MA16;
            break;

        case STFLASH_ACCESS_32_BITS:
            WriteAddr32 = (U32*)Access_Addr;
            *WriteAddr32 = READ_MA32;
            break;

        default:
            break;
    }

    return( RetValue );
}


/****************************************************************************
Name         : STFLASH_Close()

Description  : Flags the specified Flash Bank as closed

Parameters   : STFLASH_Handle_t Handle points to a Flash Bank Init

Return Value : ST_ErrorCode_t specified as
               ST_ERROR_INVALID_HANDLE     Rogue Handle value
               ST_NO_ERROR                 No errors occurred

See Also     : STFLASH_Handle_t
               STFLASH_Open()
 ****************************************************************************/

ST_ErrorCode_t STFLASH_Close( STFLASH_Handle_t Handle )
{
    stflash_Inst_t  *ThisElem;;
    

    /* Perform parameter validity checks */

    ThisElem = (stflash_Inst_t *)Handle;

    if( ( ThisElem->MagicNumber != MAGIC_NUMBER ) || /* rogue Handle value? */
        ( !ThisElem->BankOpen ) )                    /* Bank not Open? */
    {
        return( ST_ERROR_INVALID_HANDLE );
    }

    ThisElem->BankOpen = FALSE;                      /* flag as closed */

    return( ST_NO_ERROR );
}


/****************************************************************************
Name         : STFLASH_Term()

Description  : Terminates the Flash Memory Interface, effectively
               supporting redefinition to another Flash device.

Parameters   : ST_DeviceName Name specifies name of Flash bank, and
               STFLASH_TermParams_t *TermParams holds ForceTerminate flag.

Return Value : ST_ErrorCode_t specified as
               ST_NO_ERROR                 No errors occurred
               ST_ERROR_UNKNOWN_DEVICE     Init not called

See Also     : STFLASH_Init()
 ****************************************************************************/

ST_ErrorCode_t STFLASH_Term( const ST_DeviceName_t Name, 
                             const STFLASH_TermParams_t *TermParams )
{
    stflash_Inst_t  *ThisElem, *PrevElem;


    /* Perform parameter validity checks */

    if( ( Name       == NULL )   ||                 /* NULL Name ptr? */
        ( TermParams == NULL ) )                    /* NULL structure ptr? */
    {
        return( ST_ERROR_BAD_PARAMETER );
    }

    if( stflash_Sentinel == NULL )               /* no Inits outstanding? */
    {
        return( ST_ERROR_UNKNOWN_DEVICE );       /* Semaphore not created */
    }

        semaphore_wait( &Atomic );               /* start of atomic region */

    for( ThisElem =

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -