Python pyb.SPI Examples

The following are 10 code examples of pyb.SPI(). 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 pyb , or try the search function .
Example #1
Source File: ws2812.py    From micropython-ws2812 with MIT License 6 votes vote down vote up
def __init__(self, spi_bus=1, led_count=1, intensity=1):
        """
        Params:
        * spi_bus = SPI bus ID (1 or 2)
        * led_count = count of LEDs
        * intensity = light intensity (float up to 1)
        """
        self.led_count = led_count
        self.intensity = intensity

        # prepare SPI data buffer (4 bytes for each color)
        self.buf_length = self.led_count * 3 * 4
        self.buf = bytearray(self.buf_length)

        # SPI init
        self.spi = pyb.SPI(spi_bus, pyb.SPI.MASTER, baudrate=3200000, polarity=0, phase=1)

        # turn LEDs off
        self.show([]) 
Example #2
Source File: epdpart.py    From micropython-epaper with Apache License 2.0 6 votes vote down vote up
def _power_off(self):                       # turn of power and all signals
        self.Pin_RESET.low()
        self.Pin_PANEL_ON.low()
        self.Pin_BORDER.low()
        self.spi.deinit()
        self.Pin_SCK.init(mode = pyb.Pin.OUT_PP)
        self.Pin_SCK.low()
        self.Pin_MOSI.init(mode = pyb.Pin.OUT_PP)
        self.Pin_MOSI.low()
        # ensure SPI MOSI and CLOCK are Low before CS Low
        self.Pin_EPD_CS.low()
        # pulse discharge pin
        self.Pin_DISCHARGE.high()
        pyb.delay(150)
        self.Pin_DISCHARGE.low()

# USER INTERFACE
# clear_screen() calls clear_data() and, if show, EPD_clear()
# showdata() called from show() 
Example #3
Source File: epd.py    From micropython-epaper with Apache License 2.0 6 votes vote down vote up
def _power_off(self):                       # turn of power and all signals
        self.Pin_PANEL_ON.low()
#        self._SPI_send(b'\x00\x00')
        self.spi.deinit()
        self.Pin_SCK.init(mode = pyb.Pin.OUT_PP)
        self.Pin_SCK.low()
        self.Pin_MOSI.init(mode = pyb.Pin.OUT_PP)
        self.Pin_MOSI.low()
        self.Pin_BORDER.low()
        # ensure SPI MOSI and CLOCK are Low before CS Low
        self.Pin_RESET.low()
        self.Pin_EPD_CS.low()
        # pulse discharge pin
        self.Pin_DISCHARGE.high()
        pyb.delay(150)
        self.Pin_DISCHARGE.low()

# One frame of data is the number of lines * rows. For example:
# The 2.7” frame of data is 176 lines * 264 dots. 
Example #4
Source File: epd.py    From micropython-epaper with Apache License 2.0 6 votes vote down vote up
def _setbuf_fixed(self, line: int, fixed_value: int):
        index = 0                           # odd pixels
        buf = ptr8(self.linebuf)
        for b in range(BYTES_PER_LINE, 0, -1): # Optimisation: replacing for .. in range made trivial gains.
            buf[index] = fixed_value # Optimisation: buffer SPI data
            index += 1
                                            # scan line
        scan_pos = (LINES_PER_DISPLAY - line - 1) >> 2
        scan_shift = (line & 3) << 1 
        for b in range(BYTES_PER_SCAN):
            if scan_pos == b:
                buf[index] = 3 << scan_shift
            else:
                buf[index] = 0
            index += 1
        for b in range(BYTES_PER_LINE):     # Even pixels
            buf[index] = fixed_value
            index += 1 
Example #5
Source File: flash.py    From micropython-epaper with Apache License 2.0 6 votes vote down vote up
def cp(source, dest):
    if dest.endswith('/'):                      # minimal way to allow
        dest = ''.join((dest, source.split('/')[-1]))  # cp /sd/file /fc/
    with open(source, 'rb') as infile:          # Caller should handle any OSError
        with open(dest,'wb') as outfile:        # e.g file not found
            while True:
                buf = infile.read(100)
                outfile.write(buf)
                if len(buf) < 100:
                    break

# FlashClass
# The flash and epaper deviices use different SPI modes. You must issue objFlash.begin() before attempting to access
# the device, and objFlash.end() afterwards
# All address method arguments are byte addresses. Hence to erase block 100, issue objFlash.sector_erase(100*4096)
# Sectors must be erased (to 0xff) before they can be written. Writing is done in 256 byte pages. The _write() method
# handles paging transparently but does assume target pages are erased. 
Example #6
Source File: ssd1306.py    From micropython-drivers with MIT License 5 votes vote down vote up
def __init__(self, pinout, height=32, external_vcc=True, i2c_devid=DEVID):
    self.external_vcc = external_vcc
    self.height       = 32 if height == 32 else 64
    self.pages        = int(self.height / 8)
    self.columns      = 128

    # Infer interface type from entries in pinout{}
    if 'dc' in pinout:
      # SPI
      rate = 16 * 1024 * 1024
      self.spi = pyb.SPI(2, pyb.SPI.MASTER, baudrate=rate, polarity=1, phase=0)  # SCK: Y6: MOSI: Y8
      self.dc  = pyb.Pin(pinout['dc'],  pyb.Pin.OUT_PP, pyb.Pin.PULL_DOWN)
      self.res = pyb.Pin(pinout['res'], pyb.Pin.OUT_PP, pyb.Pin.PULL_DOWN)
      self.offset = 0
    else:
      # Infer bus number from pin
      if pinout['sda'] == 'X9':
        self.i2c = pyb.I2C(1)
      else:
        self.i2c = pyb.I2C(2)
      self.i2c.init(pyb.I2C.MASTER, baudrate=400000) # 400kHz
      self.devid = i2c_devid
      # used to reserve an extra byte in the image buffer AND as a way to
      # infer the interface type
      self.offset = 1
      # I2C command buffer
      self.cbuffer = bytearray(2)
      self.cbuffer[0] = CTL_CMD 
