serprog: Add support for multiple SPI chip selects
Tested with an EN25F80, Pi Pico, and this serprog firmware:
https://codeberg.org/Riku_V/pico-serprog/commits/branch/multi-cs
Change-Id: If8052bc6f5c314dcc493bc083bb8270723efaae7
Signed-off-by: Riku Viitanen <riku.viitanen@protonmail.com>
Reviewed-on: https://review.sourcearcade.org/c/flashprog/+/51
Reviewed-by: Nico Huber <nico.h@gmx.de>
Tested-by: Nico Huber <nico.h@gmx.de>
diff --git a/Documentation/serprog-protocol.txt b/Documentation/serprog-protocol.txt
index 04fd223..47e1071 100644
--- a/Documentation/serprog-protocol.txt
+++ b/Documentation/serprog-protocol.txt
@@ -35,6 +35,7 @@
+ slen bytes of data
0x14 Set SPI clock frequency in Hz 32-bit requested frequency ACK + 32-bit set frequency / NAK
0x15 Toggle flash chip pin drivers 8-bit (0 disable, else enable) ACK / NAK
+0x16 Set SPI Chip Select 8-bit ACK / NAK
0x?? unimplemented command - invalid.
@@ -89,6 +90,9 @@
remain attached to the flash chip even when the board is running. The user is responsible to
NOT connect VCC and other permanently externally driven signals to the programmer as needed.
If the value is 0, then the drivers should be disabled, otherwise they should be enabled.
+ 0x16 (S_SPI_CS):
+ Set which SPI Chip Select pin to use. This operation is immediate,
+ meaning it doesn't use the operation buffer.
About mandatory commands:
The only truly mandatory commands for any device are 0x00, 0x01, 0x02 and 0x10,
but one can't really do anything with these commands.
diff --git a/serprog.c b/serprog.c
index 0416570..3525df7 100644
--- a/serprog.c
+++ b/serprog.c
@@ -3,6 +3,7 @@
*
* Copyright (C) 2009, 2011 Urja Rannikko <urjaman@gmail.com>
* Copyright (C) 2009 Carl-Daniel Hailfinger
+ * Copyright (C) 2024 Riku Viitanen <riku.viitanen@protonmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -63,6 +64,7 @@
#define S_CMD_O_SPIOP 0x13 /* Perform SPI operation. */
#define S_CMD_S_SPI_FREQ 0x14 /* Set SPI clock frequency */
#define S_CMD_S_PIN_STATE 0x15 /* Enable/disable output drivers */
+#define S_CMD_S_SPI_CS 0x16 /* Set SPI chip select to use */
#define MSGHEADER "serprog: "
@@ -497,7 +499,7 @@
/* Check for the minimum operational set of commands. */
if (serprog_buses_supported & BUS_SPI) {
uint8_t bt = BUS_SPI;
- char *spispeed;
+ char *spispeed, *cs;
if (sp_check_commandavail(S_CMD_O_SPIOP) == 0) {
msg_perr("Error: SPI operation not supported while the "
"bustype is SPI\n");
@@ -577,6 +579,30 @@
}
}
free(spispeed);
+ cs = extract_programmer_param("cs");
+ if (cs) {
+ char *endptr = NULL;
+ errno = 0;
+ unsigned long cs_num = strtoul(cs, &endptr, 0);
+ if (!*cs || errno || *endptr || cs_num > 255) {
+ msg_perr("Error: Invalid chip select requested! "
+ "Only 0-255 are valid.\n");
+ free(cs);
+ goto init_err_cleanup_exit;
+ }
+ free(cs);
+ if (!sp_check_commandavail(S_CMD_S_SPI_CS)) {
+ msg_perr("Error: Setting SPI chip select is not supported!\n");
+ goto init_err_cleanup_exit;
+ }
+ msg_pdbg(MSGHEADER "Requested to use chip select %lu.\n", cs_num);
+ uint8_t cs_num8 = cs_num;
+ if (sp_docommand(S_CMD_S_SPI_CS, 1, &cs_num8, 0, NULL)) {
+ msg_perr("Error: Chip select %u not supported "
+ "by programmer!\n", cs_num8);
+ goto init_err_cleanup_exit;
+ }
+ }
bt = serprog_buses_supported;
if (sp_docommand(S_CMD_S_BUSTYPE, 1, &bt, 0, NULL))
goto init_err_cleanup_exit;