From 8cb36642707d876f5de511d5cd80404a44bd6587 Mon Sep 17 00:00:00 2001
From: Daniel van der Schuur <schuur@astron.nl>
Date: Fri, 3 Nov 2017 11:17:18 +0000
Subject: [PATCH] -The need to reflash is now determined by the scripts
 themselves.

---
 .../apertif/commissioning/central_commands.sh | 127 ++++++++++++------
 .../apertif/commissioning/dish_commands.sh    | 127 ++++++++++++++----
 2 files changed, 189 insertions(+), 65 deletions(-)

diff --git a/applications/apertif/commissioning/central_commands.sh b/applications/apertif/commissioning/central_commands.sh
index b750533591..038d9a279a 100755
--- a/applications/apertif/commissioning/central_commands.sh
+++ b/applications/apertif/commissioning/central_commands.sh
@@ -28,23 +28,52 @@
 # Usage: 
 # . called by main.py
 
+###############################################################################
+# Parse arguments
+###############################################################################
 APP=$1 # apertif, arts_sc1, arts_sc4
 TELS=$2
 UNBS=$3
 OPTIONS=$4
 POL=$5
 
-DATAWRITER="wcudata1"
-
 echo central_commands.sh: Application  : $APP
 echo central_commands.sh: Telescopes   : $TELS
 echo central_commands.sh: UniBoards    : $UNBS
 echo central_commands.sh: Options      : $OPTIONS
 echo central_commands.sh: Polarisation : $POL
 
+# Select correct firmware image for this application
+if [ "$APP" == "apertif" ]; then
+    # Below image is commented out due to BSN alignment issues on several subsets of dishes
+    #IMAGE=/home/kooistra/images/apertif_unb1_correlator_full-r16744-v2.rbf # Hajees image
+    #IMAGE=/home/kooistra/images/apertif_unb1_correlator_full-r16829.rbf    # Erics equivalent to r16744-v2
+    # Daniel's image (2017, week 14) with increased MESH buffer depth. Fixes above issue.
+    # IMAGE=/home/daniel/apertif_unb1_correlator_full-r17077.rbf 
+    # IMAGE=/home/kooistra/images/apertif_unb1_correlator_full-r17164.rbf # Erics image, 2 bit change to optimize quantisation in correlator (20 Jun 2017)
+    IMAGE=/home/daniel/apertif_unb1_correlator_full-r17077.rbf # SR1
+elif [ "$APP" == "apertif-sr2.6" ]; then
+    IMAGE=/home/kooistra/images/apertif_unb1_correlator_full-r17164.rbf # SR2.5
+elif [ "$APP" == "arts_sc1" ]; then
+    IMAGE=/home/arts/arts_unb1_sc1-r16748.rbf
+elif [ "$APP" == "arts_sc4" ]; then
+    #IMAGE=/home/hargreaves/arts_unb1_sc4-r17371.rbf # first 'working' version
+    #IMAGE=/home/hargreaves/arts_unb1_sc4-r17505.rbf # data buffer before the filterbank
+    #IMAGE=/home/hargreaves/arts_unb1_sc4-r17552.rbf # force and data buffer before the mesh
+    #IMAGE=/home/hargreaves/arts_unb1_sc4-r17576_10GbEin.rbf # enable 10GbE output with 12 copies of IAB
+    #IMAGE=/home/hargreaves/arts_unb1_sc4-r17615.rbf # test version with no 10GbE and bypassed channelizer
+    IMAGE=/home/hargreaves/arts_unb1_sc4-r17628.rbf # no 10GbE, data buffer after the WPFB, fixed packet size?
+fi
+
+#Extract the RBF file names (without path and extention)
+IMAGE_FILENAME=$(basename "$IMAGE")
+IMAGE_RBFNAME="${IMAGE_FILENAME%.*}"
+
 ###############################################################################
 # Destination MAC/IP adresses of output streams
 ###############################################################################
+DATAWRITER="wcudata1"
+
 WCUDATA1_ETH0_ETH_DST_MAC=250813975111312 #0xe41d2de42690
 WCUDATA1_ETH0_IP_DST_ADDR=174285825 #10.99.100.1
 
