stm8l15x_i2c.c 48 KB

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