Line data Source code
1 : /**
2 : * @file SIChipSlave.cpp
3 : *
4 : * This is part of the DUNE DAQ Software Suite, copyright 2020.
5 : * Licensing/copyright details are in the COPYING file that you should have
6 : * received with this code.
7 : */
8 :
9 : #include "timing/SIChipSlave.hpp"
10 :
11 : // PDT headers
12 : #include "ers/ers.hpp"
13 : #include "timing/toolbox.hpp"
14 :
15 : #include <boost/tuple/tuple.hpp>
16 :
17 : #include <fstream>
18 : #include <sstream>
19 :
20 : namespace dunedaq {
21 : namespace timing {
22 :
23 : //-----------------------------------------------------------------------------
24 0 : SIChipSlave::SIChipSlave(const I2CMasterNode* i2c_master, uint8_t address) // NOLINT(build/unsigned)
25 0 : : I2CSlave(i2c_master, address)
26 0 : {}
27 : //-----------------------------------------------------------------------------
28 :
29 : //-----------------------------------------------------------------------------
30 0 : SIChipSlave::~SIChipSlave() {}
31 : //-----------------------------------------------------------------------------
32 :
33 : //-----------------------------------------------------------------------------
34 : uint8_t // NOLINT(build/unsigned)
35 0 : SIChipSlave::read_page() const
36 : {
37 :
38 0 : TLOG_DEBUG(7) << "<- Reading page ";
39 :
40 : // Read from the page address (0x1?)
41 0 : return read_i2c(0x1);
42 : }
43 : //-----------------------------------------------------------------------------
44 :
45 : //-----------------------------------------------------------------------------
46 : void
47 0 : SIChipSlave::switch_page(uint8_t page) const // NOLINT(build/unsigned)
48 : {
49 :
50 : // Prepare a data block with address and new page
51 : // std::vector<uint8_t> lData = {0x1, page};// NOLINT(build/unsigned)
52 0 : TLOG_DEBUG(7) << "-> Switching to page " << format_reg_value((uint32_t)page); // NOLINT(build/unsigned)
53 0 : write_i2c(0x1, page);
54 0 : }
55 : //-----------------------------------------------------------------------------
56 :
57 : //-----------------------------------------------------------------------------
58 : uint32_t // NOLINT(build/unsigned)
59 0 : SIChipSlave::read_device_version() const
60 : {
61 :
62 : // Go to the right page
63 0 : switch_page(0x0);
64 : // Read 2 words from 0x2
65 0 : auto version = read_i2cArray(0x2, 2);
66 :
67 0 : return (((uint32_t)version[1] << 8) + (uint32_t)version[0]); // NOLINT(build/unsigned)
68 0 : }
69 : //-----------------------------------------------------------------------------
70 :
71 : //-----------------------------------------------------------------------------
72 : uint8_t // NOLINT(build/unsigned)
73 0 : SIChipSlave::read_clock_register(uint16_t address) const // NOLINT(build/unsigned)
74 : {
75 :
76 0 : uint8_t reg_address = (address & 0xff); // NOLINT(build/unsigned)
77 0 : uint8_t page_address = (address >> 8) & 0xff; // NOLINT(build/unsigned)
78 0 : std::stringstream debug_stream;
79 0 : debug_stream << std::showbase << std::hex << "Read Address " << (uint32_t)address // NOLINT(build/unsigned)
80 0 : << " reg: " << (uint32_t)reg_address // NOLINT(build/unsigned)
81 0 : << " page: " << (uint32_t)page_address; // NOLINT(build/unsigned)
82 0 : TLOG_DEBUG(6) << debug_stream.str();
83 : // Change page only when required.
84 : // (The SI5344 don't like to have the page register id to be written all the time.)
85 0 : uint8_t current_address = read_page(); // NOLINT(build/unsigned)
86 0 : if (page_address != current_address) {
87 0 : switch_page(page_address);
88 : }
89 :
90 : // Read the register
91 0 : return read_i2c(reg_address);
92 0 : }
93 : //-----------------------------------------------------------------------------
94 :
95 : //-----------------------------------------------------------------------------
96 : void
97 0 : SIChipSlave::write_clock_register(uint16_t address, uint8_t data) const // NOLINT(build/unsigned)
98 : {
99 :
100 0 : uint8_t reg_address = (address & 0xff); // NOLINT(build/unsigned)
101 0 : uint8_t page_address = (address >> 8) & 0xff; // NOLINT(build/unsigned)
102 :
103 0 : std::stringstream debug_stream;
104 0 : debug_stream << std::showbase << std::hex << "Write Address " << (uint32_t)address // NOLINT(build/unsigned)
105 0 : << " reg: " << (uint32_t)reg_address // NOLINT(build/unsigned)
106 0 : << " page: " << (uint32_t)page_address; // NOLINT(build/unsigned)
107 0 : TLOG_DEBUG(6) << debug_stream.str();
108 : // Change page only when required.
109 : // (The SI5344 don't like to have the page register id to be written all the time.)
110 0 : uint8_t current_address = read_page(); // NOLINT(build/unsigned)
111 0 : if (page_address != current_address) {
112 0 : switch_page(page_address);
113 : }
114 :
115 0 : return write_i2c(reg_address, data);
116 0 : }
117 : //-----------------------------------------------------------------------------
118 :
119 : } // namespace timing
120 : } // namespace dunedaq
|