i2c Reader utility
[keystone-rtos/ibl.git] / src / util / i2cRead / i2cRead.c
1 /****************************************************************************************************
2  * FILE PURPOSE: Read an I2C eeprom
3  ****************************************************************************************************
4  * @file i2cRead.c
5  *
6  * @brief
7  *   Reads bytes from an i2c eeprom
8  *
9  ****************************************************************************************************/
11 #include "types.h"
12 #include "i2c.h"
13 #include "target.h"
14 #include <stdio.h>
16 #define I2C_SIZE_BYTES  0x10000
18 /* Run time configuration */
19 unsigned int   deviceFreqMhz = 1000;
20 unsigned short busAddress    = 0x50;
21 unsigned int   nbytes        = I2C_SIZE_BYTES;
22 unsigned int   firstByte     = 0;
25 #pragma DATA_SECTION(i2cData, ".i2cData")
26 unsigned char i2cData[I2C_SIZE_BYTES];
28 /**
29  *  @brief
30  *     Display the error returned by the i2c driver 
31  */
32 void showI2cError (I2C_RET iret)
33 {
34     char *ecode;
36     switch (iret)  {
37         case I2C_RET_LOST_ARB:        ecode = "I2C master lost an arbitration battle";
38                                       break;
40         case I2C_RET_NO_ACK:          ecode = "I2C master detected no ack from slave";
41                                       break;
43         case I2C_RET_IDLE_TIMEOUT:    ecode = "I2C timed out";
44                                       break;
46         case I2C_RET_BAD_REQUEST:     ecode = "I2C driver detected a bad data request";
47                                       break;
49         case I2C_RET_CLOCK_STUCK_LOW: ecode = "I2C driver found the bus stuck low";
50                                       break;
52         case I2C_RET_GEN_ERROR:       ecode = "I2C driver reported a general error";
53                                       break;
55     }
57     printf ("I2C reported error: %s\n", ecode);
59 }
62 void main (void)
63 {
64     I2C_RET i2cRet;
66     hwI2Cinit (deviceFreqMhz,
67                DEVICE_I2C_MODULE_DIVISOR,
68                25,                              /* Run the bus at 25 kHz */
69                10);
72     i2cRet = hwI2cMasterRead (firstByte,
73                               nbytes,
74                               i2cData,
75                               busAddress,
76                               10 );
79     if (i2cRet != I2C_RET_OK) 
80         showI2cError (i2cRet);
81     else
82         printf ("I2C read complete\n");
84 }
85