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 my $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 open(my $pio, '-|', 'sunxi-pio -m print');
165 my @p = split(/\s+/,$_);
166 warn "# pio ",dump(\@p);
167 # annotate input 0 and output 1 pins
168 # annotate_pin $p[0], ( $p[1] ? 'O' : 'I' ) . ':' . $p[4] if $p[1] == 0 || $p[1] == 1;
170 annotate_pin $pin, join(' ',@p) if ! $opt_svg;
174 my @max_len = ( 0,0,0,0 );
177 shift(@lines) while ( ! $lines[0] ); # remove empty at beginning
178 pop(@lines) while ( ! $lines[-1] ); # remove empty at end
180 foreach my $line (@lines) {
181 if ( $line =~ m/^#/ ) {
182 push @line_parts, [ $line ] unless $opt_svg && $line =~ m/^###+/; # SVG doesn't display 3rd level comments
185 $line =~ s/\[(\w+)\s+(\w+)\] \[\1\s+(\w+)\]/[$1 $2 $3]/g; # compress kernel annotation with same prefix
186 $line =~ s/(\[(?:uart\d*|serial|tty\w+))([^\t]*\]\s[^\t]*(rx|tx)d?)/$1 $3$2/gi;
187 $line =~ s/(\[i2c)([^\t]*\]\s[^\t]*(scl?k?|sda))/$1 $3$2/gi;
188 $line =~ s/(\[spi)([^\t]*\]\s[^\t]*(miso|mosi|s?clk|c[se]\d*))/$1 $3$2/gi;
189 $line =~ s/\s*\([^\)]+\)//g if ! $opt_alt;
191 # shorten duplicate kernel device/function
192 $line =~ s/\[serial (\w+) (uart\d+)\]/[$2 $1]/g;
193 $line =~ s/\[(\w+) (\w+) \1(\d+)\]/[$1$3 $2]/g;
195 $line =~ s/\[(\w+)\s+([^\]]+)\s+\1\]/[$1 $2]/g; # duplicate
197 my @v = split(/\s*\t+\s*/,$line,4);
198 @v = ( $v[2], $v[3], $v[0], $v[1] ) if $opt_horizontal && $v[2];
200 push @line_parts, [ @v ];
201 foreach my $i ( 0 .. 3 ) {
202 next unless exists $v[$i];
203 next if $v[$i] =~ m/^#/; # don't calculate comments into max length
204 my $l = length($v[$i]);
205 $max_len[$i] = $l if $l > $max_len[$i];
209 warn "# max_len = ",dump( \@max_len );
210 warn "# line_parts = ",dump( \@line_parts );
212 #print "$_\n" foreach @lines;
218 print qq{<?xml version="1.0" encoding="UTF-8" standalone="no"?>
220 xmlns:dc="http://purl.org/dc/elements/1.1/"
221 xmlns:cc="http://creativecommons.org/ns#"
222 xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
223 xmlns:svg="http://www.w3.org/2000/svg"
224 xmlns="http://www.w3.org/2000/svg"
225 xmlns:xlink="http://www.w3.org/1999/xlink"
228 viewBox="0 0 210 297"
235 }; # svg, insert rest of rect
237 print qq{<rect x="0" y="0" width="210" height="297" style="fill:#000000" id="high-contrast"/>} if $opt_invert;
242 my $cols = { # foreground background
243 txt => [ '#000000', '#ffffff' ],
244 pins => [ '#ffffff', '#ff00ff' ],
245 vcc => [ '#ff0000', '#ffff00' ],
246 gnd => [ '#000000', '#00ffff' ],
247 i2c => [ '#008800', '#ffcccc' ],
248 serial=>[ '#000088', '#ccffcc' ],
249 spi => [ '#880000', '#ccccff' ],
254 die "$swap not found in ",dump($cols) unless $cols->{$swap};
255 my ( $c1, $c2 ) = @{ $cols->{$swap} };
256 $cols->{$swap} = [ $c2, $c1 ];
259 swap_cols 'txt' if $opt_invert;
263 my ($name,$x,$y,$col) = @_;
265 return '' unless $opt_color;
267 $y -= $font_b; # shift box overlay to right vertical position based on font baseline
270 my ($x,$y,$col,$fill) = @_;
271 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};
275 if ( $name =~ m/^(\d+)$/ ) { # pins
277 my ( $fg, $bg ) = @{ $cols->{pins} };
279 my $w = $max_len[$col]*$font_w - 0.1;
282 #print qq{<polygon points="$x,$y $cx,$y $x,$cy $x,$y" stroke="$fg" stroke-width="0.25" fill="$bg" />};
283 #print qq{<polygon points="$x,$cy $cx,$cy $cx,$y $x,$cy" stroke="$bg" stroke-width="0.25" fill="$fg" />};
284 print qq{<rect x="$x" y="$y" width="$w" height="2.54" stroke="$fg" stroke-width="0.3" fill="$bg" />};
285 my ( $fg, $bg ) = @{ $cols->{txt} };
286 print qq{<rect x="$x" y="$y" width="$w" height="2.54" rx="1" ry="1" stroke="$fg" stroke-width="0.3" fill="$bg" />};
290 return qq{ style="fill:$bg"};
293 if ( $name =~ m/(VCC|3V3|3.3V|5v)/i ) {
294 my ($fg,$bg) = @{ $cols->{vcc} };
296 return qq{ style="fill:$fg"};
297 } elsif ( $name =~ m/(G(ND|Round)|VSS|0v)/i ) {
298 my ($fg,$bg) = @{ $cols->{gnd} };
300 return qq{ style="fill:$fg"};
301 } elsif ( $name =~ m/\[(\w+)/ ) { # kernel
303 my ($fg,$bg) = @{ $cols->{txt} };
304 $dev = 'serial' if $dev =~ m/^tty/;
305 ($fg,$bg) = @{ $cols->{$dev} } if exists $cols->{$dev};
307 return qq{ style="fill:$fg"};
309 my ( $fg, $bg ) = @{ $cols->{txt} };
311 #return qq{ style="fill:$fg"};
318 my @cols_order = ( 0,1,2,3 );
319 my @cols_align = ( '','-','','-' ); # sprintf prefix
321 my @cols_shuffle = @cols_order;
324 # pins outside on the right
325 @cols_shuffle = ( 0,1,3,2 ) if $opt_edge;
326 @cols_align = ( '-','-','','' );
327 } elsif ( $opt_middle ) {
329 @cols_shuffle = ( 1,0,2,3 );
330 @cols_align = ( '','','-','-' );
334 my ( $what, $order ) = @_;
336 foreach my $i ( 0 .. $#$what ) {
337 $new->[$i] = $what->[ $order->[$i] ];
339 warn "# cols_shuffle what=",dump($what)," order=",dump($order)," new=",dump($new);
343 @cols_order = cols_shuffle( \@cols_order, \@cols_shuffle );
344 @max_len = cols_shuffle( \@max_len, \@cols_shuffle );
346 warn "# cols_order = ",dump( \@cols_order );
347 warn "# cols_align = ",dump( \@cols_align );
349 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";
353 my ($fg,$bg) = @{ $cols->{txt} };
354 my $line_fmt = qq{<line x1="%s" y1="%s" x2="%s" y2="%s" style="stroke:$fg;stroke-width:0.10;fill:$bg" />\n};
359 return unless $opt_svg;
360 push @cut_marks, sprintf($line_fmt, $x-5, $y-$font_b, $x+5, $y-$font_b);
361 push @cut_marks, sprintf($line_fmt, $x, $y-$font_b-5, $x, $y-$font_b+5);
365 $max_x += $max_len[$_] * $font_w foreach ( 0 .. 3 );
366 #cut_mark $max_x, $y;
369 my ($x,$y,$max_x) = @_;
370 push @cut_marks, sprintf($line_fmt, $x, $y-$font_b, $max_x, $y-$font_b);
374 my $last_cut_mark = 0;
377 my ( $from, $to ) = @_;
378 warn "# connector $from - $to ",dump( $line_parts[$from], $line_parts[$to] );
379 if ( $opt_vertical ) {
380 foreach my $i ( 0 .. int(($to-$from)/2) ) {
381 my $t = $line_parts[$from + $i];
382 $line_parts[$from + $i] = $line_parts[$to - $i];
383 $line_parts[$to - $i] = $t;
390 foreach my $i ( 0 .. $#line_parts ) {
391 next if $line_parts[$i]->[0] =~ m/^###/;
392 if (exists $line_parts[$i]->[1]) {
398 } elsif ($from && $to) {
399 connector $from => $to;
403 connector $from => $to if $from && $to;
405 foreach my $i ( 0 .. $#line_parts ) {
406 # $i = $#line_parts - $i if $opt_vertical;
407 my $line = $line_parts[$i];
411 # not a minimal two column pin description
412 if ( ! exists $line->[1] ) {
413 $last_cut_mark = 1 if $line->[0] =~ m/^##/; # skip comments
415 # before first empty line
416 if ( $last_cut_mark == 0 ) {
420 line $x, $y, $max_x if $opt_lines;
421 $y += 15; # make spacing between pinouts
423 } elsif ( $last_cut_mark ) {
429 #warn "CUTMARK no magic";
432 line $x, $y, $max_x if $opt_lines && exists $line->[1];
434 my ($fg,$bg) = @{ $cols->{txt} };
435 my $tspan = qq{<tspan x="$x" y="$y" style="line-height:2.54;fill:$fg;stroke:none;">\n};
438 foreach my $i ( 0 .. $#cols_order ) {
439 my $order = $cols_order[$i];
440 next unless $line->[$order];
442 my $text_anchor = 'middle';
443 my $len = $max_len[$i];
444 my $x2 = $x_pos + ( $len * $font_w ) / 2;
446 if ( $#$line == 0 && $line->[$order] =~ s/^#+s*// ) {
447 # comment, center over whole width
448 $len = length($line->[$order]);
449 $x2 = $x + (($max_x-$x)/2); # middle
450 $tspan .= qq{\t<tspan x="$x2" text-anchor="$text_anchor"}.sprintf( '>%' . $cols_align[$i] . $len . 's</tspan>', $line->[0]);
452 $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]);
454 $x_pos += $len * $font_w;
457 $tspan .= qq{\n</tspan>\n};
458 push @later,sprintf $tspan, @$line;
461 # swap pin colors for line stripe
463 swap_cols $_ foreach qw( pins txt );
470 if ( $#$line == 0 ) {
471 print $line->[0], "\n";
473 push @$line, '' while ($#$line < 3); # fill-in single row header
474 printf $fmt, map { $line->[$_] } @cols_order;
483 line $x, $y, $max_x if $opt_lines;
490 style="font-size:2.34px;line-height:2.54px;font-family:'Andale Mono';stroke:none"
491 xml:space="preserve">
495 print @later, qq{</text>\n}, @cut_marks, qq{</g>\n</svg>};
499 # you can add pin definitions below, but they should go into pins/