コード例 #1
1
ファイル: spi.py プロジェクト: A-L-E-X/micropython
from pyb import SPI

spi = SPI(1)
print(spi)

spi = SPI(1, SPI.MASTER)
spi = SPI(1, SPI.MASTER, baudrate=500000)
spi = SPI(1, SPI.MASTER, 500000, polarity=1, phase=0, bits=8, firstbit=SPI.MSB, ti=False, crc=None)
print(spi)

spi.init(SPI.SLAVE, phase=1)
print(spi)

spi.init(SPI.MASTER)
spi.send(1, timeout=100)
print(spi.recv(1, timeout=100))
print(spi.send_recv(1, timeout=100))
コード例 #2
0
    def __init__(self, cs='PE3', spi=1, debug=False):
        self._debug = debug
        self.cs_pin = Pin(cs, Pin.OUT_PP, Pin.PULL_NONE)
        self.cs_pin.high()
        self.spi = SPI(spi, SPI.MASTER, baudrate=328125, polarity=0, phase=1,
                       bits=8)

        self.read_id()
        # First SPI read always returns 255 --> discard and read ID again
        self.who_am_i = self.read_id()
        self.debug("Accel-ID: %s" % self.who_am_i)

        if self.who_am_i == LIS302DL_WHO_AM_I_VAL:
            self.write_bytes(LIS302DL_CTRL_REG1_ADDR, LIS302DL_CONF)
            self.sensitivity = 18
        elif self.who_am_i == LIS3DSH_WHO_AM_I_VAL:
            self.write_bytes(LIS3DSH_CTRL_REG4_ADDR, LIS3DSH_CTRL_REG4_CONF)
            self.write_bytes(LIS3DSH_CTRL_REG5_ADDR, LIS3DSH_CTRL_REG5_CONF)
            self.sensitivity = 0.06 * 256
        else:
            msg = 'LIS302DL or LIS3DSH accelerometer not present'

            if self._debug:
                self.debug(msg)
            else:
                raise IOError(msg)
コード例 #3
0
ファイル: staccel.py プロジェクト: magickcoding/micropython
 def __init__(self):
     self.cs_pin = Pin('PE3', Pin.OUT_PP, Pin.PULL_NONE)
     self.cs_pin.high()
     self.spi = SPI(1, SPI.MASTER, baudrate=328125, polarity=0, phase=1, bits=8)
     self.write_bytes(LIS302DL_CTRL_REG1_ADDR, bytearray([LIS302DL_CONF]))
     if self.read_id() != LIS302DL_WHO_AM_I_VAL:
         raise Exception('LIS302DL accelerometer not present')
コード例 #4
0
 def __init__(self, bus=1, baudrate=328125, polarity=0, phase=0, ss='A4'):
     self.ss = Pin(ss, Pin.OUT)
     self.ss.high()
     self.spi = SPI(bus, SPI.MASTER, baudrate=baudrate, polarity=polarity,
                    phase=phase)
     self.msgbuf = bytearray(32)
     self.status = bytearray(4)
コード例 #5
0
ファイル: spiMgr.py プロジェクト: gratefulfrog/ArduGuitar
 def __init__(self,spiOnX,latchPin):
     # create an SPI.MASTER instance on the 'X' side of the board, 
     # first arg=1 means 'X side' of the board
     boardSide = 1
     if not spiOnX:
         boardSide = 2
     self.spi = SPI(boardSide,SPI.MASTER)
     # create the stcp pin on the "latch" pin
     self.stcp = Pin(latchPin, Pin.OUT_PP)
コード例 #6
0
ファイル: staccel.py プロジェクト: Achimh3011/micropython
class STAccel:
    def __init__(self):
        self.cs_pin = Pin('PE3', Pin.OUT_PP, Pin.PULL_NONE)
        self.cs_pin.high()
        self.spi = SPI(1, SPI.MASTER, baudrate=328125, polarity=0, phase=1, bits=8)

        self.who_am_i = self.read_id()

        if self.who_am_i == LIS302DL_WHO_AM_I_VAL:
            self.write_bytes(LIS302DL_CTRL_REG1_ADDR, bytearray([LIS302DL_CONF]))
            self.sensitivity = 18
        elif self.who_am_i == LIS3DSH_WHO_AM_I_VAL:
            self.write_bytes(LIS3DSH_CTRL_REG4_ADDR, bytearray([LIS3DSH_CTRL_REG4_CONF]))
            self.write_bytes(LIS3DSH_CTRL_REG5_ADDR, bytearray([LIS3DSH_CTRL_REG5_CONF]))
            self.sensitivity = 0.06 * 256
        else:
            raise Exception('LIS302DL or LIS3DSH accelerometer not present')

    def convert_raw_to_g(self, x):
        if x & 0x80:
            x = x - 256
        return x * self.sensitivity / 1000

    def read_bytes(self, addr, nbytes):
        if nbytes > 1:
            addr |= READWRITE_CMD | MULTIPLEBYTE_CMD
        else:
            addr |= READWRITE_CMD
        self.cs_pin.low()
        self.spi.send(addr)
        #buf = self.spi.send_recv(bytearray(nbytes * [0])) # read data, MSB first
        buf = self.spi.recv(nbytes)
        self.cs_pin.high()
        return buf

    def write_bytes(self, addr, buf):
        if len(buf) > 1:
            addr |= MULTIPLEBYTE_CMD
        self.cs_pin.low()
        self.spi.send(addr)
        for b in buf:
            self.spi.send(b)
        self.cs_pin.high()

    def read_id(self):
        return self.read_bytes(WHO_AM_I_ADDR, 1)[0]

    def x(self):
        return self.convert_raw_to_g(self.read_bytes(OUT_X_ADDR, 1)[0])

    def y(self):
        return self.convert_raw_to_g(self.read_bytes(OUT_Y_ADDR, 1)[0])

    def z(self):
        return self.convert_raw_to_g(self.read_bytes(OUT_Z_ADDR, 1)[0])

    def xyz(self):
        return (self.x(), self.y(), self.z())
コード例 #7
0
ファイル: staccel.py プロジェクト: Achimh3011/micropython
    def __init__(self):
        self.cs_pin = Pin('PE3', Pin.OUT_PP, Pin.PULL_NONE)
        self.cs_pin.high()
        self.spi = SPI(1, SPI.MASTER, baudrate=328125, polarity=0, phase=1, bits=8)

        self.who_am_i = self.read_id()

        if self.who_am_i == LIS302DL_WHO_AM_I_VAL:
            self.write_bytes(LIS302DL_CTRL_REG1_ADDR, bytearray([LIS302DL_CONF]))
            self.sensitivity = 18
        elif self.who_am_i == LIS3DSH_WHO_AM_I_VAL:
            self.write_bytes(LIS3DSH_CTRL_REG4_ADDR, bytearray([LIS3DSH_CTRL_REG4_CONF]))
            self.write_bytes(LIS3DSH_CTRL_REG5_ADDR, bytearray([LIS3DSH_CTRL_REG5_CONF]))
            self.sensitivity = 0.06 * 256
        else:
            raise Exception('LIS302DL or LIS3DSH accelerometer not present')
コード例 #8
0
ファイル: staccel.py プロジェクト: magickcoding/micropython
class STAccel:
    def __init__(self):
        self.cs_pin = Pin('PE3', Pin.OUT_PP, Pin.PULL_NONE)
        self.cs_pin.high()
        self.spi = SPI(1, SPI.MASTER, baudrate=328125, polarity=0, phase=1, bits=8)
        self.write_bytes(LIS302DL_CTRL_REG1_ADDR, bytearray([LIS302DL_CONF]))
        if self.read_id() != LIS302DL_WHO_AM_I_VAL:
            raise Exception('LIS302DL accelerometer not present')

    def read_bytes(self, addr, nbytes):
        if nbytes > 1:
            addr |= READWRITE_CMD | MULTIPLEBYTE_CMD
        else:
            addr |= READWRITE_CMD
        self.cs_pin.low()
        self.spi.send(addr)
        #buf = self.spi.send_recv(bytearray(nbytes * [0])) # read data, MSB first
        buf = self.spi.recv(nbytes)
        self.cs_pin.high()
        return buf

    def write_bytes(self, addr, buf):
        if len(buf) > 1:
            addr |= MULTIPLEBYTE_CMD
        self.cs_pin.low()
        self.spi.send(addr)
        for b in buf:
            self.spi.send(b)
        self.cs_pin.high()

    def read_id(self):
        return self.read_bytes(LIS302DL_WHO_AM_I_ADDR, 1)[0]

    def x(self):
        return convert_raw_to_g(self.read_bytes(LIS302DL_OUT_X, 1)[0])

    def y(self):
        return convert_raw_to_g(self.read_bytes(LIS302DL_OUT_Y, 1)[0])

    def z(self):
        return convert_raw_to_g(self.read_bytes(LIS302DL_OUT_Z, 1)[0])

    def xyz(self):
        val = self.read_bytes(LIS302DL_OUT_X, 5)
        return [convert_raw_to_g(val[0]),
                convert_raw_to_g(val[2]),
                convert_raw_to_g(val[4])]
