stm32f2xx_i2c.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402
  1. /**
  2. ******************************************************************************
  3. * @file stm32f2xx_i2c.c
  4. * @author MCD Application Team
  5. * @version V1.1.3
  6. * @date 31-December-2021
  7. * @brief This file provides firmware functions to manage the following
  8. * functionalities of the Inter-integrated circuit (I2C)
  9. * - Initialization and Configuration
  10. * - Data transfers
  11. * - PEC management
  12. * - DMA transfers management
  13. * - Interrupts, events and flags management
  14. *
  15. * @verbatim
  16. *
  17. * ===================================================================
  18. * How to use this driver
  19. * ===================================================================
  20. * 1. Enable peripheral clock using RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2Cx, ENABLE)
  21. * function for I2C1, I2C2 or I2C3.
  22. *
  23. * 2. Enable SDA, SCL and SMBA (when used) GPIO clocks using
  24. * RCC_AHBPeriphClockCmd() function.
  25. *
  26. * 3. Peripherals alternate function:
  27. * - Connect the pin to the desired peripherals' Alternate
  28. * Function (AF) using GPIO_PinAFConfig() function
  29. * - Configure the desired pin in alternate function by:
  30. * GPIO_InitStruct->GPIO_Mode = GPIO_Mode_AF
  31. * - Select the type, pull-up/pull-down and output speed via
  32. * GPIO_PuPd, GPIO_OType and GPIO_Speed members
  33. * - Call GPIO_Init() function
  34. * Recommended configuration is Push-Pull, Pull-up, Open-Drain.
  35. * Add an external pull up if necessary (typically 4.7 KOhm).
  36. *
  37. * 4. Program the Mode, duty cycle , Own address, Ack, Speed and Acknowledged
  38. * Address using the I2C_Init() function.
  39. *
  40. * 5. Optionally you can enable/configure the following parameters without
  41. * re-initialization (i.e there is no need to call again I2C_Init() function):
  42. * - Enable the acknowledge feature using I2C_AcknowledgeConfig() function
  43. * - Enable the dual addressing mode using I2C_DualAddressCmd() function
  44. * - Enable the general call using the I2C_GeneralCallCmd() function
  45. * - Enable the clock stretching using I2C_StretchClockCmd() function
  46. * - Enable the fast mode duty cycle using the I2C_FastModeDutyCycleConfig()
  47. * function.
  48. * - Configure the NACK position for Master Receiver mode in case of
  49. * 2 bytes reception using the function I2C_NACKPositionConfig().
  50. * - Enable the PEC Calculation using I2C_CalculatePEC() function
  51. * - For SMBus Mode:
  52. * - Enable the Address Resolution Protocol (ARP) using I2C_ARPCmd() function
  53. * - Configure the SMBusAlert pin using I2C_SMBusAlertConfig() function
  54. *
  55. * 6. Enable the NVIC and the corresponding interrupt using the function
  56. * I2C_ITConfig() if you need to use interrupt mode.
  57. *
  58. * 7. When using the DMA mode
  59. * - Configure the DMA using DMA_Init() function
  60. * - Active the needed channel Request using I2C_DMACmd() or
  61. * I2C_DMALastTransferCmd() function.
  62. * @note When using DMA mode, I2C interrupts may be used at the same time to
  63. * control the communication flow (Start/Stop/Ack... events and errors).
  64. *
  65. * 8. Enable the I2C using the I2C_Cmd() function.
  66. *
  67. * 9. Enable the DMA using the DMA_Cmd() function when using DMA mode in the
  68. * transfers.
  69. *
  70. * @endverbatim
  71. *
  72. ******************************************************************************
  73. * @attention
  74. *
  75. * Copyright (c) 2012 STMicroelectronics.
  76. * All rights reserved.
  77. *
  78. * This software is licensed under terms that can be found in the LICENSE file
  79. * in the root directory of this software component.
  80. * If no LICENSE file comes with this software, it is provided AS-IS.
  81. *
  82. ******************************************************************************
  83. */
  84. /* Includes ------------------------------------------------------------------*/
  85. #include "stm32f2xx_i2c.h"
  86. #include "stm32f2xx_rcc.h"
  87. /** @addtogroup STM32F2xx_StdPeriph_Driver
  88. * @{
  89. */
  90. /** @defgroup I2C
  91. * @brief I2C driver modules
  92. * @{
  93. */
  94. /* Private typedef -----------------------------------------------------------*/
  95. /* Private define ------------------------------------------------------------*/
  96. #define CR1_CLEAR_MASK ((uint16_t)0xFBF5) /*<! I2C registers Masks */
  97. #define FLAG_MASK ((uint32_t)0x00FFFFFF) /*<! I2C FLAG mask */
  98. #define ITEN_MASK ((uint32_t)0x07000000) /*<! I2C Interrupt Enable mask */
  99. /* Private macro -------------------------------------------------------------*/
  100. /* Private variables ---------------------------------------------------------*/
  101. /* Private function prototypes -----------------------------------------------*/
  102. /* Private functions ---------------------------------------------------------*/
  103. /** @defgroup I2C_Private_Functions
  104. * @{
  105. */
  106. /** @defgroup I2C_Group1 Initialization and Configuration functions
  107. * @brief Initialization and Configuration functions
  108. *
  109. @verbatim
  110. ===============================================================================
  111. Initialization and Configuration functions
  112. ===============================================================================
  113. @endverbatim
  114. * @{
  115. */
  116. /**
  117. * @brief Deinitialize the I2Cx peripheral registers to their default reset values.
  118. * @param I2Cx: where x can be 1, 2 or 3 to select the I2C peripheral.
  119. * @retval None
  120. */
  121. void I2C_DeInit(I2C_TypeDef* I2Cx)
  122. {
  123. /* Check the parameters */
  124. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  125. if (I2Cx == I2C1)
  126. {
  127. /* Enable I2C1 reset state */
  128. RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, ENABLE);
  129. /* Release I2C1 from reset state */
  130. RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, DISABLE);
  131. }
  132. else if (I2Cx == I2C2)
  133. {
  134. /* Enable I2C2 reset state */
  135. RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, ENABLE);
  136. /* Release I2C2 from reset state */
  137. RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, DISABLE);
  138. }
  139. else
  140. {
  141. if (I2Cx == I2C3)
  142. {
  143. /* Enable I2C3 reset state */
  144. RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C3, ENABLE);
  145. /* Release I2C3 from reset state */
  146. RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C3, DISABLE);
  147. }
  148. }
  149. }
  150. /**
  151. * @brief Initializes the I2Cx peripheral according to the specified
  152. * parameters in the I2C_InitStruct.
  153. *
  154. * @note To use the I2C at 400 KHz (in fast mode), the PCLK1 frequency
  155. * (I2C peripheral input clock) must be a multiple of 10 MHz.
  156. *
  157. * @param I2Cx: where x can be 1, 2 or 3 to select the I2C peripheral.
  158. * @param I2C_InitStruct: pointer to a I2C_InitTypeDef structure that contains
  159. * the configuration information for the specified I2C peripheral.
  160. * @retval None
  161. */
  162. void I2C_Init(I2C_TypeDef* I2Cx, I2C_InitTypeDef* I2C_InitStruct)
  163. {
  164. uint16_t tmpreg = 0, freqrange = 0;
  165. uint16_t result = 0x04;
  166. uint32_t pclk1 = 8000000;
  167. RCC_ClocksTypeDef rcc_clocks;
  168. /* Check the parameters */
  169. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  170. assert_param(IS_I2C_CLOCK_SPEED(I2C_InitStruct->I2C_ClockSpeed));
  171. assert_param(IS_I2C_MODE(I2C_InitStruct->I2C_Mode));
  172. assert_param(IS_I2C_DUTY_CYCLE(I2C_InitStruct->I2C_DutyCycle));
  173. assert_param(IS_I2C_OWN_ADDRESS1(I2C_InitStruct->I2C_OwnAddress1));
  174. assert_param(IS_I2C_ACK_STATE(I2C_InitStruct->I2C_Ack));
  175. assert_param(IS_I2C_ACKNOWLEDGE_ADDRESS(I2C_InitStruct->I2C_AcknowledgedAddress));
  176. /*---------------------------- I2Cx CR2 Configuration ------------------------*/
  177. /* Get the I2Cx CR2 value */
  178. tmpreg = I2Cx->CR2;
  179. /* Clear frequency FREQ[5:0] bits */
  180. tmpreg &= (uint16_t)~((uint16_t)I2C_CR2_FREQ);
  181. /* Get pclk1 frequency value */
  182. RCC_GetClocksFreq(&rcc_clocks);
  183. pclk1 = rcc_clocks.PCLK1_Frequency;
  184. /* Set frequency bits depending on pclk1 value */
  185. freqrange = (uint16_t)(pclk1 / 1000000);
  186. tmpreg |= freqrange;
  187. /* Write to I2Cx CR2 */
  188. I2Cx->CR2 = tmpreg;
  189. /*---------------------------- I2Cx CCR Configuration ------------------------*/
  190. /* Disable the selected I2C peripheral to configure TRISE */
  191. I2Cx->CR1 &= (uint16_t)~((uint16_t)I2C_CR1_PE);
  192. /* Reset tmpreg value */
  193. /* Clear F/S, DUTY and CCR[11:0] bits */
  194. tmpreg = 0;
  195. /* Configure speed in standard mode */
  196. if (I2C_InitStruct->I2C_ClockSpeed <= 100000)
  197. {
  198. /* Standard mode speed calculate */
  199. result = (uint16_t)(pclk1 / (I2C_InitStruct->I2C_ClockSpeed << 1));
  200. /* Test if CCR value is under 0x4*/
  201. if (result < 0x04)
  202. {
  203. /* Set minimum allowed value */
  204. result = 0x04;
  205. }
  206. /* Set speed value for standard mode */
  207. tmpreg |= result;
  208. /* Set Maximum Rise Time for standard mode */
  209. I2Cx->TRISE = freqrange + 1;
  210. }
  211. /* Configure speed in fast mode */
  212. /* To use the I2C at 400 KHz (in fast mode), the PCLK1 frequency (I2C peripheral
  213. input clock) must be a multiple of 10 MHz */
  214. else /*(I2C_InitStruct->I2C_ClockSpeed <= 400000)*/
  215. {
  216. if (I2C_InitStruct->I2C_DutyCycle == I2C_DutyCycle_2)
  217. {
  218. /* Fast mode speed calculate: Tlow/Thigh = 2 */
  219. result = (uint16_t)(pclk1 / (I2C_InitStruct->I2C_ClockSpeed * 3));
  220. }
  221. else /*I2C_InitStruct->I2C_DutyCycle == I2C_DutyCycle_16_9*/
  222. {
  223. /* Fast mode speed calculate: Tlow/Thigh = 16/9 */
  224. result = (uint16_t)(pclk1 / (I2C_InitStruct->I2C_ClockSpeed * 25));
  225. /* Set DUTY bit */
  226. result |= I2C_DutyCycle_16_9;
  227. }
  228. /* Test if CCR value is under 0x1*/
  229. if ((result & I2C_CCR_CCR) == 0)
  230. {
  231. /* Set minimum allowed value */
  232. result |= (uint16_t)0x0001;
  233. }
  234. /* Set speed value and set F/S bit for fast mode */
  235. tmpreg |= (uint16_t)(result | I2C_CCR_FS);
  236. /* Set Maximum Rise Time for fast mode */
  237. I2Cx->TRISE = (uint16_t)(((freqrange * (uint16_t)300) / (uint16_t)1000) + (uint16_t)1);
  238. }
  239. /* Write to I2Cx CCR */
  240. I2Cx->CCR = tmpreg;
  241. /* Enable the selected I2C peripheral */
  242. I2Cx->CR1 |= I2C_CR1_PE;
  243. /*---------------------------- I2Cx CR1 Configuration ------------------------*/
  244. /* Get the I2Cx CR1 value */
  245. tmpreg = I2Cx->CR1;
  246. /* Clear ACK, SMBTYPE and SMBUS bits */
  247. tmpreg &= CR1_CLEAR_MASK;
  248. /* Configure I2Cx: mode and acknowledgement */
  249. /* Set SMBTYPE and SMBUS bits according to I2C_Mode value */
  250. /* Set ACK bit according to I2C_Ack value */
  251. tmpreg |= (uint16_t)((uint32_t)I2C_InitStruct->I2C_Mode | I2C_InitStruct->I2C_Ack);
  252. /* Write to I2Cx CR1 */
  253. I2Cx->CR1 = tmpreg;
  254. /*---------------------------- I2Cx OAR1 Configuration -----------------------*/
  255. /* Set I2Cx Own Address1 and acknowledged address */
  256. I2Cx->OAR1 = (I2C_InitStruct->I2C_AcknowledgedAddress | I2C_InitStruct->I2C_OwnAddress1);
  257. }
  258. /**
  259. * @brief Fills each I2C_InitStruct member with its default value.
  260. * @param I2C_InitStruct: pointer to an I2C_InitTypeDef structure which will be initialized.
  261. * @retval None
  262. */
  263. void I2C_StructInit(I2C_InitTypeDef* I2C_InitStruct)
  264. {
  265. /*---------------- Reset I2C init structure parameters values ----------------*/
  266. /* initialize the I2C_ClockSpeed member */
  267. I2C_InitStruct->I2C_ClockSpeed = 5000;
  268. /* Initialize the I2C_Mode member */
  269. I2C_InitStruct->I2C_Mode = I2C_Mode_I2C;
  270. /* Initialize the I2C_DutyCycle member */
  271. I2C_InitStruct->I2C_DutyCycle = I2C_DutyCycle_2;
  272. /* Initialize the I2C_OwnAddress1 member */
  273. I2C_InitStruct->I2C_OwnAddress1 = 0;
  274. /* Initialize the I2C_Ack member */
  275. I2C_InitStruct->I2C_Ack = I2C_Ack_Disable;
  276. /* Initialize the I2C_AcknowledgedAddress member */
  277. I2C_InitStruct->I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
  278. }
  279. /**
  280. * @brief Enables or disables the specified I2C peripheral.
  281. * @param I2Cx: where x can be 1, 2 or 3 to select the I2C peripheral.
  282. * @param NewState: new state of the I2Cx peripheral.
  283. * This parameter can be: ENABLE or DISABLE.
  284. * @retval None
  285. */
  286. void I2C_Cmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
  287. {
  288. /* Check the parameters */
  289. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  290. assert_param(IS_FUNCTIONAL_STATE(NewState));
  291. if (NewState != DISABLE)
  292. {
  293. /* Enable the selected I2C peripheral */
  294. I2Cx->CR1 |= I2C_CR1_PE;
  295. }
  296. else
  297. {
  298. /* Disable the selected I2C peripheral */
  299. I2Cx->CR1 &= (uint16_t)~((uint16_t)I2C_CR1_PE);
  300. }
  301. }
  302. /**
  303. * @brief Generates I2Cx communication START condition.
  304. * @param I2Cx: where x can be 1, 2 or 3 to select the I2C peripheral.
  305. * @param NewState: new state of the I2C START condition generation.
  306. * This parameter can be: ENABLE or DISABLE.
  307. * @retval None.
  308. */
  309. void I2C_GenerateSTART(I2C_TypeDef* I2Cx, FunctionalState NewState)
  310. {
  311. /* Check the parameters */
  312. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  313. assert_param(IS_FUNCTIONAL_STATE(NewState));
  314. if (NewState != DISABLE)
  315. {
  316. /* Generate a START condition */
  317. I2Cx->CR1 |= I2C_CR1_START;
  318. }
  319. else
  320. {
  321. /* Disable the START condition generation */
  322. I2Cx->CR1 &= (uint16_t)~((uint16_t)I2C_CR1_START);
  323. }
  324. }
  325. /**
  326. * @brief Generates I2Cx communication STOP condition.
  327. * @param I2Cx: where x can be 1, 2 or 3 to select the I2C peripheral.
  328. * @param NewState: new state of the I2C STOP condition generation.
  329. * This parameter can be: ENABLE or DISABLE.
  330. * @retval None.
  331. */
  332. void I2C_GenerateSTOP(I2C_TypeDef* I2Cx, FunctionalState NewState)
  333. {
  334. /* Check the parameters */
  335. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  336. assert_param(IS_FUNCTIONAL_STATE(NewState));
  337. if (NewState != DISABLE)
  338. {
  339. /* Generate a STOP condition */
  340. I2Cx->CR1 |= I2C_CR1_STOP;
  341. }
  342. else
  343. {
  344. /* Disable the STOP condition generation */
  345. I2Cx->CR1 &= (uint16_t)~((uint16_t)I2C_CR1_STOP);
  346. }
  347. }
  348. /**
  349. * @brief Transmits the address byte to select the slave device.
  350. * @param I2Cx: where x can be 1, 2 or 3 to select the I2C peripheral.
  351. * @param Address: specifies the slave address which will be transmitted
  352. * @param I2C_Direction: specifies whether the I2C device will be a Transmitter
  353. * or a Receiver.
  354. * This parameter can be one of the following values
  355. * @arg I2C_Direction_Transmitter: Transmitter mode
  356. * @arg I2C_Direction_Receiver: Receiver mode
  357. * @retval None.
  358. */
  359. void I2C_Send7bitAddress(I2C_TypeDef* I2Cx, uint8_t Address, uint8_t I2C_Direction)
  360. {
  361. /* Check the parameters */
  362. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  363. assert_param(IS_I2C_DIRECTION(I2C_Direction));
  364. /* Test on the direction to set/reset the read/write bit */
  365. if (I2C_Direction != I2C_Direction_Transmitter)
  366. {
  367. /* Set the address bit0 for read */
  368. Address |= I2C_OAR1_ADD0;
  369. }
  370. else
  371. {
  372. /* Reset the address bit0 for write */
  373. Address &= (uint8_t)~((uint8_t)I2C_OAR1_ADD0);
  374. }
  375. /* Send the address */
  376. I2Cx->DR = Address;
  377. }
  378. /**
  379. * @brief Enables or disables the specified I2C acknowledge feature.
  380. * @param I2Cx: where x can be 1, 2 or 3 to select the I2C peripheral.
  381. * @param NewState: new state of the I2C Acknowledgement.
  382. * This parameter can be: ENABLE or DISABLE.
  383. * @retval None.
  384. */
  385. void I2C_AcknowledgeConfig(I2C_TypeDef* I2Cx, FunctionalState NewState)
  386. {
  387. /* Check the parameters */
  388. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  389. assert_param(IS_FUNCTIONAL_STATE(NewState));
  390. if (NewState != DISABLE)
  391. {
  392. /* Enable the acknowledgement */
  393. I2Cx->CR1 |= I2C_CR1_ACK;
  394. }
  395. else
  396. {
  397. /* Disable the acknowledgement */
  398. I2Cx->CR1 &= (uint16_t)~((uint16_t)I2C_CR1_ACK);
  399. }
  400. }
  401. /**
  402. * @brief Configures the specified I2C own address2.
  403. * @param I2Cx: where x can be 1, 2 or 3 to select the I2C peripheral.
  404. * @param Address: specifies the 7bit I2C own address2.
  405. * @retval None.
  406. */
  407. void I2C_OwnAddress2Config(I2C_TypeDef* I2Cx, uint8_t Address)
  408. {
  409. uint16_t tmpreg = 0;
  410. /* Check the parameters */
  411. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  412. /* Get the old register value */
  413. tmpreg = I2Cx->OAR2;
  414. /* Reset I2Cx Own address2 bit [7:1] */
  415. tmpreg &= (uint16_t)~((uint16_t)I2C_OAR2_ADD2);
  416. /* Set I2Cx Own address2 */
  417. tmpreg |= (uint16_t)((uint16_t)Address & (uint16_t)0x00FE);
  418. /* Store the new register value */
  419. I2Cx->OAR2 = tmpreg;
  420. }
  421. /**
  422. * @brief Enables or disables the specified I2C dual addressing mode.
  423. * @param I2Cx: where x can be 1, 2 or 3 to select the I2C peripheral.
  424. * @param NewState: new state of the I2C dual addressing mode.
  425. * This parameter can be: ENABLE or DISABLE.
  426. * @retval None
  427. */
  428. void I2C_DualAddressCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
  429. {
  430. /* Check the parameters */
  431. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  432. assert_param(IS_FUNCTIONAL_STATE(NewState));
  433. if (NewState != DISABLE)
  434. {
  435. /* Enable dual addressing mode */
  436. I2Cx->OAR2 |= I2C_OAR2_ENDUAL;
  437. }
  438. else
  439. {
  440. /* Disable dual addressing mode */
  441. I2Cx->OAR2 &= (uint16_t)~((uint16_t)I2C_OAR2_ENDUAL);
  442. }
  443. }
  444. /**
  445. * @brief Enables or disables the specified I2C general call feature.
  446. * @param I2Cx: where x can be 1, 2 or 3 to select the I2C peripheral.
  447. * @param NewState: new state of the I2C General call.
  448. * This parameter can be: ENABLE or DISABLE.
  449. * @retval None
  450. */
  451. void I2C_GeneralCallCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
  452. {
  453. /* Check the parameters */
  454. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  455. assert_param(IS_FUNCTIONAL_STATE(NewState));
  456. if (NewState != DISABLE)
  457. {
  458. /* Enable generall call */
  459. I2Cx->CR1 |= I2C_CR1_ENGC;
  460. }
  461. else
  462. {
  463. /* Disable generall call */
  464. I2Cx->CR1 &= (uint16_t)~((uint16_t)I2C_CR1_ENGC);
  465. }
  466. }
  467. /**
  468. * @brief Enables or disables the specified I2C software reset.
  469. * @note When software reset is enabled, the I2C IOs are released (this can
  470. * be useful to recover from bus errors).
  471. * @param I2Cx: where x can be 1, 2 or 3 to select the I2C peripheral.
  472. * @param NewState: new state of the I2C software reset.
  473. * This parameter can be: ENABLE or DISABLE.
  474. * @retval None
  475. */
  476. void I2C_SoftwareResetCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
  477. {
  478. /* Check the parameters */
  479. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  480. assert_param(IS_FUNCTIONAL_STATE(NewState));
  481. if (NewState != DISABLE)
  482. {
  483. /* Peripheral under reset */
  484. I2Cx->CR1 |= I2C_CR1_SWRST;
  485. }
  486. else
  487. {
  488. /* Peripheral not under reset */
  489. I2Cx->CR1 &= (uint16_t)~((uint16_t)I2C_CR1_SWRST);
  490. }
  491. }
  492. /**
  493. * @brief Enables or disables the specified I2C Clock stretching.
  494. * @param I2Cx: where x can be 1, 2 or 3 to select the I2C peripheral.
  495. * @param NewState: new state of the I2Cx Clock stretching.
  496. * This parameter can be: ENABLE or DISABLE.
  497. * @retval None
  498. */
  499. void I2C_StretchClockCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
  500. {
  501. /* Check the parameters */
  502. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  503. assert_param(IS_FUNCTIONAL_STATE(NewState));
  504. if (NewState == DISABLE)
  505. {
  506. /* Enable the selected I2C Clock stretching */
  507. I2Cx->CR1 |= I2C_CR1_NOSTRETCH;
  508. }
  509. else
  510. {
  511. /* Disable the selected I2C Clock stretching */
  512. I2Cx->CR1 &= (uint16_t)~((uint16_t)I2C_CR1_NOSTRETCH);
  513. }
  514. }
  515. /**
  516. * @brief Selects the specified I2C fast mode duty cycle.
  517. * @param I2Cx: where x can be 1, 2 or 3 to select the I2C peripheral.
  518. * @param I2C_DutyCycle: specifies the fast mode duty cycle.
  519. * This parameter can be one of the following values:
  520. * @arg I2C_DutyCycle_2: I2C fast mode Tlow/Thigh = 2
  521. * @arg I2C_DutyCycle_16_9: I2C fast mode Tlow/Thigh = 16/9
  522. * @retval None
  523. */
  524. void I2C_FastModeDutyCycleConfig(I2C_TypeDef* I2Cx, uint16_t I2C_DutyCycle)
  525. {
  526. /* Check the parameters */
  527. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  528. assert_param(IS_I2C_DUTY_CYCLE(I2C_DutyCycle));
  529. if (I2C_DutyCycle != I2C_DutyCycle_16_9)
  530. {
  531. /* I2C fast mode Tlow/Thigh=2 */
  532. I2Cx->CCR &= I2C_DutyCycle_2;
  533. }
  534. else
  535. {
  536. /* I2C fast mode Tlow/Thigh=16/9 */
  537. I2Cx->CCR |= I2C_DutyCycle_16_9;
  538. }
  539. }
  540. /**
  541. * @brief Selects the specified I2C NACK position in master receiver mode.
  542. * @note This function is useful in I2C Master Receiver mode when the number
  543. * of data to be received is equal to 2. In this case, this function
  544. * should be called (with parameter I2C_NACKPosition_Next) before data
  545. * reception starts,as described in the 2-byte reception procedure
  546. * recommended in Reference Manual in Section: Master receiver.
  547. * @param I2Cx: where x can be 1, 2 or 3 to select the I2C peripheral.
  548. * @param I2C_NACKPosition: specifies the NACK position.
  549. * This parameter can be one of the following values:
  550. * @arg I2C_NACKPosition_Next: indicates that the next byte will be the last
  551. * received byte.
  552. * @arg I2C_NACKPosition_Current: indicates that current byte is the last
  553. * received byte.
  554. *
  555. * @note This function configures the same bit (POS) as I2C_PECPositionConfig()
  556. * but is intended to be used in I2C mode while I2C_PECPositionConfig()
  557. * is intended to used in SMBUS mode.
  558. *
  559. * @retval None
  560. */
  561. void I2C_NACKPositionConfig(I2C_TypeDef* I2Cx, uint16_t I2C_NACKPosition)
  562. {
  563. /* Check the parameters */
  564. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  565. assert_param(IS_I2C_NACK_POSITION(I2C_NACKPosition));
  566. /* Check the input parameter */
  567. if (I2C_NACKPosition == I2C_NACKPosition_Next)
  568. {
  569. /* Next byte in shift register is the last received byte */
  570. I2Cx->CR1 |= I2C_NACKPosition_Next;
  571. }
  572. else
  573. {
  574. /* Current byte in shift register is the last received byte */
  575. I2Cx->CR1 &= I2C_NACKPosition_Current;
  576. }
  577. }
  578. /**
  579. * @brief Drives the SMBusAlert pin high or low for the specified I2C.
  580. * @param I2Cx: where x can be 1, 2 or 3 to select the I2C peripheral.
  581. * @param I2C_SMBusAlert: specifies SMBAlert pin level.
  582. * This parameter can be one of the following values:
  583. * @arg I2C_SMBusAlert_Low: SMBAlert pin driven low
  584. * @arg I2C_SMBusAlert_High: SMBAlert pin driven high
  585. * @retval None
  586. */
  587. void I2C_SMBusAlertConfig(I2C_TypeDef* I2Cx, uint16_t I2C_SMBusAlert)
  588. {
  589. /* Check the parameters */
  590. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  591. assert_param(IS_I2C_SMBUS_ALERT(I2C_SMBusAlert));
  592. if (I2C_SMBusAlert == I2C_SMBusAlert_Low)
  593. {
  594. /* Drive the SMBusAlert pin Low */
  595. I2Cx->CR1 |= I2C_SMBusAlert_Low;
  596. }
  597. else
  598. {
  599. /* Drive the SMBusAlert pin High */
  600. I2Cx->CR1 &= I2C_SMBusAlert_High;
  601. }
  602. }
  603. /**
  604. * @brief Enables or disables the specified I2C ARP.
  605. * @param I2Cx: where x can be 1, 2 or 3 to select the I2C peripheral.
  606. * @param NewState: new state of the I2Cx ARP.
  607. * This parameter can be: ENABLE or DISABLE.
  608. * @retval None
  609. */
  610. void I2C_ARPCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
  611. {
  612. /* Check the parameters */
  613. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  614. assert_param(IS_FUNCTIONAL_STATE(NewState));
  615. if (NewState != DISABLE)
  616. {
  617. /* Enable the selected I2C ARP */
  618. I2Cx->CR1 |= I2C_CR1_ENARP;
  619. }
  620. else
  621. {
  622. /* Disable the selected I2C ARP */
  623. I2Cx->CR1 &= (uint16_t)~((uint16_t)I2C_CR1_ENARP);
  624. }
  625. }
  626. /**
  627. * @}
  628. */
  629. /** @defgroup I2C_Group2 Data transfers functions
  630. * @brief Data transfers functions
  631. *
  632. @verbatim
  633. ===============================================================================
  634. Data transfers functions
  635. ===============================================================================
  636. @endverbatim
  637. * @{
  638. */
  639. /**
  640. * @brief Sends a data byte through the I2Cx peripheral.
  641. * @param I2Cx: where x can be 1, 2 or 3 to select the I2C peripheral.
  642. * @param Data: Byte to be transmitted..
  643. * @retval None
  644. */
  645. void I2C_SendData(I2C_TypeDef* I2Cx, uint8_t Data)
  646. {
  647. /* Check the parameters */
  648. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  649. /* Write in the DR register the data to be sent */
  650. I2Cx->DR = Data;
  651. }
  652. /**
  653. * @brief Returns the most recent received data by the I2Cx peripheral.
  654. * @param I2Cx: where x can be 1, 2 or 3 to select the I2C peripheral.
  655. * @retval The value of the received data.
  656. */
  657. uint8_t I2C_ReceiveData(I2C_TypeDef* I2Cx)
  658. {
  659. /* Check the parameters */
  660. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  661. /* Return the data in the DR register */
  662. return (uint8_t)I2Cx->DR;
  663. }
  664. /**
  665. * @}
  666. */
  667. /** @defgroup I2C_Group3 PEC management functions
  668. * @brief PEC management functions
  669. *
  670. @verbatim
  671. ===============================================================================
  672. PEC management functions
  673. ===============================================================================
  674. @endverbatim
  675. * @{
  676. */
  677. /**
  678. * @brief Enables or disables the specified I2C PEC transfer.
  679. * @param I2Cx: where x can be 1, 2 or 3 to select the I2C peripheral.
  680. * @param NewState: new state of the I2C PEC transmission.
  681. * This parameter can be: ENABLE or DISABLE.
  682. * @retval None
  683. */
  684. void I2C_TransmitPEC(I2C_TypeDef* I2Cx, FunctionalState NewState)
  685. {
  686. /* Check the parameters */
  687. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  688. assert_param(IS_FUNCTIONAL_STATE(NewState));
  689. if (NewState != DISABLE)
  690. {
  691. /* Enable the selected I2C PEC transmission */
  692. I2Cx->CR1 |= I2C_CR1_PEC;
  693. }
  694. else
  695. {
  696. /* Disable the selected I2C PEC transmission */
  697. I2Cx->CR1 &= (uint16_t)~((uint16_t)I2C_CR1_PEC);
  698. }
  699. }
  700. /**
  701. * @brief Selects the specified I2C PEC position.
  702. * @param I2Cx: where x can be 1, 2 or 3 to select the I2C peripheral.
  703. * @param I2C_PECPosition: specifies the PEC position.
  704. * This parameter can be one of the following values:
  705. * @arg I2C_PECPosition_Next: indicates that the next byte is PEC
  706. * @arg I2C_PECPosition_Current: indicates that current byte is PEC
  707. *
  708. * @note This function configures the same bit (POS) as I2C_NACKPositionConfig()
  709. * but is intended to be used in SMBUS mode while I2C_NACKPositionConfig()
  710. * is intended to used in I2C mode.
  711. *
  712. * @retval None
  713. */
  714. void I2C_PECPositionConfig(I2C_TypeDef* I2Cx, uint16_t I2C_PECPosition)
  715. {
  716. /* Check the parameters */
  717. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  718. assert_param(IS_I2C_PEC_POSITION(I2C_PECPosition));
  719. if (I2C_PECPosition == I2C_PECPosition_Next)
  720. {
  721. /* Next byte in shift register is PEC */
  722. I2Cx->CR1 |= I2C_PECPosition_Next;
  723. }
  724. else
  725. {
  726. /* Current byte in shift register is PEC */
  727. I2Cx->CR1 &= I2C_PECPosition_Current;
  728. }
  729. }
  730. /**
  731. * @brief Enables or disables the PEC value calculation of the transferred bytes.
  732. * @param I2Cx: where x can be 1, 2 or 3 to select the I2C peripheral.
  733. * @param NewState: new state of the I2Cx PEC value calculation.
  734. * This parameter can be: ENABLE or DISABLE.
  735. * @retval None
  736. */
  737. void I2C_CalculatePEC(I2C_TypeDef* I2Cx, FunctionalState NewState)
  738. {
  739. /* Check the parameters */
  740. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  741. assert_param(IS_FUNCTIONAL_STATE(NewState));
  742. if (NewState != DISABLE)
  743. {
  744. /* Enable the selected I2C PEC calculation */
  745. I2Cx->CR1 |= I2C_CR1_ENPEC;
  746. }
  747. else
  748. {
  749. /* Disable the selected I2C PEC calculation */
  750. I2Cx->CR1 &= (uint16_t)~((uint16_t)I2C_CR1_ENPEC);
  751. }
  752. }
  753. /**
  754. * @brief Returns the PEC value for the specified I2C.
  755. * @param I2Cx: where x can be 1, 2 or 3 to select the I2C peripheral.
  756. * @retval The PEC value.
  757. */
  758. uint8_t I2C_GetPEC(I2C_TypeDef* I2Cx)
  759. {
  760. /* Check the parameters */
  761. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  762. /* Return the selected I2C PEC value */
  763. return ((I2Cx->SR2) >> 8);
  764. }
  765. /**
  766. * @}
  767. */
  768. /** @defgroup I2C_Group4 DMA transfers management functions
  769. * @brief DMA transfers management functions
  770. *
  771. @verbatim
  772. ===============================================================================
  773. DMA transfers management functions
  774. ===============================================================================
  775. This section provides functions allowing to configure the I2C DMA channels
  776. requests.
  777. @endverbatim
  778. * @{
  779. */
  780. /**
  781. * @brief Enables or disables the specified I2C DMA requests.
  782. * @param I2Cx: where x can be 1, 2 or 3 to select the I2C peripheral.
  783. * @param NewState: new state of the I2C DMA transfer.
  784. * This parameter can be: ENABLE or DISABLE.
  785. * @retval None
  786. */
  787. void I2C_DMACmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
  788. {
  789. /* Check the parameters */
  790. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  791. assert_param(IS_FUNCTIONAL_STATE(NewState));
  792. if (NewState != DISABLE)
  793. {
  794. /* Enable the selected I2C DMA requests */
  795. I2Cx->CR2 |= I2C_CR2_DMAEN;
  796. }
  797. else
  798. {
  799. /* Disable the selected I2C DMA requests */
  800. I2Cx->CR2 &= (uint16_t)~((uint16_t)I2C_CR2_DMAEN);
  801. }
  802. }
  803. /**
  804. * @brief Specifies that the next DMA transfer is the last one.
  805. * @param I2Cx: where x can be 1, 2 or 3 to select the I2C peripheral.
  806. * @param NewState: new state of the I2C DMA last transfer.
  807. * This parameter can be: ENABLE or DISABLE.
  808. * @retval None
  809. */
  810. void I2C_DMALastTransferCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
  811. {
  812. /* Check the parameters */
  813. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  814. assert_param(IS_FUNCTIONAL_STATE(NewState));
  815. if (NewState != DISABLE)
  816. {
  817. /* Next DMA transfer is the last transfer */
  818. I2Cx->CR2 |= I2C_CR2_LAST;
  819. }
  820. else
  821. {
  822. /* Next DMA transfer is not the last transfer */
  823. I2Cx->CR2 &= (uint16_t)~((uint16_t)I2C_CR2_LAST);
  824. }
  825. }
  826. /**
  827. * @}
  828. */
  829. /** @defgroup I2C_Group5 Interrupts events and flags management functions
  830. * @brief Interrupts, events and flags management functions
  831. *
  832. @verbatim
  833. ===============================================================================
  834. Interrupts, events and flags management functions
  835. ===============================================================================
  836. This section provides functions allowing to configure the I2C Interrupts
  837. sources and check or clear the flags or pending bits status.
  838. The user should identify which mode will be used in his application to manage
  839. the communication: Polling mode, Interrupt mode or DMA mode.
  840. ===============================================================================
  841. I2C State Monitoring Functions
  842. ===============================================================================
  843. This I2C driver provides three different ways for I2C state monitoring
  844. depending on the application requirements and constraints:
  845. 1. Basic state monitoring (Using I2C_CheckEvent() function)
  846. -----------------------------------------------------------
  847. It compares the status registers (SR1 and SR2) content to a given event
  848. (can be the combination of one or more flags).
  849. It returns SUCCESS if the current status includes the given flags
  850. and returns ERROR if one or more flags are missing in the current status.
  851. - When to use
  852. - This function is suitable for most applications as well as for startup
  853. activity since the events are fully described in the product reference
  854. manual (RM0033).
  855. - It is also suitable for users who need to define their own events.
  856. - Limitations
  857. - If an error occurs (ie. error flags are set besides to the monitored
  858. flags), the I2C_CheckEvent() function may return SUCCESS despite
  859. the communication hold or corrupted real state.
  860. In this case, it is advised to use error interrupts to monitor
  861. the error events and handle them in the interrupt IRQ handler.
  862. @note
  863. For error management, it is advised to use the following functions:
  864. - I2C_ITConfig() to configure and enable the error interrupts (I2C_IT_ERR).
  865. - I2Cx_ER_IRQHandler() which is called when the error interrupt occurs.
  866. Where x is the peripheral instance (I2C1, I2C2 ...)
  867. - I2C_GetFlagStatus() or I2C_GetITStatus() to be called into the
  868. I2Cx_ER_IRQHandler() function in order to determine which error occurred.
  869. - I2C_ClearFlag() or I2C_ClearITPendingBit() and/or I2C_SoftwareResetCmd()
  870. and/or I2C_GenerateStop() in order to clear the error flag and source
  871. and return to correct communication status.
  872. 2. Advanced state monitoring (Using the function I2C_GetLastEvent())
  873. --------------------------------------------------------------------
  874. Using the function I2C_GetLastEvent() which returns the image of both status
  875. registers in a single word (uint32_t) (Status Register 2 value is shifted left
  876. by 16 bits and concatenated to Status Register 1).
  877. - When to use
  878. - This function is suitable for the same applications above but it
  879. allows to overcome the mentioned limitation of I2C_GetFlagStatus()
  880. function.
  881. - The returned value could be compared to events already defined in
  882. the library (stm32f2xx_i2c.h) or to custom values defined by user.
  883. This function is suitable when multiple flags are monitored at the
  884. same time.
  885. - At the opposite of I2C_CheckEvent() function, this function allows
  886. user to choose when an event is accepted (when all events flags are
  887. set and no other flags are set or just when the needed flags are set
  888. like I2C_CheckEvent() function.
  889. - Limitations
  890. - User may need to define his own events.
  891. - Same remark concerning the error management is applicable for this
  892. function if user decides to check only regular communication flags
  893. (and ignores error flags).
  894. 3. Flag-based state monitoring (Using the function I2C_GetFlagStatus())
  895. -----------------------------------------------------------------------
  896. Using the function I2C_GetFlagStatus() which simply returns the status of
  897. one single flag (ie. I2C_FLAG_RXNE ...).
  898. - When to use
  899. - This function could be used for specific applications or in debug
  900. phase.
  901. - It is suitable when only one flag checking is needed (most I2C
  902. events are monitored through multiple flags).
  903. - Limitations:
  904. - When calling this function, the Status register is accessed.
  905. Some flags are cleared when the status register is accessed.
  906. So checking the status of one Flag, may clear other ones.
  907. - Function may need to be called twice or more in order to monitor
  908. one single event.
  909. For detailed description of Events, please refer to section I2C_Events in
  910. stm32f2xx_i2c.h file.
  911. @endverbatim
  912. * @{
  913. */
  914. /**
  915. * @brief Reads the specified I2C register and returns its value.
  916. * @param I2C_Register: specifies the register to read.
  917. * This parameter can be one of the following values:
  918. * @arg I2C_Register_CR1: CR1 register.
  919. * @arg I2C_Register_CR2: CR2 register.
  920. * @arg I2C_Register_OAR1: OAR1 register.
  921. * @arg I2C_Register_OAR2: OAR2 register.
  922. * @arg I2C_Register_DR: DR register.
  923. * @arg I2C_Register_SR1: SR1 register.
  924. * @arg I2C_Register_SR2: SR2 register.
  925. * @arg I2C_Register_CCR: CCR register.
  926. * @arg I2C_Register_TRISE: TRISE register.
  927. * @retval The value of the read register.
  928. */
  929. uint16_t I2C_ReadRegister(I2C_TypeDef* I2Cx, uint8_t I2C_Register)
  930. {
  931. __IO uint32_t tmp = 0;
  932. /* Check the parameters */
  933. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  934. assert_param(IS_I2C_REGISTER(I2C_Register));
  935. tmp = (uint32_t) I2Cx;
  936. tmp += I2C_Register;
  937. /* Return the selected register value */
  938. return (*(__IO uint16_t *) tmp);
  939. }
  940. /**
  941. * @brief Enables or disables the specified I2C interrupts.
  942. * @param I2Cx: where x can be 1, 2 or 3 to select the I2C peripheral.
  943. * @param I2C_IT: specifies the I2C interrupts sources to be enabled or disabled.
  944. * This parameter can be any combination of the following values:
  945. * @arg I2C_IT_BUF: Buffer interrupt mask
  946. * @arg I2C_IT_EVT: Event interrupt mask
  947. * @arg I2C_IT_ERR: Error interrupt mask
  948. * @param NewState: new state of the specified I2C interrupts.
  949. * This parameter can be: ENABLE or DISABLE.
  950. * @retval None
  951. */
  952. void I2C_ITConfig(I2C_TypeDef* I2Cx, uint16_t I2C_IT, FunctionalState NewState)
  953. {
  954. /* Check the parameters */
  955. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  956. assert_param(IS_FUNCTIONAL_STATE(NewState));
  957. assert_param(IS_I2C_CONFIG_IT(I2C_IT));
  958. if (NewState != DISABLE)
  959. {
  960. /* Enable the selected I2C interrupts */
  961. I2Cx->CR2 |= I2C_IT;
  962. }
  963. else
  964. {
  965. /* Disable the selected I2C interrupts */
  966. I2Cx->CR2 &= (uint16_t)~I2C_IT;
  967. }
  968. }
  969. /*
  970. ===============================================================================
  971. 1. Basic state monitoring
  972. ===============================================================================
  973. */
  974. /**
  975. * @brief Checks whether the last I2Cx Event is equal to the one passed
  976. * as parameter.
  977. * @param I2Cx: where x can be 1, 2 or 3 to select the I2C peripheral.
  978. * @param I2C_EVENT: specifies the event to be checked.
  979. * This parameter can be one of the following values:
  980. * @arg I2C_EVENT_SLAVE_TRANSMITTER_ADDRESS_MATCHED: EV1
  981. * @arg I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED: EV1
  982. * @arg I2C_EVENT_SLAVE_TRANSMITTER_SECONDADDRESS_MATCHED: EV1
  983. * @arg I2C_EVENT_SLAVE_RECEIVER_SECONDADDRESS_MATCHED: EV1
  984. * @arg I2C_EVENT_SLAVE_GENERALCALLADDRESS_MATCHED: EV1
  985. * @arg I2C_EVENT_SLAVE_BYTE_RECEIVED: EV2
  986. * @arg (I2C_EVENT_SLAVE_BYTE_RECEIVED | I2C_FLAG_DUALF): EV2
  987. * @arg (I2C_EVENT_SLAVE_BYTE_RECEIVED | I2C_FLAG_GENCALL): EV2
  988. * @arg I2C_EVENT_SLAVE_BYTE_TRANSMITTED: EV3
  989. * @arg (I2C_EVENT_SLAVE_BYTE_TRANSMITTED | I2C_FLAG_DUALF): EV3
  990. * @arg (I2C_EVENT_SLAVE_BYTE_TRANSMITTED | I2C_FLAG_GENCALL): EV3
  991. * @arg I2C_EVENT_SLAVE_ACK_FAILURE: EV3_2
  992. * @arg I2C_EVENT_SLAVE_STOP_DETECTED: EV4
  993. * @arg I2C_EVENT_MASTER_MODE_SELECT: EV5
  994. * @arg I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED: EV6
  995. * @arg I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED: EV6
  996. * @arg I2C_EVENT_MASTER_BYTE_RECEIVED: EV7
  997. * @arg I2C_EVENT_MASTER_BYTE_TRANSMITTING: EV8
  998. * @arg I2C_EVENT_MASTER_BYTE_TRANSMITTED: EV8_2
  999. * @arg I2C_EVENT_MASTER_MODE_ADDRESS10: EV9
  1000. *
  1001. * @note For detailed description of Events, please refer to section I2C_Events
  1002. * in stm32f2xx_i2c.h file.
  1003. *
  1004. * @retval An ErrorStatus enumeration value:
  1005. * - SUCCESS: Last event is equal to the I2C_EVENT
  1006. * - ERROR: Last event is different from the I2C_EVENT
  1007. */
  1008. ErrorStatus I2C_CheckEvent(I2C_TypeDef* I2Cx, uint32_t I2C_EVENT)
  1009. {
  1010. uint32_t lastevent = 0;
  1011. uint32_t flag1 = 0, flag2 = 0;
  1012. ErrorStatus status = ERROR;
  1013. /* Check the parameters */
  1014. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  1015. assert_param(IS_I2C_EVENT(I2C_EVENT));
  1016. /* Read the I2Cx status register */
  1017. flag1 = I2Cx->SR1;
  1018. /* I2C_SR2 must be read only when ADDR is found set in I2C_SR1 or when the STOPF bit is cleared */
  1019. if((flag1 & I2C_SR1_ADDR) || (flag1 & ~I2C_SR1_STOPF))
  1020. {
  1021. flag2 = I2Cx->SR2;
  1022. }
  1023. else
  1024. {
  1025. return ERROR;
  1026. }
  1027. flag2 = flag2 << 16;
  1028. /* Get the last event value from I2C status register */
  1029. lastevent = (flag1 | flag2) & FLAG_MASK;
  1030. /* Check whether the last event contains the I2C_EVENT */
  1031. if ((lastevent & I2C_EVENT) == I2C_EVENT)
  1032. {
  1033. /* SUCCESS: last event is equal to I2C_EVENT */
  1034. status = SUCCESS;
  1035. }
  1036. else
  1037. {
  1038. /* ERROR: last event is different from I2C_EVENT */
  1039. status = ERROR;
  1040. }
  1041. /* Return status */
  1042. return status;
  1043. }
  1044. /*
  1045. ===============================================================================
  1046. 2. Advanced state monitoring
  1047. ===============================================================================
  1048. */
  1049. /**
  1050. * @brief Returns the last I2Cx Event.
  1051. * @param I2Cx: where x can be 1, 2 or 3 to select the I2C peripheral.
  1052. *
  1053. * @note For detailed description of Events, please refer to section I2C_Events
  1054. * in stm32f2xx_i2c.h file.
  1055. *
  1056. * @retval The last event
  1057. */
  1058. uint32_t I2C_GetLastEvent(I2C_TypeDef* I2Cx)
  1059. {
  1060. uint32_t lastevent = 0;
  1061. uint32_t flag1 = 0, flag2 = 0;
  1062. /* Check the parameters */
  1063. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  1064. /* Read the I2Cx status register */
  1065. flag1 = I2Cx->SR1;
  1066. flag2 = I2Cx->SR2;
  1067. flag2 = flag2 << 16;
  1068. /* Get the last event value from I2C status register */
  1069. lastevent = (flag1 | flag2) & FLAG_MASK;
  1070. /* Return status */
  1071. return lastevent;
  1072. }
  1073. /*
  1074. ===============================================================================
  1075. 3. Flag-based state monitoring
  1076. ===============================================================================
  1077. */
  1078. /**
  1079. * @brief Checks whether the specified I2C flag is set or not.
  1080. * @param I2Cx: where x can be 1, 2 or 3 to select the I2C peripheral.
  1081. * @param I2C_FLAG: specifies the flag to check.
  1082. * This parameter can be one of the following values:
  1083. * @arg I2C_FLAG_DUALF: Dual flag (Slave mode)
  1084. * @arg I2C_FLAG_SMBHOST: SMBus host header (Slave mode)
  1085. * @arg I2C_FLAG_SMBDEFAULT: SMBus default header (Slave mode)
  1086. * @arg I2C_FLAG_GENCALL: General call header flag (Slave mode)
  1087. * @arg I2C_FLAG_TRA: Transmitter/Receiver flag
  1088. * @arg I2C_FLAG_BUSY: Bus busy flag
  1089. * @arg I2C_FLAG_MSL: Master/Slave flag
  1090. * @arg I2C_FLAG_SMBALERT: SMBus Alert flag
  1091. * @arg I2C_FLAG_TIMEOUT: Timeout or Tlow error flag
  1092. * @arg I2C_FLAG_PECERR: PEC error in reception flag
  1093. * @arg I2C_FLAG_OVR: Overrun/Underrun flag (Slave mode)
  1094. * @arg I2C_FLAG_AF: Acknowledge failure flag
  1095. * @arg I2C_FLAG_ARLO: Arbitration lost flag (Master mode)
  1096. * @arg I2C_FLAG_BERR: Bus error flag
  1097. * @arg I2C_FLAG_TXE: Data register empty flag (Transmitter)
  1098. * @arg I2C_FLAG_RXNE: Data register not empty (Receiver) flag
  1099. * @arg I2C_FLAG_STOPF: Stop detection flag (Slave mode)
  1100. * @arg I2C_FLAG_ADD10: 10-bit header sent flag (Master mode)
  1101. * @arg I2C_FLAG_BTF: Byte transfer finished flag
  1102. * @arg I2C_FLAG_ADDR: Address sent flag (Master mode) "ADSL"
  1103. * Address matched flag (Slave mode)"ENDAD"
  1104. * @arg I2C_FLAG_SB: Start bit flag (Master mode)
  1105. * @retval The new state of I2C_FLAG (SET or RESET).
  1106. */
  1107. FlagStatus I2C_GetFlagStatus(I2C_TypeDef* I2Cx, uint32_t I2C_FLAG)
  1108. {
  1109. FlagStatus bitstatus = RESET;
  1110. __IO uint32_t i2creg = 0, i2cxbase = 0;
  1111. /* Check the parameters */
  1112. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  1113. assert_param(IS_I2C_GET_FLAG(I2C_FLAG));
  1114. /* Get the I2Cx peripheral base address */
  1115. i2cxbase = (uint32_t)I2Cx;
  1116. /* Read flag register index */
  1117. i2creg = I2C_FLAG >> 28;
  1118. /* Get bit[23:0] of the flag */
  1119. I2C_FLAG &= FLAG_MASK;
  1120. if(i2creg != 0)
  1121. {
  1122. /* Get the I2Cx SR1 register address */
  1123. i2cxbase += 0x14;
  1124. }
  1125. else
  1126. {
  1127. /* Flag in I2Cx SR2 Register */
  1128. I2C_FLAG = (uint32_t)(I2C_FLAG >> 16);
  1129. /* Get the I2Cx SR2 register address */
  1130. i2cxbase += 0x18;
  1131. }
  1132. if(((*(__IO uint32_t *)i2cxbase) & I2C_FLAG) != (uint32_t)RESET)
  1133. {
  1134. /* I2C_FLAG is set */
  1135. bitstatus = SET;
  1136. }
  1137. else
  1138. {
  1139. /* I2C_FLAG is reset */
  1140. bitstatus = RESET;
  1141. }
  1142. /* Return the I2C_FLAG status */
  1143. return bitstatus;
  1144. }
  1145. /**
  1146. * @brief Clears the I2Cx's pending flags.
  1147. * @param I2Cx: where x can be 1, 2 or 3 to select the I2C peripheral.
  1148. * @param I2C_FLAG: specifies the flag to clear.
  1149. * This parameter can be any combination of the following values:
  1150. * @arg I2C_FLAG_SMBALERT: SMBus Alert flag
  1151. * @arg I2C_FLAG_TIMEOUT: Timeout or Tlow error flag
  1152. * @arg I2C_FLAG_PECERR: PEC error in reception flag
  1153. * @arg I2C_FLAG_OVR: Overrun/Underrun flag (Slave mode)
  1154. * @arg I2C_FLAG_AF: Acknowledge failure flag
  1155. * @arg I2C_FLAG_ARLO: Arbitration lost flag (Master mode)
  1156. * @arg I2C_FLAG_BERR: Bus error flag
  1157. *
  1158. * @note STOPF (STOP detection) is cleared by software sequence: a read operation
  1159. * to I2C_SR1 register (I2C_GetFlagStatus()) followed by a write operation
  1160. * to I2C_CR1 register (I2C_Cmd() to re-enable the I2C peripheral).
  1161. * @note ADD10 (10-bit header sent) is cleared by software sequence: a read
  1162. * operation to I2C_SR1 (I2C_GetFlagStatus()) followed by writing the
  1163. * second byte of the address in DR register.
  1164. * @note BTF (Byte Transfer Finished) is cleared by software sequence: a read
  1165. * operation to I2C_SR1 register (I2C_GetFlagStatus()) followed by a
  1166. * read/write to I2C_DR register (I2C_SendData()).
  1167. * @note ADDR (Address sent) is cleared by software sequence: a read operation to
  1168. * I2C_SR1 register (I2C_GetFlagStatus()) followed by a read operation to
  1169. * I2C_SR2 register ((void)(I2Cx->SR2)).
  1170. * @note SB (Start Bit) is cleared software sequence: a read operation to I2C_SR1
  1171. * register (I2C_GetFlagStatus()) followed by a write operation to I2C_DR
  1172. * register (I2C_SendData()).
  1173. *
  1174. * @retval None
  1175. */
  1176. void I2C_ClearFlag(I2C_TypeDef* I2Cx, uint32_t I2C_FLAG)
  1177. {
  1178. uint32_t flagpos = 0;
  1179. /* Check the parameters */
  1180. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  1181. assert_param(IS_I2C_CLEAR_FLAG(I2C_FLAG));
  1182. /* Get the I2C flag position */
  1183. flagpos = I2C_FLAG & FLAG_MASK;
  1184. /* Clear the selected I2C flag */
  1185. I2Cx->SR1 = (uint16_t)~flagpos;
  1186. }
  1187. /**
  1188. * @brief Checks whether the specified I2C interrupt has occurred or not.
  1189. * @param I2Cx: where x can be 1, 2 or 3 to select the I2C peripheral.
  1190. * @param I2C_IT: specifies the interrupt source to check.
  1191. * This parameter can be one of the following values:
  1192. * @arg I2C_IT_SMBALERT: SMBus Alert flag
  1193. * @arg I2C_IT_TIMEOUT: Timeout or Tlow error flag
  1194. * @arg I2C_IT_PECERR: PEC error in reception flag
  1195. * @arg I2C_IT_OVR: Overrun/Underrun flag (Slave mode)
  1196. * @arg I2C_IT_AF: Acknowledge failure flag
  1197. * @arg I2C_IT_ARLO: Arbitration lost flag (Master mode)
  1198. * @arg I2C_IT_BERR: Bus error flag
  1199. * @arg I2C_IT_TXE: Data register empty flag (Transmitter)
  1200. * @arg I2C_IT_RXNE: Data register not empty (Receiver) flag
  1201. * @arg I2C_IT_STOPF: Stop detection flag (Slave mode)
  1202. * @arg I2C_IT_ADD10: 10-bit header sent flag (Master mode)
  1203. * @arg I2C_IT_BTF: Byte transfer finished flag
  1204. * @arg I2C_IT_ADDR: Address sent flag (Master mode) "ADSL"
  1205. * Address matched flag (Slave mode)"ENDAD"
  1206. * @arg I2C_IT_SB: Start bit flag (Master mode)
  1207. * @retval The new state of I2C_IT (SET or RESET).
  1208. */
  1209. ITStatus I2C_GetITStatus(I2C_TypeDef* I2Cx, uint32_t I2C_IT)
  1210. {
  1211. ITStatus bitstatus = RESET;
  1212. uint32_t enablestatus = 0;
  1213. /* Check the parameters */
  1214. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  1215. assert_param(IS_I2C_GET_IT(I2C_IT));
  1216. /* Check if the interrupt source is enabled or not */
  1217. enablestatus = (uint32_t)(((I2C_IT & ITEN_MASK) >> 16) & (I2Cx->CR2)) ;
  1218. /* Get bit[23:0] of the flag */
  1219. I2C_IT &= FLAG_MASK;
  1220. /* Check the status of the specified I2C flag */
  1221. if (((I2Cx->SR1 & I2C_IT) != (uint32_t)RESET) && enablestatus)
  1222. {
  1223. /* I2C_IT is set */
  1224. bitstatus = SET;
  1225. }
  1226. else
  1227. {
  1228. /* I2C_IT is reset */
  1229. bitstatus = RESET;
  1230. }
  1231. /* Return the I2C_IT status */
  1232. return bitstatus;
  1233. }
  1234. /**
  1235. * @brief Clears the I2Cx's interrupt pending bits.
  1236. * @param I2Cx: where x can be 1, 2 or 3 to select the I2C peripheral.
  1237. * @param I2C_IT: specifies the interrupt pending bit to clear.
  1238. * This parameter can be any combination of the following values:
  1239. * @arg I2C_IT_SMBALERT: SMBus Alert interrupt
  1240. * @arg I2C_IT_TIMEOUT: Timeout or Tlow error interrupt
  1241. * @arg I2C_IT_PECERR: PEC error in reception interrupt
  1242. * @arg I2C_IT_OVR: Overrun/Underrun interrupt (Slave mode)
  1243. * @arg I2C_IT_AF: Acknowledge failure interrupt
  1244. * @arg I2C_IT_ARLO: Arbitration lost interrupt (Master mode)
  1245. * @arg I2C_IT_BERR: Bus error interrupt
  1246. *
  1247. * @note STOPF (STOP detection) is cleared by software sequence: a read operation
  1248. * to I2C_SR1 register (I2C_GetITStatus()) followed by a write operation to
  1249. * I2C_CR1 register (I2C_Cmd() to re-enable the I2C peripheral).
  1250. * @note ADD10 (10-bit header sent) is cleared by software sequence: a read
  1251. * operation to I2C_SR1 (I2C_GetITStatus()) followed by writing the second
  1252. * byte of the address in I2C_DR register.
  1253. * @note BTF (Byte Transfer Finished) is cleared by software sequence: a read
  1254. * operation to I2C_SR1 register (I2C_GetITStatus()) followed by a
  1255. * read/write to I2C_DR register (I2C_SendData()).
  1256. * @note ADDR (Address sent) is cleared by software sequence: a read operation to
  1257. * I2C_SR1 register (I2C_GetITStatus()) followed by a read operation to
  1258. * I2C_SR2 register ((void)(I2Cx->SR2)).
  1259. * @note SB (Start Bit) is cleared by software sequence: a read operation to
  1260. * I2C_SR1 register (I2C_GetITStatus()) followed by a write operation to
  1261. * I2C_DR register (I2C_SendData()).
  1262. * @retval None
  1263. */
  1264. void I2C_ClearITPendingBit(I2C_TypeDef* I2Cx, uint32_t I2C_IT)
  1265. {
  1266. uint32_t flagpos = 0;
  1267. /* Check the parameters */
  1268. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  1269. assert_param(IS_I2C_CLEAR_IT(I2C_IT));
  1270. /* Get the I2C flag position */
  1271. flagpos = I2C_IT & FLAG_MASK;
  1272. /* Clear the selected I2C flag */
  1273. I2Cx->SR1 = (uint16_t)~flagpos;
  1274. }
  1275. /**
  1276. * @}
  1277. */
  1278. /**
  1279. * @}
  1280. */
  1281. /**
  1282. * @}
  1283. */
  1284. /**
  1285. * @}
  1286. */