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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
|
/*
* fusb302 usb phy driver for type-c and PD
*
* Copyright (C) 2015, 2016 Fairchild Semiconductor Corporation
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. Seee the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "AlternateModes.h"
FSC_BOOL startDRPSource = FALSE;
extern FSC_BOOL g_Idle;
void SetStateAlternateUnattached(void)
{
#ifdef FSC_HAVE_DRP
if (PortType == USBTypeC_DRP) // If we are a DRP
{
SetStateAlternateUnattachedSink();
}
#endif // FSC_HAVE_DRP
#ifdef FSC_HAVE_ACCMODE
if ((PortType == USBTypeC_Sink) && (blnAccSupport)) // If we are a sink supporting accessories
{
#ifdef FSC_HAVE_DRP
SetStateAlternateDRP();
#endif // FSC_HAVE_DRP
}
#endif // FSC_HAVE_ACCMODE
#ifdef FSC_HAVE_SRC
if (PortType == USBTypeC_Source) // If we are strictly a Source
{
SetStateAlternateUnattachedSource();
}
#endif // FSC_HAVE_SRC
#ifdef FSC_HAVE_SNK
if (PortType == USBTypeC_Sink) // If we are strictly a Sink
{
SetStateAlternateUnattachedSink();
}
#endif // FSC_HAVE_SNK
}
void StateMachineAlternateUnattached(void)
{
#ifdef FSC_HAVE_DRP
if (PortType == USBTypeC_DRP) // If we are a DRP
{
StateMachineAlternateUnattachedSink();
}
#endif // FSC_HAVE_DRP
#ifdef FSC_HAVE_ACCMODE
if ((PortType == USBTypeC_Sink) && (blnAccSupport)) // If we are a sink supporting accessories
{
#ifdef FSC_HAVE_DRP
StateMachineAlternateDRP();
#endif // FSC_HAVE_DRP
}
#endif // FSC_HAVE_ACCMODE
#ifdef FSC_HAVE_SRC
if (PortType == USBTypeC_Source) // If we are strictly a Source
{
StateMachineAlternateUnattachedSource();
}
#endif // FSC_HAVE_SRC
#ifdef FSC_HAVE_SNK
if (PortType == USBTypeC_Sink) // If we are strictly a Sink
{
StateMachineAlternateUnattachedSink();
}
#endif // FSC_HAVE_SNK
}
#ifdef FSC_HAVE_DRP
void SetStateAlternateDRP(void)
{
#ifdef FSC_INTERRUPT_TRIGGERED
g_Idle = FALSE; // Run continuously (unmask all)
Registers.Mask.byte = 0x00;
DeviceWrite(regMask, 1, &Registers.Mask.byte);
Registers.MaskAdv.byte[0] = 0x00;
DeviceWrite(regMaska, 1, &Registers.MaskAdv.byte[0]);
Registers.MaskAdv.M_GCRCSENT = 0;
DeviceWrite(regMaskb, 1, &Registers.MaskAdv.byte[1]);
platform_enable_timer(TRUE);
#endif
platform_set_vbus_lvl_enable(VBUS_LVL_ALL, FALSE, FALSE); // Disable VBUS output
ConnState = Unattached; // Set the state machine variable to unattached
if((blnCCPinIsCC1 && startDRPSource) || (blnCCPinIsCC2 && (startDRPSource == FALSE)))
{
sourceOrSink = SOURCE; // This doesn't make sense for this state, so this will pertain to CC1
Registers.Switches.byte[0] = 0x46; // Enable Pullup1, Pulldown2, and Meas1
DeviceWrite(regSwitches0, 1, &Registers.Switches.byte[0]); // Commit the switch state
}
else
{
sourceOrSink = SINK; // This doesn't make sense for this state, so this will pertain to CC1
Registers.Switches.byte[0] = 0x89; // Enable Pullup2, Pulldown1, and Meas1
DeviceWrite(regSwitches0, 1, &Registers.Switches.byte[0]); // Commit the switch state
}
startDRPSource = FALSE;
Registers.Power.PWR = 0x7; // Enable everything except internal oscillator
DeviceWrite(regPower, 1, &Registers.Power.byte); // Commit the power state
updateSourceCurrent(); // Updates source current
USBPDDisable(TRUE); // Disable the USB PD state machine (no need to write Device again since we are doing it here)
SinkCurrent = utccNone;
resetDebounceVariables();
blnCCPinIsCC1 = FALSE; // Clear the CC1 pin flag
blnCCPinIsCC2 = FALSE; // Clear the CC2 pin flag
StateTimer = tAlternateDRPSwap;
PDDebounce = T_TIMER_DISABLE; // enable the 1st level debounce timer, not used in this state
CCDebounce = T_TIMER_DISABLE; // enable the 2nd level debounce timer, not used in this state
ToggleTimer = tDeviceToggle; // enable the toggle timer
OverPDDebounce = tPDDebounce; // enable PD filter timer
#ifdef FSC_DEBUG
WriteStateLog(&TypeCStateLog, ConnState, Timer_tms, Timer_S);
#endif // FSC_DEBUG
}
#endif // FSC_HAVE_DRP
#ifdef FSC_HAVE_DRP
void StateMachineAlternateDRP(void)
{
debounceCC();
if(StateTimer == 0)
{
AlternateDRPSwap();
StateTimer = tAlternateDRPSwap;
}
if((CC1TermPrevious >= CCTypeRdUSB) && (CC1TermPrevious <= CCTypeRd3p0) && (sourceOrSink == SOURCE))
{
blnCCPinIsCC1 = TRUE;
blnCCPinIsCC2 = FALSE;
#ifdef FSC_HAVE_ACCMODE
if ((PortType == USBTypeC_Sink) && (blnAccSupport)) // If we are configured as a sink and support accessories...
checkForAccessory(); // Go to the AttachWaitAcc state
else // Otherwise we must be configured as a source or DRP
#endif // FSC_HAVE_ACCMODE
SetStateAttachWaitSource();
}
// NOTE: Remember sourceOrSink refers to CC1 in this funky state - CC2 is opposite
else if((CC2TermPrevious >= CCTypeRdUSB) && (CC2TermPrevious <= CCTypeRd3p0) && (sourceOrSink == SINK))
{
blnCCPinIsCC1 = FALSE;
blnCCPinIsCC2 = TRUE;
#ifdef FSC_HAVE_ACCMODE
if ((PortType == USBTypeC_Sink) && (blnAccSupport)) // If we are configured as a sink and support accessories...
checkForAccessory(); // Go to the AttachWaitAcc state
else // Otherwise we must be configured as a source or DRP
#endif // FSC_HAVE_ACCMODE
SetStateAttachWaitSource();
}
else if((CC1TermPrevious >= CCTypeRdUSB) && (CC1TermPrevious <= CCTypeRd3p0) && (sourceOrSink == SINK))
{
blnCCPinIsCC1 = TRUE;
blnCCPinIsCC2 = FALSE;
SetStateAttachWaitSink();
}
// NOTE: Remember sourceOrSink refers to CC1 in this funky state - CC2 is opposite
else if((CC2TermPrevious >= CCTypeRdUSB) && (CC2TermPrevious <= CCTypeRd3p0) && (sourceOrSink == SOURCE))
{
blnCCPinIsCC1 = FALSE;
blnCCPinIsCC2 = TRUE;
SetStateAttachWaitSink();
}
}
#endif // FSC_HAVE_DRP
#ifdef FSC_HAVE_DRP
void AlternateDRPSwap(void)
{
if(sourceOrSink == SOURCE)
{
Registers.Switches.byte[0] = 0x89; // Enable Pullup2, Pulldown1, and Meas2
DeviceWrite(regSwitches0, 1, &Registers.Switches.byte[0]); // Commit the switch state
sourceOrSink = SINK;
}
else
{
Registers.Switches.byte[0] = 0x46; // Enable Pullup1, Pulldown2, and Meas1
DeviceWrite(regSwitches0, 1, &Registers.Switches.byte[0]); // Commit the switch state
sourceOrSink = SOURCE;
}
}
#endif // FSC_HAVE_DRP
#ifdef FSC_HAVE_DRP
void AlternateDRPSourceSinkSwap(void)
{
if(ConnState == Unattached)
{
if(Registers.Switches.MEAS_CC2 == 1) // CC2 is opposite in this state
{
if(sourceOrSink == SOURCE)
{
sourceOrSink = SINK;
}
else
{
sourceOrSink = SOURCE;
}
}
}
}
#endif // FSC_HAVE_DRP
#ifdef FSC_HAVE_SRC
void SetStateAlternateUnattachedSource(void)
{
#ifdef FSC_INTERRUPT_TRIGGERED
g_Idle = TRUE; // Idle until COMP or BC_LVL
Registers.Mask.byte = 0xFF;
Registers.Mask.M_COMP_CHNG = 0;
Registers.Mask.M_BC_LVL = 0;
DeviceWrite(regMask, 1, &Registers.Mask.byte);
Registers.MaskAdv.byte[0] = 0xFF;
DeviceWrite(regMaska, 1, &Registers.MaskAdv.byte[0]);
Registers.MaskAdv.M_GCRCSENT = 1;
DeviceWrite(regMaskb, 1, &Registers.MaskAdv.byte[1]);
platform_enable_timer(FALSE);
#endif
platform_set_vbus_lvl_enable(VBUS_LVL_ALL, FALSE, FALSE); // Disable VBUS output
ConnState = UnattachedSource; // Set the state machine variable to unattached
sourceOrSink = SOURCE;
Registers.Switches.byte[0] = 0xC4; // Enable both pull-ups and measure on CC1
Registers.Power.PWR = 0x7; // Enable everything except internal oscillator
DeviceWrite(regPower, 1, &Registers.Power.byte); // Commit the power state
DeviceWrite(regSwitches0, 1, &Registers.Switches.byte[0]); // Commit the switch state
USBPDDisable(TRUE); // Disable the USB PD state machine (no need to write Device again since we are doing it here)
SinkCurrent = utccNone;
resetDebounceVariables();
blnCCPinIsCC1 = FALSE; // Clear the CC1 pin flag
blnCCPinIsCC2 = FALSE; // Clear the CC2 pin flag
#ifdef FSC_HAVE_DRP
if (PortType == USBTypeC_DRP) // If we are a DRP
{
StateTimer = tAlternateDRPSwap; // Enable state timer for DRP toggle
}
else
#endif // FSC_HAVE_DRP
#ifdef FSC_HAVE_ACCMODE
if ((PortType == USBTypeC_Sink) && (blnAccSupport)) // ... or a sink supporting accessories
{
StateTimer = tAlternateDRPSwap; // Enable state timer for DRP toggle
}
else
#endif // FSC_HAVE_ACCMODE
{
StateTimer = T_TIMER_DISABLE; // Disable the state timer, not used in this state
}
PDDebounce = tPDDebounce; // enable the 1st level debounce timer, not used in this state
CCDebounce = tCCDebounce; // enable the 2nd level debounce timer, not used in this state
ToggleTimer = T_TIMER_DISABLE; // disable the toggle timer
DRPToggleTimer = T_TIMER_DISABLE; // Timer to switch from unattachedSrc to unattachedSnk in DRP
OverPDDebounce = T_TIMER_DISABLE; // enable PD filter timer
#ifdef FSC_DEBUG
WriteStateLog(&TypeCStateLog, ConnState, Timer_tms, Timer_S);
#endif // FSC_DEBUG
}
#endif // FSC_HAVE_SRC
#ifdef FSC_HAVE_SRC
void StateMachineAlternateUnattachedSource(void) // CC1 and CC2 are shorted, so we just look for CC1 < 2.6V for Ra/Rd and > 0.2V for no Ra
{
CCTermType previous;
if(Registers.Control.HOST_CUR != 0b11) // Set host current to 330uA if it is not by default
{
Registers.Control.HOST_CUR = 0b11;
DeviceWrite(regControl0, 1, &Registers.Control.byte[0]);
}
previous = AlternateDecodeCCTerminationSource(); // Get CC Termination Value
updateSourceCurrent(); // Returns host current to current current setting
#ifdef FSC_HAVE_ACCMODE
if(blnAccSupport)
{
if (previous == CCTypeRa)
{
blnCCPinIsCC1 = FALSE; // Setting both to false will have the next state figure it out
blnCCPinIsCC2 = FALSE;
SetStateAttachWaitSource();
return;
}
}
#endif // FSC_HAVE_ACCMODE
if ((previous == CCTypeRd3p0) || (previous == CCTypeRd1p5)) // If a CC pin is Rd
{
blnCCPinIsCC1 = FALSE; // Setting both to false will have the next state figure it out
blnCCPinIsCC2 = FALSE;
SetStateAttachWaitSource(); // Go to the Attached.Src state
}
else if(previous == CCTypeRdUSB) // Either Ra-Open or Rd-Ra
{
peekCC1Source(); // Connect if Rd on CC1
if((CC1TermPrevious == CCTypeRdUSB) || (CC1TermPrevious == CCTypeRd1p5) || (CC1TermPrevious == CCTypeRd3p0))
{
blnCCPinIsCC1 = FALSE;
blnCCPinIsCC2 = FALSE;
SetStateAttachWaitSource();
}
else
{
peekCC2Source(); // Else connect if Rd on CC2
if((CC2TermPrevious == CCTypeRdUSB) || (CC2TermPrevious == CCTypeRd1p5) || (CC2TermPrevious == CCTypeRd3p0))
{
blnCCPinIsCC1 = FALSE;
blnCCPinIsCC2 = FALSE;
SetStateAttachWaitSource();
}
}
}
else if (StateTimer == 0)
{
#ifdef FSC_HAVE_SNK
SetStateAlternateUnattachedSink();
#else
SetStateAlternateUnattachedSource();
#endif // FSC_HAVE_SNK
}
}
#endif // FSC_HAVE_SRC
#ifdef FSC_HAVE_SNK
void StateMachineAlternateUnattachedSink(void)
{
debounceCC();
if ((CC1TermPrevious >= CCTypeRdUSB) && (CC1TermPrevious < CCTypeUndefined) && ((CC2TermPrevious == CCTypeRa) || CC2TermPrevious == CCTypeOpen)) // If the CC1 pin is Rd for atleast tPDDebounce...
{
blnCCPinIsCC1 = TRUE; // The CC pin is CC1
blnCCPinIsCC2 = FALSE;
SetStateAttachWaitSink(); // Go to the Attached.Snk state
}
else if ((CC2TermPrevious >= CCTypeRdUSB) && (CC2TermPrevious < CCTypeUndefined) && ((CC1TermPrevious == CCTypeRa) || CC1TermPrevious == CCTypeOpen)) // If the CC2 pin is Rd for atleast tPDDebounce...
{
blnCCPinIsCC1 = FALSE; // The CC pin is CC2
blnCCPinIsCC2 = TRUE;
SetStateAttachWaitSink(); // Go to the Attached.Snk state
}
else if (StateTimer == 0)
{
#ifdef FSC_HAVE_SRC
SetStateAlternateUnattachedSource();
#else
SetStateAlternateUnattachedSink();
#endif // FSC_HAVE_SRC
}
}
#endif // FSC_HAVE_SNK
#ifdef FSC_HAVE_SNK
void SetStateAlternateUnattachedSink(void)
{
#ifdef FSC_INTERRUPT_TRIGGERED
g_Idle = FALSE; // run continuously
Registers.Mask.byte = 0x00;
DeviceWrite(regMask, 1, &Registers.Mask.byte);
Registers.MaskAdv.byte[0] = 0x00;
DeviceWrite(regMaska, 1, &Registers.MaskAdv.byte[0]);
Registers.MaskAdv.M_GCRCSENT = 0;
DeviceWrite(regMaskb, 1, &Registers.MaskAdv.byte[1]);
platform_enable_timer(TRUE);
#endif
platform_set_vbus_lvl_enable(VBUS_LVL_ALL, FALSE, FALSE); // Disable VBUS output
ConnState = Unattached; // Set the state machine variable to unattached
sourceOrSink = SINK;
Registers.Switches.byte[0] = 0x07; // Enable both pull-downs and measure on CC1
Registers.Power.PWR = 0x7; // Enable everything except internal oscillator
DeviceWrite(regPower, 1, &Registers.Power.byte); // Commit the power state
DeviceWrite(regSwitches0, 1, &Registers.Switches.byte[0]); // Commit the switch state
USBPDDisable(TRUE); // Disable the USB PD state machine (no need to write Device again since we are doing it here)
SinkCurrent = utccNone;
resetDebounceVariables();
blnCCPinIsCC1 = FALSE; // Clear the CC1 pin flag
blnCCPinIsCC2 = FALSE; // Clear the CC2 pin flag
#ifdef FSC_HAVE_DRP
if (PortType == USBTypeC_DRP) // If we are a DRP
{
StateTimer = tAlternateDRPSwap; // Enable state timer for DRP toggle
}
else
#endif // FSC_HAVE_DRP
#ifdef FSC_HAVE_ACCMODE
if ((PortType == USBTypeC_Sink) && (blnAccSupport)) // ... or a sink supporting accessories
{
StateTimer = tAlternateDRPSwap; // Enable state timer for DRP toggle
}
else
#endif // FSC_HAVE_ACCMODE
{
StateTimer = T_TIMER_DISABLE; // Disable the state timer, not used in this state
}
PDDebounce = tPDDebounce; // enable the 1st level debounce timer, not used in this state
CCDebounce = tCCDebounce; // enable the 2nd level debounce timer, not used in this state
ToggleTimer = tDeviceToggle; // disable the toggle timer
DRPToggleTimer = tDeviceToggle; // Timer to switch from unattachedSrc to unattachedSnk in DRP
OverPDDebounce = T_TIMER_DISABLE; // enable PD filter timer
#ifdef FSC_DEBUG
WriteStateLog(&TypeCStateLog, ConnState, Timer_tms, Timer_S);
#endif // FSC_DEBUG
}
#endif // FSC_HAVE_SNK
#ifdef FSC_HAVE_ACCMODE
void SetStateAlternateAudioAccessory(void)
{
#ifdef FSC_INTERRUPT_TRIGGERED
g_Idle = FALSE; // Run continuously (unmask all)
Registers.Mask.byte = 0x00;
DeviceWrite(regMask, 1, &Registers.Mask.byte);
Registers.MaskAdv.byte[0] = 0x00;
DeviceWrite(regMaska, 1, &Registers.MaskAdv.byte[0]);
Registers.MaskAdv.M_GCRCSENT = 0;
DeviceWrite(regMaskb, 1, &Registers.MaskAdv.byte[1]);
platform_enable_timer(TRUE);
#endif
platform_set_vbus_lvl_enable(VBUS_LVL_ALL, FALSE, FALSE); // Disable VBUS output
ConnState = AudioAccessory; // Set the state machine variable to Audio.Accessory
sourceOrSink = SOURCE;
Registers.Power.PWR = 0x7; // Enable everything except internal oscillator
DeviceWrite(regPower, 1, &Registers.Power.byte); // Commit the power state
Registers.Control.HOST_CUR = 0b11; // Set host current to 330uA
DeviceWrite(regControl0, 1, &Registers.Control.byte[0]); // Commit host current
Registers.Switches.byte[0] = 0xC4; // Enable both pull-ups and measure on CC1
DeviceWrite(regSwitches0, 1, &Registers.Switches.byte[0]); // Commit the switch state
SinkCurrent = utccNone; // Not used in accessories
OverPDDebounce = T_TIMER_DISABLE; // Disable PD filter timer
StateTimer = T_TIMER_DISABLE; // Disable the state timer, not used in this state
PDDebounce = tCCDebounce; // Once in this state, we are waiting for the lines to be stable for tCCDebounce before changing states
CCDebounce = T_TIMER_DISABLE; // Disable the 2nd level debouncing initially to force completion of a 1st level debouncing
ToggleTimer = T_TIMER_DISABLE; // Once we are in the audio.accessory state, we are going to stop toggling and only monitor CC1
#ifdef FSC_DEBUG
WriteStateLog(&TypeCStateLog, ConnState, Timer_tms, Timer_S);
#endif // FSC_DEBUG
}
#endif // FSC_HAVE_ACCMODE
#ifdef FSC_HAVE_SRC
CCTermType AlternateDecodeCCTerminationSource(void)
{
CCTermType Termination = CCTypeUndefined; // By default set it to undefined
Registers.Measure.MDAC = MDAC_2P058V; // Set up DAC threshold to 2.05V
DeviceWrite(regMeasure, 1, &Registers.Measure.byte); // Commit the DAC threshold
platform_delay_10us(25); // Delay to allow measurement to settle
DeviceRead(regStatus0, 1, &Registers.Status.byte[4]);
if (Registers.Status.COMP == 1)
{
Termination = CCTypeOpen;
return Termination;
}
else if(Registers.Status.BC_LVL == 0)
{
Termination = CCTypeRa;
}
else if(Registers.Status.BC_LVL == 3)
{
Termination = CCTypeRd3p0;
}
else if(Registers.Status.BC_LVL == 2)
{
Termination = CCTypeRd1p5;
}
else
{
Termination = CCTypeRdUSB;
}
return Termination; // Return the termination type
}
#endif // FSC_HAVE_SRC
|