2019-05-11 02:14:44 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* apps/system/spi/spi_exch.c
|
|
|
|
*
|
2021-06-10 13:17:16 +02:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2019-05-11 02:14:44 +02:00
|
|
|
*
|
2021-06-10 13:17:16 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2019-05-11 02:14:44 +02:00
|
|
|
*
|
2021-06-10 13:17:16 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2019-05-11 02:14:44 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <ctype.h>
|
2023-02-04 14:56:06 +01:00
|
|
|
#include <unistd.h>
|
2019-05-11 02:14:44 +02:00
|
|
|
|
|
|
|
#include <nuttx/spi/spi_transfer.h>
|
|
|
|
|
|
|
|
#include "spitool.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: spicmd_exch
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#define ISHEX(x) ((((x)>='0') && ((x)<='9')) || ((toupper(x)>='A') && (toupper(x)<='F')))
|
2022-10-08 10:31:21 +02:00
|
|
|
#define HTOI(x) ((((x)>='0') && ((x)<='9')) ? (x)-'0':toupper(x)-'A'+10)
|
2019-05-11 02:14:44 +02:00
|
|
|
|
|
|
|
int spicmd_exch(FAR struct spitool_s *spitool, int argc, FAR char **argv)
|
|
|
|
{
|
|
|
|
FAR char *ptr;
|
|
|
|
int nargs;
|
|
|
|
int argndx;
|
|
|
|
int ret;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
uint8_t txdata[MAX_XDATA] =
|
|
|
|
{
|
|
|
|
0
|
|
|
|
};
|
2021-04-26 20:05:42 +02:00
|
|
|
|
2019-05-11 02:14:44 +02:00
|
|
|
uint8_t rxdata[MAX_XDATA] =
|
|
|
|
{
|
|
|
|
0
|
|
|
|
};
|
2021-04-26 20:05:42 +02:00
|
|
|
|
2019-05-11 02:14:44 +02:00
|
|
|
uint8_t *txdatap = txdata;
|
|
|
|
struct spi_trans_s trans;
|
|
|
|
struct spi_sequence_s seq;
|
|
|
|
uint32_t d;
|
|
|
|
|
|
|
|
/* Parse any command line arguments */
|
|
|
|
|
|
|
|
for (argndx = 1; argndx < argc; )
|
|
|
|
{
|
|
|
|
/* Break out of the loop when the last option has been parsed */
|
|
|
|
|
|
|
|
ptr = argv[argndx];
|
|
|
|
if (*ptr != '-')
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Otherwise, check for common options */
|
|
|
|
|
|
|
|
nargs = spitool_common_args(spitool, &argv[argndx]);
|
|
|
|
if (nargs < 0)
|
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
argndx += nargs;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* There may be transmit data on the command line */
|
|
|
|
|
2019-10-17 16:05:44 +02:00
|
|
|
if (argc - argndx > spitool->count)
|
|
|
|
{
|
|
|
|
spitool_printf(spitool, g_spitoomanyargs, argv[0]);
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (argndx < argc)
|
2019-05-11 02:14:44 +02:00
|
|
|
{
|
|
|
|
FAR uint8_t *a = (uint8_t *)argv[argndx];
|
|
|
|
while (*a)
|
|
|
|
{
|
|
|
|
if ((*(a + 1) == 0) || !ISHEX(*a) || !ISHEX(*(a + 1)))
|
|
|
|
{
|
2021-04-26 20:05:42 +02:00
|
|
|
/* Uneven number of characters or illegal character error */
|
2019-05-11 02:14:44 +02:00
|
|
|
|
|
|
|
spitool_printf(spitool, g_spiincompleteparam, argv[0]);
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
*txdatap++ = (HTOI(*a) << 4) | HTOI(*(a + 1));
|
|
|
|
a += 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
argndx += 1;
|
|
|
|
}
|
|
|
|
|
2021-04-26 19:52:09 +02:00
|
|
|
spitool_printf(spitool, "Sending:\t");
|
|
|
|
for (d = 0; d < spitool->count; d++)
|
|
|
|
{
|
|
|
|
if (spitool->width <= 8)
|
|
|
|
{
|
|
|
|
spitool_printf(spitool, "%02X ", txdata[d]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
spitool_printf(spitool, "%04X ", ((uint16_t *)txdata)[d]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
spitool_printf(spitool, "\n");
|
|
|
|
|
2019-05-11 02:14:44 +02:00
|
|
|
/* Get a handle to the SPI bus */
|
|
|
|
|
|
|
|
fd = spidev_open(spitool->bus);
|
|
|
|
if (fd < 0)
|
|
|
|
{
|
|
|
|
spitool_printf(spitool, "Failed to get bus %d\n", spitool->bus);
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set up the transfer profile */
|
|
|
|
|
2019-10-18 15:43:38 +02:00
|
|
|
seq.dev = SPIDEV_ID(spitool->devtype, spitool->csn);
|
2019-05-11 02:14:44 +02:00
|
|
|
seq.mode = spitool->mode;
|
|
|
|
seq.nbits = spitool->width;
|
|
|
|
seq.frequency = spitool->freq;
|
|
|
|
seq.ntrans = 1;
|
|
|
|
seq.trans = &trans;
|
|
|
|
|
2019-10-17 16:05:44 +02:00
|
|
|
trans.deselect = true;
|
2019-05-11 02:14:44 +02:00
|
|
|
#ifdef CONFIG_SPI_CMDDATA
|
|
|
|
trans.cmd = spitool->command;
|
|
|
|
#endif
|
|
|
|
trans.delay = spitool->udelay;
|
|
|
|
trans.nwords = spitool->count;
|
|
|
|
trans.txbuffer = txdata;
|
|
|
|
trans.rxbuffer = rxdata;
|
|
|
|
|
|
|
|
ret = spidev_transfer(fd, &seq);
|
|
|
|
|
2020-01-02 13:09:50 +01:00
|
|
|
close(fd);
|
2019-05-11 02:14:44 +02:00
|
|
|
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-04-26 19:52:09 +02:00
|
|
|
spitool_printf(spitool, "Received:\t");
|
2019-05-11 02:14:44 +02:00
|
|
|
for (d = 0; d < spitool->count; d++)
|
|
|
|
{
|
|
|
|
if (spitool->width <= 8)
|
|
|
|
{
|
|
|
|
spitool_printf(spitool, "%02X ", rxdata[d]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
spitool_printf(spitool, "%04X ", ((uint16_t *)rxdata)[d]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
spitool_printf(spitool, "\n");
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|