]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/adafruit_blinka/microcontroller/generic_linux/i2c.py
Merge pull request #709 from makermelissa/libgpiod-fix
[Adafruit_Blinka-hackapet.git] / src / adafruit_blinka / microcontroller / generic_linux / i2c.py
1 # SPDX-FileCopyrightText: 2021 Melissa LeBlanc-Williams for Adafruit Industries
2 #
3 # SPDX-License-Identifier: MIT
4 """Generic Linux I2C class using PureIO's smbus class"""
5 from Adafruit_PureIO import smbus
6
7
8 class I2C:
9     """I2C class"""
10
11     MASTER = 0
12     SLAVE = 1
13     _baudrate = None
14     _mode = None
15     _i2c_bus = None
16
17     # pylint: disable=unused-argument
18     def __init__(self, bus_num, mode=MASTER, baudrate=None):
19         if mode != self.MASTER:
20             raise NotImplementedError("Only I2C Master supported!")
21         _mode = self.MASTER
22
23         # if baudrate != None:
24         #    print("I2C frequency is not settable in python, ignoring!")
25
26         try:
27             self._i2c_bus = smbus.SMBus(bus_num)
28         except FileNotFoundError:
29             raise RuntimeError(
30                 "I2C Bus #%d not found, check if enabled in config!" % bus_num
31             ) from RuntimeError
32
33     # pylint: enable=unused-argument
34
35     def scan(self):
36         """Try to read a byte from each address, if you get an OSError
37         it means the device isnt there"""
38         found = []
39         for addr in range(0, 0x80):
40             try:
41                 self._i2c_bus.read_byte(addr)
42             except OSError:
43                 continue
44             found.append(addr)
45         return found
46
47     # pylint: disable=unused-argument
48     def writeto(self, address, buffer, *, start=0, end=None, stop=True):
49         """Write data from the buffer to an address"""
50         if end is None:
51             end = len(buffer)
52         self._i2c_bus.write_bytes(address, buffer[start:end])
53
54     def readfrom_into(self, address, buffer, *, start=0, end=None, stop=True):
55         """Read data from an address and into the buffer"""
56         if end is None:
57             end = len(buffer)
58
59         readin = self._i2c_bus.read_bytes(address, end - start)
60         for i in range(end - start):
61             buffer[i + start] = readin[i]
62
63     # pylint: enable=unused-argument
64
65     def writeto_then_readfrom(
66         self,
67         address,
68         buffer_out,
69         buffer_in,
70         *,
71         out_start=0,
72         out_end=None,
73         in_start=0,
74         in_end=None,
75         stop=False,
76     ):
77         """Write data from buffer_out to an address and then
78         read data from an address and into buffer_in
79         """
80         if out_end is None:
81             out_end = len(buffer_out)
82         if in_end is None:
83             in_end = len(buffer_in)
84         if stop:
85             # To generate a stop in linux, do in two transactions
86             self.writeto(address, buffer_out, start=out_start, end=out_end, stop=True)
87             self.readfrom_into(address, buffer_in, start=in_start, end=in_end)
88         else:
89             # To generate without a stop, do in one block transaction
90             readin = self._i2c_bus.read_i2c_block_data(
91                 address, buffer_out[out_start:out_end], in_end - in_start
92             )
93             for i in range(in_end - in_start):
94                 buffer_in[i + in_start] = readin[i]