Airspy HF+ Flash Utility (airspyhf_flash.exe)
=============================================

One program replaces bossac.exe, hfplus_ppb.exe, hfplus_reboot.exe, hfplus_fw.exe and libusb-1.0.dll; FLASH.bat is
now a small launcher for it. Nothing else is needed beside it except the firmware files: no driver to install. For Windows 10 and 11
(64-bit; a 32-bit build exists for 32-bit Windows 10). Windows 7 and 8 need the bootloader driver in the "driver"
folder, once: see "Windows 7 and 8" below.

Standard firmware update
------------------------

1. Connect the unit and close any program using it (SDR#, ...).
2. Double-click FLASH.bat.
3. Wait for "Done." and press a key.

Going back to a previous firmware
---------------------------------

Before it writes anything, FLASH.bat saves the unit's whole flash on this PC (see below). To put one of those
copies back:

1. Connect the unit and close any program using it (SDR#, ...).
2. Double-click RESTORE.bat.
3. The utility lists the unit's saved copies, newest first, each with the firmware in it (for example R5.0.1-CD or
   R5.0.1-BB). Type the number of the copy that holds the firmware you want back.
4. Wait for "Done." (it shows the firmware now running) and press a key.

The copies are kept on the PC that did the update: on another PC the list is empty. RESTORE.bat runs
"airspyhf_flash restore", which saves the flash it replaces first, so a restore can be undone the same way. To put
back a given copy, drop its file on RESTORE.bat.

Windows 7 and 8
---------------

Windows 7 and 8 have no driver of their own for the bootloader's COM port (USB VID_03EB PID_6124): FLASH.bat stops
with "the bootloader ... has no COM port" (its advice about "USB Serial Device" is for Windows 10 and 11), and the
unit waits in its bootloader with its firmware intact. Install the driver in the "driver" folder once (a signed
driver that gives the bootloader Windows' own USB serial driver, usbser.sys):

1. Leave the unit connected: it is in its bootloader.
2. Open Device Manager, right-click the bootloader (under "Other devices", with a warning sign) > Update Driver
   Software > Browse my computer for driver software > choose the "driver\x64" folder (64-bit Windows) or
   "driver\x86" (32-bit Windows) > Next.
3. It shows up under "Ports (COM & LPT)" as "Bossa Program Port (COMn)".
4. Run FLASH.bat (or RESTORE.bat) again: it carries on from the bootloader.

Leave the receiver's own driver (USB VID_03EB PID_800C, the one SDR# uses) as it is.

airspyhf_flash.exe on its own (double-clicked, or run without a command) only shows what is connected and its
details; it changes nothing. FLASH.bat runs it as "airspyhf_flash flash".

Started without a unit connected, it waits for one ("Waiting for an Airspy HF+: connect it now"); while SDR# or
another program has the unit open, it waits for that program to close. Ctrl+C quits.

It finds the unit, reboots it into its bootloader, reads its tuner (CD or BB) from the hardware, picks the
matching firmware file next to the exe (hfplus-firmware-cd.bin or hfplus-firmware-bb.bin), writes and verifies it,
and starts it. The calibration stays as it was: the last flash page, which holds the frequency
calibration (ppb) and the factory IRC calibration, is never erased. A unit with no frequency calibration stored
gets the default one, 2500 ppb. Before anything is written, the whole flash (firmware and calibration page, 64 KB)
is saved in %LOCALAPPDATA%\Airspy\airspyhf_flash (flash_<serial>_<date>.bin); if it cannot be saved, nothing is
written. "airspyhf_flash restore" puts such a copy back (it saves the flash it replaces first). It refuses a copy of
another unit, or one whose firmware is built for the other tuner than the one probed (--force overrides).

To flash another file, drop it on the exe, or run:   airspyhf_flash <file.bin>

If an update is interrupted (cable pulled, PC asleep), the unit stays in its bootloader and does not work as a
receiver: run FLASH.bat again and it carries on from there.

Commands (from a command prompt)
--------------------------------

  airspyhf_flash                     what is connected and its details (as "info"); nothing is changed
  airspyhf_flash flash [file.bin]    flash the firmware that fits the unit (as above), or that file
  airspyhf_flash status              flashed and running, or in the bootloader (unflashed)?
  airspyhf_flash info                serial number, firmware, calibration, chip state
  airspyhf_flash verify <file.bin>   compare the unit's flash with a file
  airspyhf_flash restore [file]      write a saved whole-flash copy back: firmware and calibration page (without a
                                     file: pick one of the unit's copies, newest first; -y takes the newest)
  airspyhf_flash ppb                 print the frequency calibration (ppb)
  airspyhf_flash ppb <value>         write it
  airspyhf_flash vctcxo [value]      read / write the VCTCXO word of the calibration page (stored only: the HF+
                                     firmware does not use it)
                                     (ppb and vctcxo work only while the unit runs its firmware, not in the
                                     bootloader)
  airspyhf_flash driver              which drivers the bootloader and the receiver use
  airspyhf_flash bootloader          reboot the unit into its bootloader and leave it there
  airspyhf_flash boot [file.bin]     start the firmware already in flash (leaves the bootloader); with the file,
                                     the flash is checked against it first
  airspyhf_flash probe               read the tuner from the hardware (through the bootloader; a running unit is
                                     started again afterwards; nothing is written)
  airspyhf_flash check <file.bin>    show a firmware file's version and the tuner it is built for

Options: --force (flash a file built for the other tuner than the one probed), --erase-all (erase every page but
the calibration page first), --default-ppb N (0 = do not write a default), --port COMn, --serial SN (several units
connected), -y (yes to questions), --nopause, --slow (bossac's pace on the bootloader link), -v (more detail).

Safety: the firmware's first page (its vector table) is blanked first and written last, after the rest verified, so
an update cut short never leaves a half-written firmware that would start. The calibration page is never erased:
when a page does not write or verify, or with --erase-all, every other page is erased one by one and the firmware
written again (the chip's erase-all command is not used, since it would take the calibration page with it). The
calibration page is only written when the calibration itself changes (ppb / vctcxo, the 2500 ppb default for a unit
that has none, or a saved copy put back). When a unit in the bootloader shows a blank calibration page and a saved
copy exists for its serial number, the utility offers to put it back.

"status" exit codes: 0 firmware running, 2 bootloader, 3 bootloader without a COM port, 4 HF+ without WinUSB,
1 nothing.

Tuner: CD or BB
---------------

The HF+ Discovery has the STA709 CD tuner; the HF+ Dual Port and the first HF+ Discovery units have the STA709 BB.
Each needs its own firmware ("R6.0.0-CD", "R6.0.0-BB"). The utility always reads the tuner from the hardware, never
from the firmware the unit runs or holds (which may be the wrong one): in the bootloader it loads a small probe
program into the chip's RAM - nothing is written to flash - which powers the tuner, resets it and reads its chip ID
over I2C. A CD or BB STA709 gets its firmware; anything else (no STA709 answering, or an unknown one) is reported as
not an Airspy HF+ and nothing is written. When nothing is written (no tuner recognised, no matching file), a unit
that was running its firmware is started again on it. The utility refuses to flash a file built for the other tuner
(--force overrides).


