################################################################################
##		MICETEK International Inc.
##		Linux Shell Script for Multi targets
##		Target : General targets
##		Engineer : Blues
##		Date : 2006/01/19
################################################################################


# Variables
Tf0=0
Tf1=0
Tf2=0
Tf3=0
Tf4=0
Tf5=0
Tf6=0
Tf7=0
sflag=0
lflag=0
ledflag=0


# Turn off all Led
led 8 off
for i in $TarTaps
do
	led $i off
done


#topen -c $ARMCORE -e $Endian -s $TckFreq -r $RstType -t $TapIndex -m $MultiCfg

# Target open judge
#if [ $? -ne 0 ]
#then
	#echo Target open error!
	#exit
#else
	for i in $TarTaps
	do
		topen -c $ARMCORE -e $Endian -s $TckFreq -r $RstType -t $i -m $MultiCfg
		status -t $i

		# Target status judge
		if [ $? -ne 1 ]
		then
			halt -t $i
			status -t $i

			if [ $? -ne 1 ]
			then
				eval Tf$i=1
				echo Target $i do not stop!
			fi
		# Target status judge end
		fi
	done

	for i in $TarTaps
	do
		eval sflag=$T$i
		
		#long 0xc 0xeafffffe -t $i
		#long 0x10 0xeafffffe -t $i

		# Target stop judge
		if [ $sflag -eq 0 ]
		then
			echo Target $i Initializing now......
			tinit $TarInitFile -t $i
			twatch -c 0x100 -cm 0xf7 -a $BreakPoint -am 0x0 -dm 0xffffffff -i 0 -t $i
			ferase $TarInitFile -t $i
		# Target stop judge end
		fi
	done

	#Wait for erase end and start to program
	for i in $TarTaps
	do
		eval sflag=$T$i
		eval lflag=$L$i
		
		# Target error judge
		if [ $sflag -eq 0 ]
		then
			echo Target $i ......
			status -t $i -o $timeout
			
			# Target erase stop judge
			if [ $? -ne 1 ]
			then
				ledflag=1
				eval Tf$i=1
				halt -t $i
				led $lflag off
				echo Target $i erase Timeout!
			else
				register pc -t $i > $PCFile
				chkr $PCFile $BreakPoint
				
				# Target stop right check
				if [ $? -eq 0 ]
				then
					register r5 -t $i > $TarErrFile
					chkr $TarErrFile 0
			
					# Target erase OK judge
					if [ $? -ne 0 ]
					then
						ledflag=1
						eval Tf$i=1
						led $lflag off
						echo Target $i erase Error!
					else
						led $lflag on
						echo Target $i erase OK!
						twatch -c 0x100 -cm 0xf7 -a $BreakPoint -am 0x0 -dm 0xffffffff -i 0 -t $i
						fprog $TarInitFile $UserProgFile -t $i
					# Target erase OK judge end
					fi
					rm $TarErrFile
					led $lflag off
				else
					ledflag=1
					eval Tf$i=1
					led $lflag off
					echo Target $i erase Error!
				# Target stop right check end
				fi
				rm $PCFile
			# Target erase stop judge end
			fi
		# Target error judge end
		fi
	done

	#Wait for program end
	for i in $TarTaps
	do
		eval sflag=$T$i
		eval lflag=$L$i

		# Target error judge
		if [ $sflag -eq 0 ]
		then
			echo Target $i ......
			status -t $i -o $timeout

			# Target program stop judge
			if [ $? -ne 1 ]
			then
				ledflag=1
				eval Tf$i=1
				halt -t $i
				led $lflag off
				echo Target $i program Timeout!
			else
				register pc -t $i > $PCFile
				chkr $PCFile $BreakPoint
				
				# Target stop right check
				if [ $? -eq 0 ]
				then
					register r5 -t $i > $TarErrFile
					chkr $TarErrFile 0

					# Target program OK judge
					if [ $? -ne 0 ]
					then
						ledflag=1
						eval Tf$i=1
						led $lflag off
						echo Target $i program Error!
					else
						led $lflag on
						echo Target $i program OK!
					# Target program OK judge end
					fi
					rm $TarErrFile
				else
					ledflag=1
					eval Tf$i=1
					led $lflag off
					echo Target $i program Error!
				# Target stop right check end
				fi
				rm $PCFile
			# Target program stop judge end
			fi
		# Target error judge end
		fi
	done

	# LED flash judge
	if [ $ledflag -eq 0 ]
	then
		led 8 on
		
		for i in $TarTaps
		do
			eval lflag=$L$i
			led $lflag off
		done

		for i in $LedFlashNum
		do
			for j in $TarTaps
			do
				eval lflag=$L$j
				led $lflag on
				led $lflag on
				led $lflag off
			done
		done

		for i in $TarTaps
		do
			eval lflag=$L$i
			led $lflag on
		done
	# LED flash judge end
	fi
# Target open judge end
#fi

tclose
