/
SMBusAnalyzer.cpp
358 lines (296 loc) · 11.1 KB
/
SMBusAnalyzer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#include <AnalyzerChannelData.h>
#include <cassert>
#include <vector>
#include <algorithm>
#include "SMBusAnalyzer.h"
#include "SMBusAnalyzerSettings.h"
SMBusAnalyzer::SMBusAnalyzer() : mSimulationInitilized( false )
{
SetAnalyzerSettings( &mSettings );
}
SMBusAnalyzer::~SMBusAnalyzer()
{
KillThread();
}
void SMBusAnalyzer::SetupResults()
{
// reset the results
mResults.reset( new SMBusAnalyzerResults( this, &mSettings ) );
SetAnalyzerResults( mResults.get() );
// set which channels will carry bubbles
mResults->AddChannelBubblesWillAppearOn( mSettings.mSMBDAT );
mResults->AddChannelBubblesWillAppearOn( mSettings.mSMBCLK );
}
AnalyzerChannelData* SMBusAnalyzer::AdvanceAllTo( U64 toSample )
{
assert( mSMBCLK->GetSampleNumber() <= toSample );
assert( mSMBDAT->GetSampleNumber() <= toSample );
mSMBCLK->AdvanceToAbsPosition( toSample );
mSMBDAT->AdvanceToAbsPosition( toSample );
return GetNearestTransitionChannel();
}
AnalyzerChannelData* SMBusAnalyzer::GetNearestTransitionChannel()
{
if( !mSMBDAT->DoMoreTransitionsExistInCurrentData() )
return mSMBCLK;
if( !mSMBCLK->DoMoreTransitionsExistInCurrentData() )
return mSMBDAT;
if( mSMBDAT->GetSampleOfNextEdge() < mSMBCLK->GetSampleOfNextEdge() )
return mSMBDAT;
return mSMBCLK;
}
bool SMBusAnalyzer::GetSignal( SMBusSignalState& state )
{
func_begin:
bool ret_val = true;
state.Clear();
AnalyzerChannelData* nearestEdgeChannel = GetNearestTransitionChannel();
// first check for a start or stop
if( mSMBCLK->GetBitState() == BIT_HIGH )
{
U64 start_sample = mSMBCLK->GetSampleNumber();
if( nearestEdgeChannel == mSMBDAT )
{
ret_val = false;
// remember where we began in case this is the last packet
U64 frame_begin = mSMBDAT->GetSampleNumber();
state.sample_marker = mSMBDAT->GetSampleOfNextEdge();
if( mSMBDAT->GetBitState() == BIT_HIGH )
state.bus_signal = SMB_Start;
else
state.bus_signal = SMB_Stop;
// move all to the SMBDAT transition
nearestEdgeChannel = AdvanceAllTo( state.sample_marker );
// set the begin and ending samples for the frame
U64 frame_offset = 0;
if( nearestEdgeChannel->DoMoreTransitionsExistInCurrentData() )
{
U64 end_sample = nearestEdgeChannel->GetSampleOfNextEdge();
if( end_sample - state.sample_marker < state.sample_marker - start_sample )
frame_offset = ( end_sample - state.sample_marker ) / 2;
else
frame_offset = ( state.sample_marker - start_sample ) / 2;
}
else
{
frame_offset = state.sample_marker - frame_begin;
}
state.sample_begin = state.sample_marker - frame_offset;
state.sample_end = state.sample_marker + frame_offset;
// skip the falling SMBCLK if we have a start
// this prepares us for the first bit
if( state.bus_signal == SMB_Start && nearestEdgeChannel == mSMBCLK )
AdvanceAllTo( mSMBCLK->GetSampleOfNextEdge() );
}
else
{
// SMBCLK falls - skip to it
nearestEdgeChannel = AdvanceAllTo( nearestEdgeChannel->GetSampleOfNextEdge() );
}
}
// if we didn't find a start or stop above try parsing a bit
if( ret_val )
{
// we must have a low SMBCLK at this point
assert( mSMBCLK->GetBitState() == BIT_LOW );
// skip the preparing of SMBDAT for the rising SMBCLK egde
if( nearestEdgeChannel == mSMBDAT )
{
state.sample_begin = mSMBDAT->GetSampleOfNextEdge();
nearestEdgeChannel = AdvanceAllTo( state.sample_begin );
}
else
{
state.sample_begin = ( mSMBCLK->GetSampleNumber() + mSMBCLK->GetSampleOfNextEdge() ) / 2;
}
// do we have a rising SMBCLK at this point?
if( nearestEdgeChannel == mSMBCLK )
{
nearestEdgeChannel = AdvanceAllTo( mSMBCLK->GetSampleOfNextEdge() );
state.sample_rising_clk = mSMBCLK->GetSampleNumber();
// do we have a falling SMBCLK edge next?
if( nearestEdgeChannel == mSMBCLK )
{
// sample the state of SMBDAT
state.sample_marker = ( mSMBCLK->GetSampleOfNextEdge() + state.sample_rising_clk ) / 2;
if( state.sample_marker > state.sample_rising_clk && state.sample_marker < mSMBCLK->GetSampleOfNextEdge() )
nearestEdgeChannel = AdvanceAllTo( state.sample_marker );
state.bus_signal = mSMBDAT->GetBitState() == BIT_HIGH ? SMB_One : SMB_Zero;
// advance to SMBCLK falling edge
nearestEdgeChannel = AdvanceAllTo( mSMBCLK->GetSampleOfNextEdge() );
// where to put the end of state depends on where the next transition happens
if( nearestEdgeChannel == mSMBCLK )
state.sample_end = ( mSMBCLK->GetSampleNumber() + mSMBCLK->GetSampleOfNextEdge() ) / 2;
else
state.sample_end = mSMBDAT->GetSampleOfNextEdge();
}
else
{
// go back to parsing start/stop
goto func_begin;
}
}
else
{
// go back to parsing start/stop
goto func_begin;
}
}
return ret_val;
}
void SMBusAnalyzer::WorkerThread()
{
// get the channel pointers
mSMBDAT = GetAnalyzerChannelData( mSettings.mSMBDAT );
mSMBCLK = GetAnalyzerChannelData( mSettings.mSMBCLK );
const bool calcPEC = mSettings.CalcPEC();
bool is_bit;
SMBusByte byte;
SMBusPacket packet;
SMBusSignalState s;
for( ;; )
{
is_bit = GetSignal( s );
if( is_bit )
{
// is this a data bit?
if( byte.signals.size() < 8 )
{
byte.value <<= 1;
if( s.bus_signal == SMB_One )
byte.value |= 1;
byte.signals.push_back( s );
}
else
{
// translate zero/one to ack/nack
if( s.bus_signal == SMB_Zero )
{
byte.is_acked = true;
s.bus_signal = SMB_ACK;
}
else
{
byte.is_acked = false;
s.bus_signal = SMB_NACK;
}
// remember the ACK/NACK signal
byte.signals.push_back( s );
// add byte to command/packet
if( !packet.chunks.empty() )
packet.chunks.back().push_back( byte );
}
}
else
{
// on a Start signal begin a new chunk
if( s.bus_signal == SMB_Start )
{
if( packet.chunks.empty() )
packet.first_start = s;
packet.chunks.push_back( std::vector<SMBusByte>() );
}
}
// create the markers
s.AddMarkers( mResults.get(), mSettings.mSMBCLK, mSettings.mSMBDAT );
// branch on decode level
if( mSettings.mDecodeLevel == DL_Signals )
{
mResults->AddFrame( s.ToFrame() );
}
else if( mSettings.mDecodeLevel == DL_Bytes )
{
if( byte.IsComplete() )
{
if( !packet.chunks.empty() && packet.chunks.back().size() == 1 ) // if this is the first byte of the packet
mResults->AddFrame( byte.ToAddrFrame( NULL ) );
else
mResults->AddFrame( byte.ToFrame() );
}
}
else if( mSettings.mDecodeLevel == DL_SMBus || mSettings.mDecodeLevel == DL_PMBus || mSettings.mDecodeLevel == DL_SmartBattery )
{
if( s.bus_signal == SMB_Stop )
{
// make frames for the transaction
packet.stop = s;
// first try the SMBus protocols
bool found_match = false;
int pcnt;
for( pcnt = 0; pcnt < NUM_SMBUS_PROTOCOLS && !found_match; ++pcnt )
{
if( packet.MatchesProtocol( SMBusProtocols + pcnt, calcPEC ) )
{
// packet.CreateDescFrames(mResults.get(), SMBusProtocols + pcnt, 0);
packet.CreateFrames( mResults.get(), SMBusProtocols + pcnt, calcPEC, mSettings.mDecodeLevel );
found_match = true;
}
}
// do the PMBus specific protocols
if( !found_match && mSettings.mDecodeLevel == DL_PMBus )
{
for( pcnt = 0; pcnt < NUM_PMBUS_PROTOCOLS && !found_match; ++pcnt )
{
if( packet.MatchesProtocol( PMBusProtocols + pcnt, calcPEC ) )
{
// packet.CreateDescFrames(mResults.get(), PMBusProtocols + pcnt, 0);
packet.CreateFrames( mResults.get(), PMBusProtocols + pcnt, calcPEC, mSettings.mDecodeLevel );
found_match = true;
}
}
if( !found_match && packet.MatchesGroupCommand( calcPEC ) )
{
// packet.CreateDescFrames(mResults.get(), NULL, 1);
packet.CreateFramesForGroupCommand( mResults.get(), calcPEC );
found_match = true;
}
}
// if we have not recognized this packet, just add raw data frames
if( !found_match )
{
// packet.CreateDescFrames(mResults.get(), NULL, 2);
packet.CreateFramesForRawData( mResults.get(), calcPEC );
}
packet.Clear();
}
}
if( byte.signals.size() == 9 || !is_bit )
byte.Clear();
mResults->CommitResults();
ReportProgress( mSMBDAT->GetSampleNumber() );
}
}
bool SMBusAnalyzer::NeedsRerun()
{
return false;
}
U32 SMBusAnalyzer::GenerateSimulationData( U64 minimum_sample_index, U32 device_sample_rate,
SimulationChannelDescriptor** simulation_channels )
{
if( !mSimulationInitilized )
{
mSimulationDataGenerator.Initialize( GetSimulationSampleRate(), &mSettings );
mSimulationInitilized = true;
}
return mSimulationDataGenerator.GenerateSimulationData( minimum_sample_index, device_sample_rate, simulation_channels );
}
U32 SMBusAnalyzer::GetMinimumSampleRateHz()
{
return 1000000;
}
const char* SMBusAnalyzer::GetAnalyzerName() const
{
return ::GetAnalyzerName();
}
const char* GetAnalyzerName()
{
return "SMBus";
}
Analyzer* CreateAnalyzer()
{
return new SMBusAnalyzer();
}
void DestroyAnalyzer( Analyzer* analyzer )
{
delete analyzer;
}