2 ******************************************************************************
\r
3 * @file stm32l1xx_spi.h
\r
4 * @author MCD Application Team
\r
6 * @date 05-March-2012
\r
7 * @brief This file contains all the functions prototypes for the SPI
\r
9 ******************************************************************************
\r
12 * <h2><center>© COPYRIGHT 2012 STMicroelectronics</center></h2>
\r
14 * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
\r
15 * You may not use this file except in compliance with the License.
\r
16 * You may obtain a copy of the License at:
\r
18 * http://www.st.com/software_license_agreement_liberty_v2
\r
20 * Unless required by applicable law or agreed to in writing, software
\r
21 * distributed under the License is distributed on an "AS IS" BASIS,
\r
22 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
23 * See the License for the specific language governing permissions and
\r
24 * limitations under the License.
\r
26 ******************************************************************************
\r
29 /* Define to prevent recursive inclusion -------------------------------------*/
\r
30 #ifndef __STM32L1xx_SPI_H
\r
31 #define __STM32L1xx_SPI_H
\r
37 /* Includes ------------------------------------------------------------------*/
\r
38 #include "stm32l1xx.h"
\r
40 /** @addtogroup STM32L1xx_StdPeriph_Driver
\r
48 /* Exported types ------------------------------------------------------------*/
\r
51 * @brief SPI Init structure definition
\r
56 uint16_t SPI_Direction; /*!< Specifies the SPI unidirectional or bidirectional data mode.
\r
57 This parameter can be a value of @ref SPI_data_direction */
\r
59 uint16_t SPI_Mode; /*!< Specifies the SPI operating mode.
\r
60 This parameter can be a value of @ref SPI_mode */
\r
62 uint16_t SPI_DataSize; /*!< Specifies the SPI data size.
\r
63 This parameter can be a value of @ref SPI_data_size */
\r
65 uint16_t SPI_CPOL; /*!< Specifies the serial clock steady state.
\r
66 This parameter can be a value of @ref SPI_Clock_Polarity */
\r
68 uint16_t SPI_CPHA; /*!< Specifies the clock active edge for the bit capture.
\r
69 This parameter can be a value of @ref SPI_Clock_Phase */
\r
71 uint16_t SPI_NSS; /*!< Specifies whether the NSS signal is managed by
\r
72 hardware (NSS pin) or by software using the SSI bit.
\r
73 This parameter can be a value of @ref SPI_Slave_Select_management */
\r
75 uint16_t SPI_BaudRatePrescaler; /*!< Specifies the Baud Rate prescaler value which will be
\r
76 used to configure the transmit and receive SCK clock.
\r
77 This parameter can be a value of @ref SPI_BaudRate_Prescaler
\r
78 @note The communication clock is derived from the master
\r
79 clock. The slave clock does not need to be set. */
\r
81 uint16_t SPI_FirstBit; /*!< Specifies whether data transfers start from MSB or LSB bit.
\r
82 This parameter can be a value of @ref SPI_MSB_LSB_transmission */
\r
84 uint16_t SPI_CRCPolynomial; /*!< Specifies the polynomial used for the CRC calculation. */
\r
88 * @brief I2S Init structure definition
\r
94 uint16_t I2S_Mode; /*!< Specifies the I2S operating mode.
\r
95 This parameter can be a value of @ref SPI_I2S_Mode */
\r
97 uint16_t I2S_Standard; /*!< Specifies the standard used for the I2S communication.
\r
98 This parameter can be a value of @ref SPI_I2S_Standard */
\r
100 uint16_t I2S_DataFormat; /*!< Specifies the data format for the I2S communication.
\r
101 This parameter can be a value of @ref SPI_I2S_Data_Format */
\r
103 uint16_t I2S_MCLKOutput; /*!< Specifies whether the I2S MCLK output is enabled or not.
\r
104 This parameter can be a value of @ref SPI_I2S_MCLK_Output */
\r
106 uint32_t I2S_AudioFreq; /*!< Specifies the frequency selected for the I2S communication.
\r
107 This parameter can be a value of @ref SPI_I2S_Audio_Frequency */
\r
109 uint16_t I2S_CPOL; /*!< Specifies the idle state of the I2S clock.
\r
110 This parameter can be a value of @ref SPI_I2S_Clock_Polarity */
\r
113 /* Exported constants --------------------------------------------------------*/
\r
115 /** @defgroup SPI_Exported_Constants
\r
119 #define IS_SPI_ALL_PERIPH(PERIPH) (((PERIPH) == SPI1) || \
\r
120 ((PERIPH) == SPI2) || \
\r
121 ((PERIPH) == SPI3))
\r
122 #define IS_SPI_23_PERIPH(PERIPH) (((PERIPH) == SPI2) || \
\r
123 ((PERIPH) == SPI3))
\r
125 /** @defgroup SPI_data_direction
\r
129 #define SPI_Direction_2Lines_FullDuplex ((uint16_t)0x0000)
\r
130 #define SPI_Direction_2Lines_RxOnly ((uint16_t)0x0400)
\r
131 #define SPI_Direction_1Line_Rx ((uint16_t)0x8000)
\r
132 #define SPI_Direction_1Line_Tx ((uint16_t)0xC000)
\r
133 #define IS_SPI_DIRECTION_MODE(MODE) (((MODE) == SPI_Direction_2Lines_FullDuplex) || \
\r
134 ((MODE) == SPI_Direction_2Lines_RxOnly) || \
\r
135 ((MODE) == SPI_Direction_1Line_Rx) || \
\r
136 ((MODE) == SPI_Direction_1Line_Tx))
\r
141 /** @defgroup SPI_mode
\r
145 #define SPI_Mode_Master ((uint16_t)0x0104)
\r
146 #define SPI_Mode_Slave ((uint16_t)0x0000)
\r
147 #define IS_SPI_MODE(MODE) (((MODE) == SPI_Mode_Master) || \
\r
148 ((MODE) == SPI_Mode_Slave))
\r
153 /** @defgroup SPI_data_size
\r
157 #define SPI_DataSize_16b ((uint16_t)0x0800)
\r
158 #define SPI_DataSize_8b ((uint16_t)0x0000)
\r
159 #define IS_SPI_DATASIZE(DATASIZE) (((DATASIZE) == SPI_DataSize_16b) || \
\r
160 ((DATASIZE) == SPI_DataSize_8b))
\r
165 /** @defgroup SPI_Clock_Polarity
\r
169 #define SPI_CPOL_Low ((uint16_t)0x0000)
\r
170 #define SPI_CPOL_High ((uint16_t)0x0002)
\r
171 #define IS_SPI_CPOL(CPOL) (((CPOL) == SPI_CPOL_Low) || \
\r
172 ((CPOL) == SPI_CPOL_High))
\r
177 /** @defgroup SPI_Clock_Phase
\r
181 #define SPI_CPHA_1Edge ((uint16_t)0x0000)
\r
182 #define SPI_CPHA_2Edge ((uint16_t)0x0001)
\r
183 #define IS_SPI_CPHA(CPHA) (((CPHA) == SPI_CPHA_1Edge) || \
\r
184 ((CPHA) == SPI_CPHA_2Edge))
\r
189 /** @defgroup SPI_Slave_Select_management
\r
193 #define SPI_NSS_Soft ((uint16_t)0x0200)
\r
194 #define SPI_NSS_Hard ((uint16_t)0x0000)
\r
195 #define IS_SPI_NSS(NSS) (((NSS) == SPI_NSS_Soft) || \
\r
196 ((NSS) == SPI_NSS_Hard))
\r
201 /** @defgroup SPI_BaudRate_Prescaler
\r
205 #define SPI_BaudRatePrescaler_2 ((uint16_t)0x0000)
\r
206 #define SPI_BaudRatePrescaler_4 ((uint16_t)0x0008)
\r
207 #define SPI_BaudRatePrescaler_8 ((uint16_t)0x0010)
\r
208 #define SPI_BaudRatePrescaler_16 ((uint16_t)0x0018)
\r
209 #define SPI_BaudRatePrescaler_32 ((uint16_t)0x0020)
\r
210 #define SPI_BaudRatePrescaler_64 ((uint16_t)0x0028)
\r
211 #define SPI_BaudRatePrescaler_128 ((uint16_t)0x0030)
\r
212 #define SPI_BaudRatePrescaler_256 ((uint16_t)0x0038)
\r
213 #define IS_SPI_BAUDRATE_PRESCALER(PRESCALER) (((PRESCALER) == SPI_BaudRatePrescaler_2) || \
\r
214 ((PRESCALER) == SPI_BaudRatePrescaler_4) || \
\r
215 ((PRESCALER) == SPI_BaudRatePrescaler_8) || \
\r
216 ((PRESCALER) == SPI_BaudRatePrescaler_16) || \
\r
217 ((PRESCALER) == SPI_BaudRatePrescaler_32) || \
\r
218 ((PRESCALER) == SPI_BaudRatePrescaler_64) || \
\r
219 ((PRESCALER) == SPI_BaudRatePrescaler_128) || \
\r
220 ((PRESCALER) == SPI_BaudRatePrescaler_256))
\r
225 /** @defgroup SPI_MSB_LSB_transmission
\r
229 #define SPI_FirstBit_MSB ((uint16_t)0x0000)
\r
230 #define SPI_FirstBit_LSB ((uint16_t)0x0080)
\r
231 #define IS_SPI_FIRST_BIT(BIT) (((BIT) == SPI_FirstBit_MSB) || \
\r
232 ((BIT) == SPI_FirstBit_LSB))
\r
237 /** @defgroup SPI_I2S_Mode
\r
241 #define I2S_Mode_SlaveTx ((uint16_t)0x0000)
\r
242 #define I2S_Mode_SlaveRx ((uint16_t)0x0100)
\r
243 #define I2S_Mode_MasterTx ((uint16_t)0x0200)
\r
244 #define I2S_Mode_MasterRx ((uint16_t)0x0300)
\r
245 #define IS_I2S_MODE(MODE) (((MODE) == I2S_Mode_SlaveTx) || \
\r
246 ((MODE) == I2S_Mode_SlaveRx) || \
\r
247 ((MODE) == I2S_Mode_MasterTx)|| \
\r
248 ((MODE) == I2S_Mode_MasterRx))
\r
254 /** @defgroup SPI_I2S_Standard
\r
258 #define I2S_Standard_Phillips ((uint16_t)0x0000)
\r
259 #define I2S_Standard_MSB ((uint16_t)0x0010)
\r
260 #define I2S_Standard_LSB ((uint16_t)0x0020)
\r
261 #define I2S_Standard_PCMShort ((uint16_t)0x0030)
\r
262 #define I2S_Standard_PCMLong ((uint16_t)0x00B0)
\r
263 #define IS_I2S_STANDARD(STANDARD) (((STANDARD) == I2S_Standard_Phillips) || \
\r
264 ((STANDARD) == I2S_Standard_MSB) || \
\r
265 ((STANDARD) == I2S_Standard_LSB) || \
\r
266 ((STANDARD) == I2S_Standard_PCMShort) || \
\r
267 ((STANDARD) == I2S_Standard_PCMLong))
\r
272 /** @defgroup SPI_I2S_Data_Format
\r
276 #define I2S_DataFormat_16b ((uint16_t)0x0000)
\r
277 #define I2S_DataFormat_16bextended ((uint16_t)0x0001)
\r
278 #define I2S_DataFormat_24b ((uint16_t)0x0003)
\r
279 #define I2S_DataFormat_32b ((uint16_t)0x0005)
\r
280 #define IS_I2S_DATA_FORMAT(FORMAT) (((FORMAT) == I2S_DataFormat_16b) || \
\r
281 ((FORMAT) == I2S_DataFormat_16bextended) || \
\r
282 ((FORMAT) == I2S_DataFormat_24b) || \
\r
283 ((FORMAT) == I2S_DataFormat_32b))
\r
288 /** @defgroup SPI_I2S_MCLK_Output
\r
292 #define I2S_MCLKOutput_Enable ((uint16_t)0x0200)
\r
293 #define I2S_MCLKOutput_Disable ((uint16_t)0x0000)
\r
294 #define IS_I2S_MCLK_OUTPUT(OUTPUT) (((OUTPUT) == I2S_MCLKOutput_Enable) || \
\r
295 ((OUTPUT) == I2S_MCLKOutput_Disable))
\r
300 /** @defgroup SPI_I2S_Audio_Frequency
\r
304 #define I2S_AudioFreq_192k ((uint32_t)192000)
\r
305 #define I2S_AudioFreq_96k ((uint32_t)96000)
\r
306 #define I2S_AudioFreq_48k ((uint32_t)48000)
\r
307 #define I2S_AudioFreq_44k ((uint32_t)44100)
\r
308 #define I2S_AudioFreq_32k ((uint32_t)32000)
\r
309 #define I2S_AudioFreq_22k ((uint32_t)22050)
\r
310 #define I2S_AudioFreq_16k ((uint32_t)16000)
\r
311 #define I2S_AudioFreq_11k ((uint32_t)11025)
\r
312 #define I2S_AudioFreq_8k ((uint32_t)8000)
\r
313 #define I2S_AudioFreq_Default ((uint32_t)2)
\r
315 #define IS_I2S_AUDIO_FREQ(FREQ) ((((FREQ) >= I2S_AudioFreq_8k) && \
\r
316 ((FREQ) <= I2S_AudioFreq_192k)) || \
\r
317 ((FREQ) == I2S_AudioFreq_Default))
\r
322 /** @defgroup SPI_I2S_Clock_Polarity
\r
326 #define I2S_CPOL_Low ((uint16_t)0x0000)
\r
327 #define I2S_CPOL_High ((uint16_t)0x0008)
\r
328 #define IS_I2S_CPOL(CPOL) (((CPOL) == I2S_CPOL_Low) || \
\r
329 ((CPOL) == I2S_CPOL_High))
\r
334 /** @defgroup SPI_I2S_DMA_transfer_requests
\r
338 #define SPI_I2S_DMAReq_Tx ((uint16_t)0x0002)
\r
339 #define SPI_I2S_DMAReq_Rx ((uint16_t)0x0001)
\r
340 #define IS_SPI_I2S_DMAREQ(DMAREQ) ((((DMAREQ) & (uint16_t)0xFFFC) == 0x00) && ((DMAREQ) != 0x00))
\r
345 /** @defgroup SPI_NSS_internal_software_management
\r
349 #define SPI_NSSInternalSoft_Set ((uint16_t)0x0100)
\r
350 #define SPI_NSSInternalSoft_Reset ((uint16_t)0xFEFF)
\r
351 #define IS_SPI_NSS_INTERNAL(INTERNAL) (((INTERNAL) == SPI_NSSInternalSoft_Set) || \
\r
352 ((INTERNAL) == SPI_NSSInternalSoft_Reset))
\r
357 /** @defgroup SPI_CRC_Transmit_Receive
\r
361 #define SPI_CRC_Tx ((uint8_t)0x00)
\r
362 #define SPI_CRC_Rx ((uint8_t)0x01)
\r
363 #define IS_SPI_CRC(CRC) (((CRC) == SPI_CRC_Tx) || ((CRC) == SPI_CRC_Rx))
\r
368 /** @defgroup SPI_direction_transmit_receive
\r
372 #define SPI_Direction_Rx ((uint16_t)0xBFFF)
\r
373 #define SPI_Direction_Tx ((uint16_t)0x4000)
\r
374 #define IS_SPI_DIRECTION(DIRECTION) (((DIRECTION) == SPI_Direction_Rx) || \
\r
375 ((DIRECTION) == SPI_Direction_Tx))
\r
380 /** @defgroup SPI_I2S_interrupts_definition
\r
384 #define SPI_I2S_IT_TXE ((uint8_t)0x71)
\r
385 #define SPI_I2S_IT_RXNE ((uint8_t)0x60)
\r
386 #define SPI_I2S_IT_ERR ((uint8_t)0x50)
\r
387 #define I2S_IT_UDR ((uint8_t)0x53)
\r
388 #define SPI_I2S_IT_FRE ((uint8_t)0x58)
\r
390 #define IS_SPI_I2S_CONFIG_IT(IT) (((IT) == SPI_I2S_IT_TXE) || \
\r
391 ((IT) == SPI_I2S_IT_RXNE) || \
\r
392 ((IT) == SPI_I2S_IT_ERR))
\r
394 #define SPI_I2S_IT_OVR ((uint8_t)0x56)
\r
395 #define SPI_IT_MODF ((uint8_t)0x55)
\r
396 #define SPI_IT_CRCERR ((uint8_t)0x54)
\r
398 #define IS_SPI_I2S_CLEAR_IT(IT) (((IT) == SPI_IT_CRCERR))
\r
400 #define IS_SPI_I2S_GET_IT(IT) (((IT) == SPI_I2S_IT_RXNE) || ((IT) == SPI_I2S_IT_TXE) || \
\r
401 ((IT) == SPI_IT_CRCERR) || ((IT) == SPI_IT_MODF) || \
\r
402 ((IT) == SPI_I2S_IT_OVR) || ((IT) == I2S_IT_UDR) ||\
\r
403 ((IT) == SPI_I2S_IT_FRE))
\r
408 /** @defgroup SPI_I2S_flags_definition
\r
412 #define SPI_I2S_FLAG_RXNE ((uint16_t)0x0001)
\r
413 #define SPI_I2S_FLAG_TXE ((uint16_t)0x0002)
\r
414 #define I2S_FLAG_CHSIDE ((uint16_t)0x0004)
\r
415 #define I2S_FLAG_UDR ((uint16_t)0x0008)
\r
416 #define SPI_FLAG_CRCERR ((uint16_t)0x0010)
\r
417 #define SPI_FLAG_MODF ((uint16_t)0x0020)
\r
418 #define SPI_I2S_FLAG_OVR ((uint16_t)0x0040)
\r
419 #define SPI_I2S_FLAG_BSY ((uint16_t)0x0080)
\r
420 #define SPI_I2S_FLAG_FRE ((uint16_t)0x0100)
\r
422 #define IS_SPI_I2S_CLEAR_FLAG(FLAG) (((FLAG) == SPI_FLAG_CRCERR))
\r
423 #define IS_SPI_I2S_GET_FLAG(FLAG) (((FLAG) == SPI_I2S_FLAG_BSY) || ((FLAG) == SPI_I2S_FLAG_OVR) || \
\r
424 ((FLAG) == SPI_FLAG_MODF) || ((FLAG) == SPI_FLAG_CRCERR) || \
\r
425 ((FLAG) == I2S_FLAG_UDR) || ((FLAG) == I2S_FLAG_CHSIDE) || \
\r
426 ((FLAG) == SPI_I2S_FLAG_TXE) || ((FLAG) == SPI_I2S_FLAG_RXNE)|| \
\r
427 ((FLAG) == SPI_I2S_FLAG_FRE))
\r
432 /** @defgroup SPI_CRC_polynomial
\r
436 #define IS_SPI_CRC_POLYNOMIAL(POLYNOMIAL) ((POLYNOMIAL) >= 0x1)
\r
441 /** @defgroup SPI_I2S_Legacy
\r
445 #define SPI_DMAReq_Tx SPI_I2S_DMAReq_Tx
\r
446 #define SPI_DMAReq_Rx SPI_I2S_DMAReq_Rx
\r
447 #define SPI_IT_TXE SPI_I2S_IT_TXE
\r
448 #define SPI_IT_RXNE SPI_I2S_IT_RXNE
\r
449 #define SPI_IT_ERR SPI_I2S_IT_ERR
\r
450 #define SPI_IT_OVR SPI_I2S_IT_OVR
\r
451 #define SPI_FLAG_RXNE SPI_I2S_FLAG_RXNE
\r
452 #define SPI_FLAG_TXE SPI_I2S_FLAG_TXE
\r
453 #define SPI_FLAG_OVR SPI_I2S_FLAG_OVR
\r
454 #define SPI_FLAG_BSY SPI_I2S_FLAG_BSY
\r
455 #define SPI_DeInit SPI_I2S_DeInit
\r
456 #define SPI_ITConfig SPI_I2S_ITConfig
\r
457 #define SPI_DMACmd SPI_I2S_DMACmd
\r
458 #define SPI_SendData SPI_I2S_SendData
\r
459 #define SPI_ReceiveData SPI_I2S_ReceiveData
\r
460 #define SPI_GetFlagStatus SPI_I2S_GetFlagStatus
\r
461 #define SPI_ClearFlag SPI_I2S_ClearFlag
\r
462 #define SPI_GetITStatus SPI_I2S_GetITStatus
\r
463 #define SPI_ClearITPendingBit SPI_I2S_ClearITPendingBit
\r
472 /* Exported macro ------------------------------------------------------------*/
\r
473 /* Exported functions ------------------------------------------------------- */
\r
475 /* Function used to set the SPI configuration to the default reset state *****/
\r
476 void SPI_I2S_DeInit(SPI_TypeDef* SPIx);
\r
478 /* Initialization and Configuration functions *********************************/
\r
479 void SPI_Init(SPI_TypeDef* SPIx, SPI_InitTypeDef* SPI_InitStruct);
\r
480 void I2S_Init(SPI_TypeDef* SPIx, I2S_InitTypeDef* I2S_InitStruct);
\r
481 void SPI_StructInit(SPI_InitTypeDef* SPI_InitStruct);
\r
482 void I2S_StructInit(I2S_InitTypeDef* I2S_InitStruct);
\r
483 void SPI_Cmd(SPI_TypeDef* SPIx, FunctionalState NewState);
\r
484 void I2S_Cmd(SPI_TypeDef* SPIx, FunctionalState NewState);
\r
485 void SPI_DataSizeConfig(SPI_TypeDef* SPIx, uint16_t SPI_DataSize);
\r
486 void SPI_BiDirectionalLineConfig(SPI_TypeDef* SPIx, uint16_t SPI_Direction);
\r
487 void SPI_NSSInternalSoftwareConfig(SPI_TypeDef* SPIx, uint16_t SPI_NSSInternalSoft);
\r
488 void SPI_SSOutputCmd(SPI_TypeDef* SPIx, FunctionalState NewState);
\r
490 /* Data transfers functions ***************************************************/
\r
491 void SPI_I2S_SendData(SPI_TypeDef* SPIx, uint16_t Data);
\r
492 uint16_t SPI_I2S_ReceiveData(SPI_TypeDef* SPIx);
\r
494 /* Hardware CRC Calculation functions *****************************************/
\r
495 void SPI_CalculateCRC(SPI_TypeDef* SPIx, FunctionalState NewState);
\r
496 void SPI_TransmitCRC(SPI_TypeDef* SPIx);
\r
497 uint16_t SPI_GetCRC(SPI_TypeDef* SPIx, uint8_t SPI_CRC);
\r
498 uint16_t SPI_GetCRCPolynomial(SPI_TypeDef* SPIx);
\r
500 /* DMA transfers management functions *****************************************/
\r
501 void SPI_I2S_DMACmd(SPI_TypeDef* SPIx, uint16_t SPI_I2S_DMAReq, FunctionalState NewState);
\r
503 /* Interrupts and flags management functions **********************************/
\r
504 void SPI_I2S_ITConfig(SPI_TypeDef* SPIx, uint8_t SPI_I2S_IT, FunctionalState NewState);
\r
505 FlagStatus SPI_I2S_GetFlagStatus(SPI_TypeDef* SPIx, uint16_t SPI_I2S_FLAG);
\r
506 void SPI_I2S_ClearFlag(SPI_TypeDef* SPIx, uint16_t SPI_I2S_FLAG);
\r
507 ITStatus SPI_I2S_GetITStatus(SPI_TypeDef* SPIx, uint8_t SPI_I2S_IT);
\r
508 void SPI_I2S_ClearITPendingBit(SPI_TypeDef* SPIx, uint8_t SPI_I2S_IT);
\r
514 #endif /*__STM32L1xx_SPI_H */
\r
524 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
\r