Add CSV header with power rail names

This also switches to truncating logfiles on startup by default. This
way the CSV header line is always the first line of the file.

With this change the header line output includes:

  host_time, delta_micros, V_VDD_EE, V_VDDCPU_B, V_DCIN, V_VDDQ(DDR),
  V_VDDCPU_A, V_VSYS3V3, V_EMMC18, I_VDD_EE, I_VDDCPU_B, I_DCIN,
  I_VDDQ(DDR), I_VDDCPU_A, I_VSYS3V3, I_EMMC18, P_VDD_EE, P_VDDCPU_B,
  P_DCIN, P_VDDQ(DDR), P_VDDCPU_A, P_VSYS3V3, P_EMMC18, comment

Change-Id: I37e401bedb232ec02a47e1232204be775d9ddb2e
Reviewed-on: https://pigweed-review.googlesource.com/c/gonk/+/240932
Pigweed-Auto-Submit: Anthony DiGirolamo <tonymd@google.com>
Commit-Queue: Auto-Submit <auto-submit@pigweed-service-accounts.iam.gserviceaccount.com>
Reviewed-by: Akira Baruah <akirabaruah@google.com>
Presubmit-Verified: CQ Bot Account <pigweed-scoped@luci-project-accounts.iam.gserviceaccount.com>
Lint: Lint 🤖 <android-build-ayeaye@system.gserviceaccount.com>
diff --git a/tools/README.md b/tools/README.md
index 138e864..d651096 100644
--- a/tools/README.md
+++ b/tools/README.md
@@ -33,8 +33,8 @@
 - Device (logs from Gonk) are set to: `--device-logfile gonk-device-log.txt`
 - CSV output is set to: `--csv-logfile gonk-csv-log.txt`
 
-**Note:** Log files are always appended to whenever running `gonk`. You can
-truncate the files on startup with `--truncate-logfiles`.
+**Note:** Log files are always erased on startup whenever running
+`gonk`. You can append instead with `--no-truncate-logfiles`.
 
 Logs are not normally written to the terminal but can be using `--log-to-stderr`.
 
@@ -44,7 +44,7 @@
 ### Start Logging ADC Measurements
 
 ```sh
-gonk --log-to-stderr --truncate-logfiles
+gonk --log-to-stderr
 ```
 
 This will provision the FPGA if necessary and begin logging ADC measurements.
diff --git a/tools/gonk_tools/adc.py b/tools/gonk_tools/adc.py
index dc771eb..d164ea6 100644
--- a/tools/gonk_tools/adc.py
+++ b/tools/gonk_tools/adc.py
@@ -13,6 +13,10 @@
 # the License.
 """ADC related math functions."""
 
+import logging
+
+_CSV_LOG = logging.getLogger('gonk.csv')
+
 # CHANNEL_MASK should be set to same value used by
 #   SelectContinuousReadAdcs in the MCU
 CHANNEL_MASK = 0b011100111001
@@ -62,6 +66,22 @@
     return retval
 
 
+def log_csv_header(channel_mask: int = CHANNEL_MASK) -> None:
+    csv_header = [
+        'host_time',
+        'delta_micros',
+    ]
+    for measurement_prefix in ['V', 'I', 'P']:
+        csv_header.extend(
+            [
+                f'{measurement_prefix}_{channel}'
+                for channel in get_channel_names(channel_mask)
+            ]
+        )
+    csv_header.append('comment')
+    _CSV_LOG.info(', '.join(csv_header))
+
+
 def get_resistances(channel_mask: int = CHANNEL_MASK) -> list[float]:
     """Returns list of shunt resistance values for the activated channels."""
     retval = []
diff --git a/tools/gonk_tools/gonk.py b/tools/gonk_tools/gonk.py
index 52df2c5..2a8d23e 100644
--- a/tools/gonk_tools/gonk.py
+++ b/tools/gonk_tools/gonk.py
@@ -36,6 +36,7 @@
     tokens,
 )
 
+from gonk_tools.adc import log_csv_header
 from gonk_tools.gonk_log_stream import GonkLogStream
 from gonk_tools.firmware_files import (
     BUNDLED_ELF,
@@ -165,7 +166,8 @@
     )
     parser.add_argument(
         '--truncate-logfiles',
-        action='store_true',
+        action=argparse.BooleanOptionalAction,
+        default=True,
         help=('Truncate logfiles on before logging.'),
     )
 
@@ -381,7 +383,7 @@
     merge_device_and_host_logs: bool = False,
     verbose: bool = False,
     log_to_stderr: bool = False,
-    truncate_logfiles: bool = False,
+    truncate_logfiles: bool = True,
 ) -> int:
     """Write a bitstream file over serial while monitoring output."""
 
@@ -470,6 +472,10 @@
         csv_filehandler.setFormatter(logging.Formatter(fmt='%(message)s'))
         _CSV_LOG.addHandler(csv_filehandler)
 
+        # Write the csv header if new file or truncate_logfiles is on.
+        if truncate_logfiles or not csv_logfile.is_file():
+            log_csv_header()
+
     if log_to_stderr:
         pw_cli_log.install(
             level=log_level,
diff --git a/tools/gonk_tools/gonk_log_stream.py b/tools/gonk_tools/gonk_log_stream.py
index 4726c06..7490f61 100644
--- a/tools/gonk_tools/gonk_log_stream.py
+++ b/tools/gonk_tools/gonk_log_stream.py
@@ -263,6 +263,8 @@
             ),
         )
 
+        # NOTE: If this format is changed the log_csv_header function
+        # must be updated in adc.py
         _CSV_LOG.info(
             '%s, %s, %s, %s, %s, %s',
             host_time,