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