]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/adafruit_blinka/microcontroller/generic_micropython/spi.py
reformat
[Adafruit_Blinka-hackapet.git] / src / adafruit_blinka / microcontroller / generic_micropython / spi.py
1 # SPDX-FileCopyrightText: 2021 Melissa LeBlanc-Williams for Adafruit Industries
2 #
3 # SPDX-License-Identifier: MIT
4 """SPI Class for Generic MicroPython"""
5 from machine import SPI as _SPI
6
7 # pylint: disable=protected-access, no-self-use
8 class SPI:
9     """SPI Class for Generic MicroPython"""
10
11     MSB = _SPI.MSB
12     LSB = _SPI.LSB
13
14     def __init__(self, portId, baudrate=100000):
15         self._frequency = baudrate
16         self._spi = _SPI(portId)
17
18     # pylint: disable=too-many-arguments,unused-argument
19     def init(
20         self,
21         baudrate=1000000,
22         polarity=0,
23         phase=0,
24         bits=8,
25         firstbit=_SPI.MSB,
26     ):
27         """Initialize the Port"""
28         self._frequency = baudrate
29         self._spi.init(
30             baudrate=baudrate,
31             polarity=polarity,
32             phase=phase,
33             bits=bits,
34             firstbit=firstbit,
35             mode=_SPI.MASTER,
36         )
37
38     # pylint: enable=too-many-arguments
39
40     @property
41     def frequency(self):
42         """Return the current frequency"""
43         return self._frequency
44
45     def write(self, buf, start=0, end=None):
46         """Write data from the buffer to SPI"""
47         self._spi.write(buf)
48
49     def readinto(self, buf, start=0, end=None, write_value=0):
50         """Read data from SPI and into the buffer"""
51         self._spi.readinto(buf)
52
53     # pylint: disable=too-many-arguments
54     def write_readinto(
55         self, buffer_out, buffer_in, out_start=0, out_end=None, in_start=0, in_end=None
56     ):
57         """Perform a half-duplex write from buffer_out and then
58         read data into buffer_in
59         """
60         self._spi.write_readinto(
61             buffer_out,
62             buffer_in,
63             out_start=out_start,
64             out_end=out_end,
65             in_start=in_start,
66             in_end=in_end,
67         )
68
69     # pylint: enable=too-many-arguments