{
  "cells": [
    {
      "cell_type": "markdown",
      "id": "b2897831",
      "metadata": {},
      "source": [
        "### License\n",
        "Copyright (C) 2021-2022, Xilinx, Inc.\n",
        "<br>\n",
        "Copyright (C) 2022-2024, Advanced Micro Devices, Inc.\n",
        "<p>\n",
        "Licensed under the Apache License, Version 2.0 (the \"License\");\n",
        "you may not use this file except in compliance with the License.\n",
        "<p>\n",
        "You may obtain a copy of the License at <a href=\"http://www.apache.org/licenses/LICENSE-2.0\"?>http://www.apache.org/licenses/LICENSE-2.0</a><br><br>\n",
        "Unless required by applicable law or agreed to in writing, software\n",
        "distributed under the License is distributed on an \"AS IS\" BASIS, \n",
        "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
        "See the License for the specific language governing permissions and\n",
        "limitations under the License."
      ]
    },
    {
      "cell_type": "markdown",
      "id": "c6c0e383",
      "metadata": {},
      "source": [
        "# ChipScoPy System Monitor Example\n",
        "\n",
        "\n",
        "<img src=\"../img/api_overview.png\" width=\"500\" align=\"left\">"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "e114a2aa",
      "metadata": {},
      "source": [
        "## Description\n",
        "\n",
        "\n",
        "This demo shows how to take and display measurements with the System Monitor.\n",
        "\n",
        "\n",
        "## Requirements\n",
        "- Local or remote Xilinx Versal board, such as a VCK190\n",
        "- Xilinx hw_server 2024.1 installed and running\n",
        "- Xilinx cs_server 2024.1 installed and running\n",
        "- Python 3.8 or greater installed\n",
        "- ChipScoPy 2024.1 installed\n",
        "- Jupyter notebook support installed - Please do so, using the command `pip install chipscopy[jupyter]`"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "d09f59d5",
      "metadata": {},
      "source": [
        "## 1 - Initialization: Imports and File Paths\n",
        "\n",
        "After this step,\n",
        "- Required functions and classes are imported\n",
        "- URL paths are set correctly\n",
        "- File paths to example files are set correctly"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "id": "03a55426",
      "metadata": {},
      "outputs": [],
      "source": [
        "import os\n",
        "import time\n",
        "from chipscopy import get_design_files\n",
        "from chipscopy import __version__, dm\n",
        "from chipscopy import create_session, report_versions, delete_session"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 2,
      "id": "d1ba9fa3",
      "metadata": {},
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "HW_URL: TCP:localhost:3121\n",
            "CS_URL: TCP:localhost:3042\n",
            "PROGRAMMING_FILE: /scratch/chipscopy/venv/lib/python3.11/site-packages/chipscopy/examples/designs/vck190/production/chipscopy_ced/chipscopy_wrapper.pdi\n",
            "PROBES_FILE:/scratch/chipscopy/venv/lib/python3.11/site-packages/chipscopy/examples/designs/vck190/production/chipscopy_ced/chipscopy_wrapper.ltx\n"
          ]
        }
      ],
      "source": [
        "# Specify locations of the running hw_server and cs_server below.\n",
        "CS_URL = os.getenv(\"CS_SERVER_URL\", \"TCP:localhost:3042\")\n",
        "HW_URL = os.getenv(\"HW_SERVER_URL\", \"TCP:localhost:3121\")\n",
        "\n",
        "# specify hw and if programming is desired\n",
        "HW_PLATFORM = os.getenv(\"HW_PLATFORM\", \"vck190\")\n",
        "PROG_DEVICE = os.getenv(\"PROG_DEVICE\", 'True').lower() in ('true', '1', 't')\n",
        "\n",
        "# The get_design_files() function tries to find the PDI and LTX files. In non-standard\n",
        "# configurations, you can put the path for PROGRAMMING_FILE and PROBES_FILE below.\n",
        "design_files = get_design_files(f\"{HW_PLATFORM}/production/chipscopy_ced\")\n",
        "\n",
        "PROGRAMMING_FILE = design_files.programming_file\n",
        "PROBES_FILE = design_files.probes_file\n",
        "\n",
        "print(f\"HW_URL: {HW_URL}\")\n",
        "print(f\"CS_URL: {CS_URL}\")\n",
        "print(f\"PROGRAMMING_FILE: {PROGRAMMING_FILE}\")\n",
        "print(f\"PROBES_FILE:{PROBES_FILE}\")"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "a0cd16a7",
      "metadata": {},
      "source": [
        "## 2 - Create a session and connect to the hw_server and cs_server\n",
        "\n",
        "The session is a container that keeps track of devices and debug cores.\n",
        "After this step,\n",
        "- Session is initialized and connected to server(s)\n",
        "- Versions are detected and reported to stdout"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 3,
      "id": "47704d79",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/html": [
              "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">\n",
              "\n",
              "</pre>\n"
            ],
            "text/plain": [
              "\n",
              "\n"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        },
        {
          "data": {
            "text/html": [
              "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"font-style: italic\">                      ChipScoPy Version Information                      </span>\n",
              "┌────────────────────────────┬──────────────────────────────────────────┐\n",
              "│<span style=\"font-weight: bold\">                     Entity </span>│<span style=\"font-weight: bold\"> Version                                  </span>│\n",
              "├────────────────────────────┼──────────────────────────────────────────┤\n",
              "│                  ChipScoPy │ ┌───────────┬──────────────────────┐     │\n",
              "│                            │ │<span style=\"font-weight: bold\"> Attribute </span>│<span style=\"font-weight: bold\"> Value                </span>│     │\n",
              "│                            │ ├───────────┼──────────────────────┤     │\n",
              "│                            │ │     Build │ 2024.1.1717606426    │     │\n",
              "│                            │ │ Timestamp │ Jun 05 2024-09:53:46 │     │\n",
              "│                            │ └───────────┴──────────────────────┘     │\n",
              "│                            │                                          │\n",
              "│ hw_server @ localhost:3121 │ ┌───────────┬──────────────────────────┐ │\n",
              "│                            │ │<span style=\"font-weight: bold\"> Attribute </span>│<span style=\"font-weight: bold\"> Value                    </span>│ │\n",
              "│                            │ ├───────────┼──────────────────────────┤ │\n",
              "│                            │ │    Status │ Connected                │ │\n",
              "│                            │ │     Build │ 1716424495               │ │\n",
              "│                            │ │   Version │ 2024.1                   │ │\n",
              "│                            │ │ Timestamp │ Wed May 22 18:34:55 2024 │ │\n",
              "│                            │ └───────────┴──────────────────────────┘ │\n",
              "│                            │                                          │\n",
              "│ cs_server @ localhost:3042 │ ┌───────────────┬──────────────────────┐ │\n",
              "│                            │ │<span style=\"font-weight: bold\">     Attribute </span>│<span style=\"font-weight: bold\"> Value                </span>│ │\n",
              "│                            │ ├───────────────┼──────────────────────┤ │\n",
              "│                            │ │        Status │ Connected            │ │\n",
              "│                            │ │         Build │ 1714182049           │ │\n",
              "│                            │ │       Version │ 2024.1.0             │ │\n",
              "│                            │ │     Timestamp │ Apr 26 2024-18:40:49 │ │\n",
              "│                            │ │       Package │ 2024.1.1714182049    │ │\n",
              "│                            │ │ Artifact type │ pyinstaller          │ │\n",
              "│                            │ └───────────────┴──────────────────────┘ │\n",
              "└────────────────────────────┴──────────────────────────────────────────┘\n",
              "</pre>\n"
            ],
            "text/plain": [
              "\u001b[3m                      ChipScoPy Version Information                      \u001b[0m\n",
              "┌────────────────────────────┬──────────────────────────────────────────┐\n",
              "│\u001b[1m \u001b[0m\u001b[1m                    Entity\u001b[0m\u001b[1m \u001b[0m│\u001b[1m \u001b[0m\u001b[1mVersion                                 \u001b[0m\u001b[1m \u001b[0m│\n",
              "├────────────────────────────┼──────────────────────────────────────────┤\n",
              "│                  ChipScoPy │ ┌───────────┬──────────────────────┐     │\n",
              "│                            │ │\u001b[1m \u001b[0m\u001b[1mAttribute\u001b[0m\u001b[1m \u001b[0m│\u001b[1m \u001b[0m\u001b[1mValue               \u001b[0m\u001b[1m \u001b[0m│     │\n",
              "│                            │ ├───────────┼──────────────────────┤     │\n",
              "│                            │ │     Build │ 2024.1.1717606426    │     │\n",
              "│                            │ │ Timestamp │ Jun 05 2024-09:53:46 │     │\n",
              "│                            │ └───────────┴──────────────────────┘     │\n",
              "│                            │                                          │\n",
              "│ hw_server @ localhost:3121 │ ┌───────────┬──────────────────────────┐ │\n",
              "│                            │ │\u001b[1m \u001b[0m\u001b[1mAttribute\u001b[0m\u001b[1m \u001b[0m│\u001b[1m \u001b[0m\u001b[1mValue                   \u001b[0m\u001b[1m \u001b[0m│ │\n",
              "│                            │ ├───────────┼──────────────────────────┤ │\n",
              "│                            │ │    Status │ Connected                │ │\n",
              "│                            │ │     Build │ 1716424495               │ │\n",
              "│                            │ │   Version │ 2024.1                   │ │\n",
              "│                            │ │ Timestamp │ Wed May 22 18:34:55 2024 │ │\n",
              "│                            │ └───────────┴──────────────────────────┘ │\n",
              "│                            │                                          │\n",
              "│ cs_server @ localhost:3042 │ ┌───────────────┬──────────────────────┐ │\n",
              "│                            │ │\u001b[1m \u001b[0m\u001b[1m    Attribute\u001b[0m\u001b[1m \u001b[0m│\u001b[1m \u001b[0m\u001b[1mValue               \u001b[0m\u001b[1m \u001b[0m│ │\n",
              "│                            │ ├───────────────┼──────────────────────┤ │\n",
              "│                            │ │        Status │ Connected            │ │\n",
              "│                            │ │         Build │ 1714182049           │ │\n",
              "│                            │ │       Version │ 2024.1.0             │ │\n",
              "│                            │ │     Timestamp │ Apr 26 2024-18:40:49 │ │\n",
              "│                            │ │       Package │ 2024.1.1714182049    │ │\n",
              "│                            │ │ Artifact type │ pyinstaller          │ │\n",
              "│                            │ └───────────────┴──────────────────────┘ │\n",
              "└────────────────────────────┴──────────────────────────────────────────┘\n"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        },
        {
          "data": {
            "text/html": [
              "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">\n",
              "\n",
              "</pre>\n"
            ],
            "text/plain": [
              "\n",
              "\n"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        }
      ],
      "source": [
        "session = create_session(cs_server_url=CS_URL, hw_server_url=HW_URL)\n",
        "report_versions(session)"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "12ce820e",
      "metadata": {},
      "source": [
        "## 3 - Program the device with the example design\n",
        "\n",
        "After this step,\n",
        "- Device is programmed with the example programming file"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 4,
      "id": "d1423766",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/html": [
              "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">\n",
              "<span style=\"font-weight: bold\">--&gt; INFO: </span>Programming device with: \n",
              "/scratch/chipscopy/venv/lib/python3.11/site-packages/chipscopy/examples/designs/vck190/production/chipscopy_ced/chi\n",
              "pscopy_wrapper.pdi\n",
              "    \n",
              "</pre>\n"
            ],
            "text/plain": [
              "\n",
              "\u001b[1m--> INFO: \u001b[0mProgramming device with: \n",
              "/scratch/chipscopy/venv/lib/python3.11/site-packages/chipscopy/examples/designs/vck190/production/chipscopy_ced/chi\n",
              "pscopy_wrapper.pdi\n",
              "    \n"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        },
        {
          "data": {
            "application/vnd.jupyter.widget-view+json": {
              "model_id": "923d6b30860b45c49c8ea691955daf2f",
              "version_major": 2,
              "version_minor": 0
            },
            "text/plain": [
              "Output()"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        },
        {
          "data": {
            "text/html": [
              "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"></pre>\n"
            ],
            "text/plain": []
          },
          "metadata": {},
          "output_type": "display_data"
        },
        {
          "data": {
            "text/html": [
              "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">\n",
              "</pre>\n"
            ],
            "text/plain": [
              "\n"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        }
      ],
      "source": [
        "# Typical case - one device on the board - get it.\n",
        "device = session.devices.filter_by(family=\"versal\").get()\n",
        "if PROG_DEVICE:\n",
        "    device.program(PROGRAMMING_FILE)\n",
        "else:\n",
        "    print(\"skipping programming\")"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "e596c21f",
      "metadata": {},
      "source": [
        "## 4 - Discover Debug Cores\n",
        "\n",
        "Debug core discovery initializes the chipscope server debug cores. This brings debug cores in the chipscope server online.\n",
        "\n",
        "After this step,\n",
        "\n",
        "- The cs_server is initialized and ready for use"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 5,
      "id": "67670db8",
      "metadata": {},
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "System monitor setup and ready for use.\n"
          ]
        }
      ],
      "source": [
        "device.discover_and_setup_cores(sysmon_scan=True, ddr_scan=False)\n",
        "print(f\"System monitor setup and ready for use.\")"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "1b9c60d8",
      "metadata": {},
      "source": [
        "## 5 - Initialize System Monitor\n",
        "\n",
        "Get reference to the system monitor and initialize all sensors.\n",
        "grab the measurement schedule"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 6,
      "id": "71870922",
      "metadata": {},
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Initializing sensors\n",
            "Refresh measurement schedule\n",
            "Sensor Schedule:\n",
            "  VCCAUX\n",
            "  VCCAUX_PMC\n",
            "  VCC_PMC\n",
            "  VCC_PSFP\n",
            "  VCC_PSLP\n",
            "  VCC_SOC\n",
            "  VP_VN\n",
            "\n"
          ]
        }
      ],
      "source": [
        "sysmon = device.sysmon_root[0]\n",
        "\n",
        "print(\"Initializing sensors\")\n",
        "active_nodes = sysmon.initialize_sensors()\n",
        "\n",
        "print(\"Refresh measurement schedule\")\n",
        "schedule = sysmon.refresh_measurement_schedule()\n",
        "\n",
        "print(\"Sensor Schedule:\")\n",
        "for sensor in schedule.values():\n",
        "    print(f\"  {sensor}\")\n",
        "print()"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "61da7f7a",
      "metadata": {},
      "source": [
        "## 6 - Refresh values from hardware\n",
        "\n",
        "Perform individual sensor read"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 7,
      "id": "7208308a",
      "metadata": {},
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Individual sensor read of VCCAUX\n",
            "->VCCAUX: 1.501V\n",
            "\n"
          ]
        }
      ],
      "source": [
        "sensor_to_read = 'VCCAUX'\n",
        "current_value = sysmon.read_sensor(sensor_to_read)\n",
        "print(f\"Individual sensor read of {sensor_to_read}\")\n",
        "print(f'->{sensor_to_read}: {current_value:.3f}V')\n",
        "print()"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "f167a03a",
      "metadata": {},
      "source": [
        "## 7 - Run measurement for 5 seconds\n",
        "\n",
        "Grab samples once a second for 5 seconds then exit."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 8,
      "id": "98a08be4",
      "metadata": {},
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Group of sensors read\n",
            "  VCC_PMC: 0.798V\n",
            "  VCC_PSLP: 0.799V\n",
            "  VCC_PSFP: 0.797V\n",
            "  VCC_SOC: 0.808V\n",
            "  device_temp_min_min: 38.2°C\n",
            "  device_temp: 40.1°C\n",
            "  device_temp_max_max: 40.9°C\n",
            "\n",
            "  VCC_PMC: 0.798V\n",
            "  VCC_PSLP: 0.799V\n",
            "  VCC_PSFP: 0.797V\n",
            "  VCC_SOC: 0.805V\n",
            "  device_temp_min_min: 38.2°C\n",
            "  device_temp: 40.0°C\n",
            "  device_temp_max_max: 40.9°C\n",
            "\n",
            "  VCC_PMC: 0.798V\n",
            "  VCC_PSLP: 0.798V\n",
            "  VCC_PSFP: 0.797V\n",
            "  VCC_SOC: 0.806V\n",
            "  device_temp_min_min: 38.2°C\n",
            "  device_temp: 39.9°C\n",
            "  device_temp_max_max: 40.9°C\n",
            "\n",
            "  VCC_PMC: 0.798V\n",
            "  VCC_PSLP: 0.798V\n",
            "  VCC_PSFP: 0.797V\n",
            "  VCC_SOC: 0.809V\n",
            "  device_temp_min_min: 38.2°C\n",
            "  device_temp: 40.6°C\n",
            "  device_temp_max_max: 40.9°C\n",
            "\n",
            "  VCC_PMC: 0.798V\n",
            "  VCC_PSLP: 0.798V\n",
            "  VCC_PSFP: 0.797V\n",
            "  VCC_SOC: 0.829V\n",
            "  device_temp_min_min: 38.2°C\n",
            "  device_temp: 40.6°C\n",
            "  device_temp_max_max: 41.0°C\n",
            "\n",
            "Measurement done.\n"
          ]
        }
      ],
      "source": [
        "# Take measurements for 5 seconds then exit.\n",
        "print(\"Group of sensors read\")\n",
        "sensors_to_read = ['VCC_PMC', 'VCC_PSLP', 'VCC_PSFP', 'VCC_SOC']\n",
        "for x in range(5):\n",
        "    current_sensor_values = sysmon.read_sensors(sensors_to_read)\n",
        "    for sensor, value in current_sensor_values.items():\n",
        "        print(f'  {sensor}: {value:.3f}V')\n",
        "    temps = sysmon.read_temp()\n",
        "    for temp, value in temps.items():\n",
        "        print(f'  {temp}: {value:.1f}' + u\"\\u00b0C\")\n",
        "    print()\n",
        "    time.sleep(1)\n",
        "\n",
        "\n",
        "print(\"Measurement done.\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 9,
      "id": "909d3634",
      "metadata": {},
      "outputs": [],
      "source": [
        "## When done with testing, close the connection\n",
        "delete_session(session)"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "venv",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.11.2"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}
