Subversion Repositories LedShow

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

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