Index: output/create_exen.pl
===================================================================
--- /YC1308AC/branch/mouse_project/SA_Project/SA_152_mouse_public/output/create_exen.pl	(nonexistent)
+++ /YC1308AC/branch/mouse_project/SA_Project/SA_152_mouse_public/output/create_exen.pl	(working copy)
@@ -0,0 +1,274 @@
+
+#input 1: otp size (kb)
+#input 2: flash size (kb)
+#input 3: eeprom size (kb)
+use strict;
+
+my $memmap = "memmap.format";
+my $inputeeprom = "eeprom.dat";
+my $inputeeprom2 = "eeprom.dat";
+my $outputflash = "flash.dat";
+my $outputotp = "otp.dat";
+my @config_type = ("aa\n","55\n","05\n","00\n","ff\n","ff\n","00\n","00\n","00\n","00\n","00\n");
+my @file_end = ("55\n","aa\n","aa\n","55\n"); #crc header
+my $f_storage_size = 0;
+my $f_config_addr = 0;
+my $f_config_type = 0;
+my $i;
+my $crcoffset=0;
+
+if(@ARGV < 3 ) {
+	printf "create  input error!\n";
+	exit;
+}
+
+
+open(MEMMAP, "$memmap") or die "can't open $memmap: $!";
+my @lines = <MEMMAP>;
+my $length = @lines;
+
+my $conlen = @config_type;		
+
+
+for($i=0;$i<$length;$i+=1){
+	my @string = split(' ', $lines[$i]);
+	my $length_s = @string ;
+	if( $string[1] =~ /mem_g24_ota_config_type/ ){
+#		print "$string[1] $string[0] \n";
+#		print @config_type;	
+		$config_type[5] = substr($string[0],2,2)."\n";
+		$config_type[4] = substr($string[0],4,2)."\n";
+#		print $config_type[4];
+#		print $config_type[5];
+#		$f_config_type = hex(substr($string[0],2,4)); 
+		$f_config_type = hex($string[0]); 
+#		print $f_config_type;
+	}	
+
+	if( $string[1] =~ /mem_g24_ota_storage_size/){
+#		print "$string[1] $string[0] \n";
+		$f_storage_size = hex($string[0]); 
+#		print $f_storage_size;
+	}
+	
+	if( $string[1]  =~ /mem_g24_ota_config_addr/){
+#		print "$string[1] $string[0] \n";
+		$f_config_addr = hex($string[0]); 
+#		print $f_config_addr;
+	}	
+}
+close(MEMMAP);
+
+
+if($f_storage_size-$f_config_type ==1 && $f_config_addr- $f_storage_size ==2 )
+{}
+else
+{	die "mem_g24_ota_config_type mem_g24_ota_storage_size  mem_g24_ota_config_addr error $!";}
+
+
+
+open(EEPROM, "$inputeeprom") or die "can't open $inputeeprom: $!";
+my @eeprom = <EEPROM>;
+my $eepromlen = @eeprom; 
+close(EEPROM);
+
+my $count ;
+my $countIn;
+my $searchRes;
+my $existsum=0;
+my $sum = 0;
+my $sum_config_type = 0;
+my $sum_flash ;
+my $sum_otp;
+my $sum_eeprom;
+
+for($i=0;$i<$eepromlen;$i++){
+	if ($searchRes == 0)
+	{ 
+		$sum_config_type = 0;
+		for( $count=0;$count<6;$count++){
+			if ($eeprom[$i+$count] ne $config_type[$count]) #  $config_type[$count]
+			{
+				last;		
+			}
+#			printf  "%x  %x\n",$count,hex($config_type[$count]);
+		}
+		if($count == 6)
+		{
+			$searchRes = 1;
+			$sum_config_type = hex($eeprom[$i+$count]) + hex($eeprom[$i+$count+1]) +
+								hex($eeprom[$i+$count+2]) + hex($eeprom[$i+$count+3]) + hex($eeprom[$i+$count+4]);
+#			printf  "%x yesyes \n",$sum_config_type ;	
+			last;
+		}
+	}	
+}
+
+
+
+
+if($searchRes==1){
+
+	$i += $count;
+	$eeprom[$i+3] =	sprintf "%02x\n",($eepromlen+1 & 0xff);
+	$eeprom[$i+4] = sprintf "%02x\n",($eepromlen+1 >> 8 )& 0xff;
+	
+	
+	my @flash = @eeprom;
+	my @otp = @eeprom;
+	$flash[$i] = "04\n";
+	$otp[$i] = "03\n";
+	$eeprom[$i] = "02\n";
+	$otp[$i+1] =  sprintf "%02x\n",(($ARGV[0]) & 0xff);
+	$otp[$i+2] = sprintf "%02x\n",($ARGV[0]) >> 8 ;	
+	$flash[$i+1] =  sprintf "%02x\n",(($ARGV[1]) & 0xff);
+	$flash[$i+2] = sprintf "%02x\n",($ARGV[1]) >> 8 ;	
+	$eeprom[$i+1] =  sprintf "%02x\n",(($ARGV[2]) & 0xff);
+	$eeprom[$i+2] = sprintf "%02x\n",($ARGV[2]) >> 8 ;	
+
+	
+	
+	my $regLen = @file_end;
+	for( $count=0;$count<$regLen;$count++){
+		if($file_end[ $count] != $eeprom[$eepromlen-4-$regLen+$count])
+		{	last;}
+	}
+	if ($count == $regLen){
+		$existsum=1;
+		$crcoffset=6;
+		$sum = hex($eeprom[$eepromlen-4]) + (hex($eeprom[$eepromlen-3])<<8);
+#		printf  "   %x sum \n",($sum<$sum_config_type);
+		if($sum>=$sum_config_type){
+			$sum -= $sum_config_type;		
+		}else{	
+			$sum = ($sum + 0x10000- $sum_config_type) &0xffff;	
+		}
+	#	($sum>=$sum_config_type)? $sum -= $sum_config_type:$sum -= $sum_config_type; #$sum += 0x10000- $sum_config_type;
+		$sum_flash = $sum + hex($flash[$i])+ hex($flash[$i+1]) + hex($flash[$i+2]) + hex($eeprom[$i+3]) + hex($eeprom[$i+4]) ;
+		$sum_otp =  $sum+ hex($otp[$i]) + hex($otp[$i+1]) + hex($otp[$i+2]) + hex($eeprom[$i+3]) + hex($eeprom[$i+4]) ;
+		$sum_eeprom = $sum+ hex($eeprom[$i])+  hex($eeprom[$i+1]) + hex($eeprom[$i+2]) + hex($eeprom[$i+3]) + hex($eeprom[$i+4]) ;
+#		printf  "   %x sum \n",$sum ;	
+		}else{
+	}	
+
+	open(EEPROMOUT, ">$inputeeprom2") or die "can't open $inputeeprom: $!";
+	open(OTPOUT, ">$outputotp") or die "can't open $outputotp: $!";
+	open(FLASHOUT, ">$outputflash") or die "can't open $outputflash: $!";
+	$regLen = @config_type;
+
+	for( $count=0;$count<$eepromlen-2-2;$count++){
+		print FLASHOUT $flash[$count];
+		print EEPROMOUT $eeprom[$count];
+		print OTPOUT $otp[$count];
+	}	
+	
+	printf FLASHOUT "%02x\n%02x\n",($sum_flash & 0xff), (($sum_flash >> 8) & 0xff);
+	printf EEPROMOUT "%02x\n%02x\n",($sum_eeprom & 0xff), (($sum_eeprom >> 8) & 0xff);
+	printf OTPOUT "%02x\n%02x\n",($sum_otp & 0xff), (($sum_otp >> 8) & 0xff);	
+	
+	close(FLASHOUT);
+	close(EEPROMOUT);
+	close(OTPOUT);
+	
+}else
+{
+	$config_type[9] = sprintf "%02x\n",(($eepromlen+1+$conlen) & 0xff);
+	$config_type[10] = sprintf "%02x\n",(($eepromlen+1+$conlen) >> 8) ;
+	
+	my $regLen = @file_end;
+	for( $count=0;$count<$regLen;$count++){
+		if($file_end[ $count] != $eeprom[$eepromlen-4-$regLen+$count])
+		{	last;}
+	}
+
+	if ($count == $regLen){
+		$existsum=1;
+		$crcoffset=6;
+		$sum = hex($eeprom[$eepromlen-4]) + (hex($eeprom[$eepromlen-3])<<8);
+#		printf  "%x sum \n",$sum ;	
+	}else{
+	}
+
+	open(EEPROMOUT, ">$inputeeprom2") or die "can't open $inputeeprom: $!";
+	open(OTPOUT, ">$outputotp") or die "can't open $outputotp: $!";
+	open(FLASHOUT, ">$outputflash") or die "can't open $outputflash: $!";
+	$regLen = @config_type;
+
+	for( $count=0;$count<$eepromlen-2-$crcoffset;$count++){
+		print FLASHOUT $eeprom[$count];
+		print EEPROMOUT $eeprom[$count];
+		print OTPOUT $eeprom[$count];
+	}	
+
+	my @config_type_eeprom =  @config_type;
+	my @config_type_flash =  @config_type;
+	my @config_type_otp =  @config_type;
+	$config_type_eeprom[6] = "02\n";
+	$config_type_otp[6] =  "03\n";
+	$config_type_flash[6] =  "04\n";
+	$config_type_otp[7] =  sprintf "%02x\n",(($ARGV[0]) & 0xff);
+	$config_type_otp[8] = sprintf "%02x\n",($ARGV[0]) >> 8 ;	
+	$config_type_flash[7] =  sprintf "%02x\n",(($ARGV[1]) & 0xff);
+	$config_type_flash[8] = sprintf "%02x\n",($ARGV[1]) >> 8 ;	
+	$config_type_eeprom[7] =  sprintf "%02x\n",(($ARGV[2]) & 0xff);
+	$config_type_eeprom[8] = sprintf "%02x\n",($ARGV[2]) >> 8 ;
+	
+
+	$sum_flash = $sum ;
+	$sum_otp =  $sum;
+	$sum_eeprom = $sum;
+	{
+		for($i=0;$i<$regLen;$i++,$count++){	
+			print FLASHOUT $config_type_flash[$i];
+			print EEPROMOUT $config_type_eeprom[$i];
+			print OTPOUT $config_type_otp[$i];
+			$eeprom[$count] =  $config_type[$i];
+			$sum_flash += hex($config_type_flash[$i]);
+			$sum_eeprom += hex($config_type_eeprom[$i]);
+			$sum_otp += hex($config_type_otp[$i]);	
+#	print  $config_type_eeprom[$i], " yesyes \n",;	
+		}
+	}
+
+	if($existsum  ){
+		for($i=0;$i<@file_end;$i++,$count++){	
+			print FLASHOUT $file_end[$i];
+			print EEPROMOUT $file_end[$i];
+			print OTPOUT $file_end[$i];
+			$eeprom[$count] =  $file_end[$i];		
+		}
+	printf FLASHOUT "%02x\n%02x\n",($sum_flash & 0xff), (($sum_flash >> 8) & 0xff);
+	printf EEPROMOUT "%02x\n%02x\n",($sum_eeprom & 0xff), (($sum_eeprom >> 8) & 0xff);
+	printf OTPOUT "%02x\n%02x\n",($sum_otp & 0xff), (($sum_otp >> 8) & 0xff);	
+	}
+
+
+	close(FLASHOUT);
+	close(EEPROMOUT);
+	close(OTPOUT);
+}
+
+crc($outputflash);
+crc($inputeeprom2);
+crc($outputotp);
+
+
+sub crc{
+	open(FILEOUT, "+<$_[0]") or die "can't open $_[0]: $!";
+	my @file = <FILEOUT>;
+	my $filelen = @file;
+	my $crc = 0xffff;
+	for($i=2;$i<$filelen;$i++){   
+		$crc  = ($crc >> 8) | ($crc << 8);
+		$crc ^= hex($file[$i]) & 0xff;
+		$crc ^= ($crc & 0xff) >> 4;
+		$crc ^= $crc << 12;
+		$crc ^= ($crc & 0xff) << 5;
+		$crc &= 0xffff;
+	}	
+	printf FILEOUT "%02x\n%02x\n", (($crc >> 8) & 0xff),($crc & 0xff);
+	close(FILEOUT);
+}
+
+
+
