OpenCores
URL https://opencores.org/ocsvn/openrisc_2011-10-31/openrisc_2011-10-31/trunk

Subversion Repositories openrisc_2011-10-31

[/] [openrisc/] [trunk/] [rtos/] [freertos-6.1.1/] [Demo/] [Common/] [drivers/] [ST/] [STM32F10xFWLib/] [src/] [stm32f10x_spi.c] - Blame information for rev 637

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 608 jeremybenn
/**
2
  ******************************************************************************
3
  * @file  stm32f10x_spi.c
4
  * @author  MCD Application Team
5
  * @version  V3.0.0
6
  * @date  04/06/2009
7
  * @brief  This file provides all the SPI firmware functions.
8
  ******************************************************************************
9
  * @copy
10
  *
11
  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
12
  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
13
  * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
14
  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
15
  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
16
  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
17
  *
18
  * <h2><center>&copy; COPYRIGHT 2009 STMicroelectronics</center></h2>
19
  */
20
 
21
/* Includes ------------------------------------------------------------------*/
22
#include "stm32f10x_spi.h"
23
#include "stm32f10x_rcc.h"
24
 
25
/** @addtogroup StdPeriph_Driver
26
  * @{
27
  */
28
 
29
/** @defgroup SPI
30
  * @brief SPI driver modules
31
  * @{
32
  */
33
 
34
/** @defgroup SPI_Private_TypesDefinitions
35
  * @{
36
  */
37
 
38
/**
39
  * @}
40
  */
41
 
42
 
43
/** @defgroup SPI_Private_Defines
44
  * @{
45
  */
46
 
47
/* SPI SPE mask */
48
#define CR1_SPE_Set          ((uint16_t)0x0040)
49
#define CR1_SPE_Reset        ((uint16_t)0xFFBF)
50
 
51
/* I2S I2SE mask */
52
#define I2SCFGR_I2SE_Set     ((uint16_t)0x0400)
53
#define I2SCFGR_I2SE_Reset   ((uint16_t)0xFBFF)
54
 
55
/* SPI CRCNext mask */
56
#define CR1_CRCNext_Set      ((uint16_t)0x1000)
57
 
58
/* SPI CRCEN mask */
59
#define CR1_CRCEN_Set        ((uint16_t)0x2000)
60
#define CR1_CRCEN_Reset      ((uint16_t)0xDFFF)
61
 
62
/* SPI SSOE mask */
63
#define CR2_SSOE_Set         ((uint16_t)0x0004)
64
#define CR2_SSOE_Reset       ((uint16_t)0xFFFB)
65
 
66
/* SPI registers Masks */
67
#define CR1_CLEAR_Mask       ((uint16_t)0x3040)
68
#define I2SCFGR_CLEAR_Mask   ((uint16_t)0xF040)
69
 
70
/* SPI or I2S mode selection masks */
71
#define SPI_Mode_Select      ((uint16_t)0xF7FF)
72
#define I2S_Mode_Select      ((uint16_t)0x0800) 
73
 
74
/**
75
  * @}
76
  */
77
 
78
/** @defgroup SPI_Private_Macros
79
  * @{
80
  */
81
 
82
/**
83
  * @}
84
  */
85
 
86
/** @defgroup SPI_Private_Variables
87
  * @{
88
  */
89
 
90
/**
91
  * @}
92
  */
93
 
94
/** @defgroup SPI_Private_FunctionPrototypes
95
  * @{
96
  */
97
 
98
/**
99
  * @}
100
  */
101
 
102
/** @defgroup SPI_Private_Functions
103
  * @{
104
  */
105
 
106
/**
107
  * @brief  Deinitializes the SPIx peripheral registers to their default
108
  *   reset values (Affects also the I2Ss).
109
  * @param SPIx: where x can be 1, 2 or 3 to select the SPI peripheral.
110
  * @retval : None
111
  */
112
void SPI_I2S_DeInit(SPI_TypeDef* SPIx)
113
{
114
  /* Check the parameters */
115
  assert_param(IS_SPI_ALL_PERIPH(SPIx));
116
 
117
  switch (*(uint32_t*)&SPIx)
118
  {
119
    case SPI1_BASE:
120
      /* Enable SPI1 reset state */
121
      RCC_APB2PeriphResetCmd(RCC_APB2Periph_SPI1, ENABLE);
122
      /* Release SPI1 from reset state */
123
      RCC_APB2PeriphResetCmd(RCC_APB2Periph_SPI1, DISABLE);
124
      break;
125
    case SPI2_BASE:
126
      /* Enable SPI2 reset state */
127
      RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI2, ENABLE);
128
      /* Release SPI2 from reset state */
129
      RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI2, DISABLE);
130
      break;
131
    case SPI3_BASE:
132
      /* Enable SPI3 reset state */
133
      RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI3, ENABLE);
134
      /* Release SPI3 from reset state */
135
      RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI3, DISABLE);
136
      break;
137
    default:
138
      break;
139
  }
140
}
141
 
