DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
WIB2Frame.hpp
Go to the documentation of this file.
1
11
12#ifndef FDDETDATAFORMATS_INCLUDE_FDDETDATAFORMATS_WIB2FRAME_HPP_
13#define FDDETDATAFORMATS_INCLUDE_FDDETDATAFORMATS_WIB2FRAME_HPP_
14
15#include <algorithm> // For std::min
16#include <cassert> // For assert()
17#include <cstdint> // For uint32_t etc
18#include <cstdio>
19#include <cstdlib>
20#include <stdexcept> // For std::out_of_range
21
23
31{
32public:
33 // ===============================================================
34 // Preliminaries
35 // ===============================================================
36
37 // The definition of the format is in terms of 32-bit words
38 using word_t = uint32_t; // NOLINT
39
40 static constexpr int s_bits_per_adc = 14;
41 static constexpr int s_bits_per_word = 8 * sizeof(word_t);
42 static constexpr int s_u_channels_per_femb = 40;
43 static constexpr int s_v_channels_per_femb = 40;
44 static constexpr int s_x_channels_per_femb = 48;
46 static constexpr int s_fembs_per_frame = 2;
50
59
60 struct Trailer
61 {
62 word_t flex_bits : 16, tbd_1 : 1, tbd_2 : 1, ws : 1, psr_cal : 4, ready : 1, context_code : 8;
63 };
64
65 // ===============================================================
66 // Data members
67 // ===============================================================
71
72 // ===============================================================
73 // Accessors
74 // ===============================================================
75
88 uint16_t get_adc(int i) const // NOLINT(build/unsigned)
89 {
90 if (i < 0 || i >= s_num_channels)
91 throw std::out_of_range("ADC index out of range");
92
93 // The index of the first (and sometimes only) word containing the required ADC value
94 int word_index = s_bits_per_adc * i / s_bits_per_word;
95 assert(word_index < s_num_adc_words);
96 // Where in the word the lowest bit of our ADC value is located
97 int first_bit_position = (s_bits_per_adc * i) % s_bits_per_word;
98 // How many bits of our desired ADC are located in the `word_index`th word
99 int bits_from_first_word = std::min(s_bits_per_adc, s_bits_per_word - first_bit_position);
100 uint16_t adc = adc_words[word_index] >> first_bit_position; // NOLINT(build/unsigned)
101 // If we didn't get the full 14 bits from this word, we need the rest from the next word
102 if (bits_from_first_word < s_bits_per_adc) {
103 assert(word_index + 1 < s_num_adc_words);
104 adc |= adc_words[word_index + 1] << bits_from_first_word;
105 }
106 // Mask out all but the lowest 14 bits;
107 return adc & 0x3FFFu;
108 }
109
113 void set_adc(int i, uint16_t val) // NOLINT(build/unsigned)
114 {
115 if (i < 0 || i >= s_num_channels)
116 throw std::out_of_range("ADC index out of range");
117 if (val >= (1 << s_bits_per_adc))
118 throw std::out_of_range("ADC value out of range");
119
120 // The index of the first (and sometimes only) word containing the required ADC value
121 int word_index = s_bits_per_adc * i / s_bits_per_word;
122 assert(word_index < s_num_adc_words);
123 // Where in the word the lowest bit of our ADC value is located
124 int first_bit_position = (s_bits_per_adc * i) % s_bits_per_word;
125 // How many bits of our desired ADC are located in the `word_index`th word
126 int bits_in_first_word = std::min(s_bits_per_adc, s_bits_per_word - first_bit_position);
127 uint32_t mask = (1 << (first_bit_position)) - 1;
128 adc_words[word_index] = ((val << first_bit_position) & ~mask) | (adc_words[word_index] & mask);
129 // If we didn't put the full 14 bits in this word, we need to put the rest in the next word
130 if (bits_in_first_word < s_bits_per_adc) {
131 assert(word_index + 1 < s_num_adc_words);
132 mask = (1 << (s_bits_per_adc - bits_in_first_word)) - 1;
133 adc_words[word_index + 1] = ((val >> bits_in_first_word) & mask) | (adc_words[word_index + 1] & ~mask);
134 }
135 }
136
139 uint64_t get_timestamp() const // NOLINT(build/unsigned)
140 {
141 return (uint64_t)header.timestamp_1 | ((uint64_t)header.timestamp_2 << 32); // NOLINT(build/unsigned)
142 }
143
146 void set_timestamp(const uint64_t new_timestamp) // NOLINT(build/unsigned)
147 {
148 header.timestamp_1 = new_timestamp;
149 header.timestamp_2 = new_timestamp >> 32;
150 }
151};
152
153} // namespace dunedaq::fddetdataformats
154
155#endif // FDDETDATAFORMATS_INCLUDE_FDDETDATAFORMATS_WIB2FRAME_HPP_
156
157// Local Variables:
158// c-basic-offset: 2
159// End:
Class for accessing raw WIB v2 frames, as used in ProtoDUNE-SP-II.
Definition WIB2Frame.hpp:31
void set_timestamp(const uint64_t new_timestamp)
Set the 64-bit timestamp of the frame.
static constexpr int s_num_ch_per_frame
Definition WIB2Frame.hpp:49
static constexpr int s_x_channels_per_femb
Definition WIB2Frame.hpp:44
static constexpr int s_v_channels_per_femb
Definition WIB2Frame.hpp:43
static constexpr int s_num_channels
Definition WIB2Frame.hpp:47
uint16_t get_adc(int i) const
Get the ith ADC value in the frame.
Definition WIB2Frame.hpp:88
static constexpr int s_channels_per_femb
Definition WIB2Frame.hpp:45
static constexpr int s_u_channels_per_femb
Definition WIB2Frame.hpp:42
uint64_t get_timestamp() const
Get the 64-bit timestamp of the frame.
word_t adc_words[s_num_adc_words]
Definition WIB2Frame.hpp:69
static constexpr int s_fembs_per_frame
Definition WIB2Frame.hpp:46
void set_adc(int i, uint16_t val)
Set the ith ADC value in the frame to val.
static constexpr int s_bits_per_adc
Definition WIB2Frame.hpp:40
static constexpr int s_bits_per_word
Definition WIB2Frame.hpp:41
static constexpr int s_num_adc_words
Definition WIB2Frame.hpp:48