"""The ATmega328P breakout board of the first KiCad lesson, written as a SKiDL program.

Every part and every connection of the drawn schematic (cad/kicad/atmega-th), with the same
references, values, footprints and net names, so the two netlists can be compared pin by pin
(compare_netlists.py). Checked with SKiDL 3.0 and the KiCad 10 libraries.

Run:  python atmega328p_breakout.py      ->  atmega328p_breakout.net
"""
from skidl import Net, Part, Pin, ERC, generate_netlist, set_default_tool, KICAD10

set_default_tool(KICAD10)

R_FP = 'Resistor_THT:R_Axial_DIN0207_L6.3mm_D2.5mm_P7.62mm_Horizontal'
C_FP = 'Capacitor_THT:C_Disc_D5.0mm_W2.5mm_P2.50mm'
CP_FP = 'Capacitor_THT:CP_Radial_D5.0mm_P2.50mm'


def cap(ref, value, a, b, fp=C_FP, kind='C'):
    c = Part('Device', kind, ref=ref, value=value, footprint=fp)
    c[1] += a          # for C_Polarized pin 1 is +
    c[2] += b
    return c


# ---- nets
vcc, gnd, vin, pwr_in = Net('+5V'), Net('GND'), Net('VIN'), Net('PWR+')
reset = Net('RESET')
for n in (vcc, gnd, vin, pwr_in):
    n.drive = Pin.drives.POWER   # fed from outside the sheet: the PWR_FLAG of the drawn schematic

# ---- power: jack and screw terminal, reverse-polarity diode, regulator
j1 = Part('Connector', 'Barrel_Jack_Switch', ref='J1', footprint='Connector_BarrelJack:BarrelJack_Horizontal')
j1[1] += pwr_in        # tip
j1[2] += gnd           # sleeve
j1[3] += NC            # pin 3 (switch) is left open, as in the drawn schematic
j6 = Part('Connector', 'Screw_Terminal_01x02', ref='J6',
          footprint='TerminalBlock_Phoenix:TerminalBlock_Phoenix_MKDS-1,5-2-5.08_1x02_P5.08mm_Horizontal')
j6[1] += pwr_in
j6[2] += gnd
d3 = Part('Device', 'D_Schottky', ref='D3', value='1N5817', footprint='Diode_THT:D_DO-41_SOD81_P10.16mm_Horizontal')
d3['A'] += pwr_in
d3['K'] += vin
u2 = Part('Regulator_Linear', 'LM7805_TO220', ref='U2', footprint='Package_TO_SOT_THT:TO-220-3_Vertical')
u2['VI'] += vin
u2['GND'] += gnd
u2['VO'] += vcc
cap('C5', '10uF 25V', vin, gnd, CP_FP, 'C_Polarized')
cap('C6', '10uF 16V', vcc, gnd, CP_FP, 'C_Polarized')

# ---- the chip, its clock and its supply capacitors
u1 = Part('MCU_Microchip_ATmega', 'ATmega328P-P', ref='U1', footprint='Package_DIP:DIP-28_W7.62mm_Socket')
u1['VCC'] += vcc
u1['AVCC'] += vcc
u1['GND'] += gnd       # both GND pins, 8 and 22
xtal1, xtal2 = Net('XTAL1'), Net('XTAL2')
u1['XTAL1/PB6'] += xtal1
u1['XTAL2/PB7'] += xtal2
y1 = Part('Device', 'Crystal', ref='Y1', value='16MHz', footprint='Crystal:Crystal_HC49-4H_Vertical')
y1[1] += xtal2
y1[2] += xtal1
cap('C1', '22pF', xtal1, gnd)
cap('C2', '22pF', xtal2, gnd)
cap('C3', '100nF', vcc, gnd)               # VCC, pin 7
cap('C4', '100nF', vcc, gnd)               # AVCC, pin 20
aref = Net('AREF')
u1['AREF'] += aref
cap('C7', '100nF', aref, gnd)

# ---- reset
u1['~{RESET}/PC6'] += reset
r1 = Part('Device', 'R', ref='R1', value='10k', footprint=R_FP)
r1[1] += vcc
r1[2] += reset
sw1 = Part('Switch', 'SW_Push', ref='SW1', footprint='Button_Switch_THT:SW_PUSH_6mm')
sw1[1] += gnd
sw1[2] += reset

# ---- named signal nets, one per chip pin that leaves the board
sig = {}
for name in ['PD2', 'PD3', 'PD4', 'PD5', 'PD6', 'PD7', 'PB0', 'PB1', 'PB2',
             'PC0', 'PC1', 'PC2', 'PC3', 'PC4', 'PC5']:
    sig[name] = Net(name)
    u1[name] += sig[name]
for net_name, pin in [('RXD', 'PD0'), ('TXD', 'PD1'), ('MOSI', 'PB3'), ('MISO', 'PB4'), ('SCK', 'PB5')]:
    sig[net_name] = Net(net_name)
    u1[pin] += sig[net_name]

# ---- ISP header (AVR-ISP-6: 1 MISO, 2 VCC, 3 SCK, 4 MOSI, 5 RESET, 6 GND)
j2 = Part('Connector', 'AVR-ISP-6', ref='J2', footprint='Connector_PinHeader_2.54mm:PinHeader_2x03_P2.54mm_Vertical')
for pin, net in zip(range(1, 7), [sig['MISO'], vcc, sig['SCK'], sig['MOSI'], reset, gnd]):
    j2[pin] += net

# ---- FTDI header and the auto-reset capacitor
j5 = Part('Connector', 'Conn_01x06_Pin', ref='J5', footprint='Connector_PinHeader_2.54mm:PinHeader_1x06_P2.54mm_Vertical')
dtr = Net('DTR')
for pin, net in zip(range(1, 7), [gnd, gnd, vcc, sig['RXD'], sig['TXD'], dtr]):
    j5[pin] += net     # cable TXD (pin 4) drives the chip's RXD
cap('C8', '100nF', dtr, reset)

# ---- LEDs: power (green) and user (red, PB5 = Arduino D13)
for rref, dref, colour, src in [('R2', 'D1', 'GREEN', vcc), ('R3', 'D2', 'RED', sig['SCK'])]:
    a = Net('LED_' + colour)
    r = Part('Device', 'R', ref=rref, value='330', footprint=R_FP)
    d = Part('Device', 'LED', ref=dref, value=colour, footprint='LED_THT:LED_D3.0mm')
    r[1] += src
    r[2] += a
    d['A'] += a        # LED pin 2 is the anode, pin 1 the cathode
    d['K'] += gnd

# ---- the two 14-way sockets, in the order of the lesson's table
j3 = Part('Connector', 'Conn_01x14_Pin', ref='J3', footprint='Connector_PinSocket_2.54mm:PinSocket_1x14_P2.54mm_Vertical')
j4 = Part('Connector', 'Conn_01x14_Pin', ref='J4', footprint='Connector_PinSocket_2.54mm:PinSocket_1x14_P2.54mm_Vertical')
for i, name in enumerate(['RXD', 'TXD', 'PD2', 'PD3', 'PD4', 'PD5', 'PD6', 'PD7',
                          'PB0', 'PB1', 'PB2', 'MOSI', 'MISO', 'SCK'], 1):
    j3[i] += sig[name]
for i, net in enumerate([sig['PC0'], sig['PC1'], sig['PC2'], sig['PC3'], sig['PC4'], sig['PC5'],
                         aref, vcc, reset, vin, vcc, gnd, vcc, gnd], 1):
    j4[i] += net

ERC()
generate_netlist(file_='atmega328p_breakout.net')
