Statistics
| Branch: | Tag: | Revision:

root / host / lib / usrp / dboard_eeprom.cpp @ 715fd038

History | View | Annotate | Download (5.99 KB)

1
//
2
// Copyright 2010-2011 Ettus Research LLC
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 <uhd/usrp/dboard_eeprom.hpp>
19
#include <uhd/exception.hpp>
20
#include <uhd/utils/log.hpp>
21
#include <boost/foreach.hpp>
22
#include <boost/format.hpp>
23
#include <boost/lexical_cast.hpp>
24
#include <algorithm>
25
#include <sstream>
26

    
27
using namespace uhd;
28
using namespace uhd::usrp;
29

    
30
/***********************************************************************
31
 * Utility functions
32
 **********************************************************************/
33

    
34
//! create a string from a byte vector, return empty if invalid ascii
35
static const std::string bytes_to_string(const byte_vector_t &bytes){
36
    std::string out;
37
    BOOST_FOREACH(boost::uint8_t byte, bytes){
38
        if (byte < 32 or byte > 127) return out;
39
        out += byte;
40
    }
41
    return out;
42
}
43

    
44
//! create a byte vector from a string, null terminate unless max length
45
static const byte_vector_t string_to_bytes(const std::string &string, size_t max_length){
46
    byte_vector_t bytes;
47
    for (size_t i = 0; i < std::min(string.size(), max_length); i++){
48
        bytes.push_back(string[i]);
49
    }
50
    if (bytes.size() < max_length - 1) bytes.push_back('\0');
51
    return bytes;
52
}
53

    
54
////////////////////////////////////////////////////////////////////////
55
// format of daughterboard EEPROM
56
// 00: 0xDB code for ``I'm a daughterboard''
57
// 01:   .. Daughterboard ID (LSB)
58
// 02:   .. Daughterboard ID (MSB)
59
// 03:   .. io bits  7-0 direction (bit set if it's an output from m'board)
60
// 04:   .. io bits 15-8 direction (bit set if it's an output from m'board)
61
// 05:   .. ADC0 DC offset correction (LSB)
62
// 06:   .. ADC0 DC offset correction (MSB)
63
// 07:   .. ADC1 DC offset correction (LSB)
64
// 08:   .. ADC1 DC offset correction (MSB)
65
//  ...
66
// 1f:   .. negative of the sum of bytes [0x00, 0x1e]
67

    
68
#define DB_EEPROM_MAGIC         0x00
69
#define DB_EEPROM_MAGIC_VALUE   0xDB
70
#define DB_EEPROM_ID_LSB        0x01
71
#define DB_EEPROM_ID_MSB        0x02
72
#define DB_EEPROM_REV_LSB       0x03
73
#define DB_EEPROM_REV_MSB       0x04
74
#define DB_EEPROM_OFFSET_0_LSB  0x05 // offset correction for ADC or DAC 0
75
#define DB_EEPROM_OFFSET_0_MSB  0x06
76
#define DB_EEPROM_OFFSET_1_LSB  0x07 // offset correction for ADC or DAC 1
77
#define DB_EEPROM_OFFSET_1_MSB  0x08
78
#define DB_EEPROM_SERIAL        0x09
79
#define DB_EEPROM_SERIAL_LEN    0x09 //9 ASCII characters
80
#define DB_EEPROM_CHKSUM        0x1f
81

    
82
#define DB_EEPROM_CLEN          0x20 // length of common portion of eeprom
83

    
84
#define DB_EEPROM_CUSTOM_BASE   DB_EEPROM_CLEN // first avail offset for
85
                                               //   daughterboard specific use
