Skip to content

Commit

Permalink
Merge branch 'main' into new_hardware
Browse files Browse the repository at this point in the history
  • Loading branch information
markzakharyan committed Oct 14, 2024
2 parents d49fe69 + 2d0d93d commit 5cdd7bf
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 32 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
.vscode/ipch
*.cache*
*.ccls*
*compile_commands*
*compile_commands.json
5 changes: 0 additions & 5 deletions m4/.gitignore

This file was deleted.

8 changes: 8 additions & 0 deletions m4/gen_compile_commands.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import os
Import("env")

# include toolchain paths
env.Replace(COMPILATIONDB_INCLUDE_TOOLCHAIN=True)

# override compilation DB path
env.Replace(COMPILATIONDB_PATH="compile_commands.json")
111 changes: 90 additions & 21 deletions m4/src/Peripherals/God.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ class God {
registerMemberFunctionVector(dacLedBufferRampWrapper,
"DAC_LED_BUFFER_RAMP");
registerMemberFunction(dacChannelCalibration, "DAC_CH_CAL");
registerMemberFunctionVector(boxcarAverageRamp, "BOXCAR_BUFFER_RAMP");
registerMemberFunctionVector(boxcarAverageRamp,
"BOXCAR_BUFFER_RAMP");
}

// args:
Expand All @@ -38,23 +39,27 @@ class God {
uint32_t dac_interval_us = static_cast<uint32_t>(args[3]);
uint32_t adc_interval_us = static_cast<uint32_t>(args[4]);

// Check if we have enough arguments for all DAC and ADC channels
if (args.size() !=
static_cast<size_t>(5 + numDacChannels * 3 + numAdcChannels)) {
return OperationResult::Failure("Incorrect number of arguments");
}

// Allocate memory for DAC and ADC channel information
int* dacChannels = new int[numDacChannels];
float* dacV0s = new float[numDacChannels];
float* dacVfs = new float[numDacChannels];
int* adcChannels = new int[numAdcChannels];

// Parse DAC channel information
for (int i = 0; i < numDacChannels; ++i) {
int baseIndex = 5 + i * 3;
dacChannels[i] = static_cast<int>(args[baseIndex]);
dacV0s[i] = static_cast<float>(args[baseIndex + 1]);
dacVfs[i] = static_cast<float>(args[baseIndex + 2]);
}

// Parse ADC channel information
for (int i = 0; i < numAdcChannels; ++i) {
adcChannels[i] = static_cast<int>(args[5 + numDacChannels * 3 + i]);
}
Expand All @@ -77,12 +82,32 @@ class God {
if (numDacChannels < 1 || numAdcChannels < 1) {
return OperationResult::Failure("Invalid number of channels");
}
// uint32_t adc_comms_period_us = (1.0/SPI_SPEED)*1e6*8*4; // 8 bits per
// byte, 4 bytes per ADC conversion if (adc_interval_us <
// adc_comms_period_us*numAdcChannels) {
// return OperationResult::Failure("ADC interval too short");
// }
// uint32_t dac_comms_period_us = (1.0/SPI_SPEED)*1e6*8*3; // 8 bits per
// byte, 3 bytes per DAC update if (dac_interval_us <
// dac_comms_period_us*numDacChannels) {
// return OperationResult::Failure("DAC interval too short");
// }

int steps = 0;
int x = 0;

const int saved_data_size = numSteps * dac_interval_us / adc_interval_us;

// float** voltSetpoints = new float*[numDacChannels];

// for (int i = 0; i < numDacChannels; i++) {
// voltSetpoints[i] = new float[numSteps];
// for (int j = 0; j < numSteps; j++) {
// voltSetpoints[i][j] =
// dacV0s[i] + (dacVfs[i] - dacV0s[i]) * j / (numSteps - 1);
// }
// }

float* voltageStepSize = new float[numDacChannels];

for (int i = 0; i < numDacChannels; i++) {
Expand All @@ -105,25 +130,28 @@ class God {
}

while (x < saved_data_size && !getStopFlag()) {
for (int i = 0; i < numAdcChannels; i++) {
if (ADCController::getReadyFlag(adcChannels[i])) {
ADCController::clearReadyFlag(adcChannels[i]);
if (steps <= 1) {
if (TimingUtil::adcFlag) {
ADCBoard::commsController.beginTransaction();
if (steps <= 1) {
for (int i = 0; i < numAdcChannels; i++) {
ADCController::getVoltageDataNoTransaction(adcChannels[i]);
} else {
float* packets = new float[numAdcChannels];
for (int i = 0; i < numAdcChannels; i++) {
float v =
ADCController::getVoltageDataNoTransaction(adcChannels[i]);
packets[i] = v;
}
m4SendVoltage(packets, numAdcChannels);
delete[] packets;
x++;
}
} else {
float* packets = new float[numAdcChannels];
for (int i = 0; i < numAdcChannels; i++) {
float v =
ADCController::getVoltageDataNoTransaction(adcChannels[i]);
packets[i] = v;
}
m4SendVoltage(packets, numAdcChannels);
delete[] packets;
x++;
}
ADCBoard::commsController.endTransaction();
TimingUtil::adcFlag = false;
}
if (TimingUtil::dacFlag && steps < numSteps + 1) {
DACChannel::commsController.beginTransaction();
if (steps == 0) {
for (int i = 0; i < numDacChannels; i++) {
DACController::setVoltageNoTransactionNoLdac(dacChannels[i],
Expand All @@ -137,6 +165,7 @@ class God {
}
}
DACController::toggleLdac();
DACChannel::commsController.endTransaction();
steps++;
TimingUtil::dacFlag = false;
}
Expand Down Expand Up @@ -179,23 +208,27 @@ class God {
uint32_t dac_interval_us = static_cast<uint32_t>(args[4]);
uint32_t dac_settling_time_us = static_cast<uint32_t>(args[5]);

// Check if we have enough arguments for all DAC and ADC channels
if (args.size() !=
static_cast<size_t>(6 + numDacChannels * 3 + numAdcChannels)) {
return OperationResult::Failure("Incorrect number of arguments");
}

// Allocate memory for DAC and ADC channel information
int* dacChannels = new int[numDacChannels];
float* dacV0s = new float[numDacChannels];
float* dacVfs = new float[numDacChannels];
int* adcChannels = new int[numAdcChannels];

// Parse DAC channel information
for (int i = 0; i < numDacChannels; ++i) {
int baseIndex = 6 + i * 3;
dacChannels[i] = static_cast<int>(args[baseIndex]);
dacV0s[i] = static_cast<float>(args[baseIndex + 1]);
dacVfs[i] = static_cast<float>(args[baseIndex + 2]);
}

// Parse ADC channel information
for (int i = 0; i < numAdcChannels; ++i) {
adcChannels[i] = static_cast<int>(args[6 + numDacChannels * 3 + i]);
}
Expand Down Expand Up @@ -230,10 +263,27 @@ class God {
"DAC settling time too short for ADC conversion time");
}
}
// uint32_t adc_comms_period_us = (1.0/SPI_SPEED)*1e6*8*4; // 8 bits per
// byte, 4 bytes per ADC conversion if
// (numAdcChannels*numAdcAverages*adc_comms_period_us > dac_interval_us -
// dac_settling_time_us) {
// return OperationResult::Failure("Buffer Ramp limited by ADC SPI
// comms");
// }

int steps = 0;
int x = 0;

// float** voltSetpoints = new float*[numDacChannels];

// for (int i = 0; i < numDacChannels; i++) {
// voltSetpoints[i] = new float[numSteps];
// for (int j = 0; j < numSteps; j++) {
// voltSetpoints[i][j] =
// dacV0s[i] + (dacVfs[i] - dacV0s[i]) * j / (numSteps - 1);
// }
// }

float* voltageStepSize = new float[numDacChannels];

for (int i = 0; i < numDacChannels; i++) {
Expand All @@ -259,6 +309,7 @@ class God {
}
while (x < numSteps && !getStopFlag()) {
if (TimingUtil::adcFlag) {
ADCBoard::commsController.beginTransaction();
if (steps <= 1) {
for (int i = 0; i < numAdcChannels; i++) {
for (int j = 0; j < numAdcAverages; j++) {
Expand All @@ -280,9 +331,11 @@ class God {
delete[] packets;
x++;
}
ADCBoard::commsController.endTransaction();
TimingUtil::adcFlag = false;
}
if (TimingUtil::dacFlag && steps < numSteps + 1) {
DACChannel::commsController.beginTransaction();
if (steps == 0) {
for (int i = 0; i < numDacChannels; i++) {
DACController::setVoltageNoTransactionNoLdac(dacChannels[i],
Expand All @@ -291,11 +344,12 @@ class God {
} else {
for (int i = 0; i < numDacChannels; i++) {
DACController::setVoltageNoTransactionNoLdac(dacChannels[i],
previousVoltageSet[i]);
previousVoltageSet[i]);
previousVoltageSet[i] += voltageStepSize[i];
}
}
DACController::toggleLdac();
DACChannel::commsController.endTransaction();
steps++;
TimingUtil::dacFlag = false;
}
Expand Down Expand Up @@ -342,9 +396,11 @@ class God {
// numAdcMeasuresPerDacStep, numAdcAverages, numAdcConversionSkips,
// adcConversionTime_us, {for each dac channel: dac channel, v0_1, vf_1, v0_2,
// vf_2}, {for each adc channel: adc channel}
static OperationResult boxcarAverageRamp(const std::vector<float>& args) {
static OperationResult boxcarAverageRamp(
const std::vector<float>& args) {
size_t currentIndex = 0;

// Parse initial parameters
int numDacChannels = static_cast<int>(args[currentIndex++]);
int numAdcChannels = static_cast<int>(args[currentIndex++]);
int numDacSteps = static_cast<int>(args[currentIndex++]);
Expand Down Expand Up @@ -380,8 +436,8 @@ class God {
numAdcChannels > 1);
}

uint32_t dacPeriod_us = (numAdcMeasuresPerDacStep + numAdcConversionSkips) *
(actualConversionTime_us + 5) * numAdcChannels;
uint32_t dacPeriod_us =
(numAdcMeasuresPerDacStep + numAdcConversionSkips) * (actualConversionTime_us + 5) * numAdcChannels;

setStopFlag(false);
PeripheralCommsController::dataLedOn();
Expand All @@ -407,17 +463,27 @@ class God {
int steps = 0;
int totalSteps = 2 * numDacSteps * numAdcAverages + 1;
int x = 0;
int total_data_size = (totalSteps - 1) * numAdcMeasuresPerDacStep;
int total_data_size = (totalSteps-1) * numAdcMeasuresPerDacStep;
int adcGetsSinceLastDacSet = 0;

// float thing = static_cast<float>(total_data_size * numAdcChannels * 4);
// m4SendFloat(&thing, 1);

// for debugging:
// float dacPeriodFloat = static_cast<float>(dacPeriod_us);
// m4SendFloat(&dacPeriodFloat, 1);
// float adcPeriodFloat = static_cast<float>(actualConversionTime_us);
// m4SendFloat(&adcPeriodFloat, 1);

for (int i = 0; i < numAdcChannels; ++i) {
ADCController::startContinuousConversion(adcChannels[i]);
}

TimingUtil::setupTimersTimeSeries(dacPeriod_us, actualConversionTime_us);

while (x < total_data_size && !getStopFlag()) {
if (TimingUtil::adcFlag && x < (steps - 1) * numAdcMeasuresPerDacStep) {
if (TimingUtil::adcFlag && x < (steps-1) * numAdcMeasuresPerDacStep) {
ADCBoard::commsController.beginTransaction();
if (steps <= 1) {
for (int i = 0; i < numAdcChannels; i++) {
ADCController::getVoltageDataNoTransaction(adcChannels[i]);
Expand All @@ -436,9 +502,11 @@ class God {
}
}
adcGetsSinceLastDacSet++;
ADCBoard::commsController.endTransaction();
TimingUtil::adcFlag = false;
}
if (TimingUtil::dacFlag && steps < totalSteps) {
DACChannel::commsController.beginTransaction();
if (steps == 0) {
for (int i = 0; i < numDacChannels; i++) {
DACController::setVoltageNoTransactionNoLdac(dacChannels[i],
Expand Down Expand Up @@ -467,6 +535,7 @@ class God {
}
}
DACController::toggleLdac();
DACChannel::commsController.endTransaction();
steps++;
adcGetsSinceLastDacSet = 0;
TimingUtil::dacFlag = false;
Expand Down
5 changes: 0 additions & 5 deletions m7/.gitignore

This file was deleted.

8 changes: 8 additions & 0 deletions m7/gen_compile_commands.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import os
Import("env")

# include toolchain paths
env.Replace(COMPILATIONDB_INCLUDE_TOOLCHAIN=True)

# override compilation DB path
env.Replace(COMPILATIONDB_PATH="compile_commands.json")

0 comments on commit 5cdd7bf

Please sign in to comment.