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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [orpsocv2/] [boards/] [actel/] [ordb1a3pe1500/] [sw/] [tests/] [i2c_master_slave/] [sim/] [i2c_master_slave-loopback.c] - Blame information for rev 408

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 408 julius
/*
2
  i2c loopback test
3
 
4
  Tests a i2c master/slave read/writeback.
5
 
6
  Interrupt handler for slave merely inverts the data we received and puts
7
  it back onto the slave transmit register.
8
 
9
  Master writes and then reads back something from the same slave.
10
  Successfull if the byte read back is inverted version of what was written.
11
 
12
  Currently hardcoded for 2 I2C devices.
13
 
14
  Julius Baxter, julius@opencores.org
15
 
16
*/
17
 
18
 
19
#include "board.h"
20
#include "cpu-utils.h"
21
#include "orpsoc-defines.h"
22
#include "printf.h"
23
 
24
#include "i2c_master_slave.h"
25
 
26
const int i2c_base_adr[4] = {I2C_0_BASE, I2C_1_BASE, I2C_2_BASE, I2C_3_BASE};
27
 
28
#define I2C_MASTER_SLAVE_PRESCALER 0x0010
29
 
30
#define NUM_I2C_MASTER_SLAVE_CORES 2
31
 
32
void i2c_master_slave_0_int_handler(void);
33
void i2c_master_slave_1_int_handler(void);
34
 
35
void i2c_master_slave_0_int_handler(void)
36
{
37
  static char regdata;
38
 
39
  char status = REG8(i2c_base_adr[0] + I2C_MASTER_SLAVE_SR);
40
  //printf("i2c core 0 interrupt - status: %x\n",status);
41
  if (status & I2C_MASTER_SLAVE_SR_SLAVE_DATA_REQ) // Read req. from master
42
    {
43
      // Put data into TX reg, inverted
44
      REG8(i2c_base_adr[0] + I2C_MASTER_SLAVE_TXR) = (~regdata);
45
      // set command (slave continue), and acknowledge interrupt
46
      REG8(i2c_base_adr[0] + I2C_MASTER_SLAVE_CR) = I2C_MASTER_SLAVE_CR_SL_CONT |
47
        I2C_MASTER_SLAVE_CR_IACK;
48
    }
49
  else if ((status & I2C_MASTER_SLAVE_SR_SLAVE_DATA_AVAIL)) //Write req.
50
    {
51
      // Store the received value
52
      regdata =  REG8(i2c_base_adr[0] + I2C_MASTER_SLAVE_RXR);
53
      // set command (slave continue), and acknowledge interrupt
54
      REG8(i2c_base_adr[0] + I2C_MASTER_SLAVE_CR) = I2C_MASTER_SLAVE_CR_SL_CONT |
55
        I2C_MASTER_SLAVE_CR_IACK;
56
    }
57
  else
58
    {
59
      // Interrupt due to master receiving an OK for a transaction.
60
      // Just ACK and proceed for now.
61
      //printf("i2c 0 master RX ack\n");
62
      i2c_master_slave_ack_interrupt(0);
63
    }
64
  return;
65
}
66
 
67
 
68
void i2c_master_slave_1_int_handler(void)
69
{
70
  static char regdata;
71
  char status = REG8(i2c_base_adr[1] + I2C_MASTER_SLAVE_SR);
72
  //printf("i2c core 1 interrupt - status: %x\n",status);
73
  if (status & I2C_MASTER_SLAVE_SR_SLAVE_DATA_REQ) // Read req. from master
74
    {
75
      // Put data into TX reg, inverted
76
      REG8(i2c_base_adr[1] + I2C_MASTER_SLAVE_TXR) = (~regdata);
77
      // set command (slave continue), and acknowledge interrupt
78
      REG8(i2c_base_adr[1] + I2C_MASTER_SLAVE_CR) = I2C_MASTER_SLAVE_CR_SL_CONT |
79
        I2C_MASTER_SLAVE_CR_IACK;
80
    }
81
  else if ((status & I2C_MASTER_SLAVE_SR_SLAVE_DATA_AVAIL)) //Write req.
82
    {
83
      // Store the received value
84
      regdata =  REG8(i2c_base_adr[1] + I2C_MASTER_SLAVE_RXR);
85
      // set command (slave continue), and acknowledge interrupt
86
      REG8(i2c_base_adr[1] + I2C_MASTER_SLAVE_CR) = I2C_MASTER_SLAVE_CR_SL_CONT |
87
        I2C_MASTER_SLAVE_CR_IACK;
88
    }
89
  else
90
    {
91
      // Interrupt due to master receiving an OK for a transaction.
92
      // Just ACK and proceed for now.
93
      // printf("i2c 1 master RX ack\n");
94
      i2c_master_slave_ack_interrupt(1);
95
    }
96
 
97
  return;
98
}
99
 
100
int main()
101
{
102
 
103
  // Select which core should be master
104
  char i2c_master_core;
105
  char i2c_slave_core;
106
 
107
  // Slave addresses for each i2c core
108
  char i2c_slave_core_addresses[4] = {0x44, 0x45, 0x46, 0x47};
109
  char slave_reg_addr = 0;
110
  char test_write;
111
  char test_read;
112
 
113
  // Initialise software interrupt handler 
114
  int_init();
115
 
116
  printf("i2c loopback test.\n");
117
 
118
  printf("Init...\n");
119
  /* Install i2c core 0 interrupt handler */
120
  int_add(I2C_0_IRQ, i2c_master_slave_0_int_handler, 0);
121
 
122
  /* Install i2c core 1 interrupt handler */
123
  int_add(I2C_1_IRQ, i2c_master_slave_1_int_handler, 0);
124
 
125
  /* Install i2c core 2 interrupt handler */
126
  //int_add(I2C_2_IRQ, i2c_master_slave_2_int_handler, 0);
127
 
128
  /* Install i2c core 3 interrupt handler */
129
  //int_add(I2C_3_IRQ, i2c_master_slave_3_int_handler, 0);
130
 
131
  // Enable interrupts
132
  cpu_enable_user_interrupts();
133
 
134
  /* Set i2c core slave addresses - deact immediately again*/
135
 
136
  for(i2c_slave_core=0;i2c_slave_core<NUM_I2C_MASTER_SLAVE_CORES;
137
      i2c_slave_core++)
138
    {
139
      i2c_master_slave_init_core(i2c_slave_core, I2C_MASTER_SLAVE_PRESCALER,
140
                                 1);
141
 
142
      i2c_master_slave_init_as_slave(i2c_slave_core,
143
                                     i2c_slave_core_addresses[i2c_slave_core]);
144
    }
145
 
146
  for(i2c_master_core=0;i2c_master_core<NUM_I2C_MASTER_SLAVE_CORES;
147
      i2c_master_core++)
148
    {
149
      report(0x10000000);
150
      report(i2c_master_core);
151
 
152
      for(i2c_slave_core=0;i2c_slave_core<NUM_I2C_MASTER_SLAVE_CORES;
153
          i2c_slave_core++)
154
        {
155
          report(0x20000000);
156
          report(i2c_slave_core);
157
          // Master can't test own slave functionality, so skip
158
          if (i2c_slave_core == i2c_master_core)
159
            continue;
160
 
161
          printf("i2c master %d: Testing write to slave %d ",i2c_master_core,
162
                 i2c_slave_core);
163
 
164
          test_write = rand() & 0xff;
165
 
166
          printf("writing: 0x%.2x\n",test_write & 0xff);
167
 
168
          // Start bus write to i2c slave
169
          i2c_master_slave_master_start(i2c_master_core,
170
                                       i2c_slave_core_addresses[i2c_slave_core],
171
                                        0);
172
          // write some data (slave reg address)
173
          i2c_master_slave_master_write(i2c_master_core, slave_reg_addr, 0, 0);
174
 
175
          // write the data to slave's register, plus indicate stop
176
          i2c_master_slave_master_write(i2c_master_core, test_write, 0, 1);
177
 
178
          /* Send read access to slave */
179
          printf("i2c master %d: Testing read from i2c slave core %d - ",
180
                 i2c_master_core, i2c_slave_core);
181
 
182
          // Start bus read from i2c slave
183
          i2c_master_slave_master_start(i2c_master_core,
184
                                       i2c_slave_core_addresses[i2c_slave_core],
185
                                        1);
186
          // Read + stop
187
          i2c_master_slave_master_read(i2c_master_core, 0, 1, &test_read);
188
 
189
          report((0xff & test_read));
190
 
191
          printf("recv: 0x%.2x", test_read & 0xff);
192
 
193
          if (test_read != ~test_write)
194
            {
195
              printf(" - FAIL ( expected 0x%.2x)\n",~test_write & 0xff);
196
              exit(0xbaaaaaad);
197
            }
198
          else
199
            printf(" - OK\n");
200
        }
201
    }
202
 
203
  printf("Test completed\n");
204
  exit(0x8000000d);
205
 
206
}
207
 

powered by: WebSVN 2.1.0

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