Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 31 additions & 12 deletions examples/spi/host/src/host.c
Original file line number Diff line number Diff line change
@@ -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 <stdio.h>
#include <stdlib.h>
#include <assert.h>

#include "control_host.h"
#include "control_transport_shared.h" // for SPI_DATA_MAX_BYTES
#include "resource.h"
#include "util.h"
#include <bcm2835.h>

#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) {
Expand All @@ -31,27 +43,34 @@ int main(void)
}

printf("started\n");
printf("using payload of size %d\n", PAYLOAD_LEN);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not very flexible if its hardcoded? or am I missing something

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is configurable at compile time, before was hardcoded to be 1. We do not expect runtime variable length because it can overcomplicate the example.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps some extra docs to make it clear whats going on, otherwise this work doesn't add much value. The reader will need to know that this feature exists in order for it to be of any use

Copy link
Contributor Author

@xalbertoisorna xalbertoisorna Oct 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The benefit is that to test a transfer of Xbytes for example (for instance to compare timings with the one on fwk_rtos) is much easier to have it as a define here.

The default value is back to 1 to match the rest of the examples.


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();
Expand Down
16 changes: 9 additions & 7 deletions examples/spi/src/app.xc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <stdint.h>
#include <assert.h>
#include "control.h"
#include "control_transport_shared.h" // Needed for SPI_DATA_MAX_BYTES
#include "app.h"

void app(server interface control i_control)
Expand All @@ -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],
Expand All @@ -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;

Expand All @@ -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;
}
Expand Down