|
| 1 | +.. _zephyr_quickref: |
| 2 | + |
| 3 | +Quick reference for the Zephyr port |
| 4 | +=================================== |
| 5 | + |
| 6 | +Below is a quick reference for the Zephyr port. If it is your first time working with this port please consider reading the following sections first: |
| 7 | + |
| 8 | +.. toctree:: |
| 9 | + :maxdepth: 1 |
| 10 | + |
| 11 | + general.rst |
| 12 | + tutorial/index.rst |
| 13 | + |
| 14 | +Running MicroPython |
| 15 | +------------------- |
| 16 | + |
| 17 | +See the corresponding section of the tutorial: :ref:`intro`. |
| 18 | + |
| 19 | +Delay and timing |
| 20 | +---------------- |
| 21 | + |
| 22 | +Use the :mod:`time <utime>` module:: |
| 23 | + |
| 24 | + import time |
| 25 | + |
| 26 | + time.sleep(1) # sleep for 1 second |
| 27 | + time.sleep_ms(500) # sleep for 500 milliseconds |
| 28 | + time.sleep_us(10) # sleep for 10 microseconds |
| 29 | + start = time.ticks_ms() # get millisecond counter |
| 30 | + delta = time.ticks_diff(time.ticks_ms(), start) # compute time difference |
| 31 | + |
| 32 | +Pins and GPIO |
| 33 | +------------- |
| 34 | + |
| 35 | +Use the :ref:`machine.Pin <machine.Pin>` class:: |
| 36 | + |
| 37 | + from machine import Pin |
| 38 | + |
| 39 | + pin = Pin(("GPIO_1", 21), Pin.IN) # create input pin on GPIO1 |
| 40 | + print(pin) # print pin port and number |
| 41 | + |
| 42 | + pin.init(Pin.OUT, Pin.PULL_UP, value=1) # reinitialize pin |
| 43 | + |
| 44 | + pin.value(1) # set pin to high |
| 45 | + pin.value(0) # set pin to low |
| 46 | + |
| 47 | + pin.on() # set pin to high |
| 48 | + pin.off() # set pin to low |
| 49 | + |
| 50 | + pin = Pin(("GPIO_1", 21), Pin.IN) # create input pin on GPIO1 |
| 51 | + |
| 52 | + pin = Pin(("GPIO_1", 21), Pin.OUT, value=1) # set pin high on creation |
| 53 | + |
| 54 | + pin = Pin(("GPIO_1", 21), Pin.IN, Pin.PULL_UP) # enable internal pull-up resistor |
| 55 | + |
| 56 | + switch = Pin(("GPIO_2", 6), Pin.IN) # create input pin for a switch |
| 57 | + switch.irq(lambda t: print("SW2 changed")) # enable an interrupt when switch state is changed |
| 58 | + |
| 59 | +Hardware I2C bus |
| 60 | +---------------- |
| 61 | + |
| 62 | +Hardware I2C is accessed via the :ref:`machine.I2C <machine.I2C>` class:: |
| 63 | + |
| 64 | + from machine import I2C |
| 65 | + |
| 66 | + i2c = I2C("I2C_0") # construct an i2c bus |
| 67 | + print(i2c) # print device name |
| 68 | + |
| 69 | + i2c.scan() # scan the device for available I2C slaves |
| 70 | + |
| 71 | + i2c.readfrom(0x1D, 4) # read 4 bytes from slave 0x1D |
| 72 | + i2c.readfrom_mem(0x1D, 0x0D, 1) # read 1 byte from slave 0x1D at slave memory 0x0D |
| 73 | + |
| 74 | + i2c.writeto(0x1D, b'abcd') # write to slave with address 0x1D |
| 75 | + i2c.writeto_mem(0x1D, 0x0D, b'ab') # write to slave 0x1D at slave memory 0x0D |
| 76 | + |
| 77 | + buf = bytearray(8) # create buffer of size 8 |
| 78 | + i2c.writeto(0x1D, b'abcd') # write buf to slave 0x1D |
| 79 | + |
| 80 | +Hardware SPI bus |
| 81 | +---------------- |
| 82 | + |
| 83 | +Hardware SPI is accessed via the :ref:`machine.SPI <machine.SPI>` class:: |
| 84 | + |
| 85 | + from machine import SPI |
| 86 | + |
| 87 | + spi = SPI("SPI_0") # construct a spi bus with default configuration |
| 88 | + spi.init(baudrate=100000, polarity=0, phase=0, bits=8, firstbit=SPI.MSB) # set configuration |
| 89 | + |
| 90 | + # equivalently
10000
, construct spi bus and set configuration at the same time |
| 91 | + spi = SPI("SPI_0", baudrate=100000, polarity=0, phase=0, bits=8, firstbit=SPI.MSB) |
| 92 | + print(spi) # print device name and bus configuration |
| 93 | + |
| 94 | + spi.read(4) # read 4 bytes on MISO |
| 95 | + spi.read(4, write=0xF) # read 4 bytes while writing 0xF on MOSI |
| 96 | + |
| 97 | + buf = bytearray(8) # create a buffer of size 8 |
| 98 | + spi.readinto(buf) # read into the buffer (reads number of bytes equal to the buffer size) |
| 99 | + spi.readinto(buf, 0xF) # read into the buffer while writing 0xF on MOSI |
| 100 | + |
| 101 | + spi.write(b'abcd') # write 4 bytes on MOSI |
| 102 | + |
| 103 | + buf = bytearray(4) # create buffer of size 8 |
| 104 | + spi.write_readinto(b'abcd', buf) # write to MOSI and read from MISO into the buffer |
| 105 | + spi.write_readinto(buf, buf) # write buf to MOSI and read back into the buf |
| 106 | + |
| 107 | +Disk Access |
| 108 | +----------- |
| 109 | + |
| 110 | +Use the :ref:`zephyr.DiskAccess <zephyr.DiskAccess>` class to support filesystem:: |
| 111 | + |
| 112 | + import os |
| 113 | + from zephyr import DiskAccess |
| 114 | + |
| 115 | + block_dev = DiskAccess('SDHC') # create a block device object for an SD card |
| 116 | + os.VfsFat.mkfs(block_dev) # create FAT filesystem object using the disk storage block |
| 117 | + os.mount(block_dev, '/sd') # mount the filesystem at the SD card subdirectory |
| 118 | + |
| 119 | + # with the filesystem mounted, files can be manipulated as normal |
| 120 | + with open('/sd/hello.txt','w') as f: # open a new file in the directory |
| 121 | + f.write('Hello world') # write to the file |
| 122 | + print(open('/sd/hello.txt').read()) # print contents of the file |
| 123 | + |
| 124 | +Flash Area |
| 125 | +---------- |
| 126 | + |
| 127 | +Use the :ref:`zephyr.FlashArea <zephyr.FlashArea>` class to support filesystem:: |
| 128 | + |
| 129 | + import os |
| 130 | + from zephyr import FlashArea |
| 131 | + |
| 132 | + block_dev = FlashArea(4, 4096) # creates a block device object in the frdm-k64f flash scratch partition |
| 133 | + os.VfsLfs2.mkfs(block_dev) # create filesystem in lfs2 format using the flash block device |
| 134 | + os.mount(block_dev, '/flash') # mount the filesystem at the flash subdirectory |
| 135 | + |
| 136 | + # with the filesystem mounted, files can be manipulated as normal |
| 137 | + with open('/flash/hello.txt','w') as f: # open a new file in the directory |
| 138 | + f.write('Hello world') # write to the file |
| 139 | + print(open('/flash/hello.txt').read()) # print contents of the file |
| 140 | + |
| 141 | +Sensor |
| 142 | +------ |
| 143 | + |
| 144 | +Use the :ref:`zsensor.Sensor <zsensor.Sensor>` class to access sensor data:: |
| 145 | + |
| 146 | + import zsensor |
| 147 | + from zsensor import Sensor |
| 148 | + |
| 149 | + accel = Sensor("FXOX8700") # create sensor object for the accelerometer |
| 150 | + |
| 151 | + accel.measure() # obtain a measurement reading from the accelerometer |
| 152 | + |
| 153 | + # each of these prints the value taken by measure() |
| 154 | + accel.float(zsensor.ACCEL_X) # print measurement value for accelerometer X-axis sensor channel as float |
| 155 | + accel.millis(zsensor.ACCEL_Y) # print measurement value for accelerometer Y-axis sensor channel in millionths |
| 156 | + accel.micro(zsensor.ACCEL_Z) # print measurement value for accelerometer Z-axis sensor channel in thousandths |
| 157 | + accel.int(zsensor.ACCEL_X) # print measurement integer value only for accelerometer X-axis sensor channel |
0 commit comments