A bundled STM32F10x Std Periph and CMSIS library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

stm32f10x_spi.h 17 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. /**
  2. ******************************************************************************
  3. * @file stm32f10x_spi.h
  4. * @author MCD Application Team
  5. * @version V3.5.0
  6. * @date 11-March-2011
  7. * @brief This file contains all the functions prototypes for the SPI firmware
  8. * library.
  9. ******************************************************************************
  10. * @attention
  11. *
  12. * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  13. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  14. * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  15. * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  16. * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  17. * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  18. *
  19. * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
  20. ******************************************************************************
  21. */
  22. /* Define to prevent recursive inclusion -------------------------------------*/
  23. #ifndef __STM32F10x_SPI_H
  24. #define __STM32F10x_SPI_H
  25. #ifdef __cplusplus
  26. extern "C" {
  27. #endif
  28. /* Includes ------------------------------------------------------------------*/
  29. #include "stm32f10x.h"
  30. #include "stm32f10x_assert.h"
  31. /** @addtogroup STM32F10x_StdPeriph_Driver
  32. * @{
  33. */
  34. /** @addtogroup SPI
  35. * @{
  36. */
  37. /** @defgroup SPI_Exported_Types
  38. * @{
  39. */
  40. /**
  41. * @brief SPI Init structure definition
  42. */
  43. typedef struct
  44. {
  45. uint16_t SPI_Direction; /*!< Specifies the SPI unidirectional or bidirectional data mode.
  46. This parameter can be a value of @ref SPI_data_direction */
  47. uint16_t SPI_Mode; /*!< Specifies the SPI operating mode.
  48. This parameter can be a value of @ref SPI_mode */
  49. uint16_t SPI_DataSize; /*!< Specifies the SPI data size.
  50. This parameter can be a value of @ref SPI_data_size */
  51. uint16_t SPI_CPOL; /*!< Specifies the serial clock steady state.
  52. This parameter can be a value of @ref SPI_Clock_Polarity */
  53. uint16_t SPI_CPHA; /*!< Specifies the clock active edge for the bit capture.
  54. This parameter can be a value of @ref SPI_Clock_Phase */
  55. uint16_t SPI_NSS; /*!< Specifies whether the NSS signal is managed by
  56. hardware (NSS pin) or by software using the SSI bit.
  57. This parameter can be a value of @ref SPI_Slave_Select_management */
  58. uint16_t SPI_BaudRatePrescaler; /*!< Specifies the Baud Rate prescaler value which will be
  59. used to configure the transmit and receive SCK clock.
  60. This parameter can be a value of @ref SPI_BaudRate_Prescaler.
  61. @note The communication clock is derived from the master
  62. clock. The slave clock does not need to be set. */
  63. uint16_t SPI_FirstBit; /*!< Specifies whether data transfers start from MSB or LSB bit.
  64. This parameter can be a value of @ref SPI_MSB_LSB_transmission */
  65. uint16_t SPI_CRCPolynomial; /*!< Specifies the polynomial used for the CRC calculation. */
  66. }SPI_InitTypeDef;
  67. /**
  68. * @brief I2S Init structure definition
  69. */
  70. typedef struct
  71. {
  72. uint16_t I2S_Mode; /*!< Specifies the I2S operating mode.
  73. This parameter can be a value of @ref I2S_Mode */
  74. uint16_t I2S_Standard; /*!< Specifies the standard used for the I2S communication.
  75. This parameter can be a value of @ref I2S_Standard */
  76. uint16_t I2S_DataFormat; /*!< Specifies the data format for the I2S communication.
  77. This parameter can be a value of @ref I2S_Data_Format */
  78. uint16_t I2S_MCLKOutput; /*!< Specifies whether the I2S MCLK output is enabled or not.
  79. This parameter can be a value of @ref I2S_MCLK_Output */
  80. uint32_t I2S_AudioFreq; /*!< Specifies the frequency selected for the I2S communication.
  81. This parameter can be a value of @ref I2S_Audio_Frequency */
  82. uint16_t I2S_CPOL; /*!< Specifies the idle state of the I2S clock.
  83. This parameter can be a value of @ref I2S_Clock_Polarity */
  84. }I2S_InitTypeDef;
  85. /**
  86. * @}
  87. */
  88. /** @defgroup SPI_Exported_Constants
  89. * @{
  90. */
  91. #define IS_SPI_ALL_PERIPH(PERIPH) (((PERIPH) == SPI1) || \
  92. ((PERIPH) == SPI2) || \
  93. ((PERIPH) == SPI3))
  94. #define IS_SPI_23_PERIPH(PERIPH) (((PERIPH) == SPI2) || \
  95. ((PERIPH) == SPI3))
  96. /** @defgroup SPI_data_direction
  97. * @{
  98. */
  99. #define SPI_Direction_2Lines_FullDuplex ((uint16_t)0x0000)
  100. #define SPI_Direction_2Lines_RxOnly ((uint16_t)0x0400)
  101. #define SPI_Direction_1Line_Rx ((uint16_t)0x8000)
  102. #define SPI_Direction_1Line_Tx ((uint16_t)0xC000)
  103. #define IS_SPI_DIRECTION_MODE(MODE) (((MODE) == SPI_Direction_2Lines_FullDuplex) || \
  104. ((MODE) == SPI_Direction_2Lines_RxOnly) || \
  105. ((MODE) == SPI_Direction_1Line_Rx) || \
  106. ((MODE) == SPI_Direction_1Line_Tx))
  107. /**
  108. * @}
  109. */
  110. /** @defgroup SPI_mode
  111. * @{
  112. */
  113. #define SPI_Mode_Master ((uint16_t)0x0104)
  114. #define SPI_Mode_Slave ((uint16_t)0x0000)
  115. #define IS_SPI_MODE(MODE) (((MODE) == SPI_Mode_Master) || \
  116. ((MODE) == SPI_Mode_Slave))
  117. /**
  118. * @}
  119. */
  120. /** @defgroup SPI_data_size
  121. * @{
  122. */
  123. #define SPI_DataSize_16b ((uint16_t)0x0800)
  124. #define SPI_DataSize_8b ((uint16_t)0x0000)
  125. #define IS_SPI_DATASIZE(DATASIZE) (((DATASIZE) == SPI_DataSize_16b) || \
  126. ((DATASIZE) == SPI_DataSize_8b))
  127. /**
  128. * @}
  129. */
  130. /** @defgroup SPI_Clock_Polarity
  131. * @{
  132. */
  133. #define SPI_CPOL_Low ((uint16_t)0x0000)
  134. #define SPI_CPOL_High ((uint16_t)0x0002)
  135. #define IS_SPI_CPOL(CPOL) (((CPOL) == SPI_CPOL_Low) || \
  136. ((CPOL) == SPI_CPOL_High))
  137. /**
  138. * @}
  139. */
  140. /** @defgroup SPI_Clock_Phase
  141. * @{
  142. */
  143. #define SPI_CPHA_1Edge ((uint16_t)0x0000)
  144. #define SPI_CPHA_2Edge ((uint16_t)0x0001)
  145. #define IS_SPI_CPHA(CPHA) (((CPHA) == SPI_CPHA_1Edge) || \
  146. ((CPHA) == SPI_CPHA_2Edge))
  147. /**
  148. * @}
  149. */
  150. /** @defgroup SPI_Slave_Select_management
  151. * @{
  152. */
  153. #define SPI_NSS_Soft ((uint16_t)0x0200)
  154. #define SPI_NSS_Hard ((uint16_t)0x0000)
  155. #define IS_SPI_NSS(NSS) (((NSS) == SPI_NSS_Soft) || \
  156. ((NSS) == SPI_NSS_Hard))
  157. /**
  158. * @}
  159. */
  160. /** @defgroup SPI_BaudRate_Prescaler
  161. * @{
  162. */
  163. #define SPI_BaudRatePrescaler_2 ((uint16_t)0x0000)
  164. #define SPI_BaudRatePrescaler_4 ((uint16_t)0x0008)
  165. #define SPI_BaudRatePrescaler_8 ((uint16_t)0x0010)
  166. #define SPI_BaudRatePrescaler_16 ((uint16_t)0x0018)
  167. #define SPI_BaudRatePrescaler_32 ((uint16_t)0x0020)
  168. #define SPI_BaudRatePrescaler_64 ((uint16_t)0x0028)
  169. #define SPI_BaudRatePrescaler_128 ((uint16_t)0x0030)
  170. #define SPI_BaudRatePrescaler_256 ((uint16_t)0x0038)
  171. #define IS_SPI_BAUDRATE_PRESCALER(PRESCALER) (((PRESCALER) == SPI_BaudRatePrescaler_2) || \
  172. ((PRESCALER) == SPI_BaudRatePrescaler_4) || \
  173. ((PRESCALER) == SPI_BaudRatePrescaler_8) || \
  174. ((PRESCALER) == SPI_BaudRatePrescaler_16) || \
  175. ((PRESCALER) == SPI_BaudRatePrescaler_32) || \
  176. ((PRESCALER) == SPI_BaudRatePrescaler_64) || \
  177. ((PRESCALER) == SPI_BaudRatePrescaler_128) || \
  178. ((PRESCALER) == SPI_BaudRatePrescaler_256))
  179. /**
  180. * @}
  181. */
  182. /** @defgroup SPI_MSB_LSB_transmission
  183. * @{
  184. */
  185. #define SPI_FirstBit_MSB ((uint16_t)0x0000)
  186. #define SPI_FirstBit_LSB ((uint16_t)0x0080)
  187. #define IS_SPI_FIRST_BIT(BIT) (((BIT) == SPI_FirstBit_MSB) || \
  188. ((BIT) == SPI_FirstBit_LSB))
  189. /**
  190. * @}
  191. */
  192. /** @defgroup I2S_Mode
  193. * @{
  194. */
  195. #define I2S_Mode_SlaveTx ((uint16_t)0x0000)
  196. #define I2S_Mode_SlaveRx ((uint16_t)0x0100)
  197. #define I2S_Mode_MasterTx ((uint16_t)0x0200)
  198. #define I2S_Mode_MasterRx ((uint16_t)0x0300)
  199. #define IS_I2S_MODE(MODE) (((MODE) == I2S_Mode_SlaveTx) || \
  200. ((MODE) == I2S_Mode_SlaveRx) || \
  201. ((MODE) == I2S_Mode_MasterTx) || \
  202. ((MODE) == I2S_Mode_MasterRx) )
  203. /**
  204. * @}
  205. */
  206. /** @defgroup I2S_Standard
  207. * @{
  208. */
  209. #define I2S_Standard_Phillips ((uint16_t)0x0000)
  210. #define I2S_Standard_MSB ((uint16_t)0x0010)
  211. #define I2S_Standard_LSB ((uint16_t)0x0020)
  212. #define I2S_Standard_PCMShort ((uint16_t)0x0030)
  213. #define I2S_Standard_PCMLong ((uint16_t)0x00B0)
  214. #define IS_I2S_STANDARD(STANDARD) (((STANDARD) == I2S_Standard_Phillips) || \
  215. ((STANDARD) == I2S_Standard_MSB) || \
  216. ((STANDARD) == I2S_Standard_LSB) || \
  217. ((STANDARD) == I2S_Standard_PCMShort) || \
  218. ((STANDARD) == I2S_Standard_PCMLong))
  219. /**
  220. * @}
  221. */
  222. /** @defgroup I2S_Data_Format
  223. * @{
  224. */
  225. #define I2S_DataFormat_16b ((uint16_t)0x0000)
  226. #define I2S_DataFormat_16bextended ((uint16_t)0x0001)
  227. #define I2S_DataFormat_24b ((uint16_t)0x0003)
  228. #define I2S_DataFormat_32b ((uint16_t)0x0005)
  229. #define IS_I2S_DATA_FORMAT(FORMAT) (((FORMAT) == I2S_DataFormat_16b) || \
  230. ((FORMAT) == I2S_DataFormat_16bextended) || \
  231. ((FORMAT) == I2S_DataFormat_24b) || \
  232. ((FORMAT) == I2S_DataFormat_32b))
  233. /**
  234. * @}
  235. */
  236. /** @defgroup I2S_MCLK_Output
  237. * @{
  238. */
  239. #define I2S_MCLKOutput_Enable ((uint16_t)0x0200)
  240. #define I2S_MCLKOutput_Disable ((uint16_t)0x0000)
  241. #define IS_I2S_MCLK_OUTPUT(OUTPUT) (((OUTPUT) == I2S_MCLKOutput_Enable) || \
  242. ((OUTPUT) == I2S_MCLKOutput_Disable))
  243. /**
  244. * @}
  245. */
  246. /** @defgroup I2S_Audio_Frequency
  247. * @{
  248. */
  249. #define I2S_AudioFreq_192k ((uint32_t)192000)
  250. #define I2S_AudioFreq_96k ((uint32_t)96000)
  251. #define I2S_AudioFreq_48k ((uint32_t)48000)
  252. #define I2S_AudioFreq_44k ((uint32_t)44100)
  253. #define I2S_AudioFreq_32k ((uint32_t)32000)
  254. #define I2S_AudioFreq_22k ((uint32_t)22050)
  255. #define I2S_AudioFreq_16k ((uint32_t)16000)
  256. #define I2S_AudioFreq_11k ((uint32_t)11025)
  257. #define I2S_AudioFreq_8k ((uint32_t)8000)
  258. #define I2S_AudioFreq_Default ((uint32_t)2)
  259. #define IS_I2S_AUDIO_FREQ(FREQ) ((((FREQ) >= I2S_AudioFreq_8k) && \
  260. ((FREQ) <= I2S_AudioFreq_192k)) || \
  261. ((FREQ) == I2S_AudioFreq_Default))
  262. /**
  263. * @}
  264. */
  265. /** @defgroup I2S_Clock_Polarity
  266. * @{
  267. */
  268. #define I2S_CPOL_Low ((uint16_t)0x0000)
  269. #define I2S_CPOL_High ((uint16_t)0x0008)
  270. #define IS_I2S_CPOL(CPOL) (((CPOL) == I2S_CPOL_Low) || \
  271. ((CPOL) == I2S_CPOL_High))
  272. /**
  273. * @}
  274. */
  275. /** @defgroup SPI_I2S_DMA_transfer_requests
  276. * @{
  277. */
  278. #define SPI_I2S_DMAReq_Tx ((uint16_t)0x0002)
  279. #define SPI_I2S_DMAReq_Rx ((uint16_t)0x0001)
  280. #define IS_SPI_I2S_DMAREQ(DMAREQ) ((((DMAREQ) & (uint16_t)0xFFFC) == 0x00) && ((DMAREQ) != 0x00))
  281. /**
  282. * @}
  283. */
  284. /** @defgroup SPI_NSS_internal_software_management
  285. * @{
  286. */
  287. #define SPI_NSSInternalSoft_Set ((uint16_t)0x0100)
  288. #define SPI_NSSInternalSoft_Reset ((uint16_t)0xFEFF)
  289. #define IS_SPI_NSS_INTERNAL(INTERNAL) (((INTERNAL) == SPI_NSSInternalSoft_Set) || \
  290. ((INTERNAL) == SPI_NSSInternalSoft_Reset))
  291. /**
  292. * @}
  293. */
  294. /** @defgroup SPI_CRC_Transmit_Receive
  295. * @{
  296. */
  297. #define SPI_CRC_Tx ((uint8_t)0x00)
  298. #define SPI_CRC_Rx ((uint8_t)0x01)
  299. #define IS_SPI_CRC(CRC) (((CRC) == SPI_CRC_Tx) || ((CRC) == SPI_CRC_Rx))
  300. /**
  301. * @}
  302. */
  303. /** @defgroup SPI_direction_transmit_receive
  304. * @{
  305. */
  306. #define SPI_Direction_Rx ((uint16_t)0xBFFF)
  307. #define SPI_Direction_Tx ((uint16_t)0x4000)
  308. #define IS_SPI_DIRECTION(DIRECTION) (((DIRECTION) == SPI_Direction_Rx) || \
  309. ((DIRECTION) == SPI_Direction_Tx))
  310. /**
  311. * @}
  312. */
  313. /** @defgroup SPI_I2S_interrupts_definition
  314. * @{
  315. */
  316. #define SPI_I2S_IT_TXE ((uint8_t)0x71)
  317. #define SPI_I2S_IT_RXNE ((uint8_t)0x60)
  318. #define SPI_I2S_IT_ERR ((uint8_t)0x50)
  319. #define IS_SPI_I2S_CONFIG_IT(IT) (((IT) == SPI_I2S_IT_TXE) || \
  320. ((IT) == SPI_I2S_IT_RXNE) || \
  321. ((IT) == SPI_I2S_IT_ERR))
  322. #define SPI_I2S_IT_OVR ((uint8_t)0x56)
  323. #define SPI_IT_MODF ((uint8_t)0x55)
  324. #define SPI_IT_CRCERR ((uint8_t)0x54)
  325. #define I2S_IT_UDR ((uint8_t)0x53)
  326. #define IS_SPI_I2S_CLEAR_IT(IT) (((IT) == SPI_IT_CRCERR))
  327. #define IS_SPI_I2S_GET_IT(IT) (((IT) == SPI_I2S_IT_RXNE) || ((IT) == SPI_I2S_IT_TXE) || \
  328. ((IT) == I2S_IT_UDR) || ((IT) == SPI_IT_CRCERR) || \
  329. ((IT) == SPI_IT_MODF) || ((IT) == SPI_I2S_IT_OVR))
  330. /**
  331. * @}
  332. */
  333. /** @defgroup SPI_I2S_flags_definition
  334. * @{
  335. */
  336. #define SPI_I2S_FLAG_RXNE ((uint16_t)0x0001)
  337. #define SPI_I2S_FLAG_TXE ((uint16_t)0x0002)
  338. #define I2S_FLAG_CHSIDE ((uint16_t)0x0004)
  339. #define I2S_FLAG_UDR ((uint16_t)0x0008)
  340. #define SPI_FLAG_CRCERR ((uint16_t)0x0010)
  341. #define SPI_FLAG_MODF ((uint16_t)0x0020)
  342. #define SPI_I2S_FLAG_OVR ((uint16_t)0x0040)
  343. #define SPI_I2S_FLAG_BSY ((uint16_t)0x0080)
  344. #define IS_SPI_I2S_CLEAR_FLAG(FLAG) (((FLAG) == SPI_FLAG_CRCERR))
  345. #define IS_SPI_I2S_GET_FLAG(FLAG) (((FLAG) == SPI_I2S_FLAG_BSY) || ((FLAG) == SPI_I2S_FLAG_OVR) || \
  346. ((FLAG) == SPI_FLAG_MODF) || ((FLAG) == SPI_FLAG_CRCERR) || \
  347. ((FLAG) == I2S_FLAG_UDR) || ((FLAG) == I2S_FLAG_CHSIDE) || \
  348. ((FLAG) == SPI_I2S_FLAG_TXE) || ((FLAG) == SPI_I2S_FLAG_RXNE))
  349. /**
  350. * @}
  351. */
  352. /** @defgroup SPI_CRC_polynomial
  353. * @{
  354. */
  355. #define IS_SPI_CRC_POLYNOMIAL(POLYNOMIAL) ((POLYNOMIAL) >= 0x1)
  356. /**
  357. * @}
  358. */
  359. /**
  360. * @}
  361. */
  362. /** @defgroup SPI_Exported_Macros
  363. * @{
  364. */
  365. /**
  366. * @}
  367. */
  368. /** @defgroup SPI_Exported_Functions
  369. * @{
  370. */
  371. void SPI_I2S_DeInit(SPI_TypeDef* SPIx);
  372. void SPI_Init(SPI_TypeDef* SPIx, SPI_InitTypeDef* SPI_InitStruct);
  373. void I2S_Init(SPI_TypeDef* SPIx, I2S_InitTypeDef* I2S_InitStruct);
  374. void SPI_StructInit(SPI_InitTypeDef* SPI_InitStruct);
  375. void I2S_StructInit(I2S_InitTypeDef* I2S_InitStruct);
  376. void SPI_Cmd(SPI_TypeDef* SPIx, FunctionalState NewState);
  377. void I2S_Cmd(SPI_TypeDef* SPIx, FunctionalState NewState);
  378. void SPI_I2S_ITConfig(SPI_TypeDef* SPIx, uint8_t SPI_I2S_IT, FunctionalState NewState);
  379. void SPI_I2S_DMACmd(SPI_TypeDef* SPIx, uint16_t SPI_I2S_DMAReq, FunctionalState NewState);
  380. void SPI_I2S_SendData(SPI_TypeDef* SPIx, uint16_t Data);
  381. uint16_t SPI_I2S_ReceiveData(SPI_TypeDef* SPIx);
  382. void SPI_NSSInternalSoftwareConfig(SPI_TypeDef* SPIx, uint16_t SPI_NSSInternalSoft);
  383. void SPI_SSOutputCmd(SPI_TypeDef* SPIx, FunctionalState NewState);
  384. void SPI_DataSizeConfig(SPI_TypeDef* SPIx, uint16_t SPI_DataSize);
  385. void SPI_TransmitCRC(SPI_TypeDef* SPIx);
  386. void SPI_CalculateCRC(SPI_TypeDef* SPIx, FunctionalState NewState);
  387. uint16_t SPI_GetCRC(SPI_TypeDef* SPIx, uint8_t SPI_CRC);
  388. uint16_t SPI_GetCRCPolynomial(SPI_TypeDef* SPIx);
  389. void SPI_BiDirectionalLineConfig(SPI_TypeDef* SPIx, uint16_t SPI_Direction);
  390. FlagStatus SPI_I2S_GetFlagStatus(SPI_TypeDef* SPIx, uint16_t SPI_I2S_FLAG);
  391. void SPI_I2S_ClearFlag(SPI_TypeDef* SPIx, uint16_t SPI_I2S_FLAG);
  392. ITStatus SPI_I2S_GetITStatus(SPI_TypeDef* SPIx, uint8_t SPI_I2S_IT);
  393. void SPI_I2S_ClearITPendingBit(SPI_TypeDef* SPIx, uint8_t SPI_I2S_IT);
  394. #ifdef __cplusplus
  395. }
  396. #endif
  397. #endif /*__STM32F10x_SPI_H */
  398. /**
  399. * @}
  400. */
  401. /**
  402. * @}
  403. */
  404. /**
  405. * @}
  406. */
  407. /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/