stm32f10x_spi.c 34 KB

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