Statistics
| Branch: | Tag: | Revision:

root / host / lib / usrp / dboard_eeprom.cpp @ 16f08844

History | View | Annotate | Download (5.38 KB)

1 039eceb4 Josh Blum
//
2 de35e125 Josh Blum
// Copyright 2010-2011 Ettus Research LLC
3 039eceb4 Josh Blum
//
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 16f08844 Josh Blum
#include <uhd/exception.hpp>
20
#include <boost/foreach.hpp>
21 1217b8d6 Josh Blum
#include <boost/format.hpp>
22 814a5c40 Josh Blum
#include <algorithm>
23 1217b8d6 Josh Blum
#include <iostream>
24 039eceb4 Josh Blum
25
using namespace uhd;
26
using namespace uhd::usrp;
27
28 1217b8d6 Josh Blum
static const bool _dboard_eeprom_debug = false;
29
30 814a5c40 Josh Blum
/***********************************************************************
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 039eceb4 Josh Blum
////////////////////////////////////////////////////////////////////////
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_OE_LSB        0x03
73
#define DB_EEPROM_OE_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 814a5c40 Josh Blum
#define DB_EEPROM_SERIAL        0x09
79
#define DB_EEPROM_SERIAL_LEN    0x09 //9 ASCII characters
80 039eceb4 Josh Blum
#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 300ddf78 Josh Blum
//negative sum of bytes excluding checksum byte
89
static boost::uint8_t checksum(const byte_vector_t &bytes){
90 1217b8d6 Josh Blum
    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 300ddf78 Josh Blum
    }
94 1217b8d6 Josh Blum
    if (_dboard_eeprom_debug)
95
        std::cout << boost::format("sum: 0x%02x") % sum << std::endl;
96
    return boost::uint8_t(sum);
97 300ddf78 Josh Blum
}
98
99 de35e125 Josh Blum
dboard_eeprom_t::dboard_eeprom_t(void){
100
    id = dboard_id_t::none();
101
    serial = "";
102
}
103
104
void dboard_eeprom_t::load(i2c_iface &iface, boost::uint8_t addr){
105
    byte_vector_t bytes = iface.read_eeprom(addr, 0, DB_EEPROM_CLEN);
106
107 1217b8d6 Josh Blum
    if (_dboard_eeprom_debug){
108
        for (size_t i = 0; i < bytes.size(); i++){
109
            std::cout << boost::format(
110
                "eeprom byte[0x%02x] = 0x%02x") % i % int(bytes.at(i)
111
            ) << std::endl;
112
        }
113
    }
114 814a5c40 Josh Blum
115 039eceb4 Josh Blum
    try{
116 90ed2e3a Josh Blum
        UHD_ASSERT_THROW(bytes.size() >= DB_EEPROM_CLEN);
117
        UHD_ASSERT_THROW(bytes[DB_EEPROM_MAGIC] == DB_EEPROM_MAGIC_VALUE);
118
        UHD_ASSERT_THROW(bytes[DB_EEPROM_CHKSUM] == checksum(bytes));
119 814a5c40 Josh Blum
120
        //parse the ids
121 4d5df237 Josh Blum
        id = dboard_id_t::from_uint16(0
122
            | (boost::uint16_t(bytes[DB_EEPROM_ID_LSB]) << 0)
123
            | (boost::uint16_t(bytes[DB_EEPROM_ID_MSB]) << 8)
124
        );
125 814a5c40 Josh Blum
126
        //parse the serial
127
        serial = bytes_to_string(
128
            byte_vector_t(&bytes.at(DB_EEPROM_SERIAL),
129
            &bytes.at(DB_EEPROM_SERIAL+DB_EEPROM_SERIAL_LEN))
130
        );
131
132 4066b1f8 Josh Blum
    }catch(const uhd::assertion_error &){
133 4d5df237 Josh Blum
        id = dboard_id_t::none();
134 814a5c40 Josh Blum
        serial = "";
135 039eceb4 Josh Blum
    }
136
}
137
138 de35e125 Josh Blum
void dboard_eeprom_t::store(i2c_iface &iface, boost::uint8_t addr){
139 300ddf78 Josh Blum
    byte_vector_t bytes(DB_EEPROM_CLEN, 0); //defaults to all zeros
140 039eceb4 Josh Blum
    bytes[DB_EEPROM_MAGIC] = DB_EEPROM_MAGIC_VALUE;
141 814a5c40 Josh Blum
142
    //load the id bytes
143 4d5df237 Josh Blum
    bytes[DB_EEPROM_ID_LSB] = boost::uint8_t(id.to_uint16() >> 0);
144
    bytes[DB_EEPROM_ID_MSB] = boost::uint8_t(id.to_uint16() >> 8);
145 814a5c40 Josh Blum
146
    //load the serial bytes
147
    byte_vector_t ser_bytes = string_to_bytes(serial, DB_EEPROM_SERIAL_LEN);
148
    std::copy(ser_bytes.begin(), ser_bytes.end(), &bytes.at(DB_EEPROM_SERIAL));
149
150
    //load the checksum
151 300ddf78 Josh Blum
    bytes[DB_EEPROM_CHKSUM] = checksum(bytes);
152 039eceb4 Josh Blum
153 de35e125 Josh Blum
    iface.write_eeprom(addr, 0, bytes);
154 039eceb4 Josh Blum
}