Skip to content

Commit

Permalink
Merge pull request #340 from open-ephys/issue-334
Browse files Browse the repository at this point in the history
Reduced I2C clock when talking to atMega on miniscope
  • Loading branch information
jonnew authored Oct 21, 2024
2 parents 199c6f9 + 0b05676 commit 1c05d6a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
6 changes: 2 additions & 4 deletions OpenEphys.Onix1/ConfigureUclaMiniscopeV4.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ namespace OpenEphys.Onix1
/// </remarks>
public class ConfigureUclaMiniscopeV4 : MultiDeviceFactory
{

const double MaxVoltage = 5.6;
const double MaxVoltage = 5.7;

PortName port;
readonly ConfigureUclaMiniscopeV4PortController PortControl = new();
Expand Down Expand Up @@ -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)
Expand All @@ -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))
Expand Down
25 changes: 23 additions & 2 deletions OpenEphys.Onix1/ConfigureUclaMiniscopeV4Camera.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 1c05d6a

Please sign in to comment.