# Measure Power with ULINKplus

## In this learning path

- [Introduction](https://learn.arm.com/learning-paths/embedded-and-microcontrollers/uv_debug/)
- [Use basic run/stop debug](https://learn.arm.com/learning-paths/embedded-and-microcontrollers/uv_debug/2_basics/)
- [Debug using Event Recorder](https://learn.arm.com/learning-paths/embedded-and-microcontrollers/uv_debug/3_event_recorder/)
- [Debug using Serial Wire Viewer](https://learn.arm.com/learning-paths/embedded-and-microcontrollers/uv_debug/4_swv/)
- [Advanced debug with ETM trace](https://learn.arm.com/learning-paths/embedded-and-microcontrollers/uv_debug/5_etm_trace/)
- [Measure Power with ULINKplus](https://learn.arm.com/learning-paths/embedded-and-microcontrollers/uv_debug/6_power_ulplus/)
- [Next Steps](https://learn.arm.com/learning-paths/embedded-and-microcontrollers/uv_debug/_next-steps/)

An Arm Keil [ULINKplus debug adapter](https://developer.arm.com/Tools%20and%20Software/ULINKplus) offers serial-wire debug capabilities, measures the CPU core clock directly, and adds power measurement. Together with Event Statistics, this can be used to create power profiles for code sections.

<details>
<summary>Note</summary>
For details on how to connect the ULINKplus to your board, refer to the [documentation](https://developer.arm.com/documentation/101636/latest/Power-Measurement?lang=en).
</details>

## Configure ULINKplus with an Initialization File

You can configure the ULINKplus adapter with a set of debug commands that you can capture in a debug script. These scripts are executed when debug mode is entered. A [debug script template](https://developer.arm.com/documentation/101636/latest/Test-Automation/Debug-Script-Template) is available in your tool installation: `C:\Keil_v5\ARM\ULINK\Templates\Debug_UlinkPlus.ini` as a reference.

1. Copy this file into your project directory (where the `*.uvprojx` file is located).
2. Go to ![Image Alt Text:Options for Target](https://learn.arm.com/learning-paths/embedded-and-microcontrollers/uv_debug/b_uv4_target_options.png) **Project - Options for Target… (Alt+F7)** and switch to the **Debug** tab.
3. Click on **…** in the **Initialization File:** box, and add the file:<br/>![Image Alt Text:Debug_UlinkPlus.ini added to the target options](https://learn.arm.com/learning-paths/embedded-and-microcontrollers/uv_debug/debug_ulpl_ini.png)
4. Click **Edit…** to open the file.
5. Click **OK** to close the target options window and return to the main μVision window.
6. Click on the **Configuration Wizard** tab at the bottom of `Debug_UlinkPlus.ini` window.
7. Enable **ULINK Power Measurement** and select the value of your shunt resistor (in this case 2.55 Ohm/100 mA):<br/>![Image Alt Text:Debug_UlinkPlus.ini configuration](https://learn.arm.com/learning-paths/embedded-and-microcontrollers/uv_debug/debug_ulpl_ini_config.png)
8. Go to ![Image Alt Text:Save All](https://learn.arm.com/learning-paths/embedded-and-microcontrollers/uv_debug/b_uv4_save_all.png) **File - Save All**.

ULINKplus is now configured and ready to use.

## Measure Power in System Analyzer

1. ![Image Alt Text:Build](https://learn.arm.com/learning-paths/embedded-and-microcontrollers/uv_debug/b_uv4_debug.png) **Start a Debug Session (Ctrl+F5)** to enter the µVision debugger.
2. ![Image Alt Text:Run](https://learn.arm.com/learning-paths/embedded-and-microcontrollers/uv_debug/b_uv4_run.png) **Run (F5)** the application.
3. If the window is not already open, go to **View - Analysis Windows** and select ![Image Alt Text:System Analyzer Window](https://learn.arm.com/learning-paths/embedded-and-microcontrollers/uv_debug/b_uv4_system_analyzer.png) **System Analyzer**.
4. Click the ![Image Alt Text:Lock](https://learn.arm.com/learning-paths/embedded-and-microcontrollers/uv_debug/b_uv4_unlock.png) lock symbol to stop the data collection.
5. Zoom in or out with your mouse wheel. First, click in the waveform to set an anchor point.
6. You will have a window similar to the one below:
![Image Alt Text:System Analyzer with Power Consumption](https://learn.arm.com/learning-paths/embedded-and-microcontrollers/uv_debug/sa_w_power.png)

The **Current** and **Voltage** waveforms are shown in the **Consumption** section.

<details>
<summary>Notes</summary>
- If **Current** has negative values, reverse the shunt wires.
- The **Voltage** and **Current** Y axis numbers are adaptive and will change dynamically to suit the values displayed.
</details>

## Power Consumption in the Event Statistics Window

1. Go to **View - Analysis Windows** and select ![Image Alt Text:Event Statistics](https://learn.arm.com/learning-paths/embedded-and-microcontrollers/uv_debug/b_uv4_event_statistics.png) **Event Statistics** to open the window:<br/>![Image Alt Text:Event Statistics with Power Consumption](https://learn.arm.com/learning-paths/embedded-and-microcontrollers/uv_debug/evtstat_window_w_power.png)

The various statistics are displayed and updated in real-time. Timings, voltage, current and charges consumed are recorded according to Groups and Slots. These are created by the `EventStartX()` and `EventStopX()` function calls in the application code.

### Energy Measurement without Debug

The CoreSight debug components consume some amount of power. This will be reflected in the **System Analyzer** window. To be able to measure the real-life power without a debug connection, a specific debug mode is available. In this mode, you will not be able to relate the waveforms with your code. The effects will be most pronounced during processor Sleep and WAIT states when the current drops to very low values.

1. **Stop a Debug Session (Ctrl+F5)** to leave the µVision debugger.
2. Go to ![Image Alt Text:Energy Measurement](https://learn.arm.com/learning-paths/embedded-and-microcontrollers/uv_debug/b_uv4_energy.png) **Debug - Energy Measurement without Debug**.
3. The debug session starts and you can ![Image Alt Text:Run](https://learn.arm.com/learning-paths/embedded-and-microcontrollers/uv_debug/b_uv4_run.png) **Run (F5)** the application as usual.
4. System Analyzer will display the waveform as shown below:<br/>![Image Alt Text:Energy Measurement without Debug](https://learn.arm.com/learning-paths/embedded-and-microcontrollers/uv_debug/energy_wo_debug.png)

<details>
<summary>Note</summary>
Any features needing CoreSight will not be available.
</details>
