stm32f10x_spi.c 29 KB

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