Skip to content

Commit

Permalink
test new adc sync mode for 1d time series ramp
Browse files Browse the repository at this point in the history
  • Loading branch information
markzakharyan committed Oct 14, 2024
1 parent 5f9cdff commit ef62163
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 30 deletions.
13 changes: 10 additions & 3 deletions m4/src/Peripherals/ADC/ADCBoard.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ class ADCBoard {

public:
inline static PeripheralCommsController1 commsController;
bool data_ready = false;

ADCBoard(int cs_pin, int data_ready_pin, int reset_pin)
: cs_pin(cs_pin), data_ready_pin(data_ready_pin), reset_pin(reset_pin) {}
Expand All @@ -101,6 +102,10 @@ class ADCBoard {

void initialize() {}

int getDataReadyPin() const { return data_ready_pin; }

void setReadyFlag() { data_ready = true; }

double readVoltage(int channel_index) {
startSingleConversion(channel_index);
waitDataReady();
Expand Down Expand Up @@ -275,11 +280,13 @@ class ADCBoard {

// chop = true
float setConversionTime(int channel, float time_us) {
return setConversionTimeFloat(channel, time_us, isMoreThanOneChannelActive());
return setConversionTimeFloat(channel, time_us,
isMoreThanOneChannelActive());
}

// chop = true
float setConversionTimeFloat(int channel, float time_us, bool moreThanOneChannelActive) {
float setConversionTimeFloat(int channel, float time_us,
bool moreThanOneChannelActive) {
return setConversionTime(
channel, true,
calculateFilterWord(time_us, true, moreThanOneChannelActive),
Expand Down Expand Up @@ -373,7 +380,7 @@ class ADCBoard {

void zeroScaleSelfCalibration() {
byte data[2];
data[0] = WRITE | ADDR_MODE(0); // channel is zero but this is system-wide
data[0] = WRITE | ADDR_MODE(0); // channel is zero but this is system-wide
data[1] = ZERO_SCALE_SELF_CAL_MODE;
digitalWrite(cs_pin, LOW);
commsController.transfer(data, 2);
Expand Down
24 changes: 24 additions & 0 deletions m4/src/Peripherals/ADC/ADCController.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,31 @@ class ADCController {
}

inline static void setup() {
pinMode(adc_sync, OUTPUT);
digitalWrite(adc_sync, LOW);
initializeRegistry();
for (auto board : adc_boards) {
board.setup();
// int drdy = board.getDataReadyPin();
attachInterrupt(
digitalPinToInterrupt(47),
[]() { setReadyFlag(0); },
FALLING);
attachInterrupt(
digitalPinToInterrupt(48),
[]() { setReadyFlag(1); },
FALLING);
}
}

inline static void setReadyFlag(int i) {
adc_boards[i].setReadyFlag();
}

inline static bool getReadyFlag(int i) { return adc_boards[i].data_ready; }

inline static void clearReadyFlag(int i) { adc_boards[i].data_ready = false; }

static void initializeRegistry() {
registerMemberFunction(readChannelVoltage, "GET_ADC");
registerMemberFunction(setConversionTime, "CONVERT_TIME");
Expand Down Expand Up @@ -70,6 +89,11 @@ class ADCController {
}
}

inline static void toggleSync() {
digitalWrite(adc_sync, HIGH);
digitalWrite(adc_sync, LOW);
}

inline static float getVoltage(int channel_index) {
return adc_boards[getBoardIndexFromGlobalIndex(channel_index)].readVoltage(
getChannelIndexFromGlobalIndex(channel_index));
Expand Down
2 changes: 0 additions & 2 deletions m4/src/Peripherals/DAC/DACChannel.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ class DACChannel {
void setup() {
pinMode(cs_pin, OUTPUT);
digitalWrite(cs_pin, HIGH);
pinMode(ldac, OUTPUT);
digitalWrite(ldac, HIGH);
}

float setVoltage(float v) {
Expand Down
2 changes: 2 additions & 0 deletions m4/src/Peripherals/DAC/DACController.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ class DACController {
}

inline static void setup() {
pinMode(ldac, OUTPUT);
digitalWrite(ldac, HIGH);
initializeRegistry();
for (auto channel : dac_channels) {
channel.setup();
Expand Down
44 changes: 21 additions & 23 deletions m4/src/Peripherals/God.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ 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 Down Expand Up @@ -106,23 +105,23 @@ class God {
}

while (x < saved_data_size && !getStopFlag()) {
if (TimingUtil::adcFlag) {
if (steps <= 1) {
for (int i = 0; i < numAdcChannels; i++) {
for (int i = 0; i < numAdcChannels; i++) {
if (ADCController::getReadyFlag(adcChannels[i])) {
ADCController::clearReadyFlag(adcChannels[i]);
if (steps <= 1) {
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++;
}
TimingUtil::adcFlag = false;
}
if (TimingUtil::dacFlag && steps < numSteps + 1) {
if (steps == 0) {
Expand Down Expand Up @@ -292,7 +291,7 @@ class God {
} else {
for (int i = 0; i < numDacChannels; i++) {
DACController::setVoltageNoTransactionNoLdac(dacChannels[i],
previousVoltageSet[i]);
previousVoltageSet[i]);
previousVoltageSet[i] += voltageStepSize[i];
}
}
Expand Down Expand Up @@ -343,8 +342,7 @@ 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;

int numDacChannels = static_cast<int>(args[currentIndex++]);
Expand Down Expand Up @@ -382,8 +380,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 @@ -409,7 +407,7 @@ 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;

for (int i = 0; i < numAdcChannels; ++i) {
Expand All @@ -419,7 +417,7 @@ class God {
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) {
if (steps <= 1) {
for (int i = 0; i < numAdcChannels; i++) {
ADCController::getVoltageDataNoTransaction(adcChannels[i]);
Expand Down
4 changes: 2 additions & 2 deletions m4/src/Utils/TimingUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,13 @@ extern "C" void TIM1_UP_IRQHandler(void) {
extern "C" void TIM8_UP_TIM13_IRQHandler(void) {
if (TIM8->SR & TIM_SR_UIF) {
TIM8->SR &= ~TIM_SR_UIF;
TimingUtil::adcFlag = true;
ADCController::toggleSync();
}
}

extern "C" void TIM8_CC_IRQHandler(void) {
if (TIM8->SR & TIM_SR_CC1IF) {
TIM8->SR &= ~TIM_SR_CC1IF;
TimingUtil::adcFlag = true;
ADCController::toggleSync();
}
}

0 comments on commit ef62163

Please sign in to comment.