From e951f810a3bb0bf53be8c79ed039673212df3444 Mon Sep 17 00:00:00 2001 From: Luciano Martin Date: Fri, 25 Oct 2024 11:48:23 +0100 Subject: [PATCH 1/4] Increase SPI transfers to maximum size --- examples/spi/host/src/host.c | 31 +++++++++++++++++++++---------- examples/spi/src/app.xc | 16 +++++++++------- 2 files changed, 30 insertions(+), 17 deletions(-) diff --git a/examples/spi/host/src/host.c b/examples/spi/host/src/host.c index 5a5725f0..72a7ef43 100644 --- a/examples/spi/host/src/host.c +++ b/examples/spi/host/src/host.c @@ -3,16 +3,18 @@ #include #include #include "control_host.h" +#include "control_transport_shared.h" // Needed for SPI_DATA_MAX_BYTES #include "resource.h" #include "util.h" #include #define INVALID_CONTROL_VERSION 0xFF +#define PAYLOAD_LEN SPI_DATA_MAX_BYTES int main(void) { control_version_t version = INVALID_CONTROL_VERSION; - unsigned char payload[4]; + unsigned char payload[PAYLOAD_LEN]; uint8_t i; if (control_init_spi_pi(SPI_MODE_3, BCM2835_SPI_CLOCK_DIVIDER_8192, 2) != CONTROL_SUCCESS) { @@ -31,27 +33,36 @@ int main(void) } printf("started\n"); + printf("using payload of size %d\n", PAYLOAD_LEN); for (i = 0; i < 4; i++) { - payload[0] = i; - if (control_write_command(RESOURCE_ID, CONTROL_CMD_SET_WRITE(0), payload, 1) != CONTROL_SUCCESS) { + for (int j = 0; j < PAYLOAD_LEN; j++) { + payload[j] = i+j; + + } + + if (control_write_command(RESOURCE_ID, CONTROL_CMD_SET_WRITE(0), payload, PAYLOAD_LEN) != CONTROL_SUCCESS) { printf("control write command failed\n"); exit(1); } pause_short(); - if (control_read_command(RESOURCE_ID, CONTROL_CMD_SET_READ(0), payload, 1) != CONTROL_SUCCESS) { + if (control_read_command(RESOURCE_ID, CONTROL_CMD_SET_READ(0), payload, PAYLOAD_LEN) != CONTROL_SUCCESS) { printf("control read command failed\n"); exit(1); } - - if (payload[0] != i) { - printf("control read command returned the wrong value, expected %d, returned %d\n", i, payload[0]); - exit(1); + for (int j = 0; j < PAYLOAD_LEN; j++) { + if (payload[j] != i+j) { + printf("control read command returned the wrong value, expected %d, returned %d\n", i+j, payload[j]); + exit(1); + } } - printf("Written and read back command with payload: 0x%02X\n", payload[0]); - + printf("Written and read back command with payload: "); + for (int j = 0; j < PAYLOAD_LEN; j++) { + printf("%02X ", payload[j]); + } + printf("\n"); } control_cleanup_spi(); diff --git a/examples/spi/src/app.xc b/examples/spi/src/app.xc index fa0664bd..f3514b89 100644 --- a/examples/spi/src/app.xc +++ b/examples/spi/src/app.xc @@ -4,6 +4,7 @@ #include #include #include "control.h" +#include "control_transport_shared.h" // Needed for SPI_DATA_MAX_BYTES #include "app.h" void app(server interface control i_control) @@ -17,7 +18,7 @@ void app(server interface control i_control) #endif num_commands = 0; - uint8_t test_value = 0; + uint8_t test_values[SPI_DATA_MAX_BYTES] = {0}; while (1) { select { case i_control.register_resources(control_resid_t resources[MAX_RESOURCES_PER_INTERFACE], @@ -43,7 +44,9 @@ void app(server interface control i_control) ret = CONTROL_ERROR; break; } - test_value = payload[0]; + for (int j = 0; j < payload_len; j++) { + test_values[j] = payload[j]; + } ret = CONTROL_SUCCESS; break; @@ -60,12 +63,11 @@ void app(server interface control i_control) ret = CONTROL_ERROR; break; } - if (payload_len != 1) { - printf("expecting 1 read byte, not %d\n", payload_len); - ret = CONTROL_ERROR; - break; + + for (int j = 0; j < payload_len; j++) { + payload[j] = test_values[j]; } - payload[0] = test_value; + ret = CONTROL_SUCCESS; break; } From 3da6ad3383be550201bf5f73519465d391e46329 Mon Sep 17 00:00:00 2001 From: Luciano Martin Date: Fri, 25 Oct 2024 12:06:35 +0100 Subject: [PATCH 2/4] Use payload 1 --- examples/spi/host/src/host.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/spi/host/src/host.c b/examples/spi/host/src/host.c index 72a7ef43..f55973f1 100644 --- a/examples/spi/host/src/host.c +++ b/examples/spi/host/src/host.c @@ -9,7 +9,7 @@ #include #define INVALID_CONTROL_VERSION 0xFF -#define PAYLOAD_LEN SPI_DATA_MAX_BYTES +#define PAYLOAD_LEN 1 // Select a value between 1 and SPI_DATA_MAX_BYTES int main(void) { From 516a94a71ad38bf7704e0a31b5acc36eb3c3d7c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?xalbertoisorna=E2=80=9D?= Date: Fri, 25 Oct 2024 12:24:26 +0100 Subject: [PATCH 3/4] asserting length on host --- examples/spi/host/src/host.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/examples/spi/host/src/host.c b/examples/spi/host/src/host.c index f55973f1..5e805ef3 100644 --- a/examples/spi/host/src/host.c +++ b/examples/spi/host/src/host.c @@ -1,19 +1,23 @@ // Copyright 2016-2024 XMOS LIMITED. // This Software is subject to the terms of the XMOS Public Licence: Version 1. + #include #include +#include + #include "control_host.h" -#include "control_transport_shared.h" // Needed for SPI_DATA_MAX_BYTES +#include "control_transport_shared.h" // for SPI_DATA_MAX_BYTES #include "resource.h" #include "util.h" #include #define INVALID_CONTROL_VERSION 0xFF -#define PAYLOAD_LEN 1 // Select a value between 1 and SPI_DATA_MAX_BYTES +#define PAYLOAD_LEN 1 int main(void) { control_version_t version = INVALID_CONTROL_VERSION; + assert(PAYLOAD_LEN > 0 && PAYLOAD_LEN < SPI_DATA_MAX_BYTES); unsigned char payload[PAYLOAD_LEN]; uint8_t i; From c867e690e39ab2e6063d1fe89269c86f04c1e52a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?xalbertoisorna=E2=80=9D?= Date: Tue, 29 Oct 2024 14:36:48 +0000 Subject: [PATCH 4/4] adding example comment and removing pause short --- examples/spi/host/src/host.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/examples/spi/host/src/host.c b/examples/spi/host/src/host.c index 5e805ef3..77d9e3eb 100644 --- a/examples/spi/host/src/host.c +++ b/examples/spi/host/src/host.c @@ -1,6 +1,12 @@ // Copyright 2016-2024 XMOS LIMITED. // This Software is subject to the terms of the XMOS Public Licence: Version 1. +/* + * This example demonstrates sending a command to a device and reading it back. + * The control command utilizes a payload of size PAYLOAD_LEN bytes. + * The example verifies that the read-back value matches the written value(s). + */ + #include #include #include @@ -12,7 +18,7 @@ #include #define INVALID_CONTROL_VERSION 0xFF -#define PAYLOAD_LEN 1 +#define PAYLOAD_LEN 1 // payload length in bytes int main(void) { @@ -50,8 +56,6 @@ int main(void) exit(1); } - pause_short(); - if (control_read_command(RESOURCE_ID, CONTROL_CMD_SET_READ(0), payload, PAYLOAD_LEN) != CONTROL_SUCCESS) { printf("control read command failed\n"); exit(1);