001

Committer:
ganlikun
Date:
Sun Jun 12 14:02:44 2022 +0000
Revision:
0:13413ea9a877
00

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ganlikun 0:13413ea9a877 1 /**
ganlikun 0:13413ea9a877 2 ******************************************************************************
ganlikun 0:13413ea9a877 3 * @file stm32f4xx_ll_spi.c
ganlikun 0:13413ea9a877 4 * @author MCD Application Team
ganlikun 0:13413ea9a877 5 * @version V1.7.1
ganlikun 0:13413ea9a877 6 * @date 14-April-2017
ganlikun 0:13413ea9a877 7 * @brief SPI LL module driver.
ganlikun 0:13413ea9a877 8 ******************************************************************************
ganlikun 0:13413ea9a877 9 * @attention
ganlikun 0:13413ea9a877 10 *
ganlikun 0:13413ea9a877 11 * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
ganlikun 0:13413ea9a877 12 *
ganlikun 0:13413ea9a877 13 * Redistribution and use in source and binary forms, with or without modification,
ganlikun 0:13413ea9a877 14 * are permitted provided that the following conditions are met:
ganlikun 0:13413ea9a877 15 * 1. Redistributions of source code must retain the above copyright notice,
ganlikun 0:13413ea9a877 16 * this list of conditions and the following disclaimer.
ganlikun 0:13413ea9a877 17 * 2. Redistributions in binary form must reproduce the above copyright notice,
ganlikun 0:13413ea9a877 18 * this list of conditions and the following disclaimer in the documentation
ganlikun 0:13413ea9a877 19 * and/or other materials provided with the distribution.
ganlikun 0:13413ea9a877 20 * 3. Neither the name of STMicroelectronics nor the names of its contributors
ganlikun 0:13413ea9a877 21 * may be used to endorse or promote products derived from this software
ganlikun 0:13413ea9a877 22 * without specific prior written permission.
ganlikun 0:13413ea9a877 23 *
ganlikun 0:13413ea9a877 24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
ganlikun 0:13413ea9a877 25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
ganlikun 0:13413ea9a877 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
ganlikun 0:13413ea9a877 27 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
ganlikun 0:13413ea9a877 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
ganlikun 0:13413ea9a877 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
ganlikun 0:13413ea9a877 30 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
ganlikun 0:13413ea9a877 31 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
ganlikun 0:13413ea9a877 32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
ganlikun 0:13413ea9a877 33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
ganlikun 0:13413ea9a877 34 *
ganlikun 0:13413ea9a877 35 ******************************************************************************
ganlikun 0:13413ea9a877 36 */
ganlikun 0:13413ea9a877 37 #if defined(USE_FULL_LL_DRIVER)
ganlikun 0:13413ea9a877 38
ganlikun 0:13413ea9a877 39 /* Includes ------------------------------------------------------------------*/
ganlikun 0:13413ea9a877 40 #include "stm32f4xx_ll_spi.h"
ganlikun 0:13413ea9a877 41 #include "stm32f4xx_ll_bus.h"
ganlikun 0:13413ea9a877 42 #include "stm32f4xx_ll_rcc.h"
ganlikun 0:13413ea9a877 43
ganlikun 0:13413ea9a877 44 #ifdef USE_FULL_ASSERT
ganlikun 0:13413ea9a877 45 #include "stm32_assert.h"
ganlikun 0:13413ea9a877 46 #else
ganlikun 0:13413ea9a877 47 #define assert_param(expr) ((void)0U)
ganlikun 0:13413ea9a877 48 #endif
ganlikun 0:13413ea9a877 49
ganlikun 0:13413ea9a877 50 /** @addtogroup STM32F4xx_LL_Driver
ganlikun 0:13413ea9a877 51 * @{
ganlikun 0:13413ea9a877 52 */
ganlikun 0:13413ea9a877 53
ganlikun 0:13413ea9a877 54 #if defined (SPI1) || defined (SPI2) || defined (SPI3) || defined (SPI4) || defined (SPI5) || defined(SPI6)
ganlikun 0:13413ea9a877 55
ganlikun 0:13413ea9a877 56 /** @addtogroup SPI_LL
ganlikun 0:13413ea9a877 57 * @{
ganlikun 0:13413ea9a877 58 */
ganlikun 0:13413ea9a877 59
ganlikun 0:13413ea9a877 60 /* Private types -------------------------------------------------------------*/
ganlikun 0:13413ea9a877 61 /* Private variables ---------------------------------------------------------*/
ganlikun 0:13413ea9a877 62
ganlikun 0:13413ea9a877 63 /* Private constants ---------------------------------------------------------*/
ganlikun 0:13413ea9a877 64 /** @defgroup SPI_LL_Private_Constants SPI Private Constants
ganlikun 0:13413ea9a877 65 * @{
ganlikun 0:13413ea9a877 66 */
ganlikun 0:13413ea9a877 67 /* SPI registers Masks */
ganlikun 0:13413ea9a877 68 #define SPI_CR1_CLEAR_MASK (SPI_CR1_CPHA | SPI_CR1_CPOL | SPI_CR1_MSTR | \
ganlikun 0:13413ea9a877 69 SPI_CR1_BR | SPI_CR1_LSBFIRST | SPI_CR1_SSI | \
ganlikun 0:13413ea9a877 70 SPI_CR1_SSM | SPI_CR1_RXONLY | SPI_CR1_DFF | \
ganlikun 0:13413ea9a877 71 SPI_CR1_CRCNEXT | SPI_CR1_CRCEN | SPI_CR1_BIDIOE | \
ganlikun 0:13413ea9a877 72 SPI_CR1_BIDIMODE)
ganlikun 0:13413ea9a877 73 /**
ganlikun 0:13413ea9a877 74 * @}
ganlikun 0:13413ea9a877 75 */
ganlikun 0:13413ea9a877 76
ganlikun 0:13413ea9a877 77 /* Private macros ------------------------------------------------------------*/
ganlikun 0:13413ea9a877 78 /** @defgroup SPI_LL_Private_Macros SPI Private Macros
ganlikun 0:13413ea9a877 79 * @{
ganlikun 0:13413ea9a877 80 */
ganlikun 0:13413ea9a877 81 #define IS_LL_SPI_TRANSFER_DIRECTION(__VALUE__) (((__VALUE__) == LL_SPI_FULL_DUPLEX) \
ganlikun 0:13413ea9a877 82 || ((__VALUE__) == LL_SPI_SIMPLEX_RX) \
ganlikun 0:13413ea9a877 83 || ((__VALUE__) == LL_SPI_HALF_DUPLEX_RX) \
ganlikun 0:13413ea9a877 84 || ((__VALUE__) == LL_SPI_HALF_DUPLEX_TX))
ganlikun 0:13413ea9a877 85
ganlikun 0:13413ea9a877 86 #define IS_LL_SPI_MODE(__VALUE__) (((__VALUE__) == LL_SPI_MODE_MASTER) \
ganlikun 0:13413ea9a877 87 || ((__VALUE__) == LL_SPI_MODE_SLAVE))
ganlikun 0:13413ea9a877 88
ganlikun 0:13413ea9a877 89 #define IS_LL_SPI_DATAWIDTH(__VALUE__) (((__VALUE__) == LL_SPI_DATAWIDTH_8BIT) \
ganlikun 0:13413ea9a877 90 || ((__VALUE__) == LL_SPI_DATAWIDTH_16BIT))
ganlikun 0:13413ea9a877 91
ganlikun 0:13413ea9a877 92 #define IS_LL_SPI_POLARITY(__VALUE__) (((__VALUE__) == LL_SPI_POLARITY_LOW) \
ganlikun 0:13413ea9a877 93 || ((__VALUE__) == LL_SPI_POLARITY_HIGH))
ganlikun 0:13413ea9a877 94
ganlikun 0:13413ea9a877 95 #define IS_LL_SPI_PHASE(__VALUE__) (((__VALUE__) == LL_SPI_PHASE_1EDGE) \
ganlikun 0:13413ea9a877 96 || ((__VALUE__) == LL_SPI_PHASE_2EDGE))
ganlikun 0:13413ea9a877 97
ganlikun 0:13413ea9a877 98 #define IS_LL_SPI_NSS(__VALUE__) (((__VALUE__) == LL_SPI_NSS_SOFT) \
ganlikun 0:13413ea9a877 99 || ((__VALUE__) == LL_SPI_NSS_HARD_INPUT) \
ganlikun 0:13413ea9a877 100 || ((__VALUE__) == LL_SPI_NSS_HARD_OUTPUT))
ganlikun 0:13413ea9a877 101
ganlikun 0:13413ea9a877 102 #define IS_LL_SPI_BAUDRATE(__VALUE__) (((__VALUE__) == LL_SPI_BAUDRATEPRESCALER_DIV2) \
ganlikun 0:13413ea9a877 103 || ((__VALUE__) == LL_SPI_BAUDRATEPRESCALER_DIV4) \
ganlikun 0:13413ea9a877 104 || ((__VALUE__) == LL_SPI_BAUDRATEPRESCALER_DIV8) \
ganlikun 0:13413ea9a877 105 || ((__VALUE__) == LL_SPI_BAUDRATEPRESCALER_DIV16) \
ganlikun 0:13413ea9a877 106 || ((__VALUE__) == LL_SPI_BAUDRATEPRESCALER_DIV32) \
ganlikun 0:13413ea9a877 107 || ((__VALUE__) == LL_SPI_BAUDRATEPRESCALER_DIV64) \
ganlikun 0:13413ea9a877 108 || ((__VALUE__) == LL_SPI_BAUDRATEPRESCALER_DIV128) \
ganlikun 0:13413ea9a877 109 || ((__VALUE__) == LL_SPI_BAUDRATEPRESCALER_DIV256))
ganlikun 0:13413ea9a877 110
ganlikun 0:13413ea9a877 111 #define IS_LL_SPI_BITORDER(__VALUE__) (((__VALUE__) == LL_SPI_LSB_FIRST) \
ganlikun 0:13413ea9a877 112 || ((__VALUE__) == LL_SPI_MSB_FIRST))
ganlikun 0:13413ea9a877 113
ganlikun 0:13413ea9a877 114 #define IS_LL_SPI_CRCCALCULATION(__VALUE__) (((__VALUE__) == LL_SPI_CRCCALCULATION_ENABLE) \
ganlikun 0:13413ea9a877 115 || ((__VALUE__) == LL_SPI_CRCCALCULATION_DISABLE))
ganlikun 0:13413ea9a877 116
ganlikun 0:13413ea9a877 117 #define IS_LL_SPI_CRC_POLYNOMIAL(__VALUE__) ((__VALUE__) >= 0x1U)
ganlikun 0:13413ea9a877 118
ganlikun 0:13413ea9a877 119 /**
ganlikun 0:13413ea9a877 120 * @}
ganlikun 0:13413ea9a877 121 */
ganlikun 0:13413ea9a877 122
ganlikun 0:13413ea9a877 123 /* Private function prototypes -----------------------------------------------*/
ganlikun 0:13413ea9a877 124
ganlikun 0:13413ea9a877 125 /* Exported functions --------------------------------------------------------*/
ganlikun 0:13413ea9a877 126 /** @addtogroup SPI_LL_Exported_Functions
ganlikun 0:13413ea9a877 127 * @{
ganlikun 0:13413ea9a877 128 */
ganlikun 0:13413ea9a877 129
ganlikun 0:13413ea9a877 130 /** @addtogroup SPI_LL_EF_Init
ganlikun 0:13413ea9a877 131 * @{
ganlikun 0:13413ea9a877 132 */
ganlikun 0:13413ea9a877 133
ganlikun 0:13413ea9a877 134 /**
ganlikun 0:13413ea9a877 135 * @brief De-initialize the SPI registers to their default reset values.
ganlikun 0:13413ea9a877 136 * @param SPIx SPI Instance
ganlikun 0:13413ea9a877 137 * @retval An ErrorStatus enumeration value:
ganlikun 0:13413ea9a877 138 * - SUCCESS: SPI registers are de-initialized
ganlikun 0:13413ea9a877 139 * - ERROR: SPI registers are not de-initialized
ganlikun 0:13413ea9a877 140 */
ganlikun 0:13413ea9a877 141 ErrorStatus LL_SPI_DeInit(SPI_TypeDef *SPIx)
ganlikun 0:13413ea9a877 142 {
ganlikun 0:13413ea9a877 143 ErrorStatus status = ERROR;
ganlikun 0:13413ea9a877 144
ganlikun 0:13413ea9a877 145 /* Check the parameters */
ganlikun 0:13413ea9a877 146 assert_param(IS_SPI_ALL_INSTANCE(SPIx));
ganlikun 0:13413ea9a877 147
ganlikun 0:13413ea9a877 148 #if defined(SPI1)
ganlikun 0:13413ea9a877 149 if (SPIx == SPI1)
ganlikun 0:13413ea9a877 150 {
ganlikun 0:13413ea9a877 151 /* Force reset of SPI clock */
ganlikun 0:13413ea9a877 152 LL_APB2_GRP1_ForceReset(LL_APB2_GRP1_PERIPH_SPI1);
ganlikun 0:13413ea9a877 153
ganlikun 0:13413ea9a877 154 /* Release reset of SPI clock */
ganlikun 0:13413ea9a877 155 LL_APB2_GRP1_ReleaseReset(LL_APB2_GRP1_PERIPH_SPI1);
ganlikun 0:13413ea9a877 156
ganlikun 0:13413ea9a877 157 status = SUCCESS;
ganlikun 0:13413ea9a877 158 }
ganlikun 0:13413ea9a877 159 #endif /* SPI1 */
ganlikun 0:13413ea9a877 160 #if defined(SPI2)
ganlikun 0:13413ea9a877 161 if (SPIx == SPI2)
ganlikun 0:13413ea9a877 162 {
ganlikun 0:13413ea9a877 163 /* Force reset of SPI clock */
ganlikun 0:13413ea9a877 164 LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_SPI2);
ganlikun 0:13413ea9a877 165
ganlikun 0:13413ea9a877 166 /* Release reset of SPI clock */
ganlikun 0:13413ea9a877 167 LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_SPI2);
ganlikun 0:13413ea9a877 168
ganlikun 0:13413ea9a877 169 status = SUCCESS;
ganlikun 0:13413ea9a877 170 }
ganlikun 0:13413ea9a877 171 #endif /* SPI2 */
ganlikun 0:13413ea9a877 172 #if defined(SPI3)
ganlikun 0:13413ea9a877 173 if (SPIx == SPI3)
ganlikun 0:13413ea9a877 174 {
ganlikun 0:13413ea9a877 175 /* Force reset of SPI clock */
ganlikun 0:13413ea9a877 176 LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_SPI3);
ganlikun 0:13413ea9a877 177
ganlikun 0:13413ea9a877 178 /* Release reset of SPI clock */
ganlikun 0:13413ea9a877 179 LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_SPI3);
ganlikun 0:13413ea9a877 180
ganlikun 0:13413ea9a877 181 status = SUCCESS;
ganlikun 0:13413ea9a877 182 }
ganlikun 0:13413ea9a877 183 #endif /* SPI3 */
ganlikun 0:13413ea9a877 184 #if defined(SPI4)
ganlikun 0:13413ea9a877 185 if (SPIx == SPI4)
ganlikun 0:13413ea9a877 186 {
ganlikun 0:13413ea9a877 187 /* Force reset of SPI clock */
ganlikun 0:13413ea9a877 188 LL_APB2_GRP1_ForceReset(LL_APB2_GRP1_PERIPH_SPI4);
ganlikun 0:13413ea9a877 189
ganlikun 0:13413ea9a877 190 /* Release reset of SPI clock */
ganlikun 0:13413ea9a877 191 LL_APB2_GRP1_ReleaseReset(LL_APB2_GRP1_PERIPH_SPI4);
ganlikun 0:13413ea9a877 192
ganlikun 0:13413ea9a877 193 status = SUCCESS;
ganlikun 0:13413ea9a877 194 }
ganlikun 0:13413ea9a877 195 #endif /* SPI4 */
ganlikun 0:13413ea9a877 196 #if defined(SPI5)
ganlikun 0:13413ea9a877 197 if (SPIx == SPI5)
ganlikun 0:13413ea9a877 198 {
ganlikun 0:13413ea9a877 199 /* Force reset of SPI clock */
ganlikun 0:13413ea9a877 200 LL_APB2_GRP1_ForceReset(LL_APB2_GRP1_PERIPH_SPI5);
ganlikun 0:13413ea9a877 201
ganlikun 0:13413ea9a877 202 /* Release reset of SPI clock */
ganlikun 0:13413ea9a877 203 LL_APB2_GRP1_ReleaseReset(LL_APB2_GRP1_PERIPH_SPI5);
ganlikun 0:13413ea9a877 204
ganlikun 0:13413ea9a877 205 status = SUCCESS;
ganlikun 0:13413ea9a877 206 }
ganlikun 0:13413ea9a877 207 #endif /* SPI5 */
ganlikun 0:13413ea9a877 208 #if defined(SPI6)
ganlikun 0:13413ea9a877 209 if (SPIx == SPI6)
ganlikun 0:13413ea9a877 210 {
ganlikun 0:13413ea9a877 211 /* Force reset of SPI clock */
ganlikun 0:13413ea9a877 212 LL_APB2_GRP1_ForceReset(LL_APB2_GRP1_PERIPH_SPI6);
ganlikun 0:13413ea9a877 213
ganlikun 0:13413ea9a877 214 /* Release reset of SPI clock */
ganlikun 0:13413ea9a877 215 LL_APB2_GRP1_ReleaseReset(LL_APB2_GRP1_PERIPH_SPI6);
ganlikun 0:13413ea9a877 216
ganlikun 0:13413ea9a877 217 status = SUCCESS;
ganlikun 0:13413ea9a877 218 }
ganlikun 0:13413ea9a877 219 #endif /* SPI6 */
ganlikun 0:13413ea9a877 220
ganlikun 0:13413ea9a877 221 return status;
ganlikun 0:13413ea9a877 222 }
ganlikun 0:13413ea9a877 223
ganlikun 0:13413ea9a877 224 /**
ganlikun 0:13413ea9a877 225 * @brief Initialize the SPI registers according to the specified parameters in SPI_InitStruct.
ganlikun 0:13413ea9a877 226 * @note As some bits in SPI configuration registers can only be written when the SPI is disabled (SPI_CR1_SPE bit =0),
ganlikun 0:13413ea9a877 227 * SPI IP should be in disabled state prior calling this function. Otherwise, ERROR result will be returned.
ganlikun 0:13413ea9a877 228 * @param SPIx SPI Instance
ganlikun 0:13413ea9a877 229 * @param SPI_InitStruct pointer to a @ref LL_SPI_InitTypeDef structure
ganlikun 0:13413ea9a877 230 * @retval An ErrorStatus enumeration value. (Return always SUCCESS)
ganlikun 0:13413ea9a877 231 */
ganlikun 0:13413ea9a877 232 ErrorStatus LL_SPI_Init(SPI_TypeDef *SPIx, LL_SPI_InitTypeDef *SPI_InitStruct)
ganlikun 0:13413ea9a877 233 {
ganlikun 0:13413ea9a877 234 ErrorStatus status = ERROR;
ganlikun 0:13413ea9a877 235
ganlikun 0:13413ea9a877 236 /* Check the SPI Instance SPIx*/
ganlikun 0:13413ea9a877 237 assert_param(IS_SPI_ALL_INSTANCE(SPIx));
ganlikun 0:13413ea9a877 238
ganlikun 0:13413ea9a877 239 /* Check the SPI parameters from SPI_InitStruct*/
ganlikun 0:13413ea9a877 240 assert_param(IS_LL_SPI_TRANSFER_DIRECTION(SPI_InitStruct->TransferDirection));
ganlikun 0:13413ea9a877 241 assert_param(IS_LL_SPI_MODE(SPI_InitStruct->Mode));
ganlikun 0:13413ea9a877 242 assert_param(IS_LL_SPI_DATAWIDTH(SPI_InitStruct->DataWidth));
ganlikun 0:13413ea9a877 243 assert_param(IS_LL_SPI_POLARITY(SPI_InitStruct->ClockPolarity));
ganlikun 0:13413ea9a877 244 assert_param(IS_LL_SPI_PHASE(SPI_InitStruct->ClockPhase));
ganlikun 0:13413ea9a877 245 assert_param(IS_LL_SPI_NSS(SPI_InitStruct->NSS));
ganlikun 0:13413ea9a877 246 assert_param(IS_LL_SPI_BAUDRATE(SPI_InitStruct->BaudRate));
ganlikun 0:13413ea9a877 247 assert_param(IS_LL_SPI_BITORDER(SPI_InitStruct->BitOrder));
ganlikun 0:13413ea9a877 248 assert_param(IS_LL_SPI_CRCCALCULATION(SPI_InitStruct->CRCCalculation));
ganlikun 0:13413ea9a877 249
ganlikun 0:13413ea9a877 250 if (LL_SPI_IsEnabled(SPIx) == 0x00000000U)
ganlikun 0:13413ea9a877 251 {
ganlikun 0:13413ea9a877 252 /*---------------------------- SPIx CR1 Configuration ------------------------
ganlikun 0:13413ea9a877 253 * Configure SPIx CR1 with parameters:
ganlikun 0:13413ea9a877 254 * - TransferDirection: SPI_CR1_BIDIMODE, SPI_CR1_BIDIOE and SPI_CR1_RXONLY bits
ganlikun 0:13413ea9a877 255 * - Master/Slave Mode: SPI_CR1_MSTR bit
ganlikun 0:13413ea9a877 256 * - DataWidth: SPI_CR1_DFF bit
ganlikun 0:13413ea9a877 257 * - ClockPolarity: SPI_CR1_CPOL bit
ganlikun 0:13413ea9a877 258 * - ClockPhase: SPI_CR1_CPHA bit
ganlikun 0:13413ea9a877 259 * - NSS management: SPI_CR1_SSM bit
ganlikun 0:13413ea9a877 260 * - BaudRate prescaler: SPI_CR1_BR[2:0] bits
ganlikun 0:13413ea9a877 261 * - BitOrder: SPI_CR1_LSBFIRST bit
ganlikun 0:13413ea9a877 262 * - CRCCalculation: SPI_CR1_CRCEN bit
ganlikun 0:13413ea9a877 263 */
ganlikun 0:13413ea9a877 264 MODIFY_REG(SPIx->CR1,
ganlikun 0:13413ea9a877 265 SPI_CR1_CLEAR_MASK,
ganlikun 0:13413ea9a877 266 SPI_InitStruct->TransferDirection | SPI_InitStruct->Mode | SPI_InitStruct->DataWidth |
ganlikun 0:13413ea9a877 267 SPI_InitStruct->ClockPolarity | SPI_InitStruct->ClockPhase |
ganlikun 0:13413ea9a877 268 SPI_InitStruct->NSS | SPI_InitStruct->BaudRate |
ganlikun 0:13413ea9a877 269 SPI_InitStruct->BitOrder | SPI_InitStruct->CRCCalculation);
ganlikun 0:13413ea9a877 270
ganlikun 0:13413ea9a877 271 /*---------------------------- SPIx CR2 Configuration ------------------------
ganlikun 0:13413ea9a877 272 * Configure SPIx CR2 with parameters:
ganlikun 0:13413ea9a877 273 * - NSS management: SSOE bit
ganlikun 0:13413ea9a877 274 */
ganlikun 0:13413ea9a877 275 MODIFY_REG(SPIx->CR2, SPI_CR2_SSOE, (SPI_InitStruct->NSS >> 16U));
ganlikun 0:13413ea9a877 276
ganlikun 0:13413ea9a877 277 /*---------------------------- SPIx CRCPR Configuration ----------------------
ganlikun 0:13413ea9a877 278 * Configure SPIx CRCPR with parameters:
ganlikun 0:13413ea9a877 279 * - CRCPoly: CRCPOLY[15:0] bits
ganlikun 0:13413ea9a877 280 */
ganlikun 0:13413ea9a877 281 if (SPI_InitStruct->CRCCalculation == LL_SPI_CRCCALCULATION_ENABLE)
ganlikun 0:13413ea9a877 282 {
ganlikun 0:13413ea9a877 283 assert_param(IS_LL_SPI_CRC_POLYNOMIAL(SPI_InitStruct->CRCPoly));
ganlikun 0:13413ea9a877 284 LL_SPI_SetCRCPolynomial(SPIx, SPI_InitStruct->CRCPoly);
ganlikun 0:13413ea9a877 285 }
ganlikun 0:13413ea9a877 286 status = SUCCESS;
ganlikun 0:13413ea9a877 287 }
ganlikun 0:13413ea9a877 288
ganlikun 0:13413ea9a877 289 /* Activate the SPI mode (Reset I2SMOD bit in I2SCFGR register) */
ganlikun 0:13413ea9a877 290 CLEAR_BIT(SPIx->I2SCFGR, SPI_I2SCFGR_I2SMOD);
ganlikun 0:13413ea9a877 291 return status;
ganlikun 0:13413ea9a877 292 }
ganlikun 0:13413ea9a877 293
ganlikun 0:13413ea9a877 294 /**
ganlikun 0:13413ea9a877 295 * @brief Set each @ref LL_SPI_InitTypeDef field to default value.
ganlikun 0:13413ea9a877 296 * @param SPI_InitStruct pointer to a @ref LL_SPI_InitTypeDef structure
ganlikun 0:13413ea9a877 297 * whose fields will be set to default values.
ganlikun 0:13413ea9a877 298 * @retval None
ganlikun 0:13413ea9a877 299 */
ganlikun 0:13413ea9a877 300 void LL_SPI_StructInit(LL_SPI_InitTypeDef *SPI_InitStruct)
ganlikun 0:13413ea9a877 301 {
ganlikun 0:13413ea9a877 302 /* Set SPI_InitStruct fields to default values */
ganlikun 0:13413ea9a877 303 SPI_InitStruct->TransferDirection = LL_SPI_FULL_DUPLEX;
ganlikun 0:13413ea9a877 304 SPI_InitStruct->Mode = LL_SPI_MODE_SLAVE;
ganlikun 0:13413ea9a877 305 SPI_InitStruct->DataWidth = LL_SPI_DATAWIDTH_8BIT;
ganlikun 0:13413ea9a877 306 SPI_InitStruct->ClockPolarity = LL_SPI_POLARITY_LOW;
ganlikun 0:13413ea9a877 307 SPI_InitStruct->ClockPhase = LL_SPI_PHASE_1EDGE;
ganlikun 0:13413ea9a877 308 SPI_InitStruct->NSS = LL_SPI_NSS_HARD_INPUT;
ganlikun 0:13413ea9a877 309 SPI_InitStruct->BaudRate = LL_SPI_BAUDRATEPRESCALER_DIV2;
ganlikun 0:13413ea9a877 310 SPI_InitStruct->BitOrder = LL_SPI_MSB_FIRST;
ganlikun 0:13413ea9a877 311 SPI_InitStruct->CRCCalculation = LL_SPI_CRCCALCULATION_DISABLE;
ganlikun 0:13413ea9a877 312 SPI_InitStruct->CRCPoly = 7U;
ganlikun 0:13413ea9a877 313 }
ganlikun 0:13413ea9a877 314
ganlikun 0:13413ea9a877 315 /**
ganlikun 0:13413ea9a877 316 * @}
ganlikun 0:13413ea9a877 317 */
ganlikun 0:13413ea9a877 318
ganlikun 0:13413ea9a877 319 /**
ganlikun 0:13413ea9a877 320 * @}
ganlikun 0:13413ea9a877 321 */
ganlikun 0:13413ea9a877 322
ganlikun 0:13413ea9a877 323 /**
ganlikun 0:13413ea9a877 324 * @}
ganlikun 0:13413ea9a877 325 */
ganlikun 0:13413ea9a877 326
ganlikun 0:13413ea9a877 327 /** @addtogroup I2S_LL
ganlikun 0:13413ea9a877 328 * @{
ganlikun 0:13413ea9a877 329 */
ganlikun 0:13413ea9a877 330
ganlikun 0:13413ea9a877 331 /* Private types -------------------------------------------------------------*/
ganlikun 0:13413ea9a877 332 /* Private variables ---------------------------------------------------------*/
ganlikun 0:13413ea9a877 333 /* Private constants ---------------------------------------------------------*/
ganlikun 0:13413ea9a877 334 /** @defgroup I2S_LL_Private_Constants I2S Private Constants
ganlikun 0:13413ea9a877 335 * @{
ganlikun 0:13413ea9a877 336 */
ganlikun 0:13413ea9a877 337 /* I2S registers Masks */
ganlikun 0:13413ea9a877 338 #define I2S_I2SCFGR_CLEAR_MASK (SPI_I2SCFGR_CHLEN | SPI_I2SCFGR_DATLEN | \
ganlikun 0:13413ea9a877 339 SPI_I2SCFGR_CKPOL | SPI_I2SCFGR_I2SSTD | \
ganlikun 0:13413ea9a877 340 SPI_I2SCFGR_I2SCFG | SPI_I2SCFGR_I2SMOD )
ganlikun 0:13413ea9a877 341
ganlikun 0:13413ea9a877 342 #define I2S_I2SPR_CLEAR_MASK 0x0002U
ganlikun 0:13413ea9a877 343 /**
ganlikun 0:13413ea9a877 344 * @}
ganlikun 0:13413ea9a877 345 */
ganlikun 0:13413ea9a877 346 /* Private macros ------------------------------------------------------------*/
ganlikun 0:13413ea9a877 347 /** @defgroup I2S_LL_Private_Macros I2S Private Macros
ganlikun 0:13413ea9a877 348 * @{
ganlikun 0:13413ea9a877 349 */
ganlikun 0:13413ea9a877 350
ganlikun 0:13413ea9a877 351 #define IS_LL_I2S_DATAFORMAT(__VALUE__) (((__VALUE__) == LL_I2S_DATAFORMAT_16B) \
ganlikun 0:13413ea9a877 352 || ((__VALUE__) == LL_I2S_DATAFORMAT_16B_EXTENDED) \
ganlikun 0:13413ea9a877 353 || ((__VALUE__) == LL_I2S_DATAFORMAT_24B) \
ganlikun 0:13413ea9a877 354 || ((__VALUE__) == LL_I2S_DATAFORMAT_32B))
ganlikun 0:13413ea9a877 355
ganlikun 0:13413ea9a877 356 #define IS_LL_I2S_CPOL(__VALUE__) (((__VALUE__) == LL_I2S_POLARITY_LOW) \
ganlikun 0:13413ea9a877 357 || ((__VALUE__) == LL_I2S_POLARITY_HIGH))
ganlikun 0:13413ea9a877 358
ganlikun 0:13413ea9a877 359 #define IS_LL_I2S_STANDARD(__VALUE__) (((__VALUE__) == LL_I2S_STANDARD_PHILIPS) \
ganlikun 0:13413ea9a877 360 || ((__VALUE__) == LL_I2S_STANDARD_MSB) \
ganlikun 0:13413ea9a877 361 || ((__VALUE__) == LL_I2S_STANDARD_LSB) \
ganlikun 0:13413ea9a877 362 || ((__VALUE__) == LL_I2S_STANDARD_PCM_SHORT) \
ganlikun 0:13413ea9a877 363 || ((__VALUE__) == LL_I2S_STANDARD_PCM_LONG))
ganlikun 0:13413ea9a877 364
ganlikun 0:13413ea9a877 365 #define IS_LL_I2S_MODE(__VALUE__) (((__VALUE__) == LL_I2S_MODE_SLAVE_TX) \
ganlikun 0:13413ea9a877 366 || ((__VALUE__) == LL_I2S_MODE_SLAVE_RX) \
ganlikun 0:13413ea9a877 367 || ((__VALUE__) == LL_I2S_MODE_MASTER_TX) \
ganlikun 0:13413ea9a877 368 || ((__VALUE__) == LL_I2S_MODE_MASTER_RX))
ganlikun 0:13413ea9a877 369
ganlikun 0:13413ea9a877 370 #define IS_LL_I2S_MCLK_OUTPUT(__VALUE__) (((__VALUE__) == LL_I2S_MCLK_OUTPUT_ENABLE) \
ganlikun 0:13413ea9a877 371 || ((__VALUE__) == LL_I2S_MCLK_OUTPUT_DISABLE))
ganlikun 0:13413ea9a877 372
ganlikun 0:13413ea9a877 373 #define IS_LL_I2S_AUDIO_FREQ(__VALUE__) ((((__VALUE__) >= LL_I2S_AUDIOFREQ_8K) \
ganlikun 0:13413ea9a877 374 && ((__VALUE__) <= LL_I2S_AUDIOFREQ_192K)) \
ganlikun 0:13413ea9a877 375 || ((__VALUE__) == LL_I2S_AUDIOFREQ_DEFAULT))
ganlikun 0:13413ea9a877 376
ganlikun 0:13413ea9a877 377 #define IS_LL_I2S_PRESCALER_LINEAR(__VALUE__) ((__VALUE__) >= 0x2U)
ganlikun 0:13413ea9a877 378
ganlikun 0:13413ea9a877 379 #define IS_LL_I2S_PRESCALER_PARITY(__VALUE__) (((__VALUE__) == LL_I2S_PRESCALER_PARITY_EVEN) \
ganlikun 0:13413ea9a877 380 || ((__VALUE__) == LL_I2S_PRESCALER_PARITY_ODD))
ganlikun 0:13413ea9a877 381 /**
ganlikun 0:13413ea9a877 382 * @}
ganlikun 0:13413ea9a877 383 */
ganlikun 0:13413ea9a877 384
ganlikun 0:13413ea9a877 385 /* Private function prototypes -----------------------------------------------*/
ganlikun 0:13413ea9a877 386
ganlikun 0:13413ea9a877 387 /* Exported functions --------------------------------------------------------*/
ganlikun 0:13413ea9a877 388 /** @addtogroup I2S_LL_Exported_Functions
ganlikun 0:13413ea9a877 389 * @{
ganlikun 0:13413ea9a877 390 */
ganlikun 0:13413ea9a877 391
ganlikun 0:13413ea9a877 392 /** @addtogroup I2S_LL_EF_Init
ganlikun 0:13413ea9a877 393 * @{
ganlikun 0:13413ea9a877 394 */
ganlikun 0:13413ea9a877 395
ganlikun 0:13413ea9a877 396 /**
ganlikun 0:13413ea9a877 397 * @brief De-initialize the SPI/I2S registers to their default reset values.
ganlikun 0:13413ea9a877 398 * @param SPIx SPI Instance
ganlikun 0:13413ea9a877 399 * @retval An ErrorStatus enumeration value:
ganlikun 0:13413ea9a877 400 * - SUCCESS: SPI registers are de-initialized
ganlikun 0:13413ea9a877 401 * - ERROR: SPI registers are not de-initialized
ganlikun 0:13413ea9a877 402 */
ganlikun 0:13413ea9a877 403 ErrorStatus LL_I2S_DeInit(SPI_TypeDef *SPIx)
ganlikun 0:13413ea9a877 404 {
ganlikun 0:13413ea9a877 405 return LL_SPI_DeInit(SPIx);
ganlikun 0:13413ea9a877 406 }
ganlikun 0:13413ea9a877 407
ganlikun 0:13413ea9a877 408 /**
ganlikun 0:13413ea9a877 409 * @brief Initializes the SPI/I2S registers according to the specified parameters in I2S_InitStruct.
ganlikun 0:13413ea9a877 410 * @note As some bits in SPI configuration registers can only be written when the SPI is disabled (SPI_CR1_SPE bit =0),
ganlikun 0:13413ea9a877 411 * SPI IP should be in disabled state prior calling this function. Otherwise, ERROR result will be returned.
ganlikun 0:13413ea9a877 412 * @param SPIx SPI Instance
ganlikun 0:13413ea9a877 413 * @param I2S_InitStruct pointer to a @ref LL_I2S_InitTypeDef structure
ganlikun 0:13413ea9a877 414 * @retval An ErrorStatus enumeration value:
ganlikun 0:13413ea9a877 415 * - SUCCESS: SPI registers are Initialized
ganlikun 0:13413ea9a877 416 * - ERROR: SPI registers are not Initialized
ganlikun 0:13413ea9a877 417 */
ganlikun 0:13413ea9a877 418 ErrorStatus LL_I2S_Init(SPI_TypeDef *SPIx, LL_I2S_InitTypeDef *I2S_InitStruct)
ganlikun 0:13413ea9a877 419 {
ganlikun 0:13413ea9a877 420 uint16_t i2sdiv = 2U, i2sodd = 0U, packetlength = 1U;
ganlikun 0:13413ea9a877 421 uint32_t tmp = 0U;
ganlikun 0:13413ea9a877 422 uint32_t sourceclock = 0U;
ganlikun 0:13413ea9a877 423 ErrorStatus status = ERROR;
ganlikun 0:13413ea9a877 424
ganlikun 0:13413ea9a877 425 /* Check the I2S parameters */
ganlikun 0:13413ea9a877 426 assert_param(IS_I2S_ALL_INSTANCE(SPIx));
ganlikun 0:13413ea9a877 427 assert_param(IS_LL_I2S_MODE(I2S_InitStruct->Mode));
ganlikun 0:13413ea9a877 428 assert_param(IS_LL_I2S_STANDARD(I2S_InitStruct->Standard));
ganlikun 0:13413ea9a877 429 assert_param(IS_LL_I2S_DATAFORMAT(I2S_InitStruct->DataFormat));
ganlikun 0:13413ea9a877 430 assert_param(IS_LL_I2S_MCLK_OUTPUT(I2S_InitStruct->MCLKOutput));
ganlikun 0:13413ea9a877 431 assert_param(IS_LL_I2S_AUDIO_FREQ(I2S_InitStruct->AudioFreq));
ganlikun 0:13413ea9a877 432 assert_param(IS_LL_I2S_CPOL(I2S_InitStruct->ClockPolarity));
ganlikun 0:13413ea9a877 433
ganlikun 0:13413ea9a877 434 if (LL_I2S_IsEnabled(SPIx) == 0x00000000U)
ganlikun 0:13413ea9a877 435 {
ganlikun 0:13413ea9a877 436 /*---------------------------- SPIx I2SCFGR Configuration --------------------
ganlikun 0:13413ea9a877 437 * Configure SPIx I2SCFGR with parameters:
ganlikun 0:13413ea9a877 438 * - Mode: SPI_I2SCFGR_I2SCFG[1:0] bit
ganlikun 0:13413ea9a877 439 * - Standard: SPI_I2SCFGR_I2SSTD[1:0] and SPI_I2SCFGR_PCMSYNC bits
ganlikun 0:13413ea9a877 440 * - DataFormat: SPI_I2SCFGR_CHLEN and SPI_I2SCFGR_DATLEN bits
ganlikun 0:13413ea9a877 441 * - ClockPolarity: SPI_I2SCFGR_CKPOL bit
ganlikun 0:13413ea9a877 442 */
ganlikun 0:13413ea9a877 443
ganlikun 0:13413ea9a877 444 /* Write to SPIx I2SCFGR */
ganlikun 0:13413ea9a877 445 MODIFY_REG(SPIx->I2SCFGR,
ganlikun 0:13413ea9a877 446 I2S_I2SCFGR_CLEAR_MASK,
ganlikun 0:13413ea9a877 447 I2S_InitStruct->Mode | I2S_InitStruct->Standard |
ganlikun 0:13413ea9a877 448 I2S_InitStruct->DataFormat | I2S_InitStruct->ClockPolarity |
ganlikun 0:13413ea9a877 449 SPI_I2SCFGR_I2SMOD);
ganlikun 0:13413ea9a877 450
ganlikun 0:13413ea9a877 451 /*---------------------------- SPIx I2SPR Configuration ----------------------
ganlikun 0:13413ea9a877 452 * Configure SPIx I2SPR with parameters:
ganlikun 0:13413ea9a877 453 * - MCLKOutput: SPI_I2SPR_MCKOE bit
ganlikun 0:13413ea9a877 454 * - AudioFreq: SPI_I2SPR_I2SDIV[7:0] and SPI_I2SPR_ODD bits
ganlikun 0:13413ea9a877 455 */
ganlikun 0:13413ea9a877 456
ganlikun 0:13413ea9a877 457 /* If the requested audio frequency is not the default, compute the prescaler (i2sodd, i2sdiv)
ganlikun 0:13413ea9a877 458 * else, default values are used: i2sodd = 0U, i2sdiv = 2U.
ganlikun 0:13413ea9a877 459 */
ganlikun 0:13413ea9a877 460 if (I2S_InitStruct->AudioFreq != LL_I2S_AUDIOFREQ_DEFAULT)
ganlikun 0:13413ea9a877 461 {
ganlikun 0:13413ea9a877 462 /* Check the frame length (For the Prescaler computing)
ganlikun 0:13413ea9a877 463 * Default value: LL_I2S_DATAFORMAT_16B (packetlength = 1U).
ganlikun 0:13413ea9a877 464 */
ganlikun 0:13413ea9a877 465 if (I2S_InitStruct->DataFormat != LL_I2S_DATAFORMAT_16B)
ganlikun 0:13413ea9a877 466 {
ganlikun 0:13413ea9a877 467 /* Packet length is 32 bits */
ganlikun 0:13413ea9a877 468 packetlength = 2U;
ganlikun 0:13413ea9a877 469 }
ganlikun 0:13413ea9a877 470
ganlikun 0:13413ea9a877 471 /* If an external I2S clock has to be used, the specific define should be set
ganlikun 0:13413ea9a877 472 in the project configuration or in the stm32f4xx_ll_rcc.h file */
ganlikun 0:13413ea9a877 473 /* Get the I2S source clock value */
ganlikun 0:13413ea9a877 474 sourceclock = LL_RCC_GetI2SClockFreq(LL_RCC_I2S1_CLKSOURCE);
ganlikun 0:13413ea9a877 475
ganlikun 0:13413ea9a877 476 /* Compute the Real divider depending on the MCLK output state with a floating point */
ganlikun 0:13413ea9a877 477 if (I2S_InitStruct->MCLKOutput == LL_I2S_MCLK_OUTPUT_ENABLE)
ganlikun 0:13413ea9a877 478 {
ganlikun 0:13413ea9a877 479 /* MCLK output is enabled */
ganlikun 0:13413ea9a877 480 tmp = (uint16_t)(((((sourceclock / 256U) * 10U) / I2S_InitStruct->AudioFreq)) + 5U);
ganlikun 0:13413ea9a877 481 }
ganlikun 0:13413ea9a877 482 else
ganlikun 0:13413ea9a877 483 {
ganlikun 0:13413ea9a877 484 /* MCLK output is disabled */
ganlikun 0:13413ea9a877 485 tmp = (uint16_t)(((((sourceclock / (32U * packetlength)) * 10U) / I2S_InitStruct->AudioFreq)) + 5U);
ganlikun 0:13413ea9a877 486 }
ganlikun 0:13413ea9a877 487
ganlikun 0:13413ea9a877 488 /* Remove the floating point */
ganlikun 0:13413ea9a877 489 tmp = tmp / 10U;
ganlikun 0:13413ea9a877 490
ganlikun 0:13413ea9a877 491 /* Check the parity of the divider */
ganlikun 0:13413ea9a877 492 i2sodd = (uint16_t)(tmp & (uint16_t)0x0001U);
ganlikun 0:13413ea9a877 493
ganlikun 0:13413ea9a877 494 /* Compute the i2sdiv prescaler */
ganlikun 0:13413ea9a877 495 i2sdiv = (uint16_t)((tmp - i2sodd) / 2U);
ganlikun 0:13413ea9a877 496
ganlikun 0:13413ea9a877 497 /* Get the Mask for the Odd bit (SPI_I2SPR[8]) register */
ganlikun 0:13413ea9a877 498 i2sodd = (uint16_t)(i2sodd << 8U);
ganlikun 0:13413ea9a877 499 }
ganlikun 0:13413ea9a877 500
ganlikun 0:13413ea9a877 501 /* Test if the divider is 1 or 0 or greater than 0xFF */
ganlikun 0:13413ea9a877 502 if ((i2sdiv < 2U) || (i2sdiv > 0xFFU))
ganlikun 0:13413ea9a877 503 {
ganlikun 0:13413ea9a877 504 /* Set the default values */
ganlikun 0:13413ea9a877 505 i2sdiv = 2U;
ganlikun 0:13413ea9a877 506 i2sodd = 0U;
ganlikun 0:13413ea9a877 507 }
ganlikun 0:13413ea9a877 508
ganlikun 0:13413ea9a877 509 /* Write to SPIx I2SPR register the computed value */
ganlikun 0:13413ea9a877 510 WRITE_REG(SPIx->I2SPR, i2sdiv | i2sodd | I2S_InitStruct->MCLKOutput);
ganlikun 0:13413ea9a877 511
ganlikun 0:13413ea9a877 512 status = SUCCESS;
ganlikun 0:13413ea9a877 513 }
ganlikun 0:13413ea9a877 514 return status;
ganlikun 0:13413ea9a877 515 }
ganlikun 0:13413ea9a877 516
ganlikun 0:13413ea9a877 517 /**
ganlikun 0:13413ea9a877 518 * @brief Set each @ref LL_I2S_InitTypeDef field to default value.
ganlikun 0:13413ea9a877 519 * @param I2S_InitStruct pointer to a @ref LL_I2S_InitTypeDef structure
ganlikun 0:13413ea9a877 520 * whose fields will be set to default values.
ganlikun 0:13413ea9a877 521 * @retval None
ganlikun 0:13413ea9a877 522 */
ganlikun 0:13413ea9a877 523 void LL_I2S_StructInit(LL_I2S_InitTypeDef *I2S_InitStruct)
ganlikun 0:13413ea9a877 524 {
ganlikun 0:13413ea9a877 525 /*--------------- Reset I2S init structure parameters values -----------------*/
ganlikun 0:13413ea9a877 526 I2S_InitStruct->Mode = LL_I2S_MODE_SLAVE_TX;
ganlikun 0:13413ea9a877 527 I2S_InitStruct->Standard = LL_I2S_STANDARD_PHILIPS;
ganlikun 0:13413ea9a877 528 I2S_InitStruct->DataFormat = LL_I2S_DATAFORMAT_16B;
ganlikun 0:13413ea9a877 529 I2S_InitStruct->MCLKOutput = LL_I2S_MCLK_OUTPUT_DISABLE;
ganlikun 0:13413ea9a877 530 I2S_InitStruct->AudioFreq = LL_I2S_AUDIOFREQ_DEFAULT;
ganlikun 0:13413ea9a877 531 I2S_InitStruct->ClockPolarity = LL_I2S_POLARITY_LOW;
ganlikun 0:13413ea9a877 532 }
ganlikun 0:13413ea9a877 533
ganlikun 0:13413ea9a877 534 /**
ganlikun 0:13413ea9a877 535 * @brief Set linear and parity prescaler.
ganlikun 0:13413ea9a877 536 * @note To calculate value of PrescalerLinear(I2SDIV[7:0] bits) and PrescalerParity(ODD bit)\n
ganlikun 0:13413ea9a877 537 * Check Audio frequency table and formulas inside Reference Manual (SPI/I2S).
ganlikun 0:13413ea9a877 538 * @param SPIx SPI Instance
ganlikun 0:13413ea9a877 539 * @param PrescalerLinear value: Min_Data=0x02 and Max_Data=0xFF.
ganlikun 0:13413ea9a877 540 * @param PrescalerParity This parameter can be one of the following values:
ganlikun 0:13413ea9a877 541 * @arg @ref LL_I2S_PRESCALER_PARITY_EVEN
ganlikun 0:13413ea9a877 542 * @arg @ref LL_I2S_PRESCALER_PARITY_ODD
ganlikun 0:13413ea9a877 543 * @retval None
ganlikun 0:13413ea9a877 544 */
ganlikun 0:13413ea9a877 545 void LL_I2S_ConfigPrescaler(SPI_TypeDef *SPIx, uint32_t PrescalerLinear, uint32_t PrescalerParity)
ganlikun 0:13413ea9a877 546 {
ganlikun 0:13413ea9a877 547 /* Check the I2S parameters */
ganlikun 0:13413ea9a877 548 assert_param(IS_I2S_ALL_INSTANCE(SPIx));
ganlikun 0:13413ea9a877 549 assert_param(IS_LL_I2S_PRESCALER_LINEAR(PrescalerLinear));
ganlikun 0:13413ea9a877 550 assert_param(IS_LL_I2S_PRESCALER_PARITY(PrescalerParity));
ganlikun 0:13413ea9a877 551
ganlikun 0:13413ea9a877 552 /* Write to SPIx I2SPR */
ganlikun 0:13413ea9a877 553 MODIFY_REG(SPIx->I2SPR, SPI_I2SPR_I2SDIV | SPI_I2SPR_ODD, PrescalerLinear | (PrescalerParity << 8U));
ganlikun 0:13413ea9a877 554 }
ganlikun 0:13413ea9a877 555
ganlikun 0:13413ea9a877 556 #if defined (SPI_I2S_FULLDUPLEX_SUPPORT)
ganlikun 0:13413ea9a877 557 /**
ganlikun 0:13413ea9a877 558 * @brief Configures the full duplex mode for the I2Sx peripheral using its extension
ganlikun 0:13413ea9a877 559 * I2Sxext according to the specified parameters in the I2S_InitStruct.
ganlikun 0:13413ea9a877 560 * @note The structure pointed by I2S_InitStruct parameter should be the same
ganlikun 0:13413ea9a877 561 * used for the master I2S peripheral. In this case, if the master is
ganlikun 0:13413ea9a877 562 * configured as transmitter, the slave will be receiver and vice versa.
ganlikun 0:13413ea9a877 563 * Or you can force a different mode by modifying the field I2S_Mode to the
ganlikun 0:13413ea9a877 564 * value I2S_SlaveRx or I2S_SlaveTx independently of the master configuration.
ganlikun 0:13413ea9a877 565 * @param I2Sxext SPI Instance
ganlikun 0:13413ea9a877 566 * @param I2S_InitStruct pointer to a @ref LL_I2S_InitTypeDef structure
ganlikun 0:13413ea9a877 567 * @retval An ErrorStatus enumeration value:
ganlikun 0:13413ea9a877 568 * - SUCCESS: I2Sxext registers are Initialized
ganlikun 0:13413ea9a877 569 * - ERROR: I2Sxext registers are not Initialized
ganlikun 0:13413ea9a877 570 */
ganlikun 0:13413ea9a877 571 ErrorStatus LL_I2S_InitFullDuplex(SPI_TypeDef *I2Sxext, LL_I2S_InitTypeDef *I2S_InitStruct)
ganlikun 0:13413ea9a877 572 {
ganlikun 0:13413ea9a877 573 uint16_t mode = 0U;
ganlikun 0:13413ea9a877 574 ErrorStatus status = ERROR;
ganlikun 0:13413ea9a877 575
ganlikun 0:13413ea9a877 576 /* Check the I2S parameters */
ganlikun 0:13413ea9a877 577 assert_param(IS_I2S_EXT_ALL_INSTANCE(I2Sxext));
ganlikun 0:13413ea9a877 578 assert_param(IS_LL_I2S_MODE(I2S_InitStruct->Mode));
ganlikun 0:13413ea9a877 579 assert_param(IS_LL_I2S_STANDARD(I2S_InitStruct->Standard));
ganlikun 0:13413ea9a877 580 assert_param(IS_LL_I2S_DATAFORMAT(I2S_InitStruct->DataFormat));
ganlikun 0:13413ea9a877 581 assert_param(IS_LL_I2S_CPOL(I2S_InitStruct->ClockPolarity));
ganlikun 0:13413ea9a877 582
ganlikun 0:13413ea9a877 583 if (LL_I2S_IsEnabled(I2Sxext) == 0x00000000U)
ganlikun 0:13413ea9a877 584 {
ganlikun 0:13413ea9a877 585 /*---------------------------- SPIx I2SCFGR Configuration --------------------
ganlikun 0:13413ea9a877 586 * Configure SPIx I2SCFGR with parameters:
ganlikun 0:13413ea9a877 587 * - Mode: SPI_I2SCFGR_I2SCFG[1:0] bit
ganlikun 0:13413ea9a877 588 * - Standard: SPI_I2SCFGR_I2SSTD[1:0] and SPI_I2SCFGR_PCMSYNC bits
ganlikun 0:13413ea9a877 589 * - DataFormat: SPI_I2SCFGR_CHLEN and SPI_I2SCFGR_DATLEN bits
ganlikun 0:13413ea9a877 590 * - ClockPolarity: SPI_I2SCFGR_CKPOL bit
ganlikun 0:13413ea9a877 591 */
ganlikun 0:13413ea9a877 592
ganlikun 0:13413ea9a877 593 /* Reset I2SPR registers */
ganlikun 0:13413ea9a877 594 WRITE_REG(I2Sxext->I2SPR, I2S_I2SPR_CLEAR_MASK);
ganlikun 0:13413ea9a877 595
ganlikun 0:13413ea9a877 596 /* Get the mode to be configured for the extended I2S */
ganlikun 0:13413ea9a877 597 if ((I2S_InitStruct->Mode == LL_I2S_MODE_MASTER_TX) || (I2S_InitStruct->Mode == LL_I2S_MODE_SLAVE_TX))
ganlikun 0:13413ea9a877 598 {
ganlikun 0:13413ea9a877 599 mode = LL_I2S_MODE_SLAVE_RX;
ganlikun 0:13413ea9a877 600 }
ganlikun 0:13413ea9a877 601 else
ganlikun 0:13413ea9a877 602 {
ganlikun 0:13413ea9a877 603 if ((I2S_InitStruct->Mode == LL_I2S_MODE_MASTER_RX) || (I2S_InitStruct->Mode == LL_I2S_MODE_SLAVE_RX))
ganlikun 0:13413ea9a877 604 {
ganlikun 0:13413ea9a877 605 mode = LL_I2S_MODE_SLAVE_TX;
ganlikun 0:13413ea9a877 606 }
ganlikun 0:13413ea9a877 607 }
ganlikun 0:13413ea9a877 608
ganlikun 0:13413ea9a877 609 /* Write to SPIx I2SCFGR */
ganlikun 0:13413ea9a877 610 MODIFY_REG(I2Sxext->I2SCFGR,
ganlikun 0:13413ea9a877 611 I2S_I2SCFGR_CLEAR_MASK,
ganlikun 0:13413ea9a877 612 I2S_InitStruct->Standard |
ganlikun 0:13413ea9a877 613 I2S_InitStruct->DataFormat | I2S_InitStruct->ClockPolarity |
ganlikun 0:13413ea9a877 614 SPI_I2SCFGR_I2SMOD | mode);
ganlikun 0:13413ea9a877 615
ganlikun 0:13413ea9a877 616 status = SUCCESS;
ganlikun 0:13413ea9a877 617 }
ganlikun 0:13413ea9a877 618 return status;
ganlikun 0:13413ea9a877 619 }
ganlikun 0:13413ea9a877 620 #endif /* SPI_I2S_FULLDUPLEX_SUPPORT */
ganlikun 0:13413ea9a877 621
ganlikun 0:13413ea9a877 622 /**
ganlikun 0:13413ea9a877 623 * @}
ganlikun 0:13413ea9a877 624 */
ganlikun 0:13413ea9a877 625
ganlikun 0:13413ea9a877 626 /**
ganlikun 0:13413ea9a877 627 * @}
ganlikun 0:13413ea9a877 628 */
ganlikun 0:13413ea9a877 629
ganlikun 0:13413ea9a877 630 /**
ganlikun 0:13413ea9a877 631 * @}
ganlikun 0:13413ea9a877 632 */
ganlikun 0:13413ea9a877 633
ganlikun 0:13413ea9a877 634 #endif /* defined (SPI1) || defined (SPI2) || defined (SPI3) || defined (SPI4) || defined (SPI5) || defined(SPI6) */
ganlikun 0:13413ea9a877 635
ganlikun 0:13413ea9a877 636 /**
ganlikun 0:13413ea9a877 637 * @}
ganlikun 0:13413ea9a877 638 */
ganlikun 0:13413ea9a877 639
ganlikun 0:13413ea9a877 640 #endif /* USE_FULL_LL_DRIVER */
ganlikun 0:13413ea9a877 641
ganlikun 0:13413ea9a877 642 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
ganlikun 0:13413ea9a877 643