Skip to content

Commit

Permalink
made spi transfer slightly faster and added variable number of skips …
Browse files Browse the repository at this point in the history
…for boxcar
  • Loading branch information
markzakharyan committed Sep 29, 2024
1 parent 961ee5e commit 1338eb9
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 59 deletions.
2 changes: 0 additions & 2 deletions m4/src/FunctionRegistry/FunctionRegistryHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ template <typename Function>
void registerMemberFunction(Function function, const String& commandName) {
using Traits = FunctionTraits<Function>;
constexpr size_t argCountSizeT = Traits::arity;
static_assert(argCountSizeT <= 8,
"Too many arguments. Maximum supported is 8.");
constexpr int argCount = static_cast<int>(
argCountSizeT); // Ensure it matches FunctionRegistry's expected type

Expand Down
99 changes: 61 additions & 38 deletions m4/src/Peripherals/ADC/ADCBoard.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -172,53 +174,62 @@ class ADCBoard {
void setConversionTime(int adc_channel, int chop, int fw) {
byte chop_byte = chop == 1 ? 0x80 : 0x00;
byte send = chop_byte | static_cast<byte>(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;
}

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;
Expand All @@ -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();
}
Expand Down Expand Up @@ -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();

Expand All @@ -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) {
Expand Down Expand Up @@ -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();
Expand Down
8 changes: 3 additions & 5 deletions m4/src/Peripherals/DAC/DACChannel.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
27 changes: 13 additions & 14 deletions m4/src/Peripherals/God.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<float>& args) {
size_t currentIndex = 0;

Expand All @@ -376,6 +376,7 @@ class God {
int numDacSteps = static_cast<int>(args[currentIndex++]);
int numAdcMeasuresPerDacStep = static_cast<int>(args[currentIndex++]);
int numAdcAverages = static_cast<int>(args[currentIndex++]);
int numAdcConversionSkips = static_cast<int>(args[currentIndex++]);
uint32_t adcConversionTime_us = static_cast<uint32_t>(args[currentIndex++]);

int* dacChannels = new int[numDacChannels];
Expand Down Expand Up @@ -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);

Expand All @@ -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<float>(dacPeriod_us);
Expand All @@ -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<uint8_t>(adcChannels[i]), static_cast<uint32_t>(x),
v};
packets[i] = {static_cast<uint8_t>(adcChannels[i]),
static_cast<uint32_t>(x), v};
}
// m4SendVoltage(packets, numAdcChannels);
delete[] packets;
x++;

x++;
}
adcGetsSinceLastDacSet++;
}
ADCBoard::commsController.endTransaction();
TimingUtil::adcFlag = false;
Expand All @@ -489,7 +488,7 @@ class God {
DACChannel::commsController.endTransaction();
steps++;
TimingUtil::dacFlag = false;
justSetDac = true;
adcGetsSinceLastDacSet = 0;
TIM8->CNT = 0;
}
}
Expand Down

0 comments on commit 1338eb9

Please sign in to comment.