n32g45x_spi.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  1. /*****************************************************************************
  2. * Copyright (c) 2019, Nations Technologies Inc.
  3. *
  4. * All rights reserved.
  5. * ****************************************************************************
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. *
  10. * - Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the disclaimer below.
  12. *
  13. * Nations' name may not be used to endorse or promote products derived from
  14. * this software without specific prior written permission.
  15. *
  16. * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
  17. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  19. * DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  22. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  25. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. * ****************************************************************************/
  27. /**
  28. * @file n32g45x_spi.c
  29. * @author Nations
  30. * @version v1.0.0
  31. *
  32. * @copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
  33. */
  34. #include "n32g45x_spi.h"
  35. #include "n32g45x_rcc.h"
  36. /** @addtogroup N32G45X_StdPeriph_Driver
  37. * @{
  38. */
  39. /** @addtogroup SPI
  40. * @brief SPI driver modules
  41. * @{
  42. */
  43. /** @addtogroup SPI_Private_TypesDefinitions
  44. * @{
  45. */
  46. /**
  47. * @}
  48. */
  49. /** @addtogroup SPI_Private_Defines
  50. * @{
  51. */
  52. /* SPI SPE mask */
  53. #define CTRL1_SPIEN_ENABLE ((uint16_t)0x0040)
  54. #define CTRL1_SPIEN_DISABLE ((uint16_t)0xFFBF)
  55. /* I2S I2SE mask */
  56. #define I2SCFG_I2SEN_ENABLE ((uint16_t)0x0400)
  57. #define I2SCFG_I2SEN_DISABLE ((uint16_t)0xFBFF)
  58. /* SPI CRCNext mask */
  59. #define CTRL1_CRCNEXT_ENABLE ((uint16_t)0x1000)
  60. /* SPI CRCEN mask */
  61. #define CTRL1_CRCEN_ENABLE ((uint16_t)0x2000)
  62. #define CTRL1_CRCEN_DISABLE ((uint16_t)0xDFFF)
  63. /* SPI SSOE mask */
  64. #define CTRL2_SSOEN_ENABLE ((uint16_t)0x0004)
  65. #define CTRL2_SSOEN_DISABLE ((uint16_t)0xFFFB)
  66. /* SPI registers Masks */
  67. #define CTRL1_CLR_MASK ((uint16_t)0x3040)
  68. #define I2SCFG_CLR_MASK ((uint16_t)0xF040)
  69. /* SPI or I2S mode selection masks */
  70. #define SPI_MODE_ENABLE ((uint16_t)0xF7FF)
  71. #define I2S_MODE_ENABLE ((uint16_t)0x0800)
  72. /* I2S clock source selection masks */
  73. #define I2S2_CLKSRC ((uint32_t)(0x00020000))
  74. #define I2S3_CLKSRC ((uint32_t)(0x00040000))
  75. #define I2S_MUL_MASK ((uint32_t)(0x0000F000))
  76. #define I2S_DIV_MASK ((uint32_t)(0x000000F0))
  77. /**
  78. * @}
  79. */
  80. /** @addtogroup SPI_Private_Macros
  81. * @{
  82. */
  83. /**
  84. * @}
  85. */
  86. /** @addtogroup SPI_Private_Variables
  87. * @{
  88. */
  89. /**
  90. * @}
  91. */
  92. /** @addtogroup SPI_Private_FunctionPrototypes
  93. * @{
  94. */
  95. /**
  96. * @}
  97. */
  98. /** @addtogroup SPI_Private_Functions
  99. * @{
  100. */
  101. /**
  102. * @brief Deinitializes the SPIx peripheral registers to their default
  103. * reset values (Affects also the I2Ss).
  104. * @param SPIx where x can be 1, 2 or 3 to select the SPI peripheral.
  105. */
  106. void SPI_I2S_DeInit(SPI_Module* SPIx)
  107. {
  108. /* Check the parameters */
  109. assert_param(IS_SPI_PERIPH(SPIx));
  110. if (SPIx == SPI1)
  111. {
  112. /* Enable SPI1 reset state */
  113. RCC_EnableAPB2PeriphReset(RCC_APB2_PERIPH_SPI1, ENABLE);
  114. /* Release SPI1 from reset state */
  115. RCC_EnableAPB2PeriphReset(RCC_APB2_PERIPH_SPI1, DISABLE);
  116. }
  117. else if (SPIx == SPI2)
  118. {
  119. /* Enable SPI2 reset state */
  120. RCC_EnableAPB1PeriphReset(RCC_APB1_PERIPH_SPI2, ENABLE);
  121. /* Release SPI2 from reset state */
  122. RCC_EnableAPB1PeriphReset(RCC_APB1_PERIPH_SPI2, DISABLE);
  123. }
  124. else
  125. {
  126. if (SPIx == SPI3)
  127. {
  128. /* Enable SPI3 reset state */
  129. RCC_EnableAPB1PeriphReset(RCC_APB1_PERIPH_SPI3, ENABLE);
  130. /* Release SPI3 from reset state */
  131. RCC_EnableAPB1PeriphReset(RCC_APB1_PERIPH_SPI3, DISABLE);
  132. }
  133. }
  134. }
  135. /**
  136. * @brief Initializes the SPIx peripheral according to the specified
  137. * parameters in the SPI_InitStruct.
  138. * @param SPIx where x can be 1, 2 or 3 to select the SPI peripheral.
  139. * @param SPI_InitStruct pointer to a SPI_InitType structure that
  140. * contains the configuration information for the specified SPI peripheral.
  141. */
  142. void SPI_Init(SPI_Module* SPIx, SPI_InitType* SPI_InitStruct)
  143. {
  144. uint16_t tmpregister = 0;
  145. /* check the parameters */
  146. assert_param(IS_SPI_PERIPH(SPIx));
  147. /* Check the SPI parameters */
  148. assert_param(IS_SPI_DIR_MODE(SPI_InitStruct->DataDirection));
  149. assert_param(IS_SPI_MODE(SPI_InitStruct->SpiMode));
  150. assert_param(IS_SPI_DATASIZE(SPI_InitStruct->DataLen));
  151. assert_param(IS_SPI_CLKPOL(SPI_InitStruct->CLKPOL));
  152. assert_param(IS_SPI_CLKPHA(SPI_InitStruct->CLKPHA));
  153. assert_param(IS_SPI_NSS(SPI_InitStruct->NSS));
  154. assert_param(IS_SPI_BR_PRESCALER(SPI_InitStruct->BaudRatePres));
  155. assert_param(IS_SPI_FIRST_BIT(SPI_InitStruct->FirstBit));
  156. assert_param(IS_SPI_CRC_POLYNOMIAL(SPI_InitStruct->CRCPoly));
  157. /*---------------------------- SPIx CTRL1 Configuration ------------------------*/
  158. /* Get the SPIx CTRL1 value */
  159. tmpregister = SPIx->CTRL1;
  160. /* Clear BIDIMode, BIDIOE, RxONLY, SSM, SSI, LSBFirst, BR, MSTR, CPOL and CPHA bits */
  161. tmpregister &= CTRL1_CLR_MASK;
  162. /* Configure SPIx: direction, NSS management, first transmitted bit, BaudRate prescaler
  163. master/salve mode, CPOL and CPHA */
  164. /* Set BIDImode, BIDIOE and RxONLY bits according to DataDirection value */
  165. /* Set SSM, SSI and MSTR bits according to SpiMode and NSS values */
  166. /* Set LSBFirst bit according to FirstBit value */
  167. /* Set BR bits according to BaudRatePres value */
  168. /* Set CPOL bit according to CLKPOL value */
  169. /* Set CPHA bit according to CLKPHA value */
  170. tmpregister |= (uint16_t)((uint32_t)SPI_InitStruct->DataDirection | SPI_InitStruct->SpiMode
  171. | SPI_InitStruct->DataLen | SPI_InitStruct->CLKPOL | SPI_InitStruct->CLKPHA
  172. | SPI_InitStruct->NSS | SPI_InitStruct->BaudRatePres | SPI_InitStruct->FirstBit);
  173. /* Write to SPIx CTRL1 */
  174. SPIx->CTRL1 = tmpregister;
  175. /* Activate the SPI mode (Reset I2SMOD bit in I2SCFG register) */
  176. SPIx->I2SCFG &= SPI_MODE_ENABLE;
  177. /*---------------------------- SPIx CRCPOLY Configuration --------------------*/
  178. /* Write to SPIx CRCPOLY */
  179. SPIx->CRCPOLY = SPI_InitStruct->CRCPoly;
  180. }
  181. /**
  182. * @brief Initializes the SPIx peripheral according to the specified
  183. * parameters in the I2S_InitStruct.
  184. * @param SPIx where x can be 2 or 3 to select the SPI peripheral
  185. * (configured in I2S mode).
  186. * @param I2S_InitStruct pointer to an I2S_InitType structure that
  187. * contains the configuration information for the specified SPI peripheral
  188. * configured in I2S mode.
  189. * @note
  190. * The function calculates the optimal prescaler needed to obtain the most
  191. * accurate audio frequency (depending on the I2S clock source, the PLL values
  192. * and the product configuration). But in case the prescaler value is greater
  193. * than 511, the default value (0x02) will be configured instead. *
  194. */
  195. void I2S_Init(SPI_Module* SPIx, I2S_InitType* I2S_InitStruct)
  196. {
  197. uint16_t tmpregister = 0, i2sdiv = 2, i2sodd = 0, packetlength = 1;
  198. uint32_t tmp = 0;
  199. RCC_ClocksType RCC_Clocks;
  200. uint32_t sourceclock = 0;
  201. /* Check the I2S parameters */
  202. assert_param(IS_SPI_2OR3_PERIPH(SPIx));
  203. assert_param(IS_I2S_MODE(I2S_InitStruct->I2sMode));
  204. assert_param(IS_I2S_STANDARD(I2S_InitStruct->Standard));
  205. assert_param(IS_I2S_DATA_FMT(I2S_InitStruct->DataFormat));
  206. assert_param(IS_I2S_MCLK_ENABLE(I2S_InitStruct->MCLKEnable));
  207. assert_param(IS_I2S_AUDIO_FREQ(I2S_InitStruct->AudioFrequency));
  208. assert_param(IS_I2S_CLKPOL(I2S_InitStruct->CLKPOL));
  209. /*----------------------- SPIx I2SCFG & I2SPREDIV Configuration -----------------*/
  210. /* Clear I2SMOD, I2SE, MODCFG, PCMSYNC, STDSEL, CKPOL, TDATLEN and CHLEN bits */
  211. SPIx->I2SCFG &= I2SCFG_CLR_MASK;
  212. SPIx->I2SPREDIV = 0x0002;
  213. /* Get the I2SCFG register value */
  214. tmpregister = SPIx->I2SCFG;
  215. /* If the default value has to be written, reinitialize i2sdiv and i2sodd*/
  216. if (I2S_InitStruct->AudioFrequency == I2S_AUDIO_FREQ_DEFAULT)
  217. {
  218. i2sodd = (uint16_t)0;
  219. i2sdiv = (uint16_t)2;
  220. }
  221. /* If the requested audio frequency is not the default, compute the prescaler */
  222. else
  223. {
  224. /* Check the frame length (For the Prescaler computing) */
  225. if (I2S_InitStruct->DataFormat == I2S_DATA_FMT_16BITS)
  226. {
  227. /* Packet length is 16 bits */
  228. packetlength = 1;
  229. }
  230. else
  231. {
  232. /* Packet length is 32 bits */
  233. packetlength = 2;
  234. }
  235. /* Get the I2S clock source mask depending on the peripheral number */
  236. if (((uint32_t)SPIx) == SPI2_BASE)
  237. {
  238. /* The mask is relative to I2S2 */
  239. tmp = I2S2_CLKSRC;
  240. }
  241. else
  242. {
  243. /* The mask is relative to I2S3 */
  244. tmp = I2S3_CLKSRC;
  245. }
  246. /* I2S Clock source is System clock: Get System Clock frequency */
  247. RCC_GetClocksFreqValue(&RCC_Clocks);
  248. /* Get the source clock value: based on System Clock value */
  249. sourceclock = RCC_Clocks.SysclkFreq;
  250. /* Compute the Real divider depending on the MCLK output state with a floating point */
  251. if (I2S_InitStruct->MCLKEnable == I2S_MCLK_ENABLE)
  252. {
  253. /* MCLK output is enabled */
  254. tmp = (uint16_t)(((((sourceclock / 256) * 10) / I2S_InitStruct->AudioFrequency)) + 5);
  255. }
  256. else
  257. {
  258. /* MCLK output is disabled */
  259. tmp = (uint16_t)(((((sourceclock / (32 * packetlength)) * 10) / I2S_InitStruct->AudioFrequency)) + 5);
  260. }
  261. /* Remove the floating point */
  262. tmp = tmp / 10;
  263. /* Check the parity of the divider */
  264. i2sodd = (uint16_t)(tmp & (uint16_t)0x0001);
  265. /* Compute the i2sdiv prescaler */
  266. i2sdiv = (uint16_t)((tmp - i2sodd) / 2);
  267. /* Get the Mask for the Odd bit (SPI_I2SPREDIV[8]) register */
  268. i2sodd = (uint16_t)(i2sodd << 8);
  269. }
  270. /* Test if the divider is 1 or 0 or greater than 0xFF */
  271. if ((i2sdiv < 2) || (i2sdiv > 0xFF))
  272. {
  273. /* Set the default values */
  274. i2sdiv = 2;
  275. i2sodd = 0;
  276. }
  277. /* Write to SPIx I2SPREDIV register the computed value */
  278. SPIx->I2SPREDIV = (uint16_t)(i2sdiv | (uint16_t)(i2sodd | (uint16_t)I2S_InitStruct->MCLKEnable));
  279. /* Configure the I2S with the SPI_InitStruct values */
  280. tmpregister |= (uint16_t)(
  281. I2S_MODE_ENABLE
  282. | (uint16_t)(I2S_InitStruct->I2sMode
  283. | (uint16_t)(I2S_InitStruct->Standard
  284. | (uint16_t)(I2S_InitStruct->DataFormat | (uint16_t)I2S_InitStruct->CLKPOL))));
  285. /* Write to SPIx I2SCFG */
  286. SPIx->I2SCFG = tmpregister;
  287. }
  288. /**
  289. * @brief Fills each SPI_InitStruct member with its default value.
  290. * @param SPI_InitStruct pointer to a SPI_InitType structure which will be initialized.
  291. */
  292. void SPI_InitStruct(SPI_InitType* SPI_InitStruct)
  293. {
  294. /*--------------- Reset SPI init structure parameters values -----------------*/
  295. /* Initialize the DataDirection member */
  296. SPI_InitStruct->DataDirection = SPI_DIR_DOUBLELINE_FULLDUPLEX;
  297. /* initialize the SpiMode member */
  298. SPI_InitStruct->SpiMode = SPI_MODE_SLAVE;
  299. /* initialize the DataLen member */
  300. SPI_InitStruct->DataLen = SPI_DATA_SIZE_8BITS;
  301. /* Initialize the CLKPOL member */
  302. SPI_InitStruct->CLKPOL = SPI_CLKPOL_LOW;
  303. /* Initialize the CLKPHA member */
  304. SPI_InitStruct->CLKPHA = SPI_CLKPHA_FIRST_EDGE;
  305. /* Initialize the NSS member */
  306. SPI_InitStruct->NSS = SPI_NSS_HARD;
  307. /* Initialize the BaudRatePres member */
  308. SPI_InitStruct->BaudRatePres = SPI_BR_PRESCALER_2;
  309. /* Initialize the FirstBit member */
  310. SPI_InitStruct->FirstBit = SPI_FB_MSB;
  311. /* Initialize the CRCPoly member */
  312. SPI_InitStruct->CRCPoly = 7;
  313. }
  314. /**
  315. * @brief Fills each I2S_InitStruct member with its default value.
  316. * @param I2S_InitStruct pointer to a I2S_InitType structure which will be initialized.
  317. */
  318. void I2S_InitStruct(I2S_InitType* I2S_InitStruct)
  319. {
  320. /*--------------- Reset I2S init structure parameters values -----------------*/
  321. /* Initialize the I2sMode member */
  322. I2S_InitStruct->I2sMode = I2S_MODE_SlAVE_TX;
  323. /* Initialize the Standard member */
  324. I2S_InitStruct->Standard = I2S_STD_PHILLIPS;
  325. /* Initialize the DataFormat member */
  326. I2S_InitStruct->DataFormat = I2S_DATA_FMT_16BITS;
  327. /* Initialize the MCLKEnable member */
  328. I2S_InitStruct->MCLKEnable = I2S_MCLK_DISABLE;
  329. /* Initialize the AudioFrequency member */
  330. I2S_InitStruct->AudioFrequency = I2S_AUDIO_FREQ_DEFAULT;
  331. /* Initialize the CLKPOL member */
  332. I2S_InitStruct->CLKPOL = I2S_CLKPOL_LOW;
  333. }
  334. /**
  335. * @brief Enables or disables the specified SPI peripheral.
  336. * @param SPIx where x can be 1, 2 or 3 to select the SPI peripheral.
  337. * @param Cmd new state of the SPIx peripheral.
  338. * This parameter can be: ENABLE or DISABLE.
  339. */
  340. void SPI_Enable(SPI_Module* SPIx, FunctionalState Cmd)
  341. {
  342. /* Check the parameters */
  343. assert_param(IS_SPI_PERIPH(SPIx));
  344. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  345. if (Cmd != DISABLE)
  346. {
  347. /* Enable the selected SPI peripheral */
  348. SPIx->CTRL1 |= CTRL1_SPIEN_ENABLE;
  349. }
  350. else
  351. {
  352. /* Disable the selected SPI peripheral */
  353. SPIx->CTRL1 &= CTRL1_SPIEN_DISABLE;
  354. }
  355. }
  356. /**
  357. * @brief Enables or disables the specified SPI peripheral (in I2S mode).
  358. * @param SPIx where x can be 2 or 3 to select the SPI peripheral.
  359. * @param Cmd new state of the SPIx peripheral.
  360. * This parameter can be: ENABLE or DISABLE.
  361. */
  362. void I2S_Enable(SPI_Module* SPIx, FunctionalState Cmd)
  363. {
  364. /* Check the parameters */
  365. assert_param(IS_SPI_2OR3_PERIPH(SPIx));
  366. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  367. if (Cmd != DISABLE)
  368. {
  369. /* Enable the selected SPI peripheral (in I2S mode) */
  370. SPIx->I2SCFG |= I2SCFG_I2SEN_ENABLE;
  371. }
  372. else
  373. {
  374. /* Disable the selected SPI peripheral (in I2S mode) */
  375. SPIx->I2SCFG &= I2SCFG_I2SEN_DISABLE;
  376. }
  377. }
  378. /**
  379. * @brief Enables or disables the specified SPI/I2S interrupts.
  380. * @param SPIx where x can be
  381. * - 1, 2 or 3 in SPI mode
  382. * - 2 or 3 in I2S mode
  383. * @param SPI_I2S_IT specifies the SPI/I2S interrupt source to be enabled or disabled.
  384. * This parameter can be one of the following values:
  385. * @arg SPI_I2S_INT_TE Tx buffer empty interrupt mask
  386. * @arg SPI_I2S_INT_RNE Rx buffer not empty interrupt mask
  387. * @arg SPI_I2S_INT_ERR Error interrupt mask
  388. * @param Cmd new state of the specified SPI/I2S interrupt.
  389. * This parameter can be: ENABLE or DISABLE.
  390. */
  391. void SPI_I2S_EnableInt(SPI_Module* SPIx, uint8_t SPI_I2S_IT, FunctionalState Cmd)
  392. {
  393. uint16_t itpos = 0, itmask = 0;
  394. /* Check the parameters */
  395. assert_param(IS_SPI_PERIPH(SPIx));
  396. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  397. assert_param(IS_SPI_I2S_CONFIG_INT(SPI_I2S_IT));
  398. /* Get the SPI/I2S IT index */
  399. itpos = SPI_I2S_IT >> 4;
  400. /* Set the IT mask */
  401. itmask = (uint16_t)1 << (uint16_t)itpos;
  402. if (Cmd != DISABLE)
  403. {
  404. /* Enable the selected SPI/I2S interrupt */
  405. SPIx->CTRL2 |= itmask;
  406. }
  407. else
  408. {
  409. /* Disable the selected SPI/I2S interrupt */
  410. SPIx->CTRL2 &= (uint16_t)~itmask;
  411. }
  412. }
  413. /**
  414. * @brief Enables or disables the SPIx/I2Sx DMA interface.
  415. * @param SPIx where x can be
  416. * - 1, 2 or 3 in SPI mode
  417. * - 2 or 3 in I2S mode
  418. * @param SPI_I2S_DMAReq specifies the SPI/I2S DMA transfer request to be enabled or disabled.
  419. * This parameter can be any combination of the following values:
  420. * @arg SPI_I2S_DMA_TX Tx buffer DMA transfer request
  421. * @arg SPI_I2S_DMA_RX Rx buffer DMA transfer request
  422. * @param Cmd new state of the selected SPI/I2S DMA transfer request.
  423. * This parameter can be: ENABLE or DISABLE.
  424. */
  425. void SPI_I2S_EnableDma(SPI_Module* SPIx, uint16_t SPI_I2S_DMAReq, FunctionalState Cmd)
  426. {
  427. /* Check the parameters */
  428. assert_param(IS_SPI_PERIPH(SPIx));
  429. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  430. assert_param(IS_SPI_I2S_DMA(SPI_I2S_DMAReq));
  431. if (Cmd != DISABLE)
  432. {
  433. /* Enable the selected SPI/I2S DMA requests */
  434. SPIx->CTRL2 |= SPI_I2S_DMAReq;
  435. }
  436. else
  437. {
  438. /* Disable the selected SPI/I2S DMA requests */
  439. SPIx->CTRL2 &= (uint16_t)~SPI_I2S_DMAReq;
  440. }
  441. }
  442. /**
  443. * @brief Transmits a Data through the SPIx/I2Sx peripheral.
  444. * @param SPIx where x can be
  445. * - 1, 2 or 3 in SPI mode
  446. * - 2 or 3 in I2S mode
  447. * @param Data Data to be transmitted.
  448. */
  449. void SPI_I2S_TransmitData(SPI_Module* SPIx, uint16_t Data)
  450. {
  451. /* Check the parameters */
  452. assert_param(IS_SPI_PERIPH(SPIx));
  453. /* Write in the DAT register the data to be sent */
  454. SPIx->DAT = Data;
  455. }
  456. /**
  457. * @brief Returns the most recent received data by the SPIx/I2Sx peripheral.
  458. * @param SPIx where x can be
  459. * - 1, 2 or 3 in SPI mode
  460. * - 2 or 3 in I2S mode
  461. * @return The value of the received data.
  462. */
  463. uint16_t SPI_I2S_ReceiveData(SPI_Module* SPIx)
  464. {
  465. /* Check the parameters */
  466. assert_param(IS_SPI_PERIPH(SPIx));
  467. /* Return the data in the DAT register */
  468. return SPIx->DAT;
  469. }
  470. /**
  471. * @brief Configures internally by software the NSS pin for the selected SPI.
  472. * @param SPIx where x can be 1, 2 or 3 to select the SPI peripheral.
  473. * @param SPI_NSSInternalSoft specifies the SPI NSS internal state.
  474. * This parameter can be one of the following values:
  475. * @arg SPI_NSS_HIGH Set NSS pin internally
  476. * @arg SPI_NSS_LOW Reset NSS pin internally
  477. */
  478. void SPI_SetNssLevel(SPI_Module* SPIx, uint16_t SPI_NSSInternalSoft)
  479. {
  480. /* Check the parameters */
  481. assert_param(IS_SPI_PERIPH(SPIx));
  482. assert_param(IS_SPI_NSS_LEVEL(SPI_NSSInternalSoft));
  483. if (SPI_NSSInternalSoft != SPI_NSS_LOW)
  484. {
  485. /* Set NSS pin internally by software */
  486. SPIx->CTRL1 |= SPI_NSS_HIGH;
  487. }
  488. else
  489. {
  490. /* Reset NSS pin internally by software */
  491. SPIx->CTRL1 &= SPI_NSS_LOW;
  492. }
  493. }
  494. /**
  495. * @brief Enables or disables the SS output for the selected SPI.
  496. * @param SPIx where x can be 1, 2 or 3 to select the SPI peripheral.
  497. * @param Cmd new state of the SPIx SS output.
  498. * This parameter can be: ENABLE or DISABLE.
  499. */
  500. void SPI_SSOutputEnable(SPI_Module* SPIx, FunctionalState Cmd)
  501. {
  502. /* Check the parameters */
  503. assert_param(IS_SPI_PERIPH(SPIx));
  504. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  505. if (Cmd != DISABLE)
  506. {
  507. /* Enable the selected SPI SS output */
  508. SPIx->CTRL2 |= CTRL2_SSOEN_ENABLE;
  509. }
  510. else
  511. {
  512. /* Disable the selected SPI SS output */
  513. SPIx->CTRL2 &= CTRL2_SSOEN_DISABLE;
  514. }
  515. }
  516. /**
  517. * @brief Configures the data size for the selected SPI.
  518. * @param SPIx where x can be 1, 2 or 3 to select the SPI peripheral.
  519. * @param DataLen specifies the SPI data size.
  520. * This parameter can be one of the following values:
  521. * @arg SPI_DATA_SIZE_16BITS Set data frame format to 16bit
  522. * @arg SPI_DATA_SIZE_8BITS Set data frame format to 8bit
  523. */
  524. void SPI_ConfigDataLen(SPI_Module* SPIx, uint16_t DataLen)
  525. {
  526. /* Check the parameters */
  527. assert_param(IS_SPI_PERIPH(SPIx));
  528. assert_param(IS_SPI_DATASIZE(DataLen));
  529. /* Clear DFF bit */
  530. SPIx->CTRL1 &= (uint16_t)~SPI_DATA_SIZE_16BITS;
  531. /* Set new DFF bit value */
  532. SPIx->CTRL1 |= DataLen;
  533. }
  534. /**
  535. * @brief Transmit the SPIx CRC value.
  536. * @param SPIx where x can be 1, 2 or 3 to select the SPI peripheral.
  537. */
  538. void SPI_TransmitCrcNext(SPI_Module* SPIx)
  539. {
  540. /* Check the parameters */
  541. assert_param(IS_SPI_PERIPH(SPIx));
  542. /* Enable the selected SPI CRC transmission */
  543. SPIx->CTRL1 |= CTRL1_CRCNEXT_ENABLE;
  544. }
  545. /**
  546. * @brief Enables or disables the CRC value calculation of the transferred bytes.
  547. * @param SPIx where x can be 1, 2 or 3 to select the SPI peripheral.
  548. * @param Cmd new state of the SPIx CRC value calculation.
  549. * This parameter can be: ENABLE or DISABLE.
  550. */
  551. void SPI_EnableCalculateCrc(SPI_Module* SPIx, FunctionalState Cmd)
  552. {
  553. /* Check the parameters */
  554. assert_param(IS_SPI_PERIPH(SPIx));
  555. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  556. if (Cmd != DISABLE)
  557. {
  558. /* Enable the selected SPI CRC calculation */
  559. SPIx->CTRL1 |= CTRL1_CRCEN_ENABLE;
  560. }
  561. else
  562. {
  563. /* Disable the selected SPI CRC calculation */
  564. SPIx->CTRL1 &= CTRL1_CRCEN_DISABLE;
  565. }
  566. }
  567. /**
  568. * @brief Returns the transmit or the receive CRC register value for the specified SPI.
  569. * @param SPIx where x can be 1, 2 or 3 to select the SPI peripheral.
  570. * @param SPI_CRC specifies the CRC register to be read.
  571. * This parameter can be one of the following values:
  572. * @arg SPI_CRC_TX Selects Tx CRC register
  573. * @arg SPI_CRC_RX Selects Rx CRC register
  574. * @return The selected CRC register value..
  575. */
  576. uint16_t SPI_GetCRCDat(SPI_Module* SPIx, uint8_t SPI_CRC)
  577. {
  578. uint16_t crcreg = 0;
  579. /* Check the parameters */
  580. assert_param(IS_SPI_PERIPH(SPIx));
  581. assert_param(IS_SPI_CRC(SPI_CRC));
  582. if (SPI_CRC != SPI_CRC_RX)
  583. {
  584. /* Get the Tx CRC register */
  585. crcreg = SPIx->CRCTDAT;
  586. }
  587. else
  588. {
  589. /* Get the Rx CRC register */
  590. crcreg = SPIx->CRCRDAT;
  591. }
  592. /* Return the selected CRC register */
  593. return crcreg;
  594. }
  595. /**
  596. * @brief Returns the CRC Polynomial register value for the specified SPI.
  597. * @param SPIx where x can be 1, 2 or 3 to select the SPI peripheral.
  598. * @return The CRC Polynomial register value.
  599. */
  600. uint16_t SPI_GetCRCPoly(SPI_Module* SPIx)
  601. {
  602. /* Check the parameters */
  603. assert_param(IS_SPI_PERIPH(SPIx));
  604. /* Return the CRC polynomial register */
  605. return SPIx->CRCPOLY;
  606. }
  607. /**
  608. * @brief Selects the data transfer direction in bi-directional mode for the specified SPI.
  609. * @param SPIx where x can be 1, 2 or 3 to select the SPI peripheral.
  610. * @param DataDirection specifies the data transfer direction in bi-directional mode.
  611. * This parameter can be one of the following values:
  612. * @arg SPI_BIDIRECTION_TX Selects Tx transmission direction
  613. * @arg SPI_BIDIRECTION_RX Selects Rx receive direction
  614. */
  615. void SPI_ConfigBidirectionalMode(SPI_Module* SPIx, uint16_t DataDirection)
  616. {
  617. /* Check the parameters */
  618. assert_param(IS_SPI_PERIPH(SPIx));
  619. assert_param(IS_SPI_BIDIRECTION(DataDirection));
  620. if (DataDirection == SPI_BIDIRECTION_TX)
  621. {
  622. /* Set the Tx only mode */
  623. SPIx->CTRL1 |= SPI_BIDIRECTION_TX;
  624. }
  625. else
  626. {
  627. /* Set the Rx only mode */
  628. SPIx->CTRL1 &= SPI_BIDIRECTION_RX;
  629. }
  630. }
  631. /**
  632. * @brief Checks whether the specified SPI/I2S flag is set or not.
  633. * @param SPIx where x can be
  634. * - 1, 2 or 3 in SPI mode
  635. * - 2 or 3 in I2S mode
  636. * @param SPI_I2S_FLAG specifies the SPI/I2S flag to check.
  637. * This parameter can be one of the following values:
  638. * @arg SPI_I2S_TE_FLAG Transmit buffer empty flag.
  639. * @arg SPI_I2S_RNE_FLAG Receive buffer not empty flag.
  640. * @arg SPI_I2S_BUSY_FLAG Busy flag.
  641. * @arg SPI_I2S_OVER_FLAG Overrun flag.
  642. * @arg SPI_MODERR_FLAG Mode Fault flag.
  643. * @arg SPI_CRCERR_FLAG CRC Error flag.
  644. * @arg I2S_UNDER_FLAG Underrun Error flag.
  645. * @arg I2S_CHSIDE_FLAG Channel Side flag.
  646. * @return The new state of SPI_I2S_FLAG (SET or RESET).
  647. */
  648. FlagStatus SPI_I2S_GetStatus(SPI_Module* SPIx, uint16_t SPI_I2S_FLAG)
  649. {
  650. FlagStatus bitstatus = RESET;
  651. /* Check the parameters */
  652. assert_param(IS_SPI_PERIPH(SPIx));
  653. assert_param(IS_SPI_I2S_GET_FLAG(SPI_I2S_FLAG));
  654. /* Check the status of the specified SPI/I2S flag */
  655. if ((SPIx->STS & SPI_I2S_FLAG) != (uint16_t)RESET)
  656. {
  657. /* SPI_I2S_FLAG is set */
  658. bitstatus = SET;
  659. }
  660. else
  661. {
  662. /* SPI_I2S_FLAG is reset */
  663. bitstatus = RESET;
  664. }
  665. /* Return the SPI_I2S_FLAG status */
  666. return bitstatus;
  667. }
  668. /**
  669. * @brief Clears the SPIx CRC Error (CRCERR) flag.
  670. * @param SPIx where x can be
  671. * - 1, 2 or 3 in SPI mode
  672. * @param SPI_I2S_FLAG specifies the SPI flag to clear.
  673. * This function clears only CRCERR flag.
  674. * @note
  675. * - OVR (OverRun error) flag is cleared by software sequence: a read
  676. * operation to SPI_DAT register (SPI_I2S_ReceiveData()) followed by a read
  677. * operation to SPI_STS register (SPI_I2S_GetStatus()).
  678. * - UDR (UnderRun error) flag is cleared by a read operation to
  679. * SPI_STS register (SPI_I2S_GetStatus()).
  680. * - MODF (Mode Fault) flag is cleared by software sequence: a read/write
  681. * operation to SPI_STS register (SPI_I2S_GetStatus()) followed by a
  682. * write operation to SPI_CTRL1 register (SPI_Enable() to enable the SPI).
  683. */
  684. void SPI_I2S_ClrCRCErrFlag(SPI_Module* SPIx, uint16_t SPI_I2S_FLAG)
  685. {
  686. /* Check the parameters */
  687. assert_param(IS_SPI_PERIPH(SPIx));
  688. assert_param(IS_SPI_I2S_CLR_FLAG(SPI_I2S_FLAG));
  689. /* Clear the selected SPI CRC Error (CRCERR) flag */
  690. SPIx->STS = (uint16_t)~SPI_I2S_FLAG;
  691. }
  692. /**
  693. * @brief Checks whether the specified SPI/I2S interrupt has occurred or not.
  694. * @param SPIx where x can be
  695. * - 1, 2 or 3 in SPI mode
  696. * - 2 or 3 in I2S mode
  697. * @param SPI_I2S_IT specifies the SPI/I2S interrupt source to check.
  698. * This parameter can be one of the following values:
  699. * @arg SPI_I2S_INT_TE Transmit buffer empty interrupt.
  700. * @arg SPI_I2S_INT_RNE Receive buffer not empty interrupt.
  701. * @arg SPI_I2S_INT_OVER Overrun interrupt.
  702. * @arg SPI_INT_MODERR Mode Fault interrupt.
  703. * @arg SPI_INT_CRCERR CRC Error interrupt.
  704. * @arg I2S_INT_UNDER Underrun Error interrupt.
  705. * @return The new state of SPI_I2S_IT (SET or RESET).
  706. */
  707. INTStatus SPI_I2S_GetIntStatus(SPI_Module* SPIx, uint8_t SPI_I2S_IT)
  708. {
  709. INTStatus bitstatus = RESET;
  710. uint16_t itpos = 0, itmask = 0, enablestatus = 0;
  711. /* Check the parameters */
  712. assert_param(IS_SPI_PERIPH(SPIx));
  713. assert_param(IS_SPI_I2S_GET_INT(SPI_I2S_IT));
  714. /* Get the SPI/I2S IT index */
  715. itpos = 0x01 << (SPI_I2S_IT & 0x0F);
  716. /* Get the SPI/I2S IT mask */
  717. itmask = SPI_I2S_IT >> 4;
  718. /* Set the IT mask */
  719. itmask = 0x01 << itmask;
  720. /* Get the SPI_I2S_IT enable bit status */
  721. enablestatus = (SPIx->CTRL2 & itmask);
  722. /* Check the status of the specified SPI/I2S interrupt */
  723. if (((SPIx->STS & itpos) != (uint16_t)RESET) && enablestatus)
  724. {
  725. /* SPI_I2S_IT is set */
  726. bitstatus = SET;
  727. }
  728. else
  729. {
  730. /* SPI_I2S_IT is reset */
  731. bitstatus = RESET;
  732. }
  733. /* Return the SPI_I2S_IT status */
  734. return bitstatus;
  735. }
  736. /**
  737. * @brief Clears the SPIx CRC Error (CRCERR) interrupt pending bit.
  738. * @param SPIx where x can be
  739. * - 1, 2 or 3 in SPI mode
  740. * @param SPI_I2S_IT specifies the SPI interrupt pending bit to clear.
  741. * This function clears only CRCERR interrupt pending bit.
  742. * @note
  743. * - OVR (OverRun Error) interrupt pending bit is cleared by software
  744. * sequence: a read operation to SPI_DAT register (SPI_I2S_ReceiveData())
  745. * followed by a read operation to SPI_STS register (SPI_I2S_GetIntStatus()).
  746. * - UDR (UnderRun Error) interrupt pending bit is cleared by a read
  747. * operation to SPI_STS register (SPI_I2S_GetIntStatus()).
  748. * - MODF (Mode Fault) interrupt pending bit is cleared by software sequence:
  749. * a read/write operation to SPI_STS register (SPI_I2S_GetIntStatus())
  750. * followed by a write operation to SPI_CTRL1 register (SPI_Enable() to enable
  751. * the SPI).
  752. */
  753. void SPI_I2S_ClrITPendingBit(SPI_Module* SPIx, uint8_t SPI_I2S_IT)
  754. {
  755. uint16_t itpos = 0;
  756. /* Check the parameters */
  757. assert_param(IS_SPI_PERIPH(SPIx));
  758. assert_param(IS_SPI_I2S_CLR_INT(SPI_I2S_IT));
  759. /* Get the SPI IT index */
  760. itpos = 0x01 << (SPI_I2S_IT & 0x0F);
  761. /* Clear the selected SPI CRC Error (CRCERR) interrupt pending bit */
  762. SPIx->STS = (uint16_t)~itpos;
  763. }
  764. /**
  765. * @}
  766. */
  767. /**
  768. * @}
  769. */
  770. /**
  771. * @}
  772. */