stm32f10x_i2c.c 43 KB

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