我们从Python开源项目中,提取了以下18个代码示例,用于说明如何使用machine.SPI。
def main(): SPI = pyb.SPI(1) #DIN=>X8-MOSI/CLK=>X6-SCK #DIN =>SPI(1).MOSI 'X8' data flow (Master out, Slave in) #CLK =>SPI(1).SCK 'X6' SPI clock RST = pyb.Pin('Y10') CE = pyb.Pin('Y11') DC = pyb.Pin('Y9') LIGHT = pyb.Pin('Y12') lcd_5110 = upcd8544.PCD8544(SPI, RST, CE, DC, LIGHT) lcd_5110.lcd_write_string('Hello Python!',0,0) lcd_5110.lcd_write_string('Micropython',6,1) lcd_5110.lcd_write_string('TPYBoard',12,2) lcd_5110.lcd_write_string('v102',60,3) lcd_5110.lcd_write_string('This is a test of LCD5110',0,4)
def __init__(self, controller="XPT2046", asyn=False, *, confidence=5, margin=50, delay=10, calibration=None, spi = None): if spi is None: self.spi = SPI(-1, baudrate=1000000, sck=Pin("X12"), mosi=Pin("X11"), miso=Pin("Y2")) else: self.spi = spi self.recv = bytearray(3) self.xmit = bytearray(3) # set default values self.ready = False self.touched = False self.x = 0 self.y = 0 self.buf_length = 0 cal = TOUCH.DEFAULT_CAL if calibration is None else calibration self.asynchronous = False self.touch_parameter(confidence, margin, delay, cal) if asyn: self.asynchronous = True import uasyncio as asyncio loop = asyncio.get_event_loop() loop.create_task(self._main_thread()) # set parameters for get_touch() # res: Resolution in bits of the returned values, default = 10 # confidence: confidence level - number of consecutive touches with a margin smaller than the given level # which the function will sample until it accepts it as a valid touch # margin: Difference from mean centre at which touches are considered at the same position # delay: Delay between samples in ms. #
def read(self, size=HCI_READ_PACKET_SIZE, retry=5): """ Read packet from BlueNRG-MS module """ result = None # Exchange header header_master = b'\x0B\x00\x00\x00\x00' header_slave = bytearray(len(header_master)) while retry: with CSContext(self._nss_pin): self._spi_bus.write_readinto(header_master, header_slave) rx_read_bytes = (header_slave[4] << 8) | header_slave[3] if header_slave[0] == 0x02 and rx_read_bytes > 0: # SPI is ready # avoid to read more data that size of the buffer if rx_read_bytes > size: rx_read_bytes = size data = b'\xFF' * rx_read_bytes result = bytearray(rx_read_bytes) self._spi_bus.write_readinto(data, result) break else: utime.sleep_us(150) retry -= 1 # Add a small delay to give time to the BlueNRG to set the IRQ pin low # to avoid a useless SPI read at the end of the transaction utime.sleep_us(150) return result
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()
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()
def __init__(self, connect=None, *, pwr=None, i2c=None, spi=None, i2c_addr=98): if connect in ('X', 'Y', 'XY', 'YX'): i = connect[-1] j = connect[0] y = j + '4' elif connect == 'C': i = 2 j = 2 y = 'A7' else: if pwr is None or i2c is None or spi is None: raise ValueError('must specify valid "connect" or all of "pwr", "i2c" and "spi"') if pwr is None: pwr = machine.Pin(y, machine.Pin.OUT) if i2c is None: i2c = machine.I2C(i, freq=1000000) if spi is None: spi = machine.SPI(j, baudrate=13500000, polarity=0, phase=0) if not pwr.value(): pwr(1) sleep_ms(10) # else: # alread have power # lets be optimistic... # set connections self.pwr = pwr self.i2c = i2c self.spi = spi self.i2c_addr = i2c_addr # create temp buffers and memoryviews self.buf16 = bytearray(16) self.buf19 = bytearray(19) self.buf = [None] * 10 for i in range(1, 10): self.buf[i] = memoryview(self.buf16)[0:i] self.buf1 = self.buf[1] self.array4 = [0, 0, 0, 0] # set default orientation and window self.set_orient(PORTRAIT) self._fcmd2b('<BBBBBB', 0x76, 0, 0, self.w, self.h) # viewport 'v' self._fcmd2b('<BBBBBB', 0x79, 0, 0, self.w, self.h) # window 'y'
def __init__( self, spi_bus=machine.SPI(2, baudrate=8000000, polarity=0), irq_pin=machine.Pin('Y3', machine.Pin.IN, machine.Pin.PULL_DOWN), rst_pin=machine.Pin('Y4', machine.Pin.OUT_PP), nss_pin=machine.Pin('Y5', machine.Pin.OUT_PP), ): """ Defaults: - SPI(2) on the Y position: (NSS, SCK, MISO, MOSI) = (Y5, Y6, Y7, Y8) = (PB12, PB13, PB14, PB15) Params: phase: 0 dir: SPI_DIRECTION_2LINES bits: 8 nss: SPI_NSS_SOFT firstbit: SPI_FIRSTBIT_MSB ti: SPI_TIMODE_DISABLED crc: crc_calc: SPI_CRCCALCULATION_DISABLED - IRQ on Y3 Pin - RST on Y4 Pin - NSS on Y5 Pin - SCK on Y6 Pin - MISO on Y7 Pin - MOSI on Y8 Pin """ if not isinstance(spi_bus, machine.SPI): raise TypeError("") m_pins = (irq_pin, rst_pin, nss_pin) if not all([isinstance(pin, machine.Pin) for pin in m_pins]): raise TypeError("") self._spi_bus = spi_bus self._irq_pin = irq_pin self._rst_pin = rst_pin self._nss_pin = nss_pin # Release CS line self._nss_pin.on()
def write(self, header, param, retry=5): """ Write packet to BlueNRG-MS module """ result = None # Exchange header header_master = b'\x0A\x00\x00\x00\x00' header_slave = bytearray(len(header_master)) while retry: with CSContext(self._nss_pin): self._spi_bus.write_readinto(header_master, header_slave) rx_write_bytes = header_slave[1] rx_read_bytes = (header_slave[4] << 8) | header_slave[3] if header_slave[0] == 0x02 and ( rx_write_bytes > 0 or rx_read_bytes > 0): # SPI is ready if header: # avoid to write more data that size of the buffer if rx_write_bytes >= len(header): result = bytearray(len(header)) self._spi_bus.write_readinto(header, result) if param: rx_write_bytes -= len(header) # avoid to read more data that size of the # buffer if len(param) > rx_write_bytes: tx_bytes = rx_write_bytes else: tx_bytes = len(param) result = bytearray(tx_bytes) self._spi_bus.write_readinto(param, result) break else: break else: break else: break else: utime.sleep_us(150) retry -= 1 return result
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) spi.init(baudrate=328125) 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()
def __init__(self, spi, rst, dc, 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) spi.init(baudrate=328125) 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()