コード例 #9
0
ファイル: Lib_simple.py プロジェクト: salskraken/typsnitt
	def __init__(self):
		self.dbg = Pin("X9", Pin.OUT_PP)
		self.dbg.low()
		self.spi = SPI(2, SPI.MASTER, baudrate=5250000)
		ss = Pin(self.CS_TFT, Pin.OUT_PP)
		ss.low()
		self.dc = Pin(self.DC, Pin.OUT_PP)
		self.dc.low()
		self.DC_flag = False
		cs_sd = Pin(self.CS_SD, Pin.OUT_PP)
		cs_sd.high()
		self.lite = Pin(self.LITE, Pin.OUT_PP)
		self.lite.high()
		reset = Pin(self.RST, Pin.OUT_PP)
		reset.low()
		delay(2)
		reset.high()
		delay(200)
コード例 #10
0
ファイル: rfm69.py プロジェクト: arkorobotics/PyHAB
	def __init__(self, reset_pin=None, dio0_pin=None, spi_channel=None, config=None):
		self.reset_pin = reset_pin
		self.nss = Pin('X5', Pin.OUT_PP)
		self.reset = self.reset()
		self.spi_channel = spi_channel
		self.spi = SPI(1, SPI.MASTER, baudrate=50000, polarity=0, phase=0, firstbit=SPI.MSB, crc=None)
		self.dio0_pin = 'X3'
		self.conf = self.configure()
		self.mode = self.set_mode()

		self.txBufLen = 0
		self.txBuf = bytearray(registers["RFM69_MAX_MESSAGE_LEN"])
		
		self.rxBufLen = 0
		self.rxBuf = bytearray(registers["RFM69_MAX_MESSAGE_LEN"])

		self.version = self.getVersion()
		self.lastRssi = -(self.spi_read(registers["RFM69_REG_24_RSSI_VALUE"])/2)
		self.paLevel = 15
コード例 #11
0
ファイル: staccel.py プロジェクト: gradoj/micropython
class STAccel:
    def __init__(self):
        self.cs_pin = Pin('PE3', Pin.OUT_PP, Pin.PULL_NONE)
        self.cs_pin.high()
        self.spi = SPI(1, SPI.MASTER, baudrate=328125, polarity=0, phase=1, bits=8)
        self.wr(LIS302DL_CTRL_REG1_ADDR, bytearray([LIS302DL_CONF]))

    def rd(self, addr, nbytes):
        if nbytes > 1:
            addr |= READWRITE_CMD | MULTIPLEBYTE_CMD
        else:
            addr |= READWRITE_CMD
        self.cs_pin.low()
        self.spi.send(addr)
        buf = self.spi.send_recv(bytearray(nbytes * [0])) # read data, MSB first
        self.cs_pin.high()
        return buf

    def wr(self, addr, buf):
        if len(buf) > 1:
            addr |= MULTIPLEBYTE_CMD
        self.cs_pin.low()
        self.spi.send(addr)
        for b in buf:
            self.spi.send(b)
        self.cs_pin.high()

    def read_id(self):
        return self.rd(LIS302DL_WHO_AM_I_ADDR, 1)

    def get_xyz(self):
        val = self.rd(LIS302DL_OUT_X, 5)
        x = signed8(val[0]) * 18.0 / 1000
        y = signed8(val[2]) * 18.0 / 1000
        z = signed8(val[4]) * 18.0 / 1000
        return [x, y, z]
コード例 #12
0
ファイル: main1p.py プロジェクト: glaciercoder/EDC2019
import sensor, image, time, math
from pyb import SPI,Pin
import json

spi = SPI(2, SPI.MASTER, baudrate=281250, polarity=0, phase=0)
cs  = Pin("P3", Pin.OUT_PP)
def spi_write(data):
    if data:
        for d in data:
            cs.low()
            spi.send(d)
            cs.high()

sensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE) # grayscale is faster
sensor.set_framesize(sensor.QQVGA)
sensor.skip_frames(time = 1000)
clock = time.clock()
grayscale_thres = (100,255)
thres = (0,50)

# 所有线段都有 `x1()`, `y1()`, `x2()`, and `y2()` 方法来获得他们的终点
# 一个 `line()` 方法来获得所有上述的四个元组值,可用于 `draw_line()`.

#通过k_inv计算倾斜角[0,180)
def kToAngle(k_inv):
    angle = math.degrees(math.atan(k_inv)) + 90
    return angle

num = 33
#horiz_found = [False] * num
コード例 #13
0
ファイル: ledcontrol.py プロジェクト: willingc/snakey-bear
# MicroPython example for controlling one WS2812 LED.
# Method similar to http://www.espruino.com/WS2811
# Attribution to Markus Gritsch

from pyb import SPI

spi = SPI(1, SPI.MASTER, baudrate=6400000, polarity=0, phase=1)
spi.send(chr(0x00))

def byte2bits(byte):
    b0 = chr(0x03)
    b1 = chr(0x0F)
    bits = ''
    mask = 0x80
    while mask != 0:
        bits += b0 if ( byte & mask ) == 0 else b1
        mask >>= 1
        return bits

def sendColor(red, green, blue):
    spi.send(byte2bits(green)+byte2bits(red)+byte2bits(blue))

import math

n = 0
while True:
    r = int((1 + math.sin(n * 0.1324)) * 127)
    g = int((1 + math.sin(n * 0.1654)) * 127)
    b = int((1 + math.sin(n * 0.1)) * 127)
    sendColor(r, g, b)
    n += 1
