2009-11-29 08:50:13 +00:00
|
|
|
/*
|
2013-03-30 10:32:37 +00:00
|
|
|
ChibiOS/RT - Copyright (C) 2006-2013 Giovanni Di Sirio
|
2009-11-29 08:50:13 +00:00
|
|
|
|
2013-03-30 10:32:37 +00:00
|
|
|
Licensed 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
|
2009-11-29 08:50:13 +00:00
|
|
|
|
2013-03-30 10:32:37 +00:00
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
2009-11-29 08:50:13 +00:00
|
|
|
|
2013-03-30 10:32:37 +00: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.
|
2009-11-29 08:50:13 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2012-12-05 15:07:55 +00:00
|
|
|
* @file STM32/SPIv1/spi_lld.c
|
2010-07-26 15:01:58 +00:00
|
|
|
* @brief STM32 SPI subsystem low level driver source.
|
|
|
|
*
|
2010-10-25 18:48:13 +00:00
|
|
|
* @addtogroup SPI
|
2009-11-29 08:50:13 +00:00
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ch.h"
|
|
|
|
#include "hal.h"
|
|
|
|
|
2010-11-01 17:29:56 +00:00
|
|
|
#if HAL_USE_SPI || defined(__DOXYGEN__)
|
2009-11-29 08:50:13 +00:00
|
|
|
|
2011-11-10 17:54:41 +00:00
|
|
|
/*===========================================================================*/
|
|
|
|
/* Driver local definitions. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
#define SPI1_RX_DMA_CHANNEL \
|
|
|
|
STM32_DMA_GETCHANNEL(STM32_SPI_SPI1_RX_DMA_STREAM, \
|
|
|
|
STM32_SPI1_RX_DMA_CHN)
|
|
|
|
|
|
|
|
#define SPI1_TX_DMA_CHANNEL \
|
|
|
|
STM32_DMA_GETCHANNEL(STM32_SPI_SPI1_TX_DMA_STREAM, \
|
|
|
|
STM32_SPI1_TX_DMA_CHN)
|
|
|
|
|
|
|
|
#define SPI2_RX_DMA_CHANNEL \
|
|
|
|
STM32_DMA_GETCHANNEL(STM32_SPI_SPI2_RX_DMA_STREAM, \
|
|
|
|
STM32_SPI2_RX_DMA_CHN)
|
|
|
|
|
|
|
|
#define SPI2_TX_DMA_CHANNEL \
|
|
|
|
STM32_DMA_GETCHANNEL(STM32_SPI_SPI2_TX_DMA_STREAM, \
|
|
|
|
STM32_SPI2_TX_DMA_CHN)
|
|
|
|
|
|
|
|
#define SPI3_RX_DMA_CHANNEL \
|
|
|
|
STM32_DMA_GETCHANNEL(STM32_SPI_SPI3_RX_DMA_STREAM, \
|
|
|
|
STM32_SPI3_RX_DMA_CHN)
|
|
|
|
|
|
|
|
#define SPI3_TX_DMA_CHANNEL \
|
|
|
|
STM32_DMA_GETCHANNEL(STM32_SPI_SPI3_TX_DMA_STREAM, \
|
|
|
|
STM32_SPI3_TX_DMA_CHN)
|
|
|
|
|
2009-11-29 08:50:13 +00:00
|
|
|
/*===========================================================================*/
|
2009-12-29 13:15:29 +00:00
|
|
|
/* Driver exported variables. */
|
2009-11-29 08:50:13 +00:00
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/** @brief SPI1 driver identifier.*/
|
2010-08-08 07:57:28 +00:00
|
|
|
#if STM32_SPI_USE_SPI1 || defined(__DOXYGEN__)
|
2009-11-29 08:50:13 +00:00
|
|
|
SPIDriver SPID1;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/** @brief SPI2 driver identifier.*/
|
2010-08-08 07:57:28 +00:00
|
|
|
#if STM32_SPI_USE_SPI2 || defined(__DOXYGEN__)
|
2009-11-29 08:50:13 +00:00
|
|
|
SPIDriver SPID2;
|
|
|
|
#endif
|
|
|
|
|
2010-07-12 16:54:48 +00:00
|
|
|
/** @brief SPI3 driver identifier.*/
|
2010-08-08 07:57:28 +00:00
|
|
|
#if STM32_SPI_USE_SPI3 || defined(__DOXYGEN__)
|
2010-07-12 16:54:48 +00:00
|
|
|
SPIDriver SPID3;
|
|
|
|
#endif
|
|
|
|
|
2009-11-29 08:50:13 +00:00
|
|
|
/*===========================================================================*/
|
2013-02-28 16:23:19 +00:00
|
|
|
/* Driver local variables and types. */
|
2009-11-29 08:50:13 +00:00
|
|
|
/*===========================================================================*/
|
|
|
|
|
2010-10-11 18:55:06 +00:00
|
|
|
static uint16_t dummytx;
|
|
|
|
static uint16_t dummyrx;
|
|
|
|
|
2009-11-29 08:50:13 +00:00
|
|
|
/*===========================================================================*/
|
2009-12-29 13:15:29 +00:00
|
|
|
/* Driver local functions. */
|
2009-11-29 08:50:13 +00:00
|
|
|
/*===========================================================================*/
|
|
|
|
|
2010-10-11 11:48:03 +00:00
|
|
|
/**
|
2011-04-10 16:45:41 +00:00
|
|
|
* @brief Shared end-of-rx service routine.
|
2010-10-11 11:48:03 +00:00
|
|
|
*
|
|
|
|
* @param[in] spip pointer to the @p SPIDriver object
|
2011-04-10 16:45:41 +00:00
|
|
|
* @param[in] flags pre-shifted content of the ISR register
|
2010-10-11 11:48:03 +00:00
|
|
|
*/
|
2011-04-10 16:45:41 +00:00
|
|
|
static void spi_lld_serve_rx_interrupt(SPIDriver *spip, uint32_t flags) {
|
|
|
|
|
|
|
|
/* DMA errors handling.*/
|
|
|
|
#if defined(STM32_SPI_DMA_ERROR_HOOK)
|
2011-11-23 19:58:04 +00:00
|
|
|
if ((flags & (STM32_DMA_ISR_TEIF | STM32_DMA_ISR_DMEIF)) != 0) {
|
2011-04-10 16:45:41 +00:00
|
|
|
STM32_SPI_DMA_ERROR_HOOK(spip);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
(void)flags;
|
|
|
|
#endif
|
2009-11-29 08:50:13 +00:00
|
|
|
|
2012-01-15 09:37:27 +00:00
|
|
|
/* Stop everything.*/
|
2011-11-10 17:54:41 +00:00
|
|
|
dmaStreamDisable(spip->dmatx);
|
|
|
|
dmaStreamDisable(spip->dmarx);
|
2009-11-29 08:50:13 +00:00
|
|
|
|
2010-10-12 15:19:15 +00:00
|
|
|
/* Portable SPI ISR code defined in the high level driver, note, it is
|
|
|
|
a macro.*/
|
|
|
|
_spi_isr_code(spip);
|
2009-11-29 08:50:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-04-10 16:45:41 +00:00
|
|
|
* @brief Shared end-of-tx service routine.
|
2010-10-04 17:16:18 +00:00
|
|
|
*
|
2011-04-10 16:45:41 +00:00
|
|
|
* @param[in] spip pointer to the @p SPIDriver object
|
|
|
|
* @param[in] flags pre-shifted content of the ISR register
|
2009-11-29 08:50:13 +00:00
|
|
|
*/
|
2011-04-10 16:45:41 +00:00
|
|
|
static void spi_lld_serve_tx_interrupt(SPIDriver *spip, uint32_t flags) {
|
2009-11-29 08:50:13 +00:00
|
|
|
|
2011-04-10 16:45:41 +00:00
|
|
|
/* DMA errors handling.*/
|
|
|
|
#if defined(STM32_SPI_DMA_ERROR_HOOK)
|
2011-04-16 06:30:12 +00:00
|
|
|
(void)spip;
|
2012-01-14 10:54:56 +00:00
|
|
|
if ((flags & (STM32_DMA_ISR_TEIF | STM32_DMA_ISR_DMEIF)) != 0) {
|
2011-04-10 16:45:41 +00:00
|
|
|
STM32_SPI_DMA_ERROR_HOOK(spip);
|
2009-11-29 08:50:13 +00:00
|
|
|
}
|
2011-04-10 16:45:41 +00:00
|
|
|
#else
|
|
|
|
(void)spip;
|
|
|
|
(void)flags;
|
2009-11-29 08:50:13 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-04-10 16:45:41 +00:00
|
|
|
/*===========================================================================*/
|
|
|
|
/* Driver interrupt handlers. */
|
|
|
|
/*===========================================================================*/
|
2010-07-12 16:54:48 +00:00
|
|
|
|
2009-11-29 08:50:13 +00:00
|
|
|
/*===========================================================================*/
|
2009-12-29 13:15:29 +00:00
|
|
|
/* Driver exported functions. */
|
2009-11-29 08:50:13 +00:00
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/**
|
2010-07-26 15:01:58 +00:00
|
|
|
* @brief Low level SPI driver initialization.
|
2010-10-04 17:16:18 +00:00
|
|
|
*
|
|
|
|
* @notapi
|
2009-11-29 08:50:13 +00:00
|
|
|
*/
|
|
|
|
void spi_lld_init(void) {
|
|
|
|
|
2010-10-11 18:55:06 +00:00
|
|
|
dummytx = 0xFFFF;
|
|
|
|
|
2010-08-08 07:57:28 +00:00
|
|
|
#if STM32_SPI_USE_SPI1
|
2009-11-29 08:50:13 +00:00
|
|
|
spiObjectInit(&SPID1);
|
2011-11-10 17:54:41 +00:00
|
|
|
SPID1.spi = SPI1;
|
|
|
|
SPID1.dmarx = STM32_DMA_STREAM(STM32_SPI_SPI1_RX_DMA_STREAM);
|
|
|
|
SPID1.dmatx = STM32_DMA_STREAM(STM32_SPI_SPI1_TX_DMA_STREAM);
|
|
|
|
SPID1.rxdmamode = STM32_DMA_CR_CHSEL(SPI1_RX_DMA_CHANNEL) |
|
|
|
|
STM32_DMA_CR_PL(STM32_SPI_SPI1_DMA_PRIORITY) |
|
|
|
|
STM32_DMA_CR_DIR_P2M |
|
|
|
|
STM32_DMA_CR_TCIE |
|
2011-11-13 10:55:33 +00:00
|
|
|
STM32_DMA_CR_DMEIE |
|
2011-11-10 17:54:41 +00:00
|
|
|
STM32_DMA_CR_TEIE;
|
|
|
|
SPID1.txdmamode = STM32_DMA_CR_CHSEL(SPI1_TX_DMA_CHANNEL) |
|
|
|
|
STM32_DMA_CR_PL(STM32_SPI_SPI1_DMA_PRIORITY) |
|
|
|
|
STM32_DMA_CR_DIR_M2P |
|
2011-11-13 10:55:33 +00:00
|
|
|
STM32_DMA_CR_DMEIE |
|
2011-11-10 17:54:41 +00:00
|
|
|
STM32_DMA_CR_TEIE;
|
2009-11-29 08:50:13 +00:00
|
|
|
#endif
|
|
|
|
|
2010-08-08 07:57:28 +00:00
|
|
|
#if STM32_SPI_USE_SPI2
|
2009-11-29 08:50:13 +00:00
|
|
|
spiObjectInit(&SPID2);
|
2011-11-10 17:54:41 +00:00
|
|
|
SPID2.spi = SPI2;
|
|
|
|
SPID2.dmarx = STM32_DMA_STREAM(STM32_SPI_SPI2_RX_DMA_STREAM);
|
|
|
|
SPID2.dmatx = STM32_DMA_STREAM(STM32_SPI_SPI2_TX_DMA_STREAM);
|
|
|
|
SPID2.rxdmamode = STM32_DMA_CR_CHSEL(SPI2_RX_DMA_CHANNEL) |
|
|
|
|
STM32_DMA_CR_PL(STM32_SPI_SPI2_DMA_PRIORITY) |
|
|
|
|
STM32_DMA_CR_DIR_P2M |
|
|
|
|
STM32_DMA_CR_TCIE |
|
2011-11-13 10:55:33 +00:00
|
|
|
STM32_DMA_CR_DMEIE |
|
2011-11-10 17:54:41 +00:00
|
|
|
STM32_DMA_CR_TEIE;
|
|
|
|
SPID2.txdmamode = STM32_DMA_CR_CHSEL(SPI2_TX_DMA_CHANNEL) |
|
|
|
|
STM32_DMA_CR_PL(STM32_SPI_SPI2_DMA_PRIORITY) |
|
|
|
|
STM32_DMA_CR_DIR_M2P |
|
2011-11-13 10:55:33 +00:00
|
|
|
STM32_DMA_CR_DMEIE |
|
2011-11-10 17:54:41 +00:00
|
|
|
STM32_DMA_CR_TEIE;
|
2009-11-29 08:50:13 +00:00
|
|
|
#endif
|
2010-07-12 16:54:48 +00:00
|
|
|
|
2010-08-08 07:57:28 +00:00
|
|
|
#if STM32_SPI_USE_SPI3
|
2010-07-12 16:54:48 +00:00
|
|
|
spiObjectInit(&SPID3);
|
2011-11-10 17:54:41 +00:00
|
|
|
SPID3.spi = SPI3;
|
|
|
|
SPID3.dmarx = STM32_DMA_STREAM(STM32_SPI_SPI3_RX_DMA_STREAM);
|
|
|
|
SPID3.dmatx = STM32_DMA_STREAM(STM32_SPI_SPI3_TX_DMA_STREAM);
|
|
|
|
SPID3.rxdmamode = STM32_DMA_CR_CHSEL(SPI3_RX_DMA_CHANNEL) |
|
|
|
|
STM32_DMA_CR_PL(STM32_SPI_SPI3_DMA_PRIORITY) |
|
|
|
|
STM32_DMA_CR_DIR_P2M |
|
|
|
|
STM32_DMA_CR_TCIE |
|
2011-11-13 10:55:33 +00:00
|
|
|
STM32_DMA_CR_DMEIE |
|
2011-11-10 17:54:41 +00:00
|
|
|
STM32_DMA_CR_TEIE;
|
|
|
|
SPID3.txdmamode = STM32_DMA_CR_CHSEL(SPI3_TX_DMA_CHANNEL) |
|
|
|
|
STM32_DMA_CR_PL(STM32_SPI_SPI3_DMA_PRIORITY) |
|
|
|
|
STM32_DMA_CR_DIR_M2P |
|
2011-11-13 10:55:33 +00:00
|
|
|
STM32_DMA_CR_DMEIE |
|
2011-11-10 17:54:41 +00:00
|
|
|
STM32_DMA_CR_TEIE;
|
2010-07-12 16:54:48 +00:00
|
|
|
#endif
|
2009-11-29 08:50:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-07-26 15:01:58 +00:00
|
|
|
* @brief Configures and activates the SPI peripheral.
|
2009-11-29 08:50:13 +00:00
|
|
|
*
|
|
|
|
* @param[in] spip pointer to the @p SPIDriver object
|
2010-10-04 17:16:18 +00:00
|
|
|
*
|
|
|
|
* @notapi
|
2009-11-29 08:50:13 +00:00
|
|
|
*/
|
|
|
|
void spi_lld_start(SPIDriver *spip) {
|
|
|
|
|
|
|
|
/* If in stopped state then enables the SPI and DMA clocks.*/
|
2011-03-08 10:09:57 +00:00
|
|
|
if (spip->state == SPI_STOP) {
|
2010-08-08 07:57:28 +00:00
|
|
|
#if STM32_SPI_USE_SPI1
|
2009-11-29 08:50:13 +00:00
|
|
|
if (&SPID1 == spip) {
|
2011-08-28 08:53:14 +00:00
|
|
|
bool_t b;
|
2011-11-10 17:54:41 +00:00
|
|
|
b = dmaStreamAllocate(spip->dmarx,
|
2011-08-28 08:53:14 +00:00
|
|
|
STM32_SPI_SPI1_IRQ_PRIORITY,
|
|
|
|
(stm32_dmaisr_t)spi_lld_serve_rx_interrupt,
|
|
|
|
(void *)spip);
|
|
|
|
chDbgAssert(!b, "spi_lld_start(), #1", "stream already allocated");
|
2011-11-10 17:54:41 +00:00
|
|
|
b = dmaStreamAllocate(spip->dmatx,
|
2011-08-28 08:53:14 +00:00
|
|
|
STM32_SPI_SPI1_IRQ_PRIORITY,
|
|
|
|
(stm32_dmaisr_t)spi_lld_serve_tx_interrupt,
|
|
|
|
(void *)spip);
|
|
|
|
chDbgAssert(!b, "spi_lld_start(), #2", "stream already allocated");
|
2011-09-16 17:38:22 +00:00
|
|
|
rccEnableSPI1(FALSE);
|
2009-11-29 08:50:13 +00:00
|
|
|
}
|
|
|
|
#endif
|
2010-08-08 07:57:28 +00:00
|
|
|
#if STM32_SPI_USE_SPI2
|
2009-11-29 08:50:13 +00:00
|
|
|
if (&SPID2 == spip) {
|
2011-08-28 08:53:14 +00:00
|
|
|
bool_t b;
|
2011-11-10 17:54:41 +00:00
|
|
|
b = dmaStreamAllocate(spip->dmarx,
|
2011-08-28 08:53:14 +00:00
|
|
|
STM32_SPI_SPI2_IRQ_PRIORITY,
|
|
|
|
(stm32_dmaisr_t)spi_lld_serve_rx_interrupt,
|
|
|
|
(void *)spip);
|
|
|
|
chDbgAssert(!b, "spi_lld_start(), #3", "stream already allocated");
|
2011-11-10 17:54:41 +00:00
|
|
|
b = dmaStreamAllocate(spip->dmatx,
|
2011-08-28 08:53:14 +00:00
|
|
|
STM32_SPI_SPI2_IRQ_PRIORITY,
|
|
|
|
(stm32_dmaisr_t)spi_lld_serve_tx_interrupt,
|
|
|
|
(void *)spip);
|
|
|
|
chDbgAssert(!b, "spi_lld_start(), #4", "stream already allocated");
|
2011-09-16 17:38:22 +00:00
|
|
|
rccEnableSPI2(FALSE);
|
2009-11-29 08:50:13 +00:00
|
|
|
}
|
2010-07-12 16:54:48 +00:00
|
|
|
#endif
|
2010-08-08 07:57:28 +00:00
|
|
|
#if STM32_SPI_USE_SPI3
|
2010-07-12 16:54:48 +00:00
|
|
|
if (&SPID3 == spip) {
|
2011-08-28 08:53:14 +00:00
|
|
|
bool_t b;
|
2011-11-10 17:54:41 +00:00
|
|
|
b = dmaStreamAllocate(spip->dmarx,
|
2011-08-28 08:53:14 +00:00
|
|
|
STM32_SPI_SPI3_IRQ_PRIORITY,
|
|
|
|
(stm32_dmaisr_t)spi_lld_serve_rx_interrupt,
|
|
|
|
(void *)spip);
|
|
|
|
chDbgAssert(!b, "spi_lld_start(), #5", "stream already allocated");
|
2011-11-10 17:54:41 +00:00
|
|
|
b = dmaStreamAllocate(spip->dmatx,
|
2011-08-28 08:53:14 +00:00
|
|
|
STM32_SPI_SPI3_IRQ_PRIORITY,
|
|
|
|
(stm32_dmaisr_t)spi_lld_serve_tx_interrupt,
|
|
|
|
(void *)spip);
|
|
|
|
chDbgAssert(!b, "spi_lld_start(), #6", "stream already allocated");
|
2011-09-16 17:38:22 +00:00
|
|
|
rccEnableSPI3(FALSE);
|
2010-07-12 16:54:48 +00:00
|
|
|
}
|
2009-11-29 08:50:13 +00:00
|
|
|
#endif
|
2010-07-22 20:26:22 +00:00
|
|
|
|
|
|
|
/* DMA setup.*/
|
2011-08-28 08:53:14 +00:00
|
|
|
dmaStreamSetPeripheral(spip->dmarx, &spip->spi->DR);
|
|
|
|
dmaStreamSetPeripheral(spip->dmatx, &spip->spi->DR);
|
2009-11-29 08:50:13 +00:00
|
|
|
}
|
|
|
|
|
2011-11-10 17:54:41 +00:00
|
|
|
/* Configuration-specific DMA setup.*/
|
2012-05-24 18:31:34 +00:00
|
|
|
if ((spip->config->cr1 & SPI_CR1_DFF) == 0) {
|
|
|
|
/* Frame width is 8 bits or smaller.*/
|
2011-11-10 17:54:41 +00:00
|
|
|
spip->rxdmamode = (spip->rxdmamode & ~STM32_DMA_CR_SIZE_MASK) |
|
|
|
|
STM32_DMA_CR_PSIZE_BYTE | STM32_DMA_CR_MSIZE_BYTE;
|
|
|
|
spip->txdmamode = (spip->txdmamode & ~STM32_DMA_CR_SIZE_MASK) |
|
|
|
|
STM32_DMA_CR_PSIZE_BYTE | STM32_DMA_CR_MSIZE_BYTE;
|
|
|
|
}
|
2012-05-24 18:31:34 +00:00
|
|
|
else {
|
|
|
|
/* Frame width is larger than 8 bits.*/
|
2011-11-10 17:54:41 +00:00
|
|
|
spip->rxdmamode = (spip->rxdmamode & ~STM32_DMA_CR_SIZE_MASK) |
|
|
|
|
STM32_DMA_CR_PSIZE_HWORD | STM32_DMA_CR_MSIZE_HWORD;
|
|
|
|
spip->txdmamode = (spip->txdmamode & ~STM32_DMA_CR_SIZE_MASK) |
|
|
|
|
STM32_DMA_CR_PSIZE_HWORD | STM32_DMA_CR_MSIZE_HWORD;
|
|
|
|
}
|
2010-08-13 09:01:55 +00:00
|
|
|
/* SPI setup and enable.*/
|
2011-04-23 17:03:27 +00:00
|
|
|
spip->spi->CR1 = 0;
|
|
|
|
spip->spi->CR1 = spip->config->cr1 | SPI_CR1_MSTR | SPI_CR1_SSM |
|
|
|
|
SPI_CR1_SSI;
|
|
|
|
spip->spi->CR2 = SPI_CR2_SSOE | SPI_CR2_RXDMAEN | SPI_CR2_TXDMAEN;
|
|
|
|
spip->spi->CR1 |= SPI_CR1_SPE;
|
2009-11-29 08:50:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-07-26 15:01:58 +00:00
|
|
|
* @brief Deactivates the SPI peripheral.
|
2009-11-29 08:50:13 +00:00
|
|
|
*
|
|
|
|
* @param[in] spip pointer to the @p SPIDriver object
|
2010-10-04 17:16:18 +00:00
|
|
|
*
|
|
|
|
* @notapi
|
2009-11-29 08:50:13 +00:00
|
|
|
*/
|
|
|
|
void spi_lld_stop(SPIDriver *spip) {
|
|
|
|
|
|
|
|
/* If in ready state then disables the SPI clock.*/
|
2011-03-08 10:09:57 +00:00
|
|
|
if (spip->state == SPI_READY) {
|
2010-08-13 09:01:55 +00:00
|
|
|
|
|
|
|
/* SPI disable.*/
|
2011-03-08 10:09:57 +00:00
|
|
|
spip->spi->CR1 = 0;
|
2012-12-05 15:07:55 +00:00
|
|
|
spip->spi->CR2 = 0;
|
2011-11-10 17:54:41 +00:00
|
|
|
dmaStreamRelease(spip->dmarx);
|
|
|
|
dmaStreamRelease(spip->dmatx);
|
2010-08-13 09:01:55 +00:00
|
|
|
|
2010-08-08 07:57:28 +00:00
|
|
|
#if STM32_SPI_USE_SPI1
|
2011-11-10 17:54:41 +00:00
|
|
|
if (&SPID1 == spip)
|
2011-09-16 17:38:22 +00:00
|
|
|
rccDisableSPI1(FALSE);
|
2009-11-29 08:50:13 +00:00
|
|
|
#endif
|
2010-08-08 07:57:28 +00:00
|
|
|
#if STM32_SPI_USE_SPI2
|
2011-11-10 17:54:41 +00:00
|
|
|
if (&SPID2 == spip)
|
2011-09-16 17:38:22 +00:00
|
|
|
rccDisableSPI2(FALSE);
|
2010-07-12 16:54:48 +00:00
|
|
|
#endif
|
2010-08-08 07:57:28 +00:00
|
|
|
#if STM32_SPI_USE_SPI3
|
2011-11-10 17:54:41 +00:00
|
|
|
if (&SPID3 == spip)
|
2011-09-16 17:38:22 +00:00
|
|
|
rccDisableSPI3(FALSE);
|
2009-11-29 08:50:13 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-07-26 15:01:58 +00:00
|
|
|
* @brief Asserts the slave select signal and prepares for transfers.
|
2009-11-29 08:50:13 +00:00
|
|
|
*
|
|
|
|
* @param[in] spip pointer to the @p SPIDriver object
|
2010-10-04 17:16:18 +00:00
|
|
|
*
|
|
|
|
* @notapi
|
2009-11-29 08:50:13 +00:00
|
|
|
*/
|
|
|
|
void spi_lld_select(SPIDriver *spip) {
|
|
|
|
|
2011-03-08 10:09:57 +00:00
|
|
|
palClearPad(spip->config->ssport, spip->config->sspad);
|
2009-11-29 08:50:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-07-26 15:01:58 +00:00
|
|
|
* @brief Deasserts the slave select signal.
|
2009-11-29 08:50:13 +00:00
|
|
|
* @details The previously selected peripheral is unselected.
|
|
|
|
*
|
|
|
|
* @param[in] spip pointer to the @p SPIDriver object
|
2010-10-04 17:16:18 +00:00
|
|
|
*
|
|
|
|
* @notapi
|
2009-11-29 08:50:13 +00:00
|
|
|
*/
|
|
|
|
void spi_lld_unselect(SPIDriver *spip) {
|
|
|
|
|
2011-03-08 10:09:57 +00:00
|
|
|
palSetPad(spip->config->ssport, spip->config->sspad);
|
2009-11-29 08:50:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-07-26 15:01:58 +00:00
|
|
|
* @brief Ignores data on the SPI bus.
|
2010-10-11 11:48:03 +00:00
|
|
|
* @details This asynchronous function starts the transmission of a series of
|
|
|
|
* idle words on the SPI bus and ignores the received data.
|
|
|
|
* @post At the end of the operation the configured callback is invoked.
|
2009-11-29 08:50:13 +00:00
|
|
|
*
|
|
|
|
* @param[in] spip pointer to the @p SPIDriver object
|
|
|
|
* @param[in] n number of words to be ignored
|
2010-10-04 17:16:18 +00:00
|
|
|
*
|
|
|
|
* @notapi
|
2009-11-29 08:50:13 +00:00
|
|
|
*/
|
|
|
|
void spi_lld_ignore(SPIDriver *spip, size_t n) {
|
2010-08-10 14:07:42 +00:00
|
|
|
|
2011-08-28 08:53:14 +00:00
|
|
|
dmaStreamSetMemory0(spip->dmarx, &dummyrx);
|
|
|
|
dmaStreamSetTransactionSize(spip->dmarx, n);
|
2012-06-18 08:52:26 +00:00
|
|
|
dmaStreamSetMode(spip->dmarx, spip->rxdmamode);
|
2011-11-13 10:55:33 +00:00
|
|
|
|
2011-08-28 08:53:14 +00:00
|
|
|
dmaStreamSetMemory0(spip->dmatx, &dummytx);
|
|
|
|
dmaStreamSetTransactionSize(spip->dmatx, n);
|
2012-06-18 08:52:26 +00:00
|
|
|
dmaStreamSetMode(spip->dmatx, spip->txdmamode);
|
|
|
|
|
|
|
|
dmaStreamEnable(spip->dmarx);
|
|
|
|
dmaStreamEnable(spip->dmatx);
|
2009-11-29 08:50:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-07-26 15:01:58 +00:00
|
|
|
* @brief Exchanges data on the SPI bus.
|
2010-10-11 11:48:03 +00:00
|
|
|
* @details This asynchronous function starts a simultaneous transmit/receive
|
|
|
|
* operation.
|
|
|
|
* @post At the end of the operation the configured callback is invoked.
|
|
|
|
* @note The buffers are organized as uint8_t arrays for data sizes below or
|
|
|
|
* equal to 8 bits else it is organized as uint16_t arrays.
|
2009-11-29 08:50:13 +00:00
|
|
|
*
|
|
|
|
* @param[in] spip pointer to the @p SPIDriver object
|
|
|
|
* @param[in] n number of words to be exchanged
|
|
|
|
* @param[in] txbuf the pointer to the transmit buffer
|
|
|
|
* @param[out] rxbuf the pointer to the receive buffer
|
2010-10-04 17:16:18 +00:00
|
|
|
*
|
|
|
|
* @notapi
|
2009-11-29 08:50:13 +00:00
|
|
|
*/
|
|
|
|
void spi_lld_exchange(SPIDriver *spip, size_t n,
|
|
|
|
const void *txbuf, void *rxbuf) {
|
|
|
|
|
2011-08-28 08:53:14 +00:00
|
|
|
dmaStreamSetMemory0(spip->dmarx, rxbuf);
|
|
|
|
dmaStreamSetTransactionSize(spip->dmarx, n);
|
2012-06-18 08:52:26 +00:00
|
|
|
dmaStreamSetMode(spip->dmarx, spip->rxdmamode| STM32_DMA_CR_MINC);
|
|
|
|
|
2011-08-28 08:53:14 +00:00
|
|
|
dmaStreamSetMemory0(spip->dmatx, txbuf);
|
|
|
|
dmaStreamSetTransactionSize(spip->dmatx, n);
|
2012-06-18 08:52:26 +00:00
|
|
|
dmaStreamSetMode(spip->dmatx, spip->txdmamode | STM32_DMA_CR_MINC);
|
|
|
|
|
|
|
|
dmaStreamEnable(spip->dmarx);
|
|
|
|
dmaStreamEnable(spip->dmatx);
|
2009-11-29 08:50:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-10-11 11:48:03 +00:00
|
|
|
* @brief Sends data over the SPI bus.
|
|
|
|
* @details This asynchronous function starts a transmit operation.
|
|
|
|
* @post At the end of the operation the configured callback is invoked.
|
2010-07-26 15:01:58 +00:00
|
|
|
* @note The buffers are organized as uint8_t arrays for data sizes below or
|
|
|
|
* equal to 8 bits else it is organized as uint16_t arrays.
|
2009-11-29 08:50:13 +00:00
|
|
|
*
|
|
|
|
* @param[in] spip pointer to the @p SPIDriver object
|
|
|
|
* @param[in] n number of words to send
|
|
|
|
* @param[in] txbuf the pointer to the transmit buffer
|
2010-10-04 17:16:18 +00:00
|
|
|
*
|
|
|
|
* @notapi
|
2009-11-29 08:50:13 +00:00
|
|
|
*/
|
|
|
|
void spi_lld_send(SPIDriver *spip, size_t n, const void *txbuf) {
|
|
|
|
|
2011-08-28 08:53:14 +00:00
|
|
|
dmaStreamSetMemory0(spip->dmarx, &dummyrx);
|
|
|
|
dmaStreamSetTransactionSize(spip->dmarx, n);
|
2012-06-18 08:52:26 +00:00
|
|
|
dmaStreamSetMode(spip->dmarx, spip->rxdmamode);
|
2011-11-13 10:55:33 +00:00
|
|
|
|
2011-08-28 08:53:14 +00:00
|
|
|
dmaStreamSetMemory0(spip->dmatx, txbuf);
|
|
|
|
dmaStreamSetTransactionSize(spip->dmatx, n);
|
2012-06-18 08:52:26 +00:00
|
|
|
dmaStreamSetMode(spip->dmatx, spip->txdmamode | STM32_DMA_CR_MINC);
|
|
|
|
|
|
|
|
dmaStreamEnable(spip->dmarx);
|
|
|
|
dmaStreamEnable(spip->dmatx);
|
2009-11-29 08:50:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-07-26 15:01:58 +00:00
|
|
|
* @brief Receives data from the SPI bus.
|
2010-10-11 11:48:03 +00:00
|
|
|
* @details This asynchronous function starts a receive operation.
|
|
|
|
* @post At the end of the operation the configured callback is invoked.
|
2010-07-26 15:01:58 +00:00
|
|
|
* @note The buffers are organized as uint8_t arrays for data sizes below or
|
|
|
|
* equal to 8 bits else it is organized as uint16_t arrays.
|
2009-11-29 08:50:13 +00:00
|
|
|
*
|
|
|
|
* @param[in] spip pointer to the @p SPIDriver object
|
|
|
|
* @param[in] n number of words to receive
|
|
|
|
* @param[out] rxbuf the pointer to the receive buffer
|
2010-10-04 17:16:18 +00:00
|
|
|
*
|
|
|
|
* @notapi
|
2009-11-29 08:50:13 +00:00
|
|
|
*/
|
|
|
|
void spi_lld_receive(SPIDriver *spip, size_t n, void *rxbuf) {
|
|
|
|
|
2011-08-28 08:53:14 +00:00
|
|
|
dmaStreamSetMemory0(spip->dmarx, rxbuf);
|
|
|
|
dmaStreamSetTransactionSize(spip->dmarx, n);
|
2012-06-18 08:52:26 +00:00
|
|
|
dmaStreamSetMode(spip->dmarx, spip->rxdmamode | STM32_DMA_CR_MINC);
|
|
|
|
|
2011-08-28 08:53:14 +00:00
|
|
|
dmaStreamSetMemory0(spip->dmatx, &dummytx);
|
|
|
|
dmaStreamSetTransactionSize(spip->dmatx, n);
|
2012-06-18 08:52:26 +00:00
|
|
|
dmaStreamSetMode(spip->dmatx, spip->txdmamode);
|
|
|
|
|
|
|
|
dmaStreamEnable(spip->dmarx);
|
|
|
|
dmaStreamEnable(spip->dmatx);
|
2009-11-29 08:50:13 +00:00
|
|
|
}
|
|
|
|
|
2010-10-30 13:20:29 +00:00
|
|
|
/**
|
|
|
|
* @brief Exchanges one frame using a polled wait.
|
|
|
|
* @details This synchronous function exchanges one frame using a polled
|
|
|
|
* synchronization method. This function is useful when exchanging
|
|
|
|
* small amount of data on high speed channels, usually in this
|
|
|
|
* situation is much more efficient just wait for completion using
|
|
|
|
* polling than suspending the thread waiting for an interrupt.
|
|
|
|
*
|
|
|
|
* @param[in] spip pointer to the @p SPIDriver object
|
|
|
|
* @param[in] frame the data frame to send over the SPI bus
|
|
|
|
* @return The received data frame from the SPI bus.
|
|
|
|
*/
|
|
|
|
uint16_t spi_lld_polled_exchange(SPIDriver *spip, uint16_t frame) {
|
|
|
|
|
2011-03-08 10:09:57 +00:00
|
|
|
spip->spi->DR = frame;
|
|
|
|
while ((spip->spi->SR & SPI_SR_RXNE) == 0)
|
2010-10-30 13:20:29 +00:00
|
|
|
;
|
2011-03-08 10:09:57 +00:00
|
|
|
return spip->spi->DR;
|
2010-10-30 13:20:29 +00:00
|
|
|
}
|
|
|
|
|
2010-11-01 17:29:56 +00:00
|
|
|
#endif /* HAL_USE_SPI */
|
2009-11-29 08:50:13 +00:00
|
|
|
|
|
|
|
/** @} */
|