OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [freertos-6.1.1/] [Demo/] [CORTEX_STM32F103_GCC_Rowley/] [ST Library/] [src/] [stm32f10x_i2c.c] - Blame information for rev 582

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 582 jeremybenn
/******************** (C) COPYRIGHT 2008 STMicroelectronics ********************
2
* File Name          : stm32f10x_i2c.c
3
* Author             : MCD Application Team
4
* Version            : V2.0.1
5
* Date               : 06/13/2008
6
* Description        : This file provides all the I2C firmware functions.
7
********************************************************************************
8
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
9
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
10
* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
11
* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
12
* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
13
* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
14
*******************************************************************************/
15
 
16
/* Includes ------------------------------------------------------------------*/
17
#include "stm32f10x_i2c.h"
18
#include "stm32f10x_rcc.h"
19
 
20
/* Private typedef -----------------------------------------------------------*/
21
/* Private define ------------------------------------------------------------*/
22
/* I2C SPE mask */
23
#define CR1_PE_Set              ((u16)0x0001)
24
#define CR1_PE_Reset            ((u16)0xFFFE)
25
 
26
/* I2C START mask */
27
#define CR1_START_Set           ((u16)0x0100)
28
#define CR1_START_Reset         ((u16)0xFEFF)
29
 
30
/* I2C STOP mask */
31
#define CR1_STOP_Set            ((u16)0x0200)
32
#define CR1_STOP_Reset          ((u16)0xFDFF)
33
 
34
/* I2C ACK mask */
35
#define CR1_ACK_Set             ((u16)0x0400)
36
#define CR1_ACK_Reset           ((u16)0xFBFF)
37
 
38
/* I2C ENGC mask */
39
#define CR1_ENGC_Set            ((u16)0x0040)
40
#define CR1_ENGC_Reset          ((u16)0xFFBF)
41
 
42
/* I2C SWRST mask */
43
#define CR1_SWRST_Set           ((u16)0x8000)
44
#define CR1_SWRST_Reset         ((u16)0x7FFF)
45
 
46
/* I2C PEC mask */
47
#define CR1_PEC_Set             ((u16)0x1000)
48
#define CR1_PEC_Reset           ((u16)0xEFFF)
49
 
50
/* I2C ENPEC mask */
51
#define CR1_ENPEC_Set           ((u16)0x0020)
52
#define CR1_ENPEC_Reset         ((u16)0xFFDF)
53
 
54
/* I2C ENARP mask */
55
#define CR1_ENARP_Set           ((u16)0x0010)
56
#define CR1_ENARP_Reset         ((u16)0xFFEF)
57
 
58
/* I2C NOSTRETCH mask */
59
#define CR1_NOSTRETCH_Set       ((u16)0x0080)
60
#define CR1_NOSTRETCH_Reset     ((u16)0xFF7F)
61
 
62
/* I2C registers Masks */
63
#define CR1_CLEAR_Mask          ((u16)0xFBF5)
64
 
65
/* I2C DMAEN mask */
66
#define CR2_DMAEN_Set           ((u16)0x0800)
67
#define CR2_DMAEN_Reset         ((u16)0xF7FF)
68
 
69
/* I2C LAST mask */
70
#define CR2_LAST_Set            ((u16)0x1000)
71
#define CR2_LAST_Reset          ((u16)0xEFFF)
72
 
73
/* I2C FREQ mask */
74
#define CR2_FREQ_Reset          ((u16)0xFFC0)
75
 
76
/* I2C ADD0 mask */
77
#define OAR1_ADD0_Set           ((u16)0x0001)
78
#define OAR1_ADD0_Reset         ((u16)0xFFFE)
79
 
80
/* I2C ENDUAL mask */
81
#define OAR2_ENDUAL_Set         ((u16)0x0001)
82
#define OAR2_ENDUAL_Reset       ((u16)0xFFFE)
83
 
84
/* I2C ADD2 mask */
85
#define OAR2_ADD2_Reset         ((u16)0xFF01)
86
 
87
/* I2C F/S mask */
88
#define CCR_FS_Set              ((u16)0x8000)
89
 
90
/* I2C CCR mask */
91
#define CCR_CCR_Set             ((u16)0x0FFF)
92
 
93
/* I2C FLAG mask */
94
#define FLAG_Mask               ((u32)0x00FFFFFF)
95
 
96
/* Private macro -------------------------------------------------------------*/
97
/* Private variables ---------------------------------------------------------*/
98
/* Private function prototypes -----------------------------------------------*/
99
/* Private functions ---------------------------------------------------------*/
100
 
101
/*******************************************************************************
102
* Function Name  : I2C_DeInit
103
* Description    : Deinitializes the I2Cx peripheral registers to their default
104
*                  reset values.
105
* Input          : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
106
* Output         : None
107
* Return         : None
108
*******************************************************************************/
109
void I2C_DeInit(I2C_TypeDef* I2Cx)
110
{
111
  /* Check the parameters */
112
  assert_param(IS_I2C_ALL_PERIPH(I2Cx));
113
 
114
  switch (*(u32*)&I2Cx)
115
  {
116
    case I2C1_BASE:
117
      /* Enable I2C1 reset state */
118
      RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, ENABLE);
119
      /* Release I2C1 from reset state */
120
      RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, DISABLE);
121
      break;
122
 
123
    case I2C2_BASE:
124
      /* Enable I2C2 reset state */
125
      RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, ENABLE);
126
      /* Release I2C2 from reset state */
127
      RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, DISABLE);
128
      break;
129
 
130
    default:
131
      break;
132
  }
133
}
134
 
