serprog: Test synchronicity before trying to synchronize

Run one test round ahead of trying to synchronize. If the test
succeeds, it saves us the 1s unconditional delay on startup.

Change-Id: I5b727d299cac0cbeadab39af3975dfd8019db8d7
Signed-off-by: Nico Huber <nico.h@gmx.de>
Reviewed-on: https://review.sourcearcade.org/c/flashprog/+/72995
Reviewed-by: Arthur Heymans <arthur@aheymans.xyz>
Reviewed-by: Riku Viitanen <riku.viitanen@protonmail.com>
diff --git a/serprog.c b/serprog.c
index 3c4e609..60e87a4 100644
--- a/serprog.c
+++ b/serprog.c
@@ -197,6 +197,17 @@
 {
 	int i, ret;
 	unsigned char buf[8];
+
+	ret = sp_test_sync();
+	if (ret < 0)
+		goto err_out;
+	if (ret == 0) {
+		msg_pdbg("\n");
+		return 0;
+	}
+
+	msg_pdbg(" - attempting to synchronize\n");
+
 	/* First sends 8 NOPs, then flushes the return data - should cause *
 	 * the device serial parser to get to a sane state, unless if it   *
 	 * is waiting for a real long write-n.                             */
@@ -469,7 +480,7 @@
 		return 1;
 	}
 
-	msg_pdbg(MSGHEADER "connected - attempting to synchronize\n");
+	msg_pdbg(MSGHEADER "connected");
 
 	sp_check_avail_automatic = 0;