STM32L443xx HAL User Manual
stm32l4xx_ll_spi.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32l4xx_ll_spi.c
00004   * @author  MCD Application Team
00005   * @brief   SPI LL module driver.
00006   ******************************************************************************
00007   * @attention
00008   *
00009   * Copyright (c) 2017 STMicroelectronics.
00010   * All rights reserved.
00011   *
00012   * This software is licensed under terms that can be found in the LICENSE file
00013   * in the root directory of this software component.
00014   * If no LICENSE file comes with this software, it is provided AS-IS.
00015   *
00016   ******************************************************************************
00017   */
00018 #if defined(USE_FULL_LL_DRIVER)
00019 
00020 /* Includes ------------------------------------------------------------------*/
00021 #include "stm32l4xx_ll_spi.h"
00022 #include "stm32l4xx_ll_bus.h"
00023 
00024 #ifdef  USE_FULL_ASSERT
00025 #include "stm32_assert.h"
00026 #else
00027 #define assert_param(expr) ((void)0U)
00028 #endif /* USE_FULL_ASSERT */
00029 
00030 /** @addtogroup STM32L4xx_LL_Driver
00031   * @{
00032   */
00033 
00034 #if defined (SPI1) || defined (SPI2) || defined (SPI3)
00035 
00036 /** @addtogroup SPI_LL
00037   * @{
00038   */
00039 
00040 /* Private types -------------------------------------------------------------*/
00041 /* Private variables ---------------------------------------------------------*/
00042 
00043 /* Private constants ---------------------------------------------------------*/
00044 /** @defgroup SPI_LL_Private_Constants SPI Private Constants
00045   * @{
00046   */
00047 /* SPI registers Masks */
00048 #define SPI_CR1_CLEAR_MASK                 (SPI_CR1_CPHA    | SPI_CR1_CPOL     | SPI_CR1_MSTR   | \
00049                                             SPI_CR1_BR      | SPI_CR1_LSBFIRST | SPI_CR1_SSI    | \
00050                                             SPI_CR1_SSM     | SPI_CR1_RXONLY   | SPI_CR1_CRCL   | \
00051                                             SPI_CR1_CRCNEXT | SPI_CR1_CRCEN    | SPI_CR1_BIDIOE | \
00052                                             SPI_CR1_BIDIMODE)
00053 /**
00054   * @}
00055   */
00056 
00057 /* Private macros ------------------------------------------------------------*/
00058 /** @defgroup SPI_LL_Private_Macros SPI Private Macros
00059   * @{
00060   */
00061 #define IS_LL_SPI_TRANSFER_DIRECTION(__VALUE__) (((__VALUE__) == LL_SPI_FULL_DUPLEX)       \
00062                                                  || ((__VALUE__) == LL_SPI_SIMPLEX_RX)     \
00063                                                  || ((__VALUE__) == LL_SPI_HALF_DUPLEX_RX) \
00064                                                  || ((__VALUE__) == LL_SPI_HALF_DUPLEX_TX))
00065 
00066 #define IS_LL_SPI_MODE(__VALUE__) (((__VALUE__) == LL_SPI_MODE_MASTER) \
00067                                    || ((__VALUE__) == LL_SPI_MODE_SLAVE))
00068 
00069 #define IS_LL_SPI_DATAWIDTH(__VALUE__) (((__VALUE__) == LL_SPI_DATAWIDTH_4BIT)     \
00070                                         || ((__VALUE__) == LL_SPI_DATAWIDTH_5BIT)  \
00071                                         || ((__VALUE__) == LL_SPI_DATAWIDTH_6BIT)  \
00072                                         || ((__VALUE__) == LL_SPI_DATAWIDTH_7BIT)  \
00073                                         || ((__VALUE__) == LL_SPI_DATAWIDTH_8BIT)  \
00074                                         || ((__VALUE__) == LL_SPI_DATAWIDTH_9BIT)  \
00075                                         || ((__VALUE__) == LL_SPI_DATAWIDTH_10BIT) \
00076                                         || ((__VALUE__) == LL_SPI_DATAWIDTH_11BIT) \
00077                                         || ((__VALUE__) == LL_SPI_DATAWIDTH_12BIT) \
00078                                         || ((__VALUE__) == LL_SPI_DATAWIDTH_13BIT) \
00079                                         || ((__VALUE__) == LL_SPI_DATAWIDTH_14BIT) \
00080                                         || ((__VALUE__) == LL_SPI_DATAWIDTH_15BIT) \
00081                                         || ((__VALUE__) == LL_SPI_DATAWIDTH_16BIT))
00082 
00083 #define IS_LL_SPI_POLARITY(__VALUE__) (((__VALUE__) == LL_SPI_POLARITY_LOW) \
00084                                        || ((__VALUE__) == LL_SPI_POLARITY_HIGH))
00085 
00086 #define IS_LL_SPI_PHASE(__VALUE__) (((__VALUE__) == LL_SPI_PHASE_1EDGE) \
00087                                     || ((__VALUE__) == LL_SPI_PHASE_2EDGE))
00088 
00089 #define IS_LL_SPI_NSS(__VALUE__) (((__VALUE__) == LL_SPI_NSS_SOFT)          \
00090                                   || ((__VALUE__) == LL_SPI_NSS_HARD_INPUT) \
00091                                   || ((__VALUE__) == LL_SPI_NSS_HARD_OUTPUT))
00092 
00093 #define IS_LL_SPI_BAUDRATE(__VALUE__) (((__VALUE__) == LL_SPI_BAUDRATEPRESCALER_DIV2)      \
00094                                        || ((__VALUE__) == LL_SPI_BAUDRATEPRESCALER_DIV4)   \
00095                                        || ((__VALUE__) == LL_SPI_BAUDRATEPRESCALER_DIV8)   \
00096                                        || ((__VALUE__) == LL_SPI_BAUDRATEPRESCALER_DIV16)  \
00097                                        || ((__VALUE__) == LL_SPI_BAUDRATEPRESCALER_DIV32)  \
00098                                        || ((__VALUE__) == LL_SPI_BAUDRATEPRESCALER_DIV64)  \
00099                                        || ((__VALUE__) == LL_SPI_BAUDRATEPRESCALER_DIV128) \
00100                                        || ((__VALUE__) == LL_SPI_BAUDRATEPRESCALER_DIV256))
00101 
00102 #define IS_LL_SPI_BITORDER(__VALUE__) (((__VALUE__) == LL_SPI_LSB_FIRST) \
00103                                        || ((__VALUE__) == LL_SPI_MSB_FIRST))
00104 
00105 #define IS_LL_SPI_CRCCALCULATION(__VALUE__) (((__VALUE__) == LL_SPI_CRCCALCULATION_ENABLE) \
00106                                              || ((__VALUE__) == LL_SPI_CRCCALCULATION_DISABLE))
00107 
00108 #define IS_LL_SPI_CRC_POLYNOMIAL(__VALUE__) ((__VALUE__) >= 0x1U)
00109 
00110 /**
00111   * @}
00112   */
00113 
00114 /* Private function prototypes -----------------------------------------------*/
00115 
00116 /* Exported functions --------------------------------------------------------*/
00117 /** @addtogroup SPI_LL_Exported_Functions
00118   * @{
00119   */
00120 
00121 /** @addtogroup SPI_LL_EF_Init
00122   * @{
00123   */
00124 
00125 /**
00126   * @brief  De-initialize the SPI registers to their default reset values.
00127   * @param  SPIx SPI Instance
00128   * @retval An ErrorStatus enumeration value:
00129   *          - SUCCESS: SPI registers are de-initialized
00130   *          - ERROR: SPI registers are not de-initialized
00131   */
00132 ErrorStatus LL_SPI_DeInit(SPI_TypeDef *SPIx)
00133 {
00134   ErrorStatus status = ERROR;
00135 
00136   /* Check the parameters */
00137   assert_param(IS_SPI_ALL_INSTANCE(SPIx));
00138 
00139 #if defined(SPI1)
00140   if (SPIx == SPI1)
00141   {
00142     /* Force reset of SPI clock */
00143     LL_APB2_GRP1_ForceReset(LL_APB2_GRP1_PERIPH_SPI1);
00144 
00145     /* Release reset of SPI clock */
00146     LL_APB2_GRP1_ReleaseReset(LL_APB2_GRP1_PERIPH_SPI1);
00147 
00148     status = SUCCESS;
00149   }
00150 #endif /* SPI1 */
00151 #if defined(SPI2)
00152   if (SPIx == SPI2)
00153   {
00154     /* Force reset of SPI clock */
00155     LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_SPI2);
00156 
00157     /* Release reset of SPI clock */
00158     LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_SPI2);
00159 
00160     status = SUCCESS;
00161   }
00162 #endif /* SPI2 */
00163 #if defined(SPI3)
00164   if (SPIx == SPI3)
00165   {
00166     /* Force reset of SPI clock */
00167     LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_SPI3);
00168 
00169     /* Release reset of SPI clock */
00170     LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_SPI3);
00171 
00172     status = SUCCESS;
00173   }
00174 #endif /* SPI3 */
00175 
00176   return status;
00177 }
00178 
00179 /**
00180   * @brief  Initialize the SPI registers according to the specified parameters in SPI_InitStruct.
00181   * @note   As some bits in SPI configuration registers can only be written when the SPI is disabled (SPI_CR1_SPE bit =0),
00182   *         SPI peripheral should be in disabled state prior calling this function. Otherwise, ERROR result will be returned.
00183   * @param  SPIx SPI Instance
00184   * @param  SPI_InitStruct pointer to a @ref LL_SPI_InitTypeDef structure
00185   * @retval An ErrorStatus enumeration value. (Return always SUCCESS)
00186   */
00187 ErrorStatus LL_SPI_Init(SPI_TypeDef *SPIx, LL_SPI_InitTypeDef *SPI_InitStruct)
00188 {
00189   ErrorStatus status = ERROR;
00190 
00191   /* Check the SPI Instance SPIx*/
00192   assert_param(IS_SPI_ALL_INSTANCE(SPIx));
00193 
00194   /* Check the SPI parameters from SPI_InitStruct*/
00195   assert_param(IS_LL_SPI_TRANSFER_DIRECTION(SPI_InitStruct->TransferDirection));
00196   assert_param(IS_LL_SPI_MODE(SPI_InitStruct->Mode));
00197   assert_param(IS_LL_SPI_DATAWIDTH(SPI_InitStruct->DataWidth));
00198   assert_param(IS_LL_SPI_POLARITY(SPI_InitStruct->ClockPolarity));
00199   assert_param(IS_LL_SPI_PHASE(SPI_InitStruct->ClockPhase));
00200   assert_param(IS_LL_SPI_NSS(SPI_InitStruct->NSS));
00201   assert_param(IS_LL_SPI_BAUDRATE(SPI_InitStruct->BaudRate));
00202   assert_param(IS_LL_SPI_BITORDER(SPI_InitStruct->BitOrder));
00203   assert_param(IS_LL_SPI_CRCCALCULATION(SPI_InitStruct->CRCCalculation));
00204 
00205   if (LL_SPI_IsEnabled(SPIx) == 0x00000000U)
00206   {
00207     /*---------------------------- SPIx CR1 Configuration ------------------------
00208      * Configure SPIx CR1 with parameters:
00209      * - TransferDirection:  SPI_CR1_BIDIMODE, SPI_CR1_BIDIOE and SPI_CR1_RXONLY bits
00210      * - Master/Slave Mode:  SPI_CR1_MSTR bit
00211      * - ClockPolarity:      SPI_CR1_CPOL bit
00212      * - ClockPhase:         SPI_CR1_CPHA bit
00213      * - NSS management:     SPI_CR1_SSM bit
00214      * - BaudRate prescaler: SPI_CR1_BR[2:0] bits
00215      * - BitOrder:           SPI_CR1_LSBFIRST bit
00216      * - CRCCalculation:     SPI_CR1_CRCEN bit
00217      */
00218     MODIFY_REG(SPIx->CR1,
00219                SPI_CR1_CLEAR_MASK,
00220                SPI_InitStruct->TransferDirection | SPI_InitStruct->Mode |
00221                SPI_InitStruct->ClockPolarity | SPI_InitStruct->ClockPhase |
00222                SPI_InitStruct->NSS | SPI_InitStruct->BaudRate |
00223                SPI_InitStruct->BitOrder | SPI_InitStruct->CRCCalculation);
00224 
00225     /*---------------------------- SPIx CR2 Configuration ------------------------
00226      * Configure SPIx CR2 with parameters:
00227      * - DataWidth:          DS[3:0] bits
00228      * - NSS management:     SSOE bit
00229      */
00230     MODIFY_REG(SPIx->CR2,
00231                SPI_CR2_DS | SPI_CR2_SSOE,
00232                SPI_InitStruct->DataWidth | (SPI_InitStruct->NSS >> 16U));
00233 
00234     /* Set Rx FIFO to Quarter (1 Byte) in case of 8 Bits mode. No DataPacking by default */
00235     if (SPI_InitStruct->DataWidth < LL_SPI_DATAWIDTH_9BIT)
00236     {
00237       LL_SPI_SetRxFIFOThreshold(SPIx, LL_SPI_RX_FIFO_TH_QUARTER);
00238     }
00239 
00240     /*---------------------------- SPIx CRCPR Configuration ----------------------
00241      * Configure SPIx CRCPR with parameters:
00242      * - CRCPoly:            CRCPOLY[15:0] bits
00243      */
00244     if (SPI_InitStruct->CRCCalculation == LL_SPI_CRCCALCULATION_ENABLE)
00245     {
00246       assert_param(IS_LL_SPI_CRC_POLYNOMIAL(SPI_InitStruct->CRCPoly));
00247       LL_SPI_SetCRCPolynomial(SPIx, SPI_InitStruct->CRCPoly);
00248     }
00249     status = SUCCESS;
00250   }
00251 
00252   return status;
00253 }
00254 
00255 /**
00256   * @brief  Set each @ref LL_SPI_InitTypeDef field to default value.
00257   * @param  SPI_InitStruct pointer to a @ref LL_SPI_InitTypeDef structure
00258   * whose fields will be set to default values.
00259   * @retval None
00260   */
00261 void LL_SPI_StructInit(LL_SPI_InitTypeDef *SPI_InitStruct)
00262 {
00263   /* Set SPI_InitStruct fields to default values */
00264   SPI_InitStruct->TransferDirection = LL_SPI_FULL_DUPLEX;
00265   SPI_InitStruct->Mode              = LL_SPI_MODE_SLAVE;
00266   SPI_InitStruct->DataWidth         = LL_SPI_DATAWIDTH_8BIT;
00267   SPI_InitStruct->ClockPolarity     = LL_SPI_POLARITY_LOW;
00268   SPI_InitStruct->ClockPhase        = LL_SPI_PHASE_1EDGE;
00269   SPI_InitStruct->NSS               = LL_SPI_NSS_HARD_INPUT;
00270   SPI_InitStruct->BaudRate          = LL_SPI_BAUDRATEPRESCALER_DIV2;
00271   SPI_InitStruct->BitOrder          = LL_SPI_MSB_FIRST;
00272   SPI_InitStruct->CRCCalculation    = LL_SPI_CRCCALCULATION_DISABLE;
00273   SPI_InitStruct->CRCPoly           = 7U;
00274 }
00275 
00276 /**
00277   * @}
00278   */
00279 
00280 /**
00281   * @}
00282   */
00283 
00284 /**
00285   * @}
00286   */
00287 
00288 #endif /* defined (SPI1) || defined (SPI2) || defined (SPI3) */
00289 
00290 /**
00291   * @}
00292   */
00293 
00294 #endif /* USE_FULL_LL_DRIVER */
00295