mirror of
https://github.com/jonathanhogg/scopething
synced 2025-07-14 11:12:09 +01:00
Neater clock mode selection code
This commit is contained in:
69
scope.py
69
scope.py
@ -57,7 +57,7 @@ class Scope(vm.VirtualMachine):
|
|||||||
self.trigger_timeout_tick = 6.4e-6
|
self.trigger_timeout_tick = 6.4e-6
|
||||||
self.trigger_low = -7.517
|
self.trigger_low = -7.517
|
||||||
self.trigger_high = 10.816
|
self.trigger_high = 10.816
|
||||||
#await self.load_params() XXX switch this off until I understand EEPROM better
|
# await self.load_params() XXX switch this off until I understand EEPROM better
|
||||||
self._generator_running = False
|
self._generator_running = False
|
||||||
Log.info("Initialised scope, revision: {}".format(revision))
|
Log.info("Initialised scope, revision: {}".format(revision))
|
||||||
|
|
||||||
@ -75,13 +75,12 @@ class Scope(vm.VirtualMachine):
|
|||||||
params.append(await self.read_eeprom(i+70))
|
params.append(await self.read_eeprom(i+70))
|
||||||
params = struct.unpack('<H8fH', bytes(params))
|
params = struct.unpack('<H8fH', bytes(params))
|
||||||
if params[0] == self.PARAMS_MAGIC and params[-1] == self.PARAMS_MAGIC:
|
if params[0] == self.PARAMS_MAGIC and params[-1] == self.PARAMS_MAGIC:
|
||||||
self.analog_low_ks = tuple(params[1:4])
|
self.analog_params = tuple(params[1:7])
|
||||||
self.analog_high_ks = tuple(params[4:7])
|
|
||||||
self.analog_offsets['A'] = params[8]
|
self.analog_offsets['A'] = params[8]
|
||||||
self.analog_offsets['B'] = params[9]
|
self.analog_offsets['B'] = params[9]
|
||||||
|
|
||||||
async def save_params(self):
|
async def save_params(self):
|
||||||
params = struct.pack('<H8fH', self.PARAMS_MAGIC, *self.analog_low_ks, *self.analog_high_ks,
|
params = struct.pack('<H8fH', self.PARAMS_MAGIC, *self.analog_params,
|
||||||
self.analog_offsets['A'], self.analog_offsets['B'], self.PARAMS_MAGIC)
|
self.analog_offsets['A'], self.analog_offsets['B'], self.PARAMS_MAGIC)
|
||||||
for i, byte in enumerate(params):
|
for i, byte in enumerate(params):
|
||||||
await self.write_eeprom(i+70, byte)
|
await self.write_eeprom(i+70, byte)
|
||||||
@ -102,55 +101,21 @@ class Scope(vm.VirtualMachine):
|
|||||||
period=1e-3, nsamples=1000, timeout=None, low=None, high=None, raw=False):
|
period=1e-3, nsamples=1000, timeout=None, low=None, high=None, raw=False):
|
||||||
if 'A' in channels and 'B' in channels:
|
if 'A' in channels and 'B' in channels:
|
||||||
nsamples_multiplier = 2
|
nsamples_multiplier = 2
|
||||||
|
dual = True
|
||||||
else:
|
else:
|
||||||
nsamples_multiplier = 1
|
nsamples_multiplier = 1
|
||||||
|
dual = False
|
||||||
ticks = int(period / nsamples / nsamples_multiplier / self.capture_clock_period)
|
ticks = int(period / nsamples / nsamples_multiplier / self.capture_clock_period)
|
||||||
if ticks >= 20 and ticks < 65536:
|
for clock_mode in vm.ClockModes:
|
||||||
sample_width = 2
|
if clock_mode.dual == dual and ticks in range(clock_mode.clock_low, clock_mode.clock_high + 1):
|
||||||
buffer_width = 6*1024
|
break
|
||||||
dump_mode = vm.DumpMode.Native
|
|
||||||
if 'A' in channels and 'B' in channels:
|
|
||||||
trace_mode = vm.TraceMode.MacroChop
|
|
||||||
buffer_mode = vm.BufferMode.MacroChop
|
|
||||||
else:
|
|
||||||
trace_mode = vm.TraceMode.Macro
|
|
||||||
buffer_mode = vm.BufferMode.Macro
|
|
||||||
elif ticks >= 15 and ticks < 40:
|
|
||||||
sample_width = 1
|
|
||||||
buffer_width = 12*1024
|
|
||||||
dump_mode = vm.DumpMode.Raw
|
|
||||||
if 'A' in channels and 'B' in channels:
|
|
||||||
trace_mode = vm.TraceMode.AnalogChop
|
|
||||||
buffer_mode = vm.BufferMode.Chop
|
|
||||||
else:
|
|
||||||
trace_mode = vm.TraceMode.Analog
|
|
||||||
buffer_mode = vm.BufferMode.Single
|
|
||||||
elif ticks >= 8 and ticks < 15:
|
|
||||||
sample_width = 1
|
|
||||||
buffer_width = 12*1024
|
|
||||||
dump_mode = vm.DumpMode.Raw
|
|
||||||
if 'A' in channels and 'B' in channels:
|
|
||||||
trace_mode = vm.TraceMode.AnalogFastChop
|
|
||||||
buffer_mode = vm.BufferMode.Chop
|
|
||||||
else:
|
|
||||||
trace_mode = vm.TraceMode.AnalogFast
|
|
||||||
buffer_mode = vm.BufferMode.Single
|
|
||||||
elif ticks >= 2 and ticks < 8:
|
|
||||||
if ticks > 5:
|
|
||||||
ticks = 5
|
|
||||||
sample_width = 1
|
|
||||||
buffer_width = 12*1024
|
|
||||||
dump_mode = vm.DumpMode.Raw
|
|
||||||
if 'A' in channels and 'B' in channels:
|
|
||||||
trace_mode = vm.TraceMode.AnalogShotChop
|
|
||||||
buffer_mode = vm.BufferMode.Chop
|
|
||||||
else:
|
|
||||||
trace_mode = vm.TraceMode.AnalogShot
|
|
||||||
buffer_mode = vm.BufferMode.Single
|
|
||||||
else:
|
else:
|
||||||
raise RuntimeError("Unsupported clock period: {}".format(ticks))
|
raise RuntimeError("Unsupported clock period: {}".format(ticks))
|
||||||
|
if clock_mode.clock_max is not None and ticks > clock_mode.clock_max:
|
||||||
|
ticks = clock_mode.clock_max
|
||||||
nsamples = int(round(period / ticks / nsamples_multiplier / self.capture_clock_period))
|
nsamples = int(round(period / ticks / nsamples_multiplier / self.capture_clock_period))
|
||||||
total_samples = nsamples * nsamples_multiplier
|
total_samples = nsamples * nsamples_multiplier
|
||||||
|
buffer_width = self.capture_buffer_size // clock_mode.sample_width
|
||||||
assert total_samples <= buffer_width
|
assert total_samples <= buffer_width
|
||||||
|
|
||||||
if raw:
|
if raw:
|
||||||
@ -188,7 +153,8 @@ class Scope(vm.VirtualMachine):
|
|||||||
analog_enable |= 2
|
analog_enable |= 2
|
||||||
|
|
||||||
async with self.transaction():
|
async with self.transaction():
|
||||||
await self.set_registers(TraceMode=trace_mode, BufferMode=buffer_mode, SampleAddress=0, ClockTicks=ticks, ClockScale=1,
|
await self.set_registers(TraceMode=clock_mode.TraceMode, BufferMode=clock_mode.BufferMode,
|
||||||
|
SampleAddress=0, ClockTicks=ticks, ClockScale=1,
|
||||||
TraceIntro=total_samples//2, TraceOutro=total_samples//2, TraceDelay=0,
|
TraceIntro=total_samples//2, TraceOutro=total_samples//2, TraceDelay=0,
|
||||||
Timeout=int(round((period*5 if timeout is None else timeout) / self.trigger_timeout_tick)),
|
Timeout=int(round((period*5 if timeout is None else timeout) / self.trigger_timeout_tick)),
|
||||||
TriggerMask=0x7f, TriggerLogic=0x80, TriggerLevel=trigger_level, SpockOption=spock_option,
|
TriggerMask=0x7f, TriggerLogic=0x80, TriggerLevel=trigger_level, SpockOption=spock_option,
|
||||||
@ -207,12 +173,12 @@ class Scope(vm.VirtualMachine):
|
|||||||
for dump_channel, channel in enumerate(sorted(channels)):
|
for dump_channel, channel in enumerate(sorted(channels)):
|
||||||
async with self.transaction():
|
async with self.transaction():
|
||||||
await self.set_registers(SampleAddress=(address - nsamples) * nsamples_multiplier % buffer_width,
|
await self.set_registers(SampleAddress=(address - nsamples) * nsamples_multiplier % buffer_width,
|
||||||
DumpMode=dump_mode, DumpChan=dump_channel,
|
DumpMode=clock_mode.DumpMode, DumpChan=dump_channel,
|
||||||
DumpCount=nsamples, DumpRepeat=1, DumpSend=1, DumpSkip=0)
|
DumpCount=nsamples, DumpRepeat=1, DumpSend=1, DumpSkip=0)
|
||||||
await self.issue_program_spock_registers()
|
await self.issue_program_spock_registers()
|
||||||
await self.issue_analog_dump_binary()
|
await self.issue_analog_dump_binary()
|
||||||
data = await self._reader.readexactly(nsamples * sample_width)
|
data = await self._reader.readexactly(nsamples * clock_mode.sample_width)
|
||||||
if sample_width == 2:
|
if clock_mode.sample_width == 2:
|
||||||
if raw:
|
if raw:
|
||||||
trace = [(value / 65536 + 0.5) for value in struct.unpack('>{}h'.format(nsamples), data)]
|
trace = [(value / 65536 + 0.5) for value in struct.unpack('>{}h'.format(nsamples), data)]
|
||||||
else:
|
else:
|
||||||
@ -297,7 +263,6 @@ class Scope(vm.VirtualMachine):
|
|||||||
raise RuntimeError("Error writing EEPROM byte")
|
raise RuntimeError("Error writing EEPROM byte")
|
||||||
|
|
||||||
async def calibrate(self, n=33):
|
async def calibrate(self, n=33):
|
||||||
global data
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
from scipy.optimize import leastsq, least_squares
|
from scipy.optimize import leastsq, least_squares
|
||||||
items = []
|
items = []
|
||||||
@ -325,7 +290,7 @@ class Scope(vm.VirtualMachine):
|
|||||||
result = least_squares(f, self.analog_params, args=items.T[:4], bounds=([0, -np.inf, 250, 0, 0], [np.inf, np.inf, 350, np.inf, np.inf]))
|
result = least_squares(f, self.analog_params, args=items.T[:4], bounds=([0, -np.inf, 250, 0, 0], [np.inf, np.inf, 350, np.inf, np.inf]))
|
||||||
if result.success in range(1, 5):
|
if result.success in range(1, 5):
|
||||||
self.analog_params = tuple(result.x)
|
self.analog_params = tuple(result.x)
|
||||||
offset = items[:,4].mean()
|
offset = items[:, 4].mean()
|
||||||
self.analog_offsets = {'A': -offset, 'B': +offset}
|
self.analog_offsets = {'A': -offset, 'B': +offset}
|
||||||
else:
|
else:
|
||||||
Log.warning("Calibration failed: {}".format(result.message))
|
Log.warning("Calibration failed: {}".format(result.message))
|
||||||
|
29
streams.py
29
streams.py
@ -15,6 +15,8 @@ class SerialStream:
|
|||||||
self._connection = serial.Serial(self._device, timeout=0, write_timeout=0, **kwargs)
|
self._connection = serial.Serial(self._device, timeout=0, write_timeout=0, **kwargs)
|
||||||
self._loop = loop if loop is not None else asyncio.get_event_loop()
|
self._loop = loop if loop is not None else asyncio.get_event_loop()
|
||||||
self._input_buffer = bytes()
|
self._input_buffer = bytes()
|
||||||
|
self._output_buffer = bytes()
|
||||||
|
self._output_wait = None
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return '<{}:{}>'.format(self.__class__.__name__, self._device)
|
return '<{}:{}>'.format(self.__class__.__name__, self._device)
|
||||||
@ -23,21 +25,24 @@ class SerialStream:
|
|||||||
self._connection.close()
|
self._connection.close()
|
||||||
self._connection = None
|
self._connection = None
|
||||||
|
|
||||||
async def write(self, data):
|
def write(self, data):
|
||||||
while data:
|
self._output_buffer += data
|
||||||
n = await self._write(data)
|
if self._output_wait is None:
|
||||||
data = data[n:]
|
self._output_wait = asyncio.Future()
|
||||||
|
self._loop.add_writer(self._connection, self._feed_data)
|
||||||
|
|
||||||
def _write(self, data):
|
async def drain(self):
|
||||||
future = asyncio.Future()
|
if self._output_wait is not None:
|
||||||
self._loop.add_writer(self._connection, self._feed_data, data, future)
|
await self._output_wait
|
||||||
return future
|
|
||||||
|
|
||||||
def _feed_data(self, data, future):
|
def _feed_data(self):
|
||||||
n = self._connection.write(data)
|
n = self._connection.write(self._output_buffer)
|
||||||
Log.debug('Write {}'.format(repr(data[:n])))
|
Log.debug('Write {}'.format(repr(self._output_buffer[:n])))
|
||||||
future.set_result(n)
|
self._output_buffer = self._output_buffer[n:]
|
||||||
|
if not self._output_buffer:
|
||||||
self._loop.remove_writer(self._connection)
|
self._loop.remove_writer(self._connection)
|
||||||
|
self._output_wait.set_result(None)
|
||||||
|
self._output_wait = None
|
||||||
|
|
||||||
async def read(self, n=None):
|
async def read(self, n=None):
|
||||||
while True:
|
while True:
|
||||||
|
14
vm.py
14
vm.py
@ -1,5 +1,6 @@
|
|||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
|
from collections import namedtuple
|
||||||
from enum import IntEnum
|
from enum import IntEnum
|
||||||
import logging
|
import logging
|
||||||
import struct
|
import struct
|
||||||
@ -136,6 +137,19 @@ class KitchenSinkB(IntEnum):
|
|||||||
AnalogFilterEnable = 0x80
|
AnalogFilterEnable = 0x80
|
||||||
WaveformGeneratorEnable = 0x40
|
WaveformGeneratorEnable = 0x40
|
||||||
|
|
||||||
|
ClockMode = namedtuple('ClockMode', ('clock_low', 'clock_high', 'clock_max', 'dual', 'sample_width',
|
||||||
|
'TraceMode', 'BufferMode', 'DumpMode'))
|
||||||
|
|
||||||
|
ClockModes = [
|
||||||
|
ClockMode(40, 65536, None, False, 2, TraceMode.Macro, BufferMode.Macro, DumpMode.Native),
|
||||||
|
ClockMode(40, 65536, None, True, 2, TraceMode.MacroChop, BufferMode.MacroChop, DumpMode.Native),
|
||||||
|
ClockMode(15, 40, None, False, 1, TraceMode.Analog, BufferMode.Single, DumpMode.Raw),
|
||||||
|
ClockMode(13, 40, None, True, 1, TraceMode.AnalogChop, BufferMode.Chop, DumpMode.Raw),
|
||||||
|
ClockMode( 8, 14, None, False, 1, TraceMode.AnalogFast, BufferMode.Single, DumpMode.Raw),
|
||||||
|
ClockMode( 8, 40, None, True, 1, TraceMode.AnalogFastChop, BufferMode.Chop, DumpMode.Raw),
|
||||||
|
ClockMode( 2, 8, 5, False, 1, TraceMode.AnalogShot, BufferMode.Single, DumpMode.Raw),
|
||||||
|
ClockMode( 4, 8, 5, True, 1, TraceMode.AnalogShotChop, BufferMode.Chop, DumpMode.Raw),
|
||||||
|
]
|
||||||
|
|
||||||
def encode(value, dtype):
|
def encode(value, dtype):
|
||||||
sign = dtype[0]
|
sign = dtype[0]
|
||||||
|
Reference in New Issue
Block a user