142
/**
143
  * @brief  Initializes the SPIx peripheral according to the specified
144
  *   parameters in the SPI_InitStruct.
145
  * @param SPIx: where x can be 1, 2 or 3 to select the SPI peripheral.
146
  * @param SPI_InitStruct: pointer to a SPI_InitTypeDef structure that
147
  *   contains the configuration information for the specified
148
  *   SPI peripheral.
149
  * @retval : None
150
  */
151
void SPI_Init(SPI_TypeDef* SPIx, SPI_InitTypeDef* SPI_InitStruct)
152
{
153
  uint16_t tmpreg = 0;
154
 
155
  /* check the parameters */
156
  assert_param(IS_SPI_ALL_PERIPH(SPIx));
157
 
158
  /* Check the SPI parameters */
159
  assert_param(IS_SPI_DIRECTION_MODE(SPI_InitStruct->SPI_Direction));
160
  assert_param(IS_SPI_MODE(SPI_InitStruct->SPI_Mode));
161
  assert_param(IS_SPI_DATASIZE(SPI_InitStruct->SPI_DataSize));
162
  assert_param(IS_SPI_CPOL(SPI_InitStruct->SPI_CPOL));
163
  assert_param(IS_SPI_CPHA(SPI_InitStruct->SPI_CPHA));
164
  assert_param(IS_SPI_NSS(SPI_InitStruct->SPI_NSS));
165
  assert_param(IS_SPI_BAUDRATE_PRESCALER(SPI_InitStruct->SPI_BaudRatePrescaler));
166
  assert_param(IS_SPI_FIRST_BIT(SPI_InitStruct->SPI_FirstBit));
167
  assert_param(IS_SPI_CRC_POLYNOMIAL(SPI_InitStruct->SPI_CRCPolynomial));
168
/*---------------------------- SPIx CR1 Configuration ------------------------*/
169
  /* Get the SPIx CR1 value */
170
  tmpreg = SPIx->CR1;
171
  /* Clear BIDIMode, BIDIOE, RxONLY, SSM, SSI, LSBFirst, BR, MSTR, CPOL and CPHA bits */
172
  tmpreg &= CR1_CLEAR_Mask;
173
  /* Configure SPIx: direction, NSS management, first transmitted bit, BaudRate prescaler
174
     master/salve mode, CPOL and CPHA */
175
  /* Set BIDImode, BIDIOE and RxONLY bits according to SPI_Direction value */
176
  /* Set SSM, SSI and MSTR bits according to SPI_Mode and SPI_NSS values */
177
  /* Set LSBFirst bit according to SPI_FirstBit value */
178
  /* Set BR bits according to SPI_BaudRatePrescaler value */
179
  /* Set CPOL bit according to SPI_CPOL value */
180
  /* Set CPHA bit according to SPI_CPHA value */
181
  tmpreg |= (uint16_t)((uint32_t)SPI_InitStruct->SPI_Direction | SPI_InitStruct->SPI_Mode |
182
                  SPI_InitStruct->SPI_DataSize | SPI_InitStruct->SPI_CPOL |
183
                  SPI_InitStruct->SPI_CPHA | SPI_InitStruct->SPI_NSS |
184
                  SPI_InitStruct->SPI_BaudRatePrescaler | SPI_InitStruct->SPI_FirstBit);
185
  /* Write to SPIx CR1 */
186
  SPIx->CR1 = tmpreg;
187
 
188
  /* Activate the SPI mode (Reset I2SMOD bit in I2SCFGR register) */
189
  SPIx->I2SCFGR &= SPI_Mode_Select;
190
/*---------------------------- SPIx CRCPOLY Configuration --------------------*/
191
  /* Write to SPIx CRCPOLY */
192
  SPIx->CRCPR = SPI_InitStruct->SPI_CRCPolynomial;
193
}
194
 
195
/**
196
  * @brief  Initializes the SPIx peripheral according to the specified
197
  *   parameters in the I2S_InitStruct.
198
  * @param SPIx: where x can be  2 or 3 to select the SPI peripheral
199
  *   (configured in I2S mode).
200
  * @param I2S_InitStruct: pointer to an I2S_InitTypeDef structure that
201
  *   contains the configuration information for the specified
202
  *   SPI peripheral configured in I2S mode.
203
  * @retval : None
204
  */