135
/*******************************************************************************
136
* Function Name  : I2C_Init
137
* Description    : Initializes the I2Cx peripheral according to the specified
138
*                  parameters in the I2C_InitStruct.
139
* Input          : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
140
*                  - I2C_InitStruct: pointer to a I2C_InitTypeDef structure that
141
*                    contains the configuration information for the specified
142
*                    I2C peripheral.
143
* Output         : None
144
* Return         : None
145
******************************************************************************/
146
void I2C_Init(I2C_TypeDef* I2Cx, I2C_InitTypeDef* I2C_InitStruct)
147
{
148
  u16 tmpreg = 0, freqrange = 0;
149
  u16 result = 0x04;
150
  u32 pclk1 = 8000000;
151
  RCC_ClocksTypeDef  rcc_clocks;
152
 
153
  /* Check the parameters */
154
  assert_param(IS_I2C_ALL_PERIPH(I2Cx));
155
  assert_param(IS_I2C_MODE(I2C_InitStruct->I2C_Mode));
156
  assert_param(IS_I2C_DUTY_CYCLE(I2C_InitStruct->I2C_DutyCycle));
157
  assert_param(IS_I2C_OWN_ADDRESS1(I2C_InitStruct->I2C_OwnAddress1));
158
  assert_param(IS_I2C_ACK_STATE(I2C_InitStruct->I2C_Ack));
159
  assert_param(IS_I2C_ACKNOWLEDGE_ADDRESS(I2C_InitStruct->I2C_AcknowledgedAddress));
160
  assert_param(IS_I2C_CLOCK_SPEED(I2C_InitStruct->I2C_ClockSpeed));
161
 
162
/*---------------------------- I2Cx CR2 Configuration ------------------------*/
163
  /* Get the I2Cx CR2 value */
164
  tmpreg = I2Cx->CR2;
165
  /* Clear frequency FREQ[5:0] bits */
166
  tmpreg &= CR2_FREQ_Reset;
167
  /* Get pclk1 frequency value */
168
  RCC_GetClocksFreq(&rcc_clocks);
169
  pclk1 = rcc_clocks.PCLK1_Frequency;
170
  /* Set frequency bits depending on pclk1 value */
171
  freqrange = (u16)(pclk1 / 1000000);
172
  tmpreg |= freqrange;
173
  /* Write to I2Cx CR2 */
174
  I2Cx->CR2 = tmpreg;
175
 
176
/*---------------------------- I2Cx CCR Configuration ------------------------*/
177
  /* Disable the selected I2C peripheral to configure TRISE */
178
  I2Cx->CR1 &= CR1_PE_Reset;
179
 
180
  /* Reset tmpreg value */
181
  /* Clear F/S, DUTY and CCR[11:0] bits */
182
  tmpreg = 0;
183
 
184
  /* Configure speed in standard mode */
185
  if (I2C_InitStruct->I2C_ClockSpeed <= 100000)
186
  {
187
    /* Standard mode speed calculate */
188
    result = (u16)(pclk1 / (I2C_InitStruct->I2C_ClockSpeed << 1));
189
    /* Test if CCR value is under 0x4*/
190
    if (result < 0x04)
191
    {
192
      /* Set minimum allowed value */
193
      result = 0x04;
194
    }
195
    /* Set speed value for standard mode */
196
    tmpreg |= result;
197
    /* Set Maximum Rise Time for standard mode */
198
    I2Cx->TRISE = freqrange + 1;
199
  }
200
  /* Configure speed in fast mode */
201
  else /*(I2C_InitStruct->I2C_ClockSpeed <= 400000)*/
202
  {
203
    if (I2C_InitStruct->I2C_DutyCycle == I2C_DutyCycle_2)
204
    {
205
      /* Fast mode speed calculate: Tlow/Thigh = 2 */
206
      result = (u16)(pclk1 / (I2C_InitStruct->I2C_ClockSpeed * 3));
207
    }
208
    else /*I2C_InitStruct->I2C_DutyCycle == I2C_DutyCycle_16_9*/
209
    {
210
      /* Fast mode speed calculate: Tlow/Thigh = 16/9 */
211
      result = (u16)(pclk1 / (I2C_InitStruct->I2C_ClockSpeed * 25));
212
      /* Set DUTY bit */
213
      result |= I2C_DutyCycle_16_9;
214
    }
215
    /* Test if CCR value is under 0x1*/
216
    if ((result & CCR_CCR_Set) == 0)
217
    {
218
      /* Set minimum allowed value */
219
      result |= (u16)0x0001;
220
    }
221
    /* Set speed value and set F/S bit for fast mode */
222
    tmpreg |= result | CCR_FS_Set;
223
    /* Set Maximum Rise Time for fast mode */
224
    I2Cx->TRISE = (u16)(((freqrange * 300) / 1000) + 1);
225
  }
226
  /* Write to I2Cx CCR */
227
  I2Cx->CCR = tmpreg;
228
 
229
  /* Enable the selected I2C peripheral */
230
  I2Cx->CR1 |= CR1_PE_Set;
231
 
232
/*---------------------------- I2Cx CR1 Configuration ------------------------*/
233
  /* Get the I2Cx CR1 value */
234
  tmpreg = I2Cx->CR1;
235
  /* Clear ACK, SMBTYPE and  SMBUS bits */
236
  tmpreg &= CR1_CLEAR_Mask;
237
  /* Configure I2Cx: mode and acknowledgement */
238
  /* Set SMBTYPE and SMBUS bits according to I2C_Mode value */
239
  /* Set ACK bit according to I2C_Ack value */
240
  tmpreg |= (u16)((u32)I2C_InitStruct->I2C_Mode | I2C_InitStruct->I2C_Ack);
241
  /* Write to I2Cx CR1 */
242
  I2Cx->CR1 = tmpreg;
243
 
244
/*---------------------------- I2Cx OAR1 Configuration -----------------------*/
245
  /* Set I2Cx Own Address1 and acknowledged address */
246
  I2Cx->OAR1 = (I2C_InitStruct->I2C_AcknowledgedAddress | I2C_InitStruct->I2C_OwnAddress1);
247
}
248
 
249
/*******************************************************************************
250
* Function Name  : I2C_StructInit
251
* Description    : Fills each I2C_InitStruct member with its default value.
252
* Input          : - I2C_InitStruct: pointer to an I2C_InitTypeDef structure
253
*                    which will be initialized.
254
* Output         : None
255
* Return         : None
256
*******************************************************************************/
257
void I2C_StructInit(I2C_InitTypeDef* I2C_InitStruct)
258
{
259
/*---------------- Reset I2C init structure parameters values ----------------*/
260
  /* Initialize the I2C_Mode member */
261
  I2C_InitStruct->I2C_Mode = I2C_Mode_I2C;
262
 
263
  /* Initialize the I2C_DutyCycle member */
264
  I2C_InitStruct->I2C_DutyCycle = I2C_DutyCycle_2;
265
 
266
  /* Initialize the I2C_OwnAddress1 member */
267
  I2C_InitStruct->I2C_OwnAddress1 = 0;
268
 
269
  /* Initialize the I2C_Ack member */
270
  I2C_InitStruct->I2C_Ack = I2C_Ack_Disable;
271
 
272
  /* Initialize the I2C_AcknowledgedAddress member */
273
  I2C_InitStruct->I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
274
 
275
  /* initialize the I2C_ClockSpeed member */
276
  I2C_InitStruct->I2C_ClockSpeed = 5000;
277
}
278
 
279
/*******************************************************************************
280
* Function Name  : I2C_Cmd
281
* Description    : Enables or disables the specified I2C peripheral.
282
* Input          : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
283
*                  - NewState: new state of the I2Cx peripheral. This parameter
284
*                    can be: ENABLE or DISABLE.
285
* Output         : None
286
* Return         : None
287
*******************************************************************************/
288
void I2C_Cmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
289
{
290
  /* Check the parameters */
291
  assert_param(IS_I2C_ALL_PERIPH(I2Cx));
292
  assert_param(IS_FUNCTIONAL_STATE(NewState));
293
 
294
  if (NewState != DISABLE)
295
  {
296
    /* Enable the selected I2C peripheral */
297
    I2Cx->CR1 |= CR1_PE_Set;
298
  }
299
  else
300
  {
301
    /* Disable the selected I2C peripheral */
302
    I2Cx->CR1 &= CR1_PE_Reset;
303
  }
304
}
305
 
306
/*******************************************************************************
307
* Function Name  : I2C_DMACmd
308
* Description    : Enables or disables the specified I2C DMA requests.
309
* Input          : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
310
*                  - NewState: new state of the I2C DMA transfer.
311
*                    This parameter can be: ENABLE or DISABLE.
312
* Output         : None
313
* Return         : None
314
*******************************************************************************/
315
void I2C_DMACmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
316
{
317
  /* Check the parameters */
318
  assert_param(IS_I2C_ALL_PERIPH(I2Cx));
319
  assert_param(IS_FUNCTIONAL_STATE(NewState));
320
 
321
  if (NewState != DISABLE)
322
  {
323
    /* Enable the selected I2C DMA requests */
324
    I2Cx->CR2 |= CR2_DMAEN_Set;
325
  }
326
  else
327
  {
328
    /* Disable the selected I2C DMA requests */
329
    I2Cx->CR2 &= CR2_DMAEN_Reset;
330
  }
331
}
332
 
333
/*******************************************************************************
334
* Function Name  : I2C_DMALastTransferCmd
335
* Description    : Specifies that the next DMA transfer is the last one.
336
* Input          : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
337
*                  - NewState: new state of the I2C DMA last transfer.
338
*                    This parameter can be: ENABLE or DISABLE.
339
* Output         : None
340
* Return         : None
341
*******************************************************************************/
342
void I2C_DMALastTransferCmd(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
 
348
  if (NewState != DISABLE)
349
  {
350
    /* Next DMA transfer is the last transfer */
351
    I2Cx->CR2 |= CR2_LAST_Set;
352
  }
353
  else
354
  {
355
    /* Next DMA transfer is not the last transfer */
356
    I2Cx->CR2 &= CR2_LAST_Reset;
357
  }
358
}
359
 
360
/*******************************************************************************
361
* Function Name  : I2C_GenerateSTART
362
* Description    : Generates I2Cx communication START condition.
363
* Input          : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
364
*                  - NewState: new state of the I2C START condition generation.
365
*                    This parameter can be: ENABLE or DISABLE.
366
* Output         : None
367
* Return         : None.
368
*******************************************************************************/
369
void I2C_GenerateSTART(I2C_TypeDef* I2Cx, FunctionalState NewState)
370
{
371
  /* Check the parameters */
372
  assert_param(IS_I2C_ALL_PERIPH(I2Cx));
373
  assert_param(IS_FUNCTIONAL_STATE(NewState));
374
 
375
  if (NewState != DISABLE)
376
  {
377
    /* Generate a START condition */
378
    I2Cx->CR1 |= CR1_START_Set;
379
  }
380
  else
381
  {
382
    /* Disable the START condition generation */
383
    I2Cx->CR1 &= CR1_START_Reset;
384
  }
385
}
386
 
387
/*******************************************************************************
388
* Function Name  : I2C_GenerateSTOP
389
* Description    : Generates I2Cx communication STOP condition.
390
* Input          : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
391
*                  - NewState: new state of the I2C STOP condition generation.
392
*                    This parameter can be: ENABLE or DISABLE.
393
* Output         : None
394
* Return         : None.
395
*******************************************************************************/
396
void I2C_GenerateSTOP(I2C_TypeDef* I2Cx, FunctionalState NewState)
397
{
398
  /* Check the parameters */
399
  assert_param(IS_I2C_ALL_PERIPH(I2Cx));
400
  assert_param(IS_FUNCTIONAL_STATE(NewState));
401
 
402
  if (NewState != DISABLE)
403
  {
404
    /* Generate a STOP condition */
405
    I2Cx->CR1 |= CR1_STOP_Set;
406
  }
407
  else
408
  {
409
    /* Disable the STOP condition generation */
410
    I2Cx->CR1 &= CR1_STOP_Reset;
411
  }
412
}
413
 
414
/*******************************************************************************
415
* Function Name  : I2C_AcknowledgeConfig
416
* Description    : Enables or disables the specified I2C acknowledge feature.
417
* Input          : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
418
*                  - NewState: new state of the I2C Acknowledgement.
419
*                    This parameter can be: ENABLE or DISABLE.
420
* Output         : None
421
* Return         : None.
422
*******************************************************************************/
423
void I2C_AcknowledgeConfig(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
 
429
  if (NewState != DISABLE)
430
  {
431
    /* Enable the acknowledgement */
432
    I2Cx->CR1 |= CR1_ACK_Set;
433
  }
434
  else
435
  {
436
    /* Disable the acknowledgement */
437
    I2Cx->CR1 &= CR1_ACK_Reset;
438
  }
439
}
440
 
441
/*******************************************************************************
442
* Function Name  : I2C_OwnAddress2Config
443
* Description    : Configures the specified I2C own address2.
444
* Input          : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
445
*                  - Address: specifies the 7bit I2C own address2.
446
* Output         : None
447
* Return         : None.
448
*******************************************************************************/
449
void I2C_OwnAddress2Config(I2C_TypeDef* I2Cx, u8 Address)
450
{
451
  u16 tmpreg = 0;
452
 
453
  /* Check the parameters */
454
  assert_param(IS_I2C_ALL_PERIPH(I2Cx));
455
 
456
  /* Get the old register value */
457
  tmpreg = I2Cx->OAR2;
458
  /* Reset I2Cx Own address2 bit [7:1] */
459
  tmpreg &= OAR2_ADD2_Reset;
460
  /* Set I2Cx Own address2 */
461
  tmpreg |= (u16)(Address & (u16)0x00FE);
462
  /* Store the new register value */
463
  I2Cx->OAR2 = tmpreg;
464
}
465
 
