1 /******************** (C) COPYRIGHT 2007 STMicroelectronics ********************
2 * File Name : stm32f10x_i2c.c
3 * Author : MCD Application Team
4 * Date First Issued : 09/29/2006
5 * Description : This file provides all the I2C firmware functions.
6 ********************************************************************************
11 ********************************************************************************
12 * THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
13 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
14 * AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
15 * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
16 * CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
17 * INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
18 *******************************************************************************/
20 /* Includes ------------------------------------------------------------------*/
21 #include "stm32f10x_i2c.h"
22 #include "stm32f10x_rcc.h"
24 /* Private typedef -----------------------------------------------------------*/
25 /* Private define ------------------------------------------------------------*/
27 #define CR1_PE_Set ((u16)0x0001)
28 #define CR1_PE_Reset ((u16)0xFFFE)
31 #define CR2_DMAEN_Set ((u16)0x0800)
32 #define CR2_DMAEN_Reset ((u16)0xF7FF)
35 #define CR2_LAST_Set ((u16)0x1000)
36 #define CR2_LAST_Reset ((u16)0xEFFF)
39 #define CR1_START_Set ((u16)0x0100)
40 #define CR1_START_Reset ((u16)0xFEFF)
43 #define CR1_STOP_Set ((u16)0x0200)
44 #define CR1_STOP_Reset ((u16)0xFDFF)
47 #define CR1_ACK_Set ((u16)0x0400)
48 #define CR1_ACK_Reset ((u16)0xFBFF)
51 #define CR1_ENGC_Set ((u16)0x0040)
52 #define CR1_ENGC_Reset ((u16)0xFFBF)
55 #define OAR1_ADD0_Set ((u16)0x0001)
56 #define OAR1_ADD0_Reset ((u16)0xFFFE)
59 #define CR1_SWRST_Set ((u16)0x8000)
60 #define CR1_SWRST_Reset ((u16)0x7FFF)
63 #define CR1_PEC_Set ((u16)0x1000)
66 #define CR1_ENPEC_Set ((u16)0x0020)
67 #define CR1_ENPEC_Reset ((u16)0xFFDF)
70 #define CR1_ENARP_Set ((u16)0x0010)
71 #define CR1_ENARP_Reset ((u16)0xFFEF)
73 /* I2C NOSTRETCH mask */
74 #define CR1_NOSTRETCH_Set ((u16)0x0080)
75 #define CR1_NOSTRETCH_Reset ((u16)0xFF7F)
78 #define OAR2_ENDUAL_Set ((u16)0x0001)
79 #define OAR2_ENDUAL_Reset ((u16)0xFFFE)
82 #define CCR_FS_Set ((u16)0x8000)
85 #define OAR2_ADD2_Reset ((u16)0xFF01)
88 #define CR2_FREQ_Reset ((u16)0xFFC0)
91 #define CCR_CCR_Set ((u16)0x0FFF)
94 #define I2C_FLAG_Mask ((u32)0x00FFFFFF)
96 /* I2C registers Masks */
97 #define CR1_CLEAR_Mask ((u16)0xFBF5)
99 /* Private macro -------------------------------------------------------------*/
100 /* Private variables ---------------------------------------------------------*/
101 /* Private function prototypes -----------------------------------------------*/
102 /* Private functions ---------------------------------------------------------*/
104 /*******************************************************************************
105 * Function Name : I2C_DeInit
106 * Description : Deinitializes the I2Cx peripheral registers to their default
108 * Input : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
111 *******************************************************************************/
112 void I2C_DeInit(I2C_TypeDef* I2Cx)
114 switch (*(u32*)&I2Cx)
117 /* Enable I2C1 reset state */
118 RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, ENABLE);
119 /* Release I2C1 from reset state */
120 RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, DISABLE);
124 /* Enable I2C2 reset state */
125 RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, ENABLE);
126 /* Release I2C2 from reset state */
127 RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, DISABLE);
135 /*******************************************************************************
136 * Function Name : I2C_Init
137 * Description : Initializes the I2Cx according to the specified parameters
138 * in the I2C_InitStruct.
139 * Input : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
140 * - I2C_InitStruct: pointer to a I2C_InitTypeDef structure that
141 * contains the configuration information for the specified
145 ******************************************************************************/
146 void I2C_Init(I2C_TypeDef* I2Cx, I2C_InitTypeDef* I2C_InitStruct)
148 u16 tmpreg = 0, freqrange = 0;
150 u32 pclk1clock = 12000000;
151 RCC_ClocksTypeDef RCC_Clocks;
153 /* Check the parameters */
154 assert(IS_I2C_MODE(I2C_InitStruct->I2C_Mode));
155 assert(IS_I2C_DUTY_CYCLE(I2C_InitStruct->I2C_DutyCycle));
156 assert(IS_I2C_OWN_ADDRESS1(I2C_InitStruct->I2C_OwnAddress1));
157 assert(IS_I2C_ACK_STATE(I2C_InitStruct->I2C_Ack));
158 assert(IS_I2C_ACKNOWLEDGE_ADDRESS(I2C_InitStruct->I2C_AcknowledgedAddress));
159 assert(IS_I2C_CLOCK_SPEED(I2C_InitStruct->I2C_ClockSpeed));
161 /*---------------------------- I2Cx CR2 Configuration ------------------------*/
162 /* Get the I2Cx CR2 value */
164 /* Clear frequency FREQ[5:0] bits */
165 tmpreg &= CR2_FREQ_Reset;
166 /* Get PCLK1Clock frequency value */
167 RCC_GetClocksFreq(&RCC_Clocks);
168 pclk1clock = RCC_Clocks.PCLK1_Frequency;
169 /* Set frequency bits depending on PCLK1Clock value */
170 freqrange = (u16)(pclk1clock / 1000000);
172 /* Write to I2Cx CR2 */
175 /*---------------------------- I2Cx CCR Configuration ------------------------*/
176 /* Disable I2Cx to configure TRISE */
177 I2C_Cmd(I2Cx, DISABLE);
179 /* Reset tmpreg value */
180 /* Clear F/S, DUTY and CCR[11:0] bits */
183 /* Configure speed in standard mode */
184 if (I2C_InitStruct->I2C_ClockSpeed <= 100000)
186 /* Standard mode speed calculate */
187 result = (u16)(pclk1clock / (I2C_InitStruct->I2C_ClockSpeed << 1));
188 /* Test if CCR value is under 0x4*/
191 /* Set minimum allowed value */
194 /* Set speed value for standard mode */
196 /* Set Maximum Rise Time: ((1000/(1000000000/pclk1clock))+1 */
197 I2Cx->TRISE = freqrange + 1;
199 /* Configure speed in fast mode */
200 else /*(I2C_InitStruct->I2C_ClockSpeed <= 400000)*/
202 if (I2C_InitStruct->I2C_DutyCycle == I2C_DutyCycle_2)
204 /* Fast mode speed calculate: Tlow/Thigh = 2 */
205 result = (u16)(pclk1clock / (I2C_InitStruct->I2C_ClockSpeed * 3));
207 else /*I2C_InitStruct->I2C_DutyCycle == I2C_DutyCycle_16_9*/
209 /* Fast mode speed calculate: Tlow/Thigh = 16/9 */
210 result = (u16)(pclk1clock / (I2C_InitStruct->I2C_ClockSpeed * 25));
212 result |= I2C_DutyCycle_16_9;
214 /* Test if CCR value is under 0x1*/
215 if ((result & CCR_CCR_Set) == 0)
217 /* Set minimum allowed value */
218 result |= (u16)0x0001;
220 /* Set speed value and set F/S bit for fast mode */
221 tmpreg |= result | CCR_FS_Set;
222 /* Set Maximum Rise Time: ((300/(1000000000/pclk1clock))+1 */
223 I2Cx->TRISE = (u16)(((freqrange * 300) / 1000) + 1);
225 /* Write to I2Cx CCR */
229 I2C_Cmd(I2Cx, ENABLE);
231 /*---------------------------- I2Cx CR1 Configuration ------------------------*/
232 /* Get the I2Cx CR1 value */
234 /* Clear ACK, SMBTYPE and SMBUS bits */
235 tmpreg &= CR1_CLEAR_Mask;
236 /* Configure I2Cx: mode and acknowledgement */
237 /* Set SMBTYPE and SMBUS bits according to I2C_Mode value */
238 /* Set ACK bit according to I2C_Ack value */
239 tmpreg |= (u16)((u32)I2C_InitStruct->I2C_Mode | I2C_InitStruct->I2C_Ack);
240 /* Write to I2Cx CR1 */
243 /*---------------------------- I2Cx OAR1 Configuration -----------------------*/
244 /* Set I2Cx Own Address1 and acknowledged address */
245 I2Cx->OAR1 = (I2C_InitStruct->I2C_AcknowledgedAddress | I2C_InitStruct->I2C_OwnAddress1);
248 /*******************************************************************************
249 * Function Name : I2C_StructInit
250 * Description : Fills each I2C_InitStruct member with its default value.
251 * Input : - I2C_InitStruct: pointer to a I2C_InitTypeDef structure
252 * which will be initialized.
255 *******************************************************************************/
256 void I2C_StructInit(I2C_InitTypeDef* I2C_InitStruct)
258 /*---------------- Reset I2C init structure parameters values ----------------*/
259 /* Initialize the I2C_Mode member */
260 I2C_InitStruct->I2C_Mode = I2C_Mode_I2C;
262 /* Initialize the I2C_DutyCycle member */
263 I2C_InitStruct->I2C_DutyCycle = I2C_DutyCycle_2;
265 /* Initialize the I2C_OwnAddress1 member */
266 I2C_InitStruct->I2C_OwnAddress1 = 0;
268 /* Initialize the I2C_Ack member */
269 I2C_InitStruct->I2C_Ack = I2C_Ack_Disable;
271 /* Initialize the I2C_AcknowledgedAddress member */
272 I2C_InitStruct->I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
274 /* initialize the I2C_ClockSpeed member */
275 I2C_InitStruct->I2C_ClockSpeed = 5000;
278 /*******************************************************************************
279 * Function Name : I2C_Cmd
280 * Description : Enables or disables the specified I2C peripheral.
281 * Input : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
282 * - NewState: new state of the I2Cx peripheral. This parameter
283 * can be: ENABLE or DISABLE.
286 *******************************************************************************/
287 void I2C_Cmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
289 /* Check the parameters */
290 assert(IS_FUNCTIONAL_STATE(NewState));
292 if (NewState != DISABLE)
294 /* Enable the selected I2C peripheral */
295 I2Cx->CR1 |= CR1_PE_Set;
299 /* Disable the selected I2C peripheral */
300 I2Cx->CR1 &= CR1_PE_Reset;
304 /*******************************************************************************
305 * Function Name : I2C_DMACmd
306 * Description : Enables or disables the specified I2C DMA requests.
307 * Input : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
308 * - NewState: new state of the I2C DMA transfer.
309 * This parameter can be: ENABLE or DISABLE.
312 *******************************************************************************/
313 void I2C_DMACmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
315 /* Check the parameters */
316 assert(IS_FUNCTIONAL_STATE(NewState));
318 if (NewState != DISABLE)
320 /* Enable the selected I2C DMA requests */
321 I2Cx->CR2 |= CR2_DMAEN_Set;
325 /* Disable the selected I2C DMA requests */
326 I2Cx->CR2 &= CR2_DMAEN_Reset;
330 /*******************************************************************************
331 * Function Name : I2C_DMALastTransferCmd
332 * Description : Specifies that the next DMA transfer is the last one.
333 * Input : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
334 * - NewState: new state of the I2C DMA last transfer.
335 * This parameter can be: ENABLE or DISABLE.
338 *******************************************************************************/
339 void I2C_DMALastTransferCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
341 /* Check the parameters */
342 assert(IS_FUNCTIONAL_STATE(NewState));
344 if (NewState != DISABLE)
346 /* Next DMA end of transfer is the last transfer */
347 I2Cx->CR2 |= CR2_LAST_Set;
351 /* Next DMA end of transfer is not the last transfer */
352 I2Cx->CR2 &= CR2_LAST_Reset;
356 /*******************************************************************************
357 * Function Name : I2C_GenerateSTART
358 * Description : Generates I2Cx communication START condition.
359 * Input : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
360 * - NewState: new state of the I2C START condition generation.
361 * This parameter can be: ENABLE or DISABLE.
364 *******************************************************************************/
365 void I2C_GenerateSTART(I2C_TypeDef* I2Cx, FunctionalState NewState)
367 /* Check the parameters */
368 assert(IS_FUNCTIONAL_STATE(NewState));
370 if (NewState != DISABLE)
372 /* Generate a START condition */
373 I2Cx->CR1 |= CR1_START_Set;
377 /* Disable the START condition generation */
378 I2Cx->CR1 &= CR1_START_Reset;
382 /*******************************************************************************
383 * Function Name : I2C_GenerateSTOP
384 * Description : Generates I2Cx communication STOP condition.
385 * Input : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
386 * - NewState: new state of the I2C STOP condition generation.
387 * This parameter can be: ENABLE or DISABLE.
390 *******************************************************************************/
391 void I2C_GenerateSTOP(I2C_TypeDef* I2Cx, FunctionalState NewState)
393 /* Check the parameters */
394 assert(IS_FUNCTIONAL_STATE(NewState));
396 if (NewState != DISABLE)
398 /* Generate a STOP condition */
399 I2Cx->CR1 |= CR1_STOP_Set;
403 /* Disable the STOP condition generation */
404 I2Cx->CR1 &= CR1_STOP_Reset;
408 /*******************************************************************************
409 * Function Name : I2C_AcknowledgeConfig
410 * Description : Enables or disables the specified I2C acknowledge feature.
411 * Input : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
412 * - NewState: new state of the I2C Acknowledgement.
413 * This parameter can be: ENABLE or DISABLE.
416 *******************************************************************************/
417 void I2C_AcknowledgeConfig(I2C_TypeDef* I2Cx, FunctionalState NewState)
419 /* Check the parameters */
420 assert(IS_FUNCTIONAL_STATE(NewState));
422 if (NewState != DISABLE)
424 /* Enable the acknowledgement */
425 I2Cx->CR1 |= CR1_ACK_Set;
429 /* Disable the acknowledgement */
430 I2Cx->CR1 &= CR1_ACK_Reset;
434 /*******************************************************************************
435 * Function Name : I2C_OwnAddress2Config
436 * Description : Configures the specified I2C own address2.
437 * Input : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
438 * - Address: specifies the 7bit I2C own address2.
441 *******************************************************************************/
442 void I2C_OwnAddress2Config(I2C_TypeDef* I2Cx, u8 Address)
446 /* Get the old register value */
448 /* Reset I2Cx Own address2 bit [7:1] */
449 tmpreg &= OAR2_ADD2_Reset;
450 /* Set I2Cx Own address2 */
451 tmpreg |= (u16)(Address & (u16)0x00FE);
452 /* Store the new register value */
456 /*******************************************************************************
457 * Function Name : I2C_DualAddressCmd
458 * Description : Enables or disables the specified I2C dual addressing mode.
459 * Input : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
460 * - NewState: new state of the I2C dual addressing mode.
461 * This parameter can be: ENABLE or DISABLE.
464 *******************************************************************************/
465 void I2C_DualAddressCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
467 /* Check the parameters */
468 assert(IS_FUNCTIONAL_STATE(NewState));
470 if (NewState != DISABLE)
472 /* Enable dual addressing mode */
473 I2Cx->OAR2 |= OAR2_ENDUAL_Set;
477 /* Disable dual addressing mode */
478 I2Cx->OAR2 &= OAR2_ENDUAL_Reset;
482 /*******************************************************************************
483 * Function Name : I2C_GeneralCallCmd
484 * Description : Enables or disables the specified I2C general call feature.
485 * Input : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
486 * - NewState: new state of the I2C General call.
487 * This parameter can be: ENABLE or DISABLE.
490 *******************************************************************************/
491 void I2C_GeneralCallCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
493 /* Check the parameters */
494 assert(IS_FUNCTIONAL_STATE(NewState));
496 if (NewState != DISABLE)
498 /* Enable generall call */
499 I2Cx->CR1 |= CR1_ENGC_Set;
503 /* Disable generall call */
504 I2Cx->CR1 &= CR1_ENGC_Reset;
508 /*******************************************************************************
509 * Function Name : I2C_ITConfig
510 * Description : Enables or disables the specified I2C interrupts.
511 * Input : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
512 * - I2C_IT: specifies the I2C interrupts sources to be enabled
514 * This parameter can be any combination of the following values:
515 * - I2C_IT_BUF: Buffer interrupt mask
516 * - I2C_IT_EVT: Event interrupt mask
517 * - I2C_IT_ERR: Error interrupt mask
518 * - NewState: new state of the specified I2C interrupts.
519 * This parameter can be: ENABLE or DISABLE.
522 *******************************************************************************/
523 void I2C_ITConfig(I2C_TypeDef* I2Cx, u16 I2C_IT, FunctionalState NewState)
525 /* Check the parameters */
526 assert(IS_FUNCTIONAL_STATE(NewState));
527 assert(IS_I2C_CONFIG_IT(I2C_IT));
529 if (NewState != DISABLE)
531 /* Enable the selected I2C interrupts */
536 /* Disable the selected I2C interrupts */
537 I2Cx->CR2 &= (u16)~I2C_IT;
541 /*******************************************************************************
542 * Function Name : I2C_SendData
543 * Description : Sends a data byte through the I2Cx peripheral.
544 * Input : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
545 * - Data: Byte to be transmitted..
548 *******************************************************************************/
549 void I2C_SendData(I2C_TypeDef* I2Cx, u8 Data)
551 /* Write in the DR register the data to be sent */
555 /*******************************************************************************
556 * Function Name : I2C_ReceiveData
557 * Description : Returns the most recent received data by the I2Cx peripheral.
558 * Input : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
560 * Return : The value of the received data.
561 *******************************************************************************/
562 u8 I2C_ReceiveData(I2C_TypeDef* I2Cx)
564 /* Return the data in the DR register */
568 /*******************************************************************************
569 * Function Name : I2C_Send7bitAddress
570 * Description : Transmits the address byte to select the slave device.
571 * Input : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
572 * - Address: specifies the slave address which will be transmitted
573 * - Direction: specifies whether the I2C device will be a
574 * Transmitter or a Receiver.
575 * This parameter can be one of the following values
576 * - I2C_Direction_Transmitter: Transmitter mode
577 * - I2C_Direction_Receiver: Receiver mode
580 *******************************************************************************/
581 void I2C_Send7bitAddress(I2C_TypeDef* I2Cx, u8 Address, u8 I2C_Direction)
583 /* Check the parameters */
584 assert(IS_I2C_DIRECTION(I2C_Direction));
586 /* Test on the direction to set/reset the read/write bit */
587 if (I2C_Direction != I2C_Direction_Transmitter)
589 /* Set the address ADD0 bit0 for read */
590 Address |= OAR1_ADD0_Set;
594 /* Reset the address bit0 for write */
595 Address &= OAR1_ADD0_Reset;
597 /* Send the address */
601 /*******************************************************************************
602 * Function Name : I2C_ReadRegister
603 * Description : Reads the specified I2C register and returns its value.
604 * Input1 : - I2C_Register: specifies the register to read.
605 * This parameter can be one of the following values:
606 * - I2C_Register_CR1: CR1 register.
607 * - I2C_Register_CR2: CR2 register.
608 * - I2C_Register_OAR1: OAR1 register.
609 * - I2C_Register_OAR2: OAR2 register.
610 * - I2C_Register_DR: DR register.
611 * - I2C_Register_SR1: SR1 register.
612 * - I2C_Register_SR2: SR2 register.
613 * - I2C_Register_CCR: CCR register.
614 * - I2C_Register_TRISE: TRISE register.
616 * Return : The value of the read register.
617 *******************************************************************************/
618 u16 I2C_ReadRegister(I2C_TypeDef* I2Cx, u8 I2C_Register)
620 /* Check the parameters */
621 assert(IS_I2C_REGISTER(I2C_Register));
623 /* Return the selected register value */
624 return (*(u16 *)(*((u32 *)&I2Cx) + I2C_Register));
627 /*******************************************************************************
628 * Function Name : I2C_SoftwareResetCmd
629 * Description : Enables or disables the specified I2C software reset.
630 * Input : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
631 * - NewState: new state of the I2C software reset.
632 * This parameter can be: ENABLE or DISABLE.
635 *******************************************************************************/
636 void I2C_SoftwareResetCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
638 /* Check the parameters */
639 assert(IS_FUNCTIONAL_STATE(NewState));
641 if (NewState != DISABLE)
643 /* Peripheral under reset */
644 I2Cx->CR1 |= CR1_SWRST_Set;
648 /* Peripheral not under reset */
649 I2Cx->CR1 &= CR1_SWRST_Reset;
653 /*******************************************************************************
654 * Function Name : I2C_SMBusAlertConfig
655 * Description : Drives the SMBusAlert pin high or low for the specified I2C.
656 * Input : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
657 * - I2C_SMBusAlert: specifies SMBAlert pin level.
658 * This parameter can be one of the following values:
659 * - I2C_SMBusAlert_Low: SMBAlert pin driven low
660 * - I2C_SMBusAlert_High: SMBAlert pin driven high
663 *******************************************************************************/
664 void I2C_SMBusAlertConfig(I2C_TypeDef* I2Cx, u16 I2C_SMBusAlert)
666 /* Check the parameters */
667 assert(IS_I2C_SMBUS_ALERT(I2C_SMBusAlert));
669 if (I2C_SMBusAlert == I2C_SMBusAlert_Low)
671 /* Drive the SMBusAlert pin Low */
672 I2Cx->CR1 |= I2C_SMBusAlert_Low;
676 /* Drive the SMBusAlert pin High */
677 I2Cx->CR1 &= I2C_SMBusAlert_High;
681 /*******************************************************************************
682 * Function Name : I2C_TransmitPEC
683 * Description : Enables the specified I2C PEC transfer.
684 * Input : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
687 *******************************************************************************/
688 void I2C_TransmitPEC(I2C_TypeDef* I2Cx)
690 /* Enable the selected I2C PEC transmission */
691 I2Cx->CR1 |= CR1_PEC_Set;
694 /*******************************************************************************
695 * Function Name : I2C_PECPositionConfig
696 * Description : Selects the specified I2C PEC position.
697 * Input : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
698 * - I2C_PECPosition: specifies the PEC position.
699 * This parameter can be one of the following values:
700 * - I2C_PECPosition_Next: PEC bit indicates that current
702 * - I2C_PECPosition_Current: PEC bit indicates that the
706 *******************************************************************************/
707 void I2C_PECPositionConfig(I2C_TypeDef* I2Cx, u16 I2C_PECPosition)
709 /* Check the parameters */
710 assert(IS_I2C_PEC_POSITION(I2C_PECPosition));
712 if (I2C_PECPosition == I2C_PECPosition_Next)
714 /* PEC indicates that the next byte in shift register is PEC */
715 I2Cx->CR1 |= I2C_PECPosition_Next;
719 /* PEC indicates that the current byte in shift register is PEC */
720 I2Cx->CR1 &= I2C_PECPosition_Current;
724 /*******************************************************************************
725 * Function Name : I2C_CalculatePEC
726 * Description : Enables or disables the PEC value calculation of the
728 * Input : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
729 * - NewState: new state of the I2Cx PEC value calculation.
730 * This parameter can be: ENABLE or DISABLE.
733 *******************************************************************************/
734 void I2C_CalculatePEC(I2C_TypeDef* I2Cx, FunctionalState NewState)
736 /* Check the parameters */
737 assert(IS_FUNCTIONAL_STATE(NewState));
739 if (NewState != DISABLE)
741 /* Enable the selected I2C PEC calculation */
742 I2Cx->CR1 |= CR1_ENPEC_Set;
746 /* Disable the selected I2C PEC calculation */
747 I2Cx->CR1 &= CR1_ENPEC_Reset;
751 /*******************************************************************************
752 * Function Name : I2C_GetPEC
753 * Description : Returns the PEC value for the specified I2C.
754 * Input : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
756 * Return : The PEC value.
757 *******************************************************************************/
758 u8 I2C_GetPEC(I2C_TypeDef* I2Cx)
762 /* Get the PEC value */
763 pec = (I2Cx->SR2) >> 8;
764 /* Return the selected I2C PEC register value */
768 /*******************************************************************************
769 * Function Name : I2C_ARPCmd
770 * Description : Enables or disables the specified I2C ARP.
771 * Input : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
772 * - NewState: new state of the I2Cx ARP.
773 * This parameter can be: ENABLE or DISABLE.
776 *******************************************************************************/
777 void I2C_ARPCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
779 /* Check the parameters */
780 assert(IS_FUNCTIONAL_STATE(NewState));
782 if (NewState != DISABLE)
784 /* Enable the selected I2C ARP */
785 I2Cx->CR1 |= CR1_ENARP_Set;
789 /* Disable the selected I2C ARP */
790 I2Cx->CR1 &= CR1_ENARP_Reset;
794 /*******************************************************************************
795 * Function Name : I2C_StretchClockCmd
796 * Description : Enables or disables the specified I2C Clock stretching.
797 * Input : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
798 * - NewState: new state of the I2Cx Clock stretching.
799 * This parameter can be: ENABLE or DISABLE.
802 *******************************************************************************/
803 void I2C_StretchClockCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
805 /* Check the parameters */
806 assert(IS_FUNCTIONAL_STATE(NewState));
808 if (NewState == DISABLE)
810 /* Enable the selected I2C Clock stretching */
811 I2Cx->CR1 |= CR1_NOSTRETCH_Set;
815 /* Disable the selected I2C Clock stretching */
816 I2Cx->CR1 &= CR1_NOSTRETCH_Reset;
820 /*******************************************************************************
821 * Function Name : I2C_FastModeDutyCycleConfig
822 * Description : Selects the specified I2C fast mode duty cycle.
823 * Input : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
824 * - I2C_DutyCycle: specifies the fast mode duty cycle.
825 * This parameter can be one of the following values:
826 * - I2C_DutyCycle_2: I2C fast mode Tlow/Thigh = 2
827 * - I2C_DutyCycle_16_9: I2C fast mode Tlow/Thigh = 16/9
830 *******************************************************************************/
831 void I2C_FastModeDutyCycleConfig(I2C_TypeDef* I2Cx, u16 I2C_DutyCycle)
833 /* Check the parameters */
834 assert(IS_I2C_DUTY_CYCLE(I2C_DutyCycle));
836 if (I2C_DutyCycle != I2C_DutyCycle_16_9)
838 /* I2C fast mode Tlow/Thigh=2 */
839 I2Cx->CCR &= I2C_DutyCycle_2;
843 /* I2C fast mode Tlow/Thigh=16/9 */
844 I2Cx->CCR |= I2C_DutyCycle_16_9;
848 /*******************************************************************************
849 * Function Name : I2C_GetLastEvent
850 * Description : Returns the Last I2C Event.
851 * Input : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
853 * Return : The last event
854 *******************************************************************************/
855 u32 I2C_GetLastEvent(I2C_TypeDef* I2Cx)
858 u32 Flag1 = 0, Flag2 = 0;
864 /* Get the last event value from I2C status register */
865 LastEvent = (Flag1 | Flag2) & I2C_FLAG_Mask;
871 /*******************************************************************************
872 * Function Name : I2C_CheckEvent
873 * Description : Checks whether the Last I2C Event is equal to the one passed
875 * Input : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
876 * - I2C_EVENT: specifies the event to be checked.
877 * This parameter can be one of the following values:
878 * - I2C_EVENT_SLAVE_ADDRESS_MATCHED : EV1
879 * - I2C_EVENT_SLAVE_BYTE_RECEIVED : EV2
880 * - I2C_EVENT_SLAVE_BYTE_TRANSMITTED : EV3
881 * - I2C_EVENT_SLAVE_ACK_FAILURE : EV3-1
882 * - I2C_EVENT_MASTER_MODE_SELECT : EV5
883 * - I2C_EVENT_MASTER_MODE_SELECTED : EV6
884 * - I2C_EVENT_MASTER_BYTE_RECEIVED : EV7
885 * - I2C_EVENT_MASTER_BYTE_TRANSMITTED : EV8
886 * - I2C_EVENT_MASTER_MODE_ADDRESS10 : EV9
887 * - I2C_EVENT_SLAVE_STOP_DETECTED : EV4
889 * Return : An ErrorStatus enumuration value:
890 * - SUCCESS: Last event is equal to the I2C_Event
891 * - ERROR: Last event is different from the I2C_Event
892 *******************************************************************************/
893 ErrorStatus I2C_CheckEvent(I2C_TypeDef* I2Cx, u32 I2C_EVENT)
896 u32 Flag1 = 0, Flag2 = 0;
897 ErrorStatus status = ERROR;
899 /* Check the parameters */
900 assert(IS_I2C_EVENT(I2C_EVENT));
906 /* Get the last event value from I2C status register */
907 LastEvent = (Flag1 | Flag2) & I2C_FLAG_Mask;
909 /* Check whether the last event is equal to I2C_EVENT */
910 if (LastEvent == I2C_EVENT )
912 /* SUCCESS: last event is equal to I2C_EVENT */
917 /* ERROR: last event is different from I2C_EVENT */
925 /*******************************************************************************
926 * Function Name : I2C_GetFlagStatus
927 * Description : Checks whether the specified I2C flag is set or not.
928 * Input : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
929 * - I2C_FLAG: specifies the flag to check.
930 * This parameter can be one of the following values:
931 * - I2C_FLAG_DUALF: Dual flag (Slave mode)
932 * - I2C_FLAG_SMBHOST: SMBus host header (Slave mode)
933 * - I2C_FLAG_SMBDEFAULT: SMBus default header (Slave mode)
934 * - I2C_FLAG_GENCALL: General call header flag (Slave mode)
935 * - I2C_FLAG_TRA: Transmitter/Receiver flag
936 * - I2C_FLAG_BUSY: Bus busy flag
937 * - I2C_FLAG_MSL: Master/Slave flag
938 * - I2C_FLAG_SMBALERT: SMBus Alert flag
939 * - I2C_FLAG_TIMEOUT: Timeout or Tlow error flag
940 * - I2C_FLAG_PECERR: PEC error in reception flag
941 * - I2C_FLAG_OVR: Overrun/Underrun flag (Slave mode)
942 * - I2C_FLAG_AF: Acknowledge failure flag
943 * - I2C_FLAG_ARLO: Arbitration lost flag (Master mode)
944 * - I2C_FLAG_BERR: Bus error flag
945 * - I2C_FLAG_TXE: Data register empty flag (Transmitter)
946 * - I2C_FLAG_RXNE: Data register not empty (Receiver) flag
947 * - I2C_FLAG_STOPF: Stop detection flag (Slave mode)
948 * - I2C_FLAG_ADD10: 10-bit header sent flag (Master mode)
949 * - I2C_FLAG_BTF: Byte transfer finished flag
950 * - I2C_FLAG_ADDR: Address sent flag (Master mode)
\93ADSL
\94
951 * Address matched flag (Slave mode)
\94ENDAD
\94
952 * - I2C_FLAG_SB: Start bit flag (Master mode)
954 * Return : The new state of I2C_FLAG (SET or RESET).
955 *******************************************************************************/
956 FlagStatus I2C_GetFlagStatus(I2C_TypeDef* I2Cx, u32 I2C_FLAG)
958 FlagStatus bitstatus = RESET;
960 u32 Flag1 = 0, Flag2 = 0;
962 /* Check the parameters */
963 assert(IS_I2C_GET_FLAG(I2C_FLAG));
965 /* Read the I2Cx status register */
968 Flag2 = (Flag2 & I2C_FLAG_Mask) << 16;
970 /* Get the I2C status value */
971 i2cstatus = Flag1 | Flag2;
973 /* Get bit[27:0] of the flag */
974 I2C_FLAG &= I2C_FLAG_Mask;
976 /* Check the status of the specified I2C flag */
977 if ((i2cstatus & I2C_FLAG) != (u32)RESET)
979 /* I2C_FLAG is set */
984 /* I2C_FLAG is reset */
987 /* Return the I2C_FLAG status */
991 /*******************************************************************************
992 * Function Name : I2C_ClearFlag
993 * Description : Clears the I2Cx's pending flags.
994 * Input : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
995 * - I2C_FLAG: specifies the flag to clear.
996 * This parameter can be one of the following values:
997 * - I2C_FLAG_SMBALERT: SMBus Alert flag
998 * - I2C_FLAG_TIMEOUT: Timeout or Tlow error flag
999 * - I2C_FLAG_PECERR: PEC error in reception flag
1000 * - I2C_FLAG_OVR: Overrun/Underrun flag (Slave mode)
1001 * - I2C_FLAG_AF: Acknowledge failure flag
1002 * - I2C_FLAG_ARLO: Arbitration lost flag (Master mode)
1003 * - I2C_FLAG_BERR: Bus error flag
1004 * - I2C_FLAG_STOPF: Stop detection flag (Slave mode)
1005 * - I2C_FLAG_ADD10: 10-bit header sent flag (Master mode)
1006 * - I2C_FLAG_BTF: Byte transfer finished flag
1007 * - I2C_FLAG_ADDR: Address sent flag (Master mode)
\93ADSL
\94
1008 * Address matched flag (Slave mode)
\94ENDAD
\94
1009 * - I2C_FLAG_SB: Start bit flag (Master mode)
1012 *******************************************************************************/
1013 void I2C_ClearFlag(I2C_TypeDef* I2Cx, u32 I2C_FLAG)
1018 /* Check the parameters */
1019 assert(IS_I2C_CLEAR_FLAG(I2C_FLAG));
1021 /* Get the I2C flag position */
1022 flagpos = I2C_FLAG & I2C_FLAG_Mask;
1024 /* Get the I2C flag index */
1025 flagindex = I2C_FLAG >> 28;
1027 /* Clear the flag by writing 0 */
1030 /* Clear the selected I2C flag */
1031 I2Cx->SR1 &= ~flagpos;
1033 /* Flags that need a read of the SR1 register to be cleared */
1034 else if (flagindex == 2)
1036 /* Read the SR1 register */
1039 /* Flags that need a read of SR1 and a write on CR2 registers to be cleared */
1040 else if (flagindex == 6)
1042 /* Read the SR1 register */
1045 /* Write on the CR1 register */
1046 I2Cx->CR1 |= CR1_PE_Set;
1048 /* Flags that need a read of SR1 and a write on CR2 registers to be cleared */
1049 else /*flagindex == 0xA*/
1051 /* Read the SR1 register */
1054 /* Read the SR2 register */
1059 /*******************************************************************************
1060 * Function Name : I2C_GetITStatus
1061 * Description : Checks whether the specified I2C interrupt has occurred or not.
1062 * Input : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
1063 * - I2C_IT: specifies the interrupt source to check.
1064 * This parameter can be one of the following values:
1065 * - I2C_IT_SMBALERT: SMBus Alert flag
1066 * - I2C_IT_TIMEOUT: Timeout or Tlow error flag
1067 * - I2C_IT_PECERR: PEC error in reception flag
1068 * - I2C_IT_OVR: Overrun/Underrun flag (Slave mode)
1069 * - I2C_IT_AF: Acknowledge failure flag
1070 * - I2C_IT_ARLO: Arbitration lost flag (Master mode)
1071 * - I2C_IT_BERR: Bus error flag
1072 * - I2C_IT_TXE: Data register empty flag (Transmitter)
1073 * - I2C_IT_RXNE: Data register not empty (Receiver) flag
1074 * - I2C_IT_STOPF: Stop detection flag (Slave mode)
1075 * - I2C_IT_ADD10: 10-bit header sent flag (Master mode)
1076 * - I2C_IT_BTF: Byte transfer finished flag
1077 * - I2C_IT_ADDR: Address sent flag (Master mode)
\93ADSL
\94
1078 * Address matched flag (Slave mode)
\94ENDAD
\94
1079 * - I2C_IT_SB: Start bit flag (Master mode)
1081 * Return : The new state of I2C_IT (SET or RESET).
1082 *******************************************************************************/
1083 ITStatus I2C_GetITStatus(I2C_TypeDef* I2Cx, u32 I2C_IT)
1085 ITStatus bitstatus = RESET;
1087 u32 Flag1 = 0, Flag2 = 0;
1089 /* Check the parameters */
1090 assert(IS_I2C_GET_IT(I2C_IT));
1092 /* Read the I2Cx status register */
1095 Flag2 = (Flag2 & I2C_FLAG_Mask) << 16;
1097 /* Get the I2C status value */
1098 i2cstatus = Flag1 | Flag2;
1100 /* Get bit[27:0] of the flag */
1101 I2C_IT &= I2C_FLAG_Mask;
1103 /* Check the status of the specified I2C flag */
1104 if ((i2cstatus & I2C_IT) != (u32)RESET)
1111 /* I2C_IT is reset */
1114 /* Return the I2C_IT status */
1118 /*******************************************************************************
1119 * Function Name : I2C_ClearITPendingBit
1120 * Description : Clears the I2Cx
\92s interrupt pending bits.
1121 * Input : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
1122 * - I2C_IT: specifies the interrupt pending to clear.
1123 * This parameter can be one of the following values:
1124 * - I2C_IT_SMBALERT: SMBus Alert flag
1125 * - I2C_IT_TIMEOUT: Timeout or Tlow error flag
1126 * - I2C_IT_PECERR: PEC error in reception flag
1127 * - I2C_IT_OVR: Overrun/Underrun flag (Slave mode)
1128 * - I2C_IT_AF: Acknowledge failure flag
1129 * - I2C_IT_ARLO: Arbitration lost flag (Master mode)
1130 * - I2C_IT_BERR: Bus error flag
1131 * - I2C_IT_STOPF: Stop detection flag (Slave mode)
1132 * - I2C_IT_ADD10: 10-bit header sent flag (Master mode)
1133 * - I2C_IT_BTF: Byte transfer finished flag
1134 * - I2C_IT_ADDR: Address sent flag (Master mode)
\93ADSL
\94
1135 * Address matched flag (Slave mode)
\94ENDAD
\94
1136 * - I2C_IT_SB: Start bit flag (Master mode)
1139 *******************************************************************************/
1140 void I2C_ClearITPendingBit(I2C_TypeDef* I2Cx, u32 I2C_IT)
1145 /* Check the parameters */
1146 assert(IS_I2C_CLEAR_IT(I2C_IT));
1148 /* Get the I2C flag position */
1149 flagpos = I2C_IT & I2C_FLAG_Mask;
1151 /* Get the I2C flag index */
1152 flagindex = I2C_IT >> 28;
1154 /* Clear the flag by writing 0 */
1157 /* Clear the selected I2C flag */
1158 I2Cx->SR1 &= ~flagpos;
1160 /* Flags that need a read of the SR1 register to be cleared */
1161 else if (flagindex == 2)
1163 /* Read the SR1 register */
1166 /* Flags that need a read of SR1 and a write on CR2 registers to be cleared */
1167 else if (flagindex == 6)
1169 /* Read the SR1 register */
1172 /* Write on the CR1 register */
1173 I2Cx->CR1 |= CR1_PE_Set;
1175 /* Flags that need a read of SR1 and a write on CR2 registers to be cleared */
1176 else /*flagindex == 0xA*/
1178 /* Read the SR1 register */
1181 /* Read the SR2 register */
1186 /******************* (C) COPYRIGHT 2007 STMicroelectronics *****END OF FILE****/