]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/adafruit_blinka/microcontroller/tegra/t194/pin.py
84972b42993a5d311068bf293ea5cc3e000ca0ab
[Adafruit_Blinka-hackapet.git] / src / adafruit_blinka / microcontroller / tegra / t194 / pin.py
1 import sys
2 import atexit
3 sys.path.append("/opt/nvidia/jetson-gpio/lib/python")
4 sys.path.append("/opt/nvidia/jetson-gpio/lib/python/Jetson/GPIO")
5 import Jetson.GPIO as GPIO
6 GPIO.setmode(GPIO.TEGRA_SOC)
7 GPIO.setwarnings(False)   # shh!
8
9 # Pins dont exist in CPython so...lets make our own!
10 class Pin:
11     IN = 0
12     OUT = 1
13     LOW = 0
14     HIGH = 1
15     PULL_NONE = 0
16     PULL_UP = 1
17     PULL_DOWN = 2
18
19     id = None
20     _value = LOW
21     _mode = IN
22
23     def __init__(self, bcm_number):
24         self.id = bcm_number
25
26     def __repr__(self):
27         return str(self.id)
28
29     def __eq__(self, other):
30         return self.id == other
31
32     def init(self, mode=IN, pull=None):
33         if mode != None:
34             if mode == self.IN:
35                 self._mode = self.IN
36                 GPIO.setup(self.id, GPIO.IN)
37             elif mode == self.OUT:
38                 self._mode = self.OUT
39                 GPIO.setup(self.id, GPIO.OUT)
40             else:
41                 raise RuntimeError("Invalid mode for pin: %s" % self.id)
42         if pull != None:
43             if self._mode != self.IN:
44                 raise RuntimeError("Cannot set pull resistor on output")
45             if pull == self.PULL_UP:
46                 GPIO.setup(self.id, GPIO.IN, pull_up_down=GPIO.PUD_UP)
47             elif pull == self.PULL_DOWN:
48                 GPIO.setup(self.id, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
49             else:
50                 raise RuntimeError("Invalid pull for pin: %s" % self.id)
51
52     def value(self, val=None):
53         if val != None:
54             if val == self.LOW:
55                 self._value = val
56                 GPIO.output(self.id, val)
57             elif val == self.HIGH:
58                 self._value = val
59                 GPIO.output(self.id, val)
60             else:
61                 raise RuntimeError("Invalid value for pin")
62         else:
63             return GPIO.input(self.id)
64
65     @atexit.register
66     def cleanup():
67         print("Exiting... \nCleaning up pins")
68         GPIO.cleanup()
69
70 # Cannot be used as GPIO
71 SDA = Pin('DP_AUX_CH3_N')
72 SCL = Pin('DP_AUX_CH3_P')
73 SDA_1 = Pin('GEN2_I2C_SDA')
74 SCL_1 = Pin('GEN2_I2C_SCL')
75
76 Q06 = Pin('SOC_GPIO42')
77 AA03 = Pin('CAN0_DIN')
78 AA02 = Pin('CAN0_DOUT')
79 Z07 = Pin('SPI1_CS1_N')
80 Z06 = Pin('SPI1_CS0_N')
81 Z04 = Pin('SPI1_MISO')
82 Z05 = Pin('SPI1_MOSI')
83 Z03 = Pin('SPI1_SCK')
84 BB01 = Pin('CAN1_EN')
85 AA00 = Pin('CAN1_DOUT')
86 R05 = Pin('UART1_CTS')
87 R04 = Pin('UART1_RTS')
88 H07 = Pin('DAP2_SCLK')
89 I02 = Pin('DAP2_FS')
90 I01 = Pin('DAP2_DIN')
91 I00 = Pin('DAP2_DOUT')
92 N01 = Pin('SOC_GPIO54')
93 BB00 = Pin('CAN1_STB')
94 H00 = Pin('SOC_GPIO12')
95 Q01 = Pin('SOC_GPIO21')
96 AA01 = Pin('CAN1_DIN')
97 R00 = Pin('SOC_GPIO44')
98
99 i2cPorts = (
100     (8, SCL, SDA), (1, SCL_1, SDA_1),
101 )
102
103 # ordered as spiId, sckId, mosiId, misoId
104 spiPorts = ((0, Z03, Z05, Z04), )