Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Full shutdown and powerup sequences for MiniscopeV4 Python480 sensor #333

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
12 changes: 12 additions & 0 deletions .bonsai/Bonsai.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<PackageConfiguration xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Packages>
<Package id="AForge" version="2.2.5" />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aacuevas this was probably because you were messing around with the Video package in your bonsai installation, right? I don't think we need these dependencies in our local bonsai used to test this package.

<Package id="AForge.Video" version="2.2.5" />
<Package id="AForge.Video.DirectShow" version="2.2.5" />
<Package id="Bonsai" version="2.8.5" />
<Package id="Bonsai.Core" version="2.8.5" />
<Package id="Bonsai.Design" version="2.8.5" />
Expand All @@ -12,6 +15,8 @@
<Package id="Bonsai.Scripting.Expressions.Design" version="2.8.0" />
<Package id="Bonsai.System" version="2.8.1" />
<Package id="Bonsai.System.Design" version="2.8.0" />
<Package id="Bonsai.Video" version="2.4.0" />
<Package id="Bonsai.Video.Design" version="2.4.0" />
<Package id="Bonsai.Vision" version="2.8.1" />
<Package id="Bonsai.Vision.Design" version="2.8.1" />
<Package id="jacobslusser.ScintillaNET" version="3.6.3" />
Expand Down Expand Up @@ -46,10 +51,15 @@
<AssemblyReference assemblyName="Bonsai.Scripting.Expressions.Design" />
<AssemblyReference assemblyName="Bonsai.System" />
<AssemblyReference assemblyName="Bonsai.System.Design" />
<AssemblyReference assemblyName="Bonsai.Video" />
<AssemblyReference assemblyName="Bonsai.Video.Design" />
<AssemblyReference assemblyName="Bonsai.Vision" />
<AssemblyReference assemblyName="Bonsai.Vision.Design" />
</AssemblyReferences>
<AssemblyLocations>
<AssemblyLocation assemblyName="AForge" processorArchitecture="MSIL" location="Packages/AForge.2.2.5/lib/AForge.dll" />
<AssemblyLocation assemblyName="AForge.Video" processorArchitecture="MSIL" location="Packages/AForge.Video.2.2.5/lib/AForge.Video.dll" />
<AssemblyLocation assemblyName="AForge.Video.DirectShow" processorArchitecture="MSIL" location="Packages/AForge.Video.DirectShow.2.2.5/lib/AForge.Video.DirectShow.dll" />
<AssemblyLocation assemblyName="Bonsai" processorArchitecture="MSIL" location="Packages/Bonsai.2.8.5/lib/net48/Bonsai.exe" />
<AssemblyLocation assemblyName="Bonsai.Core" processorArchitecture="MSIL" location="Packages/Bonsai.Core.2.8.5/lib/net462/Bonsai.Core.dll" />
<AssemblyLocation assemblyName="Bonsai.Design" processorArchitecture="MSIL" location="Packages/Bonsai.Design.2.8.5/lib/net462/Bonsai.Design.dll" />
Expand All @@ -61,6 +71,8 @@
<AssemblyLocation assemblyName="Bonsai.Scripting.Expressions.Design" processorArchitecture="MSIL" location="Packages/Bonsai.Scripting.Expressions.Design.2.8.0/lib/net462/Bonsai.Scripting.Expressions.Design.dll" />
<AssemblyLocation assemblyName="Bonsai.System" processorArchitecture="MSIL" location="Packages/Bonsai.System.2.8.1/lib/net462/Bonsai.System.dll" />
<AssemblyLocation assemblyName="Bonsai.System.Design" processorArchitecture="MSIL" location="Packages/Bonsai.System.Design.2.8.0/lib/net462/Bonsai.System.Design.dll" />
<AssemblyLocation assemblyName="Bonsai.Video" processorArchitecture="MSIL" location="Packages/Bonsai.Video.2.4.0/lib/net462/Bonsai.Video.dll" />
<AssemblyLocation assemblyName="Bonsai.Video.Design" processorArchitecture="MSIL" location="Packages/Bonsai.Video.Design.2.4.0/lib/net462/Bonsai.Video.Design.dll" />
<AssemblyLocation assemblyName="Bonsai.Vision" processorArchitecture="MSIL" location="Packages/Bonsai.Vision.2.8.1/lib/net462/Bonsai.Vision.dll" />
<AssemblyLocation assemblyName="Bonsai.Vision.Design" processorArchitecture="MSIL" location="Packages/Bonsai.Vision.Design.2.8.1/lib/net462/Bonsai.Vision.Design.dll" />
<AssemblyLocation assemblyName="Markdig" processorArchitecture="MSIL" location="Packages/Markdig.0.18.1/lib/net40/Markdig.dll" />
Expand Down
83 changes: 73 additions & 10 deletions OpenEphys.Onix1/ConfigureUclaMiniscopeV4Camera.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Drawing.Design;
using System.Reactive.Disposables;
using System.Reactive.Subjects;
using System.Threading;
using System.Xml.Serialization;
using Bonsai;

