stm32f4xx_spi.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_spi.c
  4. * @author MCD Application Team
  5. * @version V1.0.0
  6. * @date 30-September-2011
  7. * @brief This file provides firmware functions to manage the following
  8. * functionalities of the Serial peripheral interface (SPI):
  9. * - Initialization and Configuration
  10. * - Data transfers functions
  11. * - Hardware CRC Calculation
  12. * - DMA transfers management
  13. * - Interrupts and flags management
  14. *
  15. * @verbatim
  16. *
  17. *
  18. * ===================================================================
  19. * How to use this driver
  20. * ===================================================================
  21. *
  22. * 1. Enable peripheral clock using the following functions
  23. * RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE) for SPI1
  24. * RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE) for SPI2
  25. * RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI3, ENABLE) for SPI3.
  26. *
  27. * 2. Enable SCK, MOSI, MISO and NSS GPIO clocks using RCC_AHB1PeriphClockCmd()
  28. * function.
  29. * In I2S mode, if an external clock source is used then the I2S CKIN pin GPIO
  30. * clock should also be enabled.
  31. *
  32. * 3. Peripherals alternate function:
  33. * - Connect the pin to the desired peripherals' Alternate
  34. * Function (AF) using GPIO_PinAFConfig() function
  35. * - Configure the desired pin in alternate function by:
  36. * GPIO_InitStruct->GPIO_Mode = GPIO_Mode_AF
  37. * - Select the type, pull-up/pull-down and output speed via
  38. * GPIO_PuPd, GPIO_OType and GPIO_Speed members
  39. * - Call GPIO_Init() function
  40. * In I2S mode, if an external clock source is used then the I2S CKIN pin
  41. * should be also configured in Alternate function Push-pull pull-up mode.
  42. *
  43. * 4. Program the Polarity, Phase, First Data, Baud Rate Prescaler, Slave
  44. * Management, Peripheral Mode and CRC Polynomial values using the SPI_Init()
  45. * function.
  46. * In I2S mode, program the Mode, Standard, Data Format, MCLK Output, Audio
  47. * frequency and Polarity using I2S_Init() function.
  48. * For I2S mode, make sure that either:
  49. * - I2S PLL is configured using the functions RCC_I2SCLKConfig(RCC_I2S2CLKSource_PLLI2S),
  50. * RCC_PLLI2SCmd(ENABLE) and RCC_GetFlagStatus(RCC_FLAG_PLLI2SRDY).
  51. * or
  52. * - External clock source is configured using the function
  53. * RCC_I2SCLKConfig(RCC_I2S2CLKSource_Ext) and after setting correctly the define constant
  54. * I2S_EXTERNAL_CLOCK_VAL in the stm32f4xx_conf.h file.
  55. *
  56. * 5. Enable the NVIC and the corresponding interrupt using the function
  57. * SPI_ITConfig() if you need to use interrupt mode.
  58. *
  59. * 6. When using the DMA mode
  60. * - Configure the DMA using DMA_Init() function
  61. * - Active the needed channel Request using SPI_I2S_DMACmd() function
  62. *
  63. * 7. Enable the SPI using the SPI_Cmd() function or enable the I2S using
  64. * I2S_Cmd().
  65. *
  66. * 8. Enable the DMA using the DMA_Cmd() function when using DMA mode.
  67. *
  68. * 9. Optionally, you can enable/configure the following parameters without
  69. * re-initialization (i.e there is no need to call again SPI_Init() function):
  70. * - When bidirectional mode (SPI_Direction_1Line_Rx or SPI_Direction_1Line_Tx)
  71. * is programmed as Data direction parameter using the SPI_Init() function
  72. * it can be possible to switch between SPI_Direction_Tx or SPI_Direction_Rx
  73. * using the SPI_BiDirectionalLineConfig() function.
  74. * - When SPI_NSS_Soft is selected as Slave Select Management parameter
  75. * using the SPI_Init() function it can be possible to manage the
  76. * NSS internal signal using the SPI_NSSInternalSoftwareConfig() function.
  77. * - Reconfigure the data size using the SPI_DataSizeConfig() function
  78. * - Enable or disable the SS output using the SPI_SSOutputCmd() function
  79. *
  80. * 10. To use the CRC Hardware calculation feature refer to the Peripheral
  81. * CRC hardware Calculation subsection.
  82. *
  83. *
  84. * It is possible to use SPI in I2S full duplex mode, in this case, each SPI
  85. * peripheral is able to manage sending and receiving data simultaneously
  86. * using two data lines. Each SPI peripheral has an extended block called I2Sxext
  87. * (ie. I2S2ext for SPI2 and I2S3ext for SPI3).
  88. * The extension block is not a full SPI IP, it is used only as I2S slave to
  89. * implement full duplex mode. The extension block uses the same clock sources
  90. * as its master.
  91. * To configure I2S full duplex you have to:
  92. *
  93. * 1. Configure SPIx in I2S mode (I2S_Init() function) as described above.
  94. *
  95. * 2. Call the I2S_FullDuplexConfig() function using the same strucutre passed to
  96. * I2S_Init() function.
  97. *
  98. * 3. Call I2S_Cmd() for SPIx then for its extended block.
  99. *
  100. * 4. To configure interrupts or DMA requests and to get/clear flag status,
  101. * use I2Sxext instance for the extension block.
  102. *
  103. * Functions that can be called with I2Sxext instances are:
  104. * I2S_Cmd(), I2S_FullDuplexConfig(), SPI_I2S_ReceiveData(), SPI_I2S_SendData(),
  105. * SPI_I2S_DMACmd(), SPI_I2S_ITConfig(), SPI_I2S_GetFlagStatus(), SPI_I2S_ClearFlag(),
  106. * SPI_I2S_GetITStatus() and SPI_I2S_ClearITPendingBit().
  107. *
  108. * Example: To use SPI3 in Full duplex mode (SPI3 is Master Tx, I2S3ext is Slave Rx):
  109. *
  110. * RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI3, ENABLE);
  111. * I2S_StructInit(&I2SInitStruct);
  112. * I2SInitStruct.Mode = I2S_Mode_MasterTx;
  113. * I2S_Init(SPI3, &I2SInitStruct);
  114. * I2S_FullDuplexConfig(SPI3ext, &I2SInitStruct)
  115. * I2S_Cmd(SPI3, ENABLE);
  116. * I2S_Cmd(SPI3ext, ENABLE);
  117. * ...
  118. * while (SPI_I2S_GetFlagStatus(SPI2, SPI_FLAG_TXE) == RESET)
  119. * {}
  120. * SPI_I2S_SendData(SPI3, txdata[i]);
  121. * ...
  122. * while (SPI_I2S_GetFlagStatus(I2S3ext, SPI_FLAG_RXNE) == RESET)
  123. * {}
  124. * rxdata[i] = SPI_I2S_ReceiveData(I2S3ext);
  125. * ...
  126. *
  127. *
  128. * @note In I2S mode: if an external clock is used as source clock for the I2S,
  129. * then the define I2S_EXTERNAL_CLOCK_VAL in file stm32f4xx_conf.h should
  130. * be enabled and set to the value of the source clock frequency (in Hz).
  131. *
  132. * @note In SPI mode: To use the SPI TI mode, call the function SPI_TIModeCmd()
  133. * just after calling the function SPI_Init().
  134. *
  135. * @endverbatim
  136. *
  137. ******************************************************************************
  138. * @attention
  139. *
  140. * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  141. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  142. * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  143. * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  144. * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  145. * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  146. *
  147. * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
  148. ******************************************************************************
  149. */
  150. /* Includes ------------------------------------------------------------------*/
  151. #include "stm32f4xx_spi.h"
  152. #include "stm32f4xx_rcc.h"
  153. /** @addtogroup STM32F4xx_StdPeriph_Driver
  154. * @{
  155. */
  156. /** @defgroup SPI
  157. * @brief SPI driver modules
  158. * @{
  159. */
  160. /* Private typedef -----------------------------------------------------------*/
  161. /* Private define ------------------------------------------------------------*/
  162. /* SPI registers Masks */
  163. #define CR1_CLEAR_MASK ((uint16_t)0x3040)
  164. #define I2SCFGR_CLEAR_MASK ((uint16_t)0xF040)
  165. /* RCC PLLs masks */
  166. #define PLLCFGR_PPLR_MASK ((uint32_t)0x70000000)
  167. #define PLLCFGR_PPLN_MASK ((uint32_t)0x00007FC0)
  168. #define SPI_CR2_FRF ((uint16_t)0x0010)
  169. #define SPI_SR_TIFRFE ((uint16_t)0x0100)
  170. /* Private macro -------------------------------------------------------------*/
  171. /* Private variables ---------------------------------------------------------*/
  172. /* Private function prototypes -----------------------------------------------*/
  173. /* Private functions ---------------------------------------------------------*/
  174. /** @defgroup SPI_Private_Functions
  175. * @{
  176. */
  177. /** @defgroup SPI_Group1 Initialization and Configuration functions
  178. * @brief Initialization and Configuration functions
  179. *
  180. @verbatim
  181. ===============================================================================
  182. Initialization and Configuration functions
  183. ===============================================================================
  184. This section provides a set of functions allowing to initialize the SPI Direction,
  185. SPI Mode, SPI Data Size, SPI Polarity, SPI Phase, SPI NSS Management, SPI Baud
  186. Rate Prescaler, SPI First Bit and SPI CRC Polynomial.
  187. The SPI_Init() function follows the SPI configuration procedures for Master mode
  188. and Slave mode (details for these procedures are available in reference manual
  189. (RM0090)).
  190. @endverbatim
  191. * @{
  192. */
  193. /**
  194. * @brief Deinitialize the SPIx peripheral registers to their default reset values.
  195. * @param SPIx: To select the SPIx/I2Sx peripheral, where x can be: 1, 2 or 3
  196. * in SPI mode or 2 or 3 in I2S mode.
  197. *
  198. * @note The extended I2S blocks (ie. I2S2ext and I2S3ext blocks) are deinitialized
  199. * when the relative I2S peripheral is deinitialized (the extended block's clock
  200. * is managed by the I2S peripheral clock).
  201. *
  202. * @retval None
  203. */
  204. void SPI_I2S_DeInit(SPI_TypeDef* SPIx)
  205. {
  206. /* Check the parameters */
  207. assert_param(IS_SPI_ALL_PERIPH(SPIx));
  208. if (SPIx == SPI1)
  209. {
  210. /* Enable SPI1 reset state */
  211. RCC_APB2PeriphResetCmd(RCC_APB2Periph_SPI1, ENABLE);
  212. /* Release SPI1 from reset state */
  213. RCC_APB2PeriphResetCmd(RCC_APB2Periph_SPI1, DISABLE);
  214. }
  215. else if (SPIx == SPI2)
  216. {
  217. /* Enable SPI2 reset state */
  218. RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI2, ENABLE);
  219. /* Release SPI2 from reset state */
  220. RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI2, DISABLE);
  221. }
  222. else
  223. {
  224. if (SPIx == SPI3)
  225. {
  226. /* Enable SPI3 reset state */
  227. RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI3, ENABLE);
  228. /* Release SPI3 from reset state */
  229. RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI3, DISABLE);
  230. }
  231. }
  232. }
  233. /**
  234. * @brief Initializes the SPIx peripheral according to the specified
  235. * parameters in the SPI_InitStruct.
  236. * @param SPIx: where x can be 1, 2 or 3 to select the SPI peripheral.
  237. * @param SPI_InitStruct: pointer to a SPI_InitTypeDef structure that
  238. * contains the configuration information for the specified SPI peripheral.
  239. * @retval None
  240. */
  241. void SPI_Init(SPI_TypeDef* SPIx, SPI_InitTypeDef* SPI_InitStruct)
  242. {
  243. uint16_t tmpreg = 0;
  244. /* check the parameters */
  245. assert_param(IS_SPI_ALL_PERIPH(SPIx));
  246. /* Check the SPI parameters */
  247. assert_param(IS_SPI_DIRECTION_MODE(SPI_InitStruct->SPI_Direction));
  248. assert_param(IS_SPI_MODE(SPI_InitStruct->SPI_Mode));
  249. assert_param(IS_SPI_DATASIZE(SPI_InitStruct->SPI_DataSize));
  250. assert_param(IS_SPI_CPOL(SPI_InitStruct->SPI_CPOL));
  251. assert_param(IS_SPI_CPHA(SPI_InitStruct->SPI_CPHA));
  252. assert_param(IS_SPI_NSS(SPI_InitStruct->SPI_NSS));
  253. assert_param(IS_SPI_BAUDRATE_PRESCALER(SPI_InitStruct->SPI_BaudRatePrescaler));
  254. assert_param(IS_SPI_FIRST_BIT(SPI_InitStruct->SPI_FirstBit));
  255. assert_param(IS_SPI_CRC_POLYNOMIAL(SPI_InitStruct->SPI_CRCPolynomial));
  256. /*---------------------------- SPIx CR1 Configuration ------------------------*/
  257. /* Get the SPIx CR1 value */
  258. tmpreg = SPIx->CR1;
  259. /* Clear BIDIMode, BIDIOE, RxONLY, SSM, SSI, LSBFirst, BR, MSTR, CPOL and CPHA bits */
  260. tmpreg &= CR1_CLEAR_MASK;
  261. /* Configure SPIx: direction, NSS management, first transmitted bit, BaudRate prescaler
  262. master/salve mode, CPOL and CPHA */
  263. /* Set BIDImode, BIDIOE and RxONLY bits according to SPI_Direction value */
  264. /* Set SSM, SSI and MSTR bits according to SPI_Mode and SPI_NSS values */
  265. /* Set LSBFirst bit according to SPI_FirstBit value */
  266. /* Set BR bits according to SPI_BaudRatePrescaler value */
  267. /* Set CPOL bit according to SPI_CPOL value */
  268. /* Set CPHA bit according to SPI_CPHA value */
  269. tmpreg |= (uint16_t)((uint32_t)SPI_InitStruct->SPI_Direction | SPI_InitStruct->SPI_Mode |
  270. SPI_InitStruct->SPI_DataSize | SPI_InitStruct->SPI_CPOL |
  271. SPI_InitStruct->SPI_CPHA | SPI_InitStruct->SPI_NSS |
  272. SPI_InitStruct->SPI_BaudRatePrescaler | SPI_InitStruct->SPI_FirstBit);
  273. /* Write to SPIx CR1 */
  274. SPIx->CR1 = tmpreg;
  275. /* Activate the SPI mode (Reset I2SMOD bit in I2SCFGR register) */
  276. SPIx->I2SCFGR &= (uint16_t)~((uint16_t)SPI_I2SCFGR_I2SMOD);
  277. /*---------------------------- SPIx CRCPOLY Configuration --------------------*/
  278. /* Write to SPIx CRCPOLY */
  279. SPIx->CRCPR = SPI_InitStruct->SPI_CRCPolynomial;
  280. }
  281. /**
  282. * @brief Initializes the SPIx peripheral according to the specified
  283. * parameters in the I2S_InitStruct.
  284. * @param SPIx: where x can be 2 or 3 to select the SPI peripheral (configured in I2S mode).
  285. * @param I2S_InitStruct: pointer to an I2S_InitTypeDef structure that
  286. * contains the configuration information for the specified SPI peripheral
  287. * configured in I2S mode.
  288. *
  289. * @note The function calculates the optimal prescaler needed to obtain the most
  290. * accurate audio frequency (depending on the I2S clock source, the PLL values
  291. * and the product configuration). But in case the prescaler value is greater
  292. * than 511, the default value (0x02) will be configured instead.
  293. *
  294. * @note if an external clock is used as source clock for the I2S, then the define
  295. * I2S_EXTERNAL_CLOCK_VAL in file stm32f4xx_conf.h should be enabled and set
  296. * to the value of the the source clock frequency (in Hz).
  297. *
  298. * @retval None
  299. */
  300. void I2S_Init(SPI_TypeDef* SPIx, I2S_InitTypeDef* I2S_InitStruct)
  301. {
  302. uint16_t tmpreg = 0, i2sdiv = 2, i2sodd = 0, packetlength = 1;
  303. uint32_t tmp = 0, i2sclk = 0;
  304. #ifndef I2S_EXTERNAL_CLOCK_VAL
  305. uint32_t pllm = 0, plln = 0, pllr = 0;
  306. #endif /* I2S_EXTERNAL_CLOCK_VAL */
  307. /* Check the I2S parameters */
  308. assert_param(IS_SPI_23_PERIPH(SPIx));
  309. assert_param(IS_I2S_MODE(I2S_InitStruct->I2S_Mode));
  310. assert_param(IS_I2S_STANDARD(I2S_InitStruct->I2S_Standard));
  311. assert_param(IS_I2S_DATA_FORMAT(I2S_InitStruct->I2S_DataFormat));
  312. assert_param(IS_I2S_MCLK_OUTPUT(I2S_InitStruct->I2S_MCLKOutput));
  313. assert_param(IS_I2S_AUDIO_FREQ(I2S_InitStruct->I2S_AudioFreq));
  314. assert_param(IS_I2S_CPOL(I2S_InitStruct->I2S_CPOL));
  315. /*----------------------- SPIx I2SCFGR & I2SPR Configuration -----------------*/
  316. /* Clear I2SMOD, I2SE, I2SCFG, PCMSYNC, I2SSTD, CKPOL, DATLEN and CHLEN bits */
  317. SPIx->I2SCFGR &= I2SCFGR_CLEAR_MASK;
  318. SPIx->I2SPR = 0x0002;
  319. /* Get the I2SCFGR register value */
  320. tmpreg = SPIx->I2SCFGR;
  321. /* If the default value has to be written, reinitialize i2sdiv and i2sodd*/
  322. if(I2S_InitStruct->I2S_AudioFreq == I2S_AudioFreq_Default)
  323. {
  324. i2sodd = (uint16_t)0;
  325. i2sdiv = (uint16_t)2;
  326. }
  327. /* If the requested audio frequency is not the default, compute the prescaler */
  328. else
  329. {
  330. /* Check the frame length (For the Prescaler computing) *******************/
  331. if(I2S_InitStruct->I2S_DataFormat == I2S_DataFormat_16b)
  332. {
  333. /* Packet length is 16 bits */
  334. packetlength = 1;
  335. }
  336. else
  337. {
  338. /* Packet length is 32 bits */
  339. packetlength = 2;
  340. }
  341. /* Get I2S source Clock frequency ****************************************/
  342. /* If an external I2S clock has to be used, this define should be set
  343. in the project configuration or in the stm32f4xx_conf.h file */
  344. #ifdef I2S_EXTERNAL_CLOCK_VAL
  345. /* Set external clock as I2S clock source */
  346. if ((RCC->CFGR & RCC_CFGR_I2SSRC) == 0)
  347. {
  348. RCC->CFGR |= (uint32_t)RCC_CFGR_I2SSRC;
  349. }
  350. /* Set the I2S clock to the external clock value */
  351. i2sclk = I2S_EXTERNAL_CLOCK_VAL;
  352. #else /* There is no define for External I2S clock source */
  353. /* Set PLLI2S as I2S clock source */
  354. if ((RCC->CFGR & RCC_CFGR_I2SSRC) != 0)
  355. {
  356. RCC->CFGR &= ~(uint32_t)RCC_CFGR_I2SSRC;
  357. }
  358. /* Get the PLLI2SN value */
  359. plln = (uint32_t)(((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SN) >> 6) & \
  360. (RCC_PLLI2SCFGR_PLLI2SN >> 6));
  361. /* Get the PLLI2SR value */
  362. pllr = (uint32_t)(((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SR) >> 28) & \
  363. (RCC_PLLI2SCFGR_PLLI2SR >> 28));
  364. /* Get the PLLM value */
  365. pllm = (uint32_t)(RCC->PLLCFGR & RCC_PLLCFGR_PLLM);
  366. /* Get the I2S source clock value */
  367. i2sclk = (uint32_t)(((HSE_VALUE / pllm) * plln) / pllr);
  368. #endif /* I2S_EXTERNAL_CLOCK_VAL */
  369. /* Compute the Real divider depending on the MCLK output state, with a floating point */
  370. if(I2S_InitStruct->I2S_MCLKOutput == I2S_MCLKOutput_Enable)
  371. {
  372. /* MCLK output is enabled */
  373. tmp = (uint16_t)(((((i2sclk / 256) * 10) / I2S_InitStruct->I2S_AudioFreq)) + 5);
  374. }
  375. else
  376. {
  377. /* MCLK output is disabled */
  378. tmp = (uint16_t)(((((i2sclk / (32 * packetlength)) *10 ) / I2S_InitStruct->I2S_AudioFreq)) + 5);
  379. }
  380. /* Remove the flatting point */
  381. tmp = tmp / 10;
  382. /* Check the parity of the divider */
  383. i2sodd = (uint16_t)(tmp & (uint16_t)0x0001);
  384. /* Compute the i2sdiv prescaler */
  385. i2sdiv = (uint16_t)((tmp - i2sodd) / 2);
  386. /* Get the Mask for the Odd bit (SPI_I2SPR[8]) register */
  387. i2sodd = (uint16_t) (i2sodd << 8);
  388. }
  389. /* Test if the divider is 1 or 0 or greater than 0xFF */
  390. if ((i2sdiv < 2) || (i2sdiv > 0xFF))
  391. {
  392. /* Set the default values */
  393. i2sdiv = 2;
  394. i2sodd = 0;
  395. }
  396. /* Write to SPIx I2SPR register the computed value */
  397. SPIx->I2SPR = (uint16_t)((uint16_t)i2sdiv | (uint16_t)(i2sodd | (uint16_t)I2S_InitStruct->I2S_MCLKOutput));
  398. /* Configure the I2S with the SPI_InitStruct values */
  399. tmpreg |= (uint16_t)((uint16_t)SPI_I2SCFGR_I2SMOD | (uint16_t)(I2S_InitStruct->I2S_Mode | \
  400. (uint16_t)(I2S_InitStruct->I2S_Standard | (uint16_t)(I2S_InitStruct->I2S_DataFormat | \
  401. (uint16_t)I2S_InitStruct->I2S_CPOL))));
  402. /* Write to SPIx I2SCFGR */
  403. SPIx->I2SCFGR = tmpreg;
  404. }
  405. /**
  406. * @brief Fills each SPI_InitStruct member with its default value.
  407. * @param SPI_InitStruct: pointer to a SPI_InitTypeDef structure which will be initialized.
  408. * @retval None
  409. */
  410. void SPI_StructInit(SPI_InitTypeDef* SPI_InitStruct)
  411. {
  412. /*--------------- Reset SPI init structure parameters values -----------------*/
  413. /* Initialize the SPI_Direction member */
  414. SPI_InitStruct->SPI_Direction = SPI_Direction_2Lines_FullDuplex;
  415. /* initialize the SPI_Mode member */
  416. SPI_InitStruct->SPI_Mode = SPI_Mode_Slave;
  417. /* initialize the SPI_DataSize member */
  418. SPI_InitStruct->SPI_DataSize = SPI_DataSize_8b;
  419. /* Initialize the SPI_CPOL member */
  420. SPI_InitStruct->SPI_CPOL = SPI_CPOL_Low;
  421. /* Initialize the SPI_CPHA member */
  422. SPI_InitStruct->SPI_CPHA = SPI_CPHA_1Edge;
  423. /* Initialize the SPI_NSS member */
  424. SPI_InitStruct->SPI_NSS = SPI_NSS_Hard;
  425. /* Initialize the SPI_BaudRatePrescaler member */
  426. SPI_InitStruct->SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
  427. /* Initialize the SPI_FirstBit member */
  428. SPI_InitStruct->SPI_FirstBit = SPI_FirstBit_MSB;
  429. /* Initialize the SPI_CRCPolynomial member */
  430. SPI_InitStruct->SPI_CRCPolynomial = 7;
  431. }
  432. /**
  433. * @brief Fills each I2S_InitStruct member with its default value.
  434. * @param I2S_InitStruct: pointer to a I2S_InitTypeDef structure which will be initialized.
  435. * @retval None
  436. */
  437. void I2S_StructInit(I2S_InitTypeDef* I2S_InitStruct)
  438. {
  439. /*--------------- Reset I2S init structure parameters values -----------------*/
  440. /* Initialize the I2S_Mode member */
  441. I2S_InitStruct->I2S_Mode = I2S_Mode_SlaveTx;
  442. /* Initialize the I2S_Standard member */
  443. I2S_InitStruct->I2S_Standard = I2S_Standard_Phillips;
  444. /* Initialize the I2S_DataFormat member */
  445. I2S_InitStruct->I2S_DataFormat = I2S_DataFormat_16b;
  446. /* Initialize the I2S_MCLKOutput member */
  447. I2S_InitStruct->I2S_MCLKOutput = I2S_MCLKOutput_Disable;
  448. /* Initialize the I2S_AudioFreq member */
  449. I2S_InitStruct->I2S_AudioFreq = I2S_AudioFreq_Default;
  450. /* Initialize the I2S_CPOL member */
  451. I2S_InitStruct->I2S_CPOL = I2S_CPOL_Low;
  452. }
  453. /**
  454. * @brief Enables or disables the specified SPI peripheral.
  455. * @param SPIx: where x can be 1, 2 or 3 to select the SPI peripheral.
  456. * @param NewState: new state of the SPIx peripheral.
  457. * This parameter can be: ENABLE or DISABLE.
  458. * @retval None
  459. */
  460. void SPI_Cmd(SPI_TypeDef* SPIx, FunctionalState NewState)
  461. {
  462. /* Check the parameters */
  463. assert_param(IS_SPI_ALL_PERIPH(SPIx));
  464. assert_param(IS_FUNCTIONAL_STATE(NewState));
  465. if (NewState != DISABLE)
  466. {
  467. /* Enable the selected SPI peripheral */
  468. SPIx->CR1 |= SPI_CR1_SPE;
  469. }
  470. else
  471. {
  472. /* Disable the selected SPI peripheral */
  473. SPIx->CR1 &= (uint16_t)~((uint16_t)SPI_CR1_SPE);
  474. }
  475. }
  476. /**
  477. * @brief Enables or disables the specified SPI peripheral (in I2S mode).
  478. * @param SPIx: where x can be 2 or 3 to select the SPI peripheral (or I2Sxext
  479. * for full duplex mode).
  480. * @param NewState: new state of the SPIx peripheral.
  481. * This parameter can be: ENABLE or DISABLE.
  482. * @retval None
  483. */
  484. void I2S_Cmd(SPI_TypeDef* SPIx, FunctionalState NewState)
  485. {
  486. /* Check the parameters */
  487. assert_param(IS_SPI_23_PERIPH_EXT(SPIx));
  488. assert_param(IS_FUNCTIONAL_STATE(NewState));
  489. if (NewState != DISABLE)
  490. {
  491. /* Enable the selected SPI peripheral (in I2S mode) */
  492. SPIx->I2SCFGR |= SPI_I2SCFGR_I2SE;
  493. }
  494. else
  495. {
  496. /* Disable the selected SPI peripheral in I2S mode */
  497. SPIx->I2SCFGR &= (uint16_t)~((uint16_t)SPI_I2SCFGR_I2SE);
  498. }
  499. }
  500. /**
  501. * @brief Configures the data size for the selected SPI.
  502. * @param SPIx: where x can be 1, 2 or 3 to select the SPI peripheral.
  503. * @param SPI_DataSize: specifies the SPI data size.
  504. * This parameter can be one of the following values:
  505. * @arg SPI_DataSize_16b: Set data frame format to 16bit
  506. * @arg SPI_DataSize_8b: Set data frame format to 8bit
  507. * @retval None
  508. */
  509. void SPI_DataSizeConfig(SPI_TypeDef* SPIx, uint16_t SPI_DataSize)
  510. {
  511. /* Check the parameters */
  512. assert_param(IS_SPI_ALL_PERIPH(SPIx));
  513. assert_param(IS_SPI_DATASIZE(SPI_DataSize));
  514. /* Clear DFF bit */
  515. SPIx->CR1 &= (uint16_t)~SPI_DataSize_16b;
  516. /* Set new DFF bit value */
  517. SPIx->CR1 |= SPI_DataSize;
  518. }
  519. /**
  520. * @brief Selects the data transfer direction in bidirectional mode for the specified SPI.
  521. * @param SPIx: where x can be 1, 2 or 3 to select the SPI peripheral.
  522. * @param SPI_Direction: specifies the data transfer direction in bidirectional mode.
  523. * This parameter can be one of the following values:
  524. * @arg SPI_Direction_Tx: Selects Tx transmission direction
  525. * @arg SPI_Direction_Rx: Selects Rx receive direction
  526. * @retval None
  527. */
  528. void SPI_BiDirectionalLineConfig(SPI_TypeDef* SPIx, uint16_t SPI_Direction)
  529. {
  530. /* Check the parameters */
  531. assert_param(IS_SPI_ALL_PERIPH(SPIx));
  532. assert_param(IS_SPI_DIRECTION(SPI_Direction));
  533. if (SPI_Direction == SPI_Direction_Tx)
  534. {
  535. /* Set the Tx only mode */
  536. SPIx->CR1 |= SPI_Direction_Tx;
  537. }
  538. else
  539. {
  540. /* Set the Rx only mode */
  541. SPIx->CR1 &= SPI_Direction_Rx;
  542. }
  543. }
  544. /**
  545. * @brief Configures internally by software the NSS pin for the selected SPI.
  546. * @param SPIx: where x can be 1, 2 or 3 to select the SPI peripheral.
  547. * @param SPI_NSSInternalSoft: specifies the SPI NSS internal state.
  548. * This parameter can be one of the following values:
  549. * @arg SPI_NSSInternalSoft_Set: Set NSS pin internally
  550. * @arg SPI_NSSInternalSoft_Reset: Reset NSS pin internally
  551. * @retval None
  552. */
  553. void SPI_NSSInternalSoftwareConfig(SPI_TypeDef* SPIx, uint16_t SPI_NSSInternalSoft)
  554. {
  555. /* Check the parameters */
  556. assert_param(IS_SPI_ALL_PERIPH(SPIx));
  557. assert_param(IS_SPI_NSS_INTERNAL(SPI_NSSInternalSoft));
  558. if (SPI_NSSInternalSoft != SPI_NSSInternalSoft_Reset)
  559. {
  560. /* Set NSS pin internally by software */
  561. SPIx->CR1 |= SPI_NSSInternalSoft_Set;
  562. }
  563. else
  564. {
  565. /* Reset NSS pin internally by software */
  566. SPIx->CR1 &= SPI_NSSInternalSoft_Reset;
  567. }
  568. }
  569. /**
  570. * @brief Enables or disables the SS output for the selected SPI.
  571. * @param SPIx: where x can be 1, 2 or 3 to select the SPI peripheral.
  572. * @param NewState: new state of the SPIx SS output.
  573. * This parameter can be: ENABLE or DISABLE.
  574. * @retval None
  575. */
  576. void SPI_SSOutputCmd(SPI_TypeDef* SPIx, FunctionalState NewState)
  577. {
  578. /* Check the parameters */
  579. assert_param(IS_SPI_ALL_PERIPH(SPIx));
  580. assert_param(IS_FUNCTIONAL_STATE(NewState));
  581. if (NewState != DISABLE)
  582. {
  583. /* Enable the selected SPI SS output */
  584. SPIx->CR2 |= (uint16_t)SPI_CR2_SSOE;
  585. }
  586. else
  587. {
  588. /* Disable the selected SPI SS output */
  589. SPIx->CR2 &= (uint16_t)~((uint16_t)SPI_CR2_SSOE);
  590. }
  591. }
  592. /**
  593. * @brief Enables or disables the SPIx/I2Sx DMA interface.
  594. *
  595. * @note This function can be called only after the SPI_Init() function has
  596. * been called.
  597. * @note When TI mode is selected, the control bits SSM, SSI, CPOL and CPHA
  598. * are not taken into consideration and are configured by hardware
  599. * respectively to the TI mode requirements.
  600. *
  601. * @param SPIx: where x can be 1, 2 or 3
  602. * @param NewState: new state of the selected SPI TI communication mode.
  603. * This parameter can be: ENABLE or DISABLE.
  604. * @retval None
  605. */
  606. void SPI_TIModeCmd(SPI_TypeDef* SPIx, FunctionalState NewState)
  607. {
  608. /* Check the parameters */
  609. assert_param(IS_SPI_ALL_PERIPH(SPIx));
  610. assert_param(IS_FUNCTIONAL_STATE(NewState));
  611. if (NewState != DISABLE)
  612. {
  613. /* Enable the TI mode for the selected SPI peripheral */
  614. SPIx->CR2 |= SPI_CR2_FRF;
  615. }
  616. else
  617. {
  618. /* Disable the TI mode for the selected SPI peripheral */
  619. SPIx->CR2 &= (uint16_t)~SPI_CR2_FRF;
  620. }
  621. }
  622. /**
  623. * @brief Configures the full duplex mode for the I2Sx peripheral using its
  624. * extension I2Sxext according to the specified parameters in the
  625. * I2S_InitStruct.
  626. * @param I2Sxext: where x can be 2 or 3 to select the I2S peripheral extension block.
  627. * @param I2S_InitStruct: pointer to an I2S_InitTypeDef structure that
  628. * contains the configuration information for the specified I2S peripheral
  629. * extension.
  630. *
  631. * @note The structure pointed by I2S_InitStruct parameter should be the same
  632. * used for the master I2S peripheral. In this case, if the master is
  633. * configured as transmitter, the slave will be receiver and vice versa.
  634. * Or you can force a different mode by modifying the field I2S_Mode to the
  635. * value I2S_SlaveRx or I2S_SlaveTx indepedently of the master configuration.
  636. *
  637. * @note The I2S full duplex extension can be configured in slave mode only.
  638. *
  639. * @retval None
  640. */
  641. void I2S_FullDuplexConfig(SPI_TypeDef* I2Sxext, I2S_InitTypeDef* I2S_InitStruct)
  642. {
  643. uint16_t tmpreg = 0, tmp = 0;
  644. /* Check the I2S parameters */
  645. assert_param(IS_I2S_EXT_PERIPH(I2Sxext));
  646. assert_param(IS_I2S_MODE(I2S_InitStruct->I2S_Mode));
  647. assert_param(IS_I2S_STANDARD(I2S_InitStruct->I2S_Standard));
  648. assert_param(IS_I2S_DATA_FORMAT(I2S_InitStruct->I2S_DataFormat));
  649. assert_param(IS_I2S_CPOL(I2S_InitStruct->I2S_CPOL));
  650. /*----------------------- SPIx I2SCFGR & I2SPR Configuration -----------------*/
  651. /* Clear I2SMOD, I2SE, I2SCFG, PCMSYNC, I2SSTD, CKPOL, DATLEN and CHLEN bits */
  652. I2Sxext->I2SCFGR &= I2SCFGR_CLEAR_MASK;
  653. I2Sxext->I2SPR = 0x0002;
  654. /* Get the I2SCFGR register value */
  655. tmpreg = I2Sxext->I2SCFGR;
  656. /* Get the mode to be configured for the extended I2S */
  657. if ((I2S_InitStruct->I2S_Mode == I2S_Mode_MasterTx) || (I2S_InitStruct->I2S_Mode == I2S_Mode_SlaveTx))
  658. {
  659. tmp = I2S_Mode_SlaveRx;
  660. }
  661. else
  662. {
  663. if ((I2S_InitStruct->I2S_Mode == I2S_Mode_MasterRx) || (I2S_InitStruct->I2S_Mode == I2S_Mode_SlaveRx))
  664. {
  665. tmp = I2S_Mode_SlaveTx;
  666. }
  667. }
  668. /* Configure the I2S with the SPI_InitStruct values */
  669. tmpreg |= (uint16_t)((uint16_t)SPI_I2SCFGR_I2SMOD | (uint16_t)(tmp | \
  670. (uint16_t)(I2S_InitStruct->I2S_Standard | (uint16_t)(I2S_InitStruct->I2S_DataFormat | \
  671. (uint16_t)I2S_InitStruct->I2S_CPOL))));
  672. /* Write to SPIx I2SCFGR */
  673. I2Sxext->I2SCFGR = tmpreg;
  674. }
  675. /**
  676. * @}
  677. */
  678. /** @defgroup SPI_Group2 Data transfers functions
  679. * @brief Data transfers functions
  680. *
  681. @verbatim
  682. ===============================================================================
  683. Data transfers functions
  684. ===============================================================================
  685. This section provides a set of functions allowing to manage the SPI data transfers
  686. In reception, data are received and then stored into an internal Rx buffer while
  687. In transmission, data are first stored into an internal Tx buffer before being
  688. transmitted.
  689. The read access of the SPI_DR register can be done using the SPI_I2S_ReceiveData()
  690. function and returns the Rx buffered value. Whereas a write access to the SPI_DR
  691. can be done using SPI_I2S_SendData() function and stores the written data into
  692. Tx buffer.
  693. @endverbatim
  694. * @{
  695. */
  696. /**
  697. * @brief Returns the most recent received data by the SPIx/I2Sx peripheral.
  698. * @param SPIx: To select the SPIx/I2Sx peripheral, where x can be: 1, 2 or 3
  699. * in SPI mode or 2 or 3 in I2S mode or I2Sxext for I2S full duplex mode.
  700. * @retval The value of the received data.
  701. */
  702. uint16_t SPI_I2S_ReceiveData(SPI_TypeDef* SPIx)
  703. {
  704. /* Check the parameters */
  705. assert_param(IS_SPI_ALL_PERIPH_EXT(SPIx));
  706. /* Return the data in the DR register */
  707. return SPIx->DR;
  708. }
  709. /**
  710. * @brief Transmits a Data through the SPIx/I2Sx peripheral.
  711. * @param SPIx: To select the SPIx/I2Sx peripheral, where x can be: 1, 2 or 3
  712. * in SPI mode or 2 or 3 in I2S mode or I2Sxext for I2S full duplex mode.
  713. * @param Data: Data to be transmitted.
  714. * @retval None
  715. */
  716. void SPI_I2S_SendData(SPI_TypeDef* SPIx, uint16_t Data)
  717. {
  718. /* Check the parameters */
  719. assert_param(IS_SPI_ALL_PERIPH_EXT(SPIx));
  720. /* Write in the DR register the data to be sent */
  721. SPIx->DR = Data;
  722. }
  723. /**
  724. * @}
  725. */
  726. /** @defgroup SPI_Group3 Hardware CRC Calculation functions
  727. * @brief Hardware CRC Calculation functions
  728. *
  729. @verbatim
  730. ===============================================================================
  731. Hardware CRC Calculation functions
  732. ===============================================================================
  733. This section provides a set of functions allowing to manage the SPI CRC hardware
  734. calculation
  735. SPI communication using CRC is possible through the following procedure:
  736. 1. Program the Data direction, Polarity, Phase, First Data, Baud Rate Prescaler,
  737. Slave Management, Peripheral Mode and CRC Polynomial values using the SPI_Init()
  738. function.
  739. 2. Enable the CRC calculation using the SPI_CalculateCRC() function.
  740. 3. Enable the SPI using the SPI_Cmd() function
  741. 4. Before writing the last data to the TX buffer, set the CRCNext bit using the
  742. SPI_TransmitCRC() function to indicate that after transmission of the last
  743. data, the CRC should be transmitted.
  744. 5. After transmitting the last data, the SPI transmits the CRC. The SPI_CR1_CRCNEXT
  745. bit is reset. The CRC is also received and compared against the SPI_RXCRCR
  746. value.
  747. If the value does not match, the SPI_FLAG_CRCERR flag is set and an interrupt
  748. can be generated when the SPI_I2S_IT_ERR interrupt is enabled.
  749. @note It is advised not to read the calculated CRC values during the communication.
  750. @note When the SPI is in slave mode, be careful to enable CRC calculation only
  751. when the clock is stable, that is, when the clock is in the steady state.
  752. If not, a wrong CRC calculation may be done. In fact, the CRC is sensitive
  753. to the SCK slave input clock as soon as CRCEN is set, and this, whatever
  754. the value of the SPE bit.
  755. @note With high bitrate frequencies, be careful when transmitting the CRC.
  756. As the number of used CPU cycles has to be as low as possible in the CRC
  757. transfer phase, it is forbidden to call software functions in the CRC
  758. transmission sequence to avoid errors in the last data and CRC reception.
  759. In fact, CRCNEXT bit has to be written before the end of the transmission/reception
  760. of the last data.
  761. @note For high bit rate frequencies, it is advised to use the DMA mode to avoid the
  762. degradation of the SPI speed performance due to CPU accesses impacting the
  763. SPI bandwidth.
  764. @note When the STM32F4xx is configured as slave and the NSS hardware mode is
  765. used, the NSS pin needs to be kept low between the data phase and the CRC
  766. phase.
  767. @note When the SPI is configured in slave mode with the CRC feature enabled, CRC
  768. calculation takes place even if a high level is applied on the NSS pin.
  769. This may happen for example in case of a multi-slave environment where the
  770. communication master addresses slaves alternately.
  771. @note Between a slave de-selection (high level on NSS) and a new slave selection
  772. (low level on NSS), the CRC value should be cleared on both master and slave
  773. sides in order to resynchronize the master and slave for their respective
  774. CRC calculation.
  775. @note To clear the CRC, follow the procedure below:
  776. 1. Disable SPI using the SPI_Cmd() function
  777. 2. Disable the CRC calculation using the SPI_CalculateCRC() function.
  778. 3. Enable the CRC calculation using the SPI_CalculateCRC() function.
  779. 4. Enable SPI using the SPI_Cmd() function.
  780. @endverbatim
  781. * @{
  782. */
  783. /**
  784. * @brief Enables or disables the CRC value calculation of the transferred bytes.
  785. * @param SPIx: where x can be 1, 2 or 3 to select the SPI peripheral.
  786. * @param NewState: new state of the SPIx CRC value calculation.
  787. * This parameter can be: ENABLE or DISABLE.
  788. * @retval None
  789. */
  790. void SPI_CalculateCRC(SPI_TypeDef* SPIx, FunctionalState NewState)
  791. {
  792. /* Check the parameters */
  793. assert_param(IS_SPI_ALL_PERIPH(SPIx));
  794. assert_param(IS_FUNCTIONAL_STATE(NewState));
  795. if (NewState != DISABLE)
  796. {
  797. /* Enable the selected SPI CRC calculation */
  798. SPIx->CR1 |= SPI_CR1_CRCEN;
  799. }
  800. else
  801. {
  802. /* Disable the selected SPI CRC calculation */
  803. SPIx->CR1 &= (uint16_t)~((uint16_t)SPI_CR1_CRCEN);
  804. }
  805. }
  806. /**
  807. * @brief Transmit the SPIx CRC value.
  808. * @param SPIx: where x can be 1, 2 or 3 to select the SPI peripheral.
  809. * @retval None
  810. */
  811. void SPI_TransmitCRC(SPI_TypeDef* SPIx)
  812. {
  813. /* Check the parameters */
  814. assert_param(IS_SPI_ALL_PERIPH(SPIx));
  815. /* Enable the selected SPI CRC transmission */
  816. SPIx->CR1 |= SPI_CR1_CRCNEXT;
  817. }
  818. /**
  819. * @brief Returns the transmit or the receive CRC register value for the specified SPI.
  820. * @param SPIx: where x can be 1, 2 or 3 to select the SPI peripheral.
  821. * @param SPI_CRC: specifies the CRC register to be read.
  822. * This parameter can be one of the following values:
  823. * @arg SPI_CRC_Tx: Selects Tx CRC register
  824. * @arg SPI_CRC_Rx: Selects Rx CRC register
  825. * @retval The selected CRC register value..
  826. */
  827. uint16_t SPI_GetCRC(SPI_TypeDef* SPIx, uint8_t SPI_CRC)
  828. {
  829. uint16_t crcreg = 0;
  830. /* Check the parameters */
  831. assert_param(IS_SPI_ALL_PERIPH(SPIx));
  832. assert_param(IS_SPI_CRC(SPI_CRC));
  833. if (SPI_CRC != SPI_CRC_Rx)
  834. {
  835. /* Get the Tx CRC register */
  836. crcreg = SPIx->TXCRCR;
  837. }
  838. else
  839. {
  840. /* Get the Rx CRC register */
  841. crcreg = SPIx->RXCRCR;
  842. }
  843. /* Return the selected CRC register */
  844. return crcreg;
  845. }
  846. /**
  847. * @brief Returns the CRC Polynomial register value for the specified SPI.
  848. * @param SPIx: where x can be 1, 2 or 3 to select the SPI peripheral.
  849. * @retval The CRC Polynomial register value.
  850. */
  851. uint16_t SPI_GetCRCPolynomial(SPI_TypeDef* SPIx)
  852. {
  853. /* Check the parameters */
  854. assert_param(IS_SPI_ALL_PERIPH(SPIx));
  855. /* Return the CRC polynomial register */
  856. return SPIx->CRCPR;
  857. }
  858. /**
  859. * @}
  860. */
  861. /** @defgroup SPI_Group4 DMA transfers management functions
  862. * @brief DMA transfers management functions
  863. *
  864. @verbatim
  865. ===============================================================================
  866. DMA transfers management functions
  867. ===============================================================================
  868. @endverbatim
  869. * @{
  870. */
  871. /**
  872. * @brief Enables or disables the SPIx/I2Sx DMA interface.
  873. * @param SPIx: To select the SPIx/I2Sx peripheral, where x can be: 1, 2 or 3
  874. * in SPI mode or 2 or 3 in I2S mode or I2Sxext for I2S full duplex mode.
  875. * @param SPI_I2S_DMAReq: specifies the SPI DMA transfer request to be enabled or disabled.
  876. * This parameter can be any combination of the following values:
  877. * @arg SPI_I2S_DMAReq_Tx: Tx buffer DMA transfer request
  878. * @arg SPI_I2S_DMAReq_Rx: Rx buffer DMA transfer request
  879. * @param NewState: new state of the selected SPI DMA transfer request.
  880. * This parameter can be: ENABLE or DISABLE.
  881. * @retval None
  882. */
  883. void SPI_I2S_DMACmd(SPI_TypeDef* SPIx, uint16_t SPI_I2S_DMAReq, FunctionalState NewState)
  884. {
  885. /* Check the parameters */
  886. assert_param(IS_SPI_ALL_PERIPH_EXT(SPIx));
  887. assert_param(IS_FUNCTIONAL_STATE(NewState));
  888. assert_param(IS_SPI_I2S_DMAREQ(SPI_I2S_DMAReq));
  889. if (NewState != DISABLE)
  890. {
  891. /* Enable the selected SPI DMA requests */
  892. SPIx->CR2 |= SPI_I2S_DMAReq;
  893. }
  894. else
  895. {
  896. /* Disable the selected SPI DMA requests */
  897. SPIx->CR2 &= (uint16_t)~SPI_I2S_DMAReq;
  898. }
  899. }
  900. /**
  901. * @}
  902. */
  903. /** @defgroup SPI_Group5 Interrupts and flags management functions
  904. * @brief Interrupts and flags management functions
  905. *
  906. @verbatim
  907. ===============================================================================
  908. Interrupts and flags management functions
  909. ===============================================================================
  910. This section provides a set of functions allowing to configure the SPI Interrupts
  911. sources and check or clear the flags or pending bits status.
  912. The user should identify which mode will be used in his application to manage
  913. the communication: Polling mode, Interrupt mode or DMA mode.
  914. Polling Mode
  915. =============
  916. In Polling Mode, the SPI/I2S communication can be managed by 9 flags:
  917. 1. SPI_I2S_FLAG_TXE : to indicate the status of the transmit buffer register
  918. 2. SPI_I2S_FLAG_RXNE : to indicate the status of the receive buffer register
  919. 3. SPI_I2S_FLAG_BSY : to indicate the state of the communication layer of the SPI.
  920. 4. SPI_FLAG_CRCERR : to indicate if a CRC Calculation error occur
  921. 5. SPI_FLAG_MODF : to indicate if a Mode Fault error occur
  922. 6. SPI_I2S_FLAG_OVR : to indicate if an Overrun error occur
  923. 7. I2S_FLAG_TIFRFE: to indicate a Frame Format error occurs.
  924. 8. I2S_FLAG_UDR: to indicate an Underrun error occurs.
  925. 9. I2S_FLAG_CHSIDE: to indicate Channel Side.
  926. @note Do not use the BSY flag to handle each data transmission or reception. It is
  927. better to use the TXE and RXNE flags instead.
  928. In this Mode it is advised to use the following functions:
  929. - FlagStatus SPI_I2S_GetFlagStatus(SPI_TypeDef* SPIx, uint16_t SPI_I2S_FLAG);
  930. - void SPI_I2S_ClearFlag(SPI_TypeDef* SPIx, uint16_t SPI_I2S_FLAG);
  931. Interrupt Mode
  932. ===============
  933. In Interrupt Mode, the SPI communication can be managed by 3 interrupt sources
  934. and 7 pending bits:
  935. Pending Bits:
  936. -------------
  937. 1. SPI_I2S_IT_TXE : to indicate the status of the transmit buffer register
  938. 2. SPI_I2S_IT_RXNE : to indicate the status of the receive buffer register
  939. 3. SPI_IT_CRCERR : to indicate if a CRC Calculation error occur (available in SPI mode only)
  940. 4. SPI_IT_MODF : to indicate if a Mode Fault error occur (available in SPI mode only)
  941. 5. SPI_I2S_IT_OVR : to indicate if an Overrun error occur
  942. 6. I2S_IT_UDR : to indicate an Underrun Error occurs (available in I2S mode only).
  943. 7. I2S_FLAG_TIFRFE : to indicate a Frame Format error occurs (available in TI mode only).
  944. Interrupt Source:
  945. -----------------
  946. 1. SPI_I2S_IT_TXE: specifies the interrupt source for the Tx buffer empty
  947. interrupt.
  948. 2. SPI_I2S_IT_RXNE : specifies the interrupt source for the Rx buffer not
  949. empty interrupt.
  950. 3. SPI_I2S_IT_ERR : specifies the interrupt source for the errors interrupt.
  951. In this Mode it is advised to use the following functions:
  952. - void SPI_I2S_ITConfig(SPI_TypeDef* SPIx, uint8_t SPI_I2S_IT, FunctionalState NewState);
  953. - ITStatus SPI_I2S_GetITStatus(SPI_TypeDef* SPIx, uint8_t SPI_I2S_IT);
  954. - void SPI_I2S_ClearITPendingBit(SPI_TypeDef* SPIx, uint8_t SPI_I2S_IT);
  955. DMA Mode
  956. ========
  957. In DMA Mode, the SPI communication can be managed by 2 DMA Channel requests:
  958. 1. SPI_I2S_DMAReq_Tx: specifies the Tx buffer DMA transfer request
  959. 2. SPI_I2S_DMAReq_Rx: specifies the Rx buffer DMA transfer request
  960. In this Mode it is advised to use the following function:
  961. - void SPI_I2S_DMACmd(SPI_TypeDef* SPIx, uint16_t SPI_I2S_DMAReq, FunctionalState NewState);
  962. @endverbatim
  963. * @{
  964. */
  965. /**
  966. * @brief Enables or disables the specified SPI/I2S interrupts.
  967. * @param SPIx: To select the SPIx/I2Sx peripheral, where x can be: 1, 2 or 3
  968. * in SPI mode or 2 or 3 in I2S mode or I2Sxext for I2S full duplex mode.
  969. * @param SPI_I2S_IT: specifies the SPI interrupt source to be enabled or disabled.
  970. * This parameter can be one of the following values:
  971. * @arg SPI_I2S_IT_TXE: Tx buffer empty interrupt mask
  972. * @arg SPI_I2S_IT_RXNE: Rx buffer not empty interrupt mask
  973. * @arg SPI_I2S_IT_ERR: Error interrupt mask
  974. * @param NewState: new state of the specified SPI interrupt.
  975. * This parameter can be: ENABLE or DISABLE.
  976. * @retval None
  977. */
  978. void SPI_I2S_ITConfig(SPI_TypeDef* SPIx, uint8_t SPI_I2S_IT, FunctionalState NewState)
  979. {
  980. uint16_t itpos = 0, itmask = 0 ;
  981. /* Check the parameters */
  982. assert_param(IS_SPI_ALL_PERIPH_EXT(SPIx));
  983. assert_param(IS_FUNCTIONAL_STATE(NewState));
  984. assert_param(IS_SPI_I2S_CONFIG_IT(SPI_I2S_IT));
  985. /* Get the SPI IT index */
  986. itpos = SPI_I2S_IT >> 4;
  987. /* Set the IT mask */
  988. itmask = (uint16_t)1 << (uint16_t)itpos;
  989. if (NewState != DISABLE)
  990. {
  991. /* Enable the selected SPI interrupt */
  992. SPIx->CR2 |= itmask;
  993. }
  994. else
  995. {
  996. /* Disable the selected SPI interrupt */
  997. SPIx->CR2 &= (uint16_t)~itmask;
  998. }
  999. }
  1000. /**
  1001. * @brief Checks whether the specified SPIx/I2Sx flag is set or not.
  1002. * @param SPIx: To select the SPIx/I2Sx peripheral, where x can be: 1, 2 or 3
  1003. * in SPI mode or 2 or 3 in I2S mode or I2Sxext for I2S full duplex mode.
  1004. * @param SPI_I2S_FLAG: specifies the SPI flag to check.
  1005. * This parameter can be one of the following values:
  1006. * @arg SPI_I2S_FLAG_TXE: Transmit buffer empty flag.
  1007. * @arg SPI_I2S_FLAG_RXNE: Receive buffer not empty flag.
  1008. * @arg SPI_I2S_FLAG_BSY: Busy flag.
  1009. * @arg SPI_I2S_FLAG_OVR: Overrun flag.
  1010. * @arg SPI_FLAG_MODF: Mode Fault flag.
  1011. * @arg SPI_FLAG_CRCERR: CRC Error flag.
  1012. * @arg SPI_I2S_FLAG_TIFRFE: Format Error.
  1013. * @arg I2S_FLAG_UDR: Underrun Error flag.
  1014. * @arg I2S_FLAG_CHSIDE: Channel Side flag.
  1015. * @retval The new state of SPI_I2S_FLAG (SET or RESET).
  1016. */
  1017. FlagStatus SPI_I2S_GetFlagStatus(SPI_TypeDef* SPIx, uint16_t SPI_I2S_FLAG)
  1018. {
  1019. FlagStatus bitstatus = RESET;
  1020. /* Check the parameters */
  1021. assert_param(IS_SPI_ALL_PERIPH_EXT(SPIx));
  1022. assert_param(IS_SPI_I2S_GET_FLAG(SPI_I2S_FLAG));
  1023. /* Check the status of the specified SPI flag */
  1024. if ((SPIx->SR & SPI_I2S_FLAG) != (uint16_t)RESET)
  1025. {
  1026. /* SPI_I2S_FLAG is set */
  1027. bitstatus = SET;
  1028. }
  1029. else
  1030. {
  1031. /* SPI_I2S_FLAG is reset */
  1032. bitstatus = RESET;
  1033. }
  1034. /* Return the SPI_I2S_FLAG status */
  1035. return bitstatus;
  1036. }
  1037. /**
  1038. * @brief Clears the SPIx CRC Error (CRCERR) flag.
  1039. * @param SPIx: To select the SPIx/I2Sx peripheral, where x can be: 1, 2 or 3
  1040. * in SPI mode or 2 or 3 in I2S mode or I2Sxext for I2S full duplex mode.
  1041. * @param SPI_I2S_FLAG: specifies the SPI flag to clear.
  1042. * This function clears only CRCERR flag.
  1043. * @arg SPI_FLAG_CRCERR: CRC Error flag.
  1044. *
  1045. * @note OVR (OverRun error) flag is cleared by software sequence: a read
  1046. * operation to SPI_DR register (SPI_I2S_ReceiveData()) followed by a read
  1047. * operation to SPI_SR register (SPI_I2S_GetFlagStatus()).
  1048. * @note UDR (UnderRun error) flag is cleared by a read operation to
  1049. * SPI_SR register (SPI_I2S_GetFlagStatus()).
  1050. * @note MODF (Mode Fault) flag is cleared by software sequence: a read/write
  1051. * operation to SPI_SR register (SPI_I2S_GetFlagStatus()) followed by a
  1052. * write operation to SPI_CR1 register (SPI_Cmd() to enable the SPI).
  1053. *
  1054. * @retval None
  1055. */
  1056. void SPI_I2S_ClearFlag(SPI_TypeDef* SPIx, uint16_t SPI_I2S_FLAG)
  1057. {
  1058. /* Check the parameters */
  1059. assert_param(IS_SPI_ALL_PERIPH_EXT(SPIx));
  1060. assert_param(IS_SPI_I2S_CLEAR_FLAG(SPI_I2S_FLAG));
  1061. /* Clear the selected SPI CRC Error (CRCERR) flag */
  1062. SPIx->SR = (uint16_t)~SPI_I2S_FLAG;
  1063. }
  1064. /**
  1065. * @brief Checks whether the specified SPIx/I2Sx interrupt has occurred or not.
  1066. * @param SPIx: To select the SPIx/I2Sx peripheral, where x can be: 1, 2 or 3
  1067. * in SPI mode or 2 or 3 in I2S mode or I2Sxext for I2S full duplex mode.
  1068. * @param SPI_I2S_IT: specifies the SPI interrupt source to check.
  1069. * This parameter can be one of the following values:
  1070. * @arg SPI_I2S_IT_TXE: Transmit buffer empty interrupt.
  1071. * @arg SPI_I2S_IT_RXNE: Receive buffer not empty interrupt.
  1072. * @arg SPI_I2S_IT_OVR: Overrun interrupt.
  1073. * @arg SPI_IT_MODF: Mode Fault interrupt.
  1074. * @arg SPI_IT_CRCERR: CRC Error interrupt.
  1075. * @arg I2S_IT_UDR: Underrun interrupt.
  1076. * @arg SPI_I2S_IT_TIFRFE: Format Error interrupt.
  1077. * @retval The new state of SPI_I2S_IT (SET or RESET).
  1078. */
  1079. ITStatus SPI_I2S_GetITStatus(SPI_TypeDef* SPIx, uint8_t SPI_I2S_IT)
  1080. {
  1081. ITStatus bitstatus = RESET;
  1082. uint16_t itpos = 0, itmask = 0, enablestatus = 0;
  1083. /* Check the parameters */
  1084. assert_param(IS_SPI_ALL_PERIPH_EXT(SPIx));
  1085. assert_param(IS_SPI_I2S_GET_IT(SPI_I2S_IT));
  1086. /* Get the SPI_I2S_IT index */
  1087. itpos = 0x01 << (SPI_I2S_IT & 0x0F);
  1088. /* Get the SPI_I2S_IT IT mask */
  1089. itmask = SPI_I2S_IT >> 4;
  1090. /* Set the IT mask */
  1091. itmask = 0x01 << itmask;
  1092. /* Get the SPI_I2S_IT enable bit status */
  1093. enablestatus = (SPIx->CR2 & itmask) ;
  1094. /* Check the status of the specified SPI interrupt */
  1095. if (((SPIx->SR & itpos) != (uint16_t)RESET) && enablestatus)
  1096. {
  1097. /* SPI_I2S_IT is set */
  1098. bitstatus = SET;
  1099. }
  1100. else
  1101. {
  1102. /* SPI_I2S_IT is reset */
  1103. bitstatus = RESET;
  1104. }
  1105. /* Return the SPI_I2S_IT status */
  1106. return bitstatus;
  1107. }
  1108. /**
  1109. * @brief Clears the SPIx CRC Error (CRCERR) interrupt pending bit.
  1110. * @param SPIx: To select the SPIx/I2Sx peripheral, where x can be: 1, 2 or 3
  1111. * in SPI mode or 2 or 3 in I2S mode or I2Sxext for I2S full duplex mode.
  1112. * @param SPI_I2S_IT: specifies the SPI interrupt pending bit to clear.
  1113. * This function clears only CRCERR interrupt pending bit.
  1114. * @arg SPI_IT_CRCERR: CRC Error interrupt.
  1115. *
  1116. * @note OVR (OverRun Error) interrupt pending bit is cleared by software
  1117. * sequence: a read operation to SPI_DR register (SPI_I2S_ReceiveData())
  1118. * followed by a read operation to SPI_SR register (SPI_I2S_GetITStatus()).
  1119. * @note UDR (UnderRun Error) interrupt pending bit is cleared by a read
  1120. * operation to SPI_SR register (SPI_I2S_GetITStatus()).
  1121. * @note MODF (Mode Fault) interrupt pending bit is cleared by software sequence:
  1122. * a read/write operation to SPI_SR register (SPI_I2S_GetITStatus())
  1123. * followed by a write operation to SPI_CR1 register (SPI_Cmd() to enable
  1124. * the SPI).
  1125. * @retval None
  1126. */
  1127. void SPI_I2S_ClearITPendingBit(SPI_TypeDef* SPIx, uint8_t SPI_I2S_IT)
  1128. {
  1129. uint16_t itpos = 0;
  1130. /* Check the parameters */
  1131. assert_param(IS_SPI_ALL_PERIPH_EXT(SPIx));
  1132. assert_param(IS_SPI_I2S_CLEAR_IT(SPI_I2S_IT));
  1133. /* Get the SPI_I2S IT index */
  1134. itpos = 0x01 << (SPI_I2S_IT & 0x0F);
  1135. /* Clear the selected SPI CRC Error (CRCERR) interrupt pending bit */
  1136. SPIx->SR = (uint16_t)~itpos;
  1137. }
  1138. /**
  1139. * @}
  1140. */
  1141. /**
  1142. * @}
  1143. */
  1144. /**
  1145. * @}
  1146. */
  1147. /**
  1148. * @}
  1149. */
  1150. /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/