@@ -82,53 +111,26 @@ ARTS0_P3P2_ETH_DST_MAC=40175249347569 #0x248a077753f1
 ARTS0_P3P2_IP_DST_ADDR=174311428 # 10.99.200.4
 
 ###############################################################################
-# Program and/or load application images
+# Compare required application images against images stored in flash
 ###############################################################################
-# Print current design names 
-python $UPE/peripherals/util_system_info.py --unb $UNBS --fn 0:3 --bn 0:3 -n 2
-
 # Get memory map of current images
 python $UPE/peripherals/util_system_info.py --unb $UNBS --fn 0:3 --bn 0:3 -n 4
 
-# Revert to facory image so we can reflash the user firmware
+# Revert to facory image so we can reflash/load the user firmware
 python $UPE/peripherals/util_wdi.py --unb $UNBS --fn 0:3 --bn 0:3 -n 0
 
 # Wait until the factory images are online
-sleep 4
+sleep 4 # It takes at least a second
 
 # Get memory map of factory image
 python $UPE/peripherals/util_system_info.py --unb $UNBS --fn 0:3 --bn 0:3 -n 4
 
-# Select correct firmware image for this application
-if [ "$APP" == "apertif" ]; then
-    # Below image is commented out due to BSN alignment issues on several subsets of dishes
-    #IMAGE=/home/kooistra/images/apertif_unb1_correlator_full-r16744-v2.rbf # Hajees image
-    #IMAGE=/home/kooistra/images/apertif_unb1_correlator_full-r16829.rbf    # Erics equivalent to r16744-v2
-    # Daniel's image (2017, week 14) with increased MESH buffer depth. Fixes above issue.
-    # IMAGE=/home/daniel/apertif_unb1_correlator_full-r17077.rbf 
-    # IMAGE=/home/kooistra/images/apertif_unb1_correlator_full-r17164.rbf # Erics image, 2 bit change to optimize quantisation in correlator (20 Jun 2017)
-    IMAGE=/home/daniel/apertif_unb1_correlator_full-r17077.rbf # SR1
-elif [ "$APP" == "apertif-sr2.6" ]; then
-    IMAGE=/home/kooistra/images/apertif_unb1_correlator_full-r17164.rbf # SR2.5
-elif [ "$APP" == "arts_sc1" ]; then
-    IMAGE=/home/arts/arts_unb1_sc1-r16748.rbf
-elif [ "$APP" == "arts_sc4" ]; then
-    #IMAGE=/home/hargreaves/arts_unb1_sc4-r17371.rbf # first 'working' version
-    #IMAGE=/home/hargreaves/arts_unb1_sc4-r17505.rbf # data buffer before the filterbank
-    #IMAGE=/home/hargreaves/arts_unb1_sc4-r17552.rbf # force and data buffer before the mesh
-    #IMAGE=/home/hargreaves/arts_unb1_sc4-r17576_10GbEin.rbf # enable 10GbE output with 12 copies of IAB
-    #IMAGE=/home/hargreaves/arts_unb1_sc4-r17615.rbf # test version with no 10GbE and bypassed channelizer
-    IMAGE=/home/hargreaves/arts_unb1_sc4-r17628.rbf # no 10GbE, data buffer after the WPFB, fixed packet size?
-fi
-
-if [ "$OPTIONS" != "noflash" ]; then
-    # Program the firmware to flash
-    python $UPE/peripherals/util_epcs.py --unb $UNBS --fn 0:3 --bn 0:3 -n 4 -s $IMAGE
-fi
+# Wait until PPS
+python $UPE/peripherals/util_ppsh.py --unb 0 --bn 0 -n 4
 
-# Load ARTS beamformer firmware images from flash
+# Load all firmware images from flash
 python $UPE/peripherals/util_epcs.py --unb $UNBS --fn 0:3 --bn 0:3 -n 8 &
-
+    
 # Wait until the user images are online
 sleep 4 
 
