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

📄 29f800a.c

📁 M3355的源代码
💻 C
📖 第 1 页 / 共 3 页
字号:
 * Function: int FlashProgram( DWORD dwOff, DWORD dwNum, void *Array )
 * 
 * Description: This function is used to program an array into the flash. It does
 * not erase the flash first and may fail if the block(s) are not erased first.
 * Pseudo Code:
 * Step 2: Check the offset range is valid
 * Step 4: While there is more to be programmed
 * Step 6: Program the next word
 * Step 7: Follow Data Toggle Flow Chart until Program/Erase Controller has
 * completed
 * Step 8: Return to Read mode (if an error occurred)
 * Step 9: Update pointers
 * 
 * Arguments: dwOff is the word offset into the flash to be programmed
 * dwNum holds the number of BYTEs/WORDs in the array.
 * Array is a pointer to the array to be programmed.
 * 
 * Return Value: The function returns the following conditions:
 * FLASH_SUCCESS (-1)
 * FLASH_PROGRAM_FAIL (-6)
 * FLASH_OFFSET_OUT_OF_RANGE (-7)
 * On success the function returns FLASH_SUCCESS (-1).
 * The function returns FLASH_PROGRAM_FAIL (-6) if a programming failure occurs.
 * If the address range to be programmed exceeds the address range of the Flash
 * Device the function returns FLASH_OFFSET_OUT_OF_RANGE (-7) and nothing is
 * programmed.
*******************************************************************************/
int FlashProgram( DWORD dwOff, DWORD dwNum, void *Array )
{
#if (_FLASH_TYPE_ == FLASH_x8x16_16BIT_BUS)
    WORD *ArrayPointer; /* Use an WORD to access the array */
#else if (_FLASH_TYPE_ == FLASH_x8x16_8BIT_BUS)||(_FLASH_TYPE_ == FLASH_x8_8BIT_BUS)
    BYTE *ArrayPointer; /* Use an BYTE to access the array */
#endif

    DWORD dwLastOff; /* Holds the last offset to be programmed */
    BYTE bCurBlock; /* Range Variable to track current block */

    /* Step 2: Check the offset and range are valid */
    dwLastOff = dwOff+dwNum-1;
    if( dwLastOff >= FLASH_SIZE )
    {
        return FLASH_OFFSET_OUT_OF_RANGE;
    }

    /* Step 4: While there is more to be programmed */
#if (_FLASH_TYPE_ == FLASH_x8x16_16BIT_BUS)
    ArrayPointer = (WORD *)Array;
#else if (_FLASH_TYPE_ == FLASH_x8x16_8BIT_BUS)||(_FLASH_TYPE_ == FLASH_x8_8BIT_BUS)
    ArrayPointer = (BYTE *)Array;
#endif

    while( dwOff <= dwLastOff )
    {
        /* Step 6: Program the next word */
#if (_FLASH_TYPE_ == FLASH_x8x16_16BIT_BUS)||(_FLASH_TYPE_ == FLASH_x8_8BIT_BUS)
        FlashWrite( 0x5555L, 0x00AA ); /* 1st cycle */
        FlashWrite( 0x2AAAL, 0x0055 ); /* 2nd cycle */
        FlashWrite( 0x5555L, 0x00A0 ); /* Program command */
        FlashWrite( dwOff, *ArrayPointer ); /* Program value */
#else if (_FLASH_TYPE_ == FLASH_x8x16_8BIT_BUS)
        FlashWrite( 0xAAAAL, 0xAA ); /* 1st cycle */
        FlashWrite( 0x5555L, 0x55 ); /* 2nd cycle */
        FlashWrite( 0xAAAAL, 0xA0 ); /* Program command */
        FlashWrite( dwOff, *ArrayPointer ); /* Program value */
#endif

        FlashPause( 0 );//?need?
        /* Step 7: Follow Data Toggle Flow Chart until Program/Erase Controller
        has completed */
        /* See Data Toggle Flow Chart of the Data Sheet */
        if( FlashDataToggle(100000) != FLASH_SUCCESS )//system out limit is 1s
        {
            /* Step 8: Return to Read mode (if an error occurred) */
            FlashReadReset();
            return FLASH_PROGRAM_FAIL;
        }
        /* Step 9: Update pointers */
        dwOff++;
        ArrayPointer++;
        FlashPause( 0 );//?need?
        //FlashWrite( ANY_ADDR, 0xF0 ); /* Use single instruction cycle method */
   }
    //EPRINTF("*FlashProgram return success.**************\n");
    return FLASH_SUCCESS;
}

/*******************************************************************************
 * Function: char *FlashErrorStr( int iErrNum );
 * 
 * Description: This function is used to generate a text string describing the
 * error from the flash. Call with the return value from another flash routine.
 * Pseudo Code:
 * Step 1: Check the error message range.
 * Step 2: Return the correct string.
 *
 * Arguments: iErrNum is the error number returned from another Flash Routine
 * 
 * Return Value: A pointer to a string with the error message
*******************************************************************************/
#if 0	//mark 40323
const char *FlashErrorStr( int iErrNum )
{
    static const char *str[] =
        {
            "Flash Success\n",
            "Flash Poll Failure\n",
            "Flash Too Many Blocks\n",
            "MPU is too slow to erase all the blocks\n",
            "Flash Block selected is invalid\n",
            "Flash Program Failure\n",
            "Flash Address Offset Out Of Range\n",
            "Flash is of Wrong Type\n",
            "Flash Block Failed Erase\n",
            "Flash is Unprotected\n",
            "Flash is Protected\n",
            "Flash function not supported\n",
            "Flash Vpp Invalid\n",
            "Flash Erase Fail\n",
            "Flash Toggle Flow Chart Failure\n",
            //Dickma add for system time out, 040304
            "Flash System Time Out\n",
            "Unknown Error\n"
        };

    /* Step 1: Check the error message range */
    iErrNum = -iErrNum - 1; /* All errors are negative: make +ve & adjust */
    if((iErrNum < 0) || (iErrNum >15)) /* Check range */
        return str[16];
    /* Step 2: Return the correct string */
    else
        return str[iErrNum];
}
#endif 

