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

📄 flashmem.c

📁 Am29LV64xD Flash Memory Device Driver
💻 C
📖 第 1 页 / 共 2 页
字号:
	}	*AMD_REG_2(0) = AMD_CMD_RESET;                /* assume reset device to read mode */#endif	CACHE_PIPE_FLUSH();	return(OK);}/*------------------------------------------------------------------------------**   Function Name: sysFlashWrite**   Input(s):*       UINT16* flash_buf_ptr: source buffer*       UINT32 buffer_size: buffer length*       UINT32 flash_offset: flash offset address**   Output(s):*       <none>**   Returns:*       OK, or ERROR if the write operation fails.**   Description:*       This routine copies specified data of a specified length into a*       specified offset in the flash memory. Data is passed as a string*       if not NULL.*       The parameter <flash_offset> must be appropriately aligned for*       the width of the Flash devices in use.*------------------------------------------------------------------------------*/STATUS sysFlashWrite(UINT16* flash_buf_ptr,					 UINT32 buffer_size,					 UINT32 flash_offset){	UINT16* flash_addr_ptr;	UINT16* tmp_buf_ptr = flash_buf_ptr;	UINT16* flash_ptr;	UINT32 size = 0, stat = 0;	int ps;	if (((FLASH_ADRS + flash_offset) % 2) != 0)	{		return(ERROR);	}	flash_ptr = (UINT16 *)(FLASH_ADRS + (flash_offset / AMD_FLASH_SECTOR_SIZE) * \				AMD_FLASH_SECTOR_SIZE);	flash_addr_ptr = (UINT16 *)(FLASH_ADRS + flash_offset);	if (flash_offset < 0x1000000 / 2)	{		/* Check to see if we're in unlock bypass mode */		*AMD_REG(0) = AMD_CMD_RESET;                    /* reset device to read mode */				for (size = 0; size < buffer_size; size += 2)   /* 16 bit operation*/		{			ps = intLock();			*AMD_REG(0x555) = AMD_UNLOCK_CIRCLE_1;      /* unlock 1 */			*AMD_REG(0x2AA) = AMD_UNLOCK_CIRCLE_2;      /* unlock 2 */			*AMD_REG(0x555) = AMD_CMD_PROGRAM;				*flash_addr_ptr = *tmp_buf_ptr;				intUnlock(ps);				while ((stat = sysGetFlashStatus(flash_ptr)) == AMD_STATUS_BUSY)			{				;			}				if (stat != AMD_STATUS_READY)			{				printf("\t\tIn writing process, exist error!\n");				return(ERROR);			}				flash_addr_ptr++;			tmp_buf_ptr++;		}		*AMD_REG(0) = AMD_CMD_RESET;                    /* reset device to read mode */	}	else	{		/* Check to see if we're in unlock bypass mode */		*AMD_REG_2(0) = AMD_CMD_RESET;                    /* reset device to read mode */				for (size = 0; size < buffer_size; size += 2)   /* 16 bit operation*/		{			ps = intLock();			*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_PROGRAM;				*flash_addr_ptr = *tmp_buf_ptr;				intUnlock(ps);				while ((stat = sysGetFlashStatus(flash_ptr)) == AMD_STATUS_BUSY)			{				;			}				if (stat != AMD_STATUS_READY)			{				printf("\t\tIn writing process, exist error!\n");				return(ERROR);			}				flash_addr_ptr++;			tmp_buf_ptr++;		}		*AMD_REG_2(0) = AMD_CMD_RESET;                    /* reset device to read mode */	}#ifdef FLASH_WRITE_VERIFY	/* Verify data*/	flash_addr_ptr = (UINT16 *)(FLASH_ADRS + flash_offset);	size = buffer_size;	tmp_buf_ptr = flash_buf_ptr;	while (size > 0)	{		if(*flash_addr_ptr != *tmp_buf_ptr)		{			return(ERROR);		}		flash_addr_ptr++;		tmp_buf_ptr++;		size -= 2;	}#endif  /* FLASH_WRITE_VERIFY */	return(OK);}/*------------------------------------------------------------------------------**   Function Name: sysFlashSet**   Input(s):*       UINT8* str_ptr: string to be copied into flash memory*       UINT32 str_len: maximum number of bytes to copy*       UINT32 offset: byte offset into flash memory**   Output(s):*       <none>**   Returns:*       OK, or ERROR if the write fails or the input parameters are*       out of range.**   Description:*       This routine copies a specified string into flash memory after*       calling sysFlashErase() and clearing flash memory.*       If multiple tasks are calling sysFlashSet() and sysFlashGet(),*       they should use a semaphore to ensure mutually exclusive access*       to flash memory.*------------------------------------------------------------------------------*/STATUS sysFlashSet(UINT8* str_ptr, UINT32 str_len, UINT32 offset){	UINT32 block_num = 0;	UINT16* temp_buf_ptr = (UINT16 *)str_ptr;	STATUS ret_val = OK;	if (((offset + str_len) > FLASH_MEM_SIZE) || (NULL == str_ptr))	{		return(ERROR);	}	/* see if contents are actually changing */	if (bcmp((char *)(FLASH_ADRS + offset), (char *)str_ptr, str_len) == 0)	{		return(OK);	}	/* first read existing data */	temp_buf_ptr = (UINT16 *)malloc(AMD_FLASH_SECTOR_SIZE);	if (NULL == temp_buf_ptr)	{		return(ERROR);	}	block_num = offset / AMD_FLASH_SECTOR_SIZE;		if ((offset - block_num * AMD_FLASH_SECTOR_SIZE + str_len) > AMD_FLASH_SECTOR_SIZE)	{		free (temp_buf_ptr);		return(ERROR);	}		bcopyBytes((char *)(FLASH_ADRS + block_num * AMD_FLASH_SECTOR_SIZE), (char *)temp_buf_ptr, \				AMD_FLASH_SECTOR_SIZE);	bcopyBytes((char *)str_ptr, (char *)((UINT32)temp_buf_ptr + (offset - \				block_num * AMD_FLASH_SECTOR_SIZE)), str_len);	if (sysFlashErase(offset) == ERROR) /* erase device */	{		free (temp_buf_ptr);		return(ERROR);	}	ret_val = sysFlashWrite(temp_buf_ptr, AMD_FLASH_SECTOR_SIZE, \				block_num * AMD_FLASH_SECTOR_SIZE);	free (temp_buf_ptr);	return(ret_val);}/***************************************************************************** sysNvRamGet - get the contents of non-volatile RAM** This routine calls sysFlashGet() to copy the contents of flash memory* into a specified string.  The string is terminated with an EOS.** NOTE: This routine uses flash memory, since there is no NVRAM on board.** RETURNS: The return value of sysFlashGet().** SEE ALSO: sysNvRamSet(), sysFlashGet()*/STATUS sysNvRamGet(char *string,    /* where to copy non-volatile RAM    */int strLen,      /* maximum number of bytes to copy   */int offset       /* byte offset into non-volatile RAM */){	STATUS retVal;	offset += NV_BOOT_OFFSET;   /* boot line begins at <offset> = NV_BOOT_OFFSET */	if ((offset < 0) || (strLen < 0) || ((offset + strLen) > NV_RAM_SIZE))	{		return(ERROR);	}	retVal = sysFlashGet(string, strLen, offset + sysNvRamOffset);	string [strLen] = EOS;	return(retVal);}/***************************************************************************** sysNvRamSet - write to non-volatile RAM** This routine calls sysFlashSet() to copy a specified string into* flash memory.** NOTE: This routine uses flash memory, since there is no NVRAM on board.** RETURNS: The return value of sysFlashSet().** SEE ALSO: sysNvRamGet(), sysFlashSet()*/STATUS sysNvRamSet(char *string,     /* string to be copied into non-volatile RAM */int strLen,       /* maximum number of bytes to copy           */int offset        /* byte offset into non-volatile RAM         */){	offset += NV_BOOT_OFFSET;   /* boot line begins at <offset> = 0 */	if ((offset < 0) || (strLen < 0) || ((offset + strLen) > NV_RAM_SIZE))	{		return(ERROR);	}	return(sysFlashSet(string, strLen, offset + sysNvRamOffset));}/***************************************************************************** sysFlashTypeGet - determine the device type of on-board flash memory** This routine uses the `autoselect' command to determine the device type of* on-board flash memory for flash 29LVxxx devices.** RETURNS: An integer indicating the device type of on-board flash memory.*/LOCAL STATUS sysFlashTypeGet (void *flash_addr){	volatile UINT16* pFA = flash_addr; /* flash address */	int ps;	ps = intLock();	*AMD_REG(0x555) = AMD_UNLOCK_CIRCLE_1;      /* unlock 1 */	*AMD_REG(0x2AA) = AMD_UNLOCK_CIRCLE_2;      /* unlock 2 */	*AMD_REG(0x555) = AMD_CMD_AUTOSELECT;	sysFlash.Manufacturer = *pFA++;	sysFlash.Type = *pFA++;	*AMD_REG(0x555) = AMD_CMD_RESET;	/* populate fields of the flash_t structure */	switch (sysFlash.Type)	{	case FLASH_29LV64xD:		sysFlash.nbSectors  = 128;		sysFlash.sectorSize = 0x10000;		sysFlash.Width      = 2;		bcopyBytes("AMD 29LV64xD", (char *)gFlashType, 12);		gFlashType[13] = '\0';		break;	case FLASH_29LV64xM:		sysFlash.nbSectors  = 128;		sysFlash.sectorSize = 0x10000;		sysFlash.Width      = 2;		bcopyBytes("AMD 29LV64xM", (char *)gFlashType, 12);		gFlashType[13] = '\0';		break;	default:		sysFlash.Type = 0;		intUnlock(ps);		return(ERROR);	}	intUnlock(ps);	sysFlash.Size = sysFlash.nbSectors * sysFlash.sectorSize;	return (OK);}

⌨️ 快捷键说明

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