|
| 1 | +# The MIT License (MIT) |
| 2 | +# |
| 3 | +# Copyright (c) 2016 Scott Shawcroft for Adafruit Industries |
| 4 | +# |
| 5 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +# of this software and associated documentation files (the "Software"), to deal |
| 7 | +# in the Software without restriction, including without limitation the rights |
| 8 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +# copies of the Software, and to permit persons to whom the Software is |
| 10 | +# furnished to do so, subject to the following conditions: |
| 11 | +# |
| 12 | +# The above copyright notice and this permission notice shall be included in |
| 13 | +# all copies or substantial portions of the Software. |
| 14 | +# |
| 15 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 21 | +# THE SOFTWARE. |
| 22 | + |
| 23 | +class SPIDevice: |
| 24 | + """ |
| 25 | + Represents a single SPI device and manages locking the bus and the device |
| 26 | + address. |
| 27 | +
|
| 28 | + :param SPI spi: The SPI bus the device is on |
| 29 | + :param ~microcontroller.Pin chip_select: The chip select pin |
| 30 | +
|
| 31 | + Example:: |
| 32 | +
|
| 33 | + import nativeio |
| 34 | + from board import * |
| 35 | + from adafruit_bus_device.spi_device import I2CDevice |
| 36 | +
|
| 37 | + with nativeio.SPI(SCK, MOSI, MISO) as spi: |
| 38 | + device = SPIDevice(spi, D10) |
| 39 | + bytes_read = bytearray(4) |
| 40 | + with device: |
| 41 | + device.read(bytes_read) |
| 42 | + # A second transaction |
| 43 | + with device: |
| 44 | + device.write(bytes_read) |
| 45 | + """ |
| 46 | + def __init__(self, spi, chip_select, baudrate=100000, polarity=0, phase=0): |
| 47 | + self.spi = spi |
| 48 | + self.baudrate = baudrate |
| 49 | + self.polarity = polarity |
| 50 | + self.phase = phase |
| 51 | + self.chip_select = chip_select |
| 52 | + self.chip_select.switch_to_output(value=True) |
| 53 | + |
| 54 | + def read(self, buffer, **kwargs): |
| 55 | + """ |
| 56 | + Read into ``buffer`` from the device. The number of bytes read will be the |
| 57 | + length of ``buffer``. |
| 58 | +
|
| 59 | + If ``start`` or ``end`` is provided, then the buffer will be sliced |
| 60 | + as if ``buffer[start:end]``. This will not cause an allocation like |
| 61 | + ``buffer[start:end]`` will so it saves memory. |
| 62 | +
|
| 63 | + :param bytearray buffer: buffer to write into |
| 64 | + :param int start: Index to start writing at |
| 65 | + :param int end: Index to write up to but not include |
| 66 | + """ |
| 67 | + self.spi.read(buffer, **kwargs) |
| 68 | + |
| 69 | + def write(self, buffer, **kwargs): |
| 70 | + """ |
| 71 | + Write the bytes from ``buffer`` to the device. Transmits a stop bit if |
| 72 | + ``stop`` is set. |
| 73 | +
|
| 74 | + If ``start`` or ``end`` is provided, then the buffer will be sliced |
| 75 | + as if ``buffer[start:end]``. This will not cause an allocation like |
| 76 | + ``buffer[start:end]`` will so it saves memory. |
| 77 | +
|
| 78 | + :param bytearray buffer: buffer containing the bytes to write |
| 79 | + :param int start: Index to start writing from |
| 80 | + :param int end: Index to read up to but not include |
| 81 | + :param bool stop: If true, output an I2C stop condition after the |
| 82 | + buffer is written |
| 83 | + """ |
| 84 | + self.spi.write(buffer, **kwargs) |
| 85 | + |
| 86 | + def __enter__(self): |
| 87 | + while not self.spi.try_lock(): |
| 88 | + pass |
| 89 | + self.spi.configure(baudrate=self.baudrate, polarity=self.polarity, |
| 90 | + phase=self.phase) |
| 91 | + self.chip_select.value = False |
| 92 | + return self |
| 93 | + |
| 94 | + def __exit__(self, *exc): |
| 95 | + self.chip_select.value = True |
| 96 | + self.spi.unlock() |
| 97 | + return False |
0 commit comments