466
/*******************************************************************************
467
* Function Name  : I2C_DualAddressCmd
468
* Description    : Enables or disables the specified I2C dual addressing mode.
469
* Input          : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
470
*                  - NewState: new state of the I2C dual addressing mode.
471
*                    This parameter can be: ENABLE or DISABLE.
472
* Output         : None
473
* Return         : None
474
*******************************************************************************/
475
void I2C_DualAddressCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
476
{
477
  /* Check the parameters */
478
  assert_param(IS_I2C_ALL_PERIPH(I2Cx));
479
  assert_param(IS_FUNCTIONAL_STATE(NewState));
480
 
481
  if (NewState != DISABLE)
482
  {
483
    /* Enable dual addressing mode */
484
    I2Cx->OAR2 |= OAR2_ENDUAL_Set;
485
  }
486
  else
487
  {
488
    /* Disable dual addressing mode */
489
    I2Cx->OAR2 &= OAR2_ENDUAL_Reset;
490
  }
491
}
492
 
493
/*******************************************************************************
494
* Function Name  : I2C_GeneralCallCmd
495
* Description    : Enables or disables the specified I2C general call feature.
496
* Input          : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
497
*                  - NewState: new state of the I2C General call.
498
*                    This parameter can be: ENABLE or DISABLE.
499
* Output         : None
500
* Return         : None
501
*******************************************************************************/
502
void I2C_GeneralCallCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
503
{
504
  /* Check the parameters */
505
  assert_param(IS_I2C_ALL_PERIPH(I2Cx));
506
  assert_param(IS_FUNCTIONAL_STATE(NewState));
507
 
508
  if (NewState != DISABLE)
509
  {
510
    /* Enable generall call */
511
    I2Cx->CR1 |= CR1_ENGC_Set;
512
  }
513
  else
514
  {
515
    /* Disable generall call */
516
    I2Cx->CR1 &= CR1_ENGC_Reset;
517
  }
518
}
519
 
520
/*******************************************************************************
521
* Function Name  : I2C_ITConfig
522
* Description    : Enables or disables the specified I2C interrupts.
523
* Input          : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
524
*                  - I2C_IT: specifies the I2C interrupts sources to be enabled
525
*                    or disabled.
526
*                    This parameter can be any combination of the following values:
527
*                       - I2C_IT_BUF: Buffer interrupt mask
528
*                       - I2C_IT_EVT: Event interrupt mask
529
*                       - I2C_IT_ERR: Error interrupt mask
530
*                  - NewState: new state of the specified I2C interrupts.
531
*                    This parameter can be: ENABLE or DISABLE.
532
* Output         : None
533
* Return         : None
534
*******************************************************************************/
535
void I2C_ITConfig(I2C_TypeDef* I2Cx, u16 I2C_IT, FunctionalState NewState)
536
{
537
  /* Check the parameters */
538
  assert_param(IS_I2C_ALL_PERIPH(I2Cx));
539
  assert_param(IS_FUNCTIONAL_STATE(NewState));
540
  assert_param(IS_I2C_CONFIG_IT(I2C_IT));
541
 
542
  if (NewState != DISABLE)
543
  {
544
    /* Enable the selected I2C interrupts */
545
    I2Cx->CR2 |= I2C_IT;
546
  }
547
  else
548
  {
549
    /* Disable the selected I2C interrupts */
550
    I2Cx->CR2 &= (u16)~I2C_IT;
551
  }
552
}
553
 
554
/*******************************************************************************
555
* Function Name  : I2C_SendData
556
* Description    : Sends a data byte through the I2Cx peripheral.
557
* Input          : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
558
*                  - Data: Byte to be transmitted..
559
* Output         : None
560
* Return         : None
561
*******************************************************************************/
562
void I2C_SendData(I2C_TypeDef* I2Cx, u8 Data)
563
{
564
  /* Check the parameters */
565
  assert_param(IS_I2C_ALL_PERIPH(I2Cx));
566
 
567
  /* Write in the DR register the data to be sent */
568
  I2Cx->DR = Data;
569
}
570
 
571
/*******************************************************************************
572
* Function Name  : I2C_ReceiveData
573
* Description    : Returns the most recent received data by the I2Cx peripheral.
574
* Input          : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
575
* Output         : None
576
* Return         : The value of the received data.
577
*******************************************************************************/
578
u8 I2C_ReceiveData(I2C_TypeDef* I2Cx)
579
{
580
  /* Check the parameters */
581
  assert_param(IS_I2C_ALL_PERIPH(I2Cx));
582
 
583
  /* Return the data in the DR register */
584
  return (u8)I2Cx->DR;
585
}
586
 
587
/*******************************************************************************
588
* Function Name  : I2C_Send7bitAddress
589
* Description    : Transmits the address byte to select the slave device.
590
* Input          : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
591
*                  - Address: specifies the slave address which will be transmitted
592
*                  - I2C_Direction: specifies whether the I2C device will be a
593
*                    Transmitter or a Receiver.
594
*                    This parameter can be one of the following values
595
*                       - I2C_Direction_Transmitter: Transmitter mode
596
*                       - I2C_Direction_Receiver: Receiver mode
597
* Output         : None
598
* Return         : None.
599
*******************************************************************************/
600
void I2C_Send7bitAddress(I2C_TypeDef* I2Cx, u8 Address, u8 I2C_Direction)
601
{
602
  /* Check the parameters */
603
  assert_param(IS_I2C_ALL_PERIPH(I2Cx));
604
  assert_param(IS_I2C_DIRECTION(I2C_Direction));
605
 
606
  /* Test on the direction to set/reset the read/write bit */
607
  if (I2C_Direction != I2C_Direction_Transmitter)
608
  {
609
    /* Set the address bit0 for read */
610
    Address |= OAR1_ADD0_Set;
611
  }
612
  else
613
  {
614
    /* Reset the address bit0 for write */
615
    Address &= OAR1_ADD0_Reset;
616
  }
617
  /* Send the address */
618
  I2Cx->DR = Address;
619
}
620
 
621
/*******************************************************************************
622
* Function Name  : I2C_ReadRegister
623
* Description    : Reads the specified I2C register and returns its value.
624
* Input1         : - I2C_Register: specifies the register to read.
625
*                    This parameter can be one of the following values:
626
*                       - I2C_Register_CR1:  CR1 register.
627
*                       - I2C_Register_CR2:   CR2 register.
628
*                       - I2C_Register_OAR1:  OAR1 register.
629
*                       - I2C_Register_OAR2:  OAR2 register.
630
*                       - I2C_Register_DR:    DR register.
631
*                       - I2C_Register_SR1:   SR1 register.
632
*                       - I2C_Register_SR2:   SR2 register.
633
*                       - I2C_Register_CCR:   CCR register.
634
*                       - I2C_Register_TRISE: TRISE register.
635
* Output         : None
636
* Return         : The value of the read register.
637
*******************************************************************************/
638
u16 I2C_ReadRegister(I2C_TypeDef* I2Cx, u8 I2C_Register)
639
{
640
  /* Check the parameters */
641
  assert_param(IS_I2C_ALL_PERIPH(I2Cx));
642
  assert_param(IS_I2C_REGISTER(I2C_Register));
643
 
644
  /* Return the selected register value */
645
  return (*(vu16 *)(*((vu32 *)&I2Cx) + I2C_Register));
646
}
647
 