205
void I2S_Init(SPI_TypeDef* SPIx, I2S_InitTypeDef* I2S_InitStruct)
206
{
207
  uint16_t tmpreg = 0, i2sdiv = 2, i2sodd = 0, packetlength = 1;
208
  uint32_t tmp = 0;
209
  RCC_ClocksTypeDef RCC_Clocks;
210
 
211
  /* Check the I2S parameters */
212
  assert_param(IS_SPI_23_PERIPH(SPIx));
213
  assert_param(IS_I2S_MODE(I2S_InitStruct->I2S_Mode));
214
  assert_param(IS_I2S_STANDARD(I2S_InitStruct->I2S_Standard));
215
  assert_param(IS_I2S_DATA_FORMAT(I2S_InitStruct->I2S_DataFormat));
216
  assert_param(IS_I2S_MCLK_OUTPUT(I2S_InitStruct->I2S_MCLKOutput));
217
  assert_param(IS_I2S_AUDIO_FREQ(I2S_InitStruct->I2S_AudioFreq));
218
  assert_param(IS_I2S_CPOL(I2S_InitStruct->I2S_CPOL));
219
/*----------------------- SPIx I2SCFGR & I2SPR Configuration -----------------*/
220
  /* Clear I2SMOD, I2SE, I2SCFG, PCMSYNC, I2SSTD, CKPOL, DATLEN and CHLEN bits */
221
  SPIx->I2SCFGR &= I2SCFGR_CLEAR_Mask;
222
  SPIx->I2SPR = 0x0002;
223
 
224
  /* Get the I2SCFGR register value */
225
  tmpreg = SPIx->I2SCFGR;
226
 
227
  /* If the default value has to be written, reinitialize i2sdiv and i2sodd*/
228
  if(I2S_InitStruct->I2S_AudioFreq == I2S_AudioFreq_Default)
229
  {
230
    i2sodd = (uint16_t)0;
231
    i2sdiv = (uint16_t)2;
232
  }
233
  /* If the requested audio frequency is not the default, compute the prescaler */
234
  else
235
  {
236
    /* Check the frame length (For the Prescaler computing) */
237
    if(I2S_InitStruct->I2S_DataFormat == I2S_DataFormat_16b)
238
    {
239
      /* Packet length is 16 bits */
240
      packetlength = 1;
241
    }
242
    else
243
    {
244
      /* Packet length is 32 bits */
245
      packetlength = 2;
246
    }
247
    /* Get System Clock frequency */
248
    RCC_GetClocksFreq(&RCC_Clocks);
249
 
250
    /* Compute the Real divider depending on the MCLK output state with a flaoting point */
251
    if(I2S_InitStruct->I2S_MCLKOutput == I2S_MCLKOutput_Enable)
252
    {
253
      /* MCLK output is enabled */
254
      tmp = (uint16_t)(((10 * RCC_Clocks.SYSCLK_Frequency) / (256 * I2S_InitStruct->I2S_AudioFreq)) + 5);
255
    }
256
    else
257
    {
258
      /* MCLK output is disabled */
259
      tmp = (uint16_t)(((10 * RCC_Clocks.SYSCLK_Frequency) / (32 * packetlength * I2S_InitStruct->I2S_AudioFreq)) + 5);
260
    }
261
 
262
    /* Remove the flaoting point */
263
    tmp = tmp/10;
264
 
265
    /* Check the parity of the divider */
266
    i2sodd = (uint16_t)(tmp & (uint16_t)0x0001);
267
 
268
    /* Compute the i2sdiv prescaler */
269
    i2sdiv = (uint16_t)((tmp - i2sodd) / 2);
270
 
271
    /* Get the Mask for the Odd bit (SPI_I2SPR[8]) register */
272
    i2sodd = (uint16_t) (i2sodd << 8);
273
  }
274
 
275
  /* Test if the divider is 1 or 0 */
276
  if ((i2sdiv < 2) || (i2sdiv > 0xFF))
277
  {
278
    /* Set the default values */
279
    i2sdiv = 2;
280
    i2sodd = 0;
281
  }
282
  /* Write to SPIx I2SPR register the computed value */
283
  SPIx->I2SPR = (uint16_t)(i2sdiv | i2sodd | I2S_InitStruct->I2S_MCLKOutput);
284
 
285
  /* Configure the I2S with the SPI_InitStruct values */
286
  tmpreg |= (uint16_t)(I2S_Mode_Select | I2S_InitStruct->I2S_Mode | \
287
                  I2S_InitStruct->I2S_Standard | I2S_InitStruct->I2S_DataFormat | \
288
                  I2S_InitStruct->I2S_CPOL);
289
 
290
  /* Write to SPIx I2SCFGR */
291
  SPIx->I2SCFGR = tmpreg;
292
}
293
 
294
/**
295
  * @brief  Fills each SPI_InitStruct member with its default value.
296
  * @param SPI_InitStruct : pointer to a SPI_InitTypeDef structure
297
  *   which will be initialized.
298
  * @retval : None
299
  */
300
void SPI_StructInit(SPI_InitTypeDef* SPI_InitStruct)
301
{
302
/*--------------- Reset SPI init structure parameters values -----------------*/
303
  /* Initialize the SPI_Direction member */
304
  SPI_InitStruct->SPI_Direction = SPI_Direction_2Lines_FullDuplex;
305
  /* initialize the SPI_Mode member */
306
  SPI_InitStruct->SPI_Mode = SPI_Mode_Slave;
307
  /* initialize the SPI_DataSize member */
308
  SPI_InitStruct->SPI_DataSize = SPI_DataSize_8b;
309
  /* Initialize the SPI_CPOL member */
310
  SPI_InitStruct->SPI_CPOL = SPI_CPOL_Low;
311
  /* Initialize the SPI_CPHA member */
312
  SPI_InitStruct->SPI_CPHA = SPI_CPHA_1Edge;
313
  /* Initialize the SPI_NSS member */
314
  SPI_InitStruct->SPI_NSS = SPI_NSS_Hard;
315
  /* Initialize the SPI_BaudRatePrescaler member */
316
  SPI_InitStruct->SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
317
  /* Initialize the SPI_FirstBit member */
318
  SPI_InitStruct->SPI_FirstBit = SPI_FirstBit_MSB;
319
  /* Initialize the SPI_CRCPolynomial member */
320
  SPI_InitStruct->SPI_CRCPolynomial = 7;
321
}
322
 
323
/**
324
  * @brief  Fills each I2S_InitStruct member with its default value.
325
  * @param I2S_InitStruct : pointer to a I2S_InitTypeDef structure
326
  *   which will be initialized.
327
  * @retval : None
328
  */
329
void I2S_StructInit(I2S_InitTypeDef* I2S_InitStruct)
330
{
331
/*--------------- Reset I2S init structure parameters values -----------------*/
332
  /* Initialize the I2S_Mode member */
333
  I2S_InitStruct->I2S_Mode = I2S_Mode_SlaveTx;
334
 
335
  /* Initialize the I2S_Standard member */
336
  I2S_InitStruct->I2S_Standard = I2S_Standard_Phillips;
337
 
338
  /* Initialize the I2S_DataFormat member */
339
  I2S_InitStruct->I2S_DataFormat = I2S_DataFormat_16b;
340
 
341
  /* Initialize the I2S_MCLKOutput member */
342
  I2S_InitStruct->I2S_MCLKOutput = I2S_MCLKOutput_Disable;
343
 
344
  /* Initialize the I2S_AudioFreq member */
345
  I2S_InitStruct->I2S_AudioFreq = I2S_AudioFreq_Default;
346
 
347
  /* Initialize the I2S_CPOL member */
348
  I2S_InitStruct->I2S_CPOL = I2S_CPOL_Low;
349
}
350
 
351
/**
352
  * @brief  Enables or disables the specified SPI peripheral.
353
  * @param SPIx: where x can be 1, 2 or 3 to select the SPI peripheral.
354
  * @param NewState: new state of the SPIx peripheral.
355
  *   This parameter can be: ENABLE or DISABLE.
356
  * @retval : None
357
  */
358
void SPI_Cmd(SPI_TypeDef* SPIx, FunctionalState NewState)
359
{
360
  /* Check the parameters */
361
  assert_param(IS_SPI_ALL_PERIPH(SPIx));
362
  assert_param(IS_FUNCTIONAL_STATE(NewState));
363
  if (NewState != DISABLE)
364
  {
365
    /* Enable the selected SPI peripheral */
366
    SPIx->CR1 |= CR1_SPE_Set;
367
  }
368
  else
369
  {
370
    /* Disable the selected SPI peripheral */
371
    SPIx->CR1 &= CR1_SPE_Reset;
372
  }
373
}
374
 
375
/**
376
  * @brief  Enables or disables the specified SPI peripheral (in I2S mode).
377
  * @param SPIx: where x can be 2 or 3 to select the SPI peripheral.
378
  * @param NewState: new state of the SPIx peripheral.
379
  *   This parameter can be: ENABLE or DISABLE.
380
  * @retval : None
381
  */
382
void I2S_Cmd(SPI_TypeDef* SPIx, FunctionalState NewState)
383
{
384
  /* Check the parameters */
385
  assert_param(IS_SPI_23_PERIPH(SPIx));
386
  assert_param(IS_FUNCTIONAL_STATE(NewState));
387
  if (NewState != DISABLE)
388
  {
389
    /* Enable the selected SPI peripheral (in I2S mode) */
390
    SPIx->I2SCFGR |= I2SCFGR_I2SE_Set;
391
  }
392
  else
393
  {
394
    /* Disable the selected SPI peripheral (in I2S mode) */
395
    SPIx->I2SCFGR &= I2SCFGR_I2SE_Reset;
396
  }
397
}
398
 
399
/**
400
  * @brief  Enables or disables the specified SPI/I2S interrupts.
401
  * @param SPIx: where x can be :
402
  *   1, 2 or 3 in SPI mode
403
  *   2 or 3 in I2S mode
404
  * @param SPI_I2S_IT: specifies the SPI/I2S interrupt source to be
405
  *   enabled or disabled.
406
  *   This parameter can be one of the following values:
407
  * @arg SPI_I2S_IT_TXE: Tx buffer empty interrupt mask
408
  * @arg SPI_I2S_IT_RXNE: Rx buffer not empty interrupt mask
409
  * @arg SPI_I2S_IT_ERR: Error interrupt mask
410
  * @param NewState: new state of the specified SPI/I2S interrupt.
411
  *   This parameter can be: ENABLE or DISABLE.
412
  * @retval : None
413
  */