Example #7
Source File: ws2812.py    From micropython-ws2812 with MIT License 5 votes vote down vote up
def send_buf(self):
        """
        Send buffer over SPI.
        """
        self.spi.send(self.buf)
        gc.collect() 
Example #8
Source File: flash.py    From micropython-epaper with Apache License 2.0 5 votes vote down vote up
def begin(self):                            # Baud rates of 50MHz supported by chip
        self.pinCS.high()
        self.spi = pyb.SPI(self.spi_no, pyb.SPI.MASTER, baudrate = 21000000, polarity = 1, phase = 1, bits = 8)
        if not self._available():               # Includes 1mS wakeup delay
            raise FlashException("Unsupported flash device") 
Example #9
Source File: upcd8544.py    From Smart-IoT-Planting-System with MIT License 4 votes vote down vote up
def __init__(self, spi, rst, ce, dc, light, pwr=None):
        self.width  = 84
        self.height = 48
        self.power      = self.POWER_DOWN
        self.addressing = self.ADDRESSING_HORIZ
        self.instr      = self.INSTR_BASIC
        self.display_mode = self.DISPLAY_BLANK
        self.temp_coeff = self.TEMP_COEFF_0
        self.bias       = self.BIAS_1_11
        self.voltage    = 3060

        # init the SPI bus and pins
        spi.init(spi.MASTER, baudrate=328125, bits=8, polarity=0, phase=1, firstbit=spi.MSB)
        if "OUT_PP" in dir(rst):
            # pyBoard style
            rst.init(rst.OUT_PP, rst.PULL_NONE)  # Reset line
            ce.init(ce.OUT_PP, ce.PULL_NONE)     # Chip Enable
            dc.init(dc.OUT_PP, dc.PULL_NONE)     # Data(1) / Command(0) mode
            light.init(light.OUT_PP, light.PULL_NONE)
            if pwr:
                pwr.init(pwr.OUT_PP, pwr.PULL_NONE)
        else:
            # WiPy style
            rst.init(rst.OUT, None)
            ce.init(ce.OUT, None)
            dc.init(dc.OUT, None)
            light.init(light.OUT, None)
            if pwr:
                pwr.init(pwr.OUT, None)

        self.spi   = spi
        self.rst   = rst
        self.ce    = ce
        self.dc    = dc
        self.light = light
        self.pwr   = pwr

        self.light_off()
        self.power_on()
        self.ce.value(1)  # set chip to disable (don't listen to input)
        self.reset()
        self.set_contrast(0xbf)
        self.clear()
        self.lcd_font = font.FONT6_8()
        self.chinese = chinese.CN_UTF8() 
Example #10
Source File: sdcard.py    From uPySensors with Apache License 2.0 4 votes vote down vote up
def init_card(self):
        # init CS pin
        self.cs.init(self.cs.OUT, value=1)

        # init SPI bus; use low data rate for initialisation
        self.init_spi(10000000)

        # clock card at least 100 cycles with cs high
        for i in range(16):
            self.spi.write(b'\xff')

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

        # CMD8: determine card version
        r = self.cmd(8, 0x01aa, 0x87, 4)
        if r == _R1_IDLE_STATE:
            self.init_card_v2()
        elif r == (_R1_IDLE_STATE | _R1_ILLEGAL_COMMAND):
            self.init_card_v1()
        else:
            raise OSError("couldn't determine SD card version")

        # get the number of sectors
        # CMD9: response R2 (R1 byte + 16-byte block read)
        if self.cmd(9, 0, 0, 0, False) != 0:
            raise OSError("no response from SD card")
        csd = bytearray(16)
        self.readinto(csd)
        if csd[0] & 0xc0 == 0x40: # CSD version 2.0
            self.sectors = ((csd[8] << 8 | csd[9]) + 1) * 1024
        elif csd[0] & 0xc0 == 0x00: # CSD version 1.0 (old, <=2GB)
            c_size = csd[6] & 0b11 | csd[7] << 2 | (csd[8] & 0b11000000) << 4
            c_size_mult = ((csd[9] & 0b11) << 1) | csd[10] >> 7
            self.sectors = (c_size + 1) * (2 ** (c_size_mult + 2))
        else:
            raise OSError("SD card CSD format not supported")
        #print('sectors', self.sectors)

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

        # set to high data rate now that it's initialised
        self.init_spi(1320000)