648
/*******************************************************************************
649
* Function Name  : I2C_SoftwareResetCmd
650
* Description    : Enables or disables the specified I2C software reset.
651
* Input          : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
652
*                  - NewState: new state of the I2C software reset.
653
*                    This parameter can be: ENABLE or DISABLE.
654
* Output         : None
655
* Return         : None
656
*******************************************************************************/
657
void I2C_SoftwareResetCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
658
{
659
  /* Check the parameters */
660
  assert_param(IS_I2C_ALL_PERIPH(I2Cx));
661
  assert_param(IS_FUNCTIONAL_STATE(NewState));
662
 
663
  if (NewState != DISABLE)
664
  {
665
    /* Peripheral under reset */
666
    I2Cx->CR1 |= CR1_SWRST_Set;
667
  }
668
  else
669
  {
670
    /* Peripheral not under reset */
671
    I2Cx->CR1 &= CR1_SWRST_Reset;
672
  }
673
}
674
 
675
/*******************************************************************************
676
* Function Name  : I2C_SMBusAlertConfig
677
* Description    : Drives the SMBusAlert pin high or low for the specified I2C.
678
* Input          : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
679
*                  - I2C_SMBusAlert: specifies SMBAlert pin level.
680
*                    This parameter can be one of the following values:
681
*                       - I2C_SMBusAlert_Low: SMBAlert pin driven low
682
*                       - I2C_SMBusAlert_High: SMBAlert pin driven high
683
* Output         : None
684
* Return         : None
685
*******************************************************************************/
686
void I2C_SMBusAlertConfig(I2C_TypeDef* I2Cx, u16 I2C_SMBusAlert)
687
{
688
  /* Check the parameters */
689
  assert_param(IS_I2C_ALL_PERIPH(I2Cx));
690
  assert_param(IS_I2C_SMBUS_ALERT(I2C_SMBusAlert));
691
 
692
  if (I2C_SMBusAlert == I2C_SMBusAlert_Low)
693
  {
694
    /* Drive the SMBusAlert pin Low */
695
    I2Cx->CR1 |= I2C_SMBusAlert_Low;
696
  }
697
  else
698
  {
699
    /* Drive the SMBusAlert pin High  */
700
    I2Cx->CR1 &= I2C_SMBusAlert_High;
701
  }
702
}
703
 
704
/*******************************************************************************
705
* Function Name  : I2C_TransmitPEC
706
* Description    : Enables or disables the specified I2C PEC transfer.
707
* Input          : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
708
*                  - NewState: new state of the I2C PEC transmission.
709
*                    This parameter can be: ENABLE or DISABLE.
710
* Output         : None
711
* Return         : None
712
*******************************************************************************/
713
void I2C_TransmitPEC(I2C_TypeDef* I2Cx, FunctionalState NewState)
714
{
715
  /* Check the parameters */
716
  assert_param(IS_I2C_ALL_PERIPH(I2Cx));
717
  assert_param(IS_FUNCTIONAL_STATE(NewState));
718
 
719
  if (NewState != DISABLE)
720
  {
721
    /* Enable the selected I2C PEC transmission */
722
    I2Cx->CR1 |= CR1_PEC_Set;
723
  }
724
  else
725
  {
726
    /* Disable the selected I2C PEC transmission */
727
    I2Cx->CR1 &= CR1_PEC_Reset;
728
  }
729
}
730
 
731
/*******************************************************************************
732
* Function Name  : I2C_PECPositionConfig
733
* Description    : Selects the specified I2C PEC position.
734
* Input          : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
735
*                  - I2C_PECPosition: specifies the PEC position.
736
*                    This parameter can be one of the following values:
737
*                       - I2C_PECPosition_Next: indicates that the next
738
*                         byte is PEC
739
*                       - I2C_PECPosition_Current: indicates that current
740
*                         byte is PEC
741
* Output         : None
742
* Return         : None
743
*******************************************************************************/
744
void I2C_PECPositionConfig(I2C_TypeDef* I2Cx, u16 I2C_PECPosition)
745
{
746
  /* Check the parameters */
747
  assert_param(IS_I2C_ALL_PERIPH(I2Cx));
748
  assert_param(IS_I2C_PEC_POSITION(I2C_PECPosition));
749
 
750
  if (I2C_PECPosition == I2C_PECPosition_Next)
751
  {
752
    /* Next byte in shift register is PEC */
753
    I2Cx->CR1 |= I2C_PECPosition_Next;
754
  }
755
  else
756
  {
757
    /* Current byte in shift register is PEC */
758
    I2Cx->CR1 &= I2C_PECPosition_Current;
759
  }
760
}
761
 
762
/*******************************************************************************
763
* Function Name  : I2C_CalculatePEC
764
* Description    : Enables or disables the PEC value calculation of the
765
*                  transfered bytes.
766
* Input          : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
767
*                  - NewState: new state of the I2Cx PEC value calculation.
768
*                    This parameter can be: ENABLE or DISABLE.
769
* Output         : None
770
* Return         : None
771
*******************************************************************************/
772
void I2C_CalculatePEC(I2C_TypeDef* I2Cx, FunctionalState NewState)
773
{
774
  /* Check the parameters */
775
  assert_param(IS_I2C_ALL_PERIPH(I2Cx));
776
  assert_param(IS_FUNCTIONAL_STATE(NewState));
777
 
778
  if (NewState != DISABLE)
779
  {
780
    /* Enable the selected I2C PEC calculation */
781
    I2Cx->CR1 |= CR1_ENPEC_Set;
782
  }
783
  else
784
  {
785
    /* Disable the selected I2C PEC calculation */
786
    I2Cx->CR1 &= CR1_ENPEC_Reset;
787
  }
788
}
789
 
790
/*******************************************************************************
791
* Function Name  : I2C_GetPEC
792
* Description    : Returns the PEC value for the specified I2C.
793
* Input          : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
794
* Output         : None
795
* Return         : The PEC value.
796
*******************************************************************************/
797
u8 I2C_GetPEC(I2C_TypeDef* I2Cx)
798
{
799
  /* Check the parameters */
800
  assert_param(IS_I2C_ALL_PERIPH(I2Cx));
801
 
802
  /* Return the selected I2C PEC value */
803
  return ((I2Cx->SR2) >> 8);
804
}
805
 
806
/*******************************************************************************
807
* Function Name  : I2C_ARPCmd
808
* Description    : Enables or disables the specified I2C ARP.
809
* Input          : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
810
*                  - NewState: new state of the I2Cx ARP.
811
*                    This parameter can be: ENABLE or DISABLE.
812
* Output         : None
813
* Return         : None
814
*******************************************************************************/
815
void I2C_ARPCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
816
{
817
  /* Check the parameters */
818
  assert_param(IS_I2C_ALL_PERIPH(I2Cx));
819
  assert_param(IS_FUNCTIONAL_STATE(NewState));
820
 
821
  if (NewState != DISABLE)
822
  {
823
    /* Enable the selected I2C ARP */
824
    I2Cx->CR1 |= CR1_ENARP_Set;
825
  }
826
  else
827
  {
828
    /* Disable the selected I2C ARP */
829
    I2Cx->CR1 &= CR1_ENARP_Reset;
830
  }
831
}
832
 
833
/*******************************************************************************
834
* Function Name  : I2C_StretchClockCmd
835
* Description    : Enables or disables the specified I2C Clock stretching.
836
* Input          : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
837
*                  - NewState: new state of the I2Cx Clock stretching.
838
*                    This parameter can be: ENABLE or DISABLE.
839
* Output         : None
840
* Return         : None
841
*******************************************************************************/
842
void I2C_StretchClockCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
843
{
844
  /* Check the parameters */
845
  assert_param(IS_I2C_ALL_PERIPH(I2Cx));
846
  assert_param(IS_FUNCTIONAL_STATE(NewState));
847
 
848
  if (NewState == DISABLE)
849
  {
850
    /* Enable the selected I2C Clock stretching */
851
    I2Cx->CR1 |= CR1_NOSTRETCH_Set;
852
  }
853
  else
854
  {
855
    /* Disable the selected I2C Clock stretching */
856
    I2Cx->CR1 &= CR1_NOSTRETCH_Reset;
857
  }
858
}
859
 
860
/*******************************************************************************
861
* Function Name  : I2C_FastModeDutyCycleConfig
862
* Description    : Selects the specified I2C fast mode duty cycle.
863
* Input          : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
864
*                  - I2C_DutyCycle: specifies the fast mode duty cycle.
865
*                    This parameter can be one of the following values:
866
*                       - I2C_DutyCycle_2: I2C fast mode Tlow/Thigh = 2
867
*                       - I2C_DutyCycle_16_9: I2C fast mode Tlow/Thigh = 16/9
868
* Output         : None
869
* Return         : None
870
*******************************************************************************/
871
void I2C_FastModeDutyCycleConfig(I2C_TypeDef* I2Cx, u16 I2C_DutyCycle)
872
{
873
  /* Check the parameters */
874
  assert_param(IS_I2C_ALL_PERIPH(I2Cx));
875
  assert_param(IS_I2C_DUTY_CYCLE(I2C_DutyCycle));
876
 
877
  if (I2C_DutyCycle != I2C_DutyCycle_16_9)
878
  {
879
    /* I2C fast mode Tlow/Thigh=2 */
880
    I2Cx->CCR &= I2C_DutyCycle_2;
881
  }
882
  else
883
  {
884
    /* I2C fast mode Tlow/Thigh=16/9 */
885
    I2Cx->CCR |= I2C_DutyCycle_16_9;
886
  }
887
}
888
 
889
/*******************************************************************************
890
* Function Name  : I2C_GetLastEvent
891
* Description    : Returns the last I2Cx Event.
892
* Input          : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
893
* Output         : None
894
* Return         : The last event
895
*******************************************************************************/
896
u32 I2C_GetLastEvent(I2C_TypeDef* I2Cx)
897
{
898
  u32 lastevent = 0;
899
  u32 flag1 = 0, flag2 = 0;
900
 
901
  /* Check the parameters */
902
  assert_param(IS_I2C_ALL_PERIPH(I2Cx));
903
 
904
  /* Read the I2Cx status register */
905
  flag1 = I2Cx->SR1;
906
  flag2 = I2Cx->SR2;
907
  flag2 = flag2 << 16;
908
 
909
  /* Get the last event value from I2C status register */
910
  lastevent = (flag1 | flag2) & FLAG_Mask;
911
 
912
  /* Return status */
913
  return lastevent;
914
}
915
 
916
/*******************************************************************************
917
* Function Name  : I2C_CheckEvent
918
* Description    : Checks whether the last I2Cx Event is equal to the one passed
919
*                  as parameter.
920
* Input          : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
921
*                  - I2C_EVENT: specifies the event to be checked.
922
*                    This parameter can be one of the following values:
923
*                       - I2C_EVENT_SLAVE_ADDRESS_MATCHED   : EV1
924
*                       - I2C_EVENT_SLAVE_BYTE_RECEIVED     : EV2
925
*                       - I2C_EVENT_SLAVE_BYTE_TRANSMITTED  : EV3
926
*                       - I2C_EVENT_SLAVE_ACK_FAILURE       : EV3-2
927
*                       - I2C_EVENT_MASTER_MODE_SELECT      : EV5
928
*                       - I2C_EVENT_MASTER_MODE_SELECTED    : EV6
929
*                       - I2C_EVENT_MASTER_BYTE_RECEIVED    : EV7
930
*                       - I2C_EVENT_MASTER_BYTE_TRANSMITTED : EV8
931
*                       - I2C_EVENT_MASTER_MODE_ADDRESS10   : EV9
932
*                       - I2C_EVENT_SLAVE_STOP_DETECTED     : EV4
933
* Output         : None
934
* Return         : An ErrorStatus enumuration value:
935
*                       - SUCCESS: Last event is equal to the I2C_EVENT
936
*                       - ERROR: Last event is different from the I2C_EVENT
937
*******************************************************************************/
938
ErrorStatus I2C_CheckEvent(I2C_TypeDef* I2Cx, u32 I2C_EVENT)
939
{
940
  u32 lastevent = 0;
941
  u32 flag1 = 0, flag2 = 0;
942
  ErrorStatus status = ERROR;
943
 
944
  /* Check the parameters */
945
  assert_param(IS_I2C_ALL_PERIPH(I2Cx));
946
  assert_param(IS_I2C_EVENT(I2C_EVENT));
947
 
948
  /* Read the I2Cx status register */
949
  flag1 = I2Cx->SR1;
950
  flag2 = I2Cx->SR2;
951
  flag2 = flag2 << 16;
952
 
953
  /* Get the last event value from I2C status register */
954
  lastevent = (flag1 | flag2) & FLAG_Mask;
955
 
956
  /* Check whether the last event is equal to I2C_EVENT */
957
  if (lastevent == I2C_EVENT )
958
  {
959
    /* SUCCESS: last event is equal to I2C_EVENT */
960
    status = SUCCESS;
961
  }
962
  else
963
  {
964
    /* ERROR: last event is different from I2C_EVENT */
965
    status = ERROR;
966
  }
967
 
968
  /* Return status */
969
  return status;
970
}
971
 
972
/*******************************************************************************
973
* Function Name  : I2C_GetFlagStatus
974
* Description    : Checks whether the specified I2C flag is set or not.
975
* Input          : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
976
*                  - I2C_FLAG: specifies the flag to check.
977
*                    This parameter can be one of the following values:
978
*                       - I2C_FLAG_DUALF: Dual flag (Slave mode)
979
*                       - I2C_FLAG_SMBHOST: SMBus host header (Slave mode)
980
*                       - I2C_FLAG_SMBDEFAULT: SMBus default header (Slave mode)
981
*                       - I2C_FLAG_GENCALL: General call header flag (Slave mode)
982
*                       - I2C_FLAG_TRA: Transmitter/Receiver flag
983
*                       - I2C_FLAG_BUSY: Bus busy flag
984
*                       - I2C_FLAG_MSL: Master/Slave flag
985
*                       - I2C_FLAG_SMBALERT: SMBus Alert flag
986
*                       - I2C_FLAG_TIMEOUT: Timeout or Tlow error flag
987
*                       - I2C_FLAG_PECERR: PEC error in reception flag
988
*                       - I2C_FLAG_OVR: Overrun/Underrun flag (Slave mode)
989
*                       - I2C_FLAG_AF: Acknowledge failure flag
990
*                       - I2C_FLAG_ARLO: Arbitration lost flag (Master mode)
991
*                       - I2C_FLAG_BERR: Bus error flag
992
*                       - I2C_FLAG_TXE: Data register empty flag (Transmitter)
993
*                       - I2C_FLAG_RXNE: Data register not empty (Receiver) flag
994
*                       - I2C_FLAG_STOPF: Stop detection flag (Slave mode)
995
*                       - I2C_FLAG_ADD10: 10-bit header sent flag (Master mode)
996
*                       - I2C_FLAG_BTF: Byte transfer finished flag
997
*                       - I2C_FLAG_ADDR: Address sent flag (Master mode) “ADSL”
998
*                                        Address matched flag (Slave mode)”ENDAD”
999
*                       - I2C_FLAG_SB: Start bit flag (Master mode)
1000
* Output         : None
1001
* Return         : The new state of I2C_FLAG (SET or RESET).
1002
*******************************************************************************/
1003
FlagStatus I2C_GetFlagStatus(I2C_TypeDef* I2Cx, u32 I2C_FLAG)
1004
{
1005
  FlagStatus bitstatus = RESET;
1006
  u32 i2cstatus = 0;
1007
  u32 flag1 = 0, flag2 = 0;
1008
 
1009
  /* Check the parameters */
1010
  assert_param(IS_I2C_ALL_PERIPH(I2Cx));
1011
  assert_param(IS_I2C_GET_FLAG(I2C_FLAG));
1012
 
1013
  /* Read the I2Cx status register */
1014
  flag1 = I2Cx->SR1;
1015
  flag2 = I2Cx->SR2;
1016
  flag2 = (flag2 & FLAG_Mask) << 16;
1017
 
1018
  /* Get the I2C status value */
1019
  i2cstatus = flag1 | flag2;
1020
 
1021
  /* Get bit[23:0] of the flag */
1022
  I2C_FLAG &= FLAG_Mask;
1023
 
1024
  /* Check the status of the specified I2C flag */
1025
  if ((i2cstatus & I2C_FLAG) != (u32)RESET)
1026
  {
1027
    /* I2C_FLAG is set */
1028
    bitstatus = SET;
1029
  }
1030
  else
1031
  {
1032
    /* I2C_FLAG is reset */
1033
    bitstatus = RESET;
1034
  }
1035
  /* Return the I2C_FLAG status */
1036
  return  bitstatus;
1037
}
1038
 
1039
/*******************************************************************************
1040
* Function Name  : I2C_ClearFlag
1041
* Description    : Clears the I2Cx's pending flags.
1042
* Input          : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
1043
*                  - I2C_FLAG: specifies the flag to clear.
1044
*                    This parameter can be one of the following values:
1045
*                       - I2C_FLAG_SMBALERT: SMBus Alert flag
1046
*                       - I2C_FLAG_TIMEOUT: Timeout or Tlow error flag
1047
*                       - I2C_FLAG_PECERR: PEC error in reception flag
1048
*                       - I2C_FLAG_OVR: Overrun/Underrun flag (Slave mode)
1049
*                       - I2C_FLAG_AF: Acknowledge failure flag
1050
*                       - I2C_FLAG_ARLO: Arbitration lost flag (Master mode)
1051
*                       - I2C_FLAG_BERR: Bus error flag
1052
*                       - I2C_FLAG_STOPF: Stop detection flag (Slave mode)
1053
*                       - I2C_FLAG_ADD10: 10-bit header sent flag (Master mode)
1054
*                       - I2C_FLAG_BTF: Byte transfer finished flag
1055
*                       - I2C_FLAG_ADDR: Address sent flag (Master mode) “ADSL”
1056
*                                        Address matched flag (Slave mode)”ENDAD”
1057
*                       - I2C_FLAG_SB: Start bit flag (Master mode)
1058
* Output         : None
1059
* Return         : None
1060
*******************************************************************************/
1061
void I2C_ClearFlag(I2C_TypeDef* I2Cx, u32 I2C_FLAG)
1062
{
1063
  u32 flagpos = 0;
1064
  u32 flagindex = 0;
1065
 
1066
  /* Check the parameters */
1067
  assert_param(IS_I2C_ALL_PERIPH(I2Cx));
1068
  assert_param(IS_I2C_CLEAR_FLAG(I2C_FLAG));
1069
 
1070
  /* Get the I2C flag position */
1071
  flagpos = I2C_FLAG & FLAG_Mask;
1072
 
1073
  /* Get the I2C flag index */
1074
  flagindex = I2C_FLAG >> 28;
1075
 
1076
  /* Clear the flag by writing 0 */
1077
  if (flagindex == 1)
1078
  {
1079
    /* Clear the selected I2C flag */
1080
    I2Cx->SR1 = (u16)~flagpos;
1081
  }
1082
  /* Flags that need a read of the SR1 register to be cleared */
1083
  else if (flagindex == 2)
1084
  {
1085
    /* Read the SR1 register */
1086
    (void)I2Cx->SR1;
1087
  }
1088
  /* Flags that need a read of SR1 and a write on CR1 registers to be cleared */
1089
  else if (flagindex == 6)
1090
  {
1091
    /* Read the SR1 register */
1092
    (void)I2Cx->SR1;
1093
 
1094
    /* Write on the CR1 register */
1095
    I2Cx->CR1 |= CR1_PE_Set;
1096
  }
1097
  /* Flags that need a read of SR1 and SR2 registers to be cleared */
1098
  else /*flagindex == 0xA*/
1099
  {
1100
    /* Read the SR1 register */
1101
    (void)I2Cx->SR1;
1102
 
1103
    /* Read the SR2 register */
1104
    (void)I2Cx->SR2;
1105
  }
1106
}
1107
 
