Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Problem in I2C EEPROM EXECUTION..

Status
Not open for further replies.

SHANTHI5555

Newbie level 3
Newbie level 3
Joined
Dec 24, 2014
Messages
3
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Visit site
Activity points
44
hi
i am working on a real time project using I2C EEPROM AT24C512.

For EEPROM AT24C512 testing i wrote code in QuatusII - Nios.

and it is working fine when it is in sequence commands.

then i tried it as functions. and i wrote initialize,write, and read functions. And these functions are called in main functions. now i am not getting correct output.
the code is copied below


Code C - [expand]
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
#include "system.h"
#include "stdio.h"
#include "io.h"
 
unsigned int initiate();
unsigned int i2c_write(unsigned int ,unsigned int );
unsigned int i2c_read(unsigned int );
void Delay(unsigned int,unsigned int);
 
#define I2C_PRER0 0x00
#define I2C_PRER1 0x04
#define I2C_CTR 0x08
#define I2C_TXR 0x0C
#define I2C_RXR 0x0C
#define I2C_CR 0x10
#define I2C_SR 0x10
 
#define PRER_0 0x09
#define PRER_1 0x00
#define SLAVE_ADDR_WR 0XA0
#define SLAVE_ADDR_RD 0XA1
 
unsigned int TIP[8];
unsigned int out_data;
unsigned int i;
 
int main()
{
    unsigned int i;
    unsigned int in_data=0XBB;
    unsigned int mem_addr=0x01;
 
    initiate();
    for(i=0x00;i<=0X10;i++)
    {
        i2c_write(mem_addr,0xFF);
        i2c_write(mem_addr,in_data);
        i2c_read(mem_addr);
        in_data-in_data+0x01;
    }
 
return 0;
}
 
unsigned int initiate()
{
    IOWR_32DIRECT(I2C_PIO_HW_0_BASE,I2C_PRER0,0x09);
    IOWR_32DIRECT(I2C_PIO_HW_0_BASE,I2C_PRER1,0x00);
    IOWR_32DIRECT(WB_CYC_I_BASE,I2C_CTR,0x01);
    IOWR_32DIRECT(I2C_PIO_HW_0_BASE,I2C_CTR,0x80);
    return 0;
}
 
unsigned int i2c_write(unsigned int mem_addr,unsigned int in_data)
{
    IOWR_32DIRECT(I2C_PIO_HW_0_BASE,I2C_TXR,0XA0);
    IOWR_32DIRECT(I2C_PIO_HW_0_BASE,I2C_CR,0X90);
    TIP[8]= (IORD_32DIRECT(I2C_PIO_HW_0_BASE,I2C_SR)&(0xFF)) ;
    while(TIP[1] != 0)
    {
        ;
    }
    IOWR_32DIRECT(I2C_PIO_HW_0_BASE,I2C_TXR,mem_addr);
    IOWR_32DIRECT(I2C_PIO_HW_0_BASE,I2C_CR,0X10);
    TIP[8]= (IORD_32DIRECT(I2C_PIO_HW_0_BASE,I2C_SR)&(0xFF));
    while(TIP[1] != 0)
    {
        ;
    }
    IOWR_32DIRECT(I2C_PIO_HW_0_BASE,I2C_TXR,in_data);
    IOWR_32DIRECT(I2C_PIO_HW_0_BASE,I2C_CR,0X10);
    TIP[8]= (IORD_32DIRECT(I2C_PIO_HW_0_BASE,I2C_SR)&(0xFF));
    printf("\n mem_addr %X , in_data %X",mem_addr,in_data);
    while(TIP[1] != 0)
    {
        ;
    }
    IOWR_32DIRECT(I2C_PIO_HW_0_BASE,I2C_CR,0X50);
    return 0;
}
unsigned int i2c_read(unsigned int mem_addr)
{
    IOWR_32DIRECT(I2C_PIO_HW_0_BASE,I2C_TXR,0XA0);
    IOWR_32DIRECT(I2C_PIO_HW_0_BASE,I2C_CR,0X90);
    TIP[8]= (IORD_32DIRECT(I2C_PIO_HW_0_BASE,I2C_SR)&(0xFF)) ;
    while(TIP[1] != 0)
    {
        ;
    }
    IOWR_32DIRECT(I2C_PIO_HW_0_BASE,I2C_TXR,mem_addr);
    IOWR_32DIRECT(I2C_PIO_HW_0_BASE,I2C_CR,0X10);
    TIP[8]= (IORD_32DIRECT(I2C_PIO_HW_0_BASE,I2C_SR)&(0xFF));
    while(TIP[1] != 0)
    {
        ;
    }
    IOWR_32DIRECT(I2C_PIO_HW_0_BASE,I2C_TXR,0XA1);
    IOWR_32DIRECT(I2C_PIO_HW_0_BASE,I2C_CR,0X90);
    TIP[8]= (IORD_32DIRECT(I2C_PIO_HW_0_BASE,I2C_SR)&(0XFF) );
    while(TIP[1] != 0)
        {
            ;
        }
    IOWR_32DIRECT(I2C_PIO_HW_0_BASE,I2C_CR,0X20);
    Delay(1,1);
    TIP[8]= (IORD_32DIRECT(I2C_PIO_HW_0_BASE,I2C_SR)&(0xFF) );
    printf("\n out_data = %X",IORD_32DIRECT(I2C_PIO_HW_0_BASE,I2C_RXR));
    while(TIP[1] != 0)
    {
        ;
    }
    IOWR_32DIRECT(I2C_PIO_HW_0_BASE,I2C_CR,0X28);
    return 0;
}
 
void Delay(unsigned int a,unsigned int b)
{
 unsigned int x,y;
 for(x=0;x<=a;x++)
 {
     for(y=0;y<=b;y++)
      {
 
      }
 }
}



i don't know that what is the problem in that. please tell about this.

thank you...
 
Last edited by a moderator:

I don't see any references to EEPROM I2C addresses in your code, thus I believe it doesn't actually access it.

There are other points like generating correct EEPROM commands with address field, and required ACK polling after write actions. But that's next.
 

I don't see any references to EEPROM I2C addresses in your code, thus I believe it doesn't actually access it.

There are other points like generating correct EEPROM commands with address field, and required ACK polling after write actions. But that's next.

SLAVE_ADDR_WR this is EEPROM I2C address write address,
SLAVE_ADDR_RD this is EEPROM I2C address read address defined globally because its a fixed adrresses.
that TIP is Reading of ACK and TIP bit of SR register, its a ack polling only right.
 

O.K., you have repeated SLAVE_ADDR_WR as literal down the code, like below:
Code:
unsigned int i2c_write(unsigned int mem_addr,unsigned int in_data)
{
    IOWR_32DIRECT(I2C_PIO_HW_0_BASE,I2C_TXR,0XA0);
    IOWR_32DIRECT(I2C_PIO_HW_0_BASE,I2C_CR,0X90);
    TIP[8]= (IORD_32DIRECT(I2C_PIO_HW_0_BASE,I2C_SR)&(0xFF)) ;
    while(TIP[1] != 0)
    {
        ;
    }
    // ...

You should have mentioned that you are using an opencores I2C master controller. https://opencores.com/project,i2c

I must confess that I don't understand how the code should wait for command completion. There's no interrupt defined that might change TIP[1]. So I assume, the while statement is simply skipped because TIP[1] is initially cleared. Did you copy the code somewhere?

Apart from this point, there's acknowledge polling for the EEPROM implemented in your code. `
 

O.K., you have repeated SLAVE_ADDR_WR as literal down the code, like below:
Code:
unsigned int i2c_write(unsigned int mem_addr,unsigned int in_data)
{
    IOWR_32DIRECT(I2C_PIO_HW_0_BASE,I2C_TXR,0XA0);
    IOWR_32DIRECT(I2C_PIO_HW_0_BASE,I2C_CR,0X90);
    TIP[8]= (IORD_32DIRECT(I2C_PIO_HW_0_BASE,I2C_SR)&(0xFF)) ;
    while(TIP[1] != 0)
    {
        ;
    }
    // ...

You should have mentioned that you are using an opencores I2C master controller. https://opencores.com/project,i2c

I must confess that I don't understand how the code should wait for command completion. There's no interrupt defined that might change TIP[1]. So I assume, the while statement is simply skipped because TIP[1] is initially cleared. Did you copy the code somewhere?

Apart from this point, there's acknowledge polling for the EEPROM implemented in your code. `

it is checking that while loop.. i checked in debug mode.

now it is working fine. now it is checking ack bit as well as tip bit. i just replaced while loop like this while((IORD_32DIRECT(I2C_PIO_HW_0_BASE,I2C_SR))==1);

thank you for ur support.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top