414
void SPI_I2S_ITConfig(SPI_TypeDef* SPIx, uint8_t SPI_I2S_IT, FunctionalState NewState)
415
{
416
  uint16_t itpos = 0, itmask = 0 ;
417
  /* Check the parameters */
418
  assert_param(IS_SPI_ALL_PERIPH(SPIx));
419
  assert_param(IS_FUNCTIONAL_STATE(NewState));
420
  assert_param(IS_SPI_I2S_CONFIG_IT(SPI_I2S_IT));
421
  /* Get the SPI/I2S IT index */
422
  itpos = SPI_I2S_IT >> 4;
423
  /* Set the IT mask */
424
  itmask = (uint16_t)((uint16_t)1 << itpos);
425
  if (NewState != DISABLE)
426
  {
427
    /* Enable the selected SPI/I2S interrupt */
428
    SPIx->CR2 |= itmask;
429
  }
430
  else
431
  {
432
    /* Disable the selected SPI/I2S interrupt */
433
    SPIx->CR2 &= (uint16_t)~itmask;
434
  }
435
}
436
 
437
/**
438
  * @brief  Enables or disables the SPIx/I2Sx DMA interface.
439
  * @param SPIx: where x can be :
440
  *   1, 2 or 3 in SPI mode
441
  *   2 or 3 in I2S mode
442
  * @param SPI_I2S_DMAReq: specifies the SPI/I2S DMA transfer request
443
  *   to be enabled or disabled.
444
  *   This parameter can be any combination of the following values:
445
  * @arg SPI_I2S_DMAReq_Tx: Tx buffer DMA transfer request
446
  * @arg SPI_I2S_DMAReq_Rx: Rx buffer DMA transfer request
447
  * @param NewState: new state of the selected SPI/I2S DMA transfer
448
  *   request.
449
  *   This parameter can be: ENABLE or DISABLE.
450
  * @retval : None
451
  */
452
void SPI_I2S_DMACmd(SPI_TypeDef* SPIx, uint16_t SPI_I2S_DMAReq, FunctionalState NewState)
453
{
454
  /* Check the parameters */
455
  assert_param(IS_SPI_ALL_PERIPH(SPIx));
456
  assert_param(IS_FUNCTIONAL_STATE(NewState));
457
  assert_param(IS_SPI_I2S_DMAREQ(SPI_I2S_DMAReq));
458
  if (NewState != DISABLE)
459
  {
460
    /* Enable the selected SPI/I2S DMA requests */
461
    SPIx->CR2 |= SPI_I2S_DMAReq;
462
  }
463
  else
464
  {
465
    /* Disable the selected SPI/I2S DMA requests */
466
    SPIx->CR2 &= (uint16_t)~SPI_I2S_DMAReq;
467
  }
468
}
469
 
470
/**
471
  * @brief  Transmits a Data through the SPIx/I2Sx peripheral.
472
  * @param SPIx: where x can be :
473
  *   1, 2 or 3 in SPI mode
474
  *   2 or 3 in I2S mode
475
  * @param Data : Data to be transmitted..
476
  * @retval : None
477
  */
478
void SPI_I2S_SendData(SPI_TypeDef* SPIx, uint16_t Data)
479
{
480
  /* Check the parameters */
481
  assert_param(IS_SPI_ALL_PERIPH(SPIx));
482
 
483
  /* Write in the DR register the data to be sent */
484
  SPIx->DR = Data;
485
}
486
 
487
/**
488
  * @brief  Returns the most recent received data by the SPIx/I2Sx peripheral.
489
  * @param SPIx: where x can be :
490
  *   1, 2 or 3 in SPI mode
491
  *   2 or 3 in I2S mode
492
  * @retval : The value of the received data.
493
  */
494
uint16_t SPI_I2S_ReceiveData(SPI_TypeDef* SPIx)
495
{
496
  /* Check the parameters */
497
  assert_param(IS_SPI_ALL_PERIPH(SPIx));
498
 
499
  /* Return the data in the DR register */
500
  return SPIx->DR;
501
}
502
 
503
/**
504
  * @brief  Configures internally by software the NSS pin for the selected
505
  *   SPI.
506
  * @param SPIx: where x can be 1, 2 or 3 to select the SPI peripheral.
507
  * @param SPI_NSSInternalSoft: specifies the SPI NSS internal state.
508
  *   This parameter can be one of the following values:
509
  * @arg SPI_NSSInternalSoft_Set: Set NSS pin internally
510
  * @arg SPI_NSSInternalSoft_Reset: Reset NSS pin internally
511
  * @retval : None
512
  */
513
void SPI_NSSInternalSoftwareConfig(SPI_TypeDef* SPIx, uint16_t SPI_NSSInternalSoft)
514
{
515
  /* Check the parameters */
516
  assert_param(IS_SPI_ALL_PERIPH(SPIx));
517
  assert_param(IS_SPI_NSS_INTERNAL(SPI_NSSInternalSoft));
518
  if (SPI_NSSInternalSoft != SPI_NSSInternalSoft_Reset)
519
  {
520
    /* Set NSS pin internally by software */
521
    SPIx->CR1 |= SPI_NSSInternalSoft_Set;
522
  }
523
  else
524
  {
525
    /* Reset NSS pin internally by software */
526
    SPIx->CR1 &= SPI_NSSInternalSoft_Reset;
527
  }
528
}
529
 
