]> git.sur5r.net Git - openocd/blob - contrib/gen-stellaris-part-header.pl
log: debug level is between silent and debug output levels
[openocd] / contrib / gen-stellaris-part-header.pl
1 #!/usr/bin/perl
2 # Automatically generates the StellarisParts struct in src/flash/nor/stellaris.c
3 # Uses the header files from TI/Luminary's StellarisWare complete Firmware Development Package
4 # available from: http://www.luminarymicro.com/products/software_updates.html
5
6 $comment = "// Autogenerated by contrib/gen-stellaris-part-header.pl
7 // From Stellaris Firmware Development Package revision";
8
9 $struct_header = "static struct {
10         uint32_t partno;
11         const char *partname;
12 }       StellarisParts[] =
13 {
14 ";
15
16 $struct_footer = "\t{0,\"Unknown part\"}\n};\n";
17
18 $#ARGV == 1 || die "Usage: $0 <inc directory> <output file>\n";
19 -d $ARGV[0] || die $ARGV[0]." is not a directory\n";
20 $dir = $ARGV[0];
21 -f $ARGV[1] || die $ARGV[1]." is not a file\n";
22 $file = $ARGV[1];
23 print STDERR "Scanning $dir, Updating $file\n";
24
25 opendir(DIR, $dir) || die "can't open $dir: $!";
26 @files = readdir(DIR);
27 closedir(DIR);
28
29 @short_files = sort(grep(/lm3s...\.h/, @files));
30 @long_files = sort(grep(/lm3s....\.h/, @files));
31
32 $ver = 0;
33 $new_struct = $struct_header;
34 process_file(@short_files);
35 process_file(@long_files);
36 $new_struct .= $struct_footer;
37
38 $dump = "$comment $ver\n$new_struct";
39 {
40         local($/, *INPUT);
41         open(INPUT, $file) || die "can't open $file: $!";
42         $contents = <INPUT>;
43         close(INPUT);
44 }
45
46 $old_struct = qr/((^\/\/.*?\n)*)\Q$struct_header\E.*?$struct_footer/sm;
47 $contents =~ s/$old_struct/$dump/;
48 open(OUTPUT, ">$file") || die "can't open file $file for writing: $!";
49 print OUTPUT $contents;
50 close(OUTPUT);
51
52 sub process_file {
53         foreach $h_file (@_) {
54                 ($base) = ($h_file =~ m/lm3s(.{3,4})\.h/ig);
55                 $base = uc($base);
56                 local($/, *FILE);
57                 open(FILE, "$dir/$h_file");
58                 $content = <FILE>;
59                 close(FILE);
60                 $invalid = 0;
61                 if ($content =~ /This is part of revision (\d+) of/) {
62                         if ($ver != 0 and $ver != $1) {
63                                 print STDERR "File version mismatch: $ver != $1\n";
64                                 $ver = max($ver, $1);
65                         } else {
66                                 $ver = $1;
67                         }
68                 }
69                 if ($content =~ /SYSCTL_DID1_VER_[^M]\s+0x(\S+)/) {
70                         $did1_ver = hex($1);
71                 } else {
72                         print STDERR "$h_file is missing SYSCTL_DID1_VER\n";
73                         $did1_ver = 255;
74                         $invalid = 1;
75                 }
76                 if ($content =~ /SYSCTL_DID1_PRTNO_$base\s+0x(\S+)/) {
77                         $prtno = hex($1);
78                 } else {
79                         print STDERR "$h_file is missing SYSCTL_DID1_PRTNO\n";
80                         $prtno = 0;
81                         $invalid = 1;
82                 }
83                 $id = ($did1_ver | $prtno) >> 16;
84                 $new_member = sprintf "{0x%04X,\"LM3S%s\"},", $id, $base;
85                 if ($invalid == 1) {
86                         #$new_struct .= "\t//$new_member\t// Invalid\n";
87                 } else {
88                         $new_struct .= "\t$new_member\n";
89                 }
90         }
91 }