{ "cells": [ { "cell_type": "markdown", "id": "1c4f8a37", "metadata": {}, "source": [ "\n", "*This notebook contains material from [cbe61622](https://jckantor.github.io/cbe61622);\n", "content is available [on Github](https://github.com/jckantor/cbe61622.git).*\n" ] }, { "cell_type": "markdown", "id": "9cd169c5", "metadata": {}, "source": [ "\n", "< [6.4 Event Driven Programming](https://jckantor.github.io/cbe61622/06.04-Event-Driven-Programming.html) | [Contents](toc.html) | [7.0 Interfacing with Laboratory Equipment](https://jckantor.github.io/cbe61622/07.00-Interfacing_with_Laboratory_Equipment.html) >

\"Open

\"Download\"" ] }, { "cell_type": "markdown", "id": "e2c9e693-db97-4b0c-90db-0389fdf20b75", "metadata": { "nbpages": { "level": 1, "link": "[6.5 PIO Programming](https://jckantor.github.io/cbe61622/06.05-PIO-Programming.html#6.5-PIO-Programming)", "section": "6.5 PIO Programming" } }, "source": [ "# 6.5 PIO Programming\n", "\n", "Resources:\n", "\n", "* [RP2040 Datasheet Section 3.4](https://datasheets.raspberrypi.com/rp2040/rp2040-datasheet.pdf)\n", "\n", "Life with David" ] }, { "cell_type": "markdown", "id": "e438e7c8-97ca-46fd-ad2a-c4bbb0d80147", "metadata": { "nbpages": { "level": 2, "link": "[6.5.1 Setting multiple pins from Python](https://jckantor.github.io/cbe61622/06.05-PIO-Programming.html#6.5.1-Setting-multiple-pins-from-Python)", "section": "6.5.1 Setting multiple pins from Python" } }, "source": [ "## 6.5.1 Setting multiple pins from Python" ] }, { "cell_type": "code", "execution_count": 5, "id": "3442d1f7-7282-4147-a4d6-a0e5e9d4989c", "metadata": { "nbpages": { "level": 2, "link": "[6.5.1 Setting multiple pins from Python](https://jckantor.github.io/cbe61622/06.05-PIO-Programming.html#6.5.1-Setting-multiple-pins-from-Python)", "section": "6.5.1 Setting multiple pins from Python" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "serial exception on close write failed: [Errno 6] Device not configured\n", "Found serial ports: /dev/cu.usbmodem14101, /dev/cu.Bluetooth-Incoming-Port \n", "\u001b[34mConnecting to --port=/dev/cu.usbmodem14101 --baud=115200 \u001b[0m\n", "\u001b[34mReady.\n", "\u001b[0m" ] } ], "source": [ "%serialconnect\n", "\n", "from machine import Pin\n", "import time\n", "from rp2 import PIO, StateMachine, asm_pio\n", "\n", "# decorator to translate to PIO machine code\n", "@asm_pio(\n", " out_init = (rp2.PIO.OUT_LOW,) * 8, # initialize 8 consecutive pins\n", " out_shiftdir = rp2.PIO.SHIFT_RIGHT) # output lsb bits first\n", "\n", "def parallel_prog():\n", " pull(block) # pull data from Tx FIFO. Wait for data\n", " out(pins, 8) # send 8 bits from OSR to pins\n", "\n", "# create an instance of the state machine\n", "sm = StateMachine(0, parallel_prog, freq=1000000, out_base=Pin(0))\n", "\n", "# start the state machine\n", "sm.active(1)\n", "\n", "for n in range(256):\n", " sm.put(n)\n", " time.sleep(0.01)\n" ] }, { "cell_type": "markdown", "id": "937f53ce-4dac-4e1b-99b6-a8877178ecd8", "metadata": { "nbpages": { "level": 2, "link": "[6.5.2 Writing Stepper Steps to pins](https://jckantor.github.io/cbe61622/06.05-PIO-Programming.html#6.5.2-Writing-Stepper-Steps-to-pins)", "section": "6.5.2 Writing Stepper Steps to pins" } }, "source": [ "## 6.5.2 Writing Stepper Steps to pins\n" ] }, { "cell_type": "code", "execution_count": 39, "id": "c681a96e-c4f2-4cf3-874f-f32d1339de22", "metadata": { "nbpages": { "level": 2, "link": "[6.5.2 Writing Stepper Steps to pins](https://jckantor.github.io/cbe61622/06.05-PIO-Programming.html#6.5.2-Writing-Stepper-Steps-to-pins)", "section": "6.5.2 Writing Stepper Steps to pins" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Found serial ports: /dev/cu.usbmodem14101, /dev/cu.Bluetooth-Incoming-Port \n", "\u001b[34mConnecting to --port=/dev/cu.usbmodem14101 --baud=115200 \u001b[0m\n", "\u001b[34mReady.\n", "\u001b[0m." ] } ], "source": [ "%serialconnect\n", "\n", "from machine import Pin\n", "import time\n", "from rp2 import PIO, StateMachine, asm_pio\n", "\n", "# decorator to translate to PIO machine code\n", "@asm_pio(\n", " out_init = (rp2.PIO.OUT_LOW,) * 4, # initialize 8 consecutive pins\n", " out_shiftdir = rp2.PIO.SHIFT_RIGHT) # output lsb bits first\n", "\n", "def stepper_step():\n", " pull(block) # pull data from Tx FIFO. Wait for data\n", " out(pins, 4) # send 8 bits from OSR to pins\n", "\n", "# create an instance of the state machine\n", "sm = StateMachine(0, stepper_step, freq=1000000, out_base=Pin(0))\n", "\n", "# start the state machine\n", "sm.active(1)\n", "\n", "step_sequence = [8, 12, 4, 6, 2, 3, 1, 9]\n", " \n", "for n in range(500):\n", " sm.put(step_sequence[n % len(step_sequence)])\n", " time.sleep(0.01)" ] }, { "cell_type": "code", "execution_count": 94, "id": "0cdfe539-71bc-410a-a0af-c01a66619ac8", "metadata": { "nbpages": { "level": 2, "link": "[6.5.2 Writing Stepper Steps to pins](https://jckantor.github.io/cbe61622/06.05-PIO-Programming.html#6.5.2-Writing-Stepper-Steps-to-pins)", "section": "6.5.2 Writing Stepper Steps to pins" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Found serial ports: /dev/cu.usbmodem14101, /dev/cu.Bluetooth-Incoming-Port \n", "\u001b[34mConnecting to --port=/dev/cu.usbmodem14101 --baud=115200 \u001b[0m\n", "\u001b[34mReady.\n", "\u001b[0m" ] } ], "source": [ "%serialconnect\n", "\n", "from machine import Pin\n", "import time\n", "from rp2 import PIO, StateMachine, asm_pio\n", "\n", "# decorator to translate to PIO machine code\n", "@asm_pio(\n", " set_init = rp2.PIO.OUT_LOW)\n", "\n", "def count_blink():\n", " pull()\n", " mov(x, osr)\n", " label(\"count\")\n", " set(pins, 1)\n", " set(y, 100)\n", " label(\"on\")\n", " nop() [1]\n", " jmp(y_dec, \"on\")\n", " set(pins, 0)\n", " nop() [19]\n", " nop() [19]\n", " nop() [19]\n", " nop() [19]\n", " jmp(x_dec, \"count\")\n", "\n", "# create an instance of the state machine\n", "sm = StateMachine(0, count_blink, freq=2000, set_base=Pin(25))\n", "\n", "# start the state machine\n", "sm.active(1)\n", "sm.put(20)\n" ] }, { "cell_type": "code", "execution_count": 92, "id": "8deb1349-cbb3-4730-a40b-95d50f7c5205", "metadata": { "nbpages": { "level": 2, "link": "[6.5.2 Writing Stepper Steps to pins](https://jckantor.github.io/cbe61622/06.05-PIO-Programming.html#6.5.2-Writing-Stepper-Steps-to-pins)", "section": "6.5.2 Writing Stepper Steps to pins" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "serial exception on close write failed: [Errno 6] Device not configured\n", "Found serial ports: /dev/cu.usbmodem14101, /dev/cu.Bluetooth-Incoming-Port \n", "\u001b[34mConnecting to --port=/dev/cu.usbmodem14101 --baud=115200 \u001b[0m\n", "\u001b[34mReady.\n", "\u001b[0m8\n", "12\n", "4\n", "6\n", "2\n", "3\n", "1\n", "9\n", "8\n", "12\n" ] } ], "source": [ "%serialconnect\n", "\n", "from machine import Pin\n", "import time\n", "from rp2 import PIO, StateMachine, asm_pio\n", "\n", "# decorator to translate to PIO machine code\n", "@asm_pio(\n", " out_init = (rp2.PIO.OUT_LOW,) * 4, # initialize 8 consecutive pins\n", " out_shiftdir = rp2.PIO.SHIFT_RIGHT) # output lsb bits first\n", "\n", "def stepper_step():\n", " pull(block) # pull data from Tx FIFO. Wait for data\n", " out(pins, 4) # send 8 bits from OSR to pins\n", "\n", "# create an instance of the state machine\n", "sm = StateMachine(0, stepper_step, freq=1000000, out_base=Pin(0))\n", "\n", "# start the state machine\n", "sm.active(1)\n", "\n", "step_sequence = [8, 12, 4, 6, 2, 3, 1, 9]\n", "\n", "def step():\n", " pos = 0\n", " while True:\n", " coils = step_sequence[pos % len(step_sequence)]\n", " yield coils\n", " pos += 1\n", " \n", "stepper = step()\n", "for k in range(10):\n", " c = next(stepper)\n", " print(c)\n", " \n", "\n", "\n", "#for n in range(100):\n", "# for step in step_sequence:\n", "# sm.put(step)\n", "# time.sleep(0.01)" ] }, { "cell_type": "markdown", "id": "52c14e3e-4907-438c-a8f9-9551b7c72295", "metadata": { "nbpages": { "level": 2, "link": "[6.5.3 Interacting PIO programs](https://jckantor.github.io/cbe61622/06.05-PIO-Programming.html#6.5.3-Interacting-PIO-programs)", "section": "6.5.3 Interacting PIO programs" } }, "source": [ "## 6.5.3 Interacting PIO programs\n", "\n", "Section 3.2.7 of the RP2040 data sheet describes how interactions between state machines on the same PIO processor can be managed. Here will demonstrate this in a few steps. For the first step, we create a counter that accepts an integer from the FIFO buffer, then blinks an led a fixed number of times." ] }, { "cell_type": "code", "execution_count": 28, "id": "84d3b17e-7d59-48ed-92fb-2e2253f1ad66", "metadata": { "nbpages": { "level": 2, "link": "[6.5.3 Interacting PIO programs](https://jckantor.github.io/cbe61622/06.05-PIO-Programming.html#6.5.3-Interacting-PIO-programs)", "section": "6.5.3 Interacting PIO programs" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "serial exception on close write failed: [Errno 6] Device not configured\n", "Found serial ports: /dev/cu.usbmodem14101, /dev/cu.Bluetooth-Incoming-Port \n", "\u001b[34mConnecting to --port=/dev/cu.usbmodem14101 --baud=115200 \u001b[0m\n", "\u001b[34mReady.\n", "\u001b[0m" ] } ], "source": [ "%serialconnect\n", "\n", "from machine import Pin\n", "import time\n", "import rp2\n", "from rp2 import PIO, StateMachine, asm_pio\n", "\n", "@asm_pio(out_init = rp2.PIO.OUT_LOW)\n", "def count_blink():\n", " pull(block) # wait for data on Tx FIFO\n", " set(pins, 1)\n", " set(x, osr)\n", " \n", "# create an instance of the state machine\n", "sm0 = StateMachine(0, count_blink, freq=2000, out_base=Pin(25))\n", "\n", "# start the state machine\n", "sm0.active(1)\n", "sm0.put(1)\n", "time.sleep(2)\n", "sm0.active(0)" ] }, { "cell_type": "code", "execution_count": 37, "id": "16911726-a260-488e-a64d-e74fd0f54060", "metadata": { "nbpages": { "level": 2, "link": "[6.5.3 Interacting PIO programs](https://jckantor.github.io/cbe61622/06.05-PIO-Programming.html#6.5.3-Interacting-PIO-programs)", "section": "6.5.3 Interacting PIO programs" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Found serial ports: /dev/cu.usbmodem14101, /dev/cu.Bluetooth-Incoming-Port \n", "\u001b[34mConnecting to --port=/dev/cu.usbmodem14101 --baud=115200 \u001b[0m\n", "\u001b[34mReady.\n", "\u001b[0m.." ] } ], "source": [ "%serialconnect\n", "\n", "from machine import Pin\n", "import time\n", "from rp2 import PIO, StateMachine, asm_pio\n", "\n", "# decorator to translate to PIO machine code\n", "@asm_pio(\n", " out_init = (rp2.PIO.OUT_LOW,) * 4, # initialize 8 consecutive pins\n", " out_shiftdir = rp2.PIO.SHIFT_RIGHT) # output lsb bits first\n", "\n", "def stepper_step():\n", " pull(block) # pull data from Tx FIFO. Wait for data\n", " out(pins, 4) # send 8 bits from OSR to pins\n", "\n", "# create an instance of the state machine\n", "sm = StateMachine(0, stepper_step, freq=1000000, out_base=Pin(0))\n", "\n", "# start the state machine\n", "sm.active(1)\n", "\n", "step_sequence = [8, 12, 4, 6, 2, 3, 1, 9]\n", " \n", "for n in range(1000):\n", " sm.put(step_sequence[n % len(step_sequence)])\n", " time.sleep(0.01)\n", " " ] }, { "cell_type": "code", "execution_count": null, "id": "a965c111-5f3a-4764-88a2-ffaa62f84db3", "metadata": { "nbpages": { "level": 2, "link": "[6.5.3 Interacting PIO programs](https://jckantor.github.io/cbe61622/06.05-PIO-Programming.html#6.5.3-Interacting-PIO-programs)", "section": "6.5.3 Interacting PIO programs" } }, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "29d5bda5", "metadata": {}, "source": [ "\n", "< [6.4 Event Driven Programming](https://jckantor.github.io/cbe61622/06.04-Event-Driven-Programming.html) | [Contents](toc.html) | [7.0 Interfacing with Laboratory Equipment](https://jckantor.github.io/cbe61622/07.00-Interfacing_with_Laboratory_Equipment.html) >

\"Open

\"Download\"" ] } ], "metadata": { "kernelspec": { "display_name": "MicroPython - USB", "language": "micropython", "name": "micropython" }, "language_info": { "codemirror_mode": "python", "file_extension": ".py", "mimetype": "text/python", "name": "micropython" } }, "nbformat": 4, "nbformat_minor": 5 }