<!--
SPDX-FileCopyrightText: 2023 CERN (home.cern)

SPDX-License-Identifier: CC-BY-SA-4.0+
-->

# Factory Testing Procedure

Each device produced contains an empty EEPROM that needs to be programmed with
the following information:

- device VID:PID
- product description
- serial number
- default power-on settings

Furthermore, each new device needs to be tested to verify that it is properly
functioning within specifications.

## EEPROM programming

**Preparation**

- Get a Linux computer and build and install the latest release of the
  attenuator library and test program (if not already available).
- Connect the Device Under Test (DUT) to the computer via USB.
- Make sure that the FTDI device driver which comes by default with most Linux
  distributions is not loaded after plugging in the DUT. You can use the `lsmod`
  command and look for the `ftdi_sio` driver in the output. If the driver is
  loaded, you must remove it using the `modprobe` command:
```
$ lsmod | grep -c ftdi
1
$ sudo modprobe -r ftdi_sio
$ lsmod | grep ftdi
0
```

!!! hint

    Every time you plug a new unprogrammed DUT, the operating system will try to
    reload the `ftdi_sio` driver. To prevent this behaviour, you can blacklist
    the driver like this:
    ```
    echo "blacklist ftdi_sio" > /etc/modprobe.d/ftdi-blacklist.conf
    ```
    If you are working in a non-persistent filesystem (as in the case of a CERN
    Front-End Computer), you will need to repeat this every time you reboot the
    computer.

**Procedure**

!!! warning

    Be sure to properly identify and select your USB device below.
    The programming tool will refuse to work if the VID:PID is not
    matched but, if your computer has any other USB peripherals
    using an FTDI chip (quite common, especially for USB to serial
    converters), you might accidentally program another USB device
    and brick it.

1. Connect the DUT to the computer via USB and check that the power LED is ON.
2. Use the `lsusb` command and look for the DUT, if it has never been programmed
   it will be listed with the default FTDI VID:PID (`0403:6010`), otherwise it
   will have the CERN one (`1556:0443`). Note down the `Bus` and `Device`
   number.
3. Run the test program (`oattnusbtest`) and use the `eeprom_program` command
   while providing the `Bus` and `Device` numbers from the previous steps as
   parameters. You will be also asked in a separate prompt to input the serial
   number to be programmed into the device. The result should look like this:
```
oatt> eeprom_program 1 14
Please provide the serial number to program: HCCAIAD000-CR000038

WARNING: about to erase and program the EEPROM with new serial number
[HCCAIAD000-CR000038] into dev with old serial: [EMPTY] at
busdev: 1/14, busport: 1-3.2.4
are you sure? (yes/N) yes
backing up EEPROM into /tmp/eeprom_dump-1682680819412215.bin
backing up EEPROM into /tmp/eeprom_dump-1682680819416644.bin
VID:     0x1556
PID:     0x0443
Release: 0x0700
Bus Powered: 100 mA
Manufacturer: CERN
Product:      OASIS RF Attenuator
Serial:       HCCAIAD000-CR000038
Checksum      : c928
Attached EEPROM: 93x46
PNP: 1
Channel A has Mode FIFO VCP
Channel B has Mode FIFO VCP
AL has 4 mA drive
AH has 4 mA drive
BL has 4 mA drive
BH has 4 mA drive
successful EEPROM write, resetting USB device
new serial recorded!
quitting, please restart the test program
```
4. Following the EEPROM programming, the test program will automatically terminate.
5. Unplug and replug the USB cable of the DUT to perform a power cycle.
6. Verify that all four channels power up at -40dB attenuation (a quick flicker
   during power up is to be expected).

!!! hint

    If you have a barcode sticker ready for the DUT, you can use a barcode scanner
    to pass it on to the test program during programming, to avoid any
    copying-by-hand mistakes.

## Testing

**Preparation**

- You will need the same computer setup that you used for programming the
  EEPROM and the DUT should be already [programmed](#eeprom-programming).
- Get a vector/scalar Network Analyzer (NA) which works at least up to 2 GHz.
- Set the NA to 0dB ref level, S21 measurement, 2 GHz bandwidth, high RBW/VBW
  (so that sweeps take short time).
- Connect two SMA cables to the NA ports.

**Procedure**

1. Connect the DUT to the computer via USB and check that the power LED is ON.
2. Run the test program and try detecting the DUT using the `ls` command. You
   should see an output like this:
```
oatt> ls
0:	l:0	serial:HCCAIAD000-CR000038	busdev:1/14	busport:1-3.2.4
```
3. Request access to the DUT via the `open` command. You should see an output
   like this:
```
oatt> open
current device: serial HCCAIAD000-CR000038, fd 1, 4 channels
```
4. Connect the NA to the input and output of DUT channel 1.
5. Set the attenuation of channel 1 to 0dB.
```
oatt> set_attn 1 0dB
```
6. Verify that the 0dB LED of channel 1 is on.
7. Look at the screen of the NA to see if it's around 0 dB (±3dB) in the whole
   bandwidth (up to 2 GHz). Close to the limit of 2 GHz it is ok if it goes a
   bit beyond ±3dB. However, look out for sudden spikes/dips (in the order of
   ±6dB or more) across the whole measured bandwidth.
8. Repeat steps 5 to 7 for -20dB and -40dB attenuation levels.
9. Repeat steps 4 to 8 for DUT channels 2, 3 and 4.
10. Close the access to the DUT via the `close` command.
11. Disconnect the DUT from the computer.

!!! hint

    The prompt of the test program keeps a history of previous commands. You can use
    the up/down arrows to repeat previous commands.
