micropython: add micropython component

This commit is contained in:
KY-zhang-X
2022-09-29 12:10:37 +08:00
parent 1514f1cb9b
commit dd76146324
2679 changed files with 354110 additions and 0 deletions

View File

@@ -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

View File

@@ -0,0 +1,30 @@
.. _intro_zephyr:
Getting started with MicroPython on the Zephyr port
===================================================
Lets 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.

View File

@@ -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

View File

@@ -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 zephyr-v3.0.0 ***
MicroPython v1.18-169-g665f0e2a6-dirty on 2022-03-02; 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.

View File

@@ -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 `os Filesystem Mounting <https://docs.micropython.org/en/latest/library/os.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 `os.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 `os.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.