]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/adafruit_blinka/microcontroller/tegra/t210/pin.py
8e3ad394963d3ef1ab1f07469aaffd4445e3dc2b
[Adafruit_Blinka-hackapet.git] / src / adafruit_blinka / microcontroller / tegra / t210 / pin.py
1 import sys
2 import atexit
3 import Jetson.GPIO as GPIO
4 GPIO.setmode(GPIO.TEGRA_SOC)
5 GPIO.setwarnings(False)   # shh!
6
7 # Pins dont exist in CPython so...lets make our own!
8 class Pin:
9     IN = 0
10     OUT = 1
11     LOW = 0
12     HIGH = 1
13     PULL_NONE = 0
14     PULL_UP = 1
15     PULL_DOWN = 2
16
17     id = None
18     _value = LOW
19     _mode = IN
20
21     def __init__(self, bcm_number):
22         self.id = bcm_number
23
24     def __repr__(self):
25         return str(self.id)
26
27     def __eq__(self, other):
28         return self.id == other
29
30     def init(self, mode=IN, pull=None):
31         if mode != None:
32             if mode == self.IN:
33                 self._mode = self.IN
34                 GPIO.setup(self.id, GPIO.IN)
35             elif mode == self.OUT:
36                 self._mode = self.OUT
37                 GPIO.setup(self.id, GPIO.OUT)
38             else:
39                 raise RuntimeError("Invalid mode for pin: %s" % self.id)
40         if pull != None:
41             if self._mode != self.IN:
42                 raise RuntimeError("Cannot set pull resistor on output")
43             if pull == self.PULL_UP:
44                 GPIO.setup(self.id, GPIO.IN, pull_up_down=GPIO.PUD_UP)
45             elif pull == self.PULL_DOWN:
46                 GPIO.setup(self.id, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
47             else:
48                 raise RuntimeError("Invalid pull for pin: %s" % self.id)
49
50     def value(self, val=None):
51         if val != None:
52             if val == self.LOW:
53                 self._value = val
54                 GPIO.output(self.id, val)
55             elif val == self.HIGH:
56                 self._value = val
57                 GPIO.output(self.id, val)
58             else:
59                 raise RuntimeError("Invalid value for pin")
60         else:
61             return GPIO.input(self.id)
62
63     @atexit.register
64     def cleanup():
65         print("Exiting... \nCleaning up pins")
66         GPIO.cleanup()
67
68 # Cannot be used as GPIO
69 # before #
70 #SDA = Pin('GEN1_I2C_SDA')
71 #SCL = Pin('GEN1_I2C_SCL')
72 #SDA_1 = Pin('GEN2_I2C_SDA')
73 #SCL_1 = Pin('GEN2_I2C_SCL')
74
75 # after #
76 SDA = Pin('GEN2_I2C_SDA')
77 SCL = Pin('GEN2_I2C_SCL')
78 SDA_1 = Pin('GEN1_I2C_SDA')
79 SCL_1 = Pin('GEN1_I2C_SCL')
80
81 # These pins are native to TX1
82 BB03 = Pin('GPIO_X1_AUD')
83 X02 = Pin('MOTION_INT')
84 H07 = Pin('AP_WAKE_NFC')
85 E04 = Pin('DMIC3_CLK')
86 U03 = Pin('UART1_CTS')
87 U02 = Pin('UART1_RTS')
88 B03 = Pin('DAP1_SCLK')
89 B00 = Pin('DAP1_FS')
90 B01 = Pin('DAP1_DIN')
91 B02 = Pin('DAP1_DOUT')
92 P17 = Pin('GPIO_EXP_P17')
93 E05 = Pin('DMIC3_DAT')
94 X00 = Pin('MODEM_WAKE_AP')
95 P16 = Pin('GPIO_EXP_P16')
96 X03 = Pin('ALS_PROX_INT')
97
98 # These pins are native to NANO
99 S05 = Pin('CAM_AF_EN')
100 Z00 = Pin('GPIO_PZ0')
101 V00 = Pin('LCD_BL_PW')
102 G03 = Pin('UART2_CTS')
103 G02 = Pin('UART2_RTS')
104 J07 = Pin('DAP4_SCLK')
105 J04 = Pin('DAP4_FS')
106 J05 = Pin('DAP4_DIN')
107 J06 = Pin('DAP4_DOUT')
108 Y02 = Pin('LCD_TE')
109 DD00 = Pin('SPI2_CS1')
110 B07 = Pin('SPI2_CS0')
111 B05 = Pin('SPI2_MISO')
112 B04 = Pin('SPI2_MOSI')
113 B06 = Pin('SPI2_SCK')
114
115 # These pins are shared across T210
116 BB00 = Pin('AUD_MCLK')
117 C04 = Pin('SPI1_CS1')
118 C03 = Pin('SPI1_CS0')
119 C01 = Pin('SPI1_MISO')
120 C00 = Pin('SPI1_MOSI')
121 C02 = Pin('SPI1_SCK')
122 E06 = Pin('GPIO_PE6')
123
124 i2cPorts = (
125     (0, SCL, SDA), (1, SCL_1, SDA_1),
126 )
127
128 # ordered as spiId, sckId, mosiId, misoId
129 spiPorts = ((0, C02, C00, C01), (1, B06, B04, B05))