telnet: cleanup comments
[openocd/ntfreak.git] / contrib / gen-stellaris-part-header.pl
blob0cc567f72fbafadf760f460d0957cb89fb660c8a
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
6 $comment = "// Autogenerated by contrib/gen-stellaris-part-header.pl
7 // From Stellaris Firmware Development Package revision";
9 $struct_header = "static struct {
10 uint8_t class;
11 uint8_t partno;
12 const char *partname;
13 } StellarisParts[] = {
16 $struct_footer = "\t{0xFF, 0x00, \"Unknown Part\"}\n};\n";
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";
25 opendir(DIR, $dir) || die "can't open $dir: $!";
26 @files = readdir(DIR);
27 closedir(DIR);
29 @header_files = sort(grep(/lm.+\.h/, @files));
31 $ver = 0;
32 $new_struct = $struct_header;
33 process_file(@header_files);
34 $new_struct .= $struct_footer;
36 $dump = "$comment $ver\n$new_struct";
38 local($/, *INPUT);
39 open(INPUT, $file) || die "can't open $file: $!";
40 $contents = <INPUT>;
41 close(INPUT);
44 $old_struct = qr/((^\/\/.*?\n)*)\Q$struct_header\E.*?$struct_footer/sm;
45 $contents =~ s/$old_struct/$dump/;
46 open(OUTPUT, ">$file") || die "can't open file $file for writing: $!";
47 print OUTPUT $contents;
48 close(OUTPUT);
50 sub process_file {
51 foreach $h_file (@_) {
52 ($base) = ($h_file =~ m/lm..(.{3,7})\.h/ig);
53 $base = uc($base);
54 local($/, *FILE);
55 open(FILE, "$dir/$h_file");
56 $content = <FILE>;
57 close(FILE);
58 $invalid = 0;
59 if ($content =~ /This is part of revision (\d+) of/) {
60 if ($ver != 0 and $ver != $1) {
61 print STDERR "File version mismatch: $ver != $1\n";
62 $ver = max($ver, $1);
63 } else {
64 $ver = $1;
68 if ($content =~ /SYSCTL_DID0_CLASS_[^M].+?0x(\S+)/s) {
69 $class = hex($1) >> 16;
70 } else {
71 # attempt another way to get class
72 if ($content =~ /\s(\S+)-class/) {
73 $class = getclass($1);
74 if ($class eq 0xFF) {
75 print STDERR "$h_file unknown class\n";
76 $invalid = 1;
78 } else {
79 print STDERR "$h_file is missing SYSCTL_DID0_CLASS_\n";
80 $class = 0;
81 $invalid = 1;
85 if ($content =~ /SYSCTL_DID1_PRTNO_$base.+0x(\S+)/) {
86 $prtno = hex($1);
87 $base = "LM3S" . $base;
88 } else {
89 # LM4F have a changed header
90 if ($content =~ /SYSCTL_DID1_PRTNO_LM4F$base.+?0x(\S+)/s) {
91 $prtno = hex($1);
92 $base = "LM4F" . $base;
93 } else {
94 print STDERR "$h_file is missing SYSCTL_DID1_PRTNO\n";
95 $prtno = 0;
96 $invalid = 1;
99 $new_member = sprintf "{0x%02X, 0x%02X, \"%s\"},", $class, $prtno >> 16, $base;
100 if ($invalid == 1) {
101 #$new_struct .= "\t//$new_member\t// Invalid\n";
102 } else {
103 $new_struct .= "\t$new_member\n";
108 sub getclass {
109 $class = $_[0];
110 if ($class =~ /Sandstorm/i) {
111 return 0;
112 } elsif ($class =~ /Fury/i) {
113 return 1;
114 } elsif ($class =~ /DustDevil/i) {
115 return 3;
116 } elsif ($class =~ /Tempest/i) {
117 return 4;
118 } elsif ($class =~ /Blizzard/i) {
119 return 5;
120 } elsif ($class =~ /Firestorm/i) {
121 return 6;
123 return 0xFF;