Skip to content

Commit da8edf2

Browse files
committed
Fetch Probe serial number and disable devices that are not connected
1 parent 1fad414 commit da8edf2

File tree

3 files changed

+51
-43
lines changed

3 files changed

+51
-43
lines changed

Source/Devices/Neuropixels_1.cpp

+36-31
Original file line numberDiff line numberDiff line change
@@ -59,30 +59,37 @@ Neuropixels_1::~Neuropixels_1()
5959

6060
int Neuropixels_1::enableDevice()
6161
{
62-
// Enable device streaming
63-
const oni_reg_addr_t enable_device_stream = 0x8000;
64-
int errorCode = oni_write_reg(ctx, deviceIdx, enable_device_stream, 1);
65-
66-
if (errorCode) { LOGE(oni_error_str(errorCode)); return errorCode; }
62+
// Get Probe SN
63+
uint32_t eepromOffset = 0;
64+
uint32_t i2cAddr = 0x50;
65+
uint64_t probeSN = 0;
66+
int errorCode = 0;
6767

68-
// check probe SN
68+
for (int i = 0; i < 8; i++)
69+
{
70+
oni_reg_addr_t reg_addr = ((eepromOffset + i) << 7) | i2cAddr;
71+
oni_reg_val_t reg_val;
6972

73+
errorCode = oni_read_reg(ctx, deviceIdx, reg_addr, &reg_val);
7074

71-
const oni_reg_addr_t probe_sn_msb = 0x8191;
72-
const oni_reg_addr_t probe_sn_lsb = 0x8192;
75+
if (errorCode) { LOGE(oni_error_str(errorCode)); return -1; }
7376

74-
oni_reg_val_t probe_sn_msb_val;
75-
oni_reg_val_t probe_sn_lsb_val;
77+
if (reg_val <= 0xFF)
78+
{
79+
probeSN |= (((uint64_t)reg_val) << (i * 8));
80+
}
7681

77-
errorCode = oni_read_reg(ctx, deviceIdx, probe_sn_msb, &probe_sn_msb_val);
78-
if (errorCode) { LOGE(oni_error_str(errorCode)); return errorCode; }
82+
}
83+
84+
LOGD ("Probe SN: ", probeSN);
7985

80-
errorCode = oni_read_reg(ctx, deviceIdx, probe_sn_lsb, &probe_sn_lsb_val);
81-
if (errorCode) { LOGE(oni_error_str(errorCode)); return errorCode; }
86+
// Enable device streaming
87+
const oni_reg_addr_t enable_device_stream = 0x8000;
88+
errorCode = oni_write_reg(ctx, deviceIdx, enable_device_stream, 1);
8289

83-
uint64_t probe_sn = ((uint64_t)probe_sn_msb_val << 32) | (uint64_t)probe_sn_lsb_val;
90+
if (errorCode) { LOGE(oni_error_str(errorCode)); return -2; }
8491

85-
LOGC ("******* Probe SN: ", probe_sn);
92+
oni_write_reg(ctx, deviceIdx, (uint32_t)NeuropixelsRegisters::OP_MODE , (uint32_t)OpMode::RECORD);
8693

8794
return 0;
8895
}
@@ -92,13 +99,9 @@ void Neuropixels_1::startAcquisition()
9299
{
93100
startThread();
94101

95-
int errorCode = oni_write_reg(ctx, deviceIdx, (uint32_t)NeuropixelsRegisters::REC_MOD , (uint32_t)RecMod::DIG_RESET);
96-
97-
if (errorCode) { LOGE("[", deviceIdx ,"][Register][OP_MODE] ", oni_error_str(errorCode)); }
98-
99-
errorCode = oni_write_reg(ctx, deviceIdx, (uint32_t)NeuropixelsRegisters::REC_MOD , (uint32_t)RecMod::ACTIVE);
102+
oni_write_reg(ctx, deviceIdx, (uint32_t)NeuropixelsRegisters::REC_MOD , (uint32_t)RecMod::DIG_RESET);
100103

101-
if (errorCode) { LOGE("[Neuropixels 1][", deviceIdx ,"] Error starting acquisition: ", oni_error_str(errorCode)); }
104+
oni_write_reg(ctx, deviceIdx, (uint32_t)NeuropixelsRegisters::REC_MOD , (uint32_t)RecMod::ACTIVE);
102105
}
103106

104107
void Neuropixels_1::stopAcquisition()
@@ -108,13 +111,13 @@ void Neuropixels_1::stopAcquisition()
108111

109112
waitForThreadToExit(2000);
110113

111-
int errorCode = oni_write_reg(ctx, deviceIdx, (uint32_t)NeuropixelsRegisters::REC_MOD , (uint32_t)RecMod::RESET_ALL);
112-
if (errorCode) { LOGE("[Neuropixels 1][", deviceIdx ,"] Error stopping acquisition: ", oni_error_str(errorCode)); }
114+
oni_write_reg(ctx, deviceIdx, (uint32_t)NeuropixelsRegisters::REC_MOD , (uint32_t)RecMod::RESET_ALL);
113115

114116
superFrameCount = 0;
115117
ultraFrameCount = 0;
116118
shouldAddToBuffer = false;
117-
sampleNumber = 0;
119+
apSampleNumber = 0;
120+
lfpSampleNumber = 0;
118121
}
119122

