?? flashmem.c
字號:
/*==============================================================================** FILENAME: FLASHMEM.C** DESCRIPTION: Source Code of Am29LV64xD Flash Memory Device Driver** CURRENT VERSION: 1.0** HISTORY:** Date CR No Person Description* ---------- ------------ ------ -------------* 2004-07-22 Li Lan Initial version.*==============================================================================*//*============================================================================== Includes and Variable Definitions==============================================================================*//* 包含變量定義文件 */#include "drv/mem/FlashMem.h"/*============================================================================== Include Files==============================================================================*//* 要包含的其他文件 *//*============================================================================== Constant / Defines==============================================================================*//* 常量定義,并對常量定義進行簡要說明 *//*============================================================================== Type Declarations==============================================================================*//* 類型定義,struct、enum等,對每種類型定義作簡要說明,如果是重要數(shù)據(jù)結(jié)構(gòu),要詳細 說明 *//*============================================================================== Global Constant Definitions==============================================================================*//* 全局常量定義,并對其進行簡要說明 */#ifdef FLASH_SIZE_WRITEABLE #define FLASH_MEM_SIZE FLASH_SIZE_WRITEABLE#else #define FLASH_MEM_SIZE FLASH_SIZE#endif /* FLASH_SIZE_WRITEABLE *//*============================================================================== Global Variable Definitions==============================================================================*//* 全局變量定義,并對其進行簡要說明 */char gFlashType[16];/*============================================================================== Local Object Definitions==============================================================================*//* 局部對象定義,并對其進行簡要說明 *//*============================================================================== Static Variable Definitions==============================================================================*//* 靜態(tài)變量定義,并對其進行簡要說明 */LOCAL flash_t sysFlash = {0};/*============================================================================== Forward Declarations==============================================================================*//* 前置聲明 */#ifdef __STDC__LOCAL INT32 sysGetFlashStatus(UINT16* addr_ptr);LOCAL STATUS sysFlashTypeGet(void *flash_addr);#else /* __STDC__ */LOCAL INT32 sysGetFlashStatus();LOCAL STATUS sysFlashTypeGet();#endif /* __STDC__ *//*============================================================================== Function Definitions==============================================================================*//*------------------------------------------------------------------------------** Function Name: sysFlashGet** Input(s):* UINT8* str_ptr: where to copy flash memory* UINT32 strLen: maximum number of bytes to copy* UINT32 offset: byte offset into flash memory** Output(s):* <none>** Returns:* OK, or ERROR if access is outside the flash memory range.** Description:* This routine copies the contents of flash memory into a specified* string.* If multiple tasks are calling sysFlashSet() and sysFlashGet(),* they should use a semaphore to ensure mutually exclusive access.*------------------------------------------------------------------------------*/STATUS sysFlashGet(UINT8* str_ptr, UINT32 str_len, UINT32 offset){ if (((offset + str_len) > FLASH_MEM_SIZE) || (NULL == str_ptr)) { return(ERROR); } bcopyBytes((char *)(FLASH_ADRS + offset), (char *)str_ptr, str_len); return(OK);}/*------------------------------------------------------------------------------** Function Name: sysGetFlashStatus** Input(s):* UINT8* addr_ptr: flash address(not offset)** Output(s):* <none>** Returns:* AMD_STATUS_READY;* AMD_STATUS_BUSY;* AMD_STATUS_ERSUSP;* AMD_STATUS_TIMEOUT;* AMD_STATUS_ERROR** Description:* This routine utilizes the DQ6, DQ5, and DQ2 polling algorithms.*------------------------------------------------------------------------------*/LOCAL INT32 sysGetFlashStatus(UINT16* addr_ptr){ UINT8 data, result; if(NULL == addr_ptr) { return AMD_STATUS_ERROR; } data = *addr_ptr; /* read data first time */ result = data ^ (*addr_ptr); /* see what toggled */ if (0 == result) { return AMD_STATUS_READY; /* no toggles, nothing's happening */ } if (result & AMD_DQ6) /* DQ6 */ { if (data & AMD_DQ5) /* DQ5 */ { /* return AMD_STATUS_TIMEOUT */; } else { return AMD_STATUS_BUSY; } } /* The second opteration */ data = *addr_ptr; /* read data again */ result = data ^ (*addr_ptr); /* see what toggled */ if (0 == result) { return AMD_STATUS_READY; /* no toggles, nothing's happening */ } if (AMD_DQ2 == result) /* DQ2 */ { return AMD_STATUS_ERSUSP; } if (result & AMD_DQ6) /* DQ6 */ { if (data & AMD_DQ5) /* DQ5 */ { return AMD_STATUS_TIMEOUT; } else { return AMD_STATUS_BUSY; } } return AMD_STATUS_ERROR;}/*------------------------------------------------------------------------------** Function Name: sysFlashErase** Input(s):* UINT32 flash_offset: flash offset address** Output(s):* <none>** Returns:* OK, or ERROR if access is outside the flash memory range.** Description:* This routine clears the contents of a sector of flash memory.*------------------------------------------------------------------------------*/STATUS sysFlashErase(UINT32 flash_offset){ UINT16* flash_ptr; int ps; if (flash_offset >= FLASH_MEM_SIZE) { printf("param: flashOffset error!\n"); return(ERROR); } flash_ptr = (UINT16 *)(FLASH_ADRS + (flash_offset / AMD_FLASH_SECTOR_SIZE) * \ AMD_FLASH_SECTOR_SIZE); ps = intLock(); if (flash_offset < 0x1000000 / 2) { *AMD_REG(0) = AMD_CMD_RESET; /* assume reset device to read mode */ *AMD_REG(0x555) = AMD_UNLOCK_CIRCLE_1; /* unlock 1 */ *AMD_REG(0x2AA) = AMD_UNLOCK_CIRCLE_2; /* unlock 2 */ *AMD_REG(0x555) = AMD_CMD_ERASE; *AMD_REG(0x555) = AMD_UNLOCK_CIRCLE_1; *AMD_REG(0x2AA) = AMD_UNLOCK_CIRCLE_2; } else { *AMD_REG_2(0) = AMD_CMD_RESET; /* assume reset device to read mode */ *AMD_REG_2(0x555) = AMD_UNLOCK_CIRCLE_1; /* unlock 1 */ *AMD_REG_2(0x2AA) = AMD_UNLOCK_CIRCLE_2; /* unlock 2 */ *AMD_REG_2(0x555) = AMD_CMD_ERASE; *AMD_REG_2(0x555) = AMD_UNLOCK_CIRCLE_1; *AMD_REG_2(0x2AA) = AMD_UNLOCK_CIRCLE_2; } *AMD_REG((flash_offset / AMD_FLASH_SECTOR_SIZE) * AMD_FLASH_SECTOR_SIZE / 2) = AMD_CMD_SECTOR_ERASE; intUnlock(ps); while (sysGetFlashStatus(flash_ptr) == AMD_STATUS_BUSY) { ; } if (flash_offset < 0x1000000 / 2) { *AMD_REG(0) = AMD_CMD_RESET; /* assume reset device to read mode */ } else { *AMD_REG_2(0) = AMD_CMD_RESET; /* assume reset device to read mode */ } CACHE_PIPE_FLUSH(); return(OK);}/*------------------------------------------------------------------------------** Function Name: sysFlashAllErase** Input(s):* <none>** Output(s):* <none>** Returns:* OK.** Description:* This routine clears the contents of all sectors of flash memory.*------------------------------------------------------------------------------*/STATUS sysFlashAllErase(void){ UINT16* flash_ptr; int ps; flash_ptr = (UINT16 *)(FLASH_ADRS); ps = intLock(); *AMD_REG(0) = AMD_CMD_RESET; /* assume reset device to read mode */ *AMD_REG(0x555) = AMD_UNLOCK_CIRCLE_1; /* unlock 1 */ *AMD_REG(0x2AA) = AMD_UNLOCK_CIRCLE_2; /* unlock 2 */ *AMD_REG(0x555) = AMD_CMD_ERASE; *AMD_REG(0x555) = AMD_UNLOCK_CIRCLE_1; *AMD_REG(0x2AA) = AMD_UNLOCK_CIRCLE_2; *AMD_REG(0x555) = AMD_CMD_CHIP_ERASE; intUnlock(ps); while (sysGetFlashStatus(flash_ptr) == AMD_STATUS_BUSY) { ; } *AMD_REG(0) = AMD_CMD_RESET; /* assume reset device to read mode */#ifdef MCB_FLASH_16M flash_ptr = (UINT16 *)(FLASH_ADRS + AMD_FLASH_SECTOR_SIZE * AMD_FLASH_MAX_SECTOR); ps = intLock(); *AMD_REG_2(0) = AMD_CMD_RESET; /* assume reset device to read mode */ *AMD_REG_2(0x555) = AMD_UNLOCK_CIRCLE_1; /* unlock 1 */ *AMD_REG_2(0x2AA) = AMD_UNLOCK_CIRCLE_2; /* unlock 2 */ *AMD_REG_2(0x555) = AMD_CMD_ERASE; *AMD_REG_2(0x555) = AMD_UNLOCK_CIRCLE_1; *AMD_REG_2(0x2AA) = AMD_UNLOCK_CIRCLE_2; *AMD_REG_2(0x555) = AMD_CMD_CHIP_ERASE; intUnlock(ps); while (sysGetFlashStatus(flash_ptr) == AMD_STATUS_BUSY) { ;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -