stm32f10x_i2c.c 45 KB

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