]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/adafruit_blinka/microcontroller/ft232h/pin.py
Add support for ftdi URLs in the BLINKA_FT232H variable.
[Adafruit_Blinka-hackapet.git] / src / adafruit_blinka / microcontroller / ft232h / pin.py
1 """FT232H pin names"""
2
3 from adafruit_blinka.microcontroller.ft232h.url import get_ftdi_url
4
5 class Pin:
6     """A basic Pin class for use with FT232H."""
7
8     IN = 0
9     OUT = 1
10     LOW = 0
11     HIGH = 1
12     PULL_NONE = 0
13     PULL_UP = 1
14     PULL_DOWN = 2
15
16     ft232h_gpio = None
17
18     def __init__(self, pin_id=None):
19         # setup GPIO controller if not done yet
20         # use one provided by I2C as default
21         if not Pin.ft232h_gpio:
22             # pylint: disable=import-outside-toplevel
23             from pyftdi.i2c import I2cController
24
25             # pylint: enable=import-outside-toplevel
26
27             i2c = I2cController()
28             i2c.configure(get_ftdi_url())
29             Pin.ft232h_gpio = i2c.get_gpio()
30         # check if pin is valid
31         if pin_id:
32             if Pin.ft232h_gpio.all_pins & 1 << pin_id == 0:
33                 raise ValueError("Can not use pin {} as GPIO.".format(pin_id))
34         # ID is just bit position
35         self.id = pin_id
36
37     def init(self, mode=IN, pull=None):
38         """Initialize the Pin"""
39         if not self.id:
40             raise RuntimeError("Can not init a None type pin.")
41         # FT232H does't have configurable internal pulls?
42         if pull:
43             raise NotImplementedError("Internal pull up/down not currently supported.")
44         pin_mask = Pin.ft232h_gpio.pins | 1 << self.id
45         current = Pin.ft232h_gpio.direction
46         if mode == self.OUT:
47             current |= 1 << self.id
48         else:
49             current &= ~(1 << self.id)
50         Pin.ft232h_gpio.set_direction(pin_mask, current)
51
52     def value(self, val=None):
53         """Set or return the Pin Value"""
54         if not self.id:
55             raise RuntimeError("Can not access a None type pin.")
56         current = Pin.ft232h_gpio.read(with_output=True)
57         # read
58         if val is None:
59             return 1 if current & 1 << self.id != 0 else 0
60         # write
61         if val in (self.LOW, self.HIGH):
62             if val == self.HIGH:
63                 current |= 1 << self.id
64             else:
65                 current &= ~(1 << self.id)
66             # must mask out any input pins
67             Pin.ft232h_gpio.write(current & Pin.ft232h_gpio.direction)
68             return None
69         # release the kraken
70         raise RuntimeError("Invalid value for pin")
71
72
73 # create pin instances for each pin
74 # D0 to D3 are used by I2C/SPI
75 D4 = Pin(4)
76 D5 = Pin(5)
77 D6 = Pin(6)
78 D7 = Pin(7)
79 C0 = Pin(8)
80 C1 = Pin(9)
81 C2 = Pin(10)
82 C3 = Pin(11)
83 C4 = Pin(12)
84 C5 = Pin(13)
85 C6 = Pin(14)
86 C7 = Pin(15)
87 # C8 and C9 are not GPIO
88
89 # create None type pins for I2C and SPI since they are expected to be defined
90 SCL = Pin()
91 SDA = Pin()
92 SCK = SCLK = Pin()
93 MOSI = Pin()
94 MISO = Pin()