CARME-M4 BSP  V1.5
stm32f4xx_i2c.c
Go to the documentation of this file.
1 
91 /* Includes ------------------------------------------------------------------*/
92 #include "stm32f4xx_i2c.h"
93 #include "stm32f4xx_rcc.h"
94 
104 /* Private typedef -----------------------------------------------------------*/
105 /* Private define ------------------------------------------------------------*/
106 
107 #define CR1_CLEAR_MASK ((uint16_t)0xFBF5) /*<! I2C registers Masks */
108 #define FLAG_MASK ((uint32_t)0x00FFFFFF) /*<! I2C FLAG mask */
109 #define ITEN_MASK ((uint32_t)0x07000000) /*<! I2C Interrupt Enable mask */
110 
111 /* Private macro -------------------------------------------------------------*/
112 /* Private variables ---------------------------------------------------------*/
113 /* Private function prototypes -----------------------------------------------*/
114 /* Private functions ---------------------------------------------------------*/
115 
137 void I2C_DeInit(I2C_TypeDef* I2Cx)
138 {
139  /* Check the parameters */
140  assert_param(IS_I2C_ALL_PERIPH(I2Cx));
141 
142  if (I2Cx == I2C1)
143  {
144  /* Enable I2C1 reset state */
145  RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, ENABLE);
146  /* Release I2C1 from reset state */
147  RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, DISABLE);
148  }
149  else if (I2Cx == I2C2)
150  {
151  /* Enable I2C2 reset state */
152  RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, ENABLE);
153  /* Release I2C2 from reset state */
154  RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, DISABLE);
155  }
156  else
157  {
158  if (I2Cx == I2C3)
159  {
160  /* Enable I2C3 reset state */
161  RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C3, ENABLE);
162  /* Release I2C3 from reset state */
163  RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C3, DISABLE);
164  }
165  }
166 }
167 
180 void I2C_Init(I2C_TypeDef* I2Cx, I2C_InitTypeDef* I2C_InitStruct)
181 {
182  uint16_t tmpreg = 0, freqrange = 0;
183  uint16_t result = 0x04;
184  uint32_t pclk1 = 8000000;
185  RCC_ClocksTypeDef rcc_clocks;
186  /* Check the parameters */
187  assert_param(IS_I2C_ALL_PERIPH(I2Cx));
188  assert_param(IS_I2C_CLOCK_SPEED(I2C_InitStruct->I2C_ClockSpeed));
189  assert_param(IS_I2C_MODE(I2C_InitStruct->I2C_Mode));
190  assert_param(IS_I2C_DUTY_CYCLE(I2C_InitStruct->I2C_DutyCycle));
191  assert_param(IS_I2C_OWN_ADDRESS1(I2C_InitStruct->I2C_OwnAddress1));
192  assert_param(IS_I2C_ACK_STATE(I2C_InitStruct->I2C_Ack));
193  assert_param(IS_I2C_ACKNOWLEDGE_ADDRESS(I2C_InitStruct->I2C_AcknowledgedAddress));
194 
195 /*---------------------------- I2Cx CR2 Configuration ------------------------*/
196  /* Get the I2Cx CR2 value */
197  tmpreg = I2Cx->CR2;
198  /* Clear frequency FREQ[5:0] bits */
199  tmpreg &= (uint16_t)~((uint16_t)I2C_CR2_FREQ);
200  /* Get pclk1 frequency value */
201  RCC_GetClocksFreq(&rcc_clocks);
202  pclk1 = rcc_clocks.PCLK1_Frequency;
203  /* Set frequency bits depending on pclk1 value */
204  freqrange = (uint16_t)(pclk1 / 1000000);
205  tmpreg |= freqrange;
206  /* Write to I2Cx CR2 */
207  I2Cx->CR2 = tmpreg;
208 
209 /*---------------------------- I2Cx CCR Configuration ------------------------*/
210  /* Disable the selected I2C peripheral to configure TRISE */
211  I2Cx->CR1 &= (uint16_t)~((uint16_t)I2C_CR1_PE);
212  /* Reset tmpreg value */
213  /* Clear F/S, DUTY and CCR[11:0] bits */
214  tmpreg = 0;
215 
216  /* Configure speed in standard mode */
217  if (I2C_InitStruct->I2C_ClockSpeed <= 100000)
218  {
219  /* Standard mode speed calculate */
220  result = (uint16_t)(pclk1 / (I2C_InitStruct->I2C_ClockSpeed << 1));
221  /* Test if CCR value is under 0x4*/
222  if (result < 0x04)
223  {
224  /* Set minimum allowed value */
225  result = 0x04;
226  }
227  /* Set speed value for standard mode */
228  tmpreg |= result;
229  /* Set Maximum Rise Time for standard mode */
230  I2Cx->TRISE = freqrange + 1;
231  }
232  /* Configure speed in fast mode */
233  /* To use the I2C at 400 KHz (in fast mode), the PCLK1 frequency (I2C peripheral
234  input clock) must be a multiple of 10 MHz */
235  else /*(I2C_InitStruct->I2C_ClockSpeed <= 400000)*/
236  {
237  if (I2C_InitStruct->I2C_DutyCycle == I2C_DutyCycle_2)
238  {
239  /* Fast mode speed calculate: Tlow/Thigh = 2 */
240  result = (uint16_t)(pclk1 / (I2C_InitStruct->I2C_ClockSpeed * 3));
241  }
242  else /*I2C_InitStruct->I2C_DutyCycle == I2C_DutyCycle_16_9*/
243  {
244  /* Fast mode speed calculate: Tlow/Thigh = 16/9 */
245  result = (uint16_t)(pclk1 / (I2C_InitStruct->I2C_ClockSpeed * 25));
246  /* Set DUTY bit */
247  result |= I2C_DutyCycle_16_9;
248  }
249 
250  /* Test if CCR value is under 0x1*/
251  if ((result & I2C_CCR_CCR) == 0)
252  {
253  /* Set minimum allowed value */
254  result |= (uint16_t)0x0001;
255  }
256  /* Set speed value and set F/S bit for fast mode */
257  tmpreg |= (uint16_t)(result | I2C_CCR_FS);
258  /* Set Maximum Rise Time for fast mode */
259  I2Cx->TRISE = (uint16_t)(((freqrange * (uint16_t)300) / (uint16_t)1000) + (uint16_t)1);
260  }
261 
262  /* Write to I2Cx CCR */
263  I2Cx->CCR = tmpreg;
264  /* Enable the selected I2C peripheral */
265  I2Cx->CR1 |= I2C_CR1_PE;
266 
267 /*---------------------------- I2Cx CR1 Configuration ------------------------*/
268  /* Get the I2Cx CR1 value */
269  tmpreg = I2Cx->CR1;
270  /* Clear ACK, SMBTYPE and SMBUS bits */
271  tmpreg &= CR1_CLEAR_MASK;
272  /* Configure I2Cx: mode and acknowledgement */
273  /* Set SMBTYPE and SMBUS bits according to I2C_Mode value */
274  /* Set ACK bit according to I2C_Ack value */
275  tmpreg |= (uint16_t)((uint32_t)I2C_InitStruct->I2C_Mode | I2C_InitStruct->I2C_Ack);
276  /* Write to I2Cx CR1 */
277  I2Cx->CR1 = tmpreg;
278 
279 /*---------------------------- I2Cx OAR1 Configuration -----------------------*/
280  /* Set I2Cx Own Address1 and acknowledged address */
281  I2Cx->OAR1 = (I2C_InitStruct->I2C_AcknowledgedAddress | I2C_InitStruct->I2C_OwnAddress1);
282 }
283 
290 {
291 /*---------------- Reset I2C init structure parameters values ----------------*/
292  /* initialize the I2C_ClockSpeed member */
293  I2C_InitStruct->I2C_ClockSpeed = 5000;
294  /* Initialize the I2C_Mode member */
295  I2C_InitStruct->I2C_Mode = I2C_Mode_I2C;
296  /* Initialize the I2C_DutyCycle member */
297  I2C_InitStruct->I2C_DutyCycle = I2C_DutyCycle_2;
298  /* Initialize the I2C_OwnAddress1 member */
299  I2C_InitStruct->I2C_OwnAddress1 = 0;
300  /* Initialize the I2C_Ack member */
301  I2C_InitStruct->I2C_Ack = I2C_Ack_Disable;
302  /* Initialize the I2C_AcknowledgedAddress member */
303  I2C_InitStruct->I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
304 }
305 
313 void I2C_Cmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
314 {
315  /* Check the parameters */
316  assert_param(IS_I2C_ALL_PERIPH(I2Cx));
317  assert_param(IS_FUNCTIONAL_STATE(NewState));
318  if (NewState != DISABLE)
319  {
320  /* Enable the selected I2C peripheral */
321  I2Cx->CR1 |= I2C_CR1_PE;
322  }
323  else
324  {
325  /* Disable the selected I2C peripheral */
326  I2Cx->CR1 &= (uint16_t)~((uint16_t)I2C_CR1_PE);
327  }
328 }
329 
342 void I2C_AnalogFilterCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
343 {
344  /* Check the parameters */
345  assert_param(IS_I2C_ALL_PERIPH(I2Cx));
346  assert_param(IS_FUNCTIONAL_STATE(NewState));
347  if (NewState != DISABLE)
348  {
349  /* Enable the analog filter */
350  I2Cx->FLTR &= (uint16_t)~((uint16_t)I2C_FLTR_ANOFF);
351  }
352  else
353  {
354  /* Disable the analog filter */
355  I2Cx->FLTR |= I2C_FLTR_ANOFF;
356  }
357 }
358 
371 void I2C_DigitalFilterConfig(I2C_TypeDef* I2Cx, uint16_t I2C_DigitalFilter)
372 {
373  uint16_t tmpreg = 0;
374 
375  /* Check the parameters */
376  assert_param(IS_I2C_ALL_PERIPH(I2Cx));
377  assert_param(IS_I2C_DIGITAL_FILTER(I2C_DigitalFilter));
378 
379  /* Get the old register value */
380  tmpreg = I2Cx->FLTR;
381 
382  /* Reset I2Cx DNF bit [3:0] */
383  tmpreg &= (uint16_t)~((uint16_t)I2C_FLTR_DNF);
384 
385  /* Set I2Cx DNF coefficient */
386  tmpreg |= (uint16_t)((uint16_t)I2C_DigitalFilter & I2C_FLTR_DNF);
387 
388  /* Store the new register value */
389  I2Cx->FLTR = tmpreg;
390 }
391 
399 void I2C_GenerateSTART(I2C_TypeDef* I2Cx, FunctionalState NewState)
400 {
401  /* Check the parameters */
402  assert_param(IS_I2C_ALL_PERIPH(I2Cx));
403  assert_param(IS_FUNCTIONAL_STATE(NewState));
404  if (NewState != DISABLE)
405  {
406  /* Generate a START condition */
407  I2Cx->CR1 |= I2C_CR1_START;
408  }
409  else
410  {
411  /* Disable the START condition generation */
412  I2Cx->CR1 &= (uint16_t)~((uint16_t)I2C_CR1_START);
413  }
414 }
415 
423 void I2C_GenerateSTOP(I2C_TypeDef* I2Cx, FunctionalState NewState)
424 {
425  /* Check the parameters */
426  assert_param(IS_I2C_ALL_PERIPH(I2Cx));
427  assert_param(IS_FUNCTIONAL_STATE(NewState));
428  if (NewState != DISABLE)
429  {
430  /* Generate a STOP condition */
431  I2Cx->CR1 |= I2C_CR1_STOP;
432  }
433  else
434  {
435  /* Disable the STOP condition generation */
436  I2Cx->CR1 &= (uint16_t)~((uint16_t)I2C_CR1_STOP);
437  }
438 }
439 
451 void I2C_Send7bitAddress(I2C_TypeDef* I2Cx, uint8_t Address, uint8_t I2C_Direction)
452 {
453  /* Check the parameters */
454  assert_param(IS_I2C_ALL_PERIPH(I2Cx));
455  assert_param(IS_I2C_DIRECTION(I2C_Direction));
456  /* Test on the direction to set/reset the read/write bit */
457  if (I2C_Direction != I2C_Direction_Transmitter)
458  {
459  /* Set the address bit0 for read */
460  Address |= I2C_OAR1_ADD0;
461  }
462  else
463  {
464  /* Reset the address bit0 for write */
465  Address &= (uint8_t)~((uint8_t)I2C_OAR1_ADD0);
466  }
467  /* Send the address */
468  I2Cx->DR = Address;
469 }
470 
478 void I2C_AcknowledgeConfig(I2C_TypeDef* I2Cx, FunctionalState NewState)
479 {
480  /* Check the parameters */
481  assert_param(IS_I2C_ALL_PERIPH(I2Cx));
482  assert_param(IS_FUNCTIONAL_STATE(NewState));
483  if (NewState != DISABLE)
484  {
485  /* Enable the acknowledgement */
486  I2Cx->CR1 |= I2C_CR1_ACK;
487  }
488  else
489  {
490  /* Disable the acknowledgement */
491  I2Cx->CR1 &= (uint16_t)~((uint16_t)I2C_CR1_ACK);
492  }
493 }
494 
501 void I2C_OwnAddress2Config(I2C_TypeDef* I2Cx, uint8_t Address)
502 {
503  uint16_t tmpreg = 0;
504 
505  /* Check the parameters */
506  assert_param(IS_I2C_ALL_PERIPH(I2Cx));
507 
508  /* Get the old register value */
509  tmpreg = I2Cx->OAR2;
510 
511  /* Reset I2Cx Own address2 bit [7:1] */
512  tmpreg &= (uint16_t)~((uint16_t)I2C_OAR2_ADD2);
513 
514  /* Set I2Cx Own address2 */
515  tmpreg |= (uint16_t)((uint16_t)Address & (uint16_t)0x00FE);
516 
517  /* Store the new register value */
518  I2Cx->OAR2 = tmpreg;
519 }
520 
528 void I2C_DualAddressCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
529 {
530  /* Check the parameters */
531  assert_param(IS_I2C_ALL_PERIPH(I2Cx));
532  assert_param(IS_FUNCTIONAL_STATE(NewState));
533  if (NewState != DISABLE)
534  {
535  /* Enable dual addressing mode */
536  I2Cx->OAR2 |= I2C_OAR2_ENDUAL;
537  }
538  else
539  {
540  /* Disable dual addressing mode */
541  I2Cx->OAR2 &= (uint16_t)~((uint16_t)I2C_OAR2_ENDUAL);
542  }
543 }
544 
552 void I2C_GeneralCallCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
553 {
554  /* Check the parameters */
555  assert_param(IS_I2C_ALL_PERIPH(I2Cx));
556  assert_param(IS_FUNCTIONAL_STATE(NewState));
557  if (NewState != DISABLE)
558  {
559  /* Enable generall call */
560  I2Cx->CR1 |= I2C_CR1_ENGC;
561  }
562  else
563  {
564  /* Disable generall call */
565  I2Cx->CR1 &= (uint16_t)~((uint16_t)I2C_CR1_ENGC);
566  }
567 }
568 
578 void I2C_SoftwareResetCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
579 {
580  /* Check the parameters */
581  assert_param(IS_I2C_ALL_PERIPH(I2Cx));
582  assert_param(IS_FUNCTIONAL_STATE(NewState));
583  if (NewState != DISABLE)
584  {
585  /* Peripheral under reset */
586  I2Cx->CR1 |= I2C_CR1_SWRST;
587  }
588  else
589  {
590  /* Peripheral not under reset */
591  I2Cx->CR1 &= (uint16_t)~((uint16_t)I2C_CR1_SWRST);
592  }
593 }
594 
602 void I2C_StretchClockCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
603 {
604  /* Check the parameters */
605  assert_param(IS_I2C_ALL_PERIPH(I2Cx));
606  assert_param(IS_FUNCTIONAL_STATE(NewState));
607  if (NewState == DISABLE)
608  {
609  /* Enable the selected I2C Clock stretching */
610  I2Cx->CR1 |= I2C_CR1_NOSTRETCH;
611  }
612  else
613  {
614  /* Disable the selected I2C Clock stretching */
615  I2Cx->CR1 &= (uint16_t)~((uint16_t)I2C_CR1_NOSTRETCH);
616  }
617 }
618 
628 void I2C_FastModeDutyCycleConfig(I2C_TypeDef* I2Cx, uint16_t I2C_DutyCycle)
629 {
630  /* Check the parameters */
631  assert_param(IS_I2C_ALL_PERIPH(I2Cx));
632  assert_param(IS_I2C_DUTY_CYCLE(I2C_DutyCycle));
633  if (I2C_DutyCycle != I2C_DutyCycle_16_9)
634  {
635  /* I2C fast mode Tlow/Thigh=2 */
636  I2Cx->CCR &= I2C_DutyCycle_2;
637  }
638  else
639  {
640  /* I2C fast mode Tlow/Thigh=16/9 */
641  I2Cx->CCR |= I2C_DutyCycle_16_9;
642  }
643 }
644 
666 void I2C_NACKPositionConfig(I2C_TypeDef* I2Cx, uint16_t I2C_NACKPosition)
667 {
668  /* Check the parameters */
669  assert_param(IS_I2C_ALL_PERIPH(I2Cx));
670  assert_param(IS_I2C_NACK_POSITION(I2C_NACKPosition));
671 
672  /* Check the input parameter */
673  if (I2C_NACKPosition == I2C_NACKPosition_Next)
674  {
675  /* Next byte in shift register is the last received byte */
676  I2Cx->CR1 |= I2C_NACKPosition_Next;
677  }
678  else
679  {
680  /* Current byte in shift register is the last received byte */
681  I2Cx->CR1 &= I2C_NACKPosition_Current;
682  }
683 }
684 
694 void I2C_SMBusAlertConfig(I2C_TypeDef* I2Cx, uint16_t I2C_SMBusAlert)
695 {
696  /* Check the parameters */
697  assert_param(IS_I2C_ALL_PERIPH(I2Cx));
698  assert_param(IS_I2C_SMBUS_ALERT(I2C_SMBusAlert));
699  if (I2C_SMBusAlert == I2C_SMBusAlert_Low)
700  {
701  /* Drive the SMBusAlert pin Low */
702  I2Cx->CR1 |= I2C_SMBusAlert_Low;
703  }
704  else
705  {
706  /* Drive the SMBusAlert pin High */
707  I2Cx->CR1 &= I2C_SMBusAlert_High;
708  }
709 }
710 
718 void I2C_ARPCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
719 {
720  /* Check the parameters */
721  assert_param(IS_I2C_ALL_PERIPH(I2Cx));
722  assert_param(IS_FUNCTIONAL_STATE(NewState));
723  if (NewState != DISABLE)
724  {
725  /* Enable the selected I2C ARP */
726  I2Cx->CR1 |= I2C_CR1_ENARP;
727  }
728  else
729  {
730  /* Disable the selected I2C ARP */
731  I2Cx->CR1 &= (uint16_t)~((uint16_t)I2C_CR1_ENARP);
732  }
733 }
756 void I2C_SendData(I2C_TypeDef* I2Cx, uint8_t Data)
757 {
758  /* Check the parameters */
759  assert_param(IS_I2C_ALL_PERIPH(I2Cx));
760  /* Write in the DR register the data to be sent */
761  I2Cx->DR = Data;
762 }
763 
769 uint8_t I2C_ReceiveData(I2C_TypeDef* I2Cx)
770 {
771  /* Check the parameters */
772  assert_param(IS_I2C_ALL_PERIPH(I2Cx));
773  /* Return the data in the DR register */
774  return (uint8_t)I2Cx->DR;
775 }
776 
800 void I2C_TransmitPEC(I2C_TypeDef* I2Cx, FunctionalState NewState)
801 {
802  /* Check the parameters */
803  assert_param(IS_I2C_ALL_PERIPH(I2Cx));
804  assert_param(IS_FUNCTIONAL_STATE(NewState));
805  if (NewState != DISABLE)
806  {
807  /* Enable the selected I2C PEC transmission */
808  I2Cx->CR1 |= I2C_CR1_PEC;
809  }
810  else
811  {
812  /* Disable the selected I2C PEC transmission */
813  I2Cx->CR1 &= (uint16_t)~((uint16_t)I2C_CR1_PEC);
814  }
815 }
816 
831 void I2C_PECPositionConfig(I2C_TypeDef* I2Cx, uint16_t I2C_PECPosition)
832 {
833  /* Check the parameters */
834  assert_param(IS_I2C_ALL_PERIPH(I2Cx));
835  assert_param(IS_I2C_PEC_POSITION(I2C_PECPosition));
836  if (I2C_PECPosition == I2C_PECPosition_Next)
837  {
838  /* Next byte in shift register is PEC */
839  I2Cx->CR1 |= I2C_PECPosition_Next;
840  }
841  else
842  {
843  /* Current byte in shift register is PEC */
844  I2Cx->CR1 &= I2C_PECPosition_Current;
845  }
846 }
847 
855 void I2C_CalculatePEC(I2C_TypeDef* I2Cx, FunctionalState NewState)
856 {
857  /* Check the parameters */
858  assert_param(IS_I2C_ALL_PERIPH(I2Cx));
859  assert_param(IS_FUNCTIONAL_STATE(NewState));
860  if (NewState != DISABLE)
861  {
862  /* Enable the selected I2C PEC calculation */
863  I2Cx->CR1 |= I2C_CR1_ENPEC;
864  }
865  else
866  {
867  /* Disable the selected I2C PEC calculation */
868  I2Cx->CR1 &= (uint16_t)~((uint16_t)I2C_CR1_ENPEC);
869  }
870 }
871 
877 uint8_t I2C_GetPEC(I2C_TypeDef* I2Cx)
878 {
879  /* Check the parameters */
880  assert_param(IS_I2C_ALL_PERIPH(I2Cx));
881  /* Return the selected I2C PEC value */
882  return ((I2Cx->SR2) >> 8);
883 }
884 
910 void I2C_DMACmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
911 {
912  /* Check the parameters */
913  assert_param(IS_I2C_ALL_PERIPH(I2Cx));
914  assert_param(IS_FUNCTIONAL_STATE(NewState));
915  if (NewState != DISABLE)
916  {
917  /* Enable the selected I2C DMA requests */
918  I2Cx->CR2 |= I2C_CR2_DMAEN;
919  }
920  else
921  {
922  /* Disable the selected I2C DMA requests */
923  I2Cx->CR2 &= (uint16_t)~((uint16_t)I2C_CR2_DMAEN);
924  }
925 }
926 
934 void I2C_DMALastTransferCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
935 {
936  /* Check the parameters */
937  assert_param(IS_I2C_ALL_PERIPH(I2Cx));
938  assert_param(IS_FUNCTIONAL_STATE(NewState));
939  if (NewState != DISABLE)
940  {
941  /* Next DMA transfer is the last transfer */
942  I2Cx->CR2 |= I2C_CR2_LAST;
943  }
944  else
945  {
946  /* Next DMA transfer is not the last transfer */
947  I2Cx->CR2 &= (uint16_t)~((uint16_t)I2C_CR2_LAST);
948  }
949 }
950 
1072 uint16_t I2C_ReadRegister(I2C_TypeDef* I2Cx, uint8_t I2C_Register)
1073 {
1074  __IO uint32_t tmp = 0;
1075 
1076  /* Check the parameters */
1077  assert_param(IS_I2C_ALL_PERIPH(I2Cx));
1078  assert_param(IS_I2C_REGISTER(I2C_Register));
1079 
1080  tmp = (uint32_t) I2Cx;
1081  tmp += I2C_Register;
1082 
1083  /* Return the selected register value */
1084  return (*(__IO uint16_t *) tmp);
1085 }
1086 
1099 void I2C_ITConfig(I2C_TypeDef* I2Cx, uint16_t I2C_IT, FunctionalState NewState)
1100 {
1101  /* Check the parameters */
1102  assert_param(IS_I2C_ALL_PERIPH(I2Cx));
1103  assert_param(IS_FUNCTIONAL_STATE(NewState));
1104  assert_param(IS_I2C_CONFIG_IT(I2C_IT));
1105 
1106  if (NewState != DISABLE)
1107  {
1108  /* Enable the selected I2C interrupts */
1109  I2Cx->CR2 |= I2C_IT;
1110  }
1111  else
1112  {
1113  /* Disable the selected I2C interrupts */
1114  I2Cx->CR2 &= (uint16_t)~I2C_IT;
1115  }
1116 }
1117 
1118 /*
1119  ===============================================================================
1120  1. Basic state monitoring
1121  ===============================================================================
1122  */
1123 
1158 ErrorStatus I2C_CheckEvent(I2C_TypeDef* I2Cx, uint32_t I2C_EVENT)
1159 {
1160  uint32_t lastevent = 0;
1161  uint32_t flag1 = 0, flag2 = 0;
1162  ErrorStatus status = ERROR;
1163 
1164  /* Check the parameters */
1165  assert_param(IS_I2C_ALL_PERIPH(I2Cx));
1166  assert_param(IS_I2C_EVENT(I2C_EVENT));
1167 
1168  /* Read the I2Cx status register */
1169  flag1 = I2Cx->SR1;
1170  flag2 = I2Cx->SR2;
1171  flag2 = flag2 << 16;
1172 
1173  /* Get the last event value from I2C status register */
1174  lastevent = (flag1 | flag2) & FLAG_MASK;
1175 
1176  /* Check whether the last event contains the I2C_EVENT */
1177  if ((lastevent & I2C_EVENT) == I2C_EVENT)
1178  {
1179  /* SUCCESS: last event is equal to I2C_EVENT */
1180  status = SUCCESS;
1181  }
1182  else
1183  {
1184  /* ERROR: last event is different from I2C_EVENT */
1185  status = ERROR;
1186  }
1187  /* Return status */
1188  return status;
1189 }
1190 
1191 /*
1192  ===============================================================================
1193  2. Advanced state monitoring
1194  ===============================================================================
1195  */
1196 
1206 uint32_t I2C_GetLastEvent(I2C_TypeDef* I2Cx)
1207 {
1208  uint32_t lastevent = 0;
1209  uint32_t flag1 = 0, flag2 = 0;
1210 
1211  /* Check the parameters */
1212  assert_param(IS_I2C_ALL_PERIPH(I2Cx));
1213 
1214  /* Read the I2Cx status register */
1215  flag1 = I2Cx->SR1;
1216  flag2 = I2Cx->SR2;
1217  flag2 = flag2 << 16;
1218 
1219  /* Get the last event value from I2C status register */
1220  lastevent = (flag1 | flag2) & FLAG_MASK;
1221 
1222  /* Return status */
1223  return lastevent;
1224 }
1225 
1226 /*
1227  ===============================================================================
1228  3. Flag-based state monitoring
1229  ===============================================================================
1230  */
1231 
1261 FlagStatus I2C_GetFlagStatus(I2C_TypeDef* I2Cx, uint32_t I2C_FLAG)
1262 {
1263  FlagStatus bitstatus = RESET;
1264  __IO uint32_t i2creg = 0, i2cxbase = 0;
1265 
1266  /* Check the parameters */
1267  assert_param(IS_I2C_ALL_PERIPH(I2Cx));
1268  assert_param(IS_I2C_GET_FLAG(I2C_FLAG));
1269 
1270  /* Get the I2Cx peripheral base address */
1271  i2cxbase = (uint32_t)I2Cx;
1272 
1273  /* Read flag register index */
1274  i2creg = I2C_FLAG >> 28;
1275 
1276  /* Get bit[23:0] of the flag */
1277  I2C_FLAG &= FLAG_MASK;
1278 
1279  if(i2creg != 0)
1280  {
1281  /* Get the I2Cx SR1 register address */
1282  i2cxbase += 0x14;
1283  }
1284  else
1285  {
1286  /* Flag in I2Cx SR2 Register */
1287  I2C_FLAG = (uint32_t)(I2C_FLAG >> 16);
1288  /* Get the I2Cx SR2 register address */
1289  i2cxbase += 0x18;
1290  }
1291 
1292  if(((*(__IO uint32_t *)i2cxbase) & I2C_FLAG) != (uint32_t)RESET)
1293  {
1294  /* I2C_FLAG is set */
1295  bitstatus = SET;
1296  }
1297  else
1298  {
1299  /* I2C_FLAG is reset */
1300  bitstatus = RESET;
1301  }
1302 
1303  /* Return the I2C_FLAG status */
1304  return bitstatus;
1305 }
1306 
1338 void I2C_ClearFlag(I2C_TypeDef* I2Cx, uint32_t I2C_FLAG)
1339 {
1340  uint32_t flagpos = 0;
1341  /* Check the parameters */
1342  assert_param(IS_I2C_ALL_PERIPH(I2Cx));
1343  assert_param(IS_I2C_CLEAR_FLAG(I2C_FLAG));
1344  /* Get the I2C flag position */
1345  flagpos = I2C_FLAG & FLAG_MASK;
1346  /* Clear the selected I2C flag */
1347  I2Cx->SR1 = (uint16_t)~flagpos;
1348 }
1349 
1372 ITStatus I2C_GetITStatus(I2C_TypeDef* I2Cx, uint32_t I2C_IT)
1373 {
1374  ITStatus bitstatus = RESET;
1375  uint32_t enablestatus = 0;
1376 
1377  /* Check the parameters */
1378  assert_param(IS_I2C_ALL_PERIPH(I2Cx));
1379  assert_param(IS_I2C_GET_IT(I2C_IT));
1380 
1381  /* Check if the interrupt source is enabled or not */
1382  enablestatus = (uint32_t)(((I2C_IT & ITEN_MASK) >> 16) & (I2Cx->CR2)) ;
1383 
1384  /* Get bit[23:0] of the flag */
1385  I2C_IT &= FLAG_MASK;
1386 
1387  /* Check the status of the specified I2C flag */
1388  if (((I2Cx->SR1 & I2C_IT) != (uint32_t)RESET) && enablestatus)
1389  {
1390  /* I2C_IT is set */
1391  bitstatus = SET;
1392  }
1393  else
1394  {
1395  /* I2C_IT is reset */
1396  bitstatus = RESET;
1397  }
1398  /* Return the I2C_IT status */
1399  return bitstatus;
1400 }
1401 
1432 void I2C_ClearITPendingBit(I2C_TypeDef* I2Cx, uint32_t I2C_IT)
1433 {
1434  uint32_t flagpos = 0;
1435  /* Check the parameters */
1436  assert_param(IS_I2C_ALL_PERIPH(I2Cx));
1437  assert_param(IS_I2C_CLEAR_IT(I2C_IT));
1438 
1439  /* Get the I2C flag position */
1440  flagpos = I2C_IT & FLAG_MASK;
1441 
1442  /* Clear the selected I2C flag */
1443  I2Cx->SR1 = (uint16_t)~flagpos;
1444 }
1445 
1462 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
This file contains all the functions prototypes for the RCC firmware library.
void I2C_PECPositionConfig(I2C_TypeDef *I2Cx, uint16_t I2C_PECPosition)
Selects the specified I2C PEC position.
void I2C_SMBusAlertConfig(I2C_TypeDef *I2Cx, uint16_t I2C_SMBusAlert)
Drives the SMBusAlert pin high or low for the specified I2C.
void I2C_SendData(I2C_TypeDef *I2Cx, uint8_t Data)
Sends a data byte through the I2Cx peripheral.
void I2C_DMACmd(I2C_TypeDef *I2Cx, FunctionalState NewState)
Enables or disables the specified I2C DMA requests.
void I2C_TransmitPEC(I2C_TypeDef *I2Cx, FunctionalState NewState)
Enables or disables the specified I2C PEC transfer.
uint8_t I2C_ReceiveData(I2C_TypeDef *I2Cx)
Returns the most recent received data by the I2Cx peripheral.
void I2C_GenerateSTART(I2C_TypeDef *I2Cx, FunctionalState NewState)
Generates I2Cx communication START condition.
uint16_t I2C_AcknowledgedAddress
Definition: stm32f4xx_i2c.h:71
void I2C_ClearFlag(I2C_TypeDef *I2Cx, uint32_t I2C_FLAG)
Clears the I2Cx's pending flags.
uint16_t I2C_OwnAddress1
Definition: stm32f4xx_i2c.h:65
void I2C_GenerateSTOP(I2C_TypeDef *I2Cx, FunctionalState NewState)
Generates I2Cx communication STOP condition.
FlagStatus I2C_GetFlagStatus(I2C_TypeDef *I2Cx, uint32_t I2C_FLAG)
Checks whether the specified I2C flag is set or not.
void I2C_StructInit(I2C_InitTypeDef *I2C_InitStruct)
Fills each I2C_InitStruct member with its default value.
void I2C_Cmd(I2C_TypeDef *I2Cx, FunctionalState NewState)
Enables or disables the specified I2C peripheral.
void I2C_DualAddressCmd(I2C_TypeDef *I2Cx, FunctionalState NewState)
Enables or disables the specified I2C dual addressing mode.
#define I2C_DutyCycle_2
void I2C_OwnAddress2Config(I2C_TypeDef *I2Cx, uint8_t Address)
Configures the specified I2C own address2.
void I2C_ARPCmd(I2C_TypeDef *I2Cx, FunctionalState NewState)
Enables or disables the specified I2C ARP.
#define CR1_CLEAR_MASK
uint8_t I2C_GetPEC(I2C_TypeDef *I2Cx)
Returns the PEC value for the specified I2C.
void I2C_Init(I2C_TypeDef *I2Cx, I2C_InitTypeDef *I2C_InitStruct)
Initializes the I2Cx peripheral according to the specified parameters in the I2C_InitStruct.
void I2C_DMALastTransferCmd(I2C_TypeDef *I2Cx, FunctionalState NewState)
Specifies that the next DMA transfer is the last one.
void I2C_SoftwareResetCmd(I2C_TypeDef *I2Cx, FunctionalState NewState)
Enables or disables the specified I2C software reset.
This file contains all the functions prototypes for the I2C firmware library.
uint16_t I2C_DutyCycle
Definition: stm32f4xx_i2c.h:62
void I2C_Send7bitAddress(I2C_TypeDef *I2Cx, uint8_t Address, uint8_t I2C_Direction)
Transmits the address byte to select the slave device.
void I2C_DigitalFilterConfig(I2C_TypeDef *I2Cx, uint16_t I2C_DigitalFilter)
Configures the Digital noise filter of I2C peripheral.
#define I2C_DutyCycle_16_9
ITStatus I2C_GetITStatus(I2C_TypeDef *I2Cx, uint32_t I2C_IT)
Checks whether the specified I2C interrupt has occurred or not.
void I2C_NACKPositionConfig(I2C_TypeDef *I2Cx, uint16_t I2C_NACKPosition)
Selects the specified I2C NACK position in master receiver mode.
void I2C_ITConfig(I2C_TypeDef *I2Cx, uint16_t I2C_IT, FunctionalState NewState)
Enables or disables the specified I2C interrupts.
static I2C_InitTypeDef I2C_InitStruct[2]
I2C init structures to reinitialize the interface, if it is crashed.
Definition: i2c.c:112
ErrorStatus I2C_CheckEvent(I2C_TypeDef *I2Cx, uint32_t I2C_EVENT)
Checks whether the last I2Cx Event is equal to the one passed as parameter.
void I2C_StretchClockCmd(I2C_TypeDef *I2Cx, FunctionalState NewState)
Enables or disables the specified I2C Clock stretching.
void I2C_AnalogFilterCmd(I2C_TypeDef *I2Cx, FunctionalState NewState)
Enables or disables the Analog filter of I2C peripheral.
void I2C_GeneralCallCmd(I2C_TypeDef *I2Cx, FunctionalState NewState)
Enables or disables the specified I2C general call feature.
void RCC_APB1PeriphResetCmd(uint32_t RCC_APB1Periph, FunctionalState NewState)
Forces or releases Low Speed APB (APB1) peripheral reset.
void I2C_FastModeDutyCycleConfig(I2C_TypeDef *I2Cx, uint16_t I2C_DutyCycle)
Selects the specified I2C fast mode duty cycle.
I2C Init structure definition.
Definition: stm32f4xx_i2c.h:54
uint16_t I2C_Mode
Definition: stm32f4xx_i2c.h:59
uint32_t I2C_ClockSpeed
Definition: stm32f4xx_i2c.h:56
void I2C_DeInit(I2C_TypeDef *I2Cx)
Deinitialize the I2Cx peripheral registers to their default reset values.
uint32_t PCLK1_Frequency
Definition: stm32f4xx_rcc.h:52
void RCC_GetClocksFreq(RCC_ClocksTypeDef *RCC_Clocks)
Returns the frequencies of different on chip clocks; SYSCLK, HCLK, PCLK1 and PCLK2.
uint16_t I2C_ReadRegister(I2C_TypeDef *I2Cx, uint8_t I2C_Register)
Reads the specified I2C register and returns its value.
uint16_t I2C_Ack
Definition: stm32f4xx_i2c.h:68
void I2C_AcknowledgeConfig(I2C_TypeDef *I2Cx, FunctionalState NewState)
Enables or disables the specified I2C acknowledge feature.
void I2C_CalculatePEC(I2C_TypeDef *I2Cx, FunctionalState NewState)
Enables or disables the PEC value calculation of the transferred bytes.
void I2C_ClearITPendingBit(I2C_TypeDef *I2Cx, uint32_t I2C_IT)
Clears the I2Cx's interrupt pending bits.
uint32_t I2C_GetLastEvent(I2C_TypeDef *I2Cx)
Returns the last I2Cx Event.