]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/adafruit_blinka/microcontroller/starfive/JH7110/pin.py
Update pin.py
[Adafruit_Blinka-hackapet.git] / src / adafruit_blinka / microcontroller / starfive / JH7110 / pin.py
1 # SPDX-FileCopyrightText: 2024 Vladimir Shtarev, Jetbrains Research
2 #
3 # SPDX-License-Identifier: MIT
4 """A Pin class for use with StarFive JH7110."""
5
6 import VisionFive.gpio as GPIO
7
8 GPIO.setmode(GPIO.BOARD)
9
10
11 class Pin:
12     """Pins don't exist in CPython so...lets make our own!"""
13
14     IN = 0
15     OUT = 1
16     LOW = 0
17     HIGH = 1
18     PULL_NONE = 0
19     PULL_UP = 1
20     PULL_DOWN = 2
21
22     id = None
23     _value = LOW
24     _mode = IN
25
26     def __init__(self, number):
27         self.id = number
28
29     def __repr__(self):
30         return str(self.id)
31
32     def __eq__(self, other):
33         return self.id == other
34
35     def init(self, mode=IN, pull=None):
36         """Initialize the Pin"""
37         print(self.id)
38         if mode is not None:
39             if mode == self.IN:
40                 self._mode = self.IN
41                 GPIO.setup(self.id, GPIO.IN)
42             elif mode == self.OUT:
43                 self._mode = self.OUT
44                 GPIO.setup(self.id, GPIO.OUT)
45             else:
46                 raise RuntimeError("Invalid mode for pin: %s" % self.id)
47         if pull is not None:
48             if self._mode != self.IN:
49                 raise RuntimeError("Cannot set pull resistor on output")
50             if pull == self.PULL_UP:
51                 GPIO.setup(self.id, GPIO.IN, pull_up_down=GPIO.PUD_UP)
52             elif pull == self.PULL_DOWN:
53                 GPIO.setup(self.id, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
54             else:
55                 raise RuntimeError("Invalid pull for pin: %s" % self.id)
56
57     def value(self, val=None):
58         """Set or return the Pin Value"""
59         if val is not None:
60             if val == self.LOW:
61                 self._value = val
62                 GPIO.output(self.id, val)
63             elif val == self.HIGH:
64                 self._value = val
65                 GPIO.output(self.id, val)
66             else:
67                 raise RuntimeError("Invalid value for pin")
68             return None
69         return GPIO.input(self.id)
70
71
72 D7 = Pin(7)
73 D11 = Pin(11)
74 D12 = Pin(12)
75 D13 = Pin(13)
76 D15 = Pin(15)
77 D16 = Pin(16)
78 D18 = Pin(18)
79 D22 = Pin(22)
80 D24 = Pin(24)
81 D26 = Pin(26)
82 D27 = Pin(27)
83 D28 = Pin(28)
84 D29 = Pin(29)
85 D31 = Pin(31)
86 D35 = Pin(35)
87 D36 = Pin(36)
88 D37 = Pin(37)
89 D38 = Pin(38)
90 D40 = Pin(40)
91 # I2C
92 I2C_SDA = Pin(3)
93 I2C_SCL = Pin(5)
94
95 # SPI
96 SPI_MISO = Pin(21)
97 SPI_MOSI = Pin(19)
98 SPI_SCLK = Pin(23)
99
100 # UART
101 UART_TX = Pin(8)
102 UART_RX = Pin(10)
103
104 # PWM, does not support pwmio
105 PWM1 = Pin(32)
106 PWM2 = Pin(33)
107
108 # ordered as i2cId, SCL, SDA
109 i2cPorts = ((0, I2C_SCL, I2C_SDA),)
110
111 # ordered as spiId, sckId, mosiId, misoId
112 spiPorts = ((0, SPI_SCLK, SPI_MOSI, SPI_MISO),)
113
114 # ordered as uartId, txId, rxId
115 uartPorts = ((0, UART_TX, UART_RX),)