📄 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等,对每种类型定义作简要说明,如果是重要数据结构,要详细 说明 *//*============================================================================== 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==============================================================================*//* 静态变量定义,并对其进行简要说明 */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 + -