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