5 use Data::Dump qw(dump);
12 my $opt_horizontal = 0;
23 'invert!' => \$opt_invert,
24 'vertical-flip!' => \$opt_vertical,
25 'horizontal-flip!' => \$opt_horizontal,
26 'edge-pins!' => \$opt_edge,
27 'middle-pins!' => \$opt_middle,
28 'zebra!' => \$opt_zebra,
29 'lines!' => \$opt_lines,
30 'read=s' => \$opt_read,
31 'pins=s' => \$opt_pins,
32 'color' => \$opt_color,
36 my $font_w = 1.67; # < 2.54, font is not perfect square
37 my $font_b = 2.10; # font baseline position
39 $opt_read .= '/' unless $opt_read =~ m/\/$/;
42 open(my $fh, '<', $opt_read . shift);
49 my $model = slurp('/proc/device-tree/model');
50 $model =~ s/\x00$//; # strip kernel NULL
51 warn "# model [$model]";
54 open(DATA, '<', $opt_pins) if $opt_pins;
63 warn "MODEL [$1] == [$model] ?\n";
64 if ( $model =~ m/$1/ ) {
69 } elsif ( $include || $opt_pins ) {
70 push @{ $pins->{$1} }, $line_i while ( m/\t\s*(\w+\d+)/g );
76 warn "IGNORE: [$_]\n";
80 if ( ! $opt_pins && ! $pins ) {
82 $glob =~ s/^(\w+).*$/$1/;
83 my @pins = glob "pins/${glob}*";
84 warn "# possible pins: ",dump( \@pins );
89 die "add pin definition for # $model" unless $pins;
91 #warn "# lines ",dump( \@lines );
92 warn "# pins ",dump($pins);
96 glob($opt_read . '/sys/devices/platform/soc*/*.serial/tty/tty*'), # 4.x
97 glob( '/sys/devices/soc.*/*.uart/tty/tty*') # 3.10
99 my @v = split(/\//, $_);
100 $serial_tty->{ $v[-3] } = $v[-1];
102 warn "# serial_tty = ",dump($serial_tty);
111 my ($pin, $note) = @_;
112 if ( $pins->{$pin} ) {
113 foreach my $line ( @{$pins->{$pin}} ) {
114 my $t = $lines[$line];
118 $t =~ s/$pin/$pin $note/ || warn "can't find $pin in [$t]";
121 warn "# $line: $lines[$line]\n";
124 warn "IGNORED: pin $pin function $note\n";
128 open(my $fh, '<', $opt_read . '/sys/kernel/debug/pinctrl/pinctrl-maps');
131 if ( m/^device (\S+)/ ) {
133 if ( my $replace = $serial_tty->{$device} ) {
134 $device = $replace; # replace serial hex with kernel name
136 $device =~ s/^[0-9a-f]*\.//; # remove hex address
138 } elsif ( m/^group (\w+\d+)/ ) {
141 } elsif ( m/^function (\S+)/ ) {
144 if ( $device && $pin && $function ) {
145 push @{ $pin_function->{$pin} }, "$device $function";
147 annotate_pin $pin, "[$device $function]";
149 warn "missing one of ",dump( $device, $pin, $function );
159 warn "# pin_function = ",dump($pin_function);
161 my $have_sunxi_pio = `which sunxi-pio`;
162 if ( $have_sunxi_pio ) {
164 open(my $pio, '-|', 'sunxi-pio -m print');
168 my @p = split(/\s+/,$_);
169 warn "# pio ",dump(\@p);
170 # annotate input 0 and output 1 pins
171 # annotate_pin $p[0], ( $p[1] ? 'O' : 'I' ) . ':' . $p[4] if $p[1] == 0 || $p[1] == 1;
173 annotate_pin $pin, join(' ',@p) if ! $opt_svg;
179 my @max_len = ( 0,0,0,0 );
182 shift(@lines) while ( ! $lines[0] ); # remove empty at beginning
183 pop(@lines) while ( ! $lines[-1] ); # remove empty at end
185 foreach my $line (@lines) {
186 if ( $line =~ m/^#/ ) {
187 push @line_parts, [ $line ] unless $opt_svg && $line =~ m/^###+/; # SVG doesn't display 3rd level comments
190 $line =~ s/\[(\w+)\s+(\w+)\] \[\1\s+(\w+)\]/[$1 $2 $3]/g; # compress kernel annotation with same prefix
191 $line =~ s/(\[(?:uart\d*|serial|tty\w+))([^\t]*\]\s[^\t]*(rx|tx)d?)/$1 $3$2/gi;
192 $line =~ s/(\[i2c)([^\t]*\]\s[^\t]*(scl?k?|sda))/$1 $3$2/gi;
193 $line =~ s/(\[spi)([^\t]*\]\s[^\t]*(miso|mosi|s?clk|c[se]\d*))/$1 $3$2/gi;
194 $line =~ s/\s*\([^\)]+\)//g if ! $opt_alt;
196 # shorten duplicate kernel device/function
197 $line =~ s/\[serial (\w+) (uart\d+)\]/[$2 $1]/g;
198 $line =~ s/\[(\w+) (\w+) \1(\d+)\]/[$1$3 $2]/g;
200 $line =~ s/\[(\w+)\s+([^\]]+)\s+\1\]/[$1 $2]/g; # duplicate
202 my @v = split(/\s*\t+\s*/,$line,4);
203 @v = ( $v[2], $v[3], $v[0], $v[1] ) if $opt_horizontal && $v[2];
205 push @line_parts, [ @v ];
206 foreach my $i ( 0 .. 3 ) {
207 next unless exists $v[$i];
208 next if $v[$i] =~ m/^#/; # don't calculate comments into max length
209 my $l = length($v[$i]);
210 $max_len[$i] = $l if $l > $max_len[$i];
214 warn "# max_len = ",dump( \@max_len );
215 warn "# line_parts = ",dump( \@line_parts );
217 #print "$_\n" foreach @lines;
223 print qq{<?xml version="1.0" encoding="UTF-8" standalone="no"?>
225 xmlns:dc="http://purl.org/dc/elements/1.1/"
226 xmlns:cc="http://creativecommons.org/ns#"
227 xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
228 xmlns:svg="http://www.w3.org/2000/svg"
229 xmlns="http://www.w3.org/2000/svg"
230 xmlns:xlink="http://www.w3.org/1999/xlink"
233 viewBox="0 0 210 297"
240 }; # svg, insert rest of rect
242 print qq{<rect x="0" y="0" width="210" height="297" style="fill:#000000" id="high-contrast"/>} if $opt_invert;
247 my $cols = { # foreground background
248 txt => [ '#000000', '#ffffff' ],
249 pins => [ '#ffffff', '#ff00ff' ],
250 vcc => [ '#ff0000', '#ffff00' ],
251 gnd => [ '#000000', '#00ffff' ],
252 i2c => [ '#008800', '#ffcccc' ],
253 serial=>[ '#000088', '#ccffcc' ],
254 spi => [ '#880000', '#ccccff' ],
259 die "$swap not found in ",dump($cols) unless $cols->{$swap};
260 my ( $c1, $c2 ) = @{ $cols->{$swap} };
261 $cols->{$swap} = [ $c2, $c1 ];
264 swap_cols 'txt' if $opt_invert;
268 my ($name,$x,$y,$col) = @_;
270 return '' unless $opt_color;
272 $y -= $font_b; # shift box overlay to right vertical position based on font baseline
275 my ($x,$y,$col,$fill) = @_;
276 print qq{<rect x="$x" y="$y" height="2.54" width="}, $max_len[$col] * $font_w, qq{" style="fill:$fill;stroke:#ffffff;stroke-width:0.10" />\n};
280 if ( $name =~ m/^(\d+)$/ ) { # pins
282 my ( $fg, $bg ) = @{ $cols->{pins} };
284 my $w = $max_len[$col]*$font_w - 0.1;
287 #print qq{<polygon points="$x,$y $cx,$y $x,$cy $x,$y" stroke="$fg" stroke-width="0.25" fill="$bg" />};
288 #print qq{<polygon points="$x,$cy $cx,$cy $cx,$y $x,$cy" stroke="$bg" stroke-width="0.25" fill="$fg" />};
289 print qq{<rect x="$x" y="$y" width="$w" height="2.54" stroke="$fg" stroke-width="0.3" fill="$bg" />};
290 my ( $fg, $bg ) = @{ $cols->{txt} };
291 print qq{<rect x="$x" y="$y" width="$w" height="2.54" rx="1" ry="1" stroke="$fg" stroke-width="0.3" fill="$bg" />};
295 return qq{ style="fill:$bg"};
298 if ( $name =~ m/(VCC|3V3|3.3V|5v)/i ) {
299 my ($fg,$bg) = @{ $cols->{vcc} };
301 return qq{ style="fill:$fg"};
302 } elsif ( $name =~ m/(G(ND|Round)|VSS|0v)/i ) {
303 my ($fg,$bg) = @{ $cols->{gnd} };
305 return qq{ style="fill:$fg"};
306 } elsif ( $name =~ m/\[(\w+)/ ) { # kernel
308 my ($fg,$bg) = @{ $cols->{txt} };
309 $dev = 'serial' if $dev =~ m/^tty/;
310 ($fg,$bg) = @{ $cols->{$dev} } if exists $cols->{$dev};
312 return qq{ style="fill:$fg"};
314 my ( $fg, $bg ) = @{ $cols->{txt} };
316 #return qq{ style="fill:$fg"};
323 my @cols_order = ( 0,1,2,3 );
324 my @cols_align = ( '','-','','-' ); # sprintf prefix
326 my @cols_shuffle = @cols_order;
329 # pins outside on the right
330 @cols_shuffle = ( 0,1,3,2 ) if $opt_edge;
331 @cols_align = ( '-','-','','' );
332 } elsif ( $opt_middle ) {
334 @cols_shuffle = ( 1,0,2,3 );
335 @cols_align = ( '','','-','-' );
339 my ( $what, $order ) = @_;
341 foreach my $i ( 0 .. $#$what ) {
342 $new->[$i] = $what->[ $order->[$i] ];
344 warn "# cols_shuffle what=",dump($what)," order=",dump($order)," new=",dump($new);
348 @cols_order = cols_shuffle( \@cols_order, \@cols_shuffle );
349 @max_len = cols_shuffle( \@max_len, \@cols_shuffle );
351 warn "# cols_order = ",dump( \@cols_order );
352 warn "# cols_align = ",dump( \@cols_align );
354 my $fmt = "%$cols_align[0]$max_len[0]s %$cols_align[1]$max_len[1]s %$cols_align[2]$max_len[2]s %$cols_align[3]$max_len[3]s\n";
358 my ($fg,$bg) = @{ $cols->{txt} };
359 my $line_fmt = qq{<line x1="%s" y1="%s" x2="%s" y2="%s" style="stroke:$fg;stroke-width:0.10;fill:$bg" />\n};
364 return unless $opt_svg;
365 push @cut_marks, sprintf($line_fmt, $x-5, $y-$font_b, $x+5, $y-$font_b);
366 push @cut_marks, sprintf($line_fmt, $x, $y-$font_b-5, $x, $y-$font_b+5);
370 $max_x += $max_len[$_] * $font_w foreach ( 0 .. 3 );
371 #cut_mark $max_x, $y;
374 my ($x,$y,$max_x) = @_;
375 push @cut_marks, sprintf($line_fmt, $x, $y-$font_b, $max_x, $y-$font_b);
379 my $last_cut_mark = 0;
382 my ( $from, $to ) = @_;
383 warn "# connector $from - $to ",dump( $line_parts[$from], $line_parts[$to] );
384 if ( $opt_vertical ) {
385 foreach my $i ( 0 .. int(($to-$from)/2) ) {
386 my $t = $line_parts[$from + $i];
387 $line_parts[$from + $i] = $line_parts[$to - $i];
388 $line_parts[$to - $i] = $t;
395 foreach my $i ( 0 .. $#line_parts ) {
396 next if $line_parts[$i]->[0] =~ m/^###/;
397 if (exists $line_parts[$i]->[1]) {
403 } elsif ($from && $to) {
404 connector $from => $to;
408 connector $from => $to if $from && $to;
410 foreach my $i ( 0 .. $#line_parts ) {
411 # $i = $#line_parts - $i if $opt_vertical;
412 my $line = $line_parts[$i];
416 # not a minimal two column pin description
417 if ( ! exists $line->[1] ) {
418 $last_cut_mark = 1 if $line->[0] =~ m/^##/; # skip comments
420 # before first empty line
421 if ( $last_cut_mark == 0 ) {
425 line $x, $y, $max_x if $opt_lines;
426 $y += 15; # make spacing between pinouts
428 } elsif ( $last_cut_mark ) {
434 #warn "CUTMARK no magic";
437 line $x, $y, $max_x if $opt_lines && exists $line->[1];
439 my ($fg,$bg) = @{ $cols->{txt} };
440 my $tspan = qq{<tspan x="$x" y="$y" style="line-height:2.54;fill:$fg;stroke:none;">\n};
443 foreach my $i ( 0 .. $#cols_order ) {
444 my $order = $cols_order[$i];
445 next unless $line->[$order];
447 my $text_anchor = 'middle';
448 my $len = $max_len[$i];
449 my $x2 = $x_pos + ( $len * $font_w ) / 2;
451 if ( $#$line == 0 && $line->[$order] =~ s/^#+s*// ) {
452 # comment, center over whole width
453 $len = length($line->[$order]);
454 $x2 = $x + (($max_x-$x)/2); # middle
455 $tspan .= qq{\t<tspan x="$x2" text-anchor="$text_anchor"}.sprintf( '>%' . $cols_align[$i] . $len . 's</tspan>', $line->[0]);
457 $tspan .= qq{\t<tspan x="$x2" text-anchor="$text_anchor"}.svg_style($line->[$order],$x_pos,$y,$i).sprintf( '>%' . $cols_align[$i] . $len . 's</tspan>', $line->[$order]);
459 $x_pos += $len * $font_w;
462 $tspan .= qq{\n</tspan>\n};
463 push @later,sprintf $tspan, @$line;
466 # swap pin colors for line stripe
468 swap_cols $_ foreach qw( pins txt );
475 if ( $#$line == 0 ) {
476 print $line->[0], "\n";
478 push @$line, '' while ($#$line < 3); # fill-in single row header
479 printf $fmt, map { $line->[$_] } @cols_order;
488 line $x, $y, $max_x if $opt_lines;
495 style="font-size:2.34px;line-height:2.54px;font-family:'Andale Mono';stroke:none"
496 xml:space="preserve">
500 print @later, qq{</text>\n}, @cut_marks, qq{</g>\n</svg>};
504 # you can add pin definitions below, but they should go into pins/