board_test.pl 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. #!/usr/bin/perl
  2. use List::MoreUtils 'any';
  3. use List::MoreUtils 'first_value';
  4. use File::Spec;
  5. use File::Find;
  6. use File::Path;
  7. use File::Glob;
  8. use File::stat;
  9. use Cwd;
  10. use Cwd 'abs_path';
  11. $" = "\n"; # change list separator
  12. $KEIL_PATH = 'C:/Keil/UV4'; #'/C/Keil/UV4';
  13. $IAR_PATH = glob ('C:/Program*/IAR*/Embedded*/common/bin');
  14. $XPRESSO_PATH = glob ('C:/nxp/LPCXpresso_7*/lpcxpresso');
  15. $XPRESSO_BIN_PATH = "$XPRESSO_PATH/bin";
  16. $XPRESSO_PATH = "$XPRESSO_PATH;$XPRESSO_PATH/bin;$XPRESSO_PATH/tools/bin;$XPRESSO_PATH/msys/bin";
  17. $ENV{'PATH'} .= ';' . $KEIL_PATH . ';' . $IAR_PATH . ';' . $XPRESSO_PATH;
  18. $ENV{'PATH'} .= ';' . "C:/Keil/ARM/BIN";
  19. #print $ENV{'PATH'}; die;
  20. $repo_path = abs_path(cwd . "/..");
  21. $device_dir = "device/";
  22. $host_dir = "host/";
  23. ##### Command line arguments ###
  24. $board = $ARGV[0];
  25. $is_keil = (any { /keil/ or /all/ } @ARGV);
  26. $is_iar = (any { /iar/ or /all/ } @ARGV);
  27. $is_xpresso = (any { /xpresso/ or /all/ } @ARGV);
  28. $is_download_only = (any { /download_only/ } @ARGV);
  29. if ( any { /device_/ or /host_/ } @ARGV)
  30. {
  31. my $build_project;
  32. $device_dir .= defined ($build_project = first_value { /device_/ } @ARGV) ? $build_project : "nowhere_path" ;
  33. $host_dir .= defined ($build_project = first_value { /host_/ } @ARGV) ? $build_project : "nowhere_path";
  34. }else
  35. { #default is all
  36. $device_dir .= "*";
  37. $host_dir .= "*";
  38. }
  39. #print "$device_dir $host_dir"; die;
  40. my $log_file = "board_$board.txt";
  41. unlink $log_file;
  42. ################## KEIL #####################
  43. if ($is_keil)
  44. {
  45. @KEIL_PROJECT_LIST = (<$device_dir*/*.uvproj>, <$host_dir*/*.uvproj>);
  46. foreach (@KEIL_PROJECT_LIST)
  47. {
  48. /([^\/]+).uvproj/;
  49. print_title("Keil $1");
  50. my $temp_log = "temp_log.txt";
  51. my $build_cmd = "Uv4 -b $_ -t$board -j0 -o ../../$temp_log";
  52. if ( $is_download_only || cmd_execute($build_cmd) < 2 )
  53. {
  54. append_file($log_file, $temp_log);
  55. my $flash_cmd = "Uv4 -f $_ -t$board -j0 -o ../../$temp_log";
  56. append_file($log_file, $temp_log) if flash_to_board($flash_cmd);
  57. }
  58. }
  59. }
  60. ################## IAR #####################
  61. if ($is_iar)
  62. {
  63. @IAR_PROJECT_LIST = (<$device_dir*/*.ewp>, <$host_dir*/*.ewp>);
  64. foreach (@IAR_PROJECT_LIST)
  65. {
  66. /(.+\/)([^\/]+).ewp/;
  67. print_title("IAR $2");
  68. my $build_cmd = "IarBuild $_ -build $board -log warnings >> $log_file";
  69. if ( $is_download_only || cmd_execute($build_cmd) == 0)
  70. {
  71. my $flash_cmd = "cd $1 & " . iar_flash_cmd($_);
  72. flash_to_board($flash_cmd);
  73. }
  74. }
  75. }
  76. ################## LPCXPRESSO #####################
  77. if ($is_xpresso)
  78. {
  79. (my $repo_path_other_dash = $repo_path) =~ s/\//\\/g;
  80. my $workspace_dir = "C:/Users/hathach/Dropbox/tinyusb/workspace7"; #projects must be opened in the workspace to be built
  81. my %flash_tool =
  82. ( # board => (tool, chip_name)
  83. 'Board_EA4357' => ['crt_emu_lpc18_43_nxp' , 'LPC4357' ],
  84. 'Board_NGX4330' => ['crt_emu_lpc18_43_nxp' , 'LPC4330' ],
  85. 'Board_LPCXpresso1769' => ['crt_emu_cm3_nxp' , 'LPC1769' ],
  86. 'Board_LPCXpresso1347' => ['crt_emu_lpc11_13_nxp' , 'LPC1347' ],
  87. 'Board_rf1ghznode' => ['crt_emu_lpc11_13_nxp' , 'LPC11U37/401'],
  88. );
  89. die "board is not supported" unless $flash_tool{$board};
  90. print "all projects in $workspace_dir must be opened and set to the correct MCU of the boards. Enter to continue:\n";
  91. #<STDIN>;
  92. @XPRESSO_PROJECT_LIST = (<$device_dir*/.cproject>, <$host_dir*/.cproject>);
  93. foreach (@XPRESSO_PROJECT_LIST)
  94. {
  95. /(.+\/(.+))\/.cproject/;
  96. my $proj_dir = $1;
  97. my $proj = $2;
  98. print_title("XPRESSO $proj");
  99. my $build_cmd = "lpcxpressoc -nosplash --launcher.suppressErrors -application org.eclipse.cdt.managedbuilder.core.headlessbuild -build $proj/$board -data $workspace_dir >> $log_file";
  100. system("cd $proj_dir/$board & make clean >> $log_file") if !$is_download_only; # lpcxpresso have a bug that clean the current active config instead of the passed in, manual clean
  101. if ( $is_download_only || cmd_execute($build_cmd) == 0)
  102. {
  103. my $flash_cmd = "$flash_tool{$board}[0] -p$flash_tool{$board}[1] -s2000 -flash-load-exec=$proj_dir/$board/$proj.axf";
  104. $flash_cmd .= " -flash-driver=$XPRESSO_BIN_PATH/Flash/LPC18_43_SPIFI_4MB_64KB.cfx" if $board eq 'Board_NGX4330';
  105. #print $flash_cmd; die;
  106. flash_to_board($flash_cmd);
  107. }
  108. }
  109. =pod
  110. open (my $fout, ">$log_file") or die;
  111. foreach (@log_content)
  112. {
  113. unless (/Invoking: MCU C Compiler/ or /arm-none-eabi-gcc -D/ or /Finished building:/ or /^ $/)
  114. {
  115. s/Building file:.+?([^\/]+\.[ch])/\1/;
  116. s/$repo_path//;
  117. s/$repo_path_other_dash//;
  118. print $fout $_;
  119. }
  120. }
  121. =cut
  122. }
  123. ################## HELPER #####################
  124. sub cmd_execute
  125. {
  126. print "executing: $_[0] ...";
  127. $result = system($_[0]);
  128. print "$result done\n";
  129. return $result;
  130. }
  131. sub flash_to_board
  132. {
  133. my $flash_cmd = $_[0];
  134. print "Do you want to flash y/n: ";
  135. chomp ($ask = <STDIN>);
  136. #$ask = "y";
  137. cmd_execute($flash_cmd) if ( $ask eq "y" );
  138. return ( $ask eq "y" );
  139. }
  140. sub print_title
  141. {
  142. print "---------------------------------------------------------------------\n";
  143. print "$_[0] for $board\n";
  144. print "---------------------------------------------------------------------\n";
  145. }
  146. sub append_file
  147. {
  148. my $log_file = $_[0];
  149. my $temp_log = $_[1];
  150. open(my $log_handle, ">>$log_file") or die "cannot open $log_file";
  151. open(my $temp_handle, $temp_log) or die "cannot open $temp_log";
  152. print $log_handle (@temp_content = <$temp_handle>);
  153. close($temp_handle);
  154. close($log_handle);
  155. }
  156. sub iar_flash_cmd
  157. {
  158. $_[0] =~ /^(.+)\/(.+).ewp/;
  159. my $debug_file = "$board/Exe/$2.out";
  160. my $arm_path = abs_path "$IAR_PATH/../../arm";
  161. my $bin_path = "$arm_path/bin";
  162. my $debugger_path = "$arm_path/config/debugger/NXP";
  163. my %mcu_para_hash =
  164. ( # board => family (for macro), architecture, fpu, name
  165. 'Board_EA4357' => ['LPC18xx_LPC43xx', 'Cortex-M4', 'VFPv4', 'LPC4357_M4' ],
  166. 'Board_NGX4330' => ['LPC18xx_LPC43xx', 'Cortex-M4', 'VFPv4', 'LPC4330_M4' ],
  167. 'Board_LPCXpresso1769' => ['LPC175x_LPC176x', 'Cortex-M3', 'None' , 'LPC1769' ],
  168. 'Board_LPCXpresso1347' => ['lpc1315' , 'Cortex-M3', 'None' , 'LPC1347' ],
  169. 'Board_rf1ghznode' => ['' , 'Cortex-M0', 'None' , 'LPC11U37FBD48_401'],
  170. );
  171. my @mcu_para = @{$mcu_para_hash{$board}};
  172. die "Board is not supported" unless @mcu_para;
  173. my $cmd = "cspybat \"$bin_path/armproc.dll\" \"$bin_path/armjlink.dll\" \"$debug_file\" --download_only --plugin \"$bin_path/armbat.dll\"";
  174. $cmd .= " --macro \"$debugger_path/Trace_$mcu_para[0].dmac\"" if $mcu_para[0];
  175. $repo_mcu_iar = "$repo_path/mcu/lpc43xx/iar";
  176. $cmd .= " --macro \"$repo_mcu_iar/lpc18xx_43xx_debug.mac\" --flash_loader \"$repo_mcu_iar/FlashLPC18xx_43xx_SPIFI.board\"" if $board eq 'Board_NGX4330';
  177. $cmd .= " --backend -B \"--endian=little\" \"--cpu=$mcu_para[1]\" \"--fpu=$mcu_para[2]\" \"-p\" \"$debugger_path/$mcu_para[3].ddf\" \"--semihosting\" \"--device=$mcu_para[3]\"";
  178. #SWD interface --> need change if use lpc43xx_m0
  179. $cmd .= " \"--drv_communication=USB0\" \"--jlink_speed=auto\" \"--jlink_initial_speed=1000\" \"--jlink_reset_strategy=0,0\" \"--jlink_interface=SWD\" \"--drv_catch_exceptions=0x000\" --drv_swo_clock_setup=72000000,0,2000000\"";
  180. $cmd .= " \"--jlink_script_file=$debugger_path/LPC4350_DebugCortexM4.JLinkScript\"" if $mcu_para[3] =~ /LPC43.._M4/;
  181. $cmd =~ s/\//\\/g;
  182. #print $cmd; die;
  183. return $cmd;
  184. }