]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/adafruit_blinka/microcontroller/generic_linux/i2c.py
Merge branch 'main' of https://github.com/adafruit/Adafruit_Blinka into vivid-unit2
[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
6 import warnings
7 from Adafruit_PureIO import smbus
8
9
10 class I2C:
11     """
12     I2C class
13
14     Baudrate has no effect on Linux systems. The argument is only there for compatibility.
15     """
16
17     MASTER = 0
18     SLAVE = 1
19     _baudrate = None
20     _mode = None
21     _i2c_bus = None
22
23     # pylint: disable=unused-argument
24     def __init__(self, bus_num, mode=MASTER, baudrate=None):
25         if mode != self.MASTER:
26             raise NotImplementedError("Only I2C Master supported!")
27         _mode = self.MASTER
28
29         if baudrate is not None:
30             warnings.warn(
31                 "I2C frequency is not settable in python, ignoring!", RuntimeWarning
32             )
33
34         try:
35             self._i2c_bus = smbus.SMBus(bus_num)
36         except FileNotFoundError:
37             raise RuntimeError(
38                 "I2C Bus #%d not found, check if enabled in config!" % bus_num
39             ) from RuntimeError
40
41     # pylint: enable=unused-argument
42
43     def scan(self):
44         """Try to read a byte from each address, if you get an OSError
45         it means the device isnt there"""
46         found = []
47         for addr in range(0, 0x80):
48             try:
49                 self._i2c_bus.read_byte(addr)
50             except OSError:
51                 continue
52             found.append(addr)
53         return found
54
55     # pylint: disable=unused-argument
56     def writeto(self, address, buffer, *, start=0, end=None, stop=True):
57         """Write data from the buffer to an address"""
58         if end is None:
59             end = len(buffer)
60         self._i2c_bus.write_bytes(address, buffer[start:end])
61
62     def readfrom_into(self, address, buffer, *, start=0, end=None, stop=True):
63         """Read data from an address and into the buffer"""
64         if end is None:
65             end = len(buffer)
66
67         readin = self._i2c_bus.read_bytes(address, end - start)
68         for i in range(end - start):
69             buffer[i + start] = readin[i]
70
71     # pylint: enable=unused-argument
72
73     def writeto_then_readfrom(
74         self,
75         address,
76         buffer_out,
77         buffer_in,
78         *,
79         out_start=0,
80         out_end=None,
81         in_start=0,
82         in_end=None,
83         stop=False,
84     ):
85         """Write data from buffer_out to an address and then
86         read data from an address and into buffer_in
87         """
88         if out_end is None:
89             out_end = len(buffer_out)
90         if in_end is None:
91             in_end = len(buffer_in)
92         if stop:
93             # To generate a stop in linux, do in two transactions
94             self.writeto(address, buffer_out, start=out_start, end=out_end, stop=True)
95             self.readfrom_into(address, buffer_in, start=in_start, end=in_end)
96         else:
97             # To generate without a stop, do in one block transaction
98             readin = self._i2c_bus.read_i2c_block_data(
99                 address, buffer_out[out_start:out_end], in_end - in_start
100             )
101             for i in range(in_end - in_start):
102                 buffer_in[i + in_start] = readin[i]