Imported Upstream version 3.0
[debian/gnuradio] / usrp / firmware / src / usrp2 / eeprom_io.c
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2006 Free Software Foundation, Inc.
4  * 
5  * This file is part of GNU Radio
6  * 
7  * GNU Radio is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2, or (at your option)
10  * any later version.
11  * 
12  * GNU Radio is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with GNU Radio; see the file COPYING.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street,
20  * Boston, MA 02110-1301, USA.
21  */
22
23 #include "eeprom_io.h"
24 #include "i2c.h"
25 #include "delay.h"
26
27 // returns non-zero if successful, else 0
28 unsigned char
29 eeprom_read (unsigned char i2c_addr, unsigned char eeprom_offset,
30              xdata unsigned char *buf, unsigned char len)
31 {
32   // We setup a random read by first doing a "zero byte write".
33   // Writes carry an address.  Reads use an implicit address.
34
35   static xdata unsigned char cmd[1];
36   cmd[0] = eeprom_offset;
37   if (!i2c_write(i2c_addr, cmd, 1))
38     return 0;
39
40   return i2c_read(i2c_addr, buf, len);
41 }
42
43
44 #if 0
45
46 // returns non-zero if successful, else 0
47 unsigned char
48 eeprom_write (unsigned char i2c_addr, unsigned char eeprom_offset,
49               const xdata unsigned char *buf, unsigned char len)
50 {
51   static xdata unsigned char cmd[2];
52   unsigned char ok;
53
54   while (len-- > 0){
55     cmd[0] = eeprom_offset++;
56     cmd[1] = *buf++;
57     ok = i2c_write(i2c_addr, cmd, 2);
58     mdelay(10);         // delay 10ms worst case write time
59     if (!ok)
60       return 0;
61   }
62   return 1;
63 }
64
65 #endif