Imported Upstream version 3.2.2
[debian/gnuradio] / usrp2 / firmware / apps / test_i2c.c
1 /*
2  * Copyright 2007 Free Software Foundation, Inc.
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include <stdio.h>
19 #include <stdint.h>
20 #include <u2_init.h>            /* FIXME */
21 #include <i2c.h>
22 #include <usrp2_i2c_addr.h>
23 #include <string.h>
24 #include <hal_io.h>
25
26
27
28 #define ASSERT_TRUE(x) \
29   do { \
30     if (!(x)){ \
31       printf("ASSERT_TRUE failed on line %d\n", __LINE__); \
32       nerrors++; \
33     } \
34   } while(0)
35
36 #define ASSERT_FALSE(x) \
37   do { \
38     if (x){ \
39       printf("ASSERT_FALSE failed on line %d\n", __LINE__); \
40       nerrors++; \
41     } \
42   } while(0)
43
44
45 #define BUFSIZE 128
46
47 int
48 main(void)
49 {
50   int i;
51   bool ok;
52   int  nerrors = 0;
53   uint8_t buf[BUFSIZE];
54   int not_dev_addr = 0x35;      // no device with this address on the i2c bus.
55   int offset;
56   int len;
57   
58   u2_init();
59
60   puts("test_i2c\n");
61
62   // try writing a non-existent device
63   buf[0] = 0xA5;
64   ok = i2c_write(not_dev_addr, buf, 1);
65   ASSERT_FALSE(ok);
66
67   // try read from non-existent device
68   buf[0] = 0;
69   ok = i2c_read(not_dev_addr, buf, 1);
70   ASSERT_FALSE(ok);
71
72   // try writing eeprom
73   offset = 31;
74   len = 8;
75   memset(buf, 0, sizeof(buf));
76   for (i = 0; i < len; i++)
77     buf[i] = i;
78   ok = eeprom_write(I2C_ADDR_MBOARD, offset, buf, len);
79   ASSERT_TRUE(ok);
80
81   // now try to read it back
82   offset = 31;
83   len = 8;
84   memset(buf, 0, sizeof(buf));
85   ok = eeprom_read(I2C_ADDR_MBOARD, offset, buf, len);
86   ASSERT_TRUE(ok);
87
88   // check result
89   for (i = 0; i < len; i++){
90     if (buf[i] != i){
91       printf("buf[%d] = %d, should be %d\n", i, buf[i], i);
92       nerrors++;
93     }
94   }
95   
96   if (nerrors == 0){
97     output_regs->leds = 0x3;
98     puts("PASSED\n");
99   }
100   else {
101     output_regs->leds = 0x0;
102     puts("FAILED\n");
103   }
104
105   hal_finish();
106   return 0;
107 }
108