summaryrefslogtreecommitdiff
path: root/bsp/nv32f100x/lib/src/i2c.c
blob: 65f7616ef5148ee1a9212a2ef69d52252f1228ce (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
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
/******************************************************************************
*
* @brief providing APIs for configuring I2C module (I2C). 
*
*******************************************************************************
*
* provide APIs for configuring I2C module (I2C).
******************************************************************************/
#include "common.h"
#include "i2c.h"

/******************************************************************************
* Global variables
******************************************************************************/

/******************************************************************************
* Constants and macros
******************************************************************************/

/******************************************************************************
* Local types
******************************************************************************/

/******************************************************************************
* Local function prototypes
******************************************************************************/

/******************************************************************************
* Local variables
******************************************************************************/
static I2C_CallbackType I2C_Callback[2] = {(I2C_CallbackType)NULL};
/******************************************************************************
* Local functions
******************************************************************************/
void I2C0_Isr( void );

/******************************************************************************
* Global functions
******************************************************************************/


/******************************************************************************
* define I2C APIs
*
*//*! @addtogroup i2c_api_list
* @{
*******************************************************************************/
/*****************************************************************************//*!
   *
   * @brief Initialize I2C module.
   *        
   * @param[in] pI2Cx      point to I2C module type.
 	 * @param[in] pI2CConfig point to I2C configure structure.
   *
   * @return none
   *
   * @ Pass/ Fail criteria: none
*****************************************************************************/

void I2C_Init(I2C_Type *pI2Cx,I2C_ConfigPtr pI2CConfig)
{
    uint8_t u8Temp;

#if defined(CPU_NV32)
    SIM->SCGC |= SIM_SCGC_IIC_MASK;
#elif defined(CPU_NV32M3)
    SIM->SCGC |= SIM_SCGC_IIC_MASK;
#elif defined(CPU_NV32M4)
    if(pI2Cx == I2C0)
    {
        SIM->SCGC |= SIM_SCGC_I2C0_MASK;
    }
    else
    {
        SIM->SCGC |= SIM_SCGC_I2C1_MASK;
    }
#endif

	I2C_SetBaudRate(pI2Cx,pI2CConfig->u16F);
    I2C_SetSlaveAddress(pI2Cx,pI2CConfig->u16OwnA1);
    pI2Cx->FLT = (uint8_t)pI2CConfig->u16Filt;
    pI2Cx->RA = (uint8_t)pI2CConfig->u16RangeA & 0xfe;
    I2C_SetSCLLowETMeout(pI2Cx,pI2CConfig->u16Slt);

    /* configure C2 control register */
    u8Temp = 0;
    if( pI2CConfig->sSetting.bGCAEn )
    {
        u8Temp |= I2C_C2_GCAEN_MASK;
    }
    if( pI2CConfig->sSetting.bAddressExt )
    {
        u8Temp |= I2C_C2_ADEXT_MASK;
    }
    if( pI2CConfig->sSetting.bRangeAddEn )
    {
        u8Temp |= I2C_C2_RMEN_MASK;
    }
    pI2Cx->C2 |= u8Temp;

    /* configure SMB rehister */
    u8Temp = 0;
    if( pI2CConfig->sSetting.bFackEn )
    {
        u8Temp |= I2C_SMB_FACK_MASK;
    }
    if( pI2CConfig->sSetting.bSMB_AlertEn )
    {
        u8Temp |= I2C_SMB_ALERTEN_MASK;
    }
    if( pI2CConfig->sSetting.bSecondAddressEn )
    {
        u8Temp |= I2C_SMB_SIICAEN_MASK;
    }
    if( pI2CConfig->sSetting.bSHTF2IntEn )
    {
        u8Temp |= I2C_SMB_SHTF2IE_MASK;
    }
    pI2Cx->SMB = u8Temp;
    
    /* configure C1 rehister */
    u8Temp = 0;
    if( pI2CConfig->sSetting.bIntEn )
    {
        u8Temp |= I2C_C1_IICIE_MASK;
        if(pI2Cx == I2C0)
        {
            NVIC_EnableIRQ(I2C0_IRQn);
        }
    #if defined(CPU_NV32M4)     
        else if(pI2Cx == I2C1)
        {
            NVIC_EnableIRQ(I2C1_IRQn);
        }
    #endif    
        else
        {
            //
        }
    }
    if( pI2CConfig->sSetting.bWakeUpEn )
    {
        u8Temp |= I2C_C1_WUEN_MASK;
    }
    if( pI2CConfig->sSetting.bI2CEn )
    {
        u8Temp |= I2C_C1_IICEN_MASK;
    }
    pI2Cx->C1 = u8Temp;


}
/*****************************************************************************//*!
   *
   * @brief send out start signals.
   *        
   * @param[in] pI2Cx      point to I2C module type.
   *
   * @return error status
   *
   * @ Pass/ Fail criteria: none
*****************************************************************************/
uint8_t I2C_Start(I2C_Type *pI2Cx)
{
    uint32_t u32ETMeout;
    uint8_t u8ErrorStatus;
    
    u32ETMeout = 0; 
    u8ErrorStatus = 0x00;

    I2C_TxEnable(pI2Cx);
    pI2Cx->C1 |= I2C_C1_MST_MASK;

    while( (!I2C_IsBusy(pI2Cx)) && ( u32ETMeout < I2C_WAIT_STATUS_ETMEOUT))
     {
        u32ETMeout ++;
     }

     if( u32ETMeout == I2C_WAIT_STATUS_ETMEOUT )
     {
        u8ErrorStatus |= I2C_ERROR_START_NO_BUSY_FLAG;
     }

     return u8ErrorStatus;
}

/*****************************************************************************//*!
   *
   * @brief send out stop signals.
   *        
   * @param[in] pI2Cx      point to I2C module type.
   *
   * @return error status
   *
   * @ Pass/ Fail criteria: none
*****************************************************************************/
uint8_t I2C_Stop(I2C_Type *pI2Cx)
{
    uint32_t u32ETMeout;
    uint8_t u8ErrorStatus;
    
    u32ETMeout = 0; 
    u8ErrorStatus = 0x00;
    
    pI2Cx->C1 &= ~I2C_C1_MST_MASK;

    while( (I2C_IsBusy(pI2Cx) ) && ( u32ETMeout < I2C_WAIT_STATUS_ETMEOUT))
     {
        u32ETMeout ++;
     }

     if( u32ETMeout == I2C_WAIT_STATUS_ETMEOUT )
     {
        u8ErrorStatus |= I2C_ERROR_STOP_BUSY_FLAG;
     }

     return u8ErrorStatus;
}


/*****************************************************************************//*!
   *
   * @brief send out repeat start signals.
   *        
   * @param[in] pI2Cx      point to I2C module type.
   *
   * @return error status.
   *
   * @ Pass/ Fail criteria: none
*****************************************************************************/
uint8_t I2C_RepeatStart(I2C_Type *pI2Cx)
{
    uint32_t u32ETMeout;
    uint8_t u8ErrorStatus;
    
    u32ETMeout = 0; 
    u8ErrorStatus = 0x00;
    
    pI2Cx->C1 |= I2C_C1_RSTA_MASK;

    while( (!I2C_IsBusy(I2C0) ) && ( u32ETMeout < I2C_WAIT_STATUS_ETMEOUT))
     {
        u32ETMeout ++;
     }

     if( u32ETMeout == I2C_WAIT_STATUS_ETMEOUT )
     {
        u8ErrorStatus |= I2C_ERROR_START_NO_BUSY_FLAG;
     }

     return u8ErrorStatus;
}

/*****************************************************************************//*!
   *
   * @brief set slave address.
   *        
   * @param[in] pI2Cx      point to I2C module type.
   *
   * @return none
   *
   * @ Pass/ Fail criteria: none
*****************************************************************************/
void I2C_SetSlaveAddress(I2C_Type *pI2Cx,uint16_t u16SlaveAddress)
{
    /* write low 8bit address */
    pI2Cx->A1 = (uint8_t)u16SlaveAddress;

    /* write high 3bit address if it support 10bit slave address */
    pI2Cx->C2 &= ~I2C_C2_AD_MASK;
    pI2Cx->C2 |= (uint8_t)(u16SlaveAddress>>8)&0x03;
}

/*****************************************************************************//*!
   *
   * @brief disable IICIF interrupt.
   *        
   * @param[in] pI2Cx      point to I2C module type.
   *
   * @return none.
   *
   * @ Pass/ Fail criteria: none
*****************************************************************************/
void I2C_IntDisable(I2C_Type *pI2Cx)
{
    pI2Cx->C1 &= ~I2C_C1_IICIE_MASK;
    if(pI2Cx == I2C0)
    {
        NVIC_DisableIRQ(I2C0_IRQn);
    }
    #if defined(CPU_NV32M4)
    else if(pI2Cx == I2C1)
    {
        NVIC_DisableIRQ(I2C1_IRQn);
    }
    #endif
    else
    {
        
    }
}
/*****************************************************************************//*!
   *
   * @brief enable IICIF interrupt.
   *        
   * @param[in] pI2Cx      point to I2C module type.
   *
   * @return none.
   *
   * @ Pass/ Fail criteria: none.
*****************************************************************************/
void I2C_IntEnable(I2C_Type *pI2Cx)
{
    pI2Cx->C1 |= I2C_C1_IICIE_MASK;
    if(pI2Cx == I2C0)
    {
        NVIC_EnableIRQ(I2C0_IRQn);
    }
    #if defined(CPU_NV32M4)
    else if(pI2Cx == I2C1)
    {
        NVIC_EnableIRQ(I2C1_IRQn);
    }
    #endif
    else
    {
        
    }
}

/*****************************************************************************//*!
   *
   * @brief SCL low ETMeout value that determines the ETMeout period of SCL low.
   *        
   * @param[in] pI2Cx      point to I2C module type.
   *
   * @return none.
   *
   * @ Pass/ Fail criteria: none.
*****************************************************************************/
void I2C_SetSCLLowETMeout(I2C_Type *pI2Cx, uint16_t u16ETMeout)
{
    pI2Cx->SLTL = (uint8_t)u16ETMeout;
    pI2Cx->SLTH = (uint8_t)(u16ETMeout>>8);
}
/*****************************************************************************//*!
   *
   * @brief deinit I2C module.
   *        
   * @param[in] pI2Cx    point to I2C module type.
   *
   * @return none
   *
   * @ Pass/ Fail criteria: none
*****************************************************************************/
void I2C_Deinit(I2C_Type *pI2Cx)
{
     pI2Cx->C1 &= ~I2C_C1_IICEN_MASK;
#if defined(CPU_NV32)
    SIM->SCGC &= ~SIM_SCGC_IIC_MASK;
#elif defined(CPU_NV32M3)
    SIM->SCGC &= ~SIM_SCGC_IIC_MASK;
#elif defined(CPU_NV32M4)
    if(pI2Cx == I2C0)
    {
        SIM->SCGC &= ~SIM_SCGC_I2C0_MASK;
    }
    else
    {
        SIM->SCGC &= ~SIM_SCGC_I2C1_MASK;
    }
#endif
}

/*****************************************************************************//*!
   *
   * @brief write a byte to I2C module.
   *        
   * @param[in] pI2Cx    point to I2C module type.
   * @param[in] u8WrBuff  data buffer for writing. 
   *
   * @return error status
   *
   * @ Pass/ Fail criteria: none
*****************************************************************************/

uint8_t I2C_WriteOneByte(I2C_Type *pI2Cx, uint8_t u8WrBuff)
{
    uint32_t u32ETMeout;
    uint8_t u8ErrorStatus;
    
    u32ETMeout = 0; 
    u8ErrorStatus = 0x00;
    while (((I2C_GetStatus(pI2Cx)&I2C_S_TCF_MASK) !=  I2C_S_TCF_MASK)
            && (u32ETMeout<I2C_WAIT_STATUS_ETMEOUT))
    {
        u32ETMeout ++;
    }
    if (u32ETMeout >= I2C_WAIT_STATUS_ETMEOUT)
    {
        u8ErrorStatus |= I2C_ERROR_NO_WAIT_TCF_FLAG; 
        return u8ErrorStatus;
    }
    
    I2C_TxEnable(pI2Cx);
    I2C_WriteDataReg(pI2Cx,u8WrBuff);

    u32ETMeout = 0;
    while (((I2C_GetStatus(pI2Cx)&I2C_S_IICIF_MASK) !=  I2C_S_IICIF_MASK)
            && (u32ETMeout<I2C_WAIT_STATUS_ETMEOUT))
    {
        u32ETMeout ++;
    }
    if (u32ETMeout >= I2C_WAIT_STATUS_ETMEOUT)
    {
        u8ErrorStatus |= I2C_ERROR_NO_WAIT_IICIF_FLAG; 
        return u8ErrorStatus;
    }

    /* clear IICIF flag */
    I2C_ClearStatus(pI2Cx,I2C_S_IICIF_MASK);
    if (I2C_GetStatus(pI2Cx) & I2C_S_RXAK_MASK) 
    {
        u8ErrorStatus |= I2C_ERROR_NO_GET_ACK;
    }
    return u8ErrorStatus;
}
/*****************************************************************************//*!
   *
   * @brief read a byte from slave I2C.
   *        
   * @param[in] pI2Cx    point to I2C module type.
   * @param[out] pRdBuff point to the data read from slave I2C.
   * @param[out] u8Ack   send out ack or nack.
   *
   * @return error status
   *
   * @ Pass/ Fail criteria:  none
*****************************************************************************/

uint8_t I2C_ReadOneByte(I2C_Type *pI2Cx, uint8_t *pRdBuff, uint8_t u8Ack)
{
    uint32_t u32ETMeout;
    uint8_t u8ErrorStatus;
    
    u32ETMeout = 0; 
    u8ErrorStatus = 0x00;
    while (((I2C_GetStatus(pI2Cx)&I2C_S_TCF_MASK) !=  I2C_S_TCF_MASK)
            && (u32ETMeout<I2C_WAIT_STATUS_ETMEOUT))
    {
        u32ETMeout ++;
    }
    if (u32ETMeout >= I2C_WAIT_STATUS_ETMEOUT)
    {
        u8ErrorStatus |= I2C_ERROR_NO_WAIT_TCF_FLAG; 
        return u8ErrorStatus;
    }
    
    I2C_RxEnable(pI2Cx);

    if( u8Ack )
    {
        /* send out nack */
        I2C_SendNack(pI2Cx);
       
    }
    else
    {
         /* send out ack */
        I2C_SendAck(pI2Cx);
    }
    *pRdBuff = I2C_ReadDataReg(pI2Cx);

    u32ETMeout = 0;
    while (((I2C_GetStatus(pI2Cx)&I2C_S_IICIF_MASK) !=  I2C_S_IICIF_MASK)
            && (u32ETMeout<I2C_WAIT_STATUS_ETMEOUT))
    {
        u32ETMeout ++;
    }
    if (u32ETMeout >= I2C_WAIT_STATUS_ETMEOUT)
    {
        u8ErrorStatus |= I2C_ERROR_NO_WAIT_IICIF_FLAG; 
        return u8ErrorStatus;
    }

    /* clear IICIF flag */
    I2C_ClearStatus(pI2Cx,I2C_S_IICIF_MASK);

    return u8ErrorStatus;
}
/*****************************************************************************//*!
   *
   * @brief send data to I2C, and wait to complete transfering.
   *   
   * @param[in]  pI2Cx    point to I2C module type.
   * @param[in]  u16SlaveAddress slave address. 
   * @param[in]  pWrBuff point the first address of transfering data buffer. 
   * @param[in]  the length of transfering data.
   *
   * @return error status
   *
   * @ Pass/ Fail criteria:  none
*****************************************************************************/

uint8_t I2C_MasterSendWait(I2C_Type *pI2Cx,uint16_t u16SlaveAddress,uint8_t *pWrBuff,uint32_t u32Length)
{
    uint32_t i;
    uint8_t u8ErrorStatus;

    /* send start signals to bus */
    u8ErrorStatus = I2C_Start(pI2Cx);

    /* send device address to slave */
    u8ErrorStatus = I2C_WriteOneByte(pI2Cx,((uint8_t)u16SlaveAddress<<1) | I2C_WRITE);

    /* if no error occur, received the correct ack from slave
            continue to send data to slave
        */
    if( u8ErrorStatus == I2C_ERROR_NULL )
    {
        for(i=0;i<u32Length;i++)
        {
            u8ErrorStatus = I2C_WriteOneByte(pI2Cx,pWrBuff[i]);
            if( u8ErrorStatus != I2C_ERROR_NULL )
            {
                return u8ErrorStatus;
            }
        }
     }

     /* send stop signals to bus */
     u8ErrorStatus = I2C_Stop(pI2Cx);

     return u8ErrorStatus;
            
}
/*****************************************************************************//*!
   *
   * @brief read data from I2C,and wait to complete transferring.
   *        
   * @param[in] pI2Cx    point to I2C module type.
   * @param[in]  u16SlaveAddress slave address. 
   * @param[in]  pRdBuff point the first address of reading data buffer. 
   * @param[in]  the length of transfering data.
   *
   * @return error status
   *
   * @ Pass/ Fail criteria:  none
*****************************************************************************/

uint8_t I2C_MasterReadWait(I2C_Type *pI2Cx,uint16_t u16SlaveAddress,uint8_t *pRdBuff,uint32_t u32Length)
{
    uint32_t i;
    uint8_t u8ErrorStatus;

    /* send start signals to bus */
    u8ErrorStatus = I2C_Start(pI2Cx);

    /* send device address to slave */
    u8ErrorStatus = I2C_WriteOneByte(pI2Cx,((uint8_t)u16SlaveAddress<<1) | I2C_READ);

    /* if no error occur, received the correct ack from slave
            continue to send data to slave
        */
    /* dummy read one byte to switch to Rx mode */
    I2C_ReadOneByte(pI2Cx,&pRdBuff[0],I2C_SEND_ACK);
    
    if( u8ErrorStatus == I2C_ERROR_NULL )
    {
        for(i=0;i<u32Length-1;i++)
        {
            u8ErrorStatus = I2C_ReadOneByte(pI2Cx,&pRdBuff[i],I2C_SEND_ACK);
            if( u8ErrorStatus != I2C_ERROR_NULL )
            {
                return u8ErrorStatus;
            }
        }
        u8ErrorStatus = I2C_ReadOneByte(pI2Cx,&pRdBuff[i],I2C_SEND_NACK);
     }
     /* send stop signals to bus */
     u8ErrorStatus = I2C_Stop(pI2Cx);
     
     return u8ErrorStatus;
            
}
/*****************************************************************************//*!
   *
   * @brief set call back function for I2C1 module.
   *        
   * @param[in] pCallBack point to address of I2C1 call back function.
   *
   * @return none.
   *
   * @ Pass/ Fail criteria:  none.
*****************************************************************************/

void I2C1_SetCallBack( I2C_CallbackType pCallBack )
{
    I2C_Callback[1] = pCallBack;
}

/*****************************************************************************//*!
   *
   * @brief set call back function for I2C0 module.
   *        
   * @param[in] pCallBack point to address of I2C0 call back function.
   *
   * @return none.
   *
   * @ Pass/ Fail criteria:  none.
*****************************************************************************/

void I2C0_SetCallBack( I2C_CallbackType pCallBack )
{
    I2C_Callback[0] = pCallBack;
}
/*! @} End of i2c_api_list                                               						*/


/*****************************************************************************//*!
   *
   * @brief I2C0 interrupt service routine.
   *        
   * @param  
   *
   * @return none
   *
   * @ Pass/ Fail criteria:  none
*****************************************************************************/
void I2C0_Isr( void )
{
    if( I2C_Callback[0] )
    {
        I2C_Callback[0]();
    }
}
/*****************************************************************************//*!
   *
   * @brief I2C1 interrupt service routine.
   *        
   * @param  
   *
   * @return none
   *
   * @ Pass/ Fail criteria:  none
*****************************************************************************/
void I2C1_Isr( void )
{
    if( I2C_Callback[1] )
    {
        I2C_Callback[1]();
    }
}