2016-08-05 18:23:04 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* drivers/spi/spi_transfer.c
|
|
|
|
*
|
2021-03-04 07:10:42 +01: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
|
2016-08-05 18:23:04 +02:00
|
|
|
*
|
2021-03-04 07:10:42 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2016-08-05 18:23:04 +02:00
|
|
|
*
|
2021-03-04 07:10:42 +01: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.
|
2016-08-05 18:23:04 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2016-08-05 21:22:18 +02:00
|
|
|
#include <unistd.h>
|
2016-08-05 18:23:04 +02:00
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
2017-10-06 18:15:01 +02:00
|
|
|
#include <nuttx/signal.h>
|
2016-08-05 18:23:04 +02:00
|
|
|
#include <nuttx/spi/spi.h>
|
|
|
|
#include <nuttx/spi/spi_transfer.h>
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: spi_transfer
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This is a helper function that can be used to encapsulate and manage
|
2016-08-05 21:05:41 +02:00
|
|
|
* a sequence of SPI transfers. The SPI bus will be locked and the
|
|
|
|
* SPI device selected for the duration of the transfers.
|
2016-08-05 18:23:04 +02:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* spi - An instance of the SPI device to use for the transfer
|
|
|
|
* seq - Describes the sequence of transfers.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero (OK) on success; a negated errno value on failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int spi_transfer(FAR struct spi_dev_s *spi, FAR struct spi_sequence_s *seq)
|
|
|
|
{
|
|
|
|
FAR struct spi_trans_s *trans;
|
|
|
|
int ret = OK;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
DEBUGASSERT(spi != NULL && seq != NULL && seq->trans != NULL);
|
|
|
|
|
|
|
|
/* Get exclusive access to the SPI bus */
|
|
|
|
|
|
|
|
SPI_LOCK(spi, true);
|
|
|
|
|
|
|
|
/* Establish the fixed SPI attributes for all transfers in the sequence */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SETFREQUENCY(spi, seq->frequency);
|
2016-08-05 18:23:04 +02:00
|
|
|
|
2021-11-05 17:54:40 +01:00
|
|
|
#ifdef CONFIG_SPI_DELAY_CONTROL
|
|
|
|
ret = SPI_SETDELAY(spi, seq->a, seq->b, seq->c, seq->i);
|
2016-08-05 18:23:04 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2017-03-01 12:56:15 +01:00
|
|
|
spierr("ERROR: SPI_SETDELAY failed: %d\n", ret);
|
2016-08-05 18:23:04 +02:00
|
|
|
SPI_LOCK(spi, false);
|
2017-06-28 21:17:17 +02:00
|
|
|
return ret;
|
2016-08-05 18:23:04 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2024-07-31 09:43:12 +02:00
|
|
|
SPI_SETMODE(spi, (enum spi_mode_e)seq->mode);
|
2016-08-05 18:23:04 +02:00
|
|
|
SPI_SETBITS(spi, seq->nbits);
|
|
|
|
|
2016-08-06 16:07:30 +02:00
|
|
|
/* Select the SPI device in preparation for the transfer.
|
|
|
|
* REVISIT: This is redundant.
|
|
|
|
*/
|
2016-08-05 18:23:04 +02:00
|
|
|
|
|
|
|
SPI_SELECT(spi, seq->dev, true);
|
|
|
|
|
|
|
|
/* Then perform each transfer is the sequence */
|
|
|
|
|
|
|
|
for (i = 0, trans = seq->trans; i < (int)seq->ntrans; i++, trans++)
|
|
|
|
{
|
|
|
|
/* Establish the fixed SPI attributes for unique to this transaction */
|
|
|
|
|
|
|
|
#ifdef CONFIG_SPI_HWFEATURES
|
|
|
|
ret = SPI_HWFEATURES(spi, trans->hwfeat);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2017-03-01 12:56:15 +01:00
|
|
|
spierr("ERROR: SPI_HWFEATURES failed: %d\n", ret);
|
2016-08-05 18:23:04 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_SPI_CMDDATA
|
|
|
|
ret = SPI_CMDDATA(spi, seq->dev, trans->cmd);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2017-03-01 12:56:15 +01:00
|
|
|
spierr("ERROR: SPI_CMDDATA failed: %d\n", ret);
|
2016-08-05 18:23:04 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-08-05 21:22:18 +02:00
|
|
|
/* [Re-]select the SPI device in preparation for the transfer */
|
|
|
|
|
|
|
|
SPI_SELECT(spi, seq->dev, true);
|
|
|
|
|
2016-08-05 18:23:04 +02:00
|
|
|
/* Perform the transfer */
|
|
|
|
|
|
|
|
SPI_EXCHANGE(spi, trans->txbuffer, trans->rxbuffer, trans->nwords);
|
2016-08-05 21:22:18 +02:00
|
|
|
|
|
|
|
/* Possibly de-select the SPI device after the transfer */
|
|
|
|
|
|
|
|
if (trans->deselect)
|
|
|
|
{
|
|
|
|
SPI_SELECT(spi, seq->dev, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Perform any requested inter-transfer delay */
|
|
|
|
|
|
|
|
if (trans->delay > 0)
|
|
|
|
{
|
2017-10-06 18:15:01 +02:00
|
|
|
nxsig_usleep(trans->delay);
|
2016-08-05 21:22:18 +02:00
|
|
|
}
|
2016-08-05 18:23:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SPI_SELECT(spi, seq->dev, false);
|
|
|
|
SPI_LOCK(spi, false);
|
2017-06-28 21:17:17 +02:00
|
|
|
return ret;
|
2016-08-05 18:23:04 +02:00
|
|
|
}
|
|
|
|
|