STM32F10x Standard Peripherals Library  3.5.0
/opt/STM32F10x_StdPeriph_Lib_V3.5.0/Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_i2c.c
Go to the documentation of this file.
00001 
00022 /* Includes ------------------------------------------------------------------*/
00023 #include "stm32f10x_i2c.h"
00024 #include "stm32f10x_rcc.h"
00025 
00026 
00048 /* I2C SPE mask */
00049 #define CR1_PE_Set              ((uint16_t)0x0001)
00050 #define CR1_PE_Reset            ((uint16_t)0xFFFE)
00051 
00052 /* I2C START mask */
00053 #define CR1_START_Set           ((uint16_t)0x0100)
00054 #define CR1_START_Reset         ((uint16_t)0xFEFF)
00055 
00056 /* I2C STOP mask */
00057 #define CR1_STOP_Set            ((uint16_t)0x0200)
00058 #define CR1_STOP_Reset          ((uint16_t)0xFDFF)
00059 
00060 /* I2C ACK mask */
00061 #define CR1_ACK_Set             ((uint16_t)0x0400)
00062 #define CR1_ACK_Reset           ((uint16_t)0xFBFF)
00063 
00064 /* I2C ENGC mask */
00065 #define CR1_ENGC_Set            ((uint16_t)0x0040)
00066 #define CR1_ENGC_Reset          ((uint16_t)0xFFBF)
00067 
00068 /* I2C SWRST mask */
00069 #define CR1_SWRST_Set           ((uint16_t)0x8000)
00070 #define CR1_SWRST_Reset         ((uint16_t)0x7FFF)
00071 
00072 /* I2C PEC mask */
00073 #define CR1_PEC_Set             ((uint16_t)0x1000)
00074 #define CR1_PEC_Reset           ((uint16_t)0xEFFF)
00075 
00076 /* I2C ENPEC mask */
00077 #define CR1_ENPEC_Set           ((uint16_t)0x0020)
00078 #define CR1_ENPEC_Reset         ((uint16_t)0xFFDF)
00079 
00080 /* I2C ENARP mask */
00081 #define CR1_ENARP_Set           ((uint16_t)0x0010)
00082 #define CR1_ENARP_Reset         ((uint16_t)0xFFEF)
00083 
00084 /* I2C NOSTRETCH mask */
00085 #define CR1_NOSTRETCH_Set       ((uint16_t)0x0080)
00086 #define CR1_NOSTRETCH_Reset     ((uint16_t)0xFF7F)
00087 
00088 /* I2C registers Masks */
00089 #define CR1_CLEAR_Mask          ((uint16_t)0xFBF5)
00090 
00091 /* I2C DMAEN mask */
00092 #define CR2_DMAEN_Set           ((uint16_t)0x0800)
00093 #define CR2_DMAEN_Reset         ((uint16_t)0xF7FF)
00094 
00095 /* I2C LAST mask */
00096 #define CR2_LAST_Set            ((uint16_t)0x1000)
00097 #define CR2_LAST_Reset          ((uint16_t)0xEFFF)
00098 
00099 /* I2C FREQ mask */
00100 #define CR2_FREQ_Reset          ((uint16_t)0xFFC0)
00101 
00102 /* I2C ADD0 mask */
00103 #define OAR1_ADD0_Set           ((uint16_t)0x0001)
00104 #define OAR1_ADD0_Reset         ((uint16_t)0xFFFE)
00105 
00106 /* I2C ENDUAL mask */
00107 #define OAR2_ENDUAL_Set         ((uint16_t)0x0001)
00108 #define OAR2_ENDUAL_Reset       ((uint16_t)0xFFFE)
00109 
00110 /* I2C ADD2 mask */
00111 #define OAR2_ADD2_Reset         ((uint16_t)0xFF01)
00112 
00113 /* I2C F/S mask */
00114 #define CCR_FS_Set              ((uint16_t)0x8000)
00115 
00116 /* I2C CCR mask */
00117 #define CCR_CCR_Set             ((uint16_t)0x0FFF)
00118 
00119 /* I2C FLAG mask */
00120 #define FLAG_Mask               ((uint32_t)0x00FFFFFF)
00121 
00122 /* I2C Interrupt Enable mask */
00123 #define ITEN_Mask               ((uint32_t)0x07000000)
00124 
00162 void I2C_DeInit(I2C_TypeDef* I2Cx)
00163 {
00164   /* Check the parameters */
00165   assert_param(IS_I2C_ALL_PERIPH(I2Cx));
00166 
00167   if (I2Cx == I2C1)
00168   {
00169     /* Enable I2C1 reset state */
00170     RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, ENABLE);
00171     /* Release I2C1 from reset state */
00172     RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, DISABLE);
00173   }
00174   else
00175   {
00176     /* Enable I2C2 reset state */
00177     RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, ENABLE);
00178     /* Release I2C2 from reset state */
00179     RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, DISABLE);
00180   }
00181 }
00182 
00191 void I2C_Init(I2C_TypeDef* I2Cx, I2C_InitTypeDef* I2C_InitStruct)
00192 {
00193   uint16_t tmpreg = 0, freqrange = 0;
00194   uint16_t result = 0x04;
00195   uint32_t pclk1 = 8000000;
00196   RCC_ClocksTypeDef  rcc_clocks;
00197   /* Check the parameters */
00198   assert_param(IS_I2C_ALL_PERIPH(I2Cx));
00199   assert_param(IS_I2C_CLOCK_SPEED(I2C_InitStruct->I2C_ClockSpeed));
00200   assert_param(IS_I2C_MODE(I2C_InitStruct->I2C_Mode));
00201   assert_param(IS_I2C_DUTY_CYCLE(I2C_InitStruct->I2C_DutyCycle));
00202   assert_param(IS_I2C_OWN_ADDRESS1(I2C_InitStruct->I2C_OwnAddress1));
00203   assert_param(IS_I2C_ACK_STATE(I2C_InitStruct->I2C_Ack));
00204   assert_param(IS_I2C_ACKNOWLEDGE_ADDRESS(I2C_InitStruct->I2C_AcknowledgedAddress));
00205 
00206 /*---------------------------- I2Cx CR2 Configuration ------------------------*/
00207   /* Get the I2Cx CR2 value */
00208   tmpreg = I2Cx->CR2;
00209   /* Clear frequency FREQ[5:0] bits */
00210   tmpreg &= CR2_FREQ_Reset;
00211   /* Get pclk1 frequency value */
00212   RCC_GetClocksFreq(&rcc_clocks);
00213   pclk1 = rcc_clocks.PCLK1_Frequency;
00214   /* Set frequency bits depending on pclk1 value */
00215   freqrange = (uint16_t)(pclk1 / 1000000);
00216   tmpreg |= freqrange;
00217   /* Write to I2Cx CR2 */
00218   I2Cx->CR2 = tmpreg;
00219 
00220 /*---------------------------- I2Cx CCR Configuration ------------------------*/
00221   /* Disable the selected I2C peripheral to configure TRISE */
00222   I2Cx->CR1 &= CR1_PE_Reset;
00223   /* Reset tmpreg value */
00224   /* Clear F/S, DUTY and CCR[11:0] bits */
00225   tmpreg = 0;
00226 
00227   /* Configure speed in standard mode */
00228   if (I2C_InitStruct->I2C_ClockSpeed <= 100000)
00229   {
00230     /* Standard mode speed calculate */
00231     result = (uint16_t)(pclk1 / (I2C_InitStruct->I2C_ClockSpeed << 1));
00232     /* Test if CCR value is under 0x4*/
00233     if (result < 0x04)
00234     {
00235       /* Set minimum allowed value */
00236       result = 0x04;  
00237     }
00238     /* Set speed value for standard mode */
00239     tmpreg |= result;     
00240     /* Set Maximum Rise Time for standard mode */
00241     I2Cx->TRISE = freqrange + 1; 
00242   }
00243   /* Configure speed in fast mode */
00244   else /*(I2C_InitStruct->I2C_ClockSpeed <= 400000)*/
00245   {
00246     if (I2C_InitStruct->I2C_DutyCycle == I2C_DutyCycle_2)
00247     {
00248       /* Fast mode speed calculate: Tlow/Thigh = 2 */
00249       result = (uint16_t)(pclk1 / (I2C_InitStruct->I2C_ClockSpeed * 3));
00250     }
00251     else /*I2C_InitStruct->I2C_DutyCycle == I2C_DutyCycle_16_9*/
00252     {
00253       /* Fast mode speed calculate: Tlow/Thigh = 16/9 */
00254       result = (uint16_t)(pclk1 / (I2C_InitStruct->I2C_ClockSpeed * 25));
00255       /* Set DUTY bit */
00256       result |= I2C_DutyCycle_16_9;
00257     }
00258 
00259     /* Test if CCR value is under 0x1*/
00260     if ((result & CCR_CCR_Set) == 0)
00261     {
00262       /* Set minimum allowed value */
00263       result |= (uint16_t)0x0001;  
00264     }
00265     /* Set speed value and set F/S bit for fast mode */
00266     tmpreg |= (uint16_t)(result | CCR_FS_Set);
00267     /* Set Maximum Rise Time for fast mode */
00268     I2Cx->TRISE = (uint16_t)(((freqrange * (uint16_t)300) / (uint16_t)1000) + (uint16_t)1);  
00269   }
00270 
00271   /* Write to I2Cx CCR */
00272   I2Cx->CCR = tmpreg;
00273   /* Enable the selected I2C peripheral */
00274   I2Cx->CR1 |= CR1_PE_Set;
00275 
00276 /*---------------------------- I2Cx CR1 Configuration ------------------------*/
00277   /* Get the I2Cx CR1 value */
00278   tmpreg = I2Cx->CR1;
00279   /* Clear ACK, SMBTYPE and  SMBUS bits */
00280   tmpreg &= CR1_CLEAR_Mask;
00281   /* Configure I2Cx: mode and acknowledgement */
00282   /* Set SMBTYPE and SMBUS bits according to I2C_Mode value */
00283   /* Set ACK bit according to I2C_Ack value */
00284   tmpreg |= (uint16_t)((uint32_t)I2C_InitStruct->I2C_Mode | I2C_InitStruct->I2C_Ack);
00285   /* Write to I2Cx CR1 */
00286   I2Cx->CR1 = tmpreg;
00287 
00288 /*---------------------------- I2Cx OAR1 Configuration -----------------------*/
00289   /* Set I2Cx Own Address1 and acknowledged address */
00290   I2Cx->OAR1 = (I2C_InitStruct->I2C_AcknowledgedAddress | I2C_InitStruct->I2C_OwnAddress1);
00291 }
00292 
00298 void I2C_StructInit(I2C_InitTypeDef* I2C_InitStruct)
00299 {
00300 /*---------------- Reset I2C init structure parameters values ----------------*/
00301   /* initialize the I2C_ClockSpeed member */
00302   I2C_InitStruct->I2C_ClockSpeed = 5000;
00303   /* Initialize the I2C_Mode member */
00304   I2C_InitStruct->I2C_Mode = I2C_Mode_I2C;
00305   /* Initialize the I2C_DutyCycle member */
00306   I2C_InitStruct->I2C_DutyCycle = I2C_DutyCycle_2;
00307   /* Initialize the I2C_OwnAddress1 member */
00308   I2C_InitStruct->I2C_OwnAddress1 = 0;
00309   /* Initialize the I2C_Ack member */
00310   I2C_InitStruct->I2C_Ack = I2C_Ack_Disable;
00311   /* Initialize the I2C_AcknowledgedAddress member */
00312   I2C_InitStruct->I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
00313 }
00314 
00322 void I2C_Cmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
00323 {
00324   /* Check the parameters */
00325   assert_param(IS_I2C_ALL_PERIPH(I2Cx));
00326   assert_param(IS_FUNCTIONAL_STATE(NewState));
00327   if (NewState != DISABLE)
00328   {
00329     /* Enable the selected I2C peripheral */
00330     I2Cx->CR1 |= CR1_PE_Set;
00331   }
00332   else
00333   {
00334     /* Disable the selected I2C peripheral */
00335     I2Cx->CR1 &= CR1_PE_Reset;
00336   }
00337 }
00338 
00346 void I2C_DMACmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
00347 {
00348   /* Check the parameters */
00349   assert_param(IS_I2C_ALL_PERIPH(I2Cx));
00350   assert_param(IS_FUNCTIONAL_STATE(NewState));
00351   if (NewState != DISABLE)
00352   {
00353     /* Enable the selected I2C DMA requests */
00354     I2Cx->CR2 |= CR2_DMAEN_Set;
00355   }
00356   else
00357   {
00358     /* Disable the selected I2C DMA requests */
00359     I2Cx->CR2 &= CR2_DMAEN_Reset;
00360   }
00361 }
00362 
00370 void I2C_DMALastTransferCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
00371 {
00372   /* Check the parameters */
00373   assert_param(IS_I2C_ALL_PERIPH(I2Cx));
00374   assert_param(IS_FUNCTIONAL_STATE(NewState));
00375   if (NewState != DISABLE)
00376   {
00377     /* Next DMA transfer is the last transfer */
00378     I2Cx->CR2 |= CR2_LAST_Set;
00379   }
00380   else
00381   {
00382     /* Next DMA transfer is not the last transfer */
00383     I2Cx->CR2 &= CR2_LAST_Reset;
00384   }
00385 }
00386 
00394 void I2C_GenerateSTART(I2C_TypeDef* I2Cx, FunctionalState NewState)
00395 {
00396   /* Check the parameters */
00397   assert_param(IS_I2C_ALL_PERIPH(I2Cx));
00398   assert_param(IS_FUNCTIONAL_STATE(NewState));
00399   if (NewState != DISABLE)
00400   {
00401     /* Generate a START condition */
00402     I2Cx->CR1 |= CR1_START_Set;
00403   }
00404   else
00405   {
00406     /* Disable the START condition generation */
00407     I2Cx->CR1 &= CR1_START_Reset;
00408   }
00409 }
00410 
00418 void I2C_GenerateSTOP(I2C_TypeDef* I2Cx, FunctionalState NewState)
00419 {
00420   /* Check the parameters */
00421   assert_param(IS_I2C_ALL_PERIPH(I2Cx));
00422   assert_param(IS_FUNCTIONAL_STATE(NewState));
00423   if (NewState != DISABLE)
00424   {
00425     /* Generate a STOP condition */
00426     I2Cx->CR1 |= CR1_STOP_Set;
00427   }
00428   else
00429   {
00430     /* Disable the STOP condition generation */
00431     I2Cx->CR1 &= CR1_STOP_Reset;
00432   }
00433 }
00434 
00442 void I2C_AcknowledgeConfig(I2C_TypeDef* I2Cx, FunctionalState NewState)
00443 {
00444   /* Check the parameters */
00445   assert_param(IS_I2C_ALL_PERIPH(I2Cx));
00446   assert_param(IS_FUNCTIONAL_STATE(NewState));
00447   if (NewState != DISABLE)
00448   {
00449     /* Enable the acknowledgement */
00450     I2Cx->CR1 |= CR1_ACK_Set;
00451   }
00452   else
00453   {
00454     /* Disable the acknowledgement */
00455     I2Cx->CR1 &= CR1_ACK_Reset;
00456   }
00457 }
00458 
00465 void I2C_OwnAddress2Config(I2C_TypeDef* I2Cx, uint8_t Address)
00466 {
00467   uint16_t tmpreg = 0;
00468 
00469   /* Check the parameters */
00470   assert_param(IS_I2C_ALL_PERIPH(I2Cx));
00471 
00472   /* Get the old register value */
00473   tmpreg = I2Cx->OAR2;
00474 
00475   /* Reset I2Cx Own address2 bit [7:1] */
00476   tmpreg &= OAR2_ADD2_Reset;
00477 
00478   /* Set I2Cx Own address2 */
00479   tmpreg |= (uint16_t)((uint16_t)Address & (uint16_t)0x00FE);
00480 
00481   /* Store the new register value */
00482   I2Cx->OAR2 = tmpreg;
00483 }
00484 
00492 void I2C_DualAddressCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
00493 {
00494   /* Check the parameters */
00495   assert_param(IS_I2C_ALL_PERIPH(I2Cx));
00496   assert_param(IS_FUNCTIONAL_STATE(NewState));
00497   if (NewState != DISABLE)
00498   {
00499     /* Enable dual addressing mode */
00500     I2Cx->OAR2 |= OAR2_ENDUAL_Set;
00501   }
00502   else
00503   {
00504     /* Disable dual addressing mode */
00505     I2Cx->OAR2 &= OAR2_ENDUAL_Reset;
00506   }
00507 }
00508 
00516 void I2C_GeneralCallCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
00517 {
00518   /* Check the parameters */
00519   assert_param(IS_I2C_ALL_PERIPH(I2Cx));
00520   assert_param(IS_FUNCTIONAL_STATE(NewState));
00521   if (NewState != DISABLE)
00522   {
00523     /* Enable generall call */
00524     I2Cx->CR1 |= CR1_ENGC_Set;
00525   }
00526   else
00527   {
00528     /* Disable generall call */
00529     I2Cx->CR1 &= CR1_ENGC_Reset;
00530   }
00531 }
00532 
00545 void I2C_ITConfig(I2C_TypeDef* I2Cx, uint16_t I2C_IT, FunctionalState NewState)
00546 {
00547   /* Check the parameters */
00548   assert_param(IS_I2C_ALL_PERIPH(I2Cx));
00549   assert_param(IS_FUNCTIONAL_STATE(NewState));
00550   assert_param(IS_I2C_CONFIG_IT(I2C_IT));
00551   
00552   if (NewState != DISABLE)
00553   {
00554     /* Enable the selected I2C interrupts */
00555     I2Cx->CR2 |= I2C_IT;
00556   }
00557   else
00558   {
00559     /* Disable the selected I2C interrupts */
00560     I2Cx->CR2 &= (uint16_t)~I2C_IT;
00561   }
00562 }
00563 
00570 void I2C_SendData(I2C_TypeDef* I2Cx, uint8_t Data)
00571 {
00572   /* Check the parameters */
00573   assert_param(IS_I2C_ALL_PERIPH(I2Cx));
00574   /* Write in the DR register the data to be sent */
00575   I2Cx->DR = Data;
00576 }
00577 
00583 uint8_t I2C_ReceiveData(I2C_TypeDef* I2Cx)
00584 {
00585   /* Check the parameters */
00586   assert_param(IS_I2C_ALL_PERIPH(I2Cx));
00587   /* Return the data in the DR register */
00588   return (uint8_t)I2Cx->DR;
00589 }
00590 
00601 void I2C_Send7bitAddress(I2C_TypeDef* I2Cx, uint8_t Address, uint8_t I2C_Direction)
00602 {
00603   /* Check the parameters */
00604   assert_param(IS_I2C_ALL_PERIPH(I2Cx));
00605   assert_param(IS_I2C_DIRECTION(I2C_Direction));
00606   /* Test on the direction to set/reset the read/write bit */
00607   if (I2C_Direction != I2C_Direction_Transmitter)
00608   {
00609     /* Set the address bit0 for read */
00610     Address |= OAR1_ADD0_Set;
00611   }
00612   else
00613   {
00614     /* Reset the address bit0 for write */
00615     Address &= OAR1_ADD0_Reset;
00616   }
00617   /* Send the address */
00618   I2Cx->DR = Address;
00619 }
00620 
00636 uint16_t I2C_ReadRegister(I2C_TypeDef* I2Cx, uint8_t I2C_Register)
00637 {
00638   __IO uint32_t tmp = 0;
00639 
00640   /* Check the parameters */
00641   assert_param(IS_I2C_ALL_PERIPH(I2Cx));
00642   assert_param(IS_I2C_REGISTER(I2C_Register));
00643 
00644   tmp = (uint32_t) I2Cx;
00645   tmp += I2C_Register;
00646 
00647   /* Return the selected register value */
00648   return (*(__IO uint16_t *) tmp);
00649 }
00650 
00658 void I2C_SoftwareResetCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
00659 {
00660   /* Check the parameters */
00661   assert_param(IS_I2C_ALL_PERIPH(I2Cx));
00662   assert_param(IS_FUNCTIONAL_STATE(NewState));
00663   if (NewState != DISABLE)
00664   {
00665     /* Peripheral under reset */
00666     I2Cx->CR1 |= CR1_SWRST_Set;
00667   }
00668   else
00669   {
00670     /* Peripheral not under reset */
00671     I2Cx->CR1 &= CR1_SWRST_Reset;
00672   }
00673 }
00674 
00696 void I2C_NACKPositionConfig(I2C_TypeDef* I2Cx, uint16_t I2C_NACKPosition)
00697 {
00698   /* Check the parameters */
00699   assert_param(IS_I2C_ALL_PERIPH(I2Cx));
00700   assert_param(IS_I2C_NACK_POSITION(I2C_NACKPosition));
00701   
00702   /* Check the input parameter */
00703   if (I2C_NACKPosition == I2C_NACKPosition_Next)
00704   {
00705     /* Next byte in shift register is the last received byte */
00706     I2Cx->CR1 |= I2C_NACKPosition_Next;
00707   }
00708   else
00709   {
00710     /* Current byte in shift register is the last received byte */
00711     I2Cx->CR1 &= I2C_NACKPosition_Current;
00712   }
00713 }
00714 
00724 void I2C_SMBusAlertConfig(I2C_TypeDef* I2Cx, uint16_t I2C_SMBusAlert)
00725 {
00726   /* Check the parameters */
00727   assert_param(IS_I2C_ALL_PERIPH(I2Cx));
00728   assert_param(IS_I2C_SMBUS_ALERT(I2C_SMBusAlert));
00729   if (I2C_SMBusAlert == I2C_SMBusAlert_Low)
00730   {
00731     /* Drive the SMBusAlert pin Low */
00732     I2Cx->CR1 |= I2C_SMBusAlert_Low;
00733   }
00734   else
00735   {
00736     /* Drive the SMBusAlert pin High  */
00737     I2Cx->CR1 &= I2C_SMBusAlert_High;
00738   }
00739 }
00740 
00748 void I2C_TransmitPEC(I2C_TypeDef* I2Cx, FunctionalState NewState)
00749 {
00750   /* Check the parameters */
00751   assert_param(IS_I2C_ALL_PERIPH(I2Cx));
00752   assert_param(IS_FUNCTIONAL_STATE(NewState));
00753   if (NewState != DISABLE)
00754   {
00755     /* Enable the selected I2C PEC transmission */
00756     I2Cx->CR1 |= CR1_PEC_Set;
00757   }
00758   else
00759   {
00760     /* Disable the selected I2C PEC transmission */
00761     I2Cx->CR1 &= CR1_PEC_Reset;
00762   }
00763 }
00764 
00779 void I2C_PECPositionConfig(I2C_TypeDef* I2Cx, uint16_t I2C_PECPosition)
00780 {
00781   /* Check the parameters */
00782   assert_param(IS_I2C_ALL_PERIPH(I2Cx));
00783   assert_param(IS_I2C_PEC_POSITION(I2C_PECPosition));
00784   if (I2C_PECPosition == I2C_PECPosition_Next)
00785   {
00786     /* Next byte in shift register is PEC */
00787     I2Cx->CR1 |= I2C_PECPosition_Next;
00788   }
00789   else
00790   {
00791     /* Current byte in shift register is PEC */
00792     I2Cx->CR1 &= I2C_PECPosition_Current;
00793   }
00794 }
00795 
00803 void I2C_CalculatePEC(I2C_TypeDef* I2Cx, FunctionalState NewState)
00804 {
00805   /* Check the parameters */
00806   assert_param(IS_I2C_ALL_PERIPH(I2Cx));
00807   assert_param(IS_FUNCTIONAL_STATE(NewState));
00808   if (NewState != DISABLE)
00809   {
00810     /* Enable the selected I2C PEC calculation */
00811     I2Cx->CR1 |= CR1_ENPEC_Set;
00812   }
00813   else
00814   {
00815     /* Disable the selected I2C PEC calculation */
00816     I2Cx->CR1 &= CR1_ENPEC_Reset;
00817   }
00818 }
00819 
00825 uint8_t I2C_GetPEC(I2C_TypeDef* I2Cx)
00826 {
00827   /* Check the parameters */
00828   assert_param(IS_I2C_ALL_PERIPH(I2Cx));
00829   /* Return the selected I2C PEC value */
00830   return ((I2Cx->SR2) >> 8);
00831 }
00832 
00840 void I2C_ARPCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
00841 {
00842   /* Check the parameters */
00843   assert_param(IS_I2C_ALL_PERIPH(I2Cx));
00844   assert_param(IS_FUNCTIONAL_STATE(NewState));
00845   if (NewState != DISABLE)
00846   {
00847     /* Enable the selected I2C ARP */
00848     I2Cx->CR1 |= CR1_ENARP_Set;
00849   }
00850   else
00851   {
00852     /* Disable the selected I2C ARP */
00853     I2Cx->CR1 &= CR1_ENARP_Reset;
00854   }
00855 }
00856 
00864 void I2C_StretchClockCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
00865 {
00866   /* Check the parameters */
00867   assert_param(IS_I2C_ALL_PERIPH(I2Cx));
00868   assert_param(IS_FUNCTIONAL_STATE(NewState));
00869   if (NewState == DISABLE)
00870   {
00871     /* Enable the selected I2C Clock stretching */
00872     I2Cx->CR1 |= CR1_NOSTRETCH_Set;
00873   }
00874   else
00875   {
00876     /* Disable the selected I2C Clock stretching */
00877     I2Cx->CR1 &= CR1_NOSTRETCH_Reset;
00878   }
00879 }
00880 
00890 void I2C_FastModeDutyCycleConfig(I2C_TypeDef* I2Cx, uint16_t I2C_DutyCycle)
00891 {
00892   /* Check the parameters */
00893   assert_param(IS_I2C_ALL_PERIPH(I2Cx));
00894   assert_param(IS_I2C_DUTY_CYCLE(I2C_DutyCycle));
00895   if (I2C_DutyCycle != I2C_DutyCycle_16_9)
00896   {
00897     /* I2C fast mode Tlow/Thigh=2 */
00898     I2Cx->CCR &= I2C_DutyCycle_2;
00899   }
00900   else
00901   {
00902     /* I2C fast mode Tlow/Thigh=16/9 */
00903     I2Cx->CCR |= I2C_DutyCycle_16_9;
00904   }
00905 }
00906 
00907 
00908 
01030 ErrorStatus I2C_CheckEvent(I2C_TypeDef* I2Cx, uint32_t I2C_EVENT)
01031 {
01032   uint32_t lastevent = 0;
01033   uint32_t flag1 = 0, flag2 = 0;
01034   ErrorStatus status = ERROR;
01035 
01036   /* Check the parameters */
01037   assert_param(IS_I2C_ALL_PERIPH(I2Cx));
01038   assert_param(IS_I2C_EVENT(I2C_EVENT));
01039 
01040   /* Read the I2Cx status register */
01041   flag1 = I2Cx->SR1;
01042   flag2 = I2Cx->SR2;
01043   flag2 = flag2 << 16;
01044 
01045   /* Get the last event value from I2C status register */
01046   lastevent = (flag1 | flag2) & FLAG_Mask;
01047 
01048   /* Check whether the last event contains the I2C_EVENT */
01049   if ((lastevent & I2C_EVENT) == I2C_EVENT)
01050   {
01051     /* SUCCESS: last event is equal to I2C_EVENT */
01052     status = SUCCESS;
01053   }
01054   else
01055   {
01056     /* ERROR: last event is different from I2C_EVENT */
01057     status = ERROR;
01058   }
01059   /* Return status */
01060   return status;
01061 }
01062 
01078 uint32_t I2C_GetLastEvent(I2C_TypeDef* I2Cx)
01079 {
01080   uint32_t lastevent = 0;
01081   uint32_t flag1 = 0, flag2 = 0;
01082 
01083   /* Check the parameters */
01084   assert_param(IS_I2C_ALL_PERIPH(I2Cx));
01085 
01086   /* Read the I2Cx status register */
01087   flag1 = I2Cx->SR1;
01088   flag2 = I2Cx->SR2;
01089   flag2 = flag2 << 16;
01090 
01091   /* Get the last event value from I2C status register */
01092   lastevent = (flag1 | flag2) & FLAG_Mask;
01093 
01094   /* Return status */
01095   return lastevent;
01096 }
01097 
01133 FlagStatus I2C_GetFlagStatus(I2C_TypeDef* I2Cx, uint32_t I2C_FLAG)
01134 {
01135   FlagStatus bitstatus = RESET;
01136   __IO uint32_t i2creg = 0, i2cxbase = 0;
01137 
01138   /* Check the parameters */
01139   assert_param(IS_I2C_ALL_PERIPH(I2Cx));
01140   assert_param(IS_I2C_GET_FLAG(I2C_FLAG));
01141 
01142   /* Get the I2Cx peripheral base address */
01143   i2cxbase = (uint32_t)I2Cx;
01144   
01145   /* Read flag register index */
01146   i2creg = I2C_FLAG >> 28;
01147   
01148   /* Get bit[23:0] of the flag */
01149   I2C_FLAG &= FLAG_Mask;
01150   
01151   if(i2creg != 0)
01152   {
01153     /* Get the I2Cx SR1 register address */
01154     i2cxbase += 0x14;
01155   }
01156   else
01157   {
01158     /* Flag in I2Cx SR2 Register */
01159     I2C_FLAG = (uint32_t)(I2C_FLAG >> 16);
01160     /* Get the I2Cx SR2 register address */
01161     i2cxbase += 0x18;
01162   }
01163   
01164   if(((*(__IO uint32_t *)i2cxbase) & I2C_FLAG) != (uint32_t)RESET)
01165   {
01166     /* I2C_FLAG is set */
01167     bitstatus = SET;
01168   }
01169   else
01170   {
01171     /* I2C_FLAG is reset */
01172     bitstatus = RESET;
01173   }
01174   
01175   /* Return the I2C_FLAG status */
01176   return  bitstatus;
01177 }
01178 
01179 
01180 
01212 void I2C_ClearFlag(I2C_TypeDef* I2Cx, uint32_t I2C_FLAG)
01213 {
01214   uint32_t flagpos = 0;
01215   /* Check the parameters */
01216   assert_param(IS_I2C_ALL_PERIPH(I2Cx));
01217   assert_param(IS_I2C_CLEAR_FLAG(I2C_FLAG));
01218   /* Get the I2C flag position */
01219   flagpos = I2C_FLAG & FLAG_Mask;
01220   /* Clear the selected I2C flag */
01221   I2Cx->SR1 = (uint16_t)~flagpos;
01222 }
01223 
01246 ITStatus I2C_GetITStatus(I2C_TypeDef* I2Cx, uint32_t I2C_IT)
01247 {
01248   ITStatus bitstatus = RESET;
01249   uint32_t enablestatus = 0;
01250 
01251   /* Check the parameters */
01252   assert_param(IS_I2C_ALL_PERIPH(I2Cx));
01253   assert_param(IS_I2C_GET_IT(I2C_IT));
01254 
01255   /* Check if the interrupt source is enabled or not */
01256   enablestatus = (uint32_t)(((I2C_IT & ITEN_Mask) >> 16) & (I2Cx->CR2)) ;
01257   
01258   /* Get bit[23:0] of the flag */
01259   I2C_IT &= FLAG_Mask;
01260 
01261   /* Check the status of the specified I2C flag */
01262   if (((I2Cx->SR1 & I2C_IT) != (uint32_t)RESET) && enablestatus)
01263   {
01264     /* I2C_IT is set */
01265     bitstatus = SET;
01266   }
01267   else
01268   {
01269     /* I2C_IT is reset */
01270     bitstatus = RESET;
01271   }
01272   /* Return the I2C_IT status */
01273   return  bitstatus;
01274 }
01275 
01307 void I2C_ClearITPendingBit(I2C_TypeDef* I2Cx, uint32_t I2C_IT)
01308 {
01309   uint32_t flagpos = 0;
01310   /* Check the parameters */
01311   assert_param(IS_I2C_ALL_PERIPH(I2Cx));
01312   assert_param(IS_I2C_CLEAR_IT(I2C_IT));
01313   /* Get the I2C flag position */
01314   flagpos = I2C_IT & FLAG_Mask;
01315   /* Clear the selected I2C flag */
01316   I2Cx->SR1 = (uint16_t)~flagpos;
01317 }
01318 
01331 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/