@@ -138,6 +140,55 @@ python $UPE/peripherals/util_system_info.py --unb $UNBS --fn 0:3 --bn 0:3 -n 4
 # Print the user firmware image design names
 python $UPE/peripherals/util_system_info.py --unb $UNBS --fn 0:3 --bn 0:3 -n 2
 
+# Get number of required images actually running on the FPGAs
+ACT_IMAGE=$(python $UPE/peripherals/util_system_info.py --unb $UNBS --fn 0:3 --bn 0:3 -n 2 | grep -c $IMAGE_RBFNAME)
+echo -n "Image $IMAGE_RBFNAME found running on $ACT_IMAGE processing nodes: "
+
+NOF_UNIBOARDS=$(echo $UNBS, | grep -o ',' | wc -l)
+NOF_REQUIRED_MATCHING_IMAGES=$(($NOF_UNIBOARDS * 8))
+
+if [ "$ACT_IMAGE" == "$NOF_REQUIRED_MATCHING_IMAGES" ]; then
+    echo Continuing without processing node reflash
+    FLASH=FALSE
+else
+    echo Firmware images will be rewritten to flash
+    FLASH=TRUE
+fi
+
+###############################################################################
+# Flash and reload application images if needed
+###############################################################################
+if [ "$FLASH" == "TRUE" ]; then
+    # Revert to facory image so we can reflash/load the user firmware
+    python $UPE/peripherals/util_wdi.py --unb $UNBS --fn 0:3 --bn 0:3 -n 0
+    
+    # Wait until the factory images are online
+    sleep 4 # It takes at least a second
+    
+    # Get memory map of factory image
+    python $UPE/peripherals/util_system_info.py --unb $UNBS --fn 0:3 --bn 0:3 -n 4
+    
+    # Write image to flash if needed
+    python $UPE/peripherals/util_epcs.py --unb $UNBS --fn 0:3 --bn 0:3 -n 4 -s $IMAGE
+
+    # Load the application images
+    
+    # Wait until PPS
+    python $UPE/peripherals/util_ppsh.py --unb 0 --bn 0 -n 4
+    
+    # Load all dish firmware images from flash
+    python $UPE/peripherals/util_epcs.py --unb $UNBS --fn 0:3 --bn 0:3 -n 8 &
+        
+    # Wait until the user images are online
+    sleep 4 
+    
+    # Get memory map of the user images
+    python $UPE/peripherals/util_system_info.py --unb $UNBS --fn 0:3 --bn 0:3 -n 4
+    
+    # Print the user firmware image design names
+    python $UPE/peripherals/util_system_info.py --unb $UNBS --fn 0:3 --bn 0:3 -n 2
+fi
+
 ###############################################################################
 # Vitesse XAUI<->XFI transceiver settings
 ###############################################################################
@@ -268,7 +319,7 @@ fi
 # Set destination MAC/IP/Ports
 ###############################################################################
 UNBS2="${UNBS//,/ }"
-if [ ${APP:0:7} == "apertif" ]; then
+if [ $APP == "apertif" ]; then
     if [ "$DATAWRITER" == "wcudata1" ]; then
         DATAWRITER_ETH0_ETH_DST_MAC=$WCUDATA1_ETH0_ETH_DST_MAC
         DATAWRITER_ETH0_IP_DST_ADDR=$WCUDATA1_ETH0_IP_DST_ADDR
@@ -363,7 +414,7 @@ fi
 ###############################################################################
 # Enable output streams NOTE - temporary not outputting to ARTS0 (absent network)
 ###############################################################################
-if [ ${APP:0:7} == "apertif" ]; then
+if [ $APP == "apertif" ]; then
     # Enable all output of all correlator uniboards
     python $UPE/peripherals/util_dp_xonoff.py --unb $UNBS --fn 0:3 --bn 0:3 -s OUTPUT -n 2
 elif [ $APP == "arts_sc1" ]; then
diff --git a/applications/apertif/commissioning/dish_commands.sh b/applications/apertif/commissioning/dish_commands.sh
index e2f588461e..d7ddc741cc 100755
--- a/applications/apertif/commissioning/dish_commands.sh
+++ b/applications/apertif/commissioning/dish_commands.sh
@@ -31,6 +31,9 @@
 #   . apertif
 #   . arts_sc1
 
