summaryrefslogtreecommitdiffstatshomepage
path: root/docs/zephyr/tutorial
diff options
context:
space:
mode:
authorJulia Hathaway <julia.hathaway@nxp.com>2021-08-02 17:01:57 -0500
committerDamien George <damien@micropython.org>2021-08-13 20:20:57 +1000
commit333e16521bc8af8a55f362c1bad2f21c4704d20b (patch)
tree280617f7b9c28b6e2c46c20b0812441455311f26 /docs/zephyr/tutorial
parent42d1a1635cc35fd3a9ae28b19685184fc3c23f1e (diff)
downloadmicropython-333e16521bc8af8a55f362c1bad2f21c4704d20b.tar.gz
micropython-333e16521bc8af8a55f362c1bad2f21c4704d20b.zip
docs/zephyr: Add quick reference for the Zephyr port.
Includes an introduction to using the Zephyr port on MicroPython. The quickref details examples of how to use each module the port currently supports. The tutorial provides additional details for Zephyr specific modules. Signed-off-by: Julia Hathaway <julia.hathaway@nxp.com>
Diffstat (limited to 'docs/zephyr/tutorial')
-rw-r--r--docs/zephyr/tutorial/index.rst16
-rw-r--r--docs/zephyr/tutorial/intro.rst30
-rw-r--r--docs/zephyr/tutorial/pins.rst46
-rw-r--r--docs/zephyr/tutorial/repl.rst75
-rw-r--r--docs/zephyr/tutorial/storage.rst56
5 files changed, 223 insertions, 0 deletions
diff --git a/docs/zephyr/tutorial/index.rst b/docs/zephyr/tutorial/index.rst
new file mode 100644
index 0000000000..218156b3b0
--- /dev/null
+++ b/docs/zephyr/tutorial/index.rst
@@ -0,0 +1,16 @@
+.. _zephyr_tutorial:
+
+MicroPython tutorial for the Zephyr port
+========================================
+
+This tutorial is intended to get you started with the Zephyr port.
+
+.. toctree::
+ :maxdepth: 1
+ :numbered:
+
+ intro.rst
+ repl.rst
+ storage.rst
+ pins.rst
+
diff --git a/docs/zephyr/tutorial/intro.rst b/docs/zephyr/tutorial/intro.rst
new file mode 100644
index 0000000000..ffdbea8b39
--- /dev/null
+++ b/docs/zephyr/tutorial/intro.rst
@@ -0,0 +1,30 @@
+.. _intro_zephyr:
+
+Getting started with MicroPython on the Zephyr port
+===================================================
+
+Let’s get started!
+
+Requirements
+------------
+
+To use the MicroPython Zephyr port, you will need a Zephyr supported board (for a list of acceptable
+boards see :ref:`zephyr_general`).
+
+Powering up
+-----------
+
+If your board has a USB connector on it then most likely it is powered
+through this when connected to your PC. Otherwise you will need to power
+it directly. Please refer to the documentation for your board for
+further details.
+
+Getting and deploying the firmware
+----------------------------------
+
+The first step you will need to do is either clone the `MicroPython repository <https://github.com/micropython/micropython.git>`_
+or download it from the `MicroPython downloads page <http://micropython.org/download>`_. If you are an end user of MicroPython,
+it is recommended to start with the stable firmware builds. If you would like to work on development, you may follow the daily
+builds on git.
+
+Next, follow the Zephyr port readme document (``ports/zephyr/README.md``) to build and run the application on your board.
diff --git a/docs/zephyr/tutorial/pins.rst b/docs/zephyr/tutorial/pins.rst
new file mode 100644
index 0000000000..8e1d6602af
--- /dev/null
+++ b/docs/zephyr/tutorial/pins.rst
@@ -0,0 +1,46 @@
+.. _pins_zephyr:
+
+GPIO Pins
+=========
+
+Use :ref:`machine.Pin <machine.Pin>` to control I/O pins.
+
+For Zephyr, pins are initialized using a tuple of port and pin number ``(\"GPIO_x\", pin#)``
+for the ``id`` value. For example to initialize a pin for the red LED on a FRDM-k64 board::
+
+ LED = Pin(("GPIO_1", 22), Pin.OUT)
+
+Reference your board's datasheet or Zephyr documentation for pin numbers, see below for more examples.
+
+.. list-table:: Pin Formatting
+ :header-rows: 1
+
+ * - Board
+ - Pin
+ - Format
+ * - frdm_k64f
+ - Red LED = PTB22
+ - ("GPIO_1", 22)
+ * - 96b_carbon
+ - LED1 = PD2
+ - ("GPIOD", 2)
+ * - mimxrt685_evk_cm33
+ - Green LED = PIO0_14
+ - ("GPIO0", 14)
+
+Interrupts
+----------
+
+The Zephyr port also supports interrupt handling for Pins using `machine.Pin.irq() <machine.Pin.irq>`.
+To respond to Pin change IRQs run::
+
+ from machine import Pin
+
+ SW2 = Pin(("GPIO_2", 6), Pin.IN) # create Pin object for switch 2
+ SW3 = Pin(("GPIO_0", 4), Pin.IN) # create Pin object for switch 3
+
+ SW2.irq(lambda t: print("SW2 changed")) # print message when SW2 state is changed (triggers change IRQ)
+ SW3.irq(lambda t: print("SW3 changed")) # print message when SW3 state is changed (triggers change IRQ)
+
+ while True: # wait
+ pass
diff --git a/docs/zephyr/tutorial/repl.rst b/docs/zephyr/tutorial/repl.rst
new file mode 100644
index 0000000000..a7e8955d0b
--- /dev/null
+++ b/docs/zephyr/tutorial/repl.rst
@@ -0,0 +1,75 @@
+Getting a MicroPython REPL prompt
+=================================
+
+REPL stands for Read Evaluate Print Loop, and is the name given to the
+interactive MicroPython prompt that you can access on your board through
+Zephyr. It is recommended to use REPL to test out your code and run commands.
+
+REPL over the serial port
+-------------------------
+
+The REPL is available on a UART serial peripheral specified for the board by
+the ``zephyr,console`` devicetree node. The baudrate of the REPL is 115200.
+If your board has a USB-serial convertor on it then you should be able to access
+the REPL directly from your PC.
+
+To access the prompt over USB-serial you will need to use a terminal emulator
+program. For a Linux or Mac machine, open a terminal and run::
+
+ screen /dev/ttyACM0 115200
+
+You can also try ``picocom`` or ``minicom`` instead of screen. You may have to use
+``/dev/ttyACM1`` or a higher number for ``ttyACM``. Additional permissions
+may be necessary to access this device (eg group ``uucp`` or ``dialout``, or use sudo).
+For Windows, get a terminal software, such as puTTY and connect via a serial session
+using the proper COM port.
+
+Using the REPL
+--------------
+
+With your serial program open (PuTTY, screen, picocom, etc) you may see a
+blank screen with a flashing cursor. Press Enter (or reset the board) and
+you should be presented with the following text::
+
+ *** Booting Zephyr OS build v2.6.0-rc1-416-g3056c5ec30ad ***
+ MicroPython v2.6.0-rc1-416-g3056c5ec30 on 2021-06-24; zephyr-frdm_k64f with mk64f12
+ Type "help()" for more information.
+ >>>
+
+Now you can try running MicroPython code directly on your board.
+
+Anything you type at the prompt, indicated by ``>>>``, will be executed after you press
+the Enter key. If there is an error with the text that you enter then an error
+message is printed.
+
+Start by typing the following at the prompt to make sure it is working::
+
+ >>> print("hello world!")
+ hello world!
+
+If you already know some python you can now try some basic commands here. For
+example::
+
+ >>> 1 + 2
+ 3
+ >>> 1 / 2
+ 0.5
+ >>> 3 * 'Zephyr'
+ ZephyrZephyrZephyr
+
+If your board has an LED, you can blink it using the following code::
+
+ >>>import time
+ >>>from machine import Pin
+
+ >>>LED = Pin(("GPIO_1", 21), Pin.OUT)
+ >>>while True:
+ ... LED.value(1)
+ ... time.sleep(0.5)
+ ... LED.value(0)
+ ... time.sleep(0.5)
+
+The above code uses an LED location for a FRDM-K64F board (port B, pin 21;
+following Zephyr conventions ports are identified by "GPIO_x", where *x*
+starts from 0). You will need to adjust it for another board using the board's
+reference materials.
diff --git a/docs/zephyr/tutorial/storage.rst b/docs/zephyr/tutorial/storage.rst
new file mode 100644
index 0000000000..f57a08fc40
--- /dev/null
+++ b/docs/zephyr/tutorial/storage.rst
@@ -0,0 +1,56 @@
+.. _storage_zephyr:
+
+Filesystems and Storage
+=======================
+
+Storage modules support virtual filesystem with FAT and littlefs formats, backed by either
+Zephyr DiskAccess or FlashArea (flash map) APIs depending on which the board supports.
+
+See `uos Filesystem Mounting <https://docs.micropython.org/en/latest/library/uos.html?highlight=os#filesystem-mounting>`_.
+
+Disk Access
+-----------
+
+The :ref:`zephyr.DiskAccess <zephyr.DiskAccess>` class can be used to access storage devices, such as SD cards.
+This class uses `Zephyr Disk Access API <https://docs.zephyrproject.org/latest/reference/storage/disk/access.html>`_ and
+implements the `uos.AbstractBlockDev` protocol.
+
+For use with SD card controllers, SD cards must be present at boot & not removed; they will
+be auto detected and initialized by filesystem at boot. Use the disk driver interface and a
+file system to access SD cards via disk access (see below).
+
+Example usage of FatFS with an SD card on the mimxrt1050_evk board::
+
+ import os
+ from zephyr import DiskAccess
+ bdev = zephyr.DiskAccess('SDHC') # create block device object using DiskAccess
+ os.VfsFat.mkfs(bdev) # create FAT filesystem object using the disk storage block
+ os.mount(bdev, '/sd') # mount the filesystem at the SD card subdirectory
+ with open('/sd/hello.txt','w') as f: # open a new file in the directory
+ f.write('Hello world') # write to the file
+ print(open('/sd/hello.txt').read()) # print contents of the file
+
+
+Flash Area
+----------
+
+The :ref:`zephyr.FlashArea <zephyr.FlashArea>` class can be used to implement a low-level storage system or
+customize filesystem configurations. To store persistent data on the device, using a higher-level filesystem
+API is recommended (see below).
+
+This class uses `Zephyr Flash map API <https://docs.zephyrproject.org/latest/reference/storage/flash_map/flash_map.html#>`_ and
+implements the `uos.AbstractBlockDev` protocol.
+
+Example usage with the internal flash on the reel_board or the rv32m1_vega_ri5cy board::
+
+ import os
+ from zephyr import FlashArea
+ bdev = FlashArea(FlashArea.STORAGE, 4096) # create block device object using FlashArea
+ os.VfsLfs2.mkfs(bdev) # create Little filesystem object using the flash area block
+ os.mount(bdev, '/flash') # mount the filesystem at the flash storage subdirectory
+ with open('/flash/hello.txt','w') as f: # open a new file in the directory
+ f.write('Hello world') # write to the file
+ print(open('/flash/hello.txt').read()) # print contents of the file
+
+For boards such as the frdm_k64f in which the MicroPython application spills into the default flash storage
+partition, use the scratch partition by replacing ``FlashArea.STORAGE`` with the integer value 4.