30 #ifndef __STM32F4xx_SPI_H
31 #define __STM32F4xx_SPI_H
38 #include "stm32f4xx.h"
56 uint16_t SPI_Direction;
62 uint16_t SPI_DataSize;
75 uint16_t SPI_BaudRatePrescaler;
81 uint16_t SPI_FirstBit;
97 uint16_t I2S_Standard;
100 uint16_t I2S_DataFormat;
103 uint16_t I2S_MCLKOutput;
106 uint32_t I2S_AudioFreq;
119 #define IS_SPI_ALL_PERIPH(PERIPH) (((PERIPH) == SPI1) || \
120 ((PERIPH) == SPI2) || \
123 #define IS_SPI_ALL_PERIPH_EXT(PERIPH) (((PERIPH) == SPI1) || \
124 ((PERIPH) == SPI2) || \
125 ((PERIPH) == SPI3) || \
126 ((PERIPH) == I2S2ext) || \
127 ((PERIPH) == I2S3ext))
129 #define IS_SPI_23_PERIPH(PERIPH) (((PERIPH) == SPI2) || \
132 #define IS_SPI_23_PERIPH_EXT(PERIPH) (((PERIPH) == SPI2) || \
133 ((PERIPH) == SPI3) || \
134 ((PERIPH) == I2S2ext) || \
135 ((PERIPH) == I2S3ext))
137 #define IS_I2S_EXT_PERIPH(PERIPH) (((PERIPH) == I2S2ext) || \
138 ((PERIPH) == I2S3ext))
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))
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))
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))
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))
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))
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))
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))
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))
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))
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))
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))
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))
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)
331 #define IS_I2S_AUDIO_FREQ(FREQ) ((((FREQ) >= I2S_AudioFreq_8k) && \
332 ((FREQ) <= I2S_AudioFreq_192k)) || \
333 ((FREQ) == I2S_AudioFreq_Default))
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))
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))
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))
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))
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))
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)
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))
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)
414 #define IS_SPI_I2S_CLEAR_IT(IT) (((IT) == SPI_IT_CRCERR))
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))
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)
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))
452 #define IS_SPI_CRC_POLYNOMIAL(POLYNOMIAL) ((POLYNOMIAL) >= 0x1)
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
499 void SPI_Cmd(SPI_TypeDef* SPIx, FunctionalState NewState);
500 void I2S_Cmd(SPI_TypeDef* SPIx, FunctionalState NewState);
505 void SPI_TIModeCmd(SPI_TypeDef* SPIx, FunctionalState NewState);
516 uint16_t
SPI_GetCRC(SPI_TypeDef* SPIx, uint8_t SPI_CRC);
520 void SPI_I2S_DMACmd(SPI_TypeDef* SPIx, uint16_t SPI_I2S_DMAReq, FunctionalState NewState);
523 void SPI_I2S_ITConfig(SPI_TypeDef* SPIx, uint8_t SPI_I2S_IT, FunctionalState NewState);