stm32f10x_spi.c 30 KB

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