530
/**
531
  * @brief  Enables or disables the SS output for the selected SPI.
532
  * @param SPIx: where x can be 1, 2 or 3 to select the SPI peripheral.
533
  * @param NewState: new state of the SPIx SS output.
534
  *   This parameter can be: ENABLE or DISABLE.
535
  * @retval : None
536
  */
537
void SPI_SSOutputCmd(SPI_TypeDef* SPIx, FunctionalState NewState)
538
{
539
  /* Check the parameters */
540
  assert_param(IS_SPI_ALL_PERIPH(SPIx));
541
  assert_param(IS_FUNCTIONAL_STATE(NewState));
542
  if (NewState != DISABLE)
543
  {
544
    /* Enable the selected SPI SS output */
545
    SPIx->CR2 |= CR2_SSOE_Set;
546
  }
547
  else
548
  {
549
    /* Disable the selected SPI SS output */
550
    SPIx->CR2 &= CR2_SSOE_Reset;
551
  }
552
}
553
 
554
/**
555
  * @brief  Configures the data size for the selected SPI.
556
  * @param SPIx: where x can be 1, 2 or 3 to select the SPI peripheral.
557
  * @param SPI_DataSize: specifies the SPI data size.
558
  *   This parameter can be one of the following values:
559
  * @arg SPI_DataSize_16b: Set data frame format to 16bit
560
  * @arg SPI_DataSize_8b: Set data frame format to 8bit
561
  * @retval : None
562
  */
563
void SPI_DataSizeConfig(SPI_TypeDef* SPIx, uint16_t SPI_DataSize)
564
{
565
  /* Check the parameters */
566
  assert_param(IS_SPI_ALL_PERIPH(SPIx));
567
  assert_param(IS_SPI_DATASIZE(SPI_DataSize));
568
  /* Clear DFF bit */
569
  SPIx->CR1 &= (uint16_t)~SPI_DataSize_16b;
570
  /* Set new DFF bit value */
571
  SPIx->CR1 |= SPI_DataSize;
572
}
573
 
574
/**
575
  * @brief  Transmit the SPIx CRC value.
576
  * @param SPIx: where x can be 1, 2 or 3 to select the SPI peripheral.
577
  * @retval : None
578
  */
579
void SPI_TransmitCRC(SPI_TypeDef* SPIx)
580
{
581
  /* Check the parameters */
582
  assert_param(IS_SPI_ALL_PERIPH(SPIx));
583
 
584
  /* Enable the selected SPI CRC transmission */
585
  SPIx->CR1 |= CR1_CRCNext_Set;
586
}
587
 
588
/**
589
  * @brief  Enables or disables the CRC value calculation of the
590
  *   transfered bytes.
591
  * @param SPIx: where x can be 1, 2 or 3 to select the SPI peripheral.
592
  * @param NewState: new state of the SPIx CRC value calculation.
593
  *   This parameter can be: ENABLE or DISABLE.
594
  * @retval : None
595
  */
596
void SPI_CalculateCRC(SPI_TypeDef* SPIx, FunctionalState NewState)
597
{
598
  /* Check the parameters */
599
  assert_param(IS_SPI_ALL_PERIPH(SPIx));
600
  assert_param(IS_FUNCTIONAL_STATE(NewState));
601
  if (NewState != DISABLE)
602
  {
603
    /* Enable the selected SPI CRC calculation */
604
    SPIx->CR1 |= CR1_CRCEN_Set;
605
  }
606
  else
607
  {
608
    /* Disable the selected SPI CRC calculation */
609
    SPIx->CR1 &= CR1_CRCEN_Reset;
610
  }
611
}
612
 
613
/**
614
  * @brief  Returns the transmit or the receive CRC register value for
615
  *   the specified SPI.
616
  * @param SPIx: where x can be 1, 2 or 3 to select the SPI peripheral.
617
  * @param SPI_CRC: specifies the CRC register to be read.
618
  *   This parameter can be one of the following values:
619
  * @arg SPI_CRC_Tx: Selects Tx CRC register
620
  * @arg SPI_CRC_Rx: Selects Rx CRC register
621
  * @retval : The selected CRC register value..
622
  */
623
uint16_t SPI_GetCRC(SPI_TypeDef* SPIx, uint8_t SPI_CRC)
624
{
625
  uint16_t crcreg = 0;
626
  /* Check the parameters */
627
  assert_param(IS_SPI_ALL_PERIPH(SPIx));
628
  assert_param(IS_SPI_CRC(SPI_CRC));
629
  if (SPI_CRC != SPI_CRC_Rx)
630
  {
631
    /* Get the Tx CRC register */
632
    crcreg = SPIx->TXCRCR;
633
  }
634
  else
635
  {
636
    /* Get the Rx CRC register */
637
    crcreg = SPIx->RXCRCR;
638
  }
639
  /* Return the selected CRC register */
640
  return crcreg;
641
}
642
 
643
/**
644
  * @brief  Returns the CRC Polynomial register value for the specified SPI.
645
  * @param SPIx: where x can be 1, 2 or 3 to select the SPI peripheral.
646
  * @retval : The CRC Polynomial register value.
647
  */
648
uint16_t SPI_GetCRCPolynomial(SPI_TypeDef* SPIx)
649
{
650
  /* Check the parameters */
651
  assert_param(IS_SPI_ALL_PERIPH(SPIx));
652
 
653
  /* Return the CRC polynomial register */
654
  return SPIx->CRCPR;
655
}
656
 
657
/**
658
  * @brief  Selects the data transfer direction in bi-directional mode
659
  *   for the specified SPI.
660
  * @param SPIx: where x can be 1, 2 or 3 to select the SPI peripheral.
661
  * @param SPI_Direction: specifies the data transfer direction in
662
  *   bi-directional mode.
663
  *   This parameter can be one of the following values:
664
  * @arg SPI_Direction_Tx: Selects Tx transmission direction
665
  * @arg SPI_Direction_Rx: Selects Rx receive direction
666
  * @retval : None
667
  */
668
void SPI_BiDirectionalLineConfig(SPI_TypeDef* SPIx, uint16_t SPI_Direction)
669
{
670
  /* Check the parameters */
671
  assert_param(IS_SPI_ALL_PERIPH(SPIx));
672
  assert_param(IS_SPI_DIRECTION(SPI_Direction));
673
  if (SPI_Direction == SPI_Direction_Tx)
674
  {
675
    /* Set the Tx only mode */
676
    SPIx->CR1 |= SPI_Direction_Tx;
677
  }
678
  else
679
  {
680
    /* Set the Rx only mode */
681
    SPIx->CR1 &= SPI_Direction_Rx;
682
  }
683
}
684
 
685
/**
686
  * @brief  Checks whether the specified SPI/I2S flag is set or not.
687
  * @param SPIx: where x can be :
688
  *   1, 2 or 3 in SPI mode
689
  *   2 or 3 in I2S mode
690
  * @param SPI_I2S_FLAG: specifies the SPI/I2S flag to check.
691
  *   This parameter can be one of the following values:
692
  * @arg SPI_I2S_FLAG_TXE: Transmit buffer empty flag.
693
  * @arg SPI_I2S_FLAG_RXNE: Receive buffer not empty flag.
694
  * @arg SPI_I2S_FLAG_BSY: Busy flag.
695
  * @arg SPI_I2S_FLAG_OVR: Overrun flag.
696
  * @arg SPI_FLAG_MODF: Mode Fault flag.
697
  * @arg SPI_FLAG_CRCERR: CRC Error flag.
698
  * @arg I2S_FLAG_UDR: Underrun Error flag.
699
  * @arg I2S_FLAG_CHSIDE: Channel Side flag.
700
  * @retval : The new state of SPI_I2S_FLAG (SET or RESET).
701
  */
702
FlagStatus SPI_I2S_GetFlagStatus(SPI_TypeDef* SPIx, uint16_t SPI_I2S_FLAG)
703
{
704
  FlagStatus bitstatus = RESET;
705
  /* Check the parameters */
706
  assert_param(IS_SPI_ALL_PERIPH(SPIx));
707
  assert_param(IS_SPI_I2S_GET_FLAG(SPI_I2S_FLAG));
708
  /* Check the status of the specified SPI/I2S flag */
709
  if ((SPIx->SR & SPI_I2S_FLAG) != (uint16_t)RESET)
710
  {
711
    /* SPI_I2S_FLAG is set */
712
    bitstatus = SET;
713
  }
714
  else
715
  {
716
    /* SPI_I2S_FLAG is reset */
717
    bitstatus = RESET;
718
  }
719
  /* Return the SPI_I2S_FLAG status */
720
  return  bitstatus;
721
}
722
 
723
/**
724
  * @brief  Clears the SPIx CRC Error (CRCERR) flag.
725
  * @param SPIx: where x can be :
726
  *   1, 2 or 3 in SPI mode
727
  * @param SPI_I2S_FLAG: specifies the SPI flag to clear.
728
  *   This function clears only CRCERR flag.
729
  * @note
730
  *   - OVR (OverRun error) flag is cleared by software sequence: a read
731
  *     operation to SPI_DR register (SPI_I2S_ReceiveData()) followed by a read
732
  *     operation to SPI_SR register (SPI_I2S_GetFlagStatus()).
733
  *   - UDR (UnderRun error) flag is cleared by a read operation to
734
  *     SPI_SR register (SPI_I2S_GetFlagStatus()).
735
  *   - MODF (Mode Fault) flag is cleared by software sequence: a read/write
736
  *     operation to SPI_SR register (SPI_I2S_GetFlagStatus()) followed by a
737
  *     write operation to SPI_CR1 register (SPI_Cmd() to enable the SPI).
738
  * @retval : None
739
  */
740
void SPI_I2S_ClearFlag(SPI_TypeDef* SPIx, uint16_t SPI_I2S_FLAG)
741
{
742
  /* Check the parameters */
743
  assert_param(IS_SPI_ALL_PERIPH(SPIx));
744
  assert_param(IS_SPI_I2S_CLEAR_FLAG(SPI_I2S_FLAG));
745
 
746
    /* Clear the selected SPI CRC Error (CRCERR) flag */
747
    SPIx->SR = (uint16_t)~SPI_I2S_FLAG;
748
}
749
 
750
/**
751
  * @brief  Checks whether the specified SPI/I2S interrupt has occurred or not.
752
  * @param SPIx: where x can be :
753
  *   1, 2 or 3 in SPI mode
754
  *   2 or 3 in I2S mode
755
  * @param SPI_I2S_IT: specifies the SPI/I2S interrupt source to check.
756
  *   This parameter can be one of the following values:
757
  * @arg SPI_I2S_IT_TXE: Transmit buffer empty interrupt.
758
  * @arg SPI_I2S_IT_RXNE: Receive buffer not empty interrupt.
759
  * @arg SPI_I2S_IT_OVR: Overrun interrupt.
760
  * @arg SPI_IT_MODF: Mode Fault interrupt.
761
  * @arg SPI_IT_CRCERR: CRC Error interrupt.
762
  * @arg I2S_IT_UDR: Underrun Error interrupt.
763
  * @retval : The new state of SPI_I2S_IT (SET or RESET).
764
  */
765
ITStatus SPI_I2S_GetITStatus(SPI_TypeDef* SPIx, uint8_t SPI_I2S_IT)
766
{
767
  ITStatus bitstatus = RESET;
768
  uint16_t itpos = 0, itmask = 0, enablestatus = 0;
769
  /* Check the parameters */
770
  assert_param(IS_SPI_ALL_PERIPH(SPIx));
771
  assert_param(IS_SPI_I2S_GET_IT(SPI_I2S_IT));
772
  /* Get the SPI/I2S IT index */
773
  itpos = (uint16_t)((uint16_t)0x01 << (SPI_I2S_IT & (uint8_t)0x0F));
774
  /* Get the SPI/I2S IT mask */
775
  itmask = SPI_I2S_IT >> 4;
776
  /* Set the IT mask */
777
  itmask = (uint16_t)((uint16_t)0x01 << itmask);
778
  /* Get the SPI_I2S_IT enable bit status */
779
  enablestatus = (SPIx->CR2 & itmask) ;
780
  /* Check the status of the specified SPI/I2S interrupt */
781
  if (((SPIx->SR & itpos) != (uint16_t)RESET) && enablestatus)
782
  {
783
    /* SPI_I2S_IT is set */
784
    bitstatus = SET;
785
  }
786
  else
787
  {
788
    /* SPI_I2S_IT is reset */
789
    bitstatus = RESET;
790
  }
791
  /* Return the SPI_I2S_IT status */
792
  return bitstatus;
793
}
794
 
795
/**
796
  * @brief  Clears the SPIx CRC Error (CRCERR) interrupt pending bit.
797
  * @param SPIx: where x can be :
798
  *   1, 2 or 3 in SPI mode
799
  * @param SPI_I2S_IT: specifies the SPI interrupt pending bit to clear.
800
  *   This function clears only CRCERR intetrrupt pending bit.
801
  * @note
802
  *   - OVR (OverRun Error) interrupt pending bit is cleared by software
803
  *     sequence: a read operation to SPI_DR register (SPI_I2S_ReceiveData())
804
  *     followed by a read operation to SPI_SR register (SPI_I2S_GetITStatus()).
805
  *   - UDR (UnderRun Error) interrupt pending bit is cleared by a read
806
  *     operation to SPI_SR register (SPI_I2S_GetITStatus()).
807
  *   - MODF (Mode Fault) interrupt pending bit is cleared by software sequence:
808
  *     a read/write operation to SPI_SR register (SPI_I2S_GetITStatus())
809
  *     followed by a write operation to SPI_CR1 register (SPI_Cmd() to enable
810
  *     the SPI).
811
  * @retval : None
812
  */
813
void SPI_I2S_ClearITPendingBit(SPI_TypeDef* SPIx, uint8_t SPI_I2S_IT)
814
{
815
  uint16_t itpos = 0;
816
  /* Check the parameters */
817
  assert_param(IS_SPI_ALL_PERIPH(SPIx));
818
  assert_param(IS_SPI_I2S_CLEAR_IT(SPI_I2S_IT));
819
  /* Get the SPI IT index */
820
  itpos = (uint16_t)((uint16_t)0x01 << (SPI_I2S_IT & (uint8_t)0x0F));
821
  /* Clear the selected SPI CRC Error (CRCERR) interrupt pending bit */
822
  SPIx->SR = (uint16_t)~itpos;
823
}
824
/**
825
  * @}
826
  */
827
 
828
/**
829
  * @}
830
  */
831
 
832
/**
833
  * @}
834
  */
835
 
836
/******************* (C) COPYRIGHT 2009 STMicroelectronics *****END OF FILE****/

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.