From 1338eb9d43f9c2b31c8164e28ef018a40ce7badc Mon Sep 17 00:00:00 2001 From: Mark Zakharyan Date: Sun, 29 Sep 2024 13:41:53 -0700 Subject: [PATCH] made spi transfer slightly faster and added variable number of skips for boxcar --- .../FunctionRegistryHelpers.h | 2 - m4/src/Peripherals/ADC/ADCBoard.h | 99 ++++++++++++------- m4/src/Peripherals/DAC/DACChannel.h | 8 +- m4/src/Peripherals/God.h | 27 +++-- 4 files changed, 77 insertions(+), 59 deletions(-) diff --git a/m4/src/FunctionRegistry/FunctionRegistryHelpers.h b/m4/src/FunctionRegistry/FunctionRegistryHelpers.h index 1686384..cc1abcb 100644 --- a/m4/src/FunctionRegistry/FunctionRegistryHelpers.h +++ b/m4/src/FunctionRegistry/FunctionRegistryHelpers.h @@ -17,8 +17,6 @@ template void registerMemberFunction(Function function, const String& commandName) { using Traits = FunctionTraits; constexpr size_t argCountSizeT = Traits::arity; - static_assert(argCountSizeT <= 8, - "Too many arguments. Maximum supported is 8."); constexpr int argCount = static_cast( argCountSizeT); // Ensure it matches FunctionRegistry's expected type diff --git a/m4/src/Peripherals/ADC/ADCBoard.h b/m4/src/Peripherals/ADC/ADCBoard.h index 9549eca..fe84e49 100644 --- a/m4/src/Peripherals/ADC/ADCBoard.h +++ b/m4/src/Peripherals/ADC/ADCBoard.h @@ -111,36 +111,38 @@ class ADCBoard { // return ADC status register, pg. 16 uint8_t getADCStatus() { - uint8_t data_array; - - data_array = READ | ADDR_ADCSTATUS; + byte* data = new byte[2]; + data[0] = READ | ADDR_ADCSTATUS; commsController.beginTransaction(); digitalWrite(cs_pin, LOW); - commsController.transfer(data_array); - byte b = commsController.receiveByte(); + commsController.transfer(data, 2); digitalWrite(cs_pin, HIGH); commsController.endTransaction(); - return b; + return data[1]; } void channelSetup(int adc_channel, uint8_t flags) { + byte* data = new byte[2]; + data[0] = WRITE | ADDR_CHANNELSETUP(adc_channel); + data[1] = flags; commsController.beginTransaction(); digitalWrite(cs_pin, LOW); - commsController.transfer(WRITE | ADDR_CHANNELSETUP(adc_channel)); - commsController.transfer(flags); + commsController.transfer(data, 2); digitalWrite(cs_pin, HIGH); commsController.endTransaction(); } // tells the ADC to start a single conversion on the passed channel void startSingleConversion(int adc_channel) { - commsController.beginTransaction(); - digitalWrite(cs_pin, LOW); + byte* data = new byte[2]; // setup communication register for writing operation to the mode register - commsController.transfer(WRITE | ADDR_MODE(adc_channel)); + data[0] = WRITE | ADDR_MODE(adc_channel); // setup mode register - commsController.transfer(SINGLE_CONV_MODE); + data[1] = SINGLE_CONV_MODE; + commsController.beginTransaction(); + digitalWrite(cs_pin, LOW); + commsController.transfer(data, 2); digitalWrite(cs_pin, HIGH); commsController.endTransaction(); @@ -172,32 +174,38 @@ class ADCBoard { void setConversionTime(int adc_channel, int chop, int fw) { byte chop_byte = chop == 1 ? 0x80 : 0x00; byte send = chop_byte | static_cast(fw); + + byte* data = new byte[2]; + data[0] = WRITE | ADDR_CHANNELCONVERSIONTIME(adc_channel); + data[1] = send; + commsController.beginTransaction(); digitalWrite(cs_pin, LOW); - commsController.transfer(WRITE | ADDR_CHANNELCONVERSIONTIME(adc_channel)); - commsController.transfer(send); + commsController.transfer(data, 2); digitalWrite(cs_pin, HIGH); commsController.endTransaction(); } uint32_t getConversionData(int adc_channel) { byte data_array; - uint32_t upper, lower, last; // setup communication register for reading channel data data_array = READ | ADDR_CHANNELDATA(adc_channel); + byte* data = new byte[4]; + data[0] = data_array; // write to the communication register commsController.beginTransaction(); digitalWrite(cs_pin, LOW); - commsController.transfer(data_array); // read upper and lower bytes of channel data register (16 bit mode) - upper = commsController.receiveByte(); - lower = commsController.receiveByte(); - last = commsController.receiveByte(); + commsController.transfer(data, 4); digitalWrite(cs_pin, HIGH); commsController.endTransaction(); + uint32_t upper = data[1]; + uint32_t lower = data[2]; + uint32_t last = data[3]; + uint32_t result = upper << 16 | lower << 8 | last; return result; @@ -205,20 +213,23 @@ class ADCBoard { uint32_t getConversionDataNoTransaction(int adc_channel) { byte data_array; - uint32_t upper, lower, last; // setup communication register for reading channel data data_array = READ | ADDR_CHANNELDATA(adc_channel); + byte* data = new byte[4]; + data[0] = data_array; + // write to the communication register digitalWrite(cs_pin, LOW); - commsController.transfer(data_array); // read upper and lower bytes of channel data register (16 bit mode) - upper = commsController.receiveByte(); - lower = commsController.receiveByte(); - last = commsController.receiveByte(); + commsController.transfer(data, 4); digitalWrite(cs_pin, HIGH); + uint32_t upper = data[1]; + uint32_t lower = data[2]; + uint32_t last = data[3]; + uint32_t result = upper << 16 | lower << 8 | last; return result; @@ -238,10 +249,12 @@ class ADCBoard { } void idleMode(int adc_channel) { + byte* data = new byte[2]; + data[0] = WRITE | ADDR_MODE(adc_channel); + data[1] = IDLE_MODE; commsController.beginTransaction(); digitalWrite(cs_pin, LOW); - commsController.transfer(WRITE | ADDR_MODE(adc_channel)); - commsController.transfer(IDLE_MODE); + commsController.transfer(data, 2); digitalWrite(cs_pin, HIGH); commsController.endTransaction(); } @@ -306,10 +319,14 @@ class ADCBoard { byte chop_byte = chop ? 0b10000000 : 0b00000000; byte send = chop_byte | fw; + + byte* data = new byte[2]; + data[0] = WRITE | ADDR_CHANNELCONVERSIONTIME(channel); + data[1] = send; + commsController.beginTransaction(); digitalWrite(cs_pin, LOW); - commsController.transfer(WRITE | ADDR_CHANNELCONVERSIONTIME(channel)); - commsController.transfer(send); + commsController.transfer(data, 2); digitalWrite(cs_pin, HIGH); commsController.endTransaction(); @@ -325,14 +342,15 @@ class ADCBoard { } float getConversionTime(int channel, bool moreThanOneChannelActive) { + byte* data = new byte[2]; + data[0] = READ | ADDR_CHANNELCONVERSIONTIME(channel); commsController.beginTransaction(); digitalWrite(cs_pin, LOW); - commsController.transfer(READ | ADDR_CHANNELCONVERSIONTIME(channel)); - byte b = commsController.receiveByte(); + commsController.transfer(data, 2); digitalWrite(cs_pin, HIGH); commsController.endTransaction(); - return calculateConversionTime(b, moreThanOneChannelActive); + return calculateConversionTime(data[1], moreThanOneChannelActive); } float calculateConversionTime(byte b, bool moreThanOneChannelActive) { @@ -383,31 +401,36 @@ class ADCBoard { } void zeroScaleSelfCalibration() { + byte* data = new byte[2]; + data[0] = WRITE | ADDR_MODE(0); // channel is zero but this is system-wide + data[1] = ZERO_SCALE_SELF_CAL_MODE; commsController.beginTransaction(); digitalWrite(cs_pin, LOW); - commsController.transfer( - WRITE | ADDR_MODE(0)); // channel is zero but this is system-wide - commsController.transfer(ZERO_SCALE_SELF_CAL_MODE); + commsController.transfer(data, 2); digitalWrite(cs_pin, HIGH); commsController.endTransaction(); waitDataReady(); } void zeroScaleChannelSystemSelfCalibration(int channel) { + byte* data = new byte[2]; + data[0] = WRITE | ADDR_MODE(channel); + data[1] = CH_ZERO_SCALE_SYS_CAL_MODE; commsController.beginTransaction(); digitalWrite(cs_pin, LOW); - commsController.transfer(WRITE | ADDR_MODE(channel)); - commsController.transfer(CH_ZERO_SCALE_SYS_CAL_MODE); + commsController.transfer(data, 2); digitalWrite(cs_pin, HIGH); commsController.endTransaction(); waitDataReady(); } void fullScaleChannelSystemSelfCalibration(int channel) { + byte* data = new byte[2]; + data[0] = WRITE | ADDR_MODE(channel); + data[1] = CH_FULL_SCALE_SYS_CAL_MODE; commsController.beginTransaction(); digitalWrite(cs_pin, LOW); - commsController.transfer(WRITE | ADDR_MODE(channel)); - commsController.transfer(CH_FULL_SCALE_SYS_CAL_MODE); + commsController.transfer(data, 2); digitalWrite(cs_pin, HIGH); commsController.endTransaction(); waitDataReady(); diff --git a/m4/src/Peripherals/DAC/DACChannel.h b/m4/src/Peripherals/DAC/DACChannel.h index e569362..83ab812 100644 --- a/m4/src/Peripherals/DAC/DACChannel.h +++ b/m4/src/Peripherals/DAC/DACChannel.h @@ -129,20 +129,18 @@ class DACChannel { float getVoltage() { byte bytesToSend[3] = {144, 0, 0}; + byte data[3]; commsController.beginTransaction(); digitalWrite(cs_pin, LOW); commsController.transfer(bytesToSend, 3); digitalWrite(cs_pin, HIGH); delayMicroseconds(2); digitalWrite(cs_pin, LOW); - byte b1 = commsController.receiveByte(); - byte b2 = commsController.receiveByte(); - byte b3 = commsController.receiveByte(); + commsController.transfer(data, 3); digitalWrite(cs_pin, HIGH); commsController.endTransaction(); - // Assuming threeByteToVoltage function exists and works correctly - float voltage = threeByteToVoltage(b1, b2, b3); + float voltage = threeByteToVoltage(data[0], data[1], data[2]); return gain_error * (voltage + offset_error); } diff --git a/m4/src/Peripherals/God.h b/m4/src/Peripherals/God.h index ab24325..5f1e1b5 100644 --- a/m4/src/Peripherals/God.h +++ b/m4/src/Peripherals/God.h @@ -364,9 +364,9 @@ class God { } // args: numDacChannels, numAdcChannels, numDacSteps, - // numAdcMeasuresPerDacStep, numAdcAverages, adcConversionTime_us, {for each - // dac channel: dac channel, v0_1, vf_1, v0_2, vf_2}, {for each adc channel: - // adc channel} + // 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& args) { size_t currentIndex = 0; @@ -376,6 +376,7 @@ class God { int numDacSteps = static_cast(args[currentIndex++]); int numAdcMeasuresPerDacStep = static_cast(args[currentIndex++]); int numAdcAverages = static_cast(args[currentIndex++]); + int numAdcConversionSkips = static_cast(args[currentIndex++]); uint32_t adcConversionTime_us = static_cast(args[currentIndex++]); int* dacChannels = new int[numDacChannels]; @@ -405,8 +406,8 @@ class God { numAdcChannels > 1); } - uint32_t dacPeriod_us = numAdcMeasuresPerDacStep * actualConversionTime_us + - 10 + 5 + actualConversionTime_us; + uint32_t dacPeriod_us = (numAdcMeasuresPerDacStep + numAdcConversionSkips) * + actualConversionTime_us + 500; setStopFlag(false); @@ -431,7 +432,7 @@ class God { int x = 0; int total_data_size = numDacSteps * numAdcMeasuresPerDacStep * numAdcAverages; - bool justSetDac = false; + int adcGetsSinceLastDacSet = 0; // for debugging: float dacPeriodFloat = static_cast(dacPeriod_us); @@ -453,21 +454,19 @@ class God { ADCController::getVoltageDataNoTransaction(adcChannels[i]); } } else { - if (justSetDac) { - justSetDac = false; - } else { + if (adcGetsSinceLastDacSet >= numAdcConversionSkips) { VoltagePacket* packets = new VoltagePacket[numAdcChannels]; for (int i = 0; i < numAdcChannels; i++) { float v = ADCController::getVoltageDataNoTransaction(adcChannels[i]); - packets[i] = {static_cast(adcChannels[i]), static_cast(x), - v}; + packets[i] = {static_cast(adcChannels[i]), + static_cast(x), v}; } // m4SendVoltage(packets, numAdcChannels); delete[] packets; - x++; - + x++; } + adcGetsSinceLastDacSet++; } ADCBoard::commsController.endTransaction(); TimingUtil::adcFlag = false; @@ -489,7 +488,7 @@ class God { DACChannel::commsController.endTransaction(); steps++; TimingUtil::dacFlag = false; - justSetDac = true; + adcGetsSinceLastDacSet = 0; TIM8->CNT = 0; } }