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

📄 fmwu_common.c

📁 OKI DEMO FLASH WRITE PROGRAM
💻 C
字号:
/****************************************************************************/
/*                                                                          */
/*    Copyright (C) 2006 Oki Electric Industry Co., LTD.                    */
/*                                                                          */
/*    System Name    :  ML675050                                            */
/*    Module Name    :  Flash Memory Write Utility flash module             */
/*    File   Name    :  fmwu_common.c                                       */
/*    Revision       :  01.00                                               */
/*    Date           :  2006/03/20                                          */
/*                                                                          */
/****************************************************************************/
#include "fmwu_common.h"
#include "flash_memory.h"
#include "get_data.h"

#define BUFF_SIZE   (0x00000400)
#define WIDTH16BIT  (0x28)
#define BWC         0x78100000  /* Bus Width Control register */
#define FILE_NAME   "flash_data.dat"

/* functions */
static int FlashWriteMain(void);
static int FlashWrite(void);
static int DataCmp(unsigned char *, unsigned long , unsigned long);
static int ChipErase(void);
static void PrintStartMsg(void);
static void PrintEndMsg(int err_code);
static int GetWriteSize(void);
static int WriteSizeCheck(void);
static int ProgramEndProc(int);
const char *ltoa_hex(unsigned long, int);

#ifdef _EMBEDDED_FLASH	/* Internal Flash Memory */
static int Inquiry(int);
static int SecurityCheck(void);
#endif

/* global functions */
void C_Entry(void);

enum{
	NORMAL    = 0,
	FILE_OPEN_ERR,
	DATA_SIZE_ERR,
	OV_FLASH_SIZE_ERR,
	CHIP_ERASE_ERR,
	DATA_READ_ERR,
	FLASH_WRITE_ERR,
	DATA_CMP_ERR,
	FILE_CLOSE_ERR,
	SECURITY_SET_ERR
};


/*****  Work Area Definition  *****/
static UWORD DataSize = 0;
static UWORD ErrAddress = 0;
static UWORD ErrSrcData = 0;
static UWORD ErrDstData = 0;

static unsigned char write_data_buff[BUFF_SIZE];

char str_buff[11];

/********************************************************************/
/*                  Flash Memory Write Proc MAIN                    */
/********************************************************************/
void C_Entry(void)
{
	int err_code;
	
	PrintStartMsg();
	err_code = FlashWriteMain();
	
	if(err_code != OK)
		err_code = ProgramEndProc(err_code);
		
	PrintEndMsg(err_code);
}

/********************************************************************/
/*                  Flash Write Main Proc                           */
/********************************************************************/
static int FlashWriteMain(void)
{
	int errchk = NORMAL;
	
#ifndef _EMBEDDED_FLASH			/* External Flash Memory */
	out_w(BWC, WIDTH16BIT);/* External ROM bus 16 bit width */
#endif

	if(gdFileOpen(FILE_NAME) != OK)
	{
		return FILE_OPEN_ERR;
	}
	if(GetWriteSize() != OK)
	{
		return DATA_SIZE_ERR;
	}
	if(WriteSizeCheck() != OK)
	{
		return OV_FLASH_SIZE_ERR;
	}
	if(ChipErase() != OK)
	{
		return CHIP_ERASE_ERR;
	}
	errchk = FlashWrite();
	if(errchk != NORMAL)
	{
		return errchk;
	}
	if(gdFileClose() != OK)
	{
		return FILE_CLOSE_ERR;
	}
	
#ifdef _EMBEDDED_FLASH	/* Internal Flash Memory */
	if(Inquiry(PROTECT_SET) == YES)
	{
		if(fmSetSecurity() != OK)
		{
			return SECURITY_SET_ERR;
		}
		if(SecurityCheck() != OK)
		{
			return SECURITY_SET_ERR;
		}
	}
#endif
	return OK;
}

#ifdef _EMBEDDED_FLASH	/* Internal Flash Memory */
/********************************************************************/
/*                  Security Check Proc                             */
/********************************************************************/
static int SecurityCheck(void)
{
	if(fmSecurityCheck() == NOSECURITY)
	{
		return ERROR;
	}
    Write0("Security bit is set.\n\n");
	return OK;
}
#endif

/********************************************************************/
/*                  Write Data Compare Proc                         */
/********************************************************************/
static int DataCmp(unsigned char *src_addr, unsigned long dst_offset, unsigned long cmp_size)
{
    unsigned long flash_base;
    UWORD    i;
    unsigned short dst_value, src_value;

    flash_base = fmGetFlashStart();

    for(i=0; i<cmp_size; i+=2)
    {
        dst_value = in_hw(flash_base +dst_offset +i);
        src_value = in_hw((UWORD)src_addr +i);
        if(src_value != dst_value){            /* Compare Error? */
            ErrAddress = dst_offset;
            ErrSrcData = src_value;
            ErrDstData = dst_value;
            return ERROR;
        }
    }

    return OK;
}

