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

Subversion Repositories i2cslave

[/] [i2cslave/] [trunk/] [sw/] [aardvark_c/] [i2cSlaveTest.c] - Blame information for rev 3

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 3 sfielding
/*=========================================================================
2
|
3
|--------------------------------------------------------------------------
4
|
5
| File    : i2cSlaveTest.c
6
|--------------------------------------------------------------------------
7
|
8
|--------------------------------------------------------------------------
9
 ========================================================================*/
10
 
11
//=========================================================================
12
// INCLUDES
13
//=========================================================================
14
#include <stdio.h>
15
#include <stdlib.h>
16
#include <string.h>
17
 
18
#include "aardvark.h"
19
 
20
#ifdef _MSC_VER
21
#define fileno _fileno
22
#endif
23
 
24
 
25
//=========================================================================
26
// CONSTANTS
27
//=========================================================================
28
#define I2C_BITRATE 400 // kHz
29
 
30
 
31
//=========================================================================
32
// STATIC FUNCTIONS
33
//=========================================================================
34
static int testRegs (Aardvark handle, unsigned char LEDstate)
35
{
36
    int res, i, count;
37
    unsigned char data_out[16];
38
    unsigned char data_in[16];
39
    unsigned char expected_data [] = {LEDstate, 0xab, 0xcd, 0xef, 0x12, 0x34, 0x56, 0x78};
40
 
41
 
42
    // Set address reg = 0
43
    data_out[0] = 0x00;
44
    res = aa_i2c_write(handle, 0x3c, AA_I2C_NO_FLAGS, 1, data_out);
45
    if (res < 0)  return res;
46
 
47
    if (res == 0) {
48
        printf("error: slave device 0x38 not found\n");
49
        return -1;
50
    }
51
 
52
    // Write to registers 0 through 3
53
    data_out[0] = 0x00;
54
    data_out[1] = LEDstate;
55
    data_out[2] = 0xab;
56
    data_out[3] = 0xcd;
57
    data_out[4] = 0xef;
58
    res = aa_i2c_write(handle, 0x3c, AA_I2C_NO_FLAGS, 5, data_out);
59
    if (res < 0)  return res;
60
 
61
 
62
 
63
    // Set address reg = 0
64
    data_out[0] = 0x00;
65
    res = aa_i2c_write(handle, 0x3c, AA_I2C_NO_FLAGS, 1, data_out);
66
    if (res < 0)  return res;
67
    if (res == 0) {
68
        printf("error: slave device 0x38 not found\n");
69
        return -1;
70
    }
71
    //read 8 bytes
72
    count = aa_i2c_read(handle, 0x3c, AA_I2C_NO_FLAGS, 8, data_in);
73
    if (count < 0) {
74
        printf("error: %s\n", aa_status_string(count));
75
        return -1;
76
    }
77
    if (count == 0) {
78
        printf("error: no bytes read\n");
79
        printf("  are you sure you have the right slave address?\n");
80
        return -1;
81
    }
82
    else if (count != 8) {
83
        printf("error: read %d bytes (expected 8)\n", count);
84
        return -1;
85
    }
86
    // Dump the data to the screen
87
    //printf("\nData read from device:");
88
    for (i = 0; i < count; ++i) {
89
        //printf("Reg[0x%02x] = 0x%02x\n", i, data_in[i]);
90
        if (expected_data[i] != data_in[i]) {
91
          printf("Reg[0x%02x] expected 0x%02x got 0x%02x\n", i, expected_data[i], data_in[i]);
92
          return -1;
93
        }
94
    }
95
    //printf("\n");
96
 
97
    return 0;
98
}
99
 
100
 
101
//=========================================================================
102
// MAIN PROGRAM
103
//=========================================================================
104
int main (int argc, char *argv[]) {
105
    Aardvark handle;
106
    int   port    = 0;
107
    int   bitrate = 100;
108
    int   res     = 0;
109
    //FILE *logfile = 0;
110
    int   i;
111
    unsigned char LEDstate;
112
 
113
    if (argc < 2) {
114
        printf("usage: i2cSlaveTest PORT\n");
115
        return 1;
116
    }
117
 
118
    port = atoi(argv[1]);
119
 
120
    // Open the device
121
    handle = aa_open(port);
122
    if (handle <= 0) {
123
        printf("Unable to open Aardvark device on port %d\n", port);
124
        printf("Error code = %d\n", handle);
125
        return 1;
126
    }
127
 
128
    // Enable logging
129
    //logfile = fopen("log.txt", "at");
130
    //if (logfile != 0) {
131
    //    aa_log(handle, 3, fileno(logfile));
132
    //}
133
 
134
    // Ensure that the I2C subsystem is enabled
135
    aa_configure(handle,  AA_CONFIG_SPI_I2C);
136
 
137
    // Enable the I2C bus pullup resistors (2.2k resistors).
138
    // This command is only effective on v2.0 hardware or greater.
139
    // The pullup resistors on the v1.02 hardware are enabled by default.
140
    aa_i2c_pullup(handle, AA_I2C_PULLUP_NONE);
141
 
142
    // Power the board using the Aardvark adapter's power supply.
143
    // This command is only effective on v2.0 hardware or greater.
144
    // The power pins on the v1.02 hardware are not enabled by default.
145
    aa_target_power(handle, AA_TARGET_POWER_NONE);
146
 
147
    // Set the bitrate
148
    bitrate = aa_i2c_bitrate(handle, I2C_BITRATE);
149
    printf("Bitrate set to %d kHz\n", bitrate);
150
 
151
    i = 0;
152
    LEDstate = 0x89;
153
    do {
154
      i++;
155
      res = testRegs(handle, LEDstate);
156
      if (i % 100 == 0) {
157
        if (LEDstate == 0x89) LEDstate = 0x88; else LEDstate = 0x89;
158
        printf("Test loop: %d\n", i);
159
        fflush(stdout);
160
      }
161
    } while (i <= 100000 && res >= 0);
162
    if (res < 0)
163
        printf("error: %s\n", aa_status_string(res));
164
    else
165
        printf("All tests passed\n");
166
 
167
    // Close the device and exit
168
    aa_close(handle);
169
 
170
    // Close the logging file
171
    //fclose(logfile);
172
 
173
    return 0;
174
}

powered by: WebSVN 2.1.0

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