mirror of
https://github.com/apache/nuttx-apps.git
synced 2026-08-01 20:29:00 +00:00
apps/examples: Add path option to SPI Slave test
Add path option to spislv_test example Signed-off-by: Eren Terzioglu <eren.terzioglu@espressif.com>
This commit is contained in:
parent
02153debae
commit
173e19cde1
2 changed files with 56 additions and 34 deletions
|
|
@ -12,6 +12,12 @@ config EXAMPLES_SPISLV
|
||||||
|
|
||||||
if EXAMPLES_SPISLV
|
if EXAMPLES_SPISLV
|
||||||
|
|
||||||
|
config EXAMPLES_SPISLV_DEFAULT_PATH
|
||||||
|
string "Default SPI Slave device path"
|
||||||
|
default "/dev/spislv0"
|
||||||
|
---help---
|
||||||
|
Default path for SPI Slave device
|
||||||
|
|
||||||
config EXAMPLES_SPISLV_PROGNAME
|
config EXAMPLES_SPISLV_PROGNAME
|
||||||
string "Program name"
|
string "Program name"
|
||||||
default "spislv"
|
default "spislv"
|
||||||
|
|
|
||||||
|
|
@ -40,7 +40,7 @@
|
||||||
/* Define buffer sizes */
|
/* Define buffer sizes */
|
||||||
#define RX_BUFFER_SIZE 64
|
#define RX_BUFFER_SIZE 64
|
||||||
#define TX_BUFFER_SIZE 64
|
#define TX_BUFFER_SIZE 64
|
||||||
#define SOURCE_FILE "dev/spislv2" /* SPI device path */
|
#define DEFAULT_SPI_PATH CONFIG_EXAMPLES_SPISLV_DEFAULT_PATH
|
||||||
|
|
||||||
/* Enumeration for operation modes */
|
/* Enumeration for operation modes */
|
||||||
|
|
||||||
|
|
@ -61,6 +61,7 @@ typedef struct
|
||||||
operation_mode_t mode;
|
operation_mode_t mode;
|
||||||
int num_bytes; /* Applicable for write mode */
|
int num_bytes; /* Applicable for write mode */
|
||||||
int timeout_seconds; /* Applicable for all modes */
|
int timeout_seconds; /* Applicable for all modes */
|
||||||
|
const char *spi_path; /* SPI slave device path */
|
||||||
unsigned char tx_buffer[TX_BUFFER_SIZE];
|
unsigned char tx_buffer[TX_BUFFER_SIZE];
|
||||||
} program_config_t;
|
} program_config_t;
|
||||||
|
|
||||||
|
|
@ -68,6 +69,33 @@ typedef struct
|
||||||
* Private Functions
|
* Private Functions
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
|
static void print_usage(const char *progname)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Usage:\n");
|
||||||
|
fprintf(stderr, " %s -x <num_bytes> [-p <path>] [-t <sec>] <hex_bytes>\n",
|
||||||
|
progname);
|
||||||
|
fprintf(stderr, " %s -l [-p <path>] [-t <sec>]\n", progname);
|
||||||
|
fprintf(stderr, " %s -e [-p <path>] [-t <sec>]\n", progname);
|
||||||
|
fprintf(stderr, "\n");
|
||||||
|
fprintf(stderr, "Options:\n");
|
||||||
|
fprintf(stderr, " -p SPI slave device path [default: %s]\n",
|
||||||
|
DEFAULT_SPI_PATH);
|
||||||
|
fprintf(stderr,
|
||||||
|
" -t Timeout in seconds [default: 10]\n");
|
||||||
|
fprintf(stderr,
|
||||||
|
" -x Number of bytes to queue <hex_bytes>\n");
|
||||||
|
fprintf(stderr,
|
||||||
|
" -l Only listen for data from the master\n");
|
||||||
|
fprintf(stderr,
|
||||||
|
" -e Echo received data back to the master\n");
|
||||||
|
fprintf(stderr, "\n");
|
||||||
|
fprintf(stderr, "Examples:\n");
|
||||||
|
fprintf(stderr, " %s -x 2 abba\n", progname);
|
||||||
|
fprintf(stderr, " %s -l -t 5\n", progname);
|
||||||
|
fprintf(stderr, " %s -e -t 10\n", progname);
|
||||||
|
fprintf(stderr, " %s -p /dev/spislv2 -l\n", progname);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Converts a single hexadecimal character to its byte value.
|
* @brief Converts a single hexadecimal character to its byte value.
|
||||||
*
|
*
|
||||||
|
|
@ -148,13 +176,25 @@ static int parse_arguments(int argc, char *argv[],
|
||||||
config->mode = MODE_INVALID;
|
config->mode = MODE_INVALID;
|
||||||
config->num_bytes = 0;
|
config->num_bytes = 0;
|
||||||
config->timeout_seconds = 10; /* Default timeout */
|
config->timeout_seconds = 10; /* Default timeout */
|
||||||
|
config->spi_path = DEFAULT_SPI_PATH;
|
||||||
|
|
||||||
/* Parse command-line options */
|
/* Parse command-line options */
|
||||||
|
|
||||||
while ((opt = getopt(argc, argv, "x:t:le")) != -1)
|
while ((opt = getopt(argc, argv, "p:x:t:le")) != -1)
|
||||||
{
|
{
|
||||||
switch (opt)
|
switch (opt)
|
||||||
{
|
{
|
||||||
|
case 'p':
|
||||||
|
if (optarg[0] == '\0')
|
||||||
|
{
|
||||||
|
fprintf(stderr,
|
||||||
|
"Error: SPI device path (-p) must not be empty.\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
config->spi_path = optarg;
|
||||||
|
break;
|
||||||
|
|
||||||
case 'x':
|
case 'x':
|
||||||
if (config->mode != MODE_INVALID)
|
if (config->mode != MODE_INVALID)
|
||||||
{
|
{
|
||||||
|
|
@ -208,18 +248,7 @@ static int parse_arguments(int argc, char *argv[],
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
fprintf(stderr, "Usage:\n");
|
print_usage(argv[0]);
|
||||||
fprintf(stderr,
|
|
||||||
" %s -x <num_bytes> [-t <timeout_seconds>] <hex_bytes>\n",
|
|
||||||
argv[0]);
|
|
||||||
fprintf(stderr,
|
|
||||||
" %s -l [-t <timeout_seconds>]\n", argv[0]);
|
|
||||||
fprintf(stderr,
|
|
||||||
" %s -e [-t <timeout_seconds>]\n", argv[0]);
|
|
||||||
printf("Examples:\n");
|
|
||||||
printf(" spislv -x 2 abba\n");
|
|
||||||
printf(" spislv -l -t 5\n");
|
|
||||||
printf(" spislv -e -t 10\n\n");
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -232,9 +261,7 @@ static int parse_arguments(int argc, char *argv[],
|
||||||
{
|
{
|
||||||
fprintf(stderr,
|
fprintf(stderr,
|
||||||
"Error: Missing hexadecimal bytes to send.\n");
|
"Error: Missing hexadecimal bytes to send.\n");
|
||||||
fprintf(stderr,
|
print_usage(argv[0]);
|
||||||
"Usage: %s -x <num_bytes> [-t <timeout_seconds>] <hex_bytes>\n",
|
|
||||||
argv[0]);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -265,18 +292,7 @@ static int parse_arguments(int argc, char *argv[],
|
||||||
else if (config->mode == MODE_INVALID)
|
else if (config->mode == MODE_INVALID)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "Error: No operation mode specified.\n");
|
fprintf(stderr, "Error: No operation mode specified.\n");
|
||||||
fprintf(stderr, "Usage:\n");
|
print_usage(argv[0]);
|
||||||
fprintf(stderr,
|
|
||||||
" %s -x <num_bytes> [-t <timeout_seconds>] <hex_bytes>\n",
|
|
||||||
argv[0]);
|
|
||||||
fprintf(stderr,
|
|
||||||
" %s -l [-t <timeout_seconds>]\n", argv[0]);
|
|
||||||
fprintf(stderr,
|
|
||||||
" %s -e [-t <timeout_seconds>]\n", argv[0]);
|
|
||||||
printf("Examples:\n");
|
|
||||||
printf(" spislv -x 2 abba\n");
|
|
||||||
printf(" spislv -l -t 5\n");
|
|
||||||
printf(" spislv -e -t 10\n");
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -320,7 +336,7 @@ static int write_mode(program_config_t *config, int fd)
|
||||||
if (bytes_written < 0)
|
if (bytes_written < 0)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "Error: Failed to write to %s: %s\n",
|
fprintf(stderr, "Error: Failed to write to %s: %s\n",
|
||||||
SOURCE_FILE, strerror(errno));
|
config->spi_path, strerror(errno));
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -426,7 +442,7 @@ static int read_with_timeout(program_config_t *config, int fd)
|
||||||
if (bytes_read < 0)
|
if (bytes_read < 0)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "Error: Failed to read from %s: %s\n",
|
fprintf(stderr, "Error: Failed to read from %s: %s\n",
|
||||||
SOURCE_FILE, strerror(errno));
|
config->spi_path, strerror(errno));
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -453,7 +469,7 @@ static int read_with_timeout(program_config_t *config, int fd)
|
||||||
{
|
{
|
||||||
fprintf(stderr,
|
fprintf(stderr,
|
||||||
"Error: Failed to write to %s: %s\n",
|
"Error: Failed to write to %s: %s\n",
|
||||||
SOURCE_FILE, strerror(errno));
|
config->spi_path, strerror(errno));
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
else if (bytes_written != bytes_read)
|
else if (bytes_written != bytes_read)
|
||||||
|
|
@ -503,11 +519,11 @@ int main(int argc, char *argv[])
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
fd = open(SOURCE_FILE, O_RDWR);
|
fd = open(config.spi_path, O_RDWR);
|
||||||
if (fd < 0)
|
if (fd < 0)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "Error: Failed to open %s: %s\n",
|
fprintf(stderr, "Error: Failed to open %s: %s\n",
|
||||||
SOURCE_FILE, strerror(errno));
|
config.spi_path, strerror(errno));
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue