STM32F4 Standard Peripheral bibliotheek  1.0
ST Microelectronics bibliotheek documentatie voor de STM32F4 Standard Peripheral Library
 All Data Structures Files Functions Variables Enumerations Enumerator Groups
stm32f4xx_spi.h
Go to the documentation of this file.
1 
29 /* Define to prevent recursive inclusion -------------------------------------*/
30 #ifndef __STM32F4xx_SPI_H
31 #define __STM32F4xx_SPI_H
32 
33 #ifdef __cplusplus
34  extern "C" {
35 #endif
36 
37 /* Includes ------------------------------------------------------------------*/
38 #include "stm32f4xx.h"
39 
48 /* Exported types ------------------------------------------------------------*/
49 
54 typedef struct
55 {
56  uint16_t SPI_Direction;
59  uint16_t SPI_Mode;
62  uint16_t SPI_DataSize;
65  uint16_t SPI_CPOL;
68  uint16_t SPI_CPHA;
71  uint16_t SPI_NSS;
75  uint16_t SPI_BaudRatePrescaler;
81  uint16_t SPI_FirstBit;
84  uint16_t SPI_CRCPolynomial;
86 
91 typedef struct
92 {
93 
94  uint16_t I2S_Mode;
97  uint16_t I2S_Standard;
100  uint16_t I2S_DataFormat;
103  uint16_t I2S_MCLKOutput;
106  uint32_t I2S_AudioFreq;
109  uint16_t I2S_CPOL;
112 
113 /* Exported constants --------------------------------------------------------*/
114 
119 #define IS_SPI_ALL_PERIPH(PERIPH) (((PERIPH) == SPI1) || \
120  ((PERIPH) == SPI2) || \
121  ((PERIPH) == SPI3))
122 
123 #define IS_SPI_ALL_PERIPH_EXT(PERIPH) (((PERIPH) == SPI1) || \
124  ((PERIPH) == SPI2) || \
125  ((PERIPH) == SPI3) || \
126  ((PERIPH) == I2S2ext) || \
127  ((PERIPH) == I2S3ext))
128 
129 #define IS_SPI_23_PERIPH(PERIPH) (((PERIPH) == SPI2) || \
130  ((PERIPH) == SPI3))
131 
132 #define IS_SPI_23_PERIPH_EXT(PERIPH) (((PERIPH) == SPI2) || \
133  ((PERIPH) == SPI3) || \
134  ((PERIPH) == I2S2ext) || \
135  ((PERIPH) == I2S3ext))
136 
137 #define IS_I2S_EXT_PERIPH(PERIPH) (((PERIPH) == I2S2ext) || \
138  ((PERIPH) == I2S3ext))
139 
140 
145 #define SPI_Direction_2Lines_FullDuplex ((uint16_t)0x0000)
146 #define SPI_Direction_2Lines_RxOnly ((uint16_t)0x0400)
147 #define SPI_Direction_1Line_Rx ((uint16_t)0x8000)
148 #define SPI_Direction_1Line_Tx ((uint16_t)0xC000)
149 #define IS_SPI_DIRECTION_MODE(MODE) (((MODE) == SPI_Direction_2Lines_FullDuplex) || \
150  ((MODE) == SPI_Direction_2Lines_RxOnly) || \
151  ((MODE) == SPI_Direction_1Line_Rx) || \
152  ((MODE) == SPI_Direction_1Line_Tx))
153 
161 #define SPI_Mode_Master ((uint16_t)0x0104)
162 #define SPI_Mode_Slave ((uint16_t)0x0000)
163 #define IS_SPI_MODE(MODE) (((MODE) == SPI_Mode_Master) || \
164  ((MODE) == SPI_Mode_Slave))
165 
173 #define SPI_DataSize_16b ((uint16_t)0x0800)
174 #define SPI_DataSize_8b ((uint16_t)0x0000)
175 #define IS_SPI_DATASIZE(DATASIZE) (((DATASIZE) == SPI_DataSize_16b) || \
176  ((DATASIZE) == SPI_DataSize_8b))
177 
185 #define SPI_CPOL_Low ((uint16_t)0x0000)
186 #define SPI_CPOL_High ((uint16_t)0x0002)
187 #define IS_SPI_CPOL(CPOL) (((CPOL) == SPI_CPOL_Low) || \
188  ((CPOL) == SPI_CPOL_High))
189 
197 #define SPI_CPHA_1Edge ((uint16_t)0x0000)
198 #define SPI_CPHA_2Edge ((uint16_t)0x0001)
199 #define IS_SPI_CPHA(CPHA) (((CPHA) == SPI_CPHA_1Edge) || \
200  ((CPHA) == SPI_CPHA_2Edge))
201 
209 #define SPI_NSS_Soft ((uint16_t)0x0200)
210 #define SPI_NSS_Hard ((uint16_t)0x0000)
211 #define IS_SPI_NSS(NSS) (((NSS) == SPI_NSS_Soft) || \
212  ((NSS) == SPI_NSS_Hard))
213 
221 #define SPI_BaudRatePrescaler_2 ((uint16_t)0x0000)
222 #define SPI_BaudRatePrescaler_4 ((uint16_t)0x0008)
223 #define SPI_BaudRatePrescaler_8 ((uint16_t)0x0010)
224 #define SPI_BaudRatePrescaler_16 ((uint16_t)0x0018)
225 #define SPI_BaudRatePrescaler_32 ((uint16_t)0x0020)
226 #define SPI_BaudRatePrescaler_64 ((uint16_t)0x0028)
227 #define SPI_BaudRatePrescaler_128 ((uint16_t)0x0030)
228 #define SPI_BaudRatePrescaler_256 ((uint16_t)0x0038)
229 #define IS_SPI_BAUDRATE_PRESCALER(PRESCALER) (((PRESCALER) == SPI_BaudRatePrescaler_2) || \
230  ((PRESCALER) == SPI_BaudRatePrescaler_4) || \
231  ((PRESCALER) == SPI_BaudRatePrescaler_8) || \
232  ((PRESCALER) == SPI_BaudRatePrescaler_16) || \
233  ((PRESCALER) == SPI_BaudRatePrescaler_32) || \
234  ((PRESCALER) == SPI_BaudRatePrescaler_64) || \
235  ((PRESCALER) == SPI_BaudRatePrescaler_128) || \
236  ((PRESCALER) == SPI_BaudRatePrescaler_256))
237 
245 #define SPI_FirstBit_MSB ((uint16_t)0x0000)
246 #define SPI_FirstBit_LSB ((uint16_t)0x0080)
247 #define IS_SPI_FIRST_BIT(BIT) (((BIT) == SPI_FirstBit_MSB) || \
248  ((BIT) == SPI_FirstBit_LSB))
249 
257 #define I2S_Mode_SlaveTx ((uint16_t)0x0000)
258 #define I2S_Mode_SlaveRx ((uint16_t)0x0100)
259 #define I2S_Mode_MasterTx ((uint16_t)0x0200)
260 #define I2S_Mode_MasterRx ((uint16_t)0x0300)
261 #define IS_I2S_MODE(MODE) (((MODE) == I2S_Mode_SlaveTx) || \
262  ((MODE) == I2S_Mode_SlaveRx) || \
263  ((MODE) == I2S_Mode_MasterTx)|| \
264  ((MODE) == I2S_Mode_MasterRx))
265 
274 #define I2S_Standard_Phillips ((uint16_t)0x0000)
275 #define I2S_Standard_MSB ((uint16_t)0x0010)
276 #define I2S_Standard_LSB ((uint16_t)0x0020)
277 #define I2S_Standard_PCMShort ((uint16_t)0x0030)
278 #define I2S_Standard_PCMLong ((uint16_t)0x00B0)
279 #define IS_I2S_STANDARD(STANDARD) (((STANDARD) == I2S_Standard_Phillips) || \
280  ((STANDARD) == I2S_Standard_MSB) || \
281  ((STANDARD) == I2S_Standard_LSB) || \
282  ((STANDARD) == I2S_Standard_PCMShort) || \
283  ((STANDARD) == I2S_Standard_PCMLong))
284 
292 #define I2S_DataFormat_16b ((uint16_t)0x0000)
293 #define I2S_DataFormat_16bextended ((uint16_t)0x0001)
294 #define I2S_DataFormat_24b ((uint16_t)0x0003)
295 #define I2S_DataFormat_32b ((uint16_t)0x0005)
296 #define IS_I2S_DATA_FORMAT(FORMAT) (((FORMAT) == I2S_DataFormat_16b) || \
297  ((FORMAT) == I2S_DataFormat_16bextended) || \
298  ((FORMAT) == I2S_DataFormat_24b) || \
299  ((FORMAT) == I2S_DataFormat_32b))
300 
308 #define I2S_MCLKOutput_Enable ((uint16_t)0x0200)
309 #define I2S_MCLKOutput_Disable ((uint16_t)0x0000)
310 #define IS_I2S_MCLK_OUTPUT(OUTPUT) (((OUTPUT) == I2S_MCLKOutput_Enable) || \
311  ((OUTPUT) == I2S_MCLKOutput_Disable))
312 
320 #define I2S_AudioFreq_192k ((uint32_t)192000)
321 #define I2S_AudioFreq_96k ((uint32_t)96000)
322 #define I2S_AudioFreq_48k ((uint32_t)48000)
323 #define I2S_AudioFreq_44k ((uint32_t)44100)
324 #define I2S_AudioFreq_32k ((uint32_t)32000)
325 #define I2S_AudioFreq_22k ((uint32_t)22050)
326 #define I2S_AudioFreq_16k ((uint32_t)16000)
327 #define I2S_AudioFreq_11k ((uint32_t)11025)
328 #define I2S_AudioFreq_8k ((uint32_t)8000)
329 #define I2S_AudioFreq_Default ((uint32_t)2)
330 
331 #define IS_I2S_AUDIO_FREQ(FREQ) ((((FREQ) >= I2S_AudioFreq_8k) && \
332  ((FREQ) <= I2S_AudioFreq_192k)) || \
333  ((FREQ) == I2S_AudioFreq_Default))
334 
342 #define I2S_CPOL_Low ((uint16_t)0x0000)
343 #define I2S_CPOL_High ((uint16_t)0x0008)
344 #define IS_I2S_CPOL(CPOL) (((CPOL) == I2S_CPOL_Low) || \
345  ((CPOL) == I2S_CPOL_High))
346 
354 #define SPI_I2S_DMAReq_Tx ((uint16_t)0x0002)
355 #define SPI_I2S_DMAReq_Rx ((uint16_t)0x0001)
356 #define IS_SPI_I2S_DMAREQ(DMAREQ) ((((DMAREQ) & (uint16_t)0xFFFC) == 0x00) && ((DMAREQ) != 0x00))
357 
365 #define SPI_NSSInternalSoft_Set ((uint16_t)0x0100)
366 #define SPI_NSSInternalSoft_Reset ((uint16_t)0xFEFF)
367 #define IS_SPI_NSS_INTERNAL(INTERNAL) (((INTERNAL) == SPI_NSSInternalSoft_Set) || \
368  ((INTERNAL) == SPI_NSSInternalSoft_Reset))
369 
377 #define SPI_CRC_Tx ((uint8_t)0x00)
378 #define SPI_CRC_Rx ((uint8_t)0x01)
379 #define IS_SPI_CRC(CRC) (((CRC) == SPI_CRC_Tx) || ((CRC) == SPI_CRC_Rx))
380 
388 #define SPI_Direction_Rx ((uint16_t)0xBFFF)
389 #define SPI_Direction_Tx ((uint16_t)0x4000)
390 #define IS_SPI_DIRECTION(DIRECTION) (((DIRECTION) == SPI_Direction_Rx) || \
391  ((DIRECTION) == SPI_Direction_Tx))
392 
400 #define SPI_I2S_IT_TXE ((uint8_t)0x71)
401 #define SPI_I2S_IT_RXNE ((uint8_t)0x60)
402 #define SPI_I2S_IT_ERR ((uint8_t)0x50)
403 #define I2S_IT_UDR ((uint8_t)0x53)
404 #define SPI_I2S_IT_TIFRFE ((uint8_t)0x58)
405 
406 #define IS_SPI_I2S_CONFIG_IT(IT) (((IT) == SPI_I2S_IT_TXE) || \
407  ((IT) == SPI_I2S_IT_RXNE) || \
408  ((IT) == SPI_I2S_IT_ERR))
409 
410 #define SPI_I2S_IT_OVR ((uint8_t)0x56)
411 #define SPI_IT_MODF ((uint8_t)0x55)
412 #define SPI_IT_CRCERR ((uint8_t)0x54)
413 
414 #define IS_SPI_I2S_CLEAR_IT(IT) (((IT) == SPI_IT_CRCERR))
415 
416 #define IS_SPI_I2S_GET_IT(IT) (((IT) == SPI_I2S_IT_RXNE)|| ((IT) == SPI_I2S_IT_TXE) || \
417  ((IT) == SPI_IT_CRCERR) || ((IT) == SPI_IT_MODF) || \
418  ((IT) == SPI_I2S_IT_OVR) || ((IT) == I2S_IT_UDR) ||\
419  ((IT) == SPI_I2S_IT_TIFRFE))
420 
428 #define SPI_I2S_FLAG_RXNE ((uint16_t)0x0001)
429 #define SPI_I2S_FLAG_TXE ((uint16_t)0x0002)
430 #define I2S_FLAG_CHSIDE ((uint16_t)0x0004)
431 #define I2S_FLAG_UDR ((uint16_t)0x0008)
432 #define SPI_FLAG_CRCERR ((uint16_t)0x0010)
433 #define SPI_FLAG_MODF ((uint16_t)0x0020)
434 #define SPI_I2S_FLAG_OVR ((uint16_t)0x0040)
435 #define SPI_I2S_FLAG_BSY ((uint16_t)0x0080)
436 #define SPI_I2S_FLAG_TIFRFE ((uint16_t)0x0100)
437 
438 #define IS_SPI_I2S_CLEAR_FLAG(FLAG) (((FLAG) == SPI_FLAG_CRCERR))
439 #define IS_SPI_I2S_GET_FLAG(FLAG) (((FLAG) == SPI_I2S_FLAG_BSY) || ((FLAG) == SPI_I2S_FLAG_OVR) || \
440  ((FLAG) == SPI_FLAG_MODF) || ((FLAG) == SPI_FLAG_CRCERR) || \
441  ((FLAG) == I2S_FLAG_UDR) || ((FLAG) == I2S_FLAG_CHSIDE) || \
442  ((FLAG) == SPI_I2S_FLAG_TXE) || ((FLAG) == SPI_I2S_FLAG_RXNE)|| \
443  ((FLAG) == SPI_I2S_FLAG_TIFRFE))
444 
452 #define IS_SPI_CRC_POLYNOMIAL(POLYNOMIAL) ((POLYNOMIAL) >= 0x1)
453 
461 #define SPI_DMAReq_Tx SPI_I2S_DMAReq_Tx
462 #define SPI_DMAReq_Rx SPI_I2S_DMAReq_Rx
463 #define SPI_IT_TXE SPI_I2S_IT_TXE
464 #define SPI_IT_RXNE SPI_I2S_IT_RXNE
465 #define SPI_IT_ERR SPI_I2S_IT_ERR
466 #define SPI_IT_OVR SPI_I2S_IT_OVR
467 #define SPI_FLAG_RXNE SPI_I2S_FLAG_RXNE
468 #define SPI_FLAG_TXE SPI_I2S_FLAG_TXE
469 #define SPI_FLAG_OVR SPI_I2S_FLAG_OVR
470 #define SPI_FLAG_BSY SPI_I2S_FLAG_BSY
471 #define SPI_DeInit SPI_I2S_DeInit
472 #define SPI_ITConfig SPI_I2S_ITConfig
473 #define SPI_DMACmd SPI_I2S_DMACmd
474 #define SPI_SendData SPI_I2S_SendData
475 #define SPI_ReceiveData SPI_I2S_ReceiveData
476 #define SPI_GetFlagStatus SPI_I2S_GetFlagStatus
477 #define SPI_ClearFlag SPI_I2S_ClearFlag
478 #define SPI_GetITStatus SPI_I2S_GetITStatus
479 #define SPI_ClearITPendingBit SPI_I2S_ClearITPendingBit
480 
488 /* Exported macro ------------------------------------------------------------*/
489 /* Exported functions --------------------------------------------------------*/
490 
491 /* Function used to set the SPI configuration to the default reset state *****/
492 void SPI_I2S_DeInit(SPI_TypeDef* SPIx);
493 
494 /* Initialization and Configuration functions *********************************/
495 void SPI_Init(SPI_TypeDef* SPIx, SPI_InitTypeDef* SPI_InitStruct);
496 void I2S_Init(SPI_TypeDef* SPIx, I2S_InitTypeDef* I2S_InitStruct);
497 void SPI_StructInit(SPI_InitTypeDef* SPI_InitStruct);
498 void I2S_StructInit(I2S_InitTypeDef* I2S_InitStruct);
499 void SPI_Cmd(SPI_TypeDef* SPIx, FunctionalState NewState);
500 void I2S_Cmd(SPI_TypeDef* SPIx, FunctionalState NewState);
501 void SPI_DataSizeConfig(SPI_TypeDef* SPIx, uint16_t SPI_DataSize);
502 void SPI_BiDirectionalLineConfig(SPI_TypeDef* SPIx, uint16_t SPI_Direction);
503 void SPI_NSSInternalSoftwareConfig(SPI_TypeDef* SPIx, uint16_t SPI_NSSInternalSoft);
504 void SPI_SSOutputCmd(SPI_TypeDef* SPIx, FunctionalState NewState);
505 void SPI_TIModeCmd(SPI_TypeDef* SPIx, FunctionalState NewState);
506 
507 void I2S_FullDuplexConfig(SPI_TypeDef* I2Sxext, I2S_InitTypeDef* I2S_InitStruct);
508 
509 /* Data transfers functions ***************************************************/
510 void SPI_I2S_SendData(SPI_TypeDef* SPIx, uint16_t Data);
511 uint16_t SPI_I2S_ReceiveData(SPI_TypeDef* SPIx);
512 
513 /* Hardware CRC Calculation functions *****************************************/
514 void SPI_CalculateCRC(SPI_TypeDef* SPIx, FunctionalState NewState);
515 void SPI_TransmitCRC(SPI_TypeDef* SPIx);
516 uint16_t SPI_GetCRC(SPI_TypeDef* SPIx, uint8_t SPI_CRC);
517 uint16_t SPI_GetCRCPolynomial(SPI_TypeDef* SPIx);
518 
519 /* DMA transfers management functions *****************************************/
520 void SPI_I2S_DMACmd(SPI_TypeDef* SPIx, uint16_t SPI_I2S_DMAReq, FunctionalState NewState);
521 
522 /* Interrupts and flags management functions **********************************/
523 void SPI_I2S_ITConfig(SPI_TypeDef* SPIx, uint8_t SPI_I2S_IT, FunctionalState NewState);
524 FlagStatus SPI_I2S_GetFlagStatus(SPI_TypeDef* SPIx, uint16_t SPI_I2S_FLAG);
525 void SPI_I2S_ClearFlag(SPI_TypeDef* SPIx, uint16_t SPI_I2S_FLAG);
526 ITStatus SPI_I2S_GetITStatus(SPI_TypeDef* SPIx, uint8_t SPI_I2S_IT);
527 void SPI_I2S_ClearITPendingBit(SPI_TypeDef* SPIx, uint8_t SPI_I2S_IT);
528 
529 #ifdef __cplusplus
530 }
531 #endif
532 
533 #endif /*__STM32F4xx_SPI_H */
534 
543 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/