/*******************************************************************************
List of Errors and Return values, Explanations and Help.
********************************************************************************
Return Name: Flash_SUCCESS
Return Value: -1
Description: This value indicates that the flash command has executed
correctly.
********************************************************************************
Error Name: Flash_POLL_FAIL
Notes: The Data Polling Flow Chart, which applies to M29 series Flash
only, is not used in this library. The function FlashDataPoll() is only
provided as an illustration of the Data Polling Flow Chart. This error
condition should not occur when using this library.
Return Value: -2
Description: The Program/Erase Controller algorithm has not managed to complete
the command operation successfully. This may be because the device is damaged
Solution: Try the command again. If it fails a second time then it is
likely that the device will need to be replaced.
********************************************************************************
Error Name: Flash_TOO_MANY_BLOCKS
Notes: Applies to M29 series Flash only.
Return Value: -3
Description: The user has chosen to erase more blocks than the device has.
This may be because the array of blocks to erase contains the same block
more than once.
Solutions: Check that the program is trying to erase valid blocks. The device
will only have NUM_BLOCKS blocks (defined at the top of the file). Also check
that the same block has not been added twice or more to the array.
********************************************************************************
Error Name: Flash_MPU_TOO_SLOW
Notes: Applies to M29 series Flash only.
Return Value: -4
Description: The MPU has not managed to write all of the selected blocks to the
device before the timeout period expired. See BLOCK ERASE COMMAND
section of the Data Sheet for details.
Solutions: If this occurs occasionally then it may be because an interrupt is
occuring between writing the blocks to be erased. Search for “DSI!” in
the code and disable interrupts during the time critical sections.
If this error condition always occurs then it may be time for a faster
microprocessor, a better optimising C compiler or, worse still, learn
assembly. The immediate solution is to only erase one block at a time.
Disable the test (by #define’ing out the code) and always call the function
with one block at a time.
********************************************************************************
Error Name: Flash_BLOCK_INVALID
Return Value: -5
Description: A request for an invalid block has been made. Valid blocks number
from 0 to NUM_BLOCKS-1.
Solution: Check that the block is in the valid range.
********************************************************************************
Error Name: Flash_PROGRAM_FAIL
Return Value: -6
Description: The programmed value has not been programmed correctly.
Solutions: Make sure that the block containing the value was erased before
programming. Try erasing the block and re-programming the value. If it fails
again then the device may need to be changed.
********************************************************************************
Error Name: Flash_OFFSET_OUT_OF_RANGE
Return Value: -7
Description: The address offset given is out of the range of the device.
Solution: Check that the address offset is in the valid range.
********************************************************************************
Error Name: Flash_WRONG_TYPE
Return Value: -8
Description: The source code has been used to access the wrong type of flash.
Solutions: Use a different flash chip with the target hardware or contact
STMicroelectronics for a different source code library.
********************************************************************************
Error Name: Flash_BLOCK_FAILED_ERASE
Return Value: -9
Description: The previous erase to this block has not managed to successfully
erase the block.
Solution: Sadly the flash needs replacing.
********************************************************************************
Return Name: Flash_UNPROTECTED
Notes: Applies to some M29 series Flash only. This condition should not
occur when using this library.
Return Value: -10
Description: The user has requested to unprotect a flash that is already
unprotected or the user has requested to re-protect a flash that has no
protected blocks. This is just a warning to the user that their operation
did not make any changes and was not necessary.
********************************************************************************
Return Name: Flash_PROTECTED
Notes: This condition should not occur when using this library.
Return Value: -11
Description: The user has requested to protect a flash that is already
protected. This is just a warning to the user that their operation did
not make any changes and was not necessary.
********************************************************************************
Return Name: Flash_FUNCTION_NOT_SUPPORTED
Notes: This condition should not occur when using this library.
Return Value: -12
Description: The user has attempted to make use of functionality not
available on this flash device (and thus not provided by the software
drivers). This is simply a warning to the user.
********************************************************************************
Error Name: Flash_ERASE_FAIL
Return Value: -14
Description: This indicates that the previous erasure of one block, many blocks
or of the whole device has failed.
Solution: Investigate this failure further by attempting to erase each block
individually. If erasing a single block still causes failure, then the Flash
sadly needs replacing.
*******************************************************************************
Error Name: Flash_TOGGLE_FAIL
Return Value: -15
Notes: This applies to M29 series Flash only.
Description: The Program/Erase Controller algorithm has not managed to complete
the command operation successfully. This may be because the device is damaged.
Solution: Try the command again. If it fails a second time then it is
likely that the device will need to be replaced.
*******************************************************************************
Error Name: Flash_SYSTEM_TIMEOUT
Return Value: -16
Notes: This applies to M29 series Flash only.
Description: The Toggle algorithm did not finish within specified time. 
Solution: Try the command again. If it fails a second time then it is
likely that the device will need to be replaced.
*******************************************************************************/

⌨️ 快捷键说明

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