Expand Down Expand Up @@ -158,6 +159,9 @@
// turn off LED
var atMega = new I2CRegisterContext(device, UclaMiniscopeV4.AtMegaAddress);
atMega.WriteByte(1, 0xFF);

//Turn off image sensor, recommended by the datasheet to avoid transients at power off
ShutDownCamera(device);
});
return new CompositeDisposable(
ledBrightness.Subscribe(value => SetLedBrightness(device, value)),
Expand Down Expand Up @@ -213,17 +217,10 @@

internal static void ConfigureCameraSystem(DeviceContext device, UclaMiniscopeV4FramesPerSecond frameRate, bool interleaveLed)
{
const int WaitUntilPllSettles = 200;


// set up Python480
var atMega = new I2CRegisterContext(device, UclaMiniscopeV4.AtMegaAddress);
WriteCameraRegister(atMega, 16, 3); // Turn on PLL
//Thread.Sleep(WaitUntilPllSettles); //This sometimes has good effects, sometimes adverse, we just might want to redo this entire section (see issue #331 )
WriteCameraRegister(atMega, 32, 0x7007); // Turn on clock management
//Thread.Sleep(WaitUntilPllSettles);
WriteCameraRegister(atMega, 199, 666); // Defines granularity (unit = 1/PLL clock) of exposure and reset_length
WriteCameraRegister(atMega, 200, 3300); // Set frame rate to 30 Hz
WriteCameraRegister(atMega, 201, 3000); // Set Exposure
ShutDownCamera(device);
StartUpCamera(device);

// set up potentiometer
var tpl0102 = new I2CRegisterContext(device, UclaMiniscopeV4.Tpl0102Address);
Expand All @@ -244,11 +241,77 @@
UclaMiniscopeV4FramesPerSecond.Fps30Hz => 3300,
_ => 3300
};
var atMega = new I2CRegisterContext(device, UclaMiniscopeV4.AtMegaAddress)

Check failure on line 244 in OpenEphys.Onix1/ConfigureUclaMiniscopeV4Camera.cs

View workflow job for this annotation

GitHub Actions / build (debug, ubuntu-latest)

; expected

Check failure on line 244 in OpenEphys.Onix1/ConfigureUclaMiniscopeV4Camera.cs

View workflow job for this annotation

GitHub Actions / build (debug, ubuntu-latest)

; expected

Check failure on line 244 in OpenEphys.Onix1/ConfigureUclaMiniscopeV4Camera.cs

View workflow job for this annotation

GitHub Actions / build (debug, windows-latest)

; expected

Check failure on line 244 in OpenEphys.Onix1/ConfigureUclaMiniscopeV4Camera.cs

View workflow job for this annotation

GitHub Actions / build (debug, windows-latest)

; expected

Check failure on line 244 in OpenEphys.Onix1/ConfigureUclaMiniscopeV4Camera.cs

View workflow job for this annotation

GitHub Actions / build (release, ubuntu-latest)

; expected

Check failure on line 244 in OpenEphys.Onix1/ConfigureUclaMiniscopeV4Camera.cs

View workflow job for this annotation

GitHub Actions / build (release, ubuntu-latest)

; expected

Check failure on line 244 in OpenEphys.Onix1/ConfigureUclaMiniscopeV4Camera.cs

View workflow job for this annotation

GitHub Actions / build (release, windows-latest)

; expected

Check failure on line 244 in OpenEphys.Onix1/ConfigureUclaMiniscopeV4Camera.cs

View workflow job for this annotation

GitHub Actions / build (release, windows-latest)

; expected

atMega.WriteByte(0x04, (uint)(interleaveLed ? 0x00 : 0x03));
WriteCameraRegister(atMega, 200, shutterWidth);
}