コード例 #14
0
ファイル: Lib_simple.py プロジェクト: salskraken/typsnitt
class St:
	DC      = "Y4"
	CS_TFT  = "Y5"
	CS_SD   = "Y3"
	LITE    = "Y2"
	RST     = "Y1"

	XSIZE   = 128
	YSIZE   = 160
	XMAX    = XSIZE-1
	YMAX    = YSIZE-1
	X0      = int(XSIZE/2)
	Y0      = int(YSIZE/2)

	SW_RESET    = 0x01
	SLPIN       = 0x10
	SLP_OUT     = 0x11
	PTLON       = 0x12
	NORON       = 0x13
	INVOFF      = 0x20
	INVON       = 0x21
	DISPOFF     = 0x28
	DISP_ON     = 0x29
	CA_SET      = 0x2A
	RA_SET      = 0x2B
	RAM_WR      = 0x2C
	RAMRD       = 0x2E
	PTLAR       = 0x30
	MAD_CTL     = 0x36
	COL_MOD     = 0x3A
	FRMCT1      = 0xB1
	FRMCT2      = 0xB2
	FRMCT3      = 0xB3
	INVCTR      = 0xB4
	DISSET      = 0xB6
	PWRCT1      = 0xC0
	PWRCT2      = 0xC1
	PWRCT3      = 0xC2
	PWRCT4      = 0xC3
	PWRCT5      = 0xC4
	VMCTR1      = 0xC5
	PWRCT6      = 0xFC
	GAMCTP      = 0xE0
	GAMCTN      = 0xE1

	def __init__(self):
		self.dbg = Pin("X9", Pin.OUT_PP)
		self.dbg.low()
		self.spi = SPI(2, SPI.MASTER, baudrate=5250000)
		ss = Pin(self.CS_TFT, Pin.OUT_PP)
		ss.low()
		self.dc = Pin(self.DC, Pin.OUT_PP)
		self.dc.low()
		self.DC_flag = False
		cs_sd = Pin(self.CS_SD, Pin.OUT_PP)
		cs_sd.high()
		self.lite = Pin(self.LITE, Pin.OUT_PP)
		self.lite.high()
		reset = Pin(self.RST, Pin.OUT_PP)
		reset.low()
		delay(2)
		reset.high()
		delay(200)

	def FastDrawPixel(self, x, y, color):
		self.dc.low()
		self.spi.send(self.CA_SET)
		self.dc.high()
		self.spi.send(bytearray([0, x, 0, x]))
		self.dc.low()
		self.spi.send(self.RA_SET)
		self.dc.high()
		self.spi.send(bytearray([0, y, 0, y]))
		self.dc.low()
		self.spi.send(self.RAM_WR)
		self.dc.high()
		self.DC_flag = True
		self.spi.send(bytearray([color >> 8, color & 0xFF]))

	def init_display(self):
		self.write_cmd(self.SW_RESET)
		delay(200)
		self.write_cmd(self.SLP_OUT)
		self.write_cmd(self.COL_MOD, 0x05)
		delay(200)
		self.dbg.high()
		self.dbg.low()
		self.write_cmd(self.DISP_ON)

	def fill_rect(self, x0, y0, x1, y1, color):
		width = x1 - x0 + 1
		height = y1 - y0 + 1
		self.set_addr_window(x0, y0, x1, y1)
		self.write_bulk(color, width, height)

	def fill_screen(self, color):
		self.fill_rect(0, 0, 127, 159, color)

	def clear_screen(self):
		self.fill_rect(0, 0, 127, 159, self.rgb(255, 255, 255))

	def rgb(self, r, g, b):
		r &= 0xF8
		g &= 0xFC
		b &= 0xF8
		return r << 8 | g << 3 | b >> 3

	def write_cmd(self, cmd, *value):
		if self.DC_flag:
			self.dc.low()
			self.DC_flag = False
		self.spi.send(cmd)
		if len(value):
			self.dc.high()
			self.DC_flag = True
			self.spi.send(bytearray(value))

	def write_bulk(self, value, reps, count=1):
		if self.DC_flag:
			self.dc.low()
		self.spi.send(self.RAM_WR)
		self.dc.high()
		self.DC_flag = True
		val_hi = value >> 8
		val_lo = value & 0xFF
		byte_arr = bytearray([val_hi, val_lo] * reps)
		self.dbg.high()
		for a in range(count):
			self.spi.send(byte_arr)
		self.dbg.low()

	def set_addr_window(self, x0, y0, x1, y1):
		self.write_cmd(self.CA_SET, 0, x0, 0, x1)
		self.write_cmd(self.RA_SET, 0, y0, 0, y1)
