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