From 0b05676f1d7122b26dc6f6ab282912d0a97b1840 Mon Sep 17 00:00:00 2001 From: jonnew Date: Fri, 18 Oct 2024 15:04:44 -0400 Subject: [PATCH] Reduced I2C clock when talking to atMega on miniscope - Only set reduced (80 kHz) I2C clock when talking to the atMega, which has a bit-banged implementation - For the rest of the chips, reset to to 200 kHz, which seems to provide max sampling on BNO while maintaining reliability --- OpenEphys.Onix1/ConfigureUclaMiniscopeV4.cs | 6 ++--- .../ConfigureUclaMiniscopeV4Camera.cs | 25 +++++++++++++++++-- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/OpenEphys.Onix1/ConfigureUclaMiniscopeV4.cs b/OpenEphys.Onix1/ConfigureUclaMiniscopeV4.cs index 7b77cda..74d635d 100644 --- a/OpenEphys.Onix1/ConfigureUclaMiniscopeV4.cs +++ b/OpenEphys.Onix1/ConfigureUclaMiniscopeV4.cs @@ -22,8 +22,7 @@ namespace OpenEphys.Onix1 /// public class ConfigureUclaMiniscopeV4 : MultiDeviceFactory { - - const double MaxVoltage = 5.6; + const double MaxVoltage = 5.7; PortName port; readonly ConfigureUclaMiniscopeV4PortController PortControl = new(); @@ -158,7 +157,6 @@ override protected bool CheckLinkState(DeviceContext device) const int FailureToWriteRegister = -6; try { - ConfigureUclaMiniscopeV4Camera.ConfigureSerializer(ds90ub9x); ConfigureUclaMiniscopeV4Camera.ConfigureCameraSystem(ds90ub9x, Camera.FrameRate, Camera.InterleaveLed); } catch (ONIException ex) when (ex.Number == FailureToWriteRegister) @@ -173,7 +171,7 @@ override protected bool CheckLinkState(DeviceContext device) bool CheckLinkStateWithRetry(DeviceContext device) { - const int TotalTries = 10; + const int TotalTries = 5; for (int i = 0; i < TotalTries; i++) { if (CheckLinkState(device)) diff --git a/OpenEphys.Onix1/ConfigureUclaMiniscopeV4Camera.cs b/OpenEphys.Onix1/ConfigureUclaMiniscopeV4Camera.cs index 5c3e65a..4a7fb1d 100644 --- a/OpenEphys.Onix1/ConfigureUclaMiniscopeV4Camera.cs +++ b/OpenEphys.Onix1/ConfigureUclaMiniscopeV4Camera.cs @@ -204,15 +204,26 @@ internal static void ConfigureDeserializer(DeviceContext device) i2cAlias = UclaMiniscopeV4.Max14574Address << 1; deserializer.WriteByte((uint)DS90UB9xDeserializerI2CRegister.SlaveID3, i2cAlias); deserializer.WriteByte((uint)DS90UB9xDeserializerI2CRegister.SlaveAlias3, i2cAlias); + + // Nominal I2C rate for miniscope + Set200kHzI2C(device); + } + + static void Set80kHzI2C(DeviceContext device) + { + DS90UB9x.Set933I2CRate(device, 80e3); // Empirical data for reliably communication with atMega } - internal static void ConfigureSerializer(DeviceContext device) + static void Set200kHzI2C(DeviceContext device) { - DS90UB9x.Set933I2CRate(device, 80e3); //This is an arbitrary value that is proven to work, we need to test speed vs reliability vs bno sampling speed + DS90UB9x.Set933I2CRate(device, 200e3); // This allows maximum rate bno sampling } internal static void ConfigureCameraSystem(DeviceContext device, UclaMiniscopeV4FramesPerSecond frameRate, bool interleaveLed) { + // NB: atMega (bit-banded i2c to SPI) requires that we talk slowly + Set80kHzI2C(device); + // set up Python480 var atMega = new I2CRegisterContext(device, UclaMiniscopeV4.AtMegaAddress); WriteCameraRegister(atMega, 16, 3); // Turn on PLL @@ -243,6 +254,10 @@ internal static void ConfigureCameraSystem(DeviceContext device, UclaMiniscopeV4 atMega.WriteByte(0x04, (uint)(interleaveLed ? 0x00 : 0x03)); WriteCameraRegister(atMega, 200, shutterWidth); + + // NB: interaction with the atMega (bit-banded i2c to SPI) requires that we talk slowly, reset to + // talk to normal chips + Set200kHzI2C(device); } static void WriteCameraRegister(I2CRegisterContext i2c, uint register, uint value) @@ -260,17 +275,23 @@ static void WriteCameraRegister(I2CRegisterContext i2c, uint register, uint valu internal static void SetLedBrightness(DeviceContext device, double percent) { + // NB: atMega (bit-banded i2c to SPI) requires that we talk slowly + Set80kHzI2C(device); var atMega = new I2CRegisterContext(device, UclaMiniscopeV4.AtMegaAddress); atMega.WriteByte(0x01, (uint)((percent == 0) ? 0xFF : 0x08)); var tpl0102 = new I2CRegisterContext(device, UclaMiniscopeV4.Tpl0102Address); tpl0102.WriteByte(0x01, (uint)(255 * ((100 - percent) / 100.0))); + Set200kHzI2C(device); } internal static void SetSensorGain(DeviceContext device, UclaMiniscopeV4SensorGain gain) { + // NB: atMega (bit-banded i2c to SPI) requires that we talk slowly + Set80kHzI2C(device); var atMega = new I2CRegisterContext(device, UclaMiniscopeV4.AtMegaAddress); WriteCameraRegister(atMega, 204, (uint)gain); + Set200kHzI2C(device); } internal static void SetLiquidLensVoltage(DeviceContext device, double voltage)