stm32f4xx_i2c.c 51 KB

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