ft32f0xx_i2c.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256
  1. /**
  2. ******************************************************************************
  3. * @file ft32f0xx_i2c.c
  4. * @author FMD AE
  5. * @brief This file provides firmware functions to manage the following
  6. * functionalities of the Inter-Integrated circuit (I2C):
  7. * + Initialization and Configuration
  8. * + Communications handling
  9. * + SMBUS management
  10. * + I2C registers management
  11. * + Data transfers management
  12. * + DMA transfers management
  13. * + Interrupts and flags management
  14. * @version V1.0.0
  15. * @data 2021-07-01
  16. ******************************************************************************
  17. */
  18. /* Includes ------------------------------------------------------------------*/
  19. #include "ft32f0xx_i2c.h"
  20. #include "ft32f0xx_rcc.h"
  21. #define CR1_CLEAR_MASK ((uint32_t)0x00CFE0FF) /*<! I2C CR1 clear register Mask */
  22. #define CR2_CLEAR_MASK ((uint32_t)0x07FF7FFF) /*<! I2C CR2 clear register Mask */
  23. #define TIMING_CLEAR_MASK ((uint32_t)0xF0FFFFFF) /*<! I2C TIMING clear register Mask */
  24. #define ERROR_IT_MASK ((uint32_t)0x00003F00) /*<! I2C Error interrupt register Mask */
  25. #define TC_IT_MASK ((uint32_t)0x000000C0) /*<! I2C TC interrupt register Mask */
  26. /**
  27. * @brief Deinitializes the I2Cx peripheral registers to their default reset values.
  28. * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
  29. * @retval None
  30. */
  31. void I2C_DeInit(I2C_TypeDef* I2Cx)
  32. {
  33. /* Check the parameters */
  34. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  35. if (I2Cx == I2C1)
  36. {
  37. /* Enable I2C1 reset state */
  38. RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, ENABLE);
  39. /* Release I2C1 from reset state */
  40. RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, DISABLE);
  41. }
  42. else
  43. {
  44. /* Enable I2C2 reset state */
  45. RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, ENABLE);
  46. /* Release I2C2 from reset state */
  47. RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, DISABLE);
  48. }
  49. }
  50. /**
  51. * @brief Initializes the I2Cx peripheral according to the specified
  52. * parameters in the I2C_InitStruct.
  53. * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
  54. * @param I2C_InitStruct: pointer to a I2C_InitTypeDef structure that
  55. * contains the configuration information for the specified I2C peripheral.
  56. * @retval None
  57. */
  58. void I2C_Init(I2C_TypeDef* I2Cx, I2C_InitTypeDef* I2C_InitStruct)
  59. {
  60. uint32_t tmpreg = 0;
  61. /* Check the parameters */
  62. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  63. assert_param(IS_I2C_ANALOG_FILTER(I2C_InitStruct->I2C_AnalogFilter));
  64. assert_param(IS_I2C_DIGITAL_FILTER(I2C_InitStruct->I2C_DigitalFilter));
  65. assert_param(IS_I2C_MODE(I2C_InitStruct->I2C_Mode));
  66. assert_param(IS_I2C_OWN_ADDRESS1(I2C_InitStruct->I2C_OwnAddress1));
  67. assert_param(IS_I2C_ACK(I2C_InitStruct->I2C_Ack));
  68. assert_param(IS_I2C_ACKNOWLEDGE_ADDRESS(I2C_InitStruct->I2C_AcknowledgedAddress));
  69. /* Disable I2Cx Peripheral */
  70. I2Cx->CR1 &= (uint32_t)~((uint32_t)I2C_CR1_PE);
  71. /*---------------------------- I2Cx FILTERS Configuration ------------------*/
  72. /* Get the I2Cx CR1 value */
  73. tmpreg = I2Cx->CR1;
  74. /* Clear I2Cx CR1 register */
  75. tmpreg &= CR1_CLEAR_MASK;
  76. /* Configure I2Cx: analog and digital filter */
  77. /* Set ANFOFF bit according to I2C_AnalogFilter value */
  78. /* Set DFN bits according to I2C_DigitalFilter value */
  79. tmpreg |= (uint32_t)I2C_InitStruct->I2C_AnalogFilter |(I2C_InitStruct->I2C_DigitalFilter << 8);
  80. /* Write to I2Cx CR1 */
  81. I2Cx->CR1 = tmpreg;
  82. /*---------------------------- I2Cx TIMING Configuration -------------------*/
  83. /* Configure I2Cx: Timing */
  84. /* Set TIMINGR bits according to I2C_Timing */
  85. /* Write to I2Cx TIMING */
  86. I2Cx->TIMINGR = I2C_InitStruct->I2C_Timing & TIMING_CLEAR_MASK;
  87. /* Enable I2Cx Peripheral */
  88. I2Cx->CR1 |= I2C_CR1_PE;
  89. /*---------------------------- I2Cx OAR1 Configuration ---------------------*/
  90. /* Clear tmpreg local variable */
  91. tmpreg = 0;
  92. /* Clear OAR1 register */
  93. I2Cx->OAR1 = (uint32_t)tmpreg;
  94. /* Clear OAR2 register */
  95. I2Cx->OAR2 = (uint32_t)tmpreg;
  96. /* Configure I2Cx: Own Address1 and acknowledged address */
  97. /* Set OA1MODE bit according to I2C_AcknowledgedAddress value */
  98. /* Set OA1 bits according to I2C_OwnAddress1 value */
  99. tmpreg = (uint32_t)((uint32_t)I2C_InitStruct->I2C_AcknowledgedAddress | \
  100. (uint32_t)I2C_InitStruct->I2C_OwnAddress1);
  101. /* Write to I2Cx OAR1 */
  102. I2Cx->OAR1 = tmpreg;
  103. /* Enable Own Address1 acknowledgement */
  104. I2Cx->OAR1 |= I2C_OAR1_OA1EN;
  105. /*---------------------------- I2Cx MODE Configuration ---------------------*/
  106. /* Configure I2Cx: mode */
  107. /* Set SMBDEN and SMBHEN bits according to I2C_Mode value */
  108. tmpreg = I2C_InitStruct->I2C_Mode;
  109. /* Write to I2Cx CR1 */
  110. I2Cx->CR1 |= tmpreg;
  111. /*---------------------------- I2Cx ACK Configuration ----------------------*/
  112. /* Get the I2Cx CR2 value */
  113. tmpreg = I2Cx->CR2;
  114. /* Clear I2Cx CR2 register */
  115. tmpreg &= CR2_CLEAR_MASK;
  116. /* Configure I2Cx: acknowledgement */
  117. /* Set NACK bit according to I2C_Ack value */
  118. tmpreg |= I2C_InitStruct->I2C_Ack;
  119. /* Write to I2Cx CR2 */
  120. I2Cx->CR2 = tmpreg;
  121. }
  122. /**
  123. * @brief Fills each I2C_InitStruct member with its default value.
  124. * @param I2C_InitStruct: pointer to an I2C_InitTypeDef structure which will be initialized.
  125. * @retval None
  126. */
  127. void I2C_StructInit(I2C_InitTypeDef* I2C_InitStruct)
  128. {
  129. /*---------------- Reset I2C init structure parameters values --------------*/
  130. /* Initialize the I2C_Timing member */
  131. I2C_InitStruct->I2C_Timing = 0;
  132. /* Initialize the I2C_AnalogFilter member */
  133. I2C_InitStruct->I2C_AnalogFilter = I2C_AnalogFilter_Enable;
  134. /* Initialize the I2C_DigitalFilter member */
  135. I2C_InitStruct->I2C_DigitalFilter = 0;
  136. /* Initialize the I2C_Mode member */
  137. I2C_InitStruct->I2C_Mode = I2C_Mode_I2C;
  138. /* Initialize the I2C_OwnAddress1 member */
  139. I2C_InitStruct->I2C_OwnAddress1 = 0;
  140. /* Initialize the I2C_Ack member */
  141. I2C_InitStruct->I2C_Ack = I2C_Ack_Disable;
  142. /* Initialize the I2C_AcknowledgedAddress member */
  143. I2C_InitStruct->I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
  144. }
  145. /**
  146. * @brief Enables or disables the specified I2C peripheral.
  147. * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
  148. * @param NewState: new state of the I2Cx peripheral.
  149. * This parameter can be: ENABLE or DISABLE.
  150. * @retval None
  151. */
  152. void I2C_Cmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
  153. {
  154. /* Check the parameters */
  155. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  156. assert_param(IS_FUNCTIONAL_STATE(NewState));
  157. if (NewState != DISABLE)
  158. {
  159. /* Enable the selected I2C peripheral */
  160. I2Cx->CR1 |= I2C_CR1_PE;
  161. }
  162. else
  163. {
  164. /* Disable the selected I2C peripheral */
  165. I2Cx->CR1 &= (uint32_t)~((uint32_t)I2C_CR1_PE);
  166. }
  167. }
  168. /**
  169. * @brief Enables or disables the specified I2C software reset.
  170. * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
  171. * @retval None
  172. */
  173. void I2C_SoftwareResetCmd(I2C_TypeDef* I2Cx)
  174. {
  175. /* Check the parameters */
  176. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  177. /* Disable peripheral */
  178. I2Cx->CR1 &= (uint32_t)~((uint32_t)I2C_CR1_PE);
  179. /* Perform a dummy read to delay the disable of peripheral for minimum
  180. 3 APB clock cycles to perform the software reset functionality */
  181. *(__IO uint32_t *)(uint32_t)I2Cx;
  182. /* Enable peripheral */
  183. I2Cx->CR1 |= I2C_CR1_PE;
  184. }
  185. /**
  186. * @brief Enables or disables the specified I2C interrupts.
  187. * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
  188. * @param I2C_IT: specifies the I2C interrupts sources to be enabled or disabled.
  189. * This parameter can be any combination of the following values:
  190. * @arg I2C_IT_ERRI: Error interrupt mask
  191. * @arg I2C_IT_TCI: Transfer Complete interrupt mask
  192. * @arg I2C_IT_STOPI: Stop Detection interrupt mask
  193. * @arg I2C_IT_NACKI: Not Acknowledge received interrupt mask
  194. * @arg I2C_IT_ADDRI: Address Match interrupt mask
  195. * @arg I2C_IT_RXI: RX interrupt mask
  196. * @arg I2C_IT_TXI: TX interrupt mask
  197. * @param NewState: new state of the specified I2C interrupts.
  198. * This parameter can be: ENABLE or DISABLE.
  199. * @retval None
  200. */
  201. void I2C_ITConfig(I2C_TypeDef* I2Cx, uint32_t I2C_IT, FunctionalState NewState)
  202. {
  203. /* Check the parameters */
  204. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  205. assert_param(IS_FUNCTIONAL_STATE(NewState));
  206. assert_param(IS_I2C_CONFIG_IT(I2C_IT));
  207. if (NewState != DISABLE)
  208. {
  209. /* Enable the selected I2C interrupts */
  210. I2Cx->CR1 |= I2C_IT;
  211. }
  212. else
  213. {
  214. /* Disable the selected I2C interrupts */
  215. I2Cx->CR1 &= (uint32_t)~((uint32_t)I2C_IT);
  216. }
  217. }
  218. /**
  219. * @brief Enables or disables the I2C Clock stretching.
  220. * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
  221. * @param NewState: new state of the I2Cx Clock stretching.
  222. * This parameter can be: ENABLE or DISABLE.
  223. * @retval None
  224. */
  225. void I2C_StretchClockCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
  226. {
  227. /* Check the parameters */
  228. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  229. assert_param(IS_FUNCTIONAL_STATE(NewState));
  230. if (NewState != DISABLE)
  231. {
  232. /* Enable clock stretching */
  233. I2Cx->CR1 &= (uint32_t)~((uint32_t)I2C_CR1_NOSTRETCH);
  234. }
  235. else
  236. {
  237. /* Disable clock stretching */
  238. I2Cx->CR1 |= I2C_CR1_NOSTRETCH;
  239. }
  240. }
  241. /**
  242. * @brief Enables or disables the I2C own address 2.
  243. * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
  244. * @param NewState: new state of the I2C own address 2.
  245. * This parameter can be: ENABLE or DISABLE.
  246. * @retval None
  247. */
  248. void I2C_DualAddressCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
  249. {
  250. /* Check the parameters */
  251. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  252. assert_param(IS_FUNCTIONAL_STATE(NewState));
  253. if (NewState != DISABLE)
  254. {
  255. /* Enable own address 2 */
  256. I2Cx->OAR2 |= I2C_OAR2_OA2EN;
  257. }
  258. else
  259. {
  260. /* Disable own address 2 */
  261. I2Cx->OAR2 &= (uint32_t)~((uint32_t)I2C_OAR2_OA2EN);
  262. }
  263. }
  264. /**
  265. * @brief Configures the I2C slave own address 2 and mask.
  266. * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
  267. * @param Address: specifies the slave address to be programmed.
  268. * @param Mask: specifies own address 2 mask to be programmed.
  269. * This parameter can be one of the following values:
  270. * @arg I2C_OA2_NoMask: no mask.
  271. * @arg I2C_OA2_Mask01: OA2[1] is masked and don't care.
  272. * @arg I2C_OA2_Mask02: OA2[2:1] are masked and don't care.
  273. * @arg I2C_OA2_Mask03: OA2[3:1] are masked and don't care.
  274. * @arg I2C_OA2_Mask04: OA2[4:1] are masked and don't care.
  275. * @arg I2C_OA2_Mask05: OA2[5:1] are masked and don't care.
  276. * @arg I2C_OA2_Mask06: OA2[6:1] are masked and don't care.
  277. * @arg I2C_OA2_Mask07: OA2[7:1] are masked and don't care.
  278. * @retval None
  279. */
  280. void I2C_OwnAddress2Config(I2C_TypeDef* I2Cx, uint16_t Address, uint8_t Mask)
  281. {
  282. uint32_t tmpreg = 0;
  283. /* Check the parameters */
  284. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  285. assert_param(IS_I2C_OWN_ADDRESS2(Address));
  286. assert_param(IS_I2C_OWN_ADDRESS2_MASK(Mask));
  287. /* Get the old register value */
  288. tmpreg = I2Cx->OAR2;
  289. /* Reset I2Cx OA2 bit [7:1] and OA2MSK bit [1:0] */
  290. tmpreg &= (uint32_t)~((uint32_t)(I2C_OAR2_OA2 | I2C_OAR2_OA2MSK));
  291. /* Set I2Cx SADD */
  292. tmpreg |= (uint32_t)(((uint32_t)Address & I2C_OAR2_OA2) | \
  293. (((uint32_t)Mask << 8) & I2C_OAR2_OA2MSK)) ;
  294. /* Store the new register value */
  295. I2Cx->OAR2 = tmpreg;
  296. }
  297. /**
  298. * @brief Enables or disables the I2C general call mode.
  299. * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
  300. * @param NewState: new state of the I2C general call mode.
  301. * This parameter can be: ENABLE or DISABLE.
  302. * @retval None
  303. */
  304. void I2C_GeneralCallCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
  305. {
  306. /* Check the parameters */
  307. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  308. assert_param(IS_FUNCTIONAL_STATE(NewState));
  309. if (NewState != DISABLE)
  310. {
  311. /* Enable general call mode */
  312. I2Cx->CR1 |= I2C_CR1_GCEN;
  313. }
  314. else
  315. {
  316. /* Disable general call mode */
  317. I2Cx->CR1 &= (uint32_t)~((uint32_t)I2C_CR1_GCEN);
  318. }
  319. }
  320. /**
  321. * @brief Enables or disables the I2C slave byte control.
  322. * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
  323. * @param NewState: new state of the I2C slave byte control.
  324. * This parameter can be: ENABLE or DISABLE.
  325. * @retval None
  326. */
  327. void I2C_SlaveByteControlCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
  328. {
  329. /* Check the parameters */
  330. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  331. assert_param(IS_FUNCTIONAL_STATE(NewState));
  332. if (NewState != DISABLE)
  333. {
  334. /* Enable slave byte control */
  335. I2Cx->CR1 |= I2C_CR1_SBC;
  336. }
  337. else
  338. {
  339. /* Disable slave byte control */
  340. I2Cx->CR1 &= (uint32_t)~((uint32_t)I2C_CR1_SBC);
  341. }
  342. }
  343. /**
  344. * @brief Configures the slave address to be transmitted after start generation.
  345. * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
  346. * @param Address: specifies the slave address to be programmed.
  347. * @note This function should be called before generating start condition.
  348. * @retval None
  349. */
  350. void I2C_SlaveAddressConfig(I2C_TypeDef* I2Cx, uint16_t Address)
  351. {
  352. uint32_t tmpreg = 0;
  353. /* Check the parameters */
  354. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  355. assert_param(IS_I2C_SLAVE_ADDRESS(Address));
  356. /* Get the old register value */
  357. tmpreg = I2Cx->CR2;
  358. /* Reset I2Cx SADD bit [9:0] */
  359. tmpreg &= (uint32_t)~((uint32_t)I2C_CR2_SADD);
  360. /* Set I2Cx SADD */
  361. tmpreg |= (uint32_t)((uint32_t)Address & I2C_CR2_SADD);
  362. /* Store the new register value */
  363. I2Cx->CR2 = tmpreg;
  364. }
  365. /**
  366. * @brief Enables or disables the I2C 10-bit addressing mode for the master.
  367. * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
  368. * @param NewState: new state of the I2C 10-bit addressing mode.
  369. * This parameter can be: ENABLE or DISABLE.
  370. * @note This function should be called before generating start condition.
  371. * @retval None
  372. */
  373. void I2C_10BitAddressingModeCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
  374. {
  375. /* Check the parameters */
  376. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  377. assert_param(IS_FUNCTIONAL_STATE(NewState));
  378. if (NewState != DISABLE)
  379. {
  380. /* Enable 10-bit addressing mode */
  381. I2Cx->CR2 |= I2C_CR2_ADD10;
  382. }
  383. else
  384. {
  385. /* Disable 10-bit addressing mode */
  386. I2Cx->CR2 &= (uint32_t)~((uint32_t)I2C_CR2_ADD10);
  387. }
  388. }
  389. /**
  390. * @}
  391. */
  392. /**
  393. * @brief Enables or disables the I2C automatic end mode (stop condition is
  394. * automatically sent when nbytes data are transferred).
  395. * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
  396. * @param NewState: new state of the I2C automatic end mode.
  397. * This parameter can be: ENABLE or DISABLE.
  398. * @note This function has effect if Reload mode is disabled.
  399. * @retval None
  400. */
  401. void I2C_AutoEndCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
  402. {
  403. /* Check the parameters */
  404. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  405. assert_param(IS_FUNCTIONAL_STATE(NewState));
  406. if (NewState != DISABLE)
  407. {
  408. /* Enable Auto end mode */
  409. I2Cx->CR2 |= I2C_CR2_AUTOEND;
  410. }
  411. else
  412. {
  413. /* Disable Auto end mode */
  414. I2Cx->CR2 &= (uint32_t)~((uint32_t)I2C_CR2_AUTOEND);
  415. }
  416. }
  417. /**
  418. * @brief Enables or disables the I2C nbytes reload mode.
  419. * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
  420. * @param NewState: new state of the nbytes reload mode.
  421. * This parameter can be: ENABLE or DISABLE.
  422. * @retval None
  423. */
  424. void I2C_ReloadCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
  425. {
  426. /* Check the parameters */
  427. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  428. assert_param(IS_FUNCTIONAL_STATE(NewState));
  429. if (NewState != DISABLE)
  430. {
  431. /* Enable Auto Reload mode */
  432. I2Cx->CR2 |= I2C_CR2_RELOAD;
  433. }
  434. else
  435. {
  436. /* Disable Auto Reload mode */
  437. I2Cx->CR2 &= (uint32_t)~((uint32_t)I2C_CR2_RELOAD);
  438. }
  439. }
  440. /**
  441. * @brief Configures the number of bytes to be transmitted/received.
  442. * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
  443. * @param Number_Bytes: specifies the number of bytes to be programmed.
  444. * @retval None
  445. */
  446. void I2C_NumberOfBytesConfig(I2C_TypeDef* I2Cx, uint8_t Number_Bytes)
  447. {
  448. uint32_t tmpreg = 0;
  449. /* Check the parameters */
  450. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  451. /* Get the old register value */
  452. tmpreg = I2Cx->CR2;
  453. /* Reset I2Cx Nbytes bit [7:0] */
  454. tmpreg &= (uint32_t)~((uint32_t)I2C_CR2_NBYTES);
  455. /* Set I2Cx Nbytes */
  456. tmpreg |= (uint32_t)(((uint32_t)Number_Bytes << 16 ) & I2C_CR2_NBYTES);
  457. /* Store the new register value */
  458. I2Cx->CR2 = tmpreg;
  459. }
  460. /**
  461. * @brief Configures the type of transfer request for the master.
  462. * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
  463. * @param I2C_Direction: specifies the transfer request direction to be programmed.
  464. * This parameter can be one of the following values:
  465. * @arg I2C_Direction_Transmitter: Master request a write transfer
  466. * @arg I2C_Direction_Receiver: Master request a read transfer
  467. * @retval None
  468. */
  469. void I2C_MasterRequestConfig(I2C_TypeDef* I2Cx, uint16_t I2C_Direction)
  470. {
  471. /* Check the parameters */
  472. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  473. assert_param(IS_I2C_DIRECTION(I2C_Direction));
  474. /* Test on the direction to set/reset the read/write bit */
  475. if (I2C_Direction == I2C_Direction_Transmitter)
  476. {
  477. /* Request a write Transfer */
  478. I2Cx->CR2 &= (uint32_t)~((uint32_t)I2C_CR2_RD_WRN);
  479. }
  480. else
  481. {
  482. /* Request a read Transfer */
  483. I2Cx->CR2 |= I2C_CR2_RD_WRN;
  484. }
  485. }
  486. /**
  487. * @brief Generates I2Cx communication START condition.
  488. * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
  489. * @param NewState: new state of the I2C START condition generation.
  490. * This parameter can be: ENABLE or DISABLE.
  491. * @retval None
  492. */
  493. void I2C_GenerateSTART(I2C_TypeDef* I2Cx, FunctionalState NewState)
  494. {
  495. /* Check the parameters */
  496. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  497. assert_param(IS_FUNCTIONAL_STATE(NewState));
  498. if (NewState != DISABLE)
  499. {
  500. /* Generate a START condition */
  501. I2Cx->CR2 |= I2C_CR2_START;
  502. }
  503. else
  504. {
  505. /* Disable the START condition generation */
  506. I2Cx->CR2 &= (uint32_t)~((uint32_t)I2C_CR2_START);
  507. }
  508. }
  509. /**
  510. * @brief Generates I2Cx communication STOP condition.
  511. * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
  512. * @param NewState: new state of the I2C STOP condition generation.
  513. * This parameter can be: ENABLE or DISABLE.
  514. * @retval None
  515. */
  516. void I2C_GenerateSTOP(I2C_TypeDef* I2Cx, FunctionalState NewState)
  517. {
  518. /* Check the parameters */
  519. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  520. assert_param(IS_FUNCTIONAL_STATE(NewState));
  521. if (NewState != DISABLE)
  522. {
  523. /* Generate a STOP condition */
  524. I2Cx->CR2 |= I2C_CR2_STOP;
  525. }
  526. else
  527. {
  528. /* Disable the STOP condition generation */
  529. I2Cx->CR2 &= (uint32_t)~((uint32_t)I2C_CR2_STOP);
  530. }
  531. }
  532. /**
  533. * @brief Enables or disables the I2C 10-bit header only mode with read direction.
  534. * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
  535. * @param NewState: new state of the I2C 10-bit header only mode.
  536. * This parameter can be: ENABLE or DISABLE.
  537. * @note This mode can be used only when switching from master transmitter mode
  538. * to master receiver mode.
  539. * @retval None
  540. */
  541. void I2C_10BitAddressHeaderCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
  542. {
  543. /* Check the parameters */
  544. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  545. assert_param(IS_FUNCTIONAL_STATE(NewState));
  546. if (NewState != DISABLE)
  547. {
  548. /* Enable 10-bit header only mode */
  549. I2Cx->CR2 |= I2C_CR2_HEAD10R;
  550. }
  551. else
  552. {
  553. /* Disable 10-bit header only mode */
  554. I2Cx->CR2 &= (uint32_t)~((uint32_t)I2C_CR2_HEAD10R);
  555. }
  556. }
  557. /**
  558. * @brief Generates I2C communication Acknowledge.
  559. * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
  560. * @param NewState: new state of the Acknowledge.
  561. * This parameter can be: ENABLE or DISABLE.
  562. * @retval None
  563. */
  564. void I2C_AcknowledgeConfig(I2C_TypeDef* I2Cx, FunctionalState NewState)
  565. {
  566. /* Check the parameters */
  567. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  568. assert_param(IS_FUNCTIONAL_STATE(NewState));
  569. if (NewState != DISABLE)
  570. {
  571. /* Enable ACK generation */
  572. I2Cx->CR2 &= (uint32_t)~((uint32_t)I2C_CR2_NACK);
  573. }
  574. else
  575. {
  576. /* Enable NACK generation */
  577. I2Cx->CR2 |= I2C_CR2_NACK;
  578. }
  579. }
  580. /**
  581. * @brief Returns the I2C slave matched address .
  582. * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
  583. * @retval The value of the slave matched address .
  584. */
  585. uint8_t I2C_GetAddressMatched(I2C_TypeDef* I2Cx)
  586. {
  587. /* Check the parameters */
  588. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  589. /* Return the slave matched address in the SR1 register */
  590. return (uint8_t)(((uint32_t)I2Cx->ISR & I2C_ISR_ADDCODE) >> 16) ;
  591. }
  592. /**
  593. * @brief Returns the I2C slave received request.
  594. * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
  595. * @retval The value of the received request.
  596. */
  597. uint16_t I2C_GetTransferDirection(I2C_TypeDef* I2Cx)
  598. {
  599. uint32_t tmpreg = 0;
  600. uint16_t direction = 0;
  601. /* Check the parameters */
  602. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  603. /* Return the slave matched address in the SR1 register */
  604. tmpreg = (uint32_t)(I2Cx->ISR & I2C_ISR_DIR);
  605. /* If write transfer is requested */
  606. if (tmpreg == 0)
  607. {
  608. /* write transfer is requested */
  609. direction = I2C_Direction_Transmitter;
  610. }
  611. else
  612. {
  613. /* Read transfer is requested */
  614. direction = I2C_Direction_Receiver;
  615. }
  616. return direction;
  617. }
  618. /**
  619. * @brief Handles I2Cx communication when starting transfer or during transfer (TC or TCR flag are set).
  620. * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
  621. * @param Address: specifies the slave address to be programmed.
  622. * @param Number_Bytes: specifies the number of bytes to be programmed.
  623. * This parameter must be a value between 0 and 255.
  624. * @param ReloadEndMode: new state of the I2C START condition generation.
  625. * This parameter can be one of the following values:
  626. * @arg I2C_Reload_Mode: Enable Reload mode .
  627. * @arg I2C_AutoEnd_Mode: Enable Automatic end mode.
  628. * @arg I2C_SoftEnd_Mode: Enable Software end mode.
  629. * @param StartStopMode: new state of the I2C START condition generation.
  630. * This parameter can be one of the following values:
  631. * @arg I2C_No_StartStop: Don't Generate stop and start condition.
  632. * @arg I2C_Generate_Stop: Generate stop condition (Number_Bytes should be set to 0).
  633. * @arg I2C_Generate_Start_Read: Generate Restart for read request.
  634. * @arg I2C_Generate_Start_Write: Generate Restart for write request.
  635. * @retval None
  636. */
  637. void I2C_TransferHandling(I2C_TypeDef* I2Cx, uint16_t Address, uint8_t Number_Bytes, uint32_t ReloadEndMode, uint32_t StartStopMode)
  638. {
  639. uint32_t tmpreg = 0;
  640. /* Check the parameters */
  641. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  642. assert_param(IS_I2C_SLAVE_ADDRESS(Address));
  643. assert_param(IS_RELOAD_END_MODE(ReloadEndMode));
  644. assert_param(IS_START_STOP_MODE(StartStopMode));
  645. /* Get the CR2 register value */
  646. tmpreg = I2Cx->CR2;
  647. /* clear tmpreg specific bits */
  648. tmpreg &= (uint32_t)~((uint32_t)(I2C_CR2_SADD | I2C_CR2_NBYTES | I2C_CR2_RELOAD | I2C_CR2_AUTOEND | I2C_CR2_RD_WRN | I2C_CR2_START | I2C_CR2_STOP));
  649. /* update tmpreg */
  650. tmpreg |= (uint32_t)(((uint32_t)Address & I2C_CR2_SADD) | (((uint32_t)Number_Bytes << 16 ) & I2C_CR2_NBYTES) | \
  651. (uint32_t)ReloadEndMode | (uint32_t)StartStopMode);
  652. /* update CR2 register */
  653. I2Cx->CR2 = tmpreg;
  654. }
  655. /**
  656. * @}
  657. */
  658. /**
  659. * @brief Enables or disables I2C SMBus alert.
  660. * @param I2Cx: where x can be 1 to select the I2C peripheral.
  661. * @param NewState: new state of the I2Cx SMBus alert.
  662. * This parameter can be: ENABLE or DISABLE.
  663. * @retval None
  664. */
  665. void I2C_SMBusAlertCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
  666. {
  667. /* Check the parameters */
  668. assert_param(IS_I2C_1_PERIPH(I2Cx));
  669. assert_param(IS_FUNCTIONAL_STATE(NewState));
  670. if (NewState != DISABLE)
  671. {
  672. /* Enable SMBus alert */
  673. I2Cx->CR1 |= I2C_CR1_ALERTEN;
  674. }
  675. else
  676. {
  677. /* Disable SMBus alert */
  678. I2Cx->CR1 &= (uint32_t)~((uint32_t)I2C_CR1_ALERTEN);
  679. }
  680. }
  681. /**
  682. * @brief Enables or disables I2C Clock Timeout (SCL Timeout detection).
  683. * @param I2Cx: where x can be 1 to select the I2C peripheral.
  684. * @param NewState: new state of the I2Cx clock Timeout.
  685. * This parameter can be: ENABLE or DISABLE.
  686. * @retval None
  687. */
  688. void I2C_ClockTimeoutCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
  689. {
  690. /* Check the parameters */
  691. assert_param(IS_I2C_1_PERIPH(I2Cx));
  692. assert_param(IS_FUNCTIONAL_STATE(NewState));
  693. if (NewState != DISABLE)
  694. {
  695. /* Enable Clock Timeout */
  696. I2Cx->TIMEOUTR |= I2C_TIMEOUTR_TIMOUTEN;
  697. }
  698. else
  699. {
  700. /* Disable Clock Timeout */
  701. I2Cx->TIMEOUTR &= (uint32_t)~((uint32_t)I2C_TIMEOUTR_TIMOUTEN);
  702. }
  703. }
  704. /**
  705. * @brief Enables or disables I2C Extended Clock Timeout (SCL cumulative Timeout detection).
  706. * @param I2Cx: where x can be 1 to select the I2C peripheral.
  707. * @param NewState: new state of the I2Cx Extended clock Timeout.
  708. * This parameter can be: ENABLE or DISABLE.
  709. * @retval None
  710. */
  711. void I2C_ExtendedClockTimeoutCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
  712. {
  713. /* Check the parameters */
  714. assert_param(IS_I2C_1_PERIPH(I2Cx));
  715. assert_param(IS_FUNCTIONAL_STATE(NewState));
  716. if (NewState != DISABLE)
  717. {
  718. /* Enable Clock Timeout */
  719. I2Cx->TIMEOUTR |= I2C_TIMEOUTR_TEXTEN;
  720. }
  721. else
  722. {
  723. /* Disable Clock Timeout */
  724. I2Cx->TIMEOUTR &= (uint32_t)~((uint32_t)I2C_TIMEOUTR_TEXTEN);
  725. }
  726. }
  727. /**
  728. * @brief Enables or disables I2C Idle Clock Timeout (Bus idle SCL and SDA
  729. * high detection).
  730. * @param I2Cx: where x can be 1 to select the I2C peripheral.
  731. * @param NewState: new state of the I2Cx Idle clock Timeout.
  732. * This parameter can be: ENABLE or DISABLE.
  733. * @retval None
  734. */
  735. void I2C_IdleClockTimeoutCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
  736. {
  737. /* Check the parameters */
  738. assert_param(IS_I2C_1_PERIPH(I2Cx));
  739. assert_param(IS_FUNCTIONAL_STATE(NewState));
  740. if (NewState != DISABLE)
  741. {
  742. /* Enable Clock Timeout */
  743. I2Cx->TIMEOUTR |= I2C_TIMEOUTR_TIDLE;
  744. }
  745. else
  746. {
  747. /* Disable Clock Timeout */
  748. I2Cx->TIMEOUTR &= (uint32_t)~((uint32_t)I2C_TIMEOUTR_TIDLE);
  749. }
  750. }
  751. /**
  752. * @brief Configures the I2C Bus Timeout A (SCL Timeout when TIDLE = 0 or Bus
  753. * idle SCL and SDA high when TIDLE = 1).
  754. * @param I2Cx: where x can be 1 to select the I2C peripheral.
  755. * @param Timeout: specifies the TimeoutA to be programmed.
  756. * @retval None
  757. */
  758. void I2C_TimeoutAConfig(I2C_TypeDef* I2Cx, uint16_t Timeout)
  759. {
  760. uint32_t tmpreg = 0;
  761. /* Check the parameters */
  762. assert_param(IS_I2C_1_PERIPH(I2Cx));
  763. assert_param(IS_I2C_TIMEOUT(Timeout));
  764. /* Get the old register value */
  765. tmpreg = I2Cx->TIMEOUTR;
  766. /* Reset I2Cx TIMEOUTA bit [11:0] */
  767. tmpreg &= (uint32_t)~((uint32_t)I2C_TIMEOUTR_TIMEOUTA);
  768. /* Set I2Cx TIMEOUTA */
  769. tmpreg |= (uint32_t)((uint32_t)Timeout & I2C_TIMEOUTR_TIMEOUTA) ;
  770. /* Store the new register value */
  771. I2Cx->TIMEOUTR = tmpreg;
  772. }
  773. /**
  774. * @brief Configures the I2C Bus Timeout B (SCL cumulative Timeout).
  775. * @param I2Cx: where x can be 1 to select the I2C peripheral.
  776. * @param Timeout: specifies the TimeoutB to be programmed.
  777. * @retval None
  778. */
  779. void I2C_TimeoutBConfig(I2C_TypeDef* I2Cx, uint16_t Timeout)
  780. {
  781. uint32_t tmpreg = 0;
  782. /* Check the parameters */
  783. assert_param(IS_I2C_1_PERIPH(I2Cx));
  784. assert_param(IS_I2C_TIMEOUT(Timeout));
  785. /* Get the old register value */
  786. tmpreg = I2Cx->TIMEOUTR;
  787. /* Reset I2Cx TIMEOUTB bit [11:0] */
  788. tmpreg &= (uint32_t)~((uint32_t)I2C_TIMEOUTR_TIMEOUTB);
  789. /* Set I2Cx TIMEOUTB */
  790. tmpreg |= (uint32_t)(((uint32_t)Timeout << 16) & I2C_TIMEOUTR_TIMEOUTB) ;
  791. /* Store the new register value */
  792. I2Cx->TIMEOUTR = tmpreg;
  793. }
  794. /**
  795. * @brief Enables or disables I2C PEC calculation.
  796. * @param I2Cx: where x can be 1 to select the I2C peripheral.
  797. * @param NewState: new state of the I2Cx PEC calculation.
  798. * This parameter can be: ENABLE or DISABLE.
  799. * @retval None
  800. */
  801. void I2C_CalculatePEC(I2C_TypeDef* I2Cx, FunctionalState NewState)
  802. {
  803. /* Check the parameters */
  804. assert_param(IS_I2C_1_PERIPH(I2Cx));
  805. assert_param(IS_FUNCTIONAL_STATE(NewState));
  806. if (NewState != DISABLE)
  807. {
  808. /* Enable PEC calculation */
  809. I2Cx->CR1 |= I2C_CR1_PECEN;
  810. }
  811. else
  812. {
  813. /* Disable PEC calculation */
  814. I2Cx->CR1 &= (uint32_t)~((uint32_t)I2C_CR1_PECEN);
  815. }
  816. }
  817. /**
  818. * @brief Enables or disables I2C PEC transmission/reception request.
  819. * @param I2Cx: where x can be 1 to select the I2C peripheral.
  820. * @param NewState: new state of the I2Cx PEC request.
  821. * This parameter can be: ENABLE or DISABLE.
  822. * @retval None
  823. */
  824. void I2C_PECRequestCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
  825. {
  826. /* Check the parameters */
  827. assert_param(IS_I2C_1_PERIPH(I2Cx));
  828. assert_param(IS_FUNCTIONAL_STATE(NewState));
  829. if (NewState != DISABLE)
  830. {
  831. /* Enable PEC transmission/reception request */
  832. I2Cx->CR2 |= I2C_CR2_PECBYTE;
  833. }
  834. else
  835. {
  836. /* Disable PEC transmission/reception request */
  837. I2Cx->CR2 &= (uint32_t)~((uint32_t)I2C_CR2_PECBYTE);
  838. }
  839. }
  840. /**
  841. * @brief Returns the I2C PEC.
  842. * @param I2Cx: where x can be 1 to select the I2C peripheral.
  843. * @retval The value of the PEC .
  844. */
  845. uint8_t I2C_GetPEC(I2C_TypeDef* I2Cx)
  846. {
  847. /* Check the parameters */
  848. assert_param(IS_I2C_1_PERIPH(I2Cx));
  849. /* Return the slave matched address in the SR1 register */
  850. return (uint8_t)((uint32_t)I2Cx->PECR & I2C_PECR_PEC);
  851. }
  852. /**
  853. * @}
  854. */
  855. /**
  856. * @brief Reads the specified I2C register and returns its value.
  857. * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
  858. * @param I2C_Register: specifies the register to read.
  859. * This parameter can be one of the following values:
  860. * @arg I2C_Register_CR1: CR1 register.
  861. * @arg I2C_Register_CR2: CR2 register.
  862. * @arg I2C_Register_OAR1: OAR1 register.
  863. * @arg I2C_Register_OAR2: OAR2 register.
  864. * @arg I2C_Register_TIMINGR: TIMING register.
  865. * @arg I2C_Register_TIMEOUTR: TIMEOUTR register.
  866. * @arg I2C_Register_ISR: ISR register.
  867. * @arg I2C_Register_ICR: ICR register.
  868. * @arg I2C_Register_PECR: PECR register.
  869. * @arg I2C_Register_RXDR: RXDR register.
  870. * @arg I2C_Register_TXDR: TXDR register.
  871. * @retval The value of the read register.
  872. */
  873. uint32_t I2C_ReadRegister(I2C_TypeDef* I2Cx, uint8_t I2C_Register)
  874. {
  875. __IO uint32_t tmp = 0;
  876. /* Check the parameters */
  877. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  878. assert_param(IS_I2C_REGISTER(I2C_Register));
  879. tmp = (uint32_t)I2Cx;
  880. tmp += I2C_Register;
  881. /* Return the selected register value */
  882. return (*(__IO uint32_t *) tmp);
  883. }
  884. /**
  885. * @}
  886. */
  887. /**
  888. * @brief Sends a data byte through the I2Cx peripheral.
  889. * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
  890. * @param Data: Byte to be transmitted..
  891. * @retval None
  892. */
  893. void I2C_SendData(I2C_TypeDef* I2Cx, uint8_t Data)
  894. {
  895. /* Check the parameters */
  896. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  897. /* Write in the DR register the data to be sent */
  898. I2Cx->TXDR = (uint8_t)Data;
  899. }
  900. /**
  901. * @brief Returns the most recent received data by the I2Cx peripheral.
  902. * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
  903. * @retval The value of the received data.
  904. */
  905. uint8_t I2C_ReceiveData(I2C_TypeDef* I2Cx)
  906. {
  907. /* Check the parameters */
  908. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  909. /* Return the data in the DR register */
  910. return (uint8_t)I2Cx->RXDR;
  911. }
  912. /**
  913. * @}
  914. */
  915. /**
  916. * @brief Enables or disables the I2C DMA interface.
  917. * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
  918. * @param I2C_DMAReq: specifies the I2C DMA transfer request to be enabled or disabled.
  919. * This parameter can be any combination of the following values:
  920. * @arg I2C_DMAReq_Tx: Tx DMA transfer request
  921. * @arg I2C_DMAReq_Rx: Rx DMA transfer request
  922. * @param NewState: new state of the selected I2C DMA transfer request.
  923. * This parameter can be: ENABLE or DISABLE.
  924. * @retval None
  925. */
  926. void I2C_DMACmd(I2C_TypeDef* I2Cx, uint32_t I2C_DMAReq, FunctionalState NewState)
  927. {
  928. /* Check the parameters */
  929. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  930. assert_param(IS_FUNCTIONAL_STATE(NewState));
  931. assert_param(IS_I2C_DMA_REQ(I2C_DMAReq));
  932. if (NewState != DISABLE)
  933. {
  934. /* Enable the selected I2C DMA requests */
  935. I2Cx->CR1 |= I2C_DMAReq;
  936. }
  937. else
  938. {
  939. /* Disable the selected I2C DMA requests */
  940. I2Cx->CR1 &= (uint32_t)~I2C_DMAReq;
  941. }
  942. }
  943. /**
  944. * @}
  945. */
  946. /**
  947. * @brief Checks whether the specified I2C flag is set or not.
  948. * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
  949. * @param I2C_FLAG: specifies the flag to check.
  950. * This parameter can be one of the following values:
  951. * @arg I2C_FLAG_TXE: Transmit data register empty
  952. * @arg I2C_FLAG_TXIS: Transmit interrupt status
  953. * @arg I2C_FLAG_RXNE: Receive data register not empty
  954. * @arg I2C_FLAG_ADDR: Address matched (slave mode)
  955. * @arg I2C_FLAG_NACKF: NACK received flag
  956. * @arg I2C_FLAG_STOPF: STOP detection flag
  957. * @arg I2C_FLAG_TC: Transfer complete (master mode)
  958. * @arg I2C_FLAG_TCR: Transfer complete reload
  959. * @arg I2C_FLAG_BERR: Bus error
  960. * @arg I2C_FLAG_ARLO: Arbitration lost
  961. * @arg I2C_FLAG_OVR: Overrun/Underrun
  962. * @arg I2C_FLAG_PECERR: PEC error in reception
  963. * @arg I2C_FLAG_TIMEOUT: Timeout or Tlow detection flag
  964. * @arg I2C_FLAG_ALERT: SMBus Alert
  965. * @arg I2C_FLAG_BUSY: Bus busy
  966. * @retval The new state of I2C_FLAG (SET or RESET).
  967. */
  968. FlagStatus I2C_GetFlagStatus(I2C_TypeDef* I2Cx, uint32_t I2C_FLAG)
  969. {
  970. uint32_t tmpreg = 0;
  971. FlagStatus bitstatus = RESET;
  972. /* Check the parameters */
  973. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  974. assert_param(IS_I2C_GET_FLAG(I2C_FLAG));
  975. /* Get the ISR register value */
  976. tmpreg = I2Cx->ISR;
  977. /* Get flag status */
  978. tmpreg &= I2C_FLAG;
  979. if(tmpreg != 0)
  980. {
  981. /* I2C_FLAG is set */
  982. bitstatus = SET;
  983. }
  984. else
  985. {
  986. /* I2C_FLAG is reset */
  987. bitstatus = RESET;
  988. }
  989. return bitstatus;
  990. }
  991. /**
  992. * @brief Clears the I2Cx's pending flags.
  993. * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
  994. * @param I2C_FLAG: specifies the flag to clear.
  995. * This parameter can be any combination of the following values:
  996. * @arg I2C_FLAG_ADDR: Address matched (slave mode)
  997. * @arg I2C_FLAG_NACKF: NACK received flag
  998. * @arg I2C_FLAG_STOPF: STOP detection flag
  999. * @arg I2C_FLAG_BERR: Bus error
  1000. * @arg I2C_FLAG_ARLO: Arbitration lost
  1001. * @arg I2C_FLAG_OVR: Overrun/Underrun
  1002. * @arg I2C_FLAG_PECERR: PEC error in reception
  1003. * @arg I2C_FLAG_TIMEOUT: Timeout or Tlow detection flag
  1004. * @arg I2C_FLAG_ALERT: SMBus Alert
  1005. * @retval The new state of I2C_FLAG (SET or RESET).
  1006. */
  1007. void I2C_ClearFlag(I2C_TypeDef* I2Cx, uint32_t I2C_FLAG)
  1008. {
  1009. /* Check the parameters */
  1010. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  1011. assert_param(IS_I2C_CLEAR_FLAG(I2C_FLAG));
  1012. /* Clear the selected flag */
  1013. I2Cx->ICR = I2C_FLAG;
  1014. }
  1015. /**
  1016. * @brief Checks whether the specified I2C interrupt has occurred or not.
  1017. * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
  1018. * @param I2C_IT: specifies the interrupt source to check.
  1019. * This parameter can be one of the following values:
  1020. * @arg I2C_IT_TXIS: Transmit interrupt status
  1021. * @arg I2C_IT_RXNE: Receive data register not empty
  1022. * @arg I2C_IT_ADDR: Address matched (slave mode)
  1023. * @arg I2C_IT_NACKF: NACK received flag
  1024. * @arg I2C_IT_STOPF: STOP detection flag
  1025. * @arg I2C_IT_TC: Transfer complete (master mode)
  1026. * @arg I2C_IT_TCR: Transfer complete reload
  1027. * @arg I2C_IT_BERR: Bus error
  1028. * @arg I2C_IT_ARLO: Arbitration lost
  1029. * @arg I2C_IT_OVR: Overrun/Underrun
  1030. * @arg I2C_IT_PECERR: PEC error in reception
  1031. * @arg I2C_IT_TIMEOUT: Timeout or Tlow detection flag
  1032. * @arg I2C_IT_ALERT: SMBus Alert
  1033. * @retval The new state of I2C_IT (SET or RESET).
  1034. */
  1035. ITStatus I2C_GetITStatus(I2C_TypeDef* I2Cx, uint32_t I2C_IT)
  1036. {
  1037. uint32_t tmpreg = 0;
  1038. ITStatus bitstatus = RESET;
  1039. uint32_t enablestatus = 0;
  1040. /* Check the parameters */
  1041. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  1042. assert_param(IS_I2C_GET_IT(I2C_IT));
  1043. /* Check if the interrupt source is enabled or not */
  1044. /* If Error interrupt */
  1045. if ((uint32_t)(I2C_IT & ERROR_IT_MASK))
  1046. {
  1047. enablestatus = (uint32_t)((I2C_CR1_ERRIE) & (I2Cx->CR1));
  1048. }
  1049. /* If TC interrupt */
  1050. else if ((uint32_t)(I2C_IT & TC_IT_MASK))
  1051. {
  1052. enablestatus = (uint32_t)((I2C_CR1_TCIE) & (I2Cx->CR1));
  1053. }
  1054. else
  1055. {
  1056. enablestatus = (uint32_t)((I2C_IT) & (I2Cx->CR1));
  1057. }
  1058. /* Get the ISR register value */
  1059. tmpreg = I2Cx->ISR;
  1060. /* Get flag status */
  1061. tmpreg &= I2C_IT;
  1062. /* Check the status of the specified I2C flag */
  1063. if((tmpreg != RESET) && enablestatus)
  1064. {
  1065. /* I2C_IT is set */
  1066. bitstatus = SET;
  1067. }
  1068. else
  1069. {
  1070. /* I2C_IT is reset */
  1071. bitstatus = RESET;
  1072. }
  1073. /* Return the I2C_IT status */
  1074. return bitstatus;
  1075. }
  1076. /**
  1077. * @brief Clears the I2Cx's interrupt pending bits.
  1078. * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
  1079. * @param I2C_IT: specifies the interrupt pending bit to clear.
  1080. * This parameter can be any combination of the following values:
  1081. * @arg I2C_IT_ADDR: Address matched (slave mode)
  1082. * @arg I2C_IT_NACKF: NACK received flag
  1083. * @arg I2C_IT_STOPF: STOP detection flag
  1084. * @arg I2C_IT_BERR: Bus error
  1085. * @arg I2C_IT_ARLO: Arbitration lost
  1086. * @arg I2C_IT_OVR: Overrun/Underrun
  1087. * @arg I2C_IT_PECERR: PEC error in reception
  1088. * @arg I2C_IT_TIMEOUT: Timeout or Tlow detection flag
  1089. * @arg I2C_IT_ALERT: SMBus Alert
  1090. * @retval The new state of I2C_IT (SET or RESET).
  1091. */
  1092. void I2C_ClearITPendingBit(I2C_TypeDef* I2Cx, uint32_t I2C_IT)
  1093. {
  1094. /* Check the parameters */
  1095. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  1096. assert_param(IS_I2C_CLEAR_IT(I2C_IT));
  1097. /* Clear the selected flag */
  1098. I2Cx->ICR = I2C_IT;
  1099. }
  1100. /**
  1101. * @}
  1102. */
  1103. /**
  1104. * @}
  1105. */
  1106. /**
  1107. * @}
  1108. */
  1109. /**
  1110. * @}
  1111. */
  1112. /************************ (C) COPYRIGHT FMD *****END OF FILE****/