/********************************************************************/
/*                  Flash Memory Control Proc                       */
/********************************************************************/
static int FlashWrite(void)
{
	UWORD total_size;
	UWORD read_size, write_size;
	int state = NORMAL;

    Write0("Flash Memory Writing  ... ");

	for(total_size=0; total_size < DataSize; total_size+=read_size)
	{
		read_size = DataSize - total_size > BUFF_SIZE ? BUFF_SIZE : DataSize - total_size;
		if(gdDataRead(write_data_buff, read_size) != read_size)
		{
			state = DATA_READ_ERR;
			break;
		}
		write_size = fmDataWrite(write_data_buff, total_size, read_size);
		
#ifdef _EMBEDDED_FLASH
		if(write_size - read_size > 3)
		{
			state = FLASH_WRITE_ERR;
			break;
		}
#else
		if((write_size != read_size) && (write_size != read_size+1))
		{
			state = FLASH_WRITE_ERR;
			break;
		}
#endif
		if(DataCmp(write_data_buff, total_size, write_size) != OK)
		{
			state = DATA_CMP_ERR;
			break;
		}
	}

	if(state != NORMAL)
	{
		Write0("FAILED!\n\n");
		return state;
	}
	else
	{
		Write0("OK!\n\n");
		return NORMAL;
	}
}

/********************************************************************/
/*                  Flash Memory Erase Proc                         */
/********************************************************************/
static int ChipErase(void)
{
	int state = OK;

    /* Flash Memory All Erase */
    Write0("Flash Memory Erasing  ... ");
    state = fmChipErase();
    if(state == OK) {
        Write0("OK!\n");
    } else {
        Write0("FAILED!\n\n");
    }

    return (state);
}

/********************************************************************/
/*                  Get Flash Write Size Proc                       */
/********************************************************************/
static int GetWriteSize(void)
{
	Write0("Write Data Size Check ... ");

	DataSize = (UWORD)gdGetDataSize();
	if(DataSize==0xffffffff)
	{
		Write0("FAILED!\n\n");
		return ERROR;
	}
	
	return OK;
}

/********************************************************************/
/*                  Flash Write Size Check Proc                     */
/********************************************************************/
static int WriteSizeCheck(void)
{
	if(DataSize > fmGetFlashSize())
	{
		Write0("FAILED!\n");
		Write0("   Data Size = 0x");
		Write0(ltoa_hex(DataSize, 8));
		Write0(" byte\n\n");
		Write0("   File Size Is Too Large.\n");
		Write0("   Flash Memory Size Is 0x");
		Write0(ltoa_hex(fmGetFlashSize(), 8));
		Write0("byte.\n");
		Write0("\n");

		return ERROR;
	}

	Write0("OK!\n");
	Write0("   Data Size = 0x");
	Write0(ltoa_hex(DataSize, 8));
	Write0(" byte\n\n");

	return OK;
}

/********************************************************************/
/*                  Long to hex char                                */
/********************************************************************/
const char *ltoa_hex(unsigned long i, int len)
{
    char tmp;

    str_buff[len] = '\0';
    for(len--; len>=0; len--){
        tmp = (char)(i & 0xF);
        str_buff[len] = tmp + (tmp<10 ? '0' : ('A'-10));
        i >>= 4;
    }

    return str_buff;
}

/********************************************************************/
/*                  Print Start Message Proc                        */
/********************************************************************/
static void PrintStartMsg(void)
{
	Write0("==============================================================\n");
	Write0("  Flash Memory Write Utility for ML675050                     \n");
	Write0("                                                              \n");
	Write0("        Copyright 2006, Oki Electric Industry Co.,Ltd.        \n");
	Write0("==============================================================\n\n\n");
	return ;
}

/********************************************************************/
/*                  Flash Memory Utility END Proc                   */
/********************************************************************/
static int ProgramEndProc(int return_err_code)
{
	if(return_err_code==FILE_OPEN_ERR)
		return return_err_code;
	if(return_err_code==SECURITY_SET_ERR)
		return return_err_code;
	if(return_err_code==FILE_CLOSE_ERR)
		return return_err_code;
	
	if(gdFileClose() != OK)
	{
		Write0("Error Occurred (Error Code = 0x");
		Write0(ltoa_hex((UWORD)return_err_code, 2));
		Write0(")\n");
		return_err_code = FILE_CLOSE_ERR;
	}
	
	return return_err_code;
}

/********************************************************************/
/*                  Print End Message Proc                          */
/********************************************************************/
static void PrintEndMsg(int err_code)
{
	switch(err_code){
		case OK:
			break;
		default:
			Write0("Error Occurred (Error Code = 0x");
			Write0(ltoa_hex((UWORD)err_code, 2));
			Write0(")\n\n");
			break;
	}
	Write0("Flash memory write finished.\n\n");

	return;
}
#ifdef _EMBEDDED_FLASH	/* Internal Flash Memory */
/********************************************************************/
/*                  inquiry control                                 */
/********************************************************************/
static int Inquiry(int flags)
{
    unsigned char data[] = " \0";

	if(flags == PROTECT_SET)
	{    /* Protect Set Request */
		Write0("Do you wish security is set?(y/n):");
		
		do{
			data[0] = get_char();
		}while(((data[0] & 0xdf) != 'Y') && ((data[0] & 0xdf) != 'N'));
		
		Write0(data);
		if((data[0] == 'Y') || (data[0] == 'y'))
		{
			data[0] = 0;
			Write0("\r\n\r\n");
			Write0("*********************************************************************************\n");
			Write0("*  CAUTION!!                                                                    *\n");
			Write0("*  If the security bit is set, you cannot connect your debugger to the MCU.     *\n");
			Write0("*********************************************************************************\n");
			Write0("Are you going to connect your debugger to the MCU after this?(y/n):");
			do{
				data[0] = get_char();
			}while(((data[0] & 0xdf) != 'Y') && ((data[0] & 0xdf) != 'N'));
			
			Write0(data);
			if((data[0] == 'N') || (data[0] == 'n'))
			{
				Write0("\r\n\r\n");
				return YES;
			}
		}
		Write0("\r\n\r\n");
	}
	return NO;
}
#endif

⌨️ 快捷键说明

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