internal static void ShutDownCamera(DeviceContext device)
{
var atMega = new I2CRegisterContext(device, UclaMiniscopeV4.AtMegaAddress);
//disable sequencer
WriteCameraRegister(atMega, 192, 0x0802);
//soft power down
WriteCameraRegister(atMega, 112, 0x0000); // Disable LVDS transmitters
WriteCameraRegister(atMega, 72, 0x0010); // Disable charge pump
WriteCameraRegister(atMega, 64, 0x0000); // Disable biasing block
WriteCameraRegister(atMega, 48, 0x0000); // Disable AFE
WriteCameraRegister(atMega, 42, 0x4110); // Configure image core for shutdown
WriteCameraRegister(atMega, 40, 0x0000); // Disable column multiplexer
WriteCameraRegister(atMega, 32, 0x7006); // Disable analog clock
WriteCameraRegister(atMega, 10, 0x0999); // Soft reset

//disable clock mgmt 2
WriteCameraRegister(atMega, 34, 0x0000); // Disable logic blocks
WriteCameraRegister(atMega, 32, 0x7004); // Disable logic clock
Copy link
Member

@jonnew jonnew Oct 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This always throws, indicating that the last line has somehow broken the back channel link or that the python no interacting with the bit-banged i2c to SPI translator correctly.

WriteCameraRegister(atMega, 9, 0x0009); // Soft reset clock generator

//disable clock mgmt 1
WriteCameraRegister(atMega, 16, 0x0000); // Disable PLL
WriteCameraRegister(atMega, 8, 0x0099); // Soft reset PLL


}

internal static void StartUpCamera(DeviceContext device)
{
var atMega = new I2CRegisterContext(device, UclaMiniscopeV4.AtMegaAddress);
//enable clock mgmt 1
WriteCameraRegister(atMega, 2, 0x0000); // Set Monochrome sensor
WriteCameraRegister(atMega, 8, 0x0000); // Release PLL soft reset
WriteCameraRegister(atMega, 16, 0x0003); // Enable PLL
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If i remove lines 266-268 so I can get to this point, then this always throws.

WriteCameraRegister(atMega, 17, 0x2113); // Configure PLL
WriteCameraRegister(atMega, 20, 0x0000); // Configure clock management
WriteCameraRegister(atMega, 26, 0x2280); // Configure PLL lock detector
WriteCameraRegister(atMega, 27, 0x3D2D); // Configure PLL lock detector
WriteCameraRegister(atMega, 32, 0x7014); // Configure clock management
Thread.Sleep(10); //This might not be needed, remove if it hurts

//enable clock mgmt 2
WriteCameraRegister(atMega, 9, 0x0000); // Rlease clock generator reset
WriteCameraRegister(atMega, 32, 0x7006); // Enable logic clock
WriteCameraRegister(atMega, 34, 0x0001); // Enable logic blocks

//set format data
WriteCameraRegister(atMega, 199, 666); // Defines granularity (unit = 1/PLL clock) of exposure and reset_length. PLL clock is 66.666MHz. The 480 docs says it should be 68MHz. Oh, well...
WriteCameraRegister(atMega, 200, 3300); // Set frame rate to 30 Hz
WriteCameraRegister(atMega, 201, 3000); // Set Exposure. This is a parameter we might want to play with to tune image quality

//soft power up
WriteCameraRegister(atMega, 10, 0x0000); // Release soft reset
WriteCameraRegister(atMega, 32, 0x7007); // Enable analog clock
WriteCameraRegister(atMega, 40, 0x0007); // Enable column multiplexer. This is the value used by UCLA, which enables column multiplexer bias. On a reference document I found is set to 0x0003, so not sure what the best setting is
WriteCameraRegister(atMega, 42, 0x4113); // Configure image core
WriteCameraRegister(atMega, 48, 0x0001); // Enable AFE
WriteCameraRegister(atMega, 64, 0x0001); // Enable biasing block
WriteCameraRegister(atMega, 72, 0x0017); // Enable charge pump
WriteCameraRegister(atMega, 112, 0x0000); // Disable LVDS transmitters

//enable sequencer
WriteCameraRegister(atMega, 192, 0x0803);
}

static void WriteCameraRegister(I2CRegisterContext i2c, uint register, uint value)
{
// ATMega -> Python480 passthrough protocol
Expand Down
Loading