]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/adafruit_blinka/microcontroller/starfive/JH7110/pin.py
fixes
[Adafruit_Blinka-hackapet.git] / src / adafruit_blinka / microcontroller / starfive / JH7110 / pin.py
1 # SPDX-FileCopyrightText: 2024 Vladimir Shtarev
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 D3 = Pin(3)
73 D5 = Pin(5)
74 D7 = Pin(7)
75 D8 = Pin(9)
76 D10 = Pin(10)
77 D11 = Pin(11)
78 D12 = Pin(12)
79 D13 = Pin(13)
80 D15 = Pin(15)
81 D16 = Pin(16)
82 D18 = Pin(18)
83 D19 = Pin(19)
84 D21 = Pin(21)
85 D22 = Pin(22)
86 D23 = Pin(23)
87 D24 = Pin(24)
88 D26 = Pin(26)
89 D27 = Pin(27)
90 D28 = Pin(28)
91 D29 = Pin(29)
92 D31 = Pin(31)
93 D32 = Pin(32)
94 D33 = Pin(33)
95 D35 = Pin(35)
96 D36 = Pin(36)
97 D37 = Pin(37)
98 D38 = Pin(38)
99 D40 = Pin(40)
100 # I2C
101 I2C_SDA = D3
102 I2C_SCL = D5
103
104 # SPI
105 SPI_MISO = D21
106 SPI_MOSI = D19
107 SPI_SCLK = D23
108
109 # UART
110 UART_TX = D8
111 UART_RX = D10
112
113 # ordered as i2cId, SCL, SDA
114 i2cPorts = ((0, I2C_SCL, I2C_SDA),)
115
116 # ordered as spiId, sckId, mosiId, misoId
117 spiPorts = ((0, SPI_SCLK, SPI_MOSI, SPI_MISO),)
118
119 # ordered as uartId, txId, rxId
120 uartPorts = ((0, UART_TX, UART_RX),)