# Untitled

a guest
Oct 2nd, 2020
8
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. from skidl import *
2.
4. baseVoltage = 3.3
5.
6. gnd = Net('GND')
7. vdd = Net('VDD')
8.
9.
10. def decouple(pin, v="100u", footprint='C' + std_footprint):
11.   c = Part("Device",'C', value=v,footprint=footprint)
12.   c.ref = "C."+pin.part.ref + "." + pin.num
13.   c[1] += pin
14.   c[2] += gnd
15.
16. def connect2(pin1,pin2):
17.   if pin1.name in ["GND","VDD"]:
18.     n = Net.get(pin1.name)
19.   else:
20.     n = Net(pin1.name)
21.   n += pin1
22.   n += pin2
23.
24. def connect2part(part,pin1,pin2):
25.   connect2(pin1, part[1])
26.   connect2(pin2, part[2])
27.
28. def pullup(pin, r="1k", footprint='R' + std_footprint):
29.   return connect2part(Part("Device",'R', value=r,footprint=footprint),vdd,pin)
30.
31. def pulldown(pin, r="1k", footprint='R' + std_footprint):
32.   return connect2part(Part("Device",'R', value=r,footprint=footprint),gnd,pin)
33.
34. def connectAll(part, name, net, cb=lambda none:none):
35.   if part[name]:
36.     for pin in part[name]:
37.       pin += net
38.       cb(pin)
39.
40. def createPart(part, analog=False):
41.   connectAll(part, "VDD", vdd, decouple)
42.   connectAll(part, "VSS", gnd)
43.   connectAll(part, "GND", gnd)
44.   if analog:
45.     connectAll(part, "VDDA", vdd, lambda d: decouple(d,v="1u"))
46.     connectAll(part, "VSSA", gnd)
47.   return part
48.
49. def findConnect(pin,part):
50.    if part[pin.name]:
51.      net = Net(pin.name)
52.      net += pin
53.      net += part[pin.name]
54.
55. def connectSPI(master, slave, CS):
56.   connectPins(master, ["SCK","MISO","MOSI", CS.name], slave, ["SCK","MISO","MOSI", "CS"])
57.
58. def connectPins(part1, signals1, part2, signals2):
59.   for i, j in zip(signals1,signals2):
60.       n = Net(i)
61.       n += part1[i]
62.       n += part2[j]
63.
64. def bme280(type="SPI"):
65.   b = createPart(Part("Sensor", "BME280", footprint="Bosch_LGA-8_3x3mm_P0.8mm_ClockwisePinNumbering"))
66.   if type == "SPI":
67.     b.SDI.name = "MOSI"
68.     b.SDO.name = "MISO"
69.     b.CSB.name = "CS"
70.     return b
71.
72. def roundResistor(r, series=""):
73.   return int(round(r))
74.
75. def ledResistor(drivePin, I=20,Vf=1.3, V=baseVoltage, library="LED_SMD", footprint="LED_0603_1608Metric"):
76.   r=(V-Vf)/I*1000
77.   led = Part("Device", "LED", footprint=footprint)
78.   pulldown(led.K,roundResistor(r))
79.   connect2(drivePin, led.A)
80.   return led
81.
82. #createPart connects all GND and VCC pins and puts 100nF on each VCC Pin
83. mcu = createPart(Part("MCU_ST_STM32F1", "STM32F103C8Tx", footprint="Package_QFP:LQFP-48_7x7mm_P0.5mm"), True)
84.
85. #rename Pins for SPI use
86. mcu.PA5.name = "SCK"
87. mcu.PA6.name = "MISO"
88. mcu.PA7.name = "MOSI"
89.
90. #custom part, renames Pins for SPI and uses createPart
91. bme = bme280("SPI")
92.
93. #Connects all SPI signals
94. connectSPI(mcu,bme,CS=mcu.PA4)
95.