2 ******************************************************************************
\r
3 * @file stm32l1xx_i2c.c
\r
4 * @author MCD Application Team
\r
6 * @date 05-March-2012
\r
7 * @brief This file provides firmware functions to manage the following
\r
8 * functionalities of the Inter-integrated circuit (I2C)
\r
9 * + Initialization and Configuration
\r
12 * + DMA transfers management
\r
13 * + Interrupts, events and flags management
\r
17 * ============================================================================
\r
18 * ##### How to use this driver #####
\r
19 * ============================================================================
\r
21 (#) Enable peripheral clock using RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2Cx, ENABLE)
\r
22 function for I2C1 or I2C2.
\r
23 (#) Enable SDA, SCL and SMBA (when used) GPIO clocks using
\r
24 RCC_AHBPeriphClockCmd() function.
\r
25 (#) Peripherals alternate function:
\r
26 (++) Connect the pin to the desired peripherals' Alternate
\r
27 Function (AF) using GPIO_PinAFConfig() function.
\r
28 (++) Configure the desired pin in alternate function by:
\r
29 GPIO_InitStruct->GPIO_Mode = GPIO_Mode_AF
\r
30 (++) Select the type, pull-up/pull-down and output speed via
\r
31 GPIO_PuPd, GPIO_OType and GPIO_Speed members
\r
32 (++) Call GPIO_Init() function.
\r
33 (#) Program the Mode, duty cycle , Own address, Ack, Speed and Acknowledged
\r
34 Address using the I2C_Init() function.
\r
35 (#) Optionally you can enable/configure the following parameters without
\r
36 re-initialization (i.e there is no need to call again I2C_Init() function):
\r
37 (++) Enable the acknowledge feature using I2C_AcknowledgeConfig() function.
\r
38 (++) Enable the dual addressing mode using I2C_DualAddressCmd() function.
\r
39 (++) Enable the general call using the I2C_GeneralCallCmd() function.
\r
40 (++) Enable the clock stretching using I2C_StretchClockCmd() function.
\r
41 (++) Enable the fast mode duty cycle using the I2C_FastModeDutyCycleConfig()
\r
43 (++) Enable the PEC Calculation using I2C_CalculatePEC() function.
\r
44 (++) For SMBus Mode:
\r
45 (+++) Enable the Address Resolution Protocol (ARP) using I2C_ARPCmd() function.
\r
46 (+++) Configure the SMBusAlert pin using I2C_SMBusAlertConfig() function.
\r
47 (#) Enable the NVIC and the corresponding interrupt using the function
\r
48 I2C_ITConfig() if you need to use interrupt mode.
\r
49 (#) When using the DMA mode
\r
50 (++) Configure the DMA using DMA_Init() function.
\r
51 (++) Active the needed channel Request using I2C_DMACmd() or
\r
52 I2C_DMALastTransferCmd() function.
\r
53 (#) Enable the I2C using the I2C_Cmd() function.
\r
54 (#) Enable the DMA using the DMA_Cmd() function when using DMA mode in the
\r
57 ******************************************************************************
\r
60 * <h2><center>© COPYRIGHT 2012 STMicroelectronics</center></h2>
\r
62 * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
\r
63 * You may not use this file except in compliance with the License.
\r
64 * You may obtain a copy of the License at:
\r
66 * http://www.st.com/software_license_agreement_liberty_v2
\r
68 * Unless required by applicable law or agreed to in writing, software
\r
69 * distributed under the License is distributed on an "AS IS" BASIS,
\r
70 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
71 * See the License for the specific language governing permissions and
\r
72 * limitations under the License.
\r
74 ******************************************************************************
\r
77 /* Includes ------------------------------------------------------------------*/
\r
78 #include "stm32l1xx_i2c.h"
\r
79 #include "stm32l1xx_rcc.h"
\r
82 /** @addtogroup STM32L1xx_StdPeriph_Driver
\r
87 * @brief I2C driver modules
\r
91 /* Private typedef -----------------------------------------------------------*/
\r
92 /* Private define ------------------------------------------------------------*/
\r
94 #define CR1_CLEAR_MASK ((uint16_t)0xFBF5) /*<! I2C registers Masks */
\r
95 #define FLAG_MASK ((uint32_t)0x00FFFFFF) /*<! I2C FLAG mask */
\r
96 #define ITEN_MASK ((uint32_t)0x07000000) /*<! I2C Interrupt Enable mask */
\r
98 /* Private macro -------------------------------------------------------------*/
\r
99 /* Private variables ---------------------------------------------------------*/
\r
100 /* Private function prototypes -----------------------------------------------*/
\r
101 /* Private functions ---------------------------------------------------------*/
\r
103 /** @defgroup I2C_Private_Functions
\r
107 /** @defgroup I2C_Group1 Initialization and Configuration functions
\r
108 * @brief Initialization and Configuration functions
\r
111 ===============================================================================
\r
112 ##### Initialization and Configuration functions #####
\r
113 ===============================================================================
\r
120 * @brief Deinitializes the I2Cx peripheral registers to their default reset values.
\r
121 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
\r
124 void I2C_DeInit(I2C_TypeDef* I2Cx)
\r
126 /* Check the parameters */
\r
127 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
\r
131 /* Enable I2C1 reset state */
\r
132 RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, ENABLE);
\r
133 /* Release I2C1 from reset state */
\r
134 RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, DISABLE);
\r
138 /* Enable I2C2 reset state */
\r
139 RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, ENABLE);
\r
140 /* Release I2C2 from reset state */
\r
141 RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, DISABLE);
\r
146 * @brief Initializes the I2Cx peripheral according to the specified
\r
147 * parameters in the I2C_InitStruct.
\r
148 * @note To use the I2C at 400 KHz (in fast mode), the PCLK1 frequency
\r
149 * (I2C peripheral input clock) must be a multiple of 10 MHz.
\r
150 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
\r
151 * @param I2C_InitStruct: pointer to a I2C_InitTypeDef structure that
\r
152 * contains the configuration information for the specified I2C peripheral.
\r
155 void I2C_Init(I2C_TypeDef* I2Cx, I2C_InitTypeDef* I2C_InitStruct)
\r
157 uint16_t tmpreg = 0, freqrange = 0;
\r
158 uint16_t result = 0x04;
\r
159 uint32_t pclk1 = 8000000;
\r
160 RCC_ClocksTypeDef rcc_clocks;
\r
161 /* Check the parameters */
\r
162 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
\r
163 assert_param(IS_I2C_CLOCK_SPEED(I2C_InitStruct->I2C_ClockSpeed));
\r
164 assert_param(IS_I2C_MODE(I2C_InitStruct->I2C_Mode));
\r
165 assert_param(IS_I2C_DUTY_CYCLE(I2C_InitStruct->I2C_DutyCycle));
\r
166 assert_param(IS_I2C_OWN_ADDRESS1(I2C_InitStruct->I2C_OwnAddress1));
\r
167 assert_param(IS_I2C_ACK_STATE(I2C_InitStruct->I2C_Ack));
\r
168 assert_param(IS_I2C_ACKNOWLEDGE_ADDRESS(I2C_InitStruct->I2C_AcknowledgedAddress));
\r
170 /*---------------------------- I2Cx CR2 Configuration ------------------------*/
\r
171 /* Get the I2Cx CR2 value */
\r
172 tmpreg = I2Cx->CR2;
\r
173 /* Clear frequency FREQ[5:0] bits */
\r
174 tmpreg &= (uint16_t)~((uint16_t)I2C_CR2_FREQ);
\r
175 /* Get pclk1 frequency value */
\r
176 RCC_GetClocksFreq(&rcc_clocks);
\r
177 pclk1 = rcc_clocks.PCLK1_Frequency;
\r
178 /* Set frequency bits depending on pclk1 value */
\r
179 freqrange = (uint16_t)(pclk1 / 1000000);
\r
180 tmpreg |= freqrange;
\r
181 /* Write to I2Cx CR2 */
\r
182 I2Cx->CR2 = tmpreg;
\r
184 /*---------------------------- I2Cx CCR Configuration ------------------------*/
\r
185 /* Disable the selected I2C peripheral to configure TRISE */
\r
186 I2Cx->CR1 &= (uint16_t)~((uint16_t)I2C_CR1_PE);
\r
187 /* Reset tmpreg value */
\r
188 /* Clear F/S, DUTY and CCR[11:0] bits */
\r
191 /* Configure speed in standard mode */
\r
192 if (I2C_InitStruct->I2C_ClockSpeed <= 100000)
\r
194 /* Standard mode speed calculate */
\r
195 result = (uint16_t)(pclk1 / (I2C_InitStruct->I2C_ClockSpeed << 1));
\r
196 /* Test if CCR value is under 0x4*/
\r
199 /* Set minimum allowed value */
\r
202 /* Set speed value for standard mode */
\r
204 /* Set Maximum Rise Time for standard mode */
\r
205 I2Cx->TRISE = freqrange + 1;
\r
207 /* Configure speed in fast mode */
\r
208 /* To use the I2C at 400 KHz (in fast mode), the PCLK1 frequency (I2C peripheral
\r
209 input clock) must be a multiple of 10 MHz */
\r
210 else /*(I2C_InitStruct->I2C_ClockSpeed <= 400000)*/
\r
212 if (I2C_InitStruct->I2C_DutyCycle == I2C_DutyCycle_2)
\r
214 /* Fast mode speed calculate: Tlow/Thigh = 2 */
\r
215 result = (uint16_t)(pclk1 / (I2C_InitStruct->I2C_ClockSpeed * 3));
\r
217 else /*I2C_InitStruct->I2C_DutyCycle == I2C_DutyCycle_16_9*/
\r
219 /* Fast mode speed calculate: Tlow/Thigh = 16/9 */
\r
220 result = (uint16_t)(pclk1 / (I2C_InitStruct->I2C_ClockSpeed * 25));
\r
222 result |= I2C_DutyCycle_16_9;
\r
225 /* Test if CCR value is under 0x1*/
\r
226 if ((result & I2C_CCR_CCR) == 0)
\r
228 /* Set minimum allowed value */
\r
229 result |= (uint16_t)0x0001;
\r
231 /* Set speed value and set F/S bit for fast mode */
\r
232 tmpreg |= (uint16_t)(result | I2C_CCR_FS);
\r
233 /* Set Maximum Rise Time for fast mode */
\r
234 I2Cx->TRISE = (uint16_t)(((freqrange * (uint16_t)300) / (uint16_t)1000) + (uint16_t)1);
\r
237 /* Write to I2Cx CCR */
\r
238 I2Cx->CCR = tmpreg;
\r
239 /* Enable the selected I2C peripheral */
\r
240 I2Cx->CR1 |= I2C_CR1_PE;
\r
242 /*---------------------------- I2Cx CR1 Configuration ------------------------*/
\r
243 /* Get the I2Cx CR1 value */
\r
244 tmpreg = I2Cx->CR1;
\r
245 /* Clear ACK, SMBTYPE and SMBUS bits */
\r
246 tmpreg &= CR1_CLEAR_MASK;
\r
247 /* Configure I2Cx: mode and acknowledgement */
\r
248 /* Set SMBTYPE and SMBUS bits according to I2C_Mode value */
\r
249 /* Set ACK bit according to I2C_Ack value */
\r
250 tmpreg |= (uint16_t)((uint32_t)I2C_InitStruct->I2C_Mode | I2C_InitStruct->I2C_Ack);
\r
251 /* Write to I2Cx CR1 */
\r
252 I2Cx->CR1 = tmpreg;
\r
254 /*---------------------------- I2Cx OAR1 Configuration -----------------------*/
\r
255 /* Set I2Cx Own Address1 and acknowledged address */
\r
256 I2Cx->OAR1 = (I2C_InitStruct->I2C_AcknowledgedAddress | I2C_InitStruct->I2C_OwnAddress1);
\r
260 * @brief Fills each I2C_InitStruct member with its default value.
\r
261 * @param I2C_InitStruct: pointer to an I2C_InitTypeDef structure which will be initialized.
\r
264 void I2C_StructInit(I2C_InitTypeDef* I2C_InitStruct)
\r
266 /*---------------- Reset I2C init structure parameters values ----------------*/
\r
267 /* initialize the I2C_ClockSpeed member */
\r
268 I2C_InitStruct->I2C_ClockSpeed = 5000;
\r
269 /* Initialize the I2C_Mode member */
\r
270 I2C_InitStruct->I2C_Mode = I2C_Mode_I2C;
\r
271 /* Initialize the I2C_DutyCycle member */
\r
272 I2C_InitStruct->I2C_DutyCycle = I2C_DutyCycle_2;
\r
273 /* Initialize the I2C_OwnAddress1 member */
\r
274 I2C_InitStruct->I2C_OwnAddress1 = 0;
\r
275 /* Initialize the I2C_Ack member */
\r
276 I2C_InitStruct->I2C_Ack = I2C_Ack_Disable;
\r
277 /* Initialize the I2C_AcknowledgedAddress member */
\r
278 I2C_InitStruct->I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
\r
282 * @brief Enables or disables the specified I2C peripheral.
\r
283 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
\r
284 * @param NewState: new state of the I2Cx peripheral.
\r
285 * This parameter can be: ENABLE or DISABLE.
\r
288 void I2C_Cmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
\r
290 /* Check the parameters */
\r
291 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
\r
292 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
293 if (NewState != DISABLE)
\r
295 /* Enable the selected I2C peripheral */
\r
296 I2Cx->CR1 |= I2C_CR1_PE;
\r
300 /* Disable the selected I2C peripheral */
\r
301 I2Cx->CR1 &= (uint16_t)~((uint16_t)I2C_CR1_PE);
\r
306 * @brief Generates I2Cx communication START condition.
\r
307 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
\r
308 * @param NewState: new state of the I2C START condition generation.
\r
309 * This parameter can be: ENABLE or DISABLE.
\r
312 void I2C_GenerateSTART(I2C_TypeDef* I2Cx, FunctionalState NewState)
\r
314 /* Check the parameters */
\r
315 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
\r
316 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
317 if (NewState != DISABLE)
\r
319 /* Generate a START condition */
\r
320 I2Cx->CR1 |= I2C_CR1_START;
\r
324 /* Disable the START condition generation */
\r
325 I2Cx->CR1 &= (uint16_t)~((uint16_t)I2C_CR1_START);
\r
330 * @brief Generates I2Cx communication STOP condition.
\r
331 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
\r
332 * @param NewState: new state of the I2C STOP condition generation.
\r
333 * This parameter can be: ENABLE or DISABLE.
\r
336 void I2C_GenerateSTOP(I2C_TypeDef* I2Cx, FunctionalState NewState)
\r
338 /* Check the parameters */
\r
339 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
\r
340 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
341 if (NewState != DISABLE)
\r
343 /* Generate a STOP condition */
\r
344 I2Cx->CR1 |= I2C_CR1_STOP;
\r
348 /* Disable the STOP condition generation */
\r
349 I2Cx->CR1 &= (uint16_t)~((uint16_t)I2C_CR1_STOP);
\r
354 * @brief Enables or disables the specified I2C acknowledge feature.
\r
355 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
\r
356 * @param NewState: new state of the I2C Acknowledgement.
\r
357 * This parameter can be: ENABLE or DISABLE.
\r
360 void I2C_AcknowledgeConfig(I2C_TypeDef* I2Cx, FunctionalState NewState)
\r
362 /* Check the parameters */
\r
363 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
\r
364 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
365 if (NewState != DISABLE)
\r
367 /* Enable the acknowledgement */
\r
368 I2Cx->CR1 |= I2C_CR1_ACK;
\r
372 /* Disable the acknowledgement */
\r
373 I2Cx->CR1 &= (uint16_t)~((uint16_t)I2C_CR1_ACK);
\r
378 * @brief Configures the specified I2C own address2.
\r
379 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
\r
380 * @param Address: specifies the 7bit I2C own address2.
\r
383 void I2C_OwnAddress2Config(I2C_TypeDef* I2Cx, uint8_t Address)
\r
385 uint16_t tmpreg = 0;
\r
387 /* Check the parameters */
\r
388 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
\r
390 /* Get the old register value */
\r
391 tmpreg = I2Cx->OAR2;
\r
393 /* Reset I2Cx Own address2 bit [7:1] */
\r
394 tmpreg &= (uint16_t)~((uint16_t)I2C_OAR2_ADD2);
\r
396 /* Set I2Cx Own address2 */
\r
397 tmpreg |= (uint16_t)((uint16_t)Address & (uint16_t)0x00FE);
\r
399 /* Store the new register value */
\r
400 I2Cx->OAR2 = tmpreg;
\r
404 * @brief Enables or disables the specified I2C dual addressing mode.
\r
405 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
\r
406 * @param NewState: new state of the I2C dual addressing mode.
\r
407 * This parameter can be: ENABLE or DISABLE.
\r
410 void I2C_DualAddressCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
\r
412 /* Check the parameters */
\r
413 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
\r
414 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
415 if (NewState != DISABLE)
\r
417 /* Enable dual addressing mode */
\r
418 I2Cx->OAR2 |= I2C_OAR2_ENDUAL;
\r
422 /* Disable dual addressing mode */
\r
423 I2Cx->OAR2 &= (uint16_t)~((uint16_t)I2C_OAR2_ENDUAL);
\r
428 * @brief Enables or disables the specified I2C general call feature.
\r
429 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
\r
430 * @param NewState: new state of the I2C General call.
\r
431 * This parameter can be: ENABLE or DISABLE.
\r
434 void I2C_GeneralCallCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
\r
436 /* Check the parameters */
\r
437 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
\r
438 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
439 if (NewState != DISABLE)
\r
441 /* Enable generall call */
\r
442 I2Cx->CR1 |= I2C_CR1_ENGC;
\r
446 /* Disable generall call */
\r
447 I2Cx->CR1 &= (uint16_t)~((uint16_t)I2C_CR1_ENGC);
\r
452 * @brief Enables or disables the specified I2C software reset.
\r
453 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
\r
454 * @param NewState: new state of the I2C software reset.
\r
455 * This parameter can be: ENABLE or DISABLE.
\r
458 void I2C_SoftwareResetCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
\r
460 /* Check the parameters */
\r
461 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
\r
462 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
463 if (NewState != DISABLE)
\r
465 /* Peripheral under reset */
\r
466 I2Cx->CR1 |= I2C_CR1_SWRST;
\r
470 /* Peripheral not under reset */
\r
471 I2Cx->CR1 &= (uint16_t)~((uint16_t)I2C_CR1_SWRST);
\r
476 * @brief Drives the SMBusAlert pin high or low for the specified I2C.
\r
477 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
\r
478 * @param I2C_SMBusAlert: specifies SMBAlert pin level.
\r
479 * This parameter can be one of the following values:
\r
480 * @arg I2C_SMBusAlert_Low: SMBAlert pin driven low
\r
481 * @arg I2C_SMBusAlert_High: SMBAlert pin driven high
\r
484 void I2C_SMBusAlertConfig(I2C_TypeDef* I2Cx, uint16_t I2C_SMBusAlert)
\r
486 /* Check the parameters */
\r
487 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
\r
488 assert_param(IS_I2C_SMBUS_ALERT(I2C_SMBusAlert));
\r
489 if (I2C_SMBusAlert == I2C_SMBusAlert_Low)
\r
491 /* Drive the SMBusAlert pin Low */
\r
492 I2Cx->CR1 |= I2C_SMBusAlert_Low;
\r
496 /* Drive the SMBusAlert pin High */
\r
497 I2Cx->CR1 &= I2C_SMBusAlert_High;
\r
502 * @brief Enables or disables the specified I2C ARP.
\r
503 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
\r
504 * @param NewState: new state of the I2Cx ARP.
\r
505 * This parameter can be: ENABLE or DISABLE.
\r
508 void I2C_ARPCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
\r
510 /* Check the parameters */
\r
511 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
\r
512 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
513 if (NewState != DISABLE)
\r
515 /* Enable the selected I2C ARP */
\r
516 I2Cx->CR1 |= I2C_CR1_ENARP;
\r
520 /* Disable the selected I2C ARP */
\r
521 I2Cx->CR1 &= (uint16_t)~((uint16_t)I2C_CR1_ENARP);
\r
526 * @brief Enables or disables the specified I2C Clock stretching.
\r
527 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
\r
528 * @param NewState: new state of the I2Cx Clock stretching.
\r
529 * This parameter can be: ENABLE or DISABLE.
\r
532 void I2C_StretchClockCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
\r
534 /* Check the parameters */
\r
535 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
\r
536 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
537 if (NewState == DISABLE)
\r
539 /* Enable the selected I2C Clock stretching */
\r
540 I2Cx->CR1 |= I2C_CR1_NOSTRETCH;
\r
544 /* Disable the selected I2C Clock stretching */
\r
545 I2Cx->CR1 &= (uint16_t)~((uint16_t)I2C_CR1_NOSTRETCH);
\r
550 * @brief Selects the specified I2C fast mode duty cycle.
\r
551 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
\r
552 * @param I2C_DutyCycle: specifies the fast mode duty cycle.
\r
553 * This parameter can be one of the following values:
\r
554 * @arg I2C_DutyCycle_2: I2C fast mode Tlow/Thigh = 2
\r
555 * @arg I2C_DutyCycle_16_9: I2C fast mode Tlow/Thigh = 16/9
\r
558 void I2C_FastModeDutyCycleConfig(I2C_TypeDef* I2Cx, uint16_t I2C_DutyCycle)
\r
560 /* Check the parameters */
\r
561 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
\r
562 assert_param(IS_I2C_DUTY_CYCLE(I2C_DutyCycle));
\r
563 if (I2C_DutyCycle != I2C_DutyCycle_16_9)
\r
565 /* I2C fast mode Tlow/Thigh=2 */
\r
566 I2Cx->CCR &= I2C_DutyCycle_2;
\r
570 /* I2C fast mode Tlow/Thigh=16/9 */
\r
571 I2Cx->CCR |= I2C_DutyCycle_16_9;
\r
576 * @brief Transmits the address byte to select the slave device.
\r
577 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
\r
578 * @param Address: specifies the slave address which will be transmitted.
\r
579 * @param I2C_Direction: specifies whether the I2C device will be a
\r
580 * Transmitter or a Receiver. This parameter can be one of the following values:
\r
581 * @arg I2C_Direction_Transmitter: Transmitter mode
\r
582 * @arg I2C_Direction_Receiver: Receiver mode
\r
585 void I2C_Send7bitAddress(I2C_TypeDef* I2Cx, uint8_t Address, uint8_t I2C_Direction)
\r
587 /* Check the parameters */
\r
588 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
\r
589 assert_param(IS_I2C_DIRECTION(I2C_Direction));
\r
590 /* Test on the direction to set/reset the read/write bit */
\r
591 if (I2C_Direction != I2C_Direction_Transmitter)
\r
593 /* Set the address bit0 for read */
\r
594 Address |= I2C_OAR1_ADD0;
\r
598 /* Reset the address bit0 for write */
\r
599 Address &= (uint8_t)~((uint8_t)I2C_OAR1_ADD0);
\r
601 /* Send the address */
\r
602 I2Cx->DR = Address;
\r
609 /** @defgroup I2C_Group2 Data transfers functions
\r
610 * @brief Data transfers functions
\r
613 ===============================================================================
\r
614 ##### Data transfers functions #####
\r
615 ===============================================================================
\r
622 * @brief Sends a data byte through the I2Cx peripheral.
\r
623 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
\r
624 * @param Data: Byte to be transmitted.
\r
627 void I2C_SendData(I2C_TypeDef* I2Cx, uint8_t Data)
\r
629 /* Check the parameters */
\r
630 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
\r
631 /* Write in the DR register the data to be sent */
\r
636 * @brief Returns the most recent received data by the I2Cx peripheral.
\r
637 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
\r
638 * @retval The value of the received data.
\r
640 uint8_t I2C_ReceiveData(I2C_TypeDef* I2Cx)
\r
642 /* Check the parameters */
\r
643 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
\r
644 /* Return the data in the DR register */
\r
645 return (uint8_t)I2Cx->DR;
\r
649 * @brief Selects the specified I2C NACK position in master receiver mode.
\r
650 * This function is useful in I2C Master Receiver mode when the number
\r
651 * of data to be received is equal to 2. In this case, this function
\r
652 * should be called (with parameter I2C_NACKPosition_Next) before data
\r
653 * reception starts,as described in the 2-byte reception procedure
\r
654 * recommended in Reference Manual in Section: Master receiver.
\r
655 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
\r
656 * @param I2C_NACKPosition: specifies the NACK position.
\r
657 * This parameter can be one of the following values:
\r
658 * @arg I2C_NACKPosition_Next: indicates that the next byte will be the last
\r
660 * @arg I2C_NACKPosition_Current: indicates that current byte is the last
\r
662 * @note This function configures the same bit (POS) as I2C_PECPositionConfig()
\r
663 * but is intended to be used in I2C mode while I2C_PECPositionConfig()
\r
664 * is intended to used in SMBUS mode.
\r
668 void I2C_NACKPositionConfig(I2C_TypeDef* I2Cx, uint16_t I2C_NACKPosition)
\r
670 /* Check the parameters */
\r
671 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
\r
672 assert_param(IS_I2C_NACK_POSITION(I2C_NACKPosition));
\r
674 /* Check the input parameter */
\r
675 if (I2C_NACKPosition == I2C_NACKPosition_Next)
\r
677 /* Next byte in shift register is the last received byte */
\r
678 I2Cx->CR1 |= I2C_NACKPosition_Next;
\r
682 /* Current byte in shift register is the last received byte */
\r
683 I2Cx->CR1 &= I2C_NACKPosition_Current;
\r
691 /** @defgroup I2C_Group3 PEC management functions
\r
692 * @brief PEC management functions
\r
695 ===============================================================================
\r
696 ##### PEC management functions #####
\r
697 ===============================================================================
\r
704 * @brief Enables or disables the specified I2C PEC transfer.
\r
705 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
\r
706 * @param NewState: new state of the I2C PEC transmission.
\r
707 * This parameter can be: ENABLE or DISABLE.
\r
710 void I2C_TransmitPEC(I2C_TypeDef* I2Cx, FunctionalState NewState)
\r
712 /* Check the parameters */
\r
713 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
\r
714 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
715 if (NewState != DISABLE)
\r
717 /* Enable the selected I2C PEC transmission */
\r
718 I2Cx->CR1 |= I2C_CR1_PEC;
\r
722 /* Disable the selected I2C PEC transmission */
\r
723 I2Cx->CR1 &= (uint16_t)~((uint16_t)I2C_CR1_PEC);
\r
728 * @brief Selects the specified I2C PEC position.
\r
729 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
\r
730 * @param I2C_PECPosition: specifies the PEC position.
\r
731 * This parameter can be one of the following values:
\r
732 * @arg I2C_PECPosition_Next: indicates that the next byte is PEC
\r
733 * @arg I2C_PECPosition_Current: indicates that current byte is PEC
\r
734 * @note This function configures the same bit (POS) as I2C_NACKPositionConfig()
\r
735 * but is intended to be used in SMBUS mode while I2C_NACKPositionConfig()
\r
736 * is intended to used in I2C mode.
\r
739 void I2C_PECPositionConfig(I2C_TypeDef* I2Cx, uint16_t I2C_PECPosition)
\r
741 /* Check the parameters */
\r
742 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
\r
743 assert_param(IS_I2C_PEC_POSITION(I2C_PECPosition));
\r
744 if (I2C_PECPosition == I2C_PECPosition_Next)
\r
746 /* Next byte in shift register is PEC */
\r
747 I2Cx->CR1 |= I2C_PECPosition_Next;
\r
751 /* Current byte in shift register is PEC */
\r
752 I2Cx->CR1 &= I2C_PECPosition_Current;
\r
757 * @brief Enables or disables the PEC value calculation of the transferred bytes.
\r
758 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
\r
759 * @param NewState: new state of the I2Cx PEC value calculation.
\r
760 * This parameter can be: ENABLE or DISABLE.
\r
763 void I2C_CalculatePEC(I2C_TypeDef* I2Cx, FunctionalState NewState)
\r
765 /* Check the parameters */
\r
766 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
\r
767 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
768 if (NewState != DISABLE)
\r
770 /* Enable the selected I2C PEC calculation */
\r
771 I2Cx->CR1 |= I2C_CR1_ENPEC;
\r
775 /* Disable the selected I2C PEC calculation */
\r
776 I2Cx->CR1 &= (uint16_t)~((uint16_t)I2C_CR1_ENPEC);
\r
781 * @brief Returns the PEC value for the specified I2C.
\r
782 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
\r
783 * @retval The PEC value.
\r
785 uint8_t I2C_GetPEC(I2C_TypeDef* I2Cx)
\r
787 /* Check the parameters */
\r
788 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
\r
789 /* Return the selected I2C PEC value */
\r
790 return ((I2Cx->SR2) >> 8);
\r
797 /** @defgroup I2C_Group4 DMA transfers management functions
\r
798 * @brief DMA transfers management functions
\r
801 ===============================================================================
\r
802 ##### DMA transfers management functions #####
\r
803 ===============================================================================
\r
804 [..] This section provides functions allowing to configure the I2C DMA channels
\r
811 * @brief Enables or disables the specified I2C DMA requests.
\r
812 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
\r
813 * @param NewState: new state of the I2C DMA transfer.
\r
814 * This parameter can be: ENABLE or DISABLE.
\r
817 void I2C_DMACmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
\r
819 /* Check the parameters */
\r
820 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
\r
821 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
822 if (NewState != DISABLE)
\r
824 /* Enable the selected I2C DMA requests */
\r
825 I2Cx->CR2 |= I2C_CR2_DMAEN;
\r
829 /* Disable the selected I2C DMA requests */
\r
830 I2Cx->CR2 &= (uint16_t)~((uint16_t)I2C_CR2_DMAEN);
\r
835 * @brief Specifies that the next DMA transfer is the last one.
\r
836 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
\r
837 * @param NewState: new state of the I2C DMA last transfer.
\r
838 * This parameter can be: ENABLE or DISABLE.
\r
841 void I2C_DMALastTransferCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
\r
843 /* Check the parameters */
\r
844 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
\r
845 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
846 if (NewState != DISABLE)
\r
848 /* Next DMA transfer is the last transfer */
\r
849 I2Cx->CR2 |= I2C_CR2_LAST;
\r
853 /* Next DMA transfer is not the last transfer */
\r
854 I2Cx->CR2 &= (uint16_t)~((uint16_t)I2C_CR2_LAST);
\r
862 /** @defgroup I2C_Group5 Interrupts events and flags management functions
\r
863 * @brief Interrupts, events and flags management functions
\r
866 ===============================================================================
\r
867 ##### Interrupts, events and flags management functions #####
\r
868 ===============================================================================
\r
869 [..] This section provides functions allowing to configure the I2C Interrupts
\r
870 sources and check or clear the flags or pending bits status.
\r
871 The user should identify which mode will be used in his application to manage
\r
872 the communication: Polling mode, Interrupt mode or DMA mode.
\r
875 ##### I2C State Monitoring Functions #####
\r
876 ===============================================================================
\r
877 [..]This I2C driver provides three different ways for I2C state monitoring
\r
878 depending on the application requirements and constraints:
\r
881 ***. Basic state monitoring (Using I2C_CheckEvent() function) ***
\r
882 -----------------------------------------------------------------
\r
883 [..]It compares the status registers (SR1 and SR2) content to a given event
\r
884 (can be the combination of one or more flags).
\r
885 It returns SUCCESS if the current status includes the given flags
\r
886 and returns ERROR if one or more flags are missing in the current status.
\r
889 (++) This function is suitable for most applications as well as for
\r
890 startup activity since the events are fully described in the product
\r
891 reference manual (RM0038).
\r
892 (++) It is also suitable for users who need to define their own events.
\r
894 (++) If an error occurs (ie. error flags are set besides to the monitored
\r
895 flags), the I2C_CheckEvent() function may return SUCCESS despite
\r
896 the communication hold or corrupted real state.
\r
897 In this case, it is advised to use error interrupts to monitor
\r
898 the error events and handle them in the interrupt IRQ handler.
\r
899 -@@- For error management, it is advised to use the following functions:
\r
900 (+@@) I2C_ITConfig() to configure and enable the error interrupts
\r
902 (+@@) I2Cx_ER_IRQHandler() which is called when the error interrupt occurs.
\r
903 Where x is the peripheral instance (I2C1, I2C2 ...).
\r
904 (+@@) I2C_GetFlagStatus() or I2C_GetITStatus() to be called into the
\r
905 I2Cx_ER_IRQHandler() function in order to determine which error occurred.
\r
906 (+@@) I2C_ClearFlag() or I2C_ClearITPendingBit() and/or I2C_SoftwareResetCmd()
\r
907 and/or I2C_GenerateStop() in order to clear the error flag and source
\r
908 and return to correct communication status.
\r
910 *** Advanced state monitoring (Using the function I2C_GetLastEvent()) ***
\r
911 -------------------------------------------------------------------------
\r
912 [..] Using the function I2C_GetLastEvent() which returns the image of both status
\r
913 registers in a single word (uint32_t) (Status Register 2 value is shifted left
\r
914 by 16 bits and concatenated to Status Register 1).
\r
917 (++) This function is suitable for the same applications above but it
\r
918 allows to overcome the mentioned limitation of I2C_GetFlagStatus()
\r
920 (++) The returned value could be compared to events already defined in
\r
921 the library (stm32l1xx_i2c.h) or to custom values defined by user.
\r
922 This function is suitable when multiple flags are monitored at the
\r
924 (++) At the opposite of I2C_CheckEvent() function, this function allows
\r
925 user to choose when an event is accepted (when all events flags are
\r
926 set and no other flags are set or just when the needed flags are set
\r
927 like I2C_CheckEvent() function.
\r
930 (++) User may need to define his own events.
\r
931 (++) Same remark concerning the error management is applicable for this
\r
932 function if user decides to check only regular communication flags
\r
933 (and ignores error flags).
\r
936 *** Flag-based state monitoring (Using the function I2C_GetFlagStatus()) ***
\r
937 ----------------------------------------------------------------------------
\r
938 [..] Using the function I2C_GetFlagStatus() which simply returns the status of
\r
939 one single flag (ie. I2C_FLAG_RXNE ...).
\r
941 (++) This function could be used for specific applications or in debug
\r
943 (++) It is suitable when only one flag checking is needed (most I2C
\r
944 events are monitored through multiple flags).
\r
946 (++) When calling this function, the Status register is accessed.
\r
947 Some flags are cleared when the status register is accessed.
\r
948 So checking the status of one Flag, may clear other ones.
\r
949 (++) Function may need to be called twice or more in order to monitor
\r
952 [..] For detailed description of Events, please refer to section I2C_Events in
\r
953 stm32l1xx_i2c.h file.
\r
960 * @brief Reads the specified I2C register and returns its value.
\r
961 * @param I2C_Register: specifies the register to read.
\r
962 * This parameter can be one of the following values:
\r
963 * @arg I2C_Register_CR1: CR1 register.
\r
964 * @arg I2C_Register_CR2: CR2 register.
\r
965 * @arg I2C_Register_OAR1: OAR1 register.
\r
966 * @arg I2C_Register_OAR2: OAR2 register.
\r
967 * @arg I2C_Register_DR: DR register.
\r
968 * @arg I2C_Register_SR1: SR1 register.
\r
969 * @arg I2C_Register_SR2: SR2 register.
\r
970 * @arg I2C_Register_CCR: CCR register.
\r
971 * @arg I2C_Register_TRISE: TRISE register.
\r
972 * @retval The value of the read register.
\r
974 uint16_t I2C_ReadRegister(I2C_TypeDef* I2Cx, uint8_t I2C_Register)
\r
976 __IO uint32_t tmp = 0;
\r
978 /* Check the parameters */
\r
979 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
\r
980 assert_param(IS_I2C_REGISTER(I2C_Register));
\r
982 tmp = (uint32_t) I2Cx;
\r
983 tmp += I2C_Register;
\r
985 /* Return the selected register value */
\r
986 return (*(__IO uint16_t *) tmp);
\r
990 * @brief Enables or disables the specified I2C interrupts.
\r
991 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
\r
992 * @param I2C_IT: specifies the I2C interrupts sources to be enabled or disabled.
\r
993 * This parameter can be any combination of the following values:
\r
994 * @arg I2C_IT_BUF: Buffer interrupt mask
\r
995 * @arg I2C_IT_EVT: Event interrupt mask
\r
996 * @arg I2C_IT_ERR: Error interrupt mask
\r
997 * @param NewState: new state of the specified I2C interrupts.
\r
998 * This parameter can be: ENABLE or DISABLE.
\r
1001 void I2C_ITConfig(I2C_TypeDef* I2Cx, uint16_t I2C_IT, FunctionalState NewState)
\r
1003 /* Check the parameters */
\r
1004 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
\r
1005 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
1006 assert_param(IS_I2C_CONFIG_IT(I2C_IT));
\r
1008 if (NewState != DISABLE)
\r
1010 /* Enable the selected I2C interrupts */
\r
1011 I2Cx->CR2 |= I2C_IT;
\r
1015 /* Disable the selected I2C interrupts */
\r
1016 I2Cx->CR2 &= (uint16_t)~I2C_IT;
\r
1021 ===============================================================================
\r
1022 1. Basic state monitoring
\r
1023 ===============================================================================
\r
1027 * @brief Checks whether the last I2Cx Event is equal to the one passed
\r
1029 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
\r
1030 * @param I2C_EVENT: specifies the event to be checked.
\r
1031 * This parameter can be one of the following values:
\r
1032 * @arg I2C_EVENT_SLAVE_TRANSMITTER_ADDRESS_MATCHED: EV1
\r
1033 * @arg I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED: EV1
\r
1034 * @arg I2C_EVENT_SLAVE_TRANSMITTER_SECONDADDRESS_MATCHED: EV1
\r
1035 * @arg I2C_EVENT_SLAVE_RECEIVER_SECONDADDRESS_MATCHED: EV1
\r
1036 * @arg I2C_EVENT_SLAVE_GENERALCALLADDRESS_MATCHED: EV1
\r
1037 * @arg I2C_EVENT_SLAVE_BYTE_RECEIVED: EV2
\r
1038 * @arg (I2C_EVENT_SLAVE_BYTE_RECEIVED | I2C_FLAG_DUALF): EV2
\r
1039 * @arg (I2C_EVENT_SLAVE_BYTE_RECEIVED | I2C_FLAG_GENCALL): EV2
\r
1040 * @arg I2C_EVENT_SLAVE_BYTE_TRANSMITTED: EV3
\r
1041 * @arg (I2C_EVENT_SLAVE_BYTE_TRANSMITTED | I2C_FLAG_DUALF): EV3
\r
1042 * @arg (I2C_EVENT_SLAVE_BYTE_TRANSMITTED | I2C_FLAG_GENCALL): EV3
\r
1043 * @arg I2C_EVENT_SLAVE_ACK_FAILURE: EV3_2
\r
1044 * @arg I2C_EVENT_SLAVE_STOP_DETECTED: EV4
\r
1045 * @arg I2C_EVENT_MASTER_MODE_SELECT: EV5
\r
1046 * @arg I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED: EV6
\r
1047 * @arg I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED: EV6
\r
1048 * @arg I2C_EVENT_MASTER_BYTE_RECEIVED: EV7
\r
1049 * @arg I2C_EVENT_MASTER_BYTE_TRANSMITTING: EV8
\r
1050 * @arg I2C_EVENT_MASTER_BYTE_TRANSMITTED: EV8_2
\r
1051 * @arg I2C_EVENT_MASTER_MODE_ADDRESS10: EV9
\r
1052 * @note For detailed description of Events, please refer to section
\r
1053 * I2C_Events in stm32l1xx_i2c.h file.
\r
1054 * @retval An ErrorStatus enumeration value:
\r
1055 * - SUCCESS: Last event is equal to the I2C_EVENT
\r
1056 * - ERROR: Last event is different from the I2C_EVENT
\r
1058 ErrorStatus I2C_CheckEvent(I2C_TypeDef* I2Cx, uint32_t I2C_EVENT)
\r
1060 uint32_t lastevent = 0;
\r
1061 uint32_t flag1 = 0, flag2 = 0;
\r
1062 ErrorStatus status = ERROR;
\r
1064 /* Check the parameters */
\r
1065 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
\r
1066 assert_param(IS_I2C_EVENT(I2C_EVENT));
\r
1068 /* Read the I2Cx status register */
\r
1069 flag1 = I2Cx->SR1;
\r
1070 flag2 = I2Cx->SR2;
\r
1071 flag2 = flag2 << 16;
\r
1073 /* Get the last event value from I2C status register */
\r
1074 lastevent = (flag1 | flag2) & FLAG_MASK;
\r
1076 /* Check whether the last event contains the I2C_EVENT */
\r
1077 if ((lastevent & I2C_EVENT) == I2C_EVENT)
\r
1079 /* SUCCESS: last event is equal to I2C_EVENT */
\r
1084 /* ERROR: last event is different from I2C_EVENT */
\r
1087 /* Return status */
\r
1092 ===============================================================================
\r
1093 2. Advanced state monitoring
\r
1094 ===============================================================================
\r
1098 * @brief Returns the last I2Cx Event.
\r
1099 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
\r
1101 * @note For detailed description of Events, please refer to section
\r
1102 * I2C_Events in stm32l1xx_i2c.h file.
\r
1104 * @retval The last event
\r
1106 uint32_t I2C_GetLastEvent(I2C_TypeDef* I2Cx)
\r
1108 uint32_t lastevent = 0;
\r
1109 uint32_t flag1 = 0, flag2 = 0;
\r
1111 /* Check the parameters */
\r
1112 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
\r
1114 /* Read the I2Cx status register */
\r
1115 flag1 = I2Cx->SR1;
\r
1116 flag2 = I2Cx->SR2;
\r
1117 flag2 = flag2 << 16;
\r
1119 /* Get the last event value from I2C status register */
\r
1120 lastevent = (flag1 | flag2) & FLAG_MASK;
\r
1122 /* Return status */
\r
1127 ===============================================================================
\r
1128 3. Flag-based state monitoring
\r
1129 ===============================================================================
\r
1133 * @brief Checks whether the specified I2C flag is set or not.
\r
1134 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
\r
1135 * @param I2C_FLAG: specifies the flag to check.
\r
1136 * This parameter can be one of the following values:
\r
1137 * @arg I2C_FLAG_DUALF: Dual flag (Slave mode)
\r
1138 * @arg I2C_FLAG_SMBHOST: SMBus host header (Slave mode)
\r
1139 * @arg I2C_FLAG_SMBDEFAULT: SMBus default header (Slave mode)
\r
1140 * @arg I2C_FLAG_GENCALL: General call header flag (Slave mode)
\r
1141 * @arg I2C_FLAG_TRA: Transmitter/Receiver flag
\r
1142 * @arg I2C_FLAG_BUSY: Bus busy flag
\r
1143 * @arg I2C_FLAG_MSL: Master/Slave flag
\r
1144 * @arg I2C_FLAG_SMBALERT: SMBus Alert flag
\r
1145 * @arg I2C_FLAG_TIMEOUT: Timeout or Tlow error flag
\r
1146 * @arg I2C_FLAG_PECERR: PEC error in reception flag
\r
1147 * @arg I2C_FLAG_OVR: Overrun/Underrun flag (Slave mode)
\r
1148 * @arg I2C_FLAG_AF: Acknowledge failure flag
\r
1149 * @arg I2C_FLAG_ARLO: Arbitration lost flag (Master mode)
\r
1150 * @arg I2C_FLAG_BERR: Bus error flag
\r
1151 * @arg I2C_FLAG_TXE: Data register empty flag (Transmitter)
\r
1152 * @arg I2C_FLAG_RXNE: Data register not empty (Receiver) flag
\r
1153 * @arg I2C_FLAG_STOPF: Stop detection flag (Slave mode)
\r
1154 * @arg I2C_FLAG_ADD10: 10-bit header sent flag (Master mode)
\r
1155 * @arg I2C_FLAG_BTF: Byte transfer finished flag
\r
1156 * @arg I2C_FLAG_ADDR: Address sent flag (Master mode) "ADSL"
\r
1157 * Address matched flag (Slave mode)"ENDAD"
\r
1158 * @arg I2C_FLAG_SB: Start bit flag (Master mode)
\r
1159 * @retval The new state of I2C_FLAG (SET or RESET).
\r
1161 FlagStatus I2C_GetFlagStatus(I2C_TypeDef* I2Cx, uint32_t I2C_FLAG)
\r
1163 FlagStatus bitstatus = RESET;
\r
1164 __IO uint32_t i2creg = 0, i2cxbase = 0;
\r
1166 /* Check the parameters */
\r
1167 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
\r
1168 assert_param(IS_I2C_GET_FLAG(I2C_FLAG));
\r
1170 /* Get the I2Cx peripheral base address */
\r
1171 i2cxbase = (uint32_t)I2Cx;
\r
1173 /* Read flag register index */
\r
1174 i2creg = I2C_FLAG >> 28;
\r
1176 /* Get bit[23:0] of the flag */
\r
1177 I2C_FLAG &= FLAG_MASK;
\r
1181 /* Get the I2Cx SR1 register address */
\r
1186 /* Flag in I2Cx SR2 Register */
\r
1187 I2C_FLAG = (uint32_t)(I2C_FLAG >> 16);
\r
1188 /* Get the I2Cx SR2 register address */
\r
1192 if(((*(__IO uint32_t *)i2cxbase) & I2C_FLAG) != (uint32_t)RESET)
\r
1194 /* I2C_FLAG is set */
\r
1199 /* I2C_FLAG is reset */
\r
1200 bitstatus = RESET;
\r
1203 /* Return the I2C_FLAG status */
\r
1208 * @brief Clears the I2Cx's pending flags.
\r
1209 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
\r
1210 * @param I2C_FLAG: specifies the flag to clear.
\r
1211 * This parameter can be any combination of the following values:
\r
1212 * @arg I2C_FLAG_SMBALERT: SMBus Alert flag
\r
1213 * @arg I2C_FLAG_TIMEOUT: Timeout or Tlow error flag
\r
1214 * @arg I2C_FLAG_PECERR: PEC error in reception flag
\r
1215 * @arg I2C_FLAG_OVR: Overrun/Underrun flag (Slave mode)
\r
1216 * @arg I2C_FLAG_AF: Acknowledge failure flag
\r
1217 * @arg I2C_FLAG_ARLO: Arbitration lost flag (Master mode)
\r
1218 * @arg I2C_FLAG_BERR: Bus error flag
\r
1221 *@note STOPF (STOP detection) is cleared by software sequence: a read operation
\r
1222 * to I2C_SR1 register (I2C_GetFlagStatus()) followed by a write operation
\r
1223 * to I2C_CR1 register (I2C_Cmd() to re-enable the I2C peripheral).
\r
1224 *@note ADD10 (10-bit header sent) is cleared by software sequence: a read
\r
1225 * operation to I2C_SR1 (I2C_GetFlagStatus()) followed by writing the
\r
1226 * second byte of the address in DR register.
\r
1227 *@note BTF (Byte Transfer Finished) is cleared by software sequence: a read
\r
1228 * operation to I2C_SR1 register (I2C_GetFlagStatus()) followed by a
\r
1229 * read/write to I2C_DR register (I2C_SendData()).
\r
1230 *@note ADDR (Address sent) is cleared by software sequence: a read operation to
\r
1231 * I2C_SR1 register (I2C_GetFlagStatus()) followed by a read operation to
\r
1232 * I2C_SR2 register ((void)(I2Cx->SR2)).
\r
1233 *@note SB (Start Bit) is cleared software sequence: a read operation to I2C_SR1
\r
1234 * register (I2C_GetFlagStatus()) followed by a write operation to I2C_DR
\r
1235 * register (I2C_SendData()).
\r
1238 void I2C_ClearFlag(I2C_TypeDef* I2Cx, uint32_t I2C_FLAG)
\r
1240 uint32_t flagpos = 0;
\r
1241 /* Check the parameters */
\r
1242 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
\r
1243 assert_param(IS_I2C_CLEAR_FLAG(I2C_FLAG));
\r
1244 /* Get the I2C flag position */
\r
1245 flagpos = I2C_FLAG & FLAG_MASK;
\r
1246 /* Clear the selected I2C flag */
\r
1247 I2Cx->SR1 = (uint16_t)~flagpos;
\r
1251 * @brief Checks whether the specified I2C interrupt has occurred or not.
\r
1252 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
\r
1253 * @param I2C_IT: specifies the interrupt source to check.
\r
1254 * This parameter can be one of the following values:
\r
1255 * @arg I2C_IT_SMBALERT: SMBus Alert flag
\r
1256 * @arg I2C_IT_TIMEOUT: Timeout or Tlow error flag
\r
1257 * @arg I2C_IT_PECERR: PEC error in reception flag
\r
1258 * @arg I2C_IT_OVR: Overrun/Underrun flag (Slave mode)
\r
1259 * @arg I2C_IT_AF: Acknowledge failure flag
\r
1260 * @arg I2C_IT_ARLO: Arbitration lost flag (Master mode)
\r
1261 * @arg I2C_IT_BERR: Bus error flag
\r
1262 * @arg I2C_IT_TXE: Data register empty flag (Transmitter)
\r
1263 * @arg I2C_IT_RXNE: Data register not empty (Receiver) flag
\r
1264 * @arg I2C_IT_STOPF: Stop detection flag (Slave mode)
\r
1265 * @arg I2C_IT_ADD10: 10-bit header sent flag (Master mode)
\r
1266 * @arg I2C_IT_BTF: Byte transfer finished flag
\r
1267 * @arg I2C_IT_ADDR: Address sent flag (Master mode) "ADSL"
\r
1268 * Address matched flag (Slave mode)"ENDAD"
\r
1269 * @arg I2C_IT_SB: Start bit flag (Master mode)
\r
1270 * @retval The new state of I2C_IT (SET or RESET).
\r
1272 ITStatus I2C_GetITStatus(I2C_TypeDef* I2Cx, uint32_t I2C_IT)
\r
1274 ITStatus bitstatus = RESET;
\r
1275 uint32_t enablestatus = 0;
\r
1277 /* Check the parameters */
\r
1278 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
\r
1279 assert_param(IS_I2C_GET_IT(I2C_IT));
\r
1281 /* Check if the interrupt source is enabled or not */
\r
1282 enablestatus = (uint32_t)(((I2C_IT & ITEN_MASK) >> 16) & (I2Cx->CR2)) ;
\r
1284 /* Get bit[23:0] of the flag */
\r
1285 I2C_IT &= FLAG_MASK;
\r
1287 /* Check the status of the specified I2C flag */
\r
1288 if (((I2Cx->SR1 & I2C_IT) != (uint32_t)RESET) && enablestatus)
\r
1290 /* I2C_IT is set */
\r
1295 /* I2C_IT is reset */
\r
1296 bitstatus = RESET;
\r
1298 /* Return the I2C_IT status */
\r
1303 * @brief Clears the I2Cx's interrupt pending bits.
\r
1304 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
\r
1305 * @param I2C_IT: specifies the interrupt pending bit to clear.
\r
1306 * This parameter can be any combination of the following values:
\r
1307 * @arg I2C_IT_SMBALERT: SMBus Alert interrupt
\r
1308 * @arg I2C_IT_TIMEOUT: Timeout or Tlow error interrupt
\r
1309 * @arg I2C_IT_PECERR: PEC error in reception interrupt
\r
1310 * @arg I2C_IT_OVR: Overrun/Underrun interrupt (Slave mode)
\r
1311 * @arg I2C_IT_AF: Acknowledge failure interrupt
\r
1312 * @arg I2C_IT_ARLO: Arbitration lost interrupt (Master mode)
\r
1313 * @arg I2C_IT_BERR: Bus error interrupt
\r
1316 * @note STOPF (STOP detection) is cleared by software sequence: a read operation
\r
1317 * to I2C_SR1 register (I2C_GetITStatus()) followed by a write operation to
\r
1318 * I2C_CR1 register (I2C_Cmd() to re-enable the I2C peripheral).
\r
1319 * @note ADD10 (10-bit header sent) is cleared by software sequence: a read
\r
1320 * operation to I2C_SR1 (I2C_GetITStatus()) followed by writing the second
\r
1321 * byte of the address in I2C_DR register.
\r
1322 * @note BTF (Byte Transfer Finished) is cleared by software sequence: a read
\r
1323 * operation to I2C_SR1 register (I2C_GetITStatus()) followed by a
\r
1324 * read/write to I2C_DR register (I2C_SendData()).
\r
1325 * @note ADDR (Address sent) is cleared by software sequence: a read operation to
\r
1326 * I2C_SR1 register (I2C_GetITStatus()) followed by a read operation to
\r
1327 * I2C_SR2 register ((void)(I2Cx->SR2)).
\r
1328 * @note SB (Start Bit) is cleared by software sequence: a read operation to
\r
1329 * I2C_SR1 register (I2C_GetITStatus()) followed by a write operation to
\r
1330 * I2C_DR register (I2C_SendData()).
\r
1333 void I2C_ClearITPendingBit(I2C_TypeDef* I2Cx, uint32_t I2C_IT)
\r
1335 uint32_t flagpos = 0;
\r
1336 /* Check the parameters */
\r
1337 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
\r
1338 assert_param(IS_I2C_CLEAR_IT(I2C_IT));
\r
1339 /* Get the I2C flag position */
\r
1340 flagpos = I2C_IT & FLAG_MASK;
\r
1341 /* Clear the selected I2C flag */
\r
1342 I2Cx->SR1 = (uint16_t)~flagpos;
\r
1361 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
\r