- self.state = 'WAIT FOR FALLING EDGE'
- elif self.state == 'WAIT FOR PRESENCE DETECT':
- # Sample presence status.
- t = self.rise + self.cnt_presence[self.overdrive]
- owr, pwr = self.wait({'skip': t - self.samplenum})
- self.present = owr
- self.state = 'WAIT FOR RESET SLOT END'
- elif self.state == 'WAIT FOR RESET SLOT END':
- # A reset slot ends in a long recovery period.
- t = self.rise + self.cnt_reset[self.overdrive]
- owr, pwr = self.wait({'skip': t - self.samplenum})
-
- if owr == 0:
- # This seems to be a reset slot, wait for its end.
- self.state = 'WAIT FOR RISING EDGE'
- continue
-
- p = 'false' if self.present else 'true'
- self.putrs([3, ['Presence: %s' % p, 'Presence', 'Pres', 'P']])
- self.putprs(['RESET/PRESENCE', not self.present])
-
- # Wait for next slot.
- self.state = 'WAIT FOR FALLING EDGE'
+ # Timing outside of known states.
+ self.putfr([1, ['Erroneous signal', 'Error', 'Err', 'E']])
+ self.state = 'IDLE'
+ elif self.state == 'PRESENCE DETECT HIGH': # Wait for slave presence signal.
+ # Wait for a falling edge and/or presence detect signal.
+ self.wait_falling_timeout(self.rise, timing['PDH']['max'])
+
+ # Calculate time since rising edge.
+ time = ((self.samplenum - self.rise) / self.samplerate) * 1000000.0
+
+ if self.matched[0] and not self.matched[1]:
+ # Presence detected.
+ if time < timing['PDH']['min'][self.overdrive]:
+ self.putrs([1, ['Presence detect signal is too early',
+ 'Presence detect too early',
+ 'PDH < ' + str(timing['PDH']['min'][self.overdrive])]])
+ self.fall = self.samplenum
+ self.state = 'PRESENCE DETECT LOW'
+ else: # No presence detected.
+ self.putrs([3, ['Presence: false', 'Presence', 'Pres', 'P']])
+ self.putprs(['RESET/PRESENCE', False])
+ self.state = 'IDLE'
+ elif self.state == 'PRESENCE DETECT LOW': # Slave presence signalled.
+ # Wait for end of presence signal (on rising edge).
+ self.wait({0: 'r'})
+ # Calculate time since start of presence signal.
+ time = ((self.samplenum - self.fall) / self.samplerate) * 1000000.0
+ if time < timing['PDL']['min'][self.overdrive]:
+ self.putfs([1, ['Presence detect signal is too short',
+ 'Presence detect too short',
+ 'PDL < ' + str(timing['PDL']['min'][self.overdrive])]])
+ elif time > timing['PDL']['max'][self.overdrive]:
+ self.putfs([1, ['Presence detect signal is too long',
+ 'Presence detect too long',
+ 'PDL > ' + str(timing['PDL']['max'][self.overdrive])]])
+ if time > timing['RSTH']['min'][self.overdrive]:
+ self.rise = self.samplenum
+ # Wait for end of presence detect.
+ self.state = 'PRESENCE DETECT'
+
+ # End states (for additional checks).
+ if self.state == 'SLOT': # Wait for end of time slot.
+ # Wait for a falling edge and/or end of timeslot.
+ self.wait_falling_timeout(self.fall, timing['SLOT']['min'])
+
+ if self.matched[0] and not self.matched[1]:
+ # Low detected before end of slot.
+ self.putfs([1, ['Time slot not long enough',
+ 'Slot too short',
+ 'SLOT < ' + str(timing['SLOT']['min'][self.overdrive])]])
+ # Don't output invalid bit.
+ self.fall = self.samplenum
+ self.state = 'LOW'
+ else: # End of time slot.
+ # Output bit.
+ self.putfs([0, ['Bit: %d' % self.bit, '%d' % self.bit]])
+ self.putpfs(['BIT', self.bit])
+ # Save command bits.
+ if self.bit_count >= 0:
+ self.command += (self.bit << self.bit_count)
+ self.bit_count += 1
+ # Check for overdrive ROM command.
+ if self.bit_count >= 8:
+ if self.command == 0x3c or self.command == 0x69:
+ self.overdrive = True
+ self.put(self.samplenum, self.samplenum,
+ self.out_ann,
+ [4, ['Entering overdrive mode', 'Overdrive on']])
+ self.bit_count = -1
+ self.state = 'IDLE'
+
+ if self.state == 'PRESENCE DETECT':
+ # Wait for a falling edge and/or end of presence detect.
+ self.wait_falling_timeout(self.rise, timing['RSTH']['min'])
+
+ if self.matched[0] and not self.matched[1]:
+ # Low detected before end of presence detect.
+ self.putfs([1, ['Presence detect not long enough',
+ 'Presence detect too short',
+ 'RTSH < ' + str(timing['RSTH']['min'][self.overdrive])]])
+ # Inform about presence detected.
+ self.putrs([3, ['Slave presence detected', 'Slave present',
+ 'Present', 'P']])
+ self.putprs(['RESET/PRESENCE', True])
+ self.fall = self.samplenum
+ self.state = 'LOW'
+ else: # End of time slot.
+ # Inform about presence detected.
+ self.putrs([3, ['Presence: true', 'Presence', 'Pres', 'P']])
+ self.putprs(['RESET/PRESENCE', True])
+ self.rise = self.samplenum
+ # Start counting the first 8 bits to get the ROM command.
+ self.bit_count = 0
+ self.command = 0
+ self.state = 'IDLE'