1108
/*******************************************************************************
1109
* Function Name  : I2C_GetITStatus
1110
* Description    : Checks whether the specified I2C interrupt has occurred or not.
1111
* Input          : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
1112
*                  - I2C_IT: specifies the interrupt source to check.
1113
*                    This parameter can be one of the following values:
1114
*                       - I2C_IT_SMBALERT: SMBus Alert flag
1115
*                       - I2C_IT_TIMEOUT: Timeout or Tlow error flag
1116
*                       - I2C_IT_PECERR: PEC error in reception flag
1117
*                       - I2C_IT_OVR: Overrun/Underrun flag (Slave mode)
1118
*                       - I2C_IT_AF: Acknowledge failure flag
1119
*                       - I2C_IT_ARLO: Arbitration lost flag (Master mode)
1120
*                       - I2C_IT_BERR: Bus error flag
1121
*                       - I2C_IT_TXE: Data register empty flag (Transmitter)
1122
*                       - I2C_IT_RXNE: Data register not empty (Receiver) flag
1123
*                       - I2C_IT_STOPF: Stop detection flag (Slave mode)
1124
*                       - I2C_IT_ADD10: 10-bit header sent flag (Master mode)
1125
*                       - I2C_IT_BTF: Byte transfer finished flag
1126
*                       - I2C_IT_ADDR: Address sent flag (Master mode) “ADSL”
1127
*                                        Address matched flag (Slave mode)”ENDAD”
1128
*                       - I2C_IT_SB: Start bit flag (Master mode)
1129
* Output         : None
1130
* Return         : The new state of I2C_IT (SET or RESET).
1131
*******************************************************************************/
1132
ITStatus I2C_GetITStatus(I2C_TypeDef* I2Cx, u32 I2C_IT)
1133
{
1134
  ITStatus bitstatus = RESET;
1135
  u32 i2cstatus = 0;
1136
  u32 flag1 = 0, flag2 = 0;
1137
 
1138
  /* Check the parameters */
1139
  assert_param(IS_I2C_ALL_PERIPH(I2Cx));
1140
  assert_param(IS_I2C_GET_IT(I2C_IT));
1141
 
1142
  /* Read the I2Cx status register */
1143
  flag1 = I2Cx->SR1;
1144
  flag2 = I2Cx->SR2;
1145
  flag2 = (flag2 & FLAG_Mask) << 16;
1146
 
1147
  /* Get the I2C status value */
1148
  i2cstatus = flag1 | flag2;
1149
 
1150
  /* Get bit[23:0] of the flag */
1151
  I2C_IT &= FLAG_Mask;
1152
 
1153
  /* Check the status of the specified I2C flag */
1154
  if ((i2cstatus & I2C_IT) != (u32)RESET)
1155
  {
1156
    /* I2C_IT is set */
1157
    bitstatus = SET;
1158
  }
1159
  else
1160
  {
1161
    /* I2C_IT is reset */
1162
    bitstatus = RESET;
1163
  }
1164
  /* Return the I2C_IT status */
1165
  return  bitstatus;
1166
}
1167
 
1168
/*******************************************************************************
1169
* Function Name  : I2C_ClearITPendingBit
1170
* Description    : Clears the I2Cx’s interrupt pending bits.
1171
* Input          : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
1172
*                  - I2C_IT: specifies the interrupt pending bit to clear.
1173
*                    This parameter can be one of the following values:
1174
*                       - I2C_IT_SMBALERT: SMBus Alert flag
1175
*                       - I2C_IT_TIMEOUT: Timeout or Tlow error flag
1176
*                       - I2C_IT_PECERR: PEC error in reception flag
1177
*                       - I2C_IT_OVR: Overrun/Underrun flag (Slave mode)
1178
*                       - I2C_IT_AF: Acknowledge failure flag
1179
*                       - I2C_IT_ARLO: Arbitration lost flag (Master mode)
1180
*                       - I2C_IT_BERR: Bus error flag
1181
*                       - I2C_IT_STOPF: Stop detection flag (Slave mode)
1182
*                       - I2C_IT_ADD10: 10-bit header sent flag (Master mode)
1183
*                       - I2C_IT_BTF: Byte transfer finished flag
1184
*                       - I2C_IT_ADDR: Address sent flag (Master mode) “ADSL”
1185
*                                        Address matched flag (Slave mode)”ENDAD”
1186
*                       - I2C_IT_SB: Start bit flag (Master mode)
1187
* Output         : None
1188
* Return         : None
1189
*******************************************************************************/
1190
void I2C_ClearITPendingBit(I2C_TypeDef* I2Cx, u32 I2C_IT)
1191
{
1192
  u32 flagpos = 0;
1193
  u32 flagindex = 0;
1194
 
1195
  /* Check the parameters */
1196
  assert_param(IS_I2C_ALL_PERIPH(I2Cx));
1197
  assert_param(IS_I2C_CLEAR_IT(I2C_IT));
1198
 
1199
  /* Get the I2C flag position */
1200
  flagpos = I2C_IT & FLAG_Mask;
1201
 
1202
  /* Get the I2C flag index */
1203
  flagindex = I2C_IT >> 28;
1204
 
1205
  /* Clear the flag by writing 0 */
1206
  if (flagindex == 1)
1207
  {
1208
    /* Clear the selected I2C flag */
1209
    I2Cx->SR1 = (u16)~flagpos;
1210
  }
1211
  /* Flags that need a read of the SR1 register to be cleared */
1212
  else if (flagindex == 2)
1213
  {
1214
    /* Read the SR1 register */
1215
    (void)I2Cx->SR1;
1216
  }
1217
  /* Flags that need a read of SR1 and a write on CR1 registers to be cleared */
1218
  else if (flagindex == 6)
1219
  {
1220
    /* Read the SR1 register */
1221
    (void)I2Cx->SR1;
1222
 
1223
    /* Write on the CR1 register */
1224
    I2Cx->CR1 |= CR1_PE_Set;
1225
  }
1226
  /* Flags that need a read of SR1 and SR2 registers to be cleared */
1227
  else /*flagindex == 0xA*/
1228
  {
1229
    /* Read the SR1 register */
1230
    (void)I2Cx->SR1;
1231
 
1232
    /* Read the SR2 register */
1233
    (void)I2Cx->SR2;
1234
  }
1235
}
1236
 
1237
/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.