コード例 #15
0
class SharpMemLCD:
    '''
    MicroPython driver to write to Sharp's Memory LCD. Should works with any standard pyboard
    Uses the built-in framebuffer class to format the buffer 
    Custom parameters (display pin, cs pin, screen's x and y dimensions, SPI channel) can be changed below
    '''
    CS = "C4"
    DISP = "A7"
    XDIM = 144
    YDIM = 168
    CHANNEL = 2
    BAUDRATE = 1100000

    def __init__(self):
        # define pins
        self.spi = SPI(self.CHANNEL, SPI.MASTER, self.BAUDRATE, polarity=0, phase=0, firstbit=SPI.LSB)
        self.cs = Pin(self.CS, Pin.OUT_PP)
        self.set_cs(0)
        self.disp = Pin(self.DISP, Pin.OUT_PP)
        self.disp.value(1)

        # define structures, related info
        self._xdim = self.XDIM
        self._ydim = self.YDIM
        self.buffer = bytearray((self._xdim//8) * self._ydim)
        self.framebuffer = framebuf.FrameBuffer(self.buffer, self._xdim, self._ydim, framebuf.MONO_HMSB)
        self.vcom = 2

        # begin by clearing screen
        self.clear_screen()

    @property
    def xdim(self):
        return self._xdim

    @property
    def ydim(self):
        return self._ydim

    def clear_buffer(self):
        self.framebuffer.fill(0)

    # format framebuffer with text, starting at the x,y coords
    # each character is an 8 by 8 pixel array
    # written on the next line
    def write(self, text, x, y):
        self.framebuffer.text(text, x, y)

    # draw horizontal line in the buffer
    def horizontal_line(self, x, y, length):
        self.framebuffer.hline(x, y, length, 1)

    # draw vertial line in the buffer
    def vertical_line(self, x, y, height):
        self.framebuffer.vline(x, y, height, 1)

    # draw empty rectangle in the buffer (i.e. just the outline)
    def empty_rectangle(self, x, y, width, height):
        self.framebuffer.rect(x, y, width, height, 1)

    def solid_rectangle(self, x, y, width, height):
        self.framebuffer.fill_rect(x, y, width, height, 1)

    def toggle_vcom(self):
        self.vcom ^= 2

    # send lcd command to clear screen
    def clear_screen(self):
        self.set_cs(1)
        udelay(6)          
        self.send(4 | self.vcom)
        self.send(0)
        udelay(2)          
        self.set_cs(0)
        udelay(6)          
        self.toggle_vcom()

    def set_cs(self, logic_level):
        self.cs.value(logic_level)

    def send(self, value):
        self.spi.send(value)

    # sync up screen with framebuffer contents
    # done by implementing the lcd command to write a single line, for every line in the buffer
    def sync(self):
        index = 0
        for line in range(self._ydim):
            self.set_cs(1)
            udelay(6)       # tsSCS
            self.send(1 | self.vcom)
            self.send(line+1)
            for j in range(self._xdim//8):
                self.send(self.buffer[index])
                index += 1
            self.send(0)
            udelay(2)       # thSCS
            self.set_cs(0)
            udelay(6)       # twSCSL          
コード例 #16
0
ファイル: rfm69.py プロジェクト: arkorobotics/PyHAB
	def init_spi(self):
		self.spi = SPI(1, SPI.MASTER, baudrate=50000, polarity=0, phase=0, firstbit=SPI.MSB, crc=None)
コード例 #17
0
import tftlcd
import font1
import gc
from pyb import SPI, Pin
spi = SPI(2)
tft = tftlcd.TFT(spi, cs='X11', rst='X9', rs='X10', color=2000)
tft.clean(2000)
# tft.point(10,20,100)
# tft.line(2,3,20,40,255)
# tft.fill(0,0,30,10,0)
# tft.rectangle(20,20,60,60,0)
# tft.round(50,50,10,50)

indexes_chinese16 = "液晶屏测试"
indexes_chinese12 = "文字测试"
indexes_roman = "0123456789"

tft.init_str(font1.FONT().f16, indexes_chinese16)
tft.write_str(75, 10, 16, 16, "液晶屏", 0)

tft.init_str(font1.FONT().f12, indexes_chinese12)
tft.write_str(86, 30, 16, 12, "测试", 255)

tft.init_str(font1.FONT().fnum, indexes_roman)
tft.write_str(86, 50, 8, 16, "149", TFT.RED)

tft.write_pictuer(5, 40, 72, 75, font1.image().pictuer, TFT.BRED)

gc.enable()
gc.collect()
# print(gc.mem_free())
コード例 #18
0
ファイル: rfm69.py プロジェクト: arkorobotics/PyHAB
class RFM69:
	def __init__(self, reset_pin=None, dio0_pin=None, spi_channel=None, config=None):
		self.reset_pin = reset_pin
		self.nss = Pin('X5', Pin.OUT_PP)
		self.reset = self.reset()
		self.spi_channel = spi_channel
		self.spi = SPI(1, SPI.MASTER, baudrate=50000, polarity=0, phase=0, firstbit=SPI.MSB, crc=None)
		self.dio0_pin = 'X3'
		self.conf = self.configure()
		self.mode = self.set_mode()

		self.txBufLen = 0
		self.txBuf = bytearray(registers["RFM69_MAX_MESSAGE_LEN"])
		
		self.rxBufLen = 0
		self.rxBuf = bytearray(registers["RFM69_MAX_MESSAGE_LEN"])

		self.version = self.getVersion()
		self.lastRssi = -(self.spi_read(registers["RFM69_REG_24_RSSI_VALUE"])/2)
		self.paLevel = 15

	def configure(self):
		print ("Configuring...")
		for reg, value in config.items():
			self.spi_write(registers.get(reg), value)
		return True

	def getVersion(self):
		self.version = self.spi_read(registers["RFM69_REG_10_VERSION"])
		return self.version

	def set_mode(self, newMode=registers["RFM69_MODE_RX"]):
		self.spi_write(registers["RFM69_REG_01_OPMODE"], (self.spi_read(registers["RFM69_REG_01_OPMODE"]) & 0xE3) | newMode)
		while(self.spi_read(registers["RFM69_REG_01_OPMODE"]) != newMode):
			self.spi_write(registers["RFM69_REG_01_OPMODE"], (self.spi_read(registers["RFM69_REG_01_OPMODE"]) & 0xE3) | newMode)
			print ("Waiting... Attempted mode: %d" % newMode)
			sleep(1)
			pass
		self.mode = newMode
		return newMode

	def get_mode(self):
		#self.mode = self.spi_read(registers["RFM69_REG_01_OPMODE"])
		return self.mode

	def init_gpio(self):
		self.dio0_pin = Pin('X3', Pin.IN, Pin.PULL_DOWN)

	def init_spi(self):
		self.spi = SPI(1, SPI.MASTER, baudrate=50000, polarity=0, phase=0, firstbit=SPI.MSB, crc=None)

	def reset(self):
		""" Reset the module, then check it's working. """
		print ("Initialising RFM...")

		self.nss.high()

		self.reset_pin = Pin('X4', Pin.OUT_PP)
		self.reset_pin.low()                
		sleep(0.1)
		self.reset_pin.high()
		sleep(0.1) 
		self.reset_pin.low()                
		sleep(0.1)

	def checkRx(self):
		print ("MODE: %d" % self.get_mode())
		print ("Waiting for Payload")
		while ((self.spi_read(registers["RFM69_REG_28_IRQ_FLAGS2"]) & registers["RF_IRQFLAGS2_PAYLOADREADY"]) != registers["RF_IRQFLAGS2_PAYLOADREADY"]):
			pass
		print ("MODE: %d" % self.spi_read(registers["RFM69_REG_01_OPMODE"]))
		print ("IRQ Flag: %d" % self.spi_read(registers["RFM69_REG_28_IRQ_FLAGS2"]))
		self.rxBufLen = self.spi_read(registers["RFM69_REG_00_FIFO"])+1
		print ("RX Buffer Length: %d" % self.rxBufLen)
		self.rxBuf = self.spi_burst_read(registers["RFM69_REG_00_FIFO"], registers["RFM69_FIFO_SIZE"])
		self.lastRssi = -(self.spi_read(registers["RFM69_REG_24_RSSI_VALUE"])/2)
		self.clearFifo()

	def recv(self):
		# Store received data for return
		rxTuple = (self.rxBuf, self.rxBufLen, self.lastRssi)

		# Clear RX buffer
		self.rxBufLen = 0
		self.rxBuf = bytearray(registers["RFM69_MAX_MESSAGE_LEN"])
		
		# Return received telemetry
		return rxTuple

	def send(self, data, length, power):
		if (power<2 or power > 20):
			return False	#Dangerous power levels

		oldMode = self.mode
		
		# Copy into TX buffer
		self.txBuf = data
		self.txBufLen = length

		# Start Transmitter
		print ("OLD MODE: %d" % self.mode)
		self.set_mode(registers["RFM69_MODE_TX"])
		print ("NEW MODE: %d" % self.mode)

		#Setup PA
		if (power <= 17):
			# Set PA Level
			self.paLevel = power + 14
			self.spi_write(registers["RFM69_REG_11_PA_LEVEL"], registers["RF_PALEVEL_PA0_OFF"] | registers["RF_PALEVEL_PA1_ON"] | registers["RF_PALEVEL_PA2_ON"] | self.paLevel )
		else:
			# Disable Over Current Protection
			self.spi_write(registers["RFM69_REG_13_OCP"], registers["RF_OCP_OFF"])
			# Enable High Power Registers
			self.spi_write(registers["RFM69_REG_5A_TEST_PA1"], 0x5D)
			self.spi_write(registers["RFM69_REG_5C_TEST_PA2"], 0x7C)
			# Set PA Level
			self.paLevel = power + 11
			self.spi_write(registers["RFM69_REG_11_PA_LEVEL"], registers["RF_PALEVEL_PA0_OFF"] | registers["RF_PALEVEL_PA1_ON"] | registers["RF_PALEVEL_PA2_ON"] | self.paLevel )

		# Wait for PA ramp-up
		print ("Waiting for PA ramp-up")
		while((self.spi_read(registers["RFM69_REG_27_IRQ_FLAGS1"]) & registers["RF_IRQFLAGS1_TXREADY"]) != registers["RF_IRQFLAGS1_TXREADY"]):
			pass

		# Transmit
		self.write_fifo(self.txBuf)

		# Wait for packet to be sent
		print ("Waiting for packet to be sent")
		while ((self.spi_read(registers["RFM69_REG_28_IRQ_FLAGS2"]) & registers["RF_IRQFLAGS2_PACKETSENT"]) != registers["RF_IRQFLAGS2_PACKETSENT"]):
			pass

		# Return Transceiver to original mode
		print ("OLD MODE: %d" % self.mode)
		self.set_mode(oldMode)
		print ("NEW MODE: %d" % self.mode)

		# If we were in high power, switch off High Power Registers
		if (power > 17):
			self.spi_write(registers["RFM69_REG_5A_TEST_PA1"], 0x55)
			self.spi_write(registers["RFM69_REG_5C_TEST_PA2"], 0x70)
			self.spi_write(registers["RFM69_REG_13_OCP"], (registers["RF_OCP_ON"] | registers["RF_OCP_TRIM_95"]))

		# Clear TX buffer
		self.txBufLen = 0
		self.txBuf = bytearray(registers["RFM69_MAX_MESSAGE_LEN"])

		print ("Transmission complete!")

	def setLnaMode(self, lnaMode):
		self.spi_write(registers["RFM69_REG_58_TEST_LNA"], lnaMode)

	def clearFifo(self):
		self.set_mode(registers["RFM69_MODE_STDBY"])
		self.set_mode(registers["RFM69_MODE_RX"])

	def readTemp(self):
		oldMode = self.mode

		self.set_mode(registers["RFM69_MODE_STDBY"])

		self.spi_write(registers["RFM69_REG_4E_TEMP1"], registers["RF_TEMP1_MEAS_START"])

		print ("Temp Measurement Running")
		while (self.spi_read(registers["RFM69_REG_4E_TEMP1"]) == registers["RF_TEMP1_MEAS_RUNNING"]):
			pass

		rawTemp = self.spi_read(registers["RFM69_REG_4F_TEMP2"])

		self.set_mode(oldMode)

		return (168 - rawTemp) - 5 # Offset and compensate for self-heating

	def lastRssi(self):
		return self.lastRssi

	def sampleRssi(self):
		# Must only be called in RX mode
		if (self.mode != registers["RFM69_MODE_RX"]):
			# Not sure what happens otherwise, so check this
			return 0

		# Trigger RSSI Measurement
		self.spi_write(registers["RFM69_REG_23_RSSI_CONFIG"], registers["RF_RSSI_START"])

		# Wait for Measurement to complete
		print ("Wait for RSSI")
		while((self.spi_read(registers["RFM69_REG_23_RSSI_CONFIG"]) & registers["RF_RSSI_DONE"]) != registers["RF_RSSI_DONE"]):
			pass

		# Read, store in _lastRssi and return RSSI Value
		self.lastRssi = -(self.spi_read(registers["RFM69_REG_24_RSSI_VALUE"])/2)
		return self.lastRssi

	# Read/Write Functions
	def spi_read(self, register):
		data = bytearray(2)
		data[0] = register & ~0x80
		data[1] = 0
		resp = bytearray(2)
		self.nss.low()
		self.spi.send_recv(data, resp, timeout=5000)
		self.nss.high()
		return resp[1]

	def spi_burst_read(self, register, length):
		data = bytearray(length+1)
		data[0] = register & ~0x80
		for i in range(1,length+1):
			data[i] = 0
		# We get the length again as the first character of the buffer
		buf = bytearray(length+1)
		self.nss.low()
		self.spi.send_recv(data, buf, timeout=5000)
		self.nss.high()
		return buf[1:]

	def spi_write(self, register, value):
		data = bytearray(2)
		data[0] = register | 0x80
		data[1] = value
		self.nss.low()
		self.spi.send(data, timeout=5000) 
		self.nss.high()

	def write_fifo(self, data):
		fifo_data = bytearray(len(data)+2)
		fifo_data[0] = registers["RFM69_REG_00_FIFO"] | 0x80
		fifo_data[1] = len(data)
		for i in range(2,len(data)+2):
			fifo_data[i] = data[i-2]
		self.nss.low()
		self.spi.send(fifo_data, timeout=5000)
		self.nss.high()
コード例 #19
0
class SpiMaster:
    def __init__(self, bus=1, baudrate=328125, polarity=0, phase=0, ss='A4'):
        self.ss = Pin(ss, Pin.OUT)
        self.ss.high()
        self.spi = SPI(bus, SPI.MASTER, baudrate=baudrate, polarity=polarity,
                       phase=phase)
        self.msgbuf = bytearray(32)
        self.status = bytearray(4)

    def write_status(self, status):
        self.ss.low()
        self.spi.send(0x01)
        self.spi.send(status & 0xFF)
        self.spi.send((status >> 8) & 0xFF)
        self.spi.send((status >> 16) & 0xFF)
        self.spi.send((status >> 24) & 0xFF)
        self.ss.high()

    def read_status(self):
        self.ss.low()
        self.spi.send(0x04)
        self.spi.recv(self.status)
        self.ss.high()
        return (
            self.status[0] |
            (self.status[1] << 8) |
            (self.status[2] << 16) |
            (self.status[3] << 24)
        )

    def read_data(self):
        self.ss.low()
        self.spi.send(0x03)
        self.spi.send(0x00)
        self.spi.recv(self.msgbuf)
        self.ss.high()
        return self.msgbuf

    def read_msg(self, encoding='utf-8'):
        return bytes(self.read_data()).strip('\0').decode(encoding)

    def write_data(self, data):
        self.msgbuf[:] = data[:32] + b'\0' * (32 - len(data[:32]))
        self.ss.low()
        self.spi.send(0x02)
        self.spi.send(0x00)
        self.spi.send(self.msgbuf)
        self.ss.high()
コード例 #20
0
 def __init__(self,
              spi=SPI(1, SPI.MASTER, baudrate=10000, polarity=1, phase=1),
              cs=Pin.cpu.A4):
     self.__spi = spi
     self.__cs = Pin(cs, Pin.OUT_PP)
コード例 #21
0
ファイル: ntpd.py プロジェクト: dave2/micropython-ntpd
async def _ntpd():
    print("ntpd: starting synced clock service")
    clock = SyncedClock_RTC(gps_uart=UART(2,4800,read_buf_len=200),pps_pin=Pin(Pin.board.A1,Pin.IN))
    await clock.start()
    print("ntpd: listen on udp/123")
    nic = WIZNET5K(SPI('Y'),Pin.board.B4,Pin.board.B3)
    nic.ifconfig(('10.32.34.100','255.255.255.0','10.32.34.1','8.8.8.8'))
    while True:
        if (nic.isconnected()):
            break
        await uasyncio.sleep_ms(100)
    print("ntpd: nic reports connected")
    sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
    sock.bind(('',123))
    poller = uselect.poll()
    poller.register(sock,uselect.POLLIN)
    print("ntpd: starting loop for packets")
    # buffer for outbound packets
    sendbuf = bytearray(48)
    send_payload = uctypes.struct(uctypes.addressof(sendbuf),ntpstruct,uctypes.BIG_ENDIAN)
    send_payload.mode = _NTP_MODE_SERVER
    send_payload.vn = 4
    send_payload.li = _NTP_LI_NOWARN
    send_payload.root_delay = 0
    send_payload.root_dispersion = 0
    send_payload.poll = 6
    send_payload.precision = -9
    send_payload.reference_id[0] = _NTP_REFID_0
    send_payload.reference_id[1] = _NTP_REFID_1
    send_payload.reference_id[2] = _NTP_REFID_2
    send_payload.reference_id[3] = _NTP_REFID_3
    while True:
        packet = await _get_ntp_packet(poller)
        arrival = clock.now()
        refclk = clock.refclk()
        await uasyncio.sleep(0)
        ntp_payload = uctypes.struct(uctypes.addressof(packet[0]),ntpstruct,uctypes.BIG_ENDIAN)
        if (clock.isLocked()):
            send_payload.poll = ntp_payload.poll
            if (ntp_payload.poll < 6):
                send_payload.poll = 6
            if (ntp_payload.poll > 10):
                send_payload.poll = 10
            send_payload.stratum = _NTP_STRATUM_PRIMARY
            send_payload.reference_timestamp_s = refclk[0]+2208988800
            send_payload.reference_timestamp_frac = refclk[1]
            send_payload.receive_timestamp_s = arrival[0]+2208988800
            send_payload.receive_timestamp_frac = arrival[1]
            send_payload.origin_timestamp_s = ntp_payload.transmit_timestamp_s
            send_payload.origin_timestamp_frac = ntp_payload.transmit_timestamp_frac
            await uasyncio.sleep(0)
            transmit = clock.now()
            send_payload.transmit_timestamp_s = transmit[0]+2208988800
            send_payload.transmit_timestamp_frac = transmit[1]
        else:
            await uasyncio.sleep(0)
            send_payload.stratum = _NTP_STRATUM_UNSYNCHRONISED
            send_payload.reference_timestamp_s = 0
            send_payload.reference_timestamp_frac = 0
            send_payload.receive_timestamp_s = 0
            send_payload.receive_timestamp_frac = 0
            send_payload.origin_timestamp_s = 0
            send_payload.origin_timestamp_frac = 0
            send_payload.transmit_timestamp_s = 0
            send_payload.transmit_timestamp_frac = 0
            await uasyncio.sleep(0)
        # we should poll if it's okay to write, but anyway
        sock.sendto(sendbuf,packet[1])
        await uasyncio.sleep(0)
コード例 #22
0
ファイル: test_SPI.py プロジェクト: fojie/micropython--In-Out
from pyb import SPI, Pin, delay, micros, elapsed_micros, rng, disable_irq


def print_elapsed_time(baud, start_time, bursts, nr_bytes):
	time = elapsed_micros(start_time)
	t_p_burst = time / bursts
	nb = ''
	if bursts > 1 and t_p_burst > 500:  # Set to discrimanate between nnormal and "slow" transfers
		nb = 'SLOW !'
	print("%6s %7.3f Mbaud  Tot_time:%7.3f s %6.1f us between calls" % (nb, baud / 1e6, time / 1e6, t_p_burst))


baud_rates = [328125, 656250, 1312500, 2625000, 5250000, 10500000, 21000000]

spi = SPI(2, SPI.MASTER, baud_rates[0])
'''Since MISO is not connected, disable SPI for MISO by setting Pin as normal Pin input'''
miso_dummy = Pin('Y7', Pin.IN)  # May help prevent OS error 5 ?

""" Test SPI with data similar to graphical TFT traffic; Slow for some baudrates (10.5 Mbits/s) """
print("\nGraphical type data (2048 bursts of 4 bytes):")
for baud in baud_rates:
	spi.init(SPI.MASTER, baud)  # Change baud rate
	start = micros()

	for a in range(128):
		for x in range(16):
			spi.send(bytearray([0, x, 0, a]))
	print_elapsed_time(baud, start, 2048, 4)

""" Test SPI with generic data in ONE long buffer; Always(?) fast! """
コード例 #23
0
# main.py -- put your code here!
from pyb import LCD, SPI
import math
import os
import sys
import random

SPI(5, SPI.MASTER, 6000000)
lcd = LCD(0)
lcd.line(0x1c, 1, 1, 1)
led = pyb.LED(1)


def drawLine(x0, y0, x1, y1, colour):
    x_interval = abs(x1 - x0)
    y_interval = abs(y1 - y0)

    if x_interval >= y_interval:
        interval = x_interval
    else:
        interval = y_interval

    for a in range((interval)):
        #print(a);
        #print(interval);
        signx = 1
        signy = 1
        if x_interval >= y_interval:
            if (x0 > x1):
                signx = -1
            if (y0 > y1):
コード例 #24
0
ファイル: spi.py プロジェクト: Chenjingmaha/ESP32_Micropython
from pyb import SPI

# test we can correctly create by id or name
for bus in (-1, 0, 1, 2, 3, "X", "Y", "Z"):
    try:
        SPI(bus)
        print("SPI", bus)
    except ValueError:
        print("ValueError", bus)

spi = SPI(1)
print(spi)

spi = SPI(1, SPI.MASTER)
spi = SPI(1, SPI.MASTER, baudrate=500000)
spi = SPI(1,
          SPI.MASTER,
          500000,
          polarity=1,
          phase=0,
          bits=8,
          firstbit=SPI.MSB,
          ti=False,
          crc=None)
print(spi)

spi.init(SPI.SLAVE, phase=1)
print(spi)
try:
    # need to flush input before we get an error (error is what we want to test)
    for i in range(10):
コード例 #25
0
ファイル: scroll.py プロジェクト: yoyojacky/st7789_mpy
def main():
    tft = st7789.ST7789(SPI(1, SPI.MASTER, baudrate=42000000, prescaler=2),
                        240,
                        320,
                        reset=Pin('X3', Pin.OUT),
                        cs=Pin('X5', Pin.OUT),
                        dc=Pin('X4', Pin.OUT),
                        backlight=Pin('X2', Pin.OUT),
                        rotation=0)

    colors = cycle([0xe000, 0xece0, 0xe7e0, 0x5e0, 0x00d3, 0x7030])
    foreground = next(colors)
    background = st7789.BLACK

    tft.init()
    tft.fill(background)
    utime.sleep(1)

    height = tft.height()
    width = tft.width()
    last_line = height - font.HEIGHT

    tfa = 0
    tfb = 0
    tft.vscrdef(tfa, height, tfb)

    scroll = 0
    character = 0

    while True:
        # clear top line before scrolling off display
        tft.fill_rect(0, scroll, width, 1, background)

        # Write new line when we have scrolled the height of a character
        if scroll % font.HEIGHT == 0:
            line = (scroll + last_line) % height

            # write character hex value as a string
            tft.text(font, 'x{:02x}'.format(character), 0, line, foreground,
                     background)

            # write character using a integer (could be > 0x7f)
            tft.text(font, character, 64, line, foreground, background)

            # write character+64 hex value as a string
            tft.text(font, 'x{:02x}'.format(character + 64), 120, line,
                     foreground, background)

            # write character+64 using a integer (could be > 0x7f)
            tft.text(font, character + 64, 184, line, foreground, background)

            # change color for next line
            foreground = next(colors)

            # next character with rollover at 256
            character += 1
            character %= 64

        tft.vscsad(scroll + tfa)
        scroll += 1
        scroll %= height

        utime.sleep(0.01)
コード例 #26
0
ファイル: spiflash.py プロジェクト: shazz/micropython
# spiflash  SPI flash http://www.adafruit.com/product/1564
# SPI 1 42mhz max   SPI 2  21 mhz max
# SPI1 X5-X8 CS CLK MISO MOSI   3.3v grnd
import pyb
from pyb import SPI
import ubinascii
import gc
CMD_JEDEC_ID = 0x9F
CMD_READ_STATUS = 0x05  # Read status register
CMD_READ = 0x03  # Read @ low speed
CMD_READ_HI_SPEED = 0x0B  # Read @ high speed
CMD_WRITE_ENABLE = 0x06  # Write enable
CMD_PROGRAM_PAGE = 0x02  # Write page
CMD_ERASE_4k = 0x20
CMD_ERASE_32k = 0x52
CMD_ERASE_64k = 0xD8
CMD_ERASE_CHIP = 0xC7
CMD_READ_UID = 0x4B
PAGE_SIZE = 256
cmds = {'4k': CMD_ERASE_4k, '32k': CMD_ERASE_32k, '64k': CMD_ERASE_64k}

def read_block(addr, buff):
    cs.low()
    spi.send(CMD_READ)
    spi.send(addr >> 16)
    spi.send(addr >> 8)
    spi.send(addr)
    spi.recv(buff)
    cs.high()
コード例 #27
0
class SerialFlash25:
    """Class to communicate Serial Flash Memory 25xxxx series with."""
    def __init__(self,
                 name: str,
                 size: int,
                 page_size: int,
                 addr_bytes_num: int,
                 spi: int,
                 cs: str,
                 wp: str,
                 block_size=None,
                 sector_size=None,
                 is_chip_erase=False,
                 conn_chk: bool = False) -> None:
        self.name = name
        self.size = size
        self.sector_size = sector_size
        self.block_size = block_size
        self.page_size = page_size
        self.addr_bytes_num = addr_bytes_num
        self.cs = Pin(cs, Pin.OUT_PP)
        self.wp = Pin(wp, Pin.OUT_PP)
        self.spi = SPI(spi, SPI.MASTER, prescaler=128, polarity=0, phase=0)
        self.conn_chk = conn_chk
        self.is_chip_erase = is_chip_erase
        self._pages_count = size // page_size
        self._sectors_count = size // sector_size if sector_size is not None else None
        self._blocks_count = size // block_size if block_size is not None else None

    @_is_available()
    def read_id(self) -> str:
        """Read device JEDEC Id."""
        self.cs.value(False)
        result = bytearray(4)
        self.spi.send_recv(bytearray([CMD_RDID] + [DUMMY_BYTE] * 3),
                           result)  # Id always contains 3 bytes
        self.cs.value(True)
        res_id = " ".join(["0x%02X" % x for x in result[1:]])
        if res_id == "0x00 0x00 0x00":
            raise RuntimeError(
                "Either IC is connected incorrectly "
                "or it doesn't support Read ID (0x%02X) command." % CMD_RDID)
        return res_id

    def dump(self, path: str) -> None:
        """Dump all device contents to binary file."""
        if self.conn_chk:
            self.read_id()
        if self.sector_size is not None:
            read_cnt, read_item_size = self._sectors_count, self.sector_size
        else:
            read_cnt, read_item_size = self._pages_count, self.page_size
        makedirs(dirname(path))
        with open(path, 'wb') as f:
            for i in range(0, read_cnt):
                # dumping is implemented by sectors/pages
                # because it may be impossible to allocate buffer size of full flash
                buf = self.read(i * read_item_size, read_item_size)
                f.write(buf)
                del buf
        log.info("Dump finished. %s image -> %s" % (self.name, path))

    def program(self, path: str):
        """Flash binary to device."""
        if self.conn_chk:
            self.read_id()
        if not isfile(path):
            raise FileNotFoundError("File  not found: '%s'" % path)
        filesize = getsize(path)
        if filesize != self.size:
            raise ValueError(
                "File size (0x%02X bytes) doesn't equal IC size (0x%02X bytes)"
                % (filesize, self.size))
        self.chip_erase()
        with open(path, 'rb') as f:
            for i in range(0, self._pages_count):
                buf = bytearray(f.read(self.page_size))
                self.page_program(i, buf)
        log.info("Program finished. %s image -> %s" % (path, self.name))

    def chip_erase(self) -> None:
        """Erase chip."""
        if self.is_chip_erase:
            self._wait_wip_reset()
            self._write_enable()
            self.cs.value(False)
            self.spi.send(bytearray([CMD_CE]))
            self.cs.value(True)
            self._write_disable()
            self._wait_wip_reset()
        else:
            for i in range(0, self._pages_count):
                self.page_program(i, bytearray([0xFF] * self.page_size))
        log.info("Chip erase finished.")

    @_is_available("sector_size")
    def sector_program(self, sector_num: int, bytebuffer: bytearray) -> None:
        """Program sector with bytes in bytebuffer."""
        bytebuffer_len = len(bytebuffer)
        if sector_num > self._sectors_count:
            raise ValueError(
                "Sector number (%d) is more than total sectors number (%d)." %
                (sector_num, self._sectors_count))
        if bytebuffer_len > self.page_size:
            raise ValueError(
                "Bytebuffer length (%d) is more than sector size (%d)." %
                (len(bytebuffer), self.page_size))
        self.sector_erase(sector_num)
        pages_count = bytebuffer_len // self.page_size
        remain_bytes = bytebuffer_len % self.page_size
        if remain_bytes != 0:
            log.warning("sector_program: Bytebuffer is not sector aligned.")
        start_page = sector_num * self.sector_size // self.page_size
        for i in range(0, pages_count):
            self.page_program(
                start_page + i,
                bytebuffer[self.page_size * i:self.page_size * (i + 1) + 1])
        if remain_bytes != 0:
            self.page_program(start_page + pages_count,
                              bytebuffer[self.page_size * pages_count:])
        log.info('Sector %d program finished.' % sector_num)

    @_is_available("sector_size")
    def sector_erase(self, sector_num: int) -> None:
        """Erase specified sector."""
        if sector_num > self._sectors_count:
            raise ValueError(
                "Sector number (%d) is more than total sectors number (%d)." %
                (sector_num, self._sectors_count))
        addr = sector_num * self.sector_size
        self._wait_wip_reset()
        self._write_enable()
        self.cs.value(False)
        self.spi.send(
            bytearray([CMD_SE]) + addr.to_bytes(self.addr_bytes_num, "big"))
        self.cs.value(True)
        self._write_disable()
        self._wait_wip_reset()
        log.info("Sector %d erase finished." % sector_num)

    @_is_available("block_size")
    def block_erase(self, block_num: int) -> None:
        """Erase specified block."""
        if block_num > self._blocks_count:
            raise ValueError(
                "Block number (%d) is more than total block number (%d)." %
                (block_num, self._blocks_count))
        addr = block_num * self.sector_size
        self._wait_wip_reset()
        self._write_enable()
        self.cs.value(False)
        self.spi.send(
            bytearray([CMD_BE]) + addr.to_bytes(self.addr_bytes_num, "big"))
        self.cs.value(True)
        self._write_disable()
        self._wait_wip_reset()
        log.info("Block %d erase finished." % block_num)

    def read(self, offset: int, size: int) -> bytearray:
        """Read data from specified offset using specified size."""
        if (offset > self.size) or (offset + size > self.size):
            raise ValueError(
                "Read data from 0x%02X-0x%02X is out of range. Max address is 0x%02X."
                % (offset, offset + size, self.size))
        self.cs.value(False)
        self.spi.send(
            bytearray([CMD_READ]) +
            offset.to_bytes(self.addr_bytes_num, "big"))
        result = bytearray(size)
        self.spi.send_recv(bytearray([DUMMY_BYTE] * size), result)
        self.cs.value(True)
        log.info("Data read 0x%02X-0x%02X finished." % (offset, offset + size))
        return result

    def page_program(self, page_num: int, bytebuffer: bytearray) -> None:
        """Program page with bytes in bytebuffer"""
        if page_num > self._pages_count:
            raise ValueError(
                "Page number (%d) is more than total pages number (%d)." %
                (page_num, self._pages_count))
        if len(bytebuffer) > self.page_size:
            raise ValueError(
                "Bytebuffer length (%d) is more than page size (%d)." %
                (len(bytebuffer), self.page_size))
        addr = page_num * self.page_size
        self._wait_wip_reset()
        self._write_enable()
        self.cs.value(False)
        self.spi.send(
            bytearray([CMD_PP]) + addr.to_bytes(self.addr_bytes_num, "big"))
        self.spi.send(bytebuffer)
        self.cs.value(True)
        self._write_disable()
        self._wait_wip_reset()
        log.info('Page %d program finished.' % page_num)

    def _wait_wip_reset(self) -> None:
        """Wait for WIP=0. WIP is bit0 in Status Register."""
        sr = self.read_sr()
        while sr & SR_WIP:
            sr = self.read_sr()

    def _write_enable(self) -> None:
        """Enable Write operation via command."""
        self.cs.value(False)
        self.spi.send(bytearray([CMD_WREN]))
        self.cs.value(True)

    def _write_disable(self) -> None:
        """Disable Write operation via command."""
        self.cs.value(False)
        self.spi.send(bytearray([CMD_WRDI]))
        self.cs.value(True)

    def read_sr(self):
        """Read status register."""
        result = bytearray(2)
        self.cs.value(False)
        self.spi.send_recv(bytearray([CMD_RDSR, DUMMY_BYTE]), result)
        self.cs.value(True)
        return result[1]

    def write_sr(self, sr_val: int) -> None:
        """Write status register."""
        self._write_enable()
        self.cs.value(False)
        self.spi.send(bytearray([CMD_WRSR, sr_val & 0xFF]))
        self.cs.value(True)
        self._write_disable()
コード例 #28
0
    c = left_num << 8 | right_num
    return c


#####################################[NRF寄存器配置及接收发送函数]#######################################
#####################################[NRF寄存器配置及接收发送函数]#######################################
#####################################[NRF寄存器配置及接收发送函数]#######################################

sensor.reset()  # Reset and initialize the sensor.
sensor.set_pixformat(
    sensor.RGB565)  # Set pixel format to RGB565 (or GRAYSCALE)
sensor.set_framesize(sensor.QQCIF)  # Set frame size to QVGA (320x240)
sensor.skip_frames(time=2000)  # Wait for settings take effect.
clock = time.clock()  # Create a clock object to track the FPS.

spi = SPI(2, SPI.MASTER, baudrate=int(12500 * 1000), polarity=0, phase=0)
NRF_CSN_HIGH()
status = NRF_link_Check()  #返回nrf是否连接正常
NRF_TX_Mode()  #发送模式

buf_test = [
    12, 8, 0, 1, 8, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13,
    12, 11, 10, 9, 8, 7, 6, 5, 4, 6, 6, 2
]

while (True):

    #clock.tick()                    # Update the FPS clock.
    img = sensor.snapshot()  # Take a picture and return the image.
    #print(clock.fps())             # Note: OpenMV Cam runs about half as fast when connected
コード例 #29
0
class STAccel:
    def __init__(self, cs='PE3', spi=1, debug=False):
        self._debug = debug
        self.cs_pin = Pin(cs, Pin.OUT_PP, Pin.PULL_NONE)
        self.cs_pin.high()
        self.spi = SPI(spi, SPI.MASTER, baudrate=328125, polarity=0, phase=1,
                       bits=8)

        self.read_id()
        # First SPI read always returns 255 --> discard and read ID again
        self.who_am_i = self.read_id()
        self.debug("Accel-ID: %s" % self.who_am_i)

        if self.who_am_i == LIS302DL_WHO_AM_I_VAL:
            self.write_bytes(LIS302DL_CTRL_REG1_ADDR, LIS302DL_CONF)
            self.sensitivity = 18
        elif self.who_am_i == LIS3DSH_WHO_AM_I_VAL:
            self.write_bytes(LIS3DSH_CTRL_REG4_ADDR, LIS3DSH_CTRL_REG4_CONF)
            self.write_bytes(LIS3DSH_CTRL_REG5_ADDR, LIS3DSH_CTRL_REG5_CONF)
            self.sensitivity = 0.06 * 256
        else:
            msg = 'LIS302DL or LIS3DSH accelerometer not present'

            if self._debug:
                self.debug(msg)
            else:
                raise IOError(msg)

    def debug(self, *msg):
        if self._debug:
            print(" ".join(str(m) for m in msg))

    def _convert_raw_to_g(self, x):
        if x & 0x80:
            x -= 256

        return x * self.sensitivity / 1000

    def read_bytes(self, addr, nbytes):
        self.cs_pin.low()

        if nbytes > 1:
            self.spi.send(addr | READWRITE_CMD | MULTIPLEBYTE_CMD)
        else:
            self.spi.send(addr | READWRITE_CMD)

        # read data, MSB first
        buf = self.spi.recv(nbytes)
        self.cs_pin.high()
        return buf

    def write_bytes(self, addr, buf):
        if not isinstance(buf, (int, bytes, bytearray)):
            buf = bytes(buf)

        if not isinstance(buf, int) and len(buf) > 1:
            addr |= MULTIPLEBYTE_CMD

        self.cs_pin.low()
        self.spi.send(addr)
        self.spi.send(buf)
        self.cs_pin.high()

    def read_id(self):
        return self.read_bytes(WHO_AM_I_ADDR, 1)[0]

    def x(self):
        return self._convert_raw_to_g(self.read_bytes(OUT_X_ADDR, 1)[0])

    def y(self):
        return self._convert_raw_to_g(self.read_bytes(OUT_Y_ADDR, 1)[0])

    def z(self):
        return self._convert_raw_to_g(self.read_bytes(OUT_Z_ADDR, 1)[0])

    def xyz(self):
        return (self.x(), self.y(), self.z())
コード例 #30
0
dac.write(120)  # output between 0 and 255

# UART (serial bus)
# See pyb.UART.

from pyb import UART

uart = UART(1, 9600)
uart.write("hello")
uart.read(5)  # read up to 5 bytes

# SPI bus

from pyb import SPI

spi = SPI(1, SPI.CONTROLLER, baudrate=200000, polarity=1, phase=0)
spi.send("hello")
spi.recv(5)  # receive 5 bytes on the bus
spi.send_recv("hello")  # send and receive 5 bytes

# I2C bus

from machine import I2C

i2c = I2C("X", freq=400000)  # create hardware I2c object
i2c = I2C(scl="X1", sda="X2", freq=100000)  # create software I2C object

i2c.scan()  # returns list of peripheral addresses
i2c.writeto(0x42, "hello")  # write 5 bytes to peripheral with address 0x42
i2c.readfrom(0x42, 5)  # read 5 bytes from peripheral
コード例 #31
0
#
# Automatic Hatch Blob finder
#

import sensor, image, time
import pyb
from pyb import SPI

# Camera/Hardware Objects:
fmt = sensor.RGB565
res = sensor.QVGA
led1 = pyb.LED(1)
led2 = pyb.LED(2)

# SPI bus for lighting
spi = SPI(2, SPI.MASTER, 500000, polarity=1, phase=0, crc=None)
ledreset = bytearray(24)
# Reset the LED strip
spi.write(ledreset)

# Turn on Green lighting:
ledgreen = bytearray(24)
for ii in range(0, len(ledgreen)):
    ledgreen[ii] = 128

intensity = 110

for ii in range(0, 8):
    ledgreen[ii * 3] = 128 + intensity

spi.write(ledgreen)
コード例 #32
0
import time, os
from pyb import SPI, Pin, millis, Timer, I2C

from MAX31865 import MAX31865
from MAX31855 import MAX31855
from pyb_i2c_lcd import I2cLcd
from ALTA import ALTA

##  INITIALISE PLATINUM RESISTANCE THERMOMETER (MAX31865)
spi_PTD = SPI(
    1,  # pyBoard hardware SPI 1 (X5, X6, X7, X8)
    mode=SPI.MASTER,
    baudrate=100000,
    polarity=0,
    phase=1,
    firstbit=SPI.MSB)
cs_PTD = Pin('X5', mode=Pin.OUT_PP)  # Chip select pin

ptd = MAX31865(spi_PTD, cs_PTD)

## INITIALISE K TYPE THERMOCOUPLE (MAX31855)

# This is not currently in use but I'll leave the code for posterity
'''
spi_K = SPI(2, # pyBoard harware SPI 2 (Y5, Y6, Y7, Y8)
            mode=SPI.MASTER,
            baudrate=100000,
            polarity=0,
            phase=0,
            firstbit=SPI.MSB)
cs_K = Pin('Y5', Pin.OUT_PP) # Chip select pin
コード例 #33
0
ファイル: staccel.py プロジェクト: gradoj/micropython
 def __init__(self):
     self.cs_pin = Pin('PE3', Pin.OUT_PP, Pin.PULL_NONE)
     self.cs_pin.high()
     self.spi = SPI(1, SPI.MASTER, baudrate=328125, polarity=0, phase=1, bits=8)
     self.wr(LIS302DL_CTRL_REG1_ADDR, bytearray([LIS302DL_CONF]))
コード例 #34
0
ファイル: spiMgr.py プロジェクト: gratefulfrog/ArduGuitar
class SPIMgr():
    """
    This class provides an interface to the hardware level SPI object which
    it encapsulates as a member. 
    When creating an instance you must provide the pyboard side and 
    the latch pin's name, eg.
    >>> from spiMgr import SPIMgr
    >>> from state import State
    >>> s = SPIMgr(State.spiOnX,State.spiLatchPinName)
    >>> print(s)
    SPIMgr:
    SPI:
	BoardSide:	1
	MasterOrSlave:	Master
    STCP:
    Pin:
	LatchPin:	X5
	PinOut:	OUT_PP
	Value:	0
    ++
    The class only provides one method: update(bitArray).
    This method is called with an array of ints representing the 
    bits to be set to one via an SPI call. The process is
    1. set the latch pin LOW
    2. send the bits, int by int
    3. set the latch pin to high
    Note that when doing Off-board tests, the 'send' message will 
    appear twice.
    usage:
    >>> s.update([0,1,2,4,8,16])
    Pin:
	LatchPin:	X5
	PinOut:	OUT_PP
	Value:	0
	set: LOW
    Simulated: send:	0b0
    send:	0b0
    Simulated: send:	0b1
    send:	0b1
    Simulated: send:	0b10
    send:	0b10
    Simulated: send:	0b100
    send:	0b100
    Simulated: send:	0b1000
    send:	0b1000
    Simulated: send:	0b10000
    send:	0b10000
    Pin:
	LatchPin:	X5
	PinOut:	OUT_PP
	Value:	1
	set: HIGH
    """
    def __init__(self,spiOnX,latchPin):
        # create an SPI.MASTER instance on the 'X' side of the board, 
        # first arg=1 means 'X side' of the board
        boardSide = 1
        if not spiOnX:
            boardSide = 2
        self.spi = SPI(boardSide,SPI.MASTER)
        # create the stcp pin on the "latch" pin
        self.stcp = Pin(latchPin, Pin.OUT_PP)

    def update(self,bitArray):
        # send the data bits to the shift register
        # unset the latch
        self.stcp.low()
        # send the bits
        i=0 # remove for production
        for r in bitArray:
            self.spi.send(r)
            #### COMMENT NEXT LINE FOR Off-Board TESTS!
            #State.printT('send reg:\t' + str(i)+ '\t{0:08b}'.format(r))
            i += 1  # remove for production
        # turn on the latch
        self.stcp.high()

    def __repr__(self):
        return 'SPIMgr:' + \
            '\n' + str(self.spi) + \
            '\nSTCP:\n' + str(self.stcp)
コード例 #35
0
ファイル: spi.py プロジェクト: jlillest/micropython
'''
SPI test for the CC3200 based boards.
'''

from pyb import SPI
import os

machine = os.uname().machine
if 'LaunchPad' in machine:
    spi_pins = ('GP14', 'GP16', 'GP30')
elif 'WiPy' in machine:
    spi_pins = ('GP14', 'GP16', 'GP30')
else:
    raise Exception('Board not supported!')

spi = SPI(0, SPI.MASTER, baudrate=2000000, polarity=0, phase=0, firstbit=SPI.MSB, pins=spi_pins)
print(spi)
spi = SPI(baudrate=5000000)
print(spi)
spi = SPI(0, SPI.MASTER, baudrate=200000, bits=16, polarity=0, phase=0)
print(spi)
spi = SPI(0, SPI.MASTER, baudrate=10000000, polarity=0, phase=1)
print(spi)
spi = SPI(0, SPI.MASTER, baudrate=5000000, bits=32, polarity=1, phase=0)
print(spi)
spi = SPI(0, SPI.MASTER, baudrate=10000000, polarity=1, phase=1)
print(spi)
spi.init(baudrate=20000000, polarity=0, phase=0)
print(spi)
spi=SPI()
print(spi)
コード例 #36
0
ファイル: mains.py プロジェクト: zxlcmc/micropython-samples
# SAMPLING
NSAMPLES = const(400)
def arr_gen(n):
    for _ in range(n):
        yield 0

isamples = array('h', arr_gen(NSAMPLES))
vsamples = array('h', arr_gen(NSAMPLES))
gc.collect()

# HARDWARE
vadc = ADC(Pin.board.X19)
iadc = ADC(Pin.board.X20)

spi = SPI(1)
spi.init(SPI.MASTER, polarity = 0, phase = 0)

# ************* Programmable Gain Amplifier *************

class MCP6S91():
    CHANNEL_ADDR = 0x41
    GAIN_ADDR = 0x40
    GAINVALS = (1, 2, 4, 5, 8, 10, 16, 32)
    PINS = ('X5', 'X4')
    def __init__(self, devno):
        try:
            self.csn = Pin(MCP6S91.PINS[devno], mode = Pin.OUT_PP)
        except IndexError:
            raise ValueError('MCP6S91 device no. must be 0 or 1.')
        self.csn.value(1)
コード例 #37
0
from max7219 import LedMatrix8x8
from pyb import Pin, SPI
spi = SPI(1, SPI.MASTER)
load = Pin('X1')
display = LedMatrix8x8(spi, load)

display.clear()
display.show()

teststr = 'HELLO WORLD'
for c in teststr:
    display.print(c)
    display.show()
    pyb.delay(500)
コード例 #38
0
ファイル: SPI.py プロジェクト: liudannew/NuMicroPy
from pyb import SPI
spi0=SPI(0, SPI.MASTER, baudrate=5000, bits=32)
data = spi0.read(64)
コード例 #39
0
 def __init__(self, spi, csn, ce):
     csn0 = Pin(csn, mode=Pin.OUT, value=1)
     ce0 = Pin(ce, mode=Pin.OUT, value=0)
     self.nrf = NRF24L01(SPI(spi), csn0, ce0, payload_size=8)