summaryrefslogtreecommitdiffstats
path: root/uc_str912/prj_blinky_simple_startup/str91x_lib/src/91x_i2c.c
blob: 9f15adba0ffcdcf9775630f4da673fe1a2295f36 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
/******************** (C) COPYRIGHT 2005 STMicroelectronics ********************
* File Name          : 91x_i2c.c
* Author             : MCD Application Team
* Date First Issued  : 05/18/2006 : Version 1.0
* Description        : This file provides all the I2C software functions.
********************************************************************************
* History:
* 05/24/2006 : Version 1.1
* 05/18/2006 : Version 1.0
********************************************************************************
* THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
* CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*******************************************************************************/

/* Includes ------------------------------------------------------------------*/
#include "91x_i2c.h"
#include "91x_scu.h"

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/

/* I2C IT enable */
#define  I2C_IT_Enable     0x01
#define  I2C_IT_Disable    0xFE

/* I2C Peripheral Enable/Disable */
#define  I2C_PE_Set        0x20
#define  I2C_PE_Reset      0xDF

/* Address direction bit */
#define I2C_ADD0_Set       0x01
#define I2C_ADD0_Reset     0xFE

/* I2C START Enable/Disable */
#define  I2C_Start_Enable       0x08
#define  I2C_Start_Disable      0xF7

/* I2C STOP Enable/Disable */
#define  I2C_Stop_Enable        0x02
#define  I2C_Stop_Disable       0xFD

/* I2C Masks */
#define  I2C_Frequency_Mask     0x1F
#define  I2C_AddressHigh_Mask   0xF9
#define  I2C_OwnAddress_Mask    0x0300
#define  I2C_StandardMode_Mask  0x7f
#define  I2C_FastMode_Mask      0x80
#define  I2C_Event_Mask         0x3FFF
#define  I2C_HeaderSet_Mask     0xF1
#define  I2C_HeaderReset_Mask   0xFE

/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/*******************************************************************************
* Function Name  : I2C_DeInit
* Description    : Deinitializes the I2C peripheral registers to their default
*                  reset values.
* Input          :- I2Cx: I2C peripheral can be:
*                    - I2C0
*					 - I2C1  
* Output         : None
* Return         : None
*******************************************************************************/
void I2C_DeInit(I2C_TypeDef* I2Cx)
{
  if (I2Cx == I2C0)
  {
  /* Reset the I2C0 registers values */
   SCU_APBPeriphReset(__I2C0, ENABLE);
   SCU_APBPeriphReset(__I2C0, DISABLE);
  }
   if (I2Cx == I2C1)
  {
  /* Reset the I2C1 registers values */
  SCU_APBPeriphReset(__I2C1, ENABLE);
  SCU_APBPeriphReset(__I2C1, DISABLE);
  }
}

/*******************************************************************************
* Function Name  : I2C_Init
* Description    : Initializes the I2C  peripheral according to the specified
*                  parameters in the I2C_InitTypeDef structure.
* Input          :- I2Cx: I2C peripheral can be:
*                    - I2C0
*					 - I2C1  
*
*                  - I2C_InitStruct: pointer to an I2C_InitTypeDef structure that
*                  contains the configuration information for the specified I2C
*                  peripheral.
* Output         : None
* Return         : None
*******************************************************************************/
void I2C_Init(I2C_TypeDef* I2Cx, I2C_InitTypeDef* I2C_InitStruct)
{
  u16 wResult = 0x0F;
  u32 dPCLK = 25000000;

  /* Get PCLK frequency value */
 dPCLK = SCU_GetPCLKFreqValue()*1000;
  /* Disable I2C peripheral to set FR[2:0] bits */
  I2C_Cmd (I2Cx, DISABLE);
  /* Clear frequency FR[2:0] bits */
  I2Cx->OAR2 &= I2C_Frequency_Mask;
  /* Set frequency bits depending on PCLK value */
  if ((dPCLK <1667000) & (dPCLK > 10000000))
    I2Cx->OAR2 |= 0x20;
  else if (dPCLK < 26670000)
    I2Cx->OAR2 |= 0x40;
  else if (dPCLK < 40000000)
    I2Cx->OAR2 |= 0x60;
  else if (dPCLK < 53330000)
    I2Cx->OAR2 |= 0x80;
  else if (dPCLK < 66000000)
    I2Cx->OAR2 |= 0xA0;
  else if (dPCLK < 80000000)
    I2Cx->OAR2 |= 0xC0;
  else if (dPCLK < 100000000)
    I2Cx->OAR2 |= 0xE0;
  I2C_Cmd (I2Cx, ENABLE);

  /* Configure general call */
  if (I2C_InitStruct->I2C_GeneralCall == I2C_GeneralCall_Enable)
  {
    /* Enable general call */
    I2Cx->CR |= I2C_GeneralCall_Enable;
  }
  else
  {
    /* Disable general call */
    I2Cx->CR &= I2C_GeneralCall_Disable;
  }
  /* Configure acknowledgement */
  if (I2C_InitStruct->I2C_Ack == I2C_Ack_Enable)
  {
    /* Enable acknowledgement */
    I2Cx->CR |= I2C_Ack_Enable;
  }
  else
  {
    /* Disable acknowledgement */
    I2Cx->CR &= I2C_Ack_Disable;
  }

  /* Configure LSB own address */
  I2Cx->OAR1 = I2C_InitStruct->I2C_OwnAddress;
  /* Clear MSB own address ADD[9:8] bits */
  I2Cx->OAR2 &= I2C_AddressHigh_Mask;
  /* Set MSB own address value */
  I2Cx->OAR2 |= (I2C_InitStruct->I2C_OwnAddress & I2C_OwnAddress_Mask)>>7;

  /* Configure speed in standard mode */
  if (I2C_InitStruct->I2C_CLKSpeed <= 100000)
  {
    /* Standard mode speed calculate */
    wResult = ((dPCLK/I2C_InitStruct->I2C_CLKSpeed)-7)/2;
    /* Set speed value and clear FM/SM bit for standard mode in LSB clock divider */
    I2Cx->CCR = wResult & I2C_StandardMode_Mask;
  }
  /* Configure speed in fast mode */
  else if (I2C_InitStruct->I2C_CLKSpeed <= 400000)
  {
    /* Fast mode speed calculate */
    wResult = ((dPCLK/I2C_InitStruct->I2C_CLKSpeed)-9)/3;
    /* Set speed value and set FM/SM bit for fast mode in LSB clock divider */
    I2Cx->CCR = wResult | I2C_FastMode_Mask;
  }
  /* Set speed in MSB clock divider */
  I2Cx->ECCR = wResult >>7;
}

/*******************************************************************************
* Function Name  : I2C_StructInit		         		
* Description    : Initialize the I2C Init Structure parameters
* Input          : - I2C_InitStruct: pointer to an I2C_InitTypeDef structure
                     which will be initialized.
* Output         : None	
* Return         : None.						
*******************************************************************************/
void I2C_StructInit(I2C_InitTypeDef* I2C_InitStruct)
{
  /* Initialize the I2C_CLKSpeed member */
  I2C_InitStruct->I2C_CLKSpeed = 5000;

  /* Initialize the I2C_OwnAddress member */
  I2C_InitStruct->I2C_OwnAddress = 0x0;

  /* Initialize the I2C_GeneralCall member */
  I2C_InitStruct->I2C_GeneralCall = I2C_GeneralCall_Disable;

  /* Initialize the I2C_Ack member */
  I2C_InitStruct->I2C_Ack = I2C_Ack_Disable;
}

/*******************************************************************************
* Function Name  : I2C_Cmd
* Description    : Enables or disables the specified I2C peripheral.
* Input          :- I2Cx: I2C peripheral can be:
*                    - I2C0
*					 - I2C1  
*                 - NewState: new state of the I2C peripheral. This parameter
*                    can be: ENABLE or DISABLE.
* Output         : None
* Return         : None.
*******************************************************************************/
void I2C_Cmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
{
  if (NewState == ENABLE)
  {
    /* Enable the I2C peripheral by setting twice the PE bit on the CR register */
    I2Cx->CR |= I2C_PE_Set;
	  I2Cx->CR |= I2C_PE_Set;
  }
  else
  {
    /* Disable the I2C peripheral */
    I2Cx->CR &= I2C_PE_Reset;
  }
}

/*******************************************************************************
* Function Name  : I2C_GenerateSTART
* Description    : Generates I2C communication START condition.
* Input          :- I2Cx: I2C peripheral can be:
*                    - I2C0
*					 - I2C1  
*
*                 - NewState: new state of the Start condition. This parameter
*                    can be: ENABLE or DISABLE.
* Output         : None
* Return         : None.
*******************************************************************************/
void I2C_GenerateStart(I2C_TypeDef* I2Cx, FunctionalState NewState)
{
  if (NewState == ENABLE)
  {
    /* Generate a START condition */
    I2Cx->CR |= I2C_Start_Enable;
  }
  else
  {
    /* Disable the START condition generation */
    I2Cx->CR &= I2C_Start_Disable;
  }
}

/*******************************************************************************
* Function Name  : I2C_GenerateSTOP
* Description    : Generates I2C communication STOP condition.
* Input          :- I2Cx: I2C peripheral can be:
*                    - I2C0
*					 - I2C1  
*
*                  - NewState: new state of the Stop condition. This parameter
*                    can be: ENABLE or DISABLE.
* Output         : None
* Return         : None.
*******************************************************************************/
void I2C_GenerateSTOP(I2C_TypeDef* I2Cx, FunctionalState NewState)
{
  if (NewState == ENABLE)
  {
    /* Generate a SIOP condition */
    I2Cx->CR |= I2C_Stop_Enable;
  }
  else
  {
    /* Disable the STOP condition generation */
    I2Cx->CR &= I2C_Stop_Disable;
  }
}

/*******************************************************************************
* Function Name  : I2C_AcknowledgeConfig
* Description    : Enables or disables I2C acknowledge feature.
* Input          :- I2Cx: I2C peripheral can be:
*                    - I2C0
*					 - I2C1  
*                 - NewState: new state of the Acknowledgement. This parameter
*                    can be: ENABLE or DISABLE.
* Output         : None
* Return         : None.
*******************************************************************************/
void I2C_AcknowledgeConfig(I2C_TypeDef *I2Cx, FunctionalState NewState)
{
  if (NewState == ENABLE)
  {
    /* Enable the acknowledgement */
    I2Cx->CR |= I2C_Ack_Enable;
  }
  else
  {
    /* Disable the acknowledgement */
    I2Cx->CR &= I2C_Ack_Disable;
  }
}

/*******************************************************************************
* Function Name  : I2C_ITConfig
* Description    : Enables or disables I2C interrupt feature.
* Input          :- I2Cx: I2C peripheral can be:
*                    - I2C0
*					 - I2C1  
*                  - NewState: new state of the specified I2C interrupt.
*                    This parameter can be: ENABLE or DISABLE.
* Output         : None
* Return         : None.
*******************************************************************************/
void I2C_ITConfig(I2C_TypeDef *I2Cx, FunctionalState NewState)
{
  if (NewState == ENABLE)
  {
    /* Enable the I2C interrupt */
    I2Cx->CR |= I2C_IT_Enable;
  }
  else
  {
    /* Disable the I2C interrupt */
    I2Cx->CR &= I2C_IT_Disable;
  }
}

/*******************************************************************************
* Function Name  : I2C_ReadRegister
* Description    : Reads any I2C register and returns its value.
* Input          :- I2Cx: I2C peripheral can be:
*                    - I2C0
*					 - I2C1     
*                 - I2C_Register: the I2C register to be read. This parameter
*                    can be one of the following values:
*                         - I2C_CR:   CR register.
*                         - I2C_SR1:  SR1 register.
*                         - I2C_SR2:  SR2 register.
*                         - I2C_CCR:  CCR register.
*                         - I2C_OAR1: OAR1 register.
*                         - I2C_OAR2: OAR2 register.
*                         - I2C_DR:   DR register.
*                         - I2C_ECCR: ECCR register.
* Output         : None
* Return         : The value of the register passed as parameter
*******************************************************************************/
u8 I2C_ReadRegister(I2C_TypeDef* I2Cx, u8 I2C_Register)
{
  /* Return the selected register value */
  if (I2Cx == I2C0)
  {
  return (*(u8 *)(I2C0_BASE + I2C_Register));
  }
  if (I2Cx == I2C1)
  {
  return (*(u8 *)(I2C1_BASE + I2C_Register));
  }
 return 0;
}

/*******************************************************************************
* Function Name  : I2C_GetFlagStatus
* Description    : Checks whether the specified I2C flag is set or not.
* Input          :- I2Cx: I2C peripheral can be:
*                    - I2C0
*					 - I2C1  
*                 - I2C_FLAG: flag to check. This parameter can be one of the
*                    following values:
*                         - I2C_FLAG_SB: Start bit flag
*                         - I2C_FLAG_M_SL: Master/Slave flag
*                         - I2C_FLAG_ADSL: Adress matched flag
*                         - I2C_FLAG_BTF: Byte transfer finished flag
*                         - I2C_FLAG_BUSY: Bus busy flag
*                         - I2C_FLAG_TRA: Transmitter/Receiver flag
*                         - I2C_FLAG_ADD10: 10-bit addressing in Master mode flag
*                         - I2C_FLAG_EVF: Event flag
*                         - I2C_FLAG_GCAL: General call flag
*                         - I2C_FLAG_BERR: Bus error flag
*                         - I2C_FLAG_ARLO: Arbitration lost flag
*                         - I2C_FLAG_STOPF: Stop detection flag
*                         - I2C_FLAG_AF: Acknowledge failure flag
*                         - I2C_FLAG_ENDAD: End of address transmission flag
*                         - I2C_FLAG_ACK: Acknowledge enable flag
* Output         : None	
* Return         : The NewState of the I2C_Flag (SET or RESET).
*******************************************************************************/
FlagStatus I2C_GetFlagStatus(I2C_TypeDef* I2Cx, u16 I2C_FLAG)
{
  u16 wFlag1=0, wFlag2=0, wTmp=0;

  wFlag1 = I2Cx->SR2;
  wFlag1 = wFlag1<<8;
  wFlag2 = I2Cx->CR & 0x04;

  /* Get all the I2C flags in a unique register*/
  wTmp = (((I2Cx->SR1 | (wFlag1)) & I2C_Event_Mask) | (wFlag2<<12));

  /* Check the status of the specified I2C flag */
  if((wTmp & I2C_FLAG) != RESET)
  {
    /* Return SET if I2C_FLAG is set */
    return SET;
  }
  else
  {
    /* Return RESET if I2C_FLAG is reset */
    return RESET;
  }
}

/*******************************************************************************
* Function Name  : I2C_ClearFlag
* Description    : Clears the I2C Flag passed as a parameter 
* Input          :- I2Cx: I2C peripheral can be:
*                    - I2C0
*					 - I2C1
*                 - I2C_FLAG: flag to check. This parameter can be one of the
*                    following values:
*                         - I2C_FLAG_SB: Start bit flag
*                         - I2C_FLAG_M_SL: Master/Slave flag
*                         - I2C_FLAG_ADSL: Adress matched flag
*                         - I2C_FLAG_BTF: Byte transfer finished flag
*                         - I2C_FLAG_BUSY: Bus busy flag
*                         - I2C_FLAG_TRA: Transmitter/Receiver flag
*                         - I2C_FLAG_ADD10: 10-bit addressing in Master mode flag
*                         - I2C_FLAG_EVF: Event flag
*                         - I2C_FLAG_GCAL: General call flag
*                         - I2C_FLAG_BERR: Bus error flag
*                         - I2C_FLAG_ARLO: Arbitration lost flag
*                         - I2C_FLAG_STOPF: Stop detection flag
*                         - I2C_FLAG_AF: Acknowledge failure flag
*                         - I2C_FLAG_ENDAD: End of address transmission flag
*                         - I2C_FLAG_ACK: Acknowledge enable flag
*                  - parameter needed in the case that the flag to be cleared
*                    need a write in one register
* Output         : None	
* Return         : None.
*******************************************************************************/
void I2C_ClearFlag(I2C_TypeDef* I2Cx, u16 I2C_FLAG, ...)
{
  u8 bTmp = (u8)*((u32 *) & I2C_FLAG + sizeof(I2C_FLAG));

  /* flags that need a read of the SR2 register to be cleared */
  if ((I2C_FLAG==I2C_FLAG_ADD10) || (I2C_FLAG==I2C_FLAG_EVF) || (I2C_FLAG==I2C_FLAG_BERR) || (I2C_FLAG==I2C_FLAG_ARLO) |
      (I2C_FLAG==I2C_FLAG_STOPF) ||(I2C_FLAG==I2C_FLAG_AF)  || (I2C_FLAG==I2C_FLAG_ENDAD))
  {
    /* Read the SR2 register */
    I2Cx->SR2;

    /* Two flags need a second step to be cleared */
    switch (I2C_FLAG)
    {
       case  I2C_FLAG_ADD10:
         /* Send the MSB 10bit address passed as second parameter */
         I2Cx->DR = bTmp;
         break;
       case  I2C_FLAG_ENDAD:
         /* Write to the I2C_CR register by setting PE bit */
         I2Cx->CR |= I2C_PE_Set;
         break;
    }
  }

  /* flags that need a read of the SR1 register to be cleared */
  else if (I2C_FLAG==I2C_FLAG_SB || I2C_FLAG==I2C_FLAG_ADSL || I2C_FLAG==I2C_FLAG_BTF || I2C_FLAG==I2C_FLAG_TRA)
  {
    /* Read the SR1 register */
    (void)I2Cx->SR1;

    /* three flags need a second step to be cleared */
    if (I2C_FLAG == I2C_FLAG_SB)
    {
      /* Send the address byte passed as second parameter */
      I2Cx->DR = bTmp;
    }
    else if (I2C_FLAG==I2C_FLAG_BTF || I2C_FLAG==I2C_FLAG_TRA)
    {
      /* return the received byte in the variable passed as second parameter  */
      bTmp=I2Cx->DR;
    }
  }

  /* flags that need to disable the I2C interface */
  else if ( I2C_FLAG==I2C_FLAG_M_SL || I2C_FLAG==I2C_FLAG_GCAL)
  {
    I2C_Cmd(I2Cx, DISABLE);
    I2C_Cmd(I2Cx, ENABLE);
  }
}

/*******************************************************************************
* Function Name  : I2C_Send7bitAddress                                             
* Description    : Transmits the address byte to select the slave device.      
* Input          :- I2Cx: I2C peripheral can be:
*                    - I2C0
*					 - I2C1
*                 - Address: specifies the slave address which will be transmitted    
*                 - Direction: specifies whether the I2C device will be a 
*                    Transmitter or a Receiver. This parameter can be one of the 
*                    following values
*                         - I2C_MODE_TRANSMITTER: Transmitter mode
*                         - I2C_MODE_RECEIVER: Receiver mode  
* Output         : None	
* Return         : None.                                                       
*******************************************************************************/
void I2C_Send7bitAddress(I2C_TypeDef* I2Cx, u8 Address, u8 Direction)
{
  /* Test on the direction to define the read/write bit */
  if (Direction == I2C_MODE_RECEIVER)
  {
    /* Set the address bit0 for read */
    Address |= I2C_ADD0_Set;
  }
  else
  {
    /* Reset the address bit0 for write */
    Address &= I2C_ADD0_Reset;
  }
  /* Send the address */
  I2Cx->DR = Address;
}

/*******************************************************************************
* Function Name  : I2C_SendData
* Description    : Send a data byte.
* Input          :- I2Cx: I2C peripheral can be:
*                    - I2C0
*					 - I2C1  
*                   - bData : the byte to be sent
* Output         : None	
* Return         : None.
*******************************************************************************/
void I2C_SendData(I2C_TypeDef* I2Cx, u8 bData)
{
  /* Write in the DR register the byte to be sent */
  I2Cx->DR = bData;
}

/*******************************************************************************
* Function Name  : I2C_ReceiveData
* Description    : Read the received byte.
* Input          : - I2Cx: I2C peripheral can be:
*                    - I2C0
*					 - I2C1
* Output         : None	
* Return         : The received byte
*******************************************************************************/
u8 I2C_ReceiveData(I2C_TypeDef* I2Cx)
{
  /* Return from the DR register the received byte */
  return I2Cx->DR;
}

/*******************************************************************************
* Function Name  : I2C_GetLastEvent
* Description    : Get the Last happened I2C Event.
* Input          :- I2Cx: I2C peripheral can be:
*                    - I2C0
*					 - I2C1  
* Output         : None	
* Return         : The Last happened Event.
*******************************************************************************/
u16 I2C_GetLastEvent(I2C_TypeDef* I2Cx)
{
  u16 wFlag1=0, wLastEvent=0;
  wFlag1 = I2Cx->SR2;
  wFlag1 = wFlag1<<8;
  /* Get the last event value from I2C status register */
  wLastEvent = (((I2Cx->SR1 | (wFlag1)) & I2C_Event_Mask));
  /* Return the last event */
  return wLastEvent;
}

/*******************************************************************************
* Function Name  : I2C_CheckEvent
* Description    : Checks whether the Last I2C Event is equal to the one passed
*                  as parameter.
* Input          :- I2Cx: I2C peripheral can be:
*                    - I2C0
*					 - I2C1   
*                  - I2C_EVENT: the event to check. This parameter can be one of
*                    the following values:
*                         - I2C_EVENT_SLAVE_ADDRESS_MATCHED
*                         - I2C_EVENT_SLAVE_BYTE_RECEIVED
*                         - I2C_EVENT_SLAVE_BYTE_TRANSMITTED
*                         - I2C_EVENT_MASTER_MODE_SELECT
*                         - I2C_EVENT_MASTER_MODE_SELECTED
*                         - I2C_EVENT_MASTER_BYTE_RECEIVED
*                         - I2C_EVENT_MASTER_BYTE_TRANSMITTED
*                         - I2C_EVENT_MASTER_MODE_ADDRESS10
*                         - I2C_EVENT_SLAVE_STOP_DETECTED
*                         - I2C_EVENT_SLAVE_ACK_FAILURE
* Output         : None	
* Return         : An ErrorStatus enumuration value:
*                         - SUCCESS: Last event is equal to the I2C_Event
*                         - ERROR: Last event is different from the I2C_Event
*******************************************************************************/
ErrorStatus I2C_CheckEvent(I2C_TypeDef* I2Cx,u16 I2C_EVENT)
{
  u16  wLastEvent = I2C_GetLastEvent(I2Cx);

  /* Check whther the last event is equal to I2C_EVENT */
  if (wLastEvent == I2C_EVENT)
  {
    /* Return SUCCESS when last event is equal to I2C_EVENT */
    return SUCCESS;
  }
  else
  {
    /* Return ERROR when last event is different from I2C_EVENT */
    return ERROR;
  }
}

/******************* (C) COPYRIGHT 2005 STMicroelectronics *****END OF FILE****/
OpenPOWER on IntegriCloud