Last active
January 16, 2022 05:56
-
-
Save ciniml/2fbab7f93cb01cd75d6ebc2767c1fa6e to your computer and use it in GitHub Desktop.
pin_table_gen.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import svgwrite | |
pin_type_colors = { | |
'gnd': (0x000000, 0xffffff), | |
'3v3': (0xc00000, 0xffffff), | |
'5v': (0xc00000, 0xffffff), | |
'bat': (0xc0c000, 0x000000), | |
'hpwr': (0xc0a000, 0x000000), | |
'i2c': (0x003000, 0x000000), | |
'input_only': (0x005000, 0x000000), | |
'misc': (0xc0c0c0, 0x000000), | |
'normal': (0xffffff, 0x000000), | |
} | |
usage_type_colors = { | |
'ADC': (0xffc0c0, 0xffffff), | |
'SPI': (0x2080ff, 0x000000), | |
'I2C': (0xff1010, 0xffffff), | |
'UART': (0x20a020, 0xffffff), | |
'GPIO': (0xc08080, 0x000000), | |
'I2S': (0xff8080, 0x000000), | |
'DAC': (0xff8080, 0xffffff), | |
'RST': (0xc0c0c0, 0x000000), | |
} | |
pin_definitions = { | |
'GND': {'type': 'gnd'}, | |
'3V3': {'type': '3v3'}, | |
'5V': {'type': '5v'}, | |
'BAT': {'type': 'bat'}, | |
'HPWR': {'type': 'hpwr'}, | |
# ADC pins | |
'G35': {'type': 'input_only', 'usage': 'ADC' , 'usage_type': 'ADC'}, | |
'G36': {'type': 'input_only', 'usage': 'ADC' , 'usage_type': 'ADC'}, | |
# DAC pins | |
'G25': {'type': 'normal', 'usage': 'DAC/SPK' , 'usage_type': 'DAC'}, | |
'G26': {'type': 'normal', 'usage': 'DAC' , 'usage_type': 'DAC'}, | |
# SPI pins | |
'G23': {'type': 'normal', 'usage': 'MOSI', 'usage_type': 'SPI'}, | |
'G19': {'type': 'normal', 'usage': 'MISO', 'usage_type': 'SPI'}, | |
'G18': {'type': 'normal', 'usage': 'SCK', 'usage_type': 'SPI'}, | |
# UART0 pins | |
'G3': {'type': 'normal', 'usage': 'RXD0', 'usage_type': 'UART'}, | |
'G1': {'type': 'normal', 'usage': 'TXD0', 'usage_type': 'UART'}, | |
# UART2 pins | |
'G16': {'type': 'normal', 'usage': 'RXD2', 'usage_type': 'UART'}, | |
'G17': {'type': 'normal', 'usage': 'TXD2', 'usage_type': 'UART'}, | |
# I2C pins (internal) | |
'G21': {'type': 'normal', 'usage': 'SDA', 'usage_type': 'I2C'}, | |
'G22': {'type': 'normal', 'usage': 'SCL', 'usage_type': 'I2C'}, | |
# GPIO pins | |
'G2': {'type': 'normal', 'usage': 'GPIO', 'usage_type': 'GPIO'}, | |
'G5': {'type': 'normal', 'usage': 'GPIO', 'usage_type': 'GPIO'}, | |
# I2S pins | |
'G12': {'type': 'normal', 'usage': 'I2S SK', 'usage_type': 'I2S'}, | |
'G13': {'type': 'normal', 'usage': 'I2S WS', 'usage_type': 'I2S'}, | |
'G15': {'type': 'normal', 'usage': 'I2SOUT', 'usage_type': 'I2S'}, | |
'G0': {'type': 'normal', 'usage': 'I2S MK', 'usage_type': 'I2S'}, | |
'G34': {'type': 'normal', 'usage': 'I2S IN', 'usage_type': 'I2S'}, | |
'EN': {'type': 'misc', 'usage': 'RST', 'usage_type': 'RST'}, | |
} | |
pin_map = ( | |
('GND', 'G35'), | |
('GND', 'G36'), | |
('GND', 'EN'), | |
('G23', 'G25'), | |
('G19', 'G26'), | |
('G18', '3V3'), | |
('G3', 'G1'), | |
('G16', 'G17'), | |
('G21', 'G22'), | |
('G2', 'G5'), | |
('G12', 'G13'), | |
('G15', 'G0'), | |
('HPWR', 'G34'), | |
('HPWR', '5V'), | |
('HPWR', 'BAT'), | |
) | |
drawing = svgwrite.Drawing() | |
column_width = 120 | |
column_usage_width = 80 | |
row_height = 20 | |
y = 0 | |
for row_index, row in enumerate(pin_map): | |
y = row_height * row_index | |
for column_index, pin in enumerate(row): | |
x = column_width * column_index | |
pin_definition = pin_definitions.get(pin, {'type': 'normal'}) | |
pin_type = pin_definition['type'] | |
pin_usage = pin_definition.get('usage') | |
pin_usage_type = pin_definition.get('usage_type') | |
pin_color = pin_type_colors.get(pin_type, (0x000000, 0xffffff)) | |
fill = f'#{pin_color[0]:06X}' | |
text_color = f'#{pin_color[1]:06X}' | |
if pin_usage is None: | |
rect = drawing.rect(insert=(x, y), size=(column_width, row_height), fill=fill) | |
drawing.add(rect) | |
text = drawing.text(pin, insert=(x+column_width/2, y+row_height/2), style='text-anchor:middle; dominant-baseline:central', fill=text_color) | |
drawing.add(text) | |
else: | |
pin_start_x = x if column_index == 0 else x + column_usage_width | |
pin_column_width = column_width - column_usage_width | |
usage_start_x = x + column_width - column_usage_width if column_index == 0 else x | |
rect = drawing.rect(insert=(pin_start_x, y), size=(pin_column_width, row_height), fill=fill) | |
drawing.add(rect) | |
text = drawing.text(pin, insert=(pin_start_x+pin_column_width/2, y+row_height/2), style='text-anchor:middle; dominant-baseline:central', fill=text_color) | |
drawing.add(text) | |
usage_color = usage_type_colors[pin_usage_type] | |
usage_fill = f'#{usage_color[0]:06X}' | |
usage_text_color = f'#{usage_color[1]:06X}' | |
rect = drawing.rect(insert=(usage_start_x, y), size=(column_usage_width, row_height), fill=usage_fill) | |
drawing.add(rect) | |
text = drawing.text(pin_usage, insert=(usage_start_x+column_usage_width/2, y+row_height/2), style='text-anchor:middle; dominant-baseline:central', fill=usage_text_color) | |
drawing.add(text) | |
drawing.saveas('pindef.svg', pretty=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment