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

📄 main.cpp

📁 用于上位机对德州仪器的DSP的flash烧写
💻 CPP
📖 第 1 页 / 共 2 页
字号:
				
			BurnBootloader();		
		}
		
		if(selection == 2 || selection == 3 || selection == 8 || selection == 9 || selection == 10 || selection == 11)
		{
			//烧写升级程序
			EraseUpdate();
				
			i = 0;
			while(i < 500000)
			{
				i ++;
			}		
				
			BurnUpdate(updatedir);
		}
		
		if(selection == 4 || selection == 5 || selection == 8 || selection == 9 || selection == 10 || selection == 11)
		{	
			//烧写应用程序										
			EraseProgramm();
				
			i = 0;
			while(i < 500000)
			{
				i ++;
			}		
								
			BurnProgramm(programdir);
		}
		
		if(selection == 6 || selection == 7 || selection == 8 || selection == 9)
		{	
			//烧写初始化配置		
			EraseConfiguration();
		
			i = 0;
			while(i < 500000)
			{
				i ++;
			}
			
			BurnConfiguration(&configuration);		
		}
				
		do
		{
			printf("\n");
			printf("*************************************************\n");
			printf("*Burn again?                                    *\n");
			printf("*\ty-Yes                                  *\n");
			printf("*\tn-No                                   *\n");
			printf("*************************************************\n");
			gets(text);
			if(text[0] == 'y')
				IsContinue = 1;
			else if(text[0] == 'n')
				IsContinue = 0;
			else
				printf("No such selection. Please input a valid character.\n");
		}while(text[0] != 'y' && text[0] != 'n');	
	}while(IsContinue);
	printf("All done\n");
}

static int BurnBootloader()
{
	int i = 0;
	unsigned short* p = NULL;
	
	unsigned char* pSrc = (unsigned char*)0x0;
	
	unsigned short* pDst = (unsigned short*)malloc(0x400);
	
	if(pDst == NULL)
	{
		printf("Memory alloc  failed\n");
		return -1;
	}	
	p = pDst;
	for(i = 0;i < 0x200;i++)
		*p++ = *pSrc++ |0xff00;
		
	printf("Writing bootloader ...\n");
	if(Phocus1820_flash_write(0x90000000, (unsigned int)pDst, 0x400) != 0)
	{
		printf("Write flash failed\n");
		free(pDst);
		return -1;
	}
	
	printf("Write bootloader OK\n");
	printf("\n");

	free(pDst);
	return 0;
}

static int BurnProgramm(char* pFileName)
{
	unsigned int count = 0;
	char* p = NULL;
	FILE* fp = NULL;
	unsigned char rc = 0;

	fp = fopen(pFileName,"rb");
	if(fp == 0)
	{
		printf("Open source file failed\n");
		return 1;
	}
	
	fseek(fp, 0, SEEK_END);
	count = ftell(fp);
	fseek(fp, 0, SEEK_SET);
	
	p = (char*)malloc(count);
	if(p == NULL)
	{
		printf("Memory alloc failed\n");
		rc = 1;
		goto main_exit;
	}
	printf("Start to read file from \"%s\"\n", pFileName);
	if(fread(p, count, 1, fp) != 1)
	{
		printf("Read source file failed\n");
		rc = 1;		
		goto main_exit;		
	}

	printf("Writing program ...\n");
	if(Phocus1820_flash_write(0x90080000, (unsigned int)p, count) != 0)
	{
		printf("Write flash failed\n");
		rc = 1;
		goto main_exit;
	}
	printf("Write programm OK\n");
	printf("Total write %d bytes\n", count);
	
main_exit:
	if(fp != NULL)
		fclose(fp);
	if(p != NULL)
		free(p);
	printf("\n");	
	return rc;
}

static int BurnUpdate(char* pFileName)
{
	unsigned int count = 0;
	char* p = NULL;
	FILE* fp = NULL;
	unsigned char rc = 0;

	
	fp = fopen(pFileName,"rb");
	if(fp == 0)
	{
		printf("Open update file failed\n");
		return 1;
	}
	
	fseek(fp, 0, SEEK_END);
	count = ftell(fp);
	fseek(fp, 0, SEEK_SET);
	
	p = (char*)malloc(count);
	if(p == NULL)
	{
		printf("Memory alloc failed\n");
		rc = 1;
		goto main_exit;
	}
	//printf("Start to read update file from pc...\n");
	printf("Start to read file from \"%s\"\n", pFileName);
	if(fread(p, count, 1, fp) != 1)
	{
		printf("Read update file failed\n");
		rc = 1;		
		goto main_exit;		
	}

	printf("Writing update program ...\n");
	if(Phocus1820_flash_write(0x90002000, (unsigned int)p, count) != 0)
	{
		printf("Write flash failed\n");
		rc = 1;
		goto main_exit;
	}
	printf("Write update programm OK\n");
	printf("Total write %d bytes\n", count);
	
main_exit:
	if(fp != NULL)
		fclose(fp);
	if(p != NULL)
		free(p);
	printf("\n");	
	return rc;
}


static int EraseProgramm()
{
	printf("Start to erase programm ...\n");
	if(Phocus1820_flash_erase(0x90080000, 0x400000) != 0)
	{
		printf("Erase programm failed\n");
		return -1;
	}
	printf("Erase programm OK\n");
	return 0;
	
}

static int EraseBootloader()
{
	printf("Start to erase bootloader ...\n");
	if(Phocus1820_flash_erase(0x90000000, 0x1000) != 0)
	{
		printf("Erase bootloader failed\n");
		return -1;
	}
	printf("Erase bootloader OK\n");
	return 0;
}

static int EraseUpdate()
{
	printf("Start to erase update ...\n");
	if(Phocus1820_flash_erase(0x90002000, 0x7d000) != 0)
	{
		printf("Erase update failed\n");
		return -1;
	}
	printf("Erase update OK\n");
	return 0;
}

static int EraseConfiguration()
{
	printf("Start to erase configuration ...\n");
	
	if(Phocus1820_flash_erase(0x90001000, 0x1000) != 0)
	{
		printf("Erase configuration failed\n");
		return -1;
	}
	
	if(Phocus1820_flash_erase(0x9007f000, 0x1000) != 0)
	{
		printf("Erase configuration failed\n");
		return -1;
	}	
	
	printf("Erase configuration OK\n");
	return 0;
}

static int BurnConfiguration(CONFIGURATION *pCfg)
{
	unsigned char rc = 0;
	char *p;
	
	p = (char*)malloc(0x2000);
	
	if(p == NULL)
	{
		printf("Memory alloc failed\n");
		rc = 1;
		goto main_exit;
	}
	
	memset(p, 0xff, 0x2000);
	
	memcpy(p, pCfg, sizeof(CFG_AREA_HEADER));
	memcpy(p + sizeof(CFG_AREA_HEADER) * 2, (char *)&(pCfg->sp232_info), 2 * sizeof(unsigned int));
	memcpy(p + sizeof(CFG_AREA_HEADER) + 0x400, (char *)&(pCfg->eth_info), sizeof(NET_PARAM));
	((CFG_AREA_HEADER *)p)->nCheckSum = CheckSum(p, 0x1000);
	
	memcpy(p + 0x1000, pCfg, sizeof(CFG_AREA_HEADER));
	memcpy(p + 0x1000 + sizeof(CFG_AREA_HEADER), (char *)&(pCfg->mode_info), sizeof(MODE_INFO));
	((CFG_AREA_HEADER *)p)->nCheckSum = CheckSum(p + 0x1000, 0x1000);	

	printf("Writing configuration ...\n");
	if(Phocus1820_flash_write(0x90001000, (unsigned int)p, 0x1000) != 0)
	{
		printf("Write flash failed\n");
		rc = 1;
		goto main_exit;
	}
	if(Phocus1820_flash_write(0x9007f000, (unsigned int)(p + 0x1000), 0x1000) != 0)
	{
		printf("Write flash failed\n");
		rc = 1;
		goto main_exit;
	}	
	printf("Write configuration OK\n");
	printf("\n");
	
	printf("IP address is %s .\n", pCfg->eth_info.strLocalIPAddr);
	printf("Mac is %02x-%02x-%02x-%02x-%02x-%02x\n", pCfg->eth_info.bMacAddr[0], pCfg->eth_info.bMacAddr[1], pCfg->eth_info.bMacAddr[2], pCfg->eth_info.bMacAddr[3], pCfg->eth_info.bMacAddr[4], pCfg->eth_info.bMacAddr[5]);
	printf("\n");
main_exit:
	return rc;	
}

static int ReadEthInfo(NET_PARAM *eth_info)
{
	char p[0x1000];
	if(Phocus1820_flash_read((unsigned int)p, 0x90001000, 0x1000) != 0)
	{
		printf("Read flash failed\n");
		return 1;
	}
	memcpy((char *)eth_info, p + sizeof(CFG_AREA_HEADER) + 0x400, sizeof(NET_PARAM));
	return 0;
}

static char CheckSum(char* pData, int nLen)
{
	unsigned short cksum = 0;
	while(nLen > 0)
	{
		cksum += *pData++;
		nLen--;
	}
	cksum = (cksum >> 8) + (cksum & 0xff);
	cksum += (cksum >> 8);
	return (unsigned char)(~cksum);
}

⌨️ 快捷键说明

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