diff --git a/examples/spi/host/src/host.c b/examples/spi/host/src/host.c index 5a5725f0..77d9e3eb 100644 --- a/examples/spi/host/src/host.c +++ b/examples/spi/host/src/host.c @@ -1,18 +1,30 @@ // 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 + #include "control_host.h" +#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 // payload length in bytes int main(void) { control_version_t version = INVALID_CONTROL_VERSION; - unsigned char payload[4]; + assert(PAYLOAD_LEN > 0 && PAYLOAD_LEN < SPI_DATA_MAX_BYTES); + 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 +43,34 @@ 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; }