Python adafruit_bus_device.spi_device.SPIDevice() Examples

The following are 9 code examples of adafruit_bus_device.spi_device.SPIDevice(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module adafruit_bus_device.spi_device , or try the search function .
Example #1
Source File: adafruit_esp32spi.py    From Adafruit_CircuitPython_ESP32SPI with MIT License 6 votes vote down vote up
def __init__(
        self, spi, cs_pin, ready_pin, reset_pin, gpio0_pin=None, *, debug=False
    ):
        self._debug = debug
        self.set_psk = False
        self.set_crt = False
        self._buffer = bytearray(10)
        self._pbuf = bytearray(1)  # buffer for param read
        self._sendbuf = bytearray(256)  # buffer for command sending
        self._socknum_ll = [[0]]  # pre-made list of list of socket #

        self._spi_device = SPIDevice(spi, cs_pin, baudrate=8000000)
        self._cs = cs_pin
        self._ready = ready_pin
        self._reset = reset_pin
        self._gpio0 = gpio0_pin
        self._cs.direction = Direction.OUTPUT
        self._ready.direction = Direction.INPUT
        self._reset.direction = Direction.OUTPUT
        if self._gpio0:
            self._gpio0.direction = Direction.INPUT
        self.reset()

    # pylint: enable=too-many-arguments 
Example #2
Source File: spi.py    From Adafruit_CircuitPython_PN532 with MIT License 5 votes vote down vote up
def __init__(self, spi, cs_pin, *, irq=None, reset=None, debug=False):
        """Create an instance of the PN532 class using SPI"""
        self.debug = debug
        self._irq = irq
        self._spi = spi_device.SPIDevice(spi, cs_pin)
        super().__init__(debug=debug, reset=reset) 
Example #3
Source File: adafruit_sdcard.py    From Adafruit_CircuitPython_SD with MIT License 5 votes vote down vote up
def __init__(self, spi, cs, baudrate=1320000):
        # This is the init baudrate.
        # We create a second device with the target baudrate after card initialization.
        self._spi = spi_device.SPIDevice(spi, cs, baudrate=250000, extra_clocks=8)

        self._cmdbuf = bytearray(6)
        self._single_byte = bytearray(1)

        # Card is byte addressing, set to 1 if addresses are per block
        self._cdv = 512

        # initialise the card and switch to high speed
        self._init_card(baudrate) 
Example #4
Source File: rgb.py    From Adafruit_CircuitPython_RGB_Display with MIT License 5 votes vote down vote up
def __init__(
        self,
        spi,
        dc,
        cs,
        rst=None,
        width=1,
        height=1,
        baudrate=12000000,
        polarity=0,
        phase=0,
        *,
        x_offset=0,
        y_offset=0,
        rotation=0
    ):
        self.spi_device = spi_device.SPIDevice(
            spi, cs, baudrate=baudrate, polarity=polarity, phase=phase
        )
        self.dc_pin = dc
        self.rst = rst
        self.dc_pin.switch_to_output(value=0)
        if self.rst:
            self.rst.switch_to_output(value=0)
            self.reset()
        self._X_START = x_offset  # pylint: disable=invalid-name
        self._Y_START = y_offset  # pylint: disable=invalid-name
        super().__init__(width, height, rotation)

    # pylint: enable-msg=too-many-arguments 
Example #5
Source File: adafruit_ssd1306.py    From Adafruit_CircuitPython_SSD1306 with MIT License 5 votes vote down vote up
def __init__(
        self,
        width,
        height,
        spi,
        dc,
        reset,
        cs,
        *,
        external_vcc=False,
        baudrate=8000000,
        polarity=0,
        phase=0
    ):
        self.rate = 10 * 1024 * 1024
        dc.switch_to_output(value=0)
        self.spi_device = spi_device.SPIDevice(
            spi, cs, baudrate=baudrate, polarity=polarity, phase=phase
        )
        self.dc_pin = dc
        self.buffer = bytearray((height // 8) * width)
        super().__init__(
            memoryview(self.buffer),
            width,
            height,
            external_vcc=external_vcc,
            reset=reset,
        ) 
Example #6
Source File: adafruit_lsm9ds1.py    From Adafruit_CircuitPython_LSM9DS1 with MIT License 5 votes vote down vote up
def __init__(self, spi, xgcs, mcs):
        self._mag_device = spi_device.SPIDevice(
            spi, mcs, baudrate=200000, phase=1, polarity=1
        )
        self._xg_device = spi_device.SPIDevice(
            spi, xgcs, baudrate=200000, phase=1, polarity=1
        )
        super().__init__() 
Example #7
Source File: mcp3xxx.py    From Adafruit_CircuitPython_MCP3xxx with MIT License 5 votes vote down vote up
def __init__(self, spi_bus, cs, ref_voltage=3.3):
        self._spi_device = SPIDevice(spi_bus, cs)
        self._out_buf = bytearray(3)
        self._in_buf = bytearray(3)
        self._ref_voltage = ref_voltage 
Example #8
Source File: adafruit_sdcard.py    From Adafruit_CircuitPython_SD with MIT License 4 votes vote down vote up
def _init_card(self, baudrate):
        """Initialize the card in SPI mode."""
        # clock card at least cycles with cs high
        self._clock_card(80)

        with self._spi as card:
            # CMD0: init card; should return _R1_IDLE_STATE (allow 5 attempts)
            for _ in range(5):
                if self._cmd(card, 0, 0, 0x95) == _R1_IDLE_STATE:
                    break
            else:
                raise OSError("no SD card")

            # CMD8: determine card version
            rb7 = bytearray(4)
            r = self._cmd(card, 8, 0x01AA, 0x87, rb7, data_block=False)
            if r == _R1_IDLE_STATE:
                self._init_card_v2(card)
            elif r == (_R1_IDLE_STATE | _R1_ILLEGAL_COMMAND):
                self._init_card_v1(card)
            else:
                raise OSError("couldn't determine SD card version")

            # get the number of sectors
            # CMD9: response R2 (R1 byte + 16-byte block read)
            csd = bytearray(16)
            if self._cmd(card, 9, 0, 0xAF, response_buf=csd) != 0:
                raise OSError("no response from SD card")
            # self.readinto(csd)
            csd_version = (csd[0] & 0xC0) >> 6
            if csd_version >= 2:
                raise OSError("SD card CSD format not supported")

            if csd_version == 1:
                self._sectors = ((csd[8] << 8 | csd[9]) + 1) * 1024
            else:
                block_length = 2 ** (csd[5] & 0xF)
                c_size = ((csd[6] & 0x3) << 10) | (csd[7] << 2) | ((csd[8] & 0xC) >> 6)
                mult = 2 ** (((csd[9] & 0x3) << 1 | (csd[10] & 0x80) >> 7) + 2)
                self._sectors = block_length // 512 * mult * (c_size + 1)

            # CMD16: set block length to 512 bytes
            if self._cmd(card, 16, 512, 0x15) != 0:
                raise OSError("can't set 512 block size")

        # set to high data rate now that it's initialised
        self._spi = spi_device.SPIDevice(
            self._spi.spi, self._spi.chip_select, baudrate=baudrate, extra_clocks=8
        ) 
Example #9
Source File: apa102.py    From apa102-pi with GNU General Public License v2.0 4 votes vote down vote up
def __init__(self, num_led=8, global_brightness=31,
                 order='rgb', mosi=10, sclk=11, ce=None, bus_speed_hz=8000000):
        """Initializes the library

        :param num_led: Number of LEDs in the strip
        :param global_brightness: Overall brightness
        :param order: Order in which the colours are addressed (this differs from strip to strip)
        :param mosi: Master Out pin. Use 10 for SPI0, 20 for SPI1, any GPIO pin for bitbang.
        :param sclk: Clock, use 11 for SPI0, 21 for SPI1, any GPIO pin for bitbang.
        :param ce: GPIO to use for Chip select. Can be any free GPIO pin. Warning: This will slow down the bus
                   significantly. Note: The hardware CE0 and CE1 are not used
        :param bus_speed_hz: Speed of the hardware SPI bus. If glitches on the bus are visible, lower the value.
        """
        self.num_led = num_led
        order = order.lower()  # Just in case someone use CAPS here.
        self.rgb = RGB_MAP.get(order, RGB_MAP['rgb'])
        self.global_brightness = global_brightness
        self.use_bitbang = False  # Two raw SPI devices exist: Bitbang (software) and hardware SPI.
        self.use_ce = False  # If true, use the BusDevice abstraction layer on top of the raw SPI device

        self.leds = [self.LED_START, 0, 0, 0] * self.num_led  # Pixel buffer
        if ce is not None:
            # If a chip enable value is present, use the Adafruit CircuitPython BusDevice abstraction on top
            # of the raw SPI device (hardware or bitbang)
            # The next line is just here to prevent an "unused" warning from the IDE
            digitalio.DigitalInOut(board.D1)
            # Convert the chip enable pin number into an object (reflection à la Python)
            ce = eval("digitalio.DigitalInOut(board.D"+str(ce)+")")
            self.use_ce = True
        # Heuristic: Test for the hardware SPI pins. If found, use hardware SPI, otherwise bitbang SPI
        if mosi == 10:
            if sclk != 11:
                raise ValueError("Illegal MOSI / SCLK combination")
            self.spi = busio.SPI(clock=board.SCLK, MOSI=board.MOSI)
        elif mosi == 20:
            if sclk != 21:
                raise ValueError("Illegal MOSI / SCLK combination")
            self.spi = busio.SPI(clock=board.SCLK_1, MOSI=board.MOSI_1)
        else:
            # Use Adafruit CircuitPython BitBangIO, because the pins do not match one of the hardware SPI devices
            # Reflection à la Python to get at the digital IO pins
            self.spi = bitbangio.SPI(clock=eval("board.D"+str(sclk)), MOSI=eval("board.D"+str(mosi)))
            self.use_bitbang = True
        # Add the BusDevice on top of the raw SPI
        if self.use_ce:
            self.spibus = SPIDevice(spi=self.spi,  chip_select=ce, baudrate=bus_speed_hz)
        else:
            # If the BusDevice is not used, the bus speed is set here instead
            while not self.spi.try_lock():
                pass
            self.spi.configure(baudrate=bus_speed_hz)
            self.spi.unlock()
        # Debug
        if self.use_ce:
            print("Use software chip enable")
        if self.use_bitbang:
            print("Use bitbang SPI")
        else:
            print("Use hardware SPI")