120123

@@ -131,7 +134,7 @@ void Neuropixels_1::addFrame(oni_frame_t* frame)
131134
((uint64_t)(*(uint16_t*)(dataclock + 6)) << 0);
132135
int64_t clockCounter = hubClock * sizeof(hubClock);
133136
apTimestamps[superFrameCount] = clockCounter;
134-
apSampleNumbers[superFrameCount] = sampleNumber++;
137+
apSampleNumbers[superFrameCount] = apSampleNumber++;
135138

136139
for (int i = 0; i < framesPerSuperFrame; i++)
137140
{
@@ -141,14 +144,15 @@ void Neuropixels_1::addFrame(oni_frame_t* frame)
141144
if (superCountOffset == 0)
142145
{
143146
lfpTimestamps[ultraFrameCount] = apTimestamps[superFrameCount];
144-
lfpSampleNumbers[ultraFrameCount] = apSampleNumbers[superFrameCount];
147+
lfpSampleNumbers[ultraFrameCount] = lfpSampleNumber++;
145148
}
146149

147150
for (int adc = 0; adc < 32; adc++)
148151
{
149152

150-
int chanIndex = adcToChannel[adc] + superCountOffset * 2;
151-
lfpSamples[(chanIndex * numUltraFrames) + ultraFrameCount ] = (*(dataPtr + adcToFrameIndex[adc] + dataOffset) >> 5) * 0.195f;
153+
int chanIndex = adcToChannel[adc] + superCountOffset * 2; // map the ADC to muxed channel
154+
lfpSamples[(chanIndex * numUltraFrames) + ultraFrameCount ] =
155+
float(*(dataPtr + adcToFrameIndex[adc] + dataOffset) >> 5) * 0.195f;
152156

153157
}
154158
}
@@ -157,8 +161,9 @@ void Neuropixels_1::addFrame(oni_frame_t* frame)
157161
int chanOffset = 2 * (i - 1);
158162
for (int adc = 0; adc < 32; adc++)
159163
{
160-
int chanIndex = adcToChannel[adc] + chanOffset;
161-
apSamples[(chanIndex * superFramesPerUltraFrame * numUltraFrames) + superFrameCount] = (*(dataPtr + adcToFrameIndex[adc] + dataOffset) >> 5) * 0.195f;
164+
int chanIndex = adcToChannel[adc] + chanOffset; // map the ADC to muxed channel.
165+
apSamples[(chanIndex * superFramesPerUltraFrame * numUltraFrames) + superFrameCount] =
166+
float(*(dataPtr + adcToFrameIndex[adc] + dataOffset) >> 5) * 0.195f;
162167
}
163168
}
164169
}

Source/Devices/Neuropixels_1.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ static const int adcToFrameIndex[] = {
7272
6, 13
7373
};
7474

75+
// ADC to muxed channel mapping
7576
static const int adcToChannel[] = {
7677
0, 1, 24, 25, 48, 49, 72, 73, 96, 97,
7778
120, 121, 144, 145, 168, 169, 192, 193,
@@ -129,12 +130,12 @@ class Neuropixels_1 : public OnixDevice
129130
double lfpTimestamps[numUltraFrames];
130131
uint64 lfpEventCodes[numUltraFrames];
131132

132-
133133
bool shouldAddToBuffer = false;
134134
int superFrameCount = 0;
135135
int ultraFrameCount = 0;
136136

137-
int sampleNumber = 0;
137+
int apSampleNumber = 0;
138+
int lfpSampleNumber = 0;
138139
};
139140

140141

Source/OnixSource.cpp

+12-10
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,19 @@ void OnixSource::initializeDevices()
126126
{
127127
Neuropixels_1* np1 = new Neuropixels_1("Probe-" + String::charToString(probeLetters[npxProbeIdx]), devices[dev_idx].idx, ctx);
128128

129-
if(np1->enableDevice() != 0)
129+
int res = np1->enableDevice();
130+
131+
if (res != 0)
130132
{
131-
LOGE("Error enabling device data stream.");
133+
if (res == -1)
134+
{
135+
LOGE("Device Idx: ", devices[dev_idx].idx, " Unable to read probe serial number. Device not found.");
136+
}
137+
else if (res == -2)
138+
{
139+
LOGE("Device Idx: ", devices[dev_idx].idx, " Error enabling device stream.");
140+
}
141+
132142
delete np1;
133143
continue;
134144
}
@@ -145,14 +155,6 @@ void OnixSource::initializeDevices()
145155
np1->lfpBuffer = sourceBuffers.getLast();
146156
}
147157

148-
// // Enable device stream
149-
// const oni_reg_addr_t enable_device_stream = 0x8000;
150-
// oni_write_reg(ctx, DEVICE_NPX1_1, enable_device_stream, 1);
151-
152-
// oni_write_reg(ctx, DEVICE_NPX1_1, Neuropixels_Registers::REC_MOD , (uint32_t)1 << 5);
153-
154-
// oni_write_reg(ctx, DEVICE_NPX1_1, Neuropixels_Registers::OP_MODE , (uint32_t)1 << 6);
155-
156158
npxProbeIdx++;
157159
}
158160
}

0 commit comments

Comments
 (0)