+###############################################################################
+# Parse arguments
+###############################################################################
 APP=$1 # apertif, arts_sc1, arts_sc4
 OPTIONS=$2
 POL=$3
@@ -48,21 +51,6 @@ echo dish_commands.sh: UniBoards    : $UNBS
 echo dish_commands.sh: Options      : $OPTIONS
 echo dish_commands.sh: Polarisation : $POL
 
-###############################################################################
-# Program and load the application images
-###############################################################################
-# Get memory map of current images
-python $UPE/peripherals/util_system_info.py --unb $UNBS --fn 0:3 --bn 0:3 -n 4
-
-# Revert to facory image so we can reflash the user firmware
-python $UPE/peripherals/util_wdi.py --unb $UNBS --fn 0:3 --bn 0:3 -n 0
-
-# Wait until the factory images are online
-sleep 4 # It takes at least a second
-
-# Get memory map of factory image
-python $UPE/peripherals/util_system_info.py --unb $UNBS --fn 0:3 --bn 0:3 -n 4
-
 # Default images that Hajee made (26 Jan 2017)
 IMAGE_FB=/home/kooistra/images/apertif_unb1_bn_filterbank-r16579.rbf
 IMAGE_BF=/home/kooistra/images/apertif_unb1_fn_beamformer_trans-r16510_v2.rbf
@@ -85,22 +73,28 @@ elif [ "$APP" == "arts_sc4" ]; then
     IMAGE_BF=/home/daniel/apertif_unb1_fn_beamformer_trans-r17128.rbf # 14 april 2017. Fiber delay comp.
 fi
 
-if [ "$OPTIONS" != "noflash" ]; then
-#    python $UPE/peripherals/util_epcs.py --unb $UNBS --bn 0:3 -n 4 -s $IMAGE_FB # Commented out as workaround for flash-related issues (18th of July)
-    python $UPE/peripherals/util_epcs.py --unb $UNBS --fn 0:3 -n 4 -s $IMAGE_BF
-fi
+#Extract the RBF file names (without path and extention)
+IMAGE_FB_FILENAME=$(basename "$IMAGE_FB")
+IMAGE_FB_RBFNAME="${IMAGE_FB_FILENAME%.*}"
+IMAGE_BF_FILENAME=$(basename "$IMAGE_BF")
+IMAGE_BF_RBFNAME="${IMAGE_BF_FILENAME%.*}"
 
 ###############################################################################
-# For ARTS, Set LO1 frequency to 4800
+# Compare required application images against images stored in flash
 ###############################################################################
-if [ "$APP" == "arts_sc1" ] || [ "$APP" == "arts_sc4" ]; then
-    cd /home/boudewijn/SVN/apertif/commissioning/trunk/test_scripts/python
-    python util_set_lo1freq.py 4800
-fi
 
-###############################################################################
-# Load the application images
-###############################################################################
+# Get memory map of current images
+python $UPE/peripherals/util_system_info.py --unb $UNBS --fn 0:3 --bn 0:3 -n 4
+
+# Revert to facory image so we can reflash/load the user firmware
+python $UPE/peripherals/util_wdi.py --unb $UNBS --fn 0:3 --bn 0:3 -n 0
+
+# Wait until the factory images are online
+sleep 4 # It takes at least a second
+
+# Get memory map of factory image
+python $UPE/peripherals/util_system_info.py --unb $UNBS --fn 0:3 --bn 0:3 -n 4
+
 # Wait until PPS
 python $UPE/peripherals/util_ppsh.py --unb 0 --bn 0 -n 4
 
@@ -116,11 +110,90 @@ python $UPE/peripherals/util_system_info.py --unb $UNBS --fn 0:3 --bn 0:3 -n 4
 # Print the user firmware image design names
 python $UPE/peripherals/util_system_info.py --unb $UNBS --fn 0:3 --bn 0:3 -n 2
 
+# Get number of required images actually running on BN
+ACT_IMAGE_FB=$(python $UPE/peripherals/util_system_info.py --unb $UNBS --bn 0:3 -n 2 | grep -c $IMAGE_FB_RBFNAME)
+echo -n "BN image $IMAGE_FB_RBFNAME found running on $ACT_IMAGE_FB back nodes: "
+
+if [ "$ACT_IMAGE_FB" == "32" ]; then
+    echo Continuing without BN reflash
+    FLASH_BN=FALSE
+else
+    echo BN images will be rewritten to flash
+    FLASH_BN=TRUE
+fi
+
+# Get number of required images actually running on FN
+ACT_IMAGE_BF=$(python $UPE/peripherals/util_system_info.py --unb $UNBS --fn 0:3 -n 2 | grep -c $IMAGE_BF_RBFNAME)
+echo -n "FN image $IMAGE_BF_RBFNAME found running on $ACT_IMAGE_BF front nodes: "
+
+if [ "$ACT_IMAGE_BF" == "32" ]; then
+    echo Continuing without FN reflash
+    FLASH_FN=FALSE
+else
+    echo FN images will be rewritten to flash
+    FLASH_FN=TRUE
+fi
+
+###############################################################################
+# Flash and reload application images if needed
+###############################################################################
+if [ "$FLASH_BN" == "TRUE" ] || [ "$FLASH_FN" == "TRUE" ]; then
+    # Revert to facory image so we can reflash/load the user firmware
+    python $UPE/peripherals/util_wdi.py --unb $UNBS --fn 0:3 --bn 0:3 -n 0
+    
+    # Wait until the factory images are online
+    sleep 4 # It takes at least a second
+    
+    # Get memory map of factory image
+    python $UPE/peripherals/util_system_info.py --unb $UNBS --fn 0:3 --bn 0:3 -n 4
+    
+    # Write BN image to flash if needed
+    if [ "$FLASH_BN" == "TRUE" ]; then
+        python $UPE/peripherals/util_epcs.py --unb $UNBS --bn 0:3 -n 4 -s $IMAGE_FB
+    fi
+
+    # If both images need to be reprogrammed, perform MM access in between as workaround for flash issue
+    if [ "$FLASH_BN" == "TRUE" ] && [ "$FLASH_FN" == "TRUE" ]; then
+        # Print the user firmware image design names
+        python $UPE/peripherals/util_system_info.py --unb $UNBS --fn 0:3 --bn 0:3 -n 2
+    fi
+    
+    # Write FN image to flash if needed
+    if [ "$FLASH_FN" == "TRUE" ]; then
+        python $UPE/peripherals/util_epcs.py --unb $UNBS --fn 0:3 -n 4 -s $IMAGE_BF
+    fi
+
+    # Load the application images
+    
+    # Wait until PPS
+    python $UPE/peripherals/util_ppsh.py --unb 0 --bn 0 -n 4
+    
+    # Load all dish firmware images from flash
+    python $UPE/peripherals/util_epcs.py --unb $UNBS --fn 0:3 --bn 0:3 -n 8 &
+        
+    # Wait until the user images are online
+    sleep 4 
+    
+    # Get memory map of the user images
+    python $UPE/peripherals/util_system_info.py --unb $UNBS --fn 0:3 --bn 0:3 -n 4
+    
+    # Print the user firmware image design names
+    python $UPE/peripherals/util_system_info.py --unb $UNBS --fn 0:3 --bn 0:3 -n 2
+fi
+
 ###############################################################################
 # Write temperature threshold
 ###############################################################################
 python $UPE/peripherals/util_unb_sens.py --unb 0:7 --bn 0:3 --fn 0:3 -n 7
 
+###############################################################################
+# For ARTS, Set LO1 frequency to 4800
+###############################################################################
+if [ "$APP" == "arts_sc1" ] || [ "$APP" == "arts_sc4" ]; then
+    cd /home/boudewijn/SVN/apertif/commissioning/trunk/test_scripts/python
+    python util_set_lo1freq.py 4800
+fi
+
 ###############################################################################
 # Call ADU initialization sequence
 ###############################################################################
-- 
GitLab