]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/adafruit_blinka/microcontroller/tegra/t264/pin.py
add support for jetson thor
[Adafruit_Blinka-hackapet.git] / src / adafruit_blinka / microcontroller / tegra / t264 / pin.py
1 # SPDX-FileCopyrightText: 2025 Gautham Srinivasan for NVIDIA
2 #
3 # SPDX-License-Identifier: MIT
4
5 """Tegra T264 pin names"""
6 import atexit
7 from Jetson import GPIO
8
9 GPIO.setmode(GPIO.TEGRA_SOC)
10 GPIO.setwarnings(False)  # shh!
11
12
13 class Pin:
14     """Pins dont exist in CPython so...lets make our own!"""
15
16     IN = 0
17     OUT = 1
18     LOW = 0
19     HIGH = 1
20     PULL_NONE = 0
21     PULL_UP = 1
22     PULL_DOWN = 2
23
24     id = None
25     _value = LOW
26     _mode = IN
27
28     def __init__(self, bcm_number):
29         self.id = bcm_number
30
31     def __repr__(self):
32         return str(self.id)
33
34     def __eq__(self, other):
35         return self.id == other
36
37     def init(self, mode=IN, pull=None):
38         """Initialize the Pin"""
39         if mode is not None:
40             if mode == self.IN:
41                 self._mode = self.IN
42                 GPIO.setup(self.id, GPIO.IN)
43             elif mode == self.OUT:
44                 self._mode = self.OUT
45                 GPIO.setup(self.id, GPIO.OUT)
46             else:
47                 raise RuntimeError("Invalid mode for pin: %s" % self.id)
48         if pull is not None:
49             if self._mode != self.IN:
50                 raise RuntimeError("Cannot set pull resistor on output")
51             if pull == self.PULL_UP:
52                 GPIO.setup(self.id, GPIO.IN, pull_up_down=GPIO.PUD_UP)
53             elif pull == self.PULL_DOWN:
54                 GPIO.setup(self.id, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
55             else:
56                 raise RuntimeError("Invalid pull for pin: %s" % self.id)
57
58     def value(self, val=None):
59         """Set or return the Pin Value"""
60         if val is not None:
61             if val == self.LOW:
62                 self._value = val
63                 GPIO.output(self.id, val)
64                 return None
65             if val == self.HIGH:
66                 self._value = val
67                 GPIO.output(self.id, val)
68                 return None
69             raise RuntimeError("Invalid value for pin")
70         return GPIO.input(self.id)
71
72     # pylint: disable=no-method-argument
73     @atexit.register
74     def cleanup():
75         """Clean up pins"""
76         print("Exiting... \nCleaning up pins")
77         GPIO.cleanup()
78
79     # pylint: enable=no-method-argument
80
81
82 # Cannot be used as GPIO
83 SDA = Pin("GP16_I2C8_DAT")  # I2C4
84 SCL = Pin("GP81_I2C9_CLK")
85 SDA_1 = Pin("GP14_I2C2_DAT")  # I2C2
86 SCL_1 = Pin("GP13_I2C2_CLK")
87
88 # Jetson Thor
89 L06 = Pin("GP130")
90 M04 = Pin("GP136_UART9_RTS_N")
91 V06 = Pin("GP184_DAP2_CLK")
92 M00 = Pin("GP132_PWM9")
93 F07 = Pin("GP257_PWM2")
94 DD03 = Pin("GP21")
95 U07 = Pin("GP177")
96 K01 = Pin("GP117_SPI1_MOSI")
97 K00 = Pin("GP116_SPI1_MISO")
98 U00 = Pin("GP170")
99 J07 = Pin("GP115_SPI1_CLK")
100 K02 = Pin("GP118_SPI1_CS0_N")
101 K03 = Pin("GP119_SPI1_CS1_N")
102 AD01 = Pin("GP211_CAN2_DIN")
103 AD00 = Pin("GP210_CAN2_DOUT")
104 DD04 = Pin("GGP22_SOCKET_ID_STRA")
105 AE00 = Pin("GP215_CAN3_DOUT")
106 W01 = Pin("GP187_DAP2_FS")
107 M05 = Pin("GP137_UART9_CTS_N")
108 AE01 = Pin("GP216_CAN3_DIN")
109 W00 = Pin("GP186_DAP2_DIN")
110 V07 = Pin("GP185_DAP2_DOUT")
111
112 i2cPorts = (
113     (7, SCL, SDA),
114     (1, SCL_1, SDA_1),
115 )
116
117 # ordered as spiId, sckId, mosiId, misoId
118 spiPorts = ((K02, J07, K01, K00),)