86
////////////////////////////////////////////////////////////////////////
87

    
88
//negative sum of bytes excluding checksum byte
89
static boost::uint8_t checksum(const byte_vector_t &bytes){
90
    int sum = 0;
91
    for (size_t i = 0; i < std::min(bytes.size(), size_t(DB_EEPROM_CHKSUM)); i++){
92
        sum -= int(bytes.at(i));
93
    }
94
    UHD_LOGV(often) << boost::format("sum: 0x%02x") % sum << std::endl;
95
    return boost::uint8_t(sum);
96
}
97

    
98
dboard_eeprom_t::dboard_eeprom_t(void){
99
    id = dboard_id_t::none();
100
    serial = "";
101
}
102

    
103
void dboard_eeprom_t::load(i2c_iface &iface, boost::uint8_t addr){
104
    byte_vector_t bytes = iface.read_eeprom(addr, 0, DB_EEPROM_CLEN);
105

    
106
    std::ostringstream ss;
107
    for (size_t i = 0; i < bytes.size(); i++){
108
        ss << boost::format(
109
            "eeprom byte[0x%02x] = 0x%02x") % i % int(bytes.at(i)
110
        ) << std::endl;
111
    }
112
    UHD_LOGV(often) << ss.str() << std::endl;
113

    
114
    try{
115
        UHD_ASSERT_THROW(bytes.size() >= DB_EEPROM_CLEN);
116
        UHD_ASSERT_THROW(bytes[DB_EEPROM_MAGIC] == DB_EEPROM_MAGIC_VALUE);
117
        UHD_ASSERT_THROW(bytes[DB_EEPROM_CHKSUM] == checksum(bytes));
118

    
119
        //parse the ids
120
        id = dboard_id_t::from_uint16(0
121
            | (boost::uint16_t(bytes[DB_EEPROM_ID_LSB]) << 0)
122
            | (boost::uint16_t(bytes[DB_EEPROM_ID_MSB]) << 8)
123
        );
124

    
125
        //parse the serial
126
        serial = bytes_to_string(
127
            byte_vector_t(&bytes.at(DB_EEPROM_SERIAL),
128
            &bytes.at(DB_EEPROM_SERIAL+DB_EEPROM_SERIAL_LEN))
129
        );
130

    
131
        //parse the revision
132
        const boost::uint16_t rev_num = 0
133
            | (boost::uint16_t(bytes[DB_EEPROM_REV_LSB]) << 0)
134
            | (boost::uint16_t(bytes[DB_EEPROM_REV_MSB]) << 8)
135
        ;
136
        if (rev_num != 0 and rev_num != 0xffff){
137
            revision = boost::lexical_cast<std::string>(rev_num);
138
        }
139

    
140
    }catch(const uhd::assertion_error &){
141
        id = dboard_id_t::none();
142
        serial = "";
143
    }
144
}
145

    
146
void dboard_eeprom_t::store(i2c_iface &iface, boost::uint8_t addr) const{
147
    byte_vector_t bytes(DB_EEPROM_CLEN, 0); //defaults to all zeros
148
    bytes[DB_EEPROM_MAGIC] = DB_EEPROM_MAGIC_VALUE;
149

    
150
    //load the id bytes
151
    bytes[DB_EEPROM_ID_LSB] = boost::uint8_t(id.to_uint16() >> 0);
152
    bytes[DB_EEPROM_ID_MSB] = boost::uint8_t(id.to_uint16() >> 8);
153

    
154
    //load the serial bytes
155
    byte_vector_t ser_bytes = string_to_bytes(serial, DB_EEPROM_SERIAL_LEN);
156
    std::copy(ser_bytes.begin(), ser_bytes.end(), &bytes.at(DB_EEPROM_SERIAL));
157

    
158
    //load the revision bytes
159
    if (not revision.empty()){
160
        const boost::uint16_t rev_num = boost::lexical_cast<boost::uint16_t>(revision);
161
        bytes[DB_EEPROM_REV_LSB] = boost::uint8_t(rev_num >> 0);
162
        bytes[DB_EEPROM_REV_MSB] = boost::uint8_t(rev_num >> 8);
163
    }
164

    
165
    //load the checksum
166
    bytes[DB_EEPROM_CHKSUM] = checksum(bytes);
167

    
168
    iface.write_eeprom(addr, 0, bytes);
169
}