5 use Data::Dump qw(dump);
12 my $opt_horizontal = 0;
24 'invert!' => \$opt_invert,
25 'vertical-flip!' => \$opt_vertical,
26 'horizontal-flip!' => \$opt_horizontal,
27 'edge-pins!' => \$opt_edge,
28 'middle-pins!' => \$opt_middle,
29 'zebra!' => \$opt_zebra,
30 'lines!' => \$opt_lines,
31 'read=s' => \$opt_read,
32 'pins=s' => \$opt_pins,
33 'color' => \$opt_color,
34 'pinmux' => \$opt_pinmux,
38 my $font_w = 1.67; # < 2.54, font is not perfect square
39 my $font_b = 2.10; # font baseline position
41 $opt_read .= '/' unless $opt_read =~ m/\/$/;
44 open(my $fh, '<', $opt_read . shift);
51 my $model = slurp('/proc/device-tree/model');
52 $model =~ s/\x00$//; # strip kernel NULL
53 warn "# model [$model]";
56 open(DATA, '<', $opt_pins) if $opt_pins;
65 warn "MODEL [$1] == [$model] ?\n";
66 if ( $model =~ m/$1/ ) {
71 } elsif ( $include || $opt_pins ) {
72 push @{ $pins->{$1} }, $line_i while ( m/\t\s*(\w+\d+)/g );
78 warn "IGNORE: [$_]\n";
82 if ( ! $opt_pins && ! $pins ) {
84 $glob =~ s/^(\w+).*$/$1/;
85 my @pins = glob "pins/${glob}*";
86 warn "# possible pins: ",dump( \@pins );
91 die "add pin definition for # $model" unless $pins;
93 #warn "# lines ",dump( \@lines );
94 warn "# pins ",dump($pins);
98 glob($opt_read . '/sys/devices/platform/soc*/*.serial/tty/tty*'), # 4.x
99 glob( '/sys/devices/soc.*/*.uart/tty/tty*') # 3.10
101 my @v = split(/\//, $_);
102 $serial_tty->{ $v[-3] } = $v[-1];
104 warn "# serial_tty = ",dump($serial_tty);
113 my ($pin, $note) = @_;
114 if ( $pins->{$pin} ) {
115 foreach my $line ( @{$pins->{$pin}} ) {
116 my $t = $lines[$line];
120 $t =~ s/$pin/$pin $note/ || warn "can't find $pin in [$t]";
123 warn "# $line: $lines[$line]\n";
126 warn "IGNORED: pin $pin function $note\n";
130 open(my $fh, '<', $opt_read . '/sys/kernel/debug/pinctrl/pinctrl-maps');
133 if ( m/^device (\S+)/ ) {
135 if ( my $replace = $serial_tty->{$device} ) {
136 $device = $replace; # replace serial hex with kernel name
138 $device =~ s/^[0-9a-f]*\.//; # remove hex address
140 } elsif ( m/^group (\w+\d+)/ ) {
143 } elsif ( m/^function (\S+)/ ) {
146 if ( $device && $pin && $function ) {
147 push @{ $pin_function->{$pin} }, "$device $function";
149 annotate_pin $pin, "[$device $function]";
151 warn "missing one of ",dump( $device, $pin, $function );
161 warn "# pin_function = ",dump($pin_function);
164 # insert kernel gpio info
166 open(my $pins_fh, '<', (glob "/sys/kernel/debug/pinctrl/*/pins")[0]);
168 if ( m/^pin (\d+) \(([^\)]+)\)/ ) {
169 $linux_gpio_name->{$1} = $2;
172 warn "# linux_gpio_name = ",dump( $linux_gpio_name );
176 open(my $gpio_fh, '<', '/sys/kernel/debug/gpio');
181 my @l = split(/\s*[\(\|\)]\s*/, $_);
182 warn "XXX ", dump( \@l );
183 if ( $l[0] =~ m/gpio-(\d+)/ ) {
184 my $pin = $linux_gpio_name->{$1} || die "can't find $1 in ",dump( $linux_gpio_name );
185 $gpio_debug->{ $pin } = $l[2];
186 $l[3] =~ s/\s\s+/ /g;
187 annotate_pin $pin, qq{"$l[2]" $l[3]};
192 warn "# gpio_debug = ",dump( $gpio_debug );
196 my $have_sunxi_pio = `which sunxi-pio`;
197 if ( $have_sunxi_pio ) {
199 open(my $pio, '-|', 'sunxi-pio -m print');
203 my @p = split(/\s+/,$_);
204 warn "# pio ",dump(\@p);
205 # annotate input 0 and output 1 pins
206 # annotate_pin $p[0], ( $p[1] ? 'O' : 'I' ) . ':' . $p[4] if $p[1] == 0 || $p[1] == 1;
208 annotate_pin $pin, join(' ',@p) if ! $opt_svg;
216 my $pinmux_path = (glob("/sys/kernel/debug/pinctrl/*/pinmux-functions"))[0];
217 if ( $opt_pinmux && -e $pinmux_path ) {
218 open(my $mux, '<', $pinmux_path);
221 if ( m/function: (\w+), groups = \[ (.*) \]/ ) {
222 my ( $func, $pins ) = ( $1, $2 );
223 foreach ( split(/\s+/,$pins) ) {
224 push @{ $pinmux->{$_} }, $func;
227 warn "IGNORED [$pinmux_path] [$_]\n";
231 foreach my $pin ( keys %$pinmux ) {
232 if ( exists $pins->{$pin} ) {
233 annotate_pin $pin, '{' . join(' ', @{$pinmux->{$pin}}) . '}';
235 warn "IGNORED mux on $pin\n";
239 warn "# pinmux = ",dump( $pinmux );
244 my @max_len = ( 0,0,0,0 );
247 shift(@lines) while ( ! $lines[0] ); # remove empty at beginning
248 pop(@lines) while ( ! $lines[-1] ); # remove empty at end
250 foreach my $line (@lines) {
251 if ( $line =~ m/^#/ ) {
252 push @line_parts, [ $line ] unless $opt_svg && $line =~ m/^###+/; # SVG doesn't display 3rd level comments
255 $line =~ s/\[(\w+)\s+(\w+)\] \[\1\s+(\w+)\]/[$1 $2 $3]/g; # compress kernel annotation with same prefix
256 $line =~ s/(\[(?:uart\d*|serial|tty\w+))([^\t]*\]\s[^\t]*(rx|tx)d?)/$1 $3$2/gi;
257 $line =~ s/(\[i2c)([^\t]*\]\s[^\t]*(scl?k?|sda))/$1 $3$2/gi;
258 $line =~ s/(\[spi)([^\t]*\]\s[^\t]*(miso|mosi|s?clk|c[se]\d*))/$1 $3$2/gi;
259 $line =~ s/\s*\([^\)]+\)//g if ! $opt_alt;
261 # shorten duplicate kernel device/function
262 $line =~ s/\[serial (\w+) (uart\d+)\]/[$2 $1]/g;
263 $line =~ s/\[(\w+) (\w+) \1(\d+)\]/[$1$3 $2]/g;
265 $line =~ s/\[(\w+)\s+([^\]]+)\s+\1\]/[$1 $2]/g; # duplicate
267 my @v = split(/\s*\t+\s*/,$line,4);
268 @v = ( $v[2], $v[3], $v[0], $v[1] ) if $opt_horizontal && $v[2];
270 push @line_parts, [ @v ];
271 foreach my $i ( 0 .. 3 ) {
272 next unless exists $v[$i];
273 next if $v[$i] =~ m/^#/; # don't calculate comments into max length
274 my $l = length($v[$i]);
275 $max_len[$i] = $l if $l > $max_len[$i];
279 warn "# max_len = ",dump( \@max_len );
280 warn "# line_parts = ",dump( \@line_parts );
282 #print "$_\n" foreach @lines;
288 print qq{<?xml version="1.0" encoding="UTF-8" standalone="no"?>
290 xmlns:dc="http://purl.org/dc/elements/1.1/"
291 xmlns:cc="http://creativecommons.org/ns#"
292 xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
293 xmlns:svg="http://www.w3.org/2000/svg"
294 xmlns="http://www.w3.org/2000/svg"
295 xmlns:xlink="http://www.w3.org/1999/xlink"
298 viewBox="0 0 210 297"
305 }; # svg, insert rest of rect
307 print qq{<rect x="0" y="0" width="210" height="297" style="fill:#000000" id="high-contrast"/>} if $opt_invert;
312 my $cols = { # foreground background
313 txt => [ '#000000', '#ffffff' ],
314 pins => [ '#ffffff', '#ff00ff' ],
315 vcc => [ '#ff0000', '#ffff00' ],
316 gnd => [ '#000000', '#00ffff' ],
317 i2c => [ '#008800', '#ffcccc' ],
318 serial=>[ '#000088', '#ccffcc' ],
319 spi => [ '#880000', '#ccccff' ],
324 die "$swap not found in ",dump($cols) unless $cols->{$swap};
325 my ( $c1, $c2 ) = @{ $cols->{$swap} };
326 $cols->{$swap} = [ $c2, $c1 ];
329 swap_cols 'txt' if $opt_invert;
333 my ($name,$x,$y,$col) = @_;
335 return '' unless $opt_color;
337 $y -= $font_b; # shift box overlay to right vertical position based on font baseline
340 my ($x,$y,$col,$fill) = @_;
341 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};
345 if ( $name =~ m/^(\d+)$/ ) { # pins
347 my ( $fg, $bg ) = @{ $cols->{pins} };
349 my $w = $max_len[$col]*$font_w - 0.1;
352 #print qq{<polygon points="$x,$y $cx,$y $x,$cy $x,$y" stroke="$fg" stroke-width="0.25" fill="$bg" />};
353 #print qq{<polygon points="$x,$cy $cx,$cy $cx,$y $x,$cy" stroke="$bg" stroke-width="0.25" fill="$fg" />};
354 print qq{<rect x="$x" y="$y" width="$w" height="2.54" stroke="$fg" stroke-width="0.3" fill="$bg" />};
355 my ( $fg, $bg ) = @{ $cols->{txt} };
356 print qq{<rect x="$x" y="$y" width="$w" height="2.54" rx="1" ry="1" stroke="$fg" stroke-width="0.3" fill="$bg" />};
360 return qq{ style="fill:$bg"};
363 if ( $name =~ m/(VCC|3V3|3.3V|5v)/i ) {
364 my ($fg,$bg) = @{ $cols->{vcc} };
366 return qq{ style="fill:$fg"};
367 } elsif ( $name =~ m/(G(ND|Round)|VSS|0v)/i ) {
368 my ($fg,$bg) = @{ $cols->{gnd} };
370 return qq{ style="fill:$fg"};
371 } elsif ( $name =~ m/\[(\w+)/ ) { # kernel
373 my ($fg,$bg) = @{ $cols->{txt} };
374 $dev = 'serial' if $dev =~ m/^tty/;
375 ($fg,$bg) = @{ $cols->{$dev} } if exists $cols->{$dev};
377 return qq{ style="fill:$fg"};
379 my ( $fg, $bg ) = @{ $cols->{txt} };
381 #return qq{ style="fill:$fg"};
388 my @cols_order = ( 0,1,2,3 );
389 my @cols_align = ( '','-','','-' ); # sprintf prefix
391 my @cols_shuffle = @cols_order;
394 # pins outside on the right
395 @cols_shuffle = ( 0,1,3,2 ) if $opt_edge;
396 @cols_align = ( '-','-','','' );
397 } elsif ( $opt_middle ) {
399 @cols_shuffle = ( 1,0,2,3 );
400 @cols_align = ( '','','-','-' );
404 my ( $what, $order ) = @_;
406 foreach my $i ( 0 .. $#$what ) {
407 $new->[$i] = $what->[ $order->[$i] ];
409 warn "# cols_shuffle what=",dump($what)," order=",dump($order)," new=",dump($new);
413 @cols_order = cols_shuffle( \@cols_order, \@cols_shuffle );
414 @max_len = cols_shuffle( \@max_len, \@cols_shuffle );
416 warn "# cols_order = ",dump( \@cols_order );
417 warn "# cols_align = ",dump( \@cols_align );
419 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";
423 my ($fg,$bg) = @{ $cols->{txt} };
424 my $line_fmt = qq{<line x1="%s" y1="%s" x2="%s" y2="%s" style="stroke:$fg;stroke-width:0.10;fill:$bg" />\n};
429 return unless $opt_svg;
430 push @cut_marks, sprintf($line_fmt, $x-5, $y-$font_b, $x+5, $y-$font_b);
431 push @cut_marks, sprintf($line_fmt, $x, $y-$font_b-5, $x, $y-$font_b+5);
435 $max_x += $max_len[$_] * $font_w foreach ( 0 .. 3 );
436 #cut_mark $max_x, $y;
439 my ($x,$y,$max_x) = @_;
440 push @cut_marks, sprintf($line_fmt, $x, $y-$font_b, $max_x, $y-$font_b);
444 my $last_cut_mark = 0;
447 my ( $from, $to ) = @_;
448 warn "# connector $from - $to ",dump( $line_parts[$from], $line_parts[$to] );
449 if ( $opt_vertical ) {
450 foreach my $i ( 0 .. int(($to-$from)/2) ) {
451 my $t = $line_parts[$from + $i];
452 $line_parts[$from + $i] = $line_parts[$to - $i];
453 $line_parts[$to - $i] = $t;
460 foreach my $i ( 0 .. $#line_parts ) {
461 next if $line_parts[$i]->[0] =~ m/^###/;
462 if (exists $line_parts[$i]->[1]) {
468 } elsif ($from && $to) {
469 connector $from => $to;
473 connector $from => $to if $from && $to;
475 foreach my $i ( 0 .. $#line_parts ) {
476 # $i = $#line_parts - $i if $opt_vertical;
477 my $line = $line_parts[$i];
481 # not a minimal two column pin description
482 if ( ! exists $line->[1] ) {
483 $last_cut_mark = 1 if $line->[0] =~ m/^##/; # skip comments
485 # before first empty line
486 if ( $last_cut_mark == 0 ) {
490 line $x, $y, $max_x if $opt_lines;
491 $y += 15; # make spacing between pinouts
493 } elsif ( $last_cut_mark ) {
499 #warn "CUTMARK no magic";
502 line $x, $y, $max_x if $opt_lines && exists $line->[1];
504 my ($fg,$bg) = @{ $cols->{txt} };
505 my $tspan = qq{<tspan x="$x" y="$y" style="line-height:2.54;fill:$fg;stroke:none;">\n};
508 foreach my $i ( 0 .. $#cols_order ) {
509 my $order = $cols_order[$i];
510 next unless $line->[$order];
512 my $text_anchor = 'middle';
513 my $len = $max_len[$i];
514 my $x2 = $x_pos + ( $len * $font_w ) / 2;
516 if ( $#$line == 0 && $line->[$order] =~ s/^#+s*// ) {
517 # comment, center over whole width
518 $len = length($line->[$order]);
519 $x2 = $x + (($max_x-$x)/2); # middle
520 $tspan .= qq{\t<tspan x="$x2" text-anchor="$text_anchor"}.sprintf( '>%' . $cols_align[$i] . $len . 's</tspan>', $line->[0]);
522 $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]);
524 $x_pos += $len * $font_w;
527 $tspan .= qq{\n</tspan>\n};
528 push @later,sprintf $tspan, @$line;
531 # swap pin colors for line stripe
533 swap_cols $_ foreach qw( pins txt );
540 if ( $#$line == 0 ) {
541 print $line->[0], "\n";
543 push @$line, '' while ($#$line < 3); # fill-in single row header
544 printf $fmt, map { $line->[$_] } @cols_order;
553 line $x, $y, $max_x if $opt_lines;
560 style="font-size:2.34px;line-height:2.54px;font-family:'Andale Mono';stroke:none"
561 xml:space="preserve">
565 print @later, qq{</text>\n}, @cut_marks, qq{</g>\n</svg>};
569 # you can add pin definitions below, but they should go into pins/