/** * Performs a gain calibration with specified parameters. It is important that this is not executed while * the ADC is performing anything other than it's idle task. * * @param keys char[][] C-String of the command parameter keys. * @param values char[][] C-String of the command parameter values. * @param count uint8_t The number of command parameters. * @retval Tekdaqc_Function_Error_t The function error status. */ Tekdaqc_Function_Error_t PerformSystemGainCalibration(char keys[][MAX_COMMANDPART_LENGTH], char values[][MAX_COMMANDPART_LENGTH], uint8_t count) { Tekdaqc_Function_Error_t retval = ERR_FUNCTION_OK; retval = SetADCParameters(keys, values, count); PhysicalAnalogInput_t input = EXTERNAL_0; int8_t index = -1; for (uint_fast8_t i = 0; i < NUM_SYSTEM_GCAL_PARAMS; ++i) { index = GetIndexOfArgument(keys, SYSTEM_GCAL_PARAMS[i], count); if (index >= 0) { /* We found the key in the list */ switch (i) { /* Switch on the key not position in arguments list */ case 0: /* INPUT key */ input = ADS1256_StringToBuffer(values[index]); break; default: retval = ERR_CALIBRATION_PARSE_ERROR; } } else { /* Somehow an error happened */ #ifdef CALIBRATION_DEBUG printf("[Command Interpreter] Unable to locate key: %s\n\r", READ_ANALOG_KEYS[i]); #endif retval = ERR_CALIBRATION_MISSING_KEY; /* Failed to locate a key */ } } ADC_GainCalibrate(input); return retval; }
/** * Retrieves the self gain calibration value stored in the base gain calibration table for the specified parameters. * * @param cal uint32_t* Pointer to the variable in which to store the calibration value. * @param keys char[][] C-String of the command parameter keys. * @param values char[][] C-String of the command parameter values. * @param count uint8_t The number of command parameters. * @retval Tekdaqc_Function_Error_t The function error status. */ Tekdaqc_Function_Error_t GetSelfGainCalibration(uint32_t* cal, char keys[][MAX_COMMANDPART_LENGTH], char values[][MAX_COMMANDPART_LENGTH], uint8_t count) { Tekdaqc_Function_Error_t retval = ERR_FUNCTION_OK; char* param; int8_t index = -1; /* Set the default parameters */ ADS1256_BUFFER_t buffer; ADS1256_SPS_t rate; ADS1256_PGA_t gain; uint_fast8_t i = 0U; for (; i < NUM_READ_SELF_GCAL_PARAMS; ++i) { index = GetIndexOfArgument(keys, READ_SELF_GCAL_PARAMS[i], count); if (index >= 0) { /* We found the key in the list */ param = values[index]; /* We use the discovered index for this key */ switch (i) { /* Switch on the key not position in arguments list */ case 0U: /* BUFFER key */ buffer = ADS1256_StringToBuffer(param); break; case 1U: /* RATE key */ rate = ADS1256_StringToDataRate(param); break; case 2U: /* GAIN key */ gain = ADS1256_StringToPGA(param); break; default: retval = ERR_CALIBRATION_PARSE_ERROR; } } else { /* Somehow an error happened */ #ifdef CALIBRATION_DEBUG printf("[Calibration Process] Unable to locate required key: %s\n\r", GET_SELF_GCAL_PARAMS[i]); #endif retval = ERR_CALIBRATION_MISSING_KEY; /* Failed to locate a key */ } if (retval != ERR_FUNCTION_OK) { break; } } if (retval == ERR_FUNCTION_OK) { *cal = Tekdaqc_GetBaseGainCalibration(rate, gain, buffer); } return retval; /* Return the status */ }
/** * Removes an digital output from the board's list based on the supplied parameters. * * @param keys char** Array of strings containing the command line keys. Indexed with values. * @param values char** Array of strings containing the command line values. Indexed with keys. * @param count uint8_t The number of parameters passed on the command line. * @retval int The error status code. */ Tekdaqc_Function_Error_t RemoveDigitalOutput(char keys[][MAX_COMMANDPART_LENGTH], char values[][MAX_COMMANDPART_LENGTH], int count) { Tekdaqc_Function_Error_t retval = ERR_FUNCTION_OK; char* param; int8_t index = -1; for (uint_fast8_t i = 0U; i < NUM_REMOVE_DIGITAL_OUTPUT_PARAMS; ++i) { index = GetIndexOfArgument(keys, REMOVE_DIGITAL_OUTPUT_PARAMS[i], count); if (index >= 0) { //We found the key in the list param = values[index]; //We use the discovered index for this key switch (i) { //Switch on the key not position in arguments list case 0U: { //OUTPUT key char* testPtr = NULL; uint8_t out = (uint8_t) strtol(param, &testPtr, 10); if (testPtr == param) { retval = ERR_DOUT_PARSE_ERROR; } else { if (isExternalOutput(out)) { /* A valid input number */ RemoveDigitalOutputByID(out); } else { /* Input number out of range */ #ifdef DIGITALOUTPUT_DEBUG printf("[Digital Output] The requested input number is invalid.\n\r"); #endif retval = ERR_DOUT_OUTPUT_OUTOFRANGE; } } break; } default: /* Return an error */ retval = ERR_DOUT_PARSE_ERROR; } } else { /* Somehow an error happened */ #ifdef DIGITALINPUT_DEBUG printf("[Digital Output] Unable to locate required key: %s\n\r", REMOVE_DIGITAL_OUTPUT_PARAMS[i]); #endif retval = ERR_DOUT_PARSE_MISSING_KEY; /* Failed to locate a key */ } } return retval; }
/** * Sets the ADC parameters for the upcoming calibration to the specified values. * * @param keys char[][] C-String of the command parameter keys. * @param values char[][] C-String of the command parameter values. * @param count uint8_t The number of command parameters. * @retval Tekdaqc_Function_Error_t The function error status. */ static Tekdaqc_Function_Error_t SetADCParameters(char keys[][MAX_COMMANDPART_LENGTH], char values[][MAX_COMMANDPART_LENGTH], uint8_t count) { Tekdaqc_Function_Error_t retval = ERR_FUNCTION_OK; int8_t index = -1; ADS1256_PGA_t pga = ADS1256_PGAx1; ADS1256_SPS_t rate = ADS1256_SPS_60; ADS1256_BUFFER_t buffer = ADS1256_BUFFER_DISABLED; for (uint_fast8_t i = 0; i < NUM_SYSTEM_CAL_PARAMS; ++i) { index = GetIndexOfArgument(keys, SYSTEM_CAL_PARAMS[i], count); if (index >= 0) { /* We found the key in the list */ switch (i) { /* Switch on the key not position in arguments list */ case 0: /* BUFFER key */ buffer = ADS1256_StringToBuffer(values[index]); break; case 1: /* RATE key */ rate = ADS1256_StringToDataRate(values[index]); /* We use the discovered index for this key */ break; case 2: /* GAIN key */ pga = ADS1256_StringToPGA(values[index]); /* We use the discovered index for this key */ break; default: retval = ERR_AIN_PARSE_ERROR; } } else if (i == 0 || i == 1 || i == 2 || i == 3) { /* The GAIN, RATE and BUFFER keys are not strictly required */ continue; } else { /* Somehow an error happened */ #ifdef CALIBRATION_DEBUG printf("[Calibration Process] Unable to locate key: %s\n\r", READ_ANALOG_KEYS[i]); #endif retval = ERR_AIN_PARSE_MISSING_KEY; /* Failed to locate a key */ } } ADS1256_SetInputBufferSetting(buffer); ADS1256_SetDataRate(rate); ADS1256_SetPGASetting(pga); return retval; }
Tekdaqc_Function_Error_t SetDigitalOutput(char keys[][MAX_COMMANDPART_LENGTH], char values[][MAX_COMMANDPART_LENGTH], uint8_t count) { Tekdaqc_Function_Error_t retval = ERR_FUNCTION_OK; char* param; int8_t index = -1; uint8_t output = NULL_CHANNEL; /* The physical input */ DigitalLevel_t level = OUTPUT_OFF; for (int i = 0; i < NUM_SET_DIGITAL_OUTPUT_PARAMS; ++i) { index = GetIndexOfArgument(keys, SET_DIGITAL_OUTPUT_PARAMS[i], count); if (index >= 0) { /* We found the key in the list */ param = values[index]; /* We use the discovered index for this key */ switch (i) { /* Switch on the key not position in arguments list */ case 0: { /* OUTPUT key */ char* testPtr = NULL; uint8_t out = (uint8_t) strtol(param, &testPtr, 10); if (testPtr == param) { retval = ERR_DOUT_PARSE_ERROR; } else { if (out >= 0U && out <= NUM_DIGITAL_OUTPUTS) { /* A valid input number */ output = out; } else { /* Input number out of range */ #ifdef DIGITALOUTPUT_DEBUG printf("[Digital Output] The requested output number is invalid.\n\r"); #endif retval = ERR_DOUT_OUTPUT_OUTOFRANGE; } } break; } case 1: /* STATE key */ if (strcmp(param, "ON") == 0) { level = OUTPUT_ON; } else { level = OUTPUT_OFF; } break; default: /* Return an error */ retval = ERR_DOUT_PARSE_ERROR; } } else { #ifdef DIGITALOUTPUT_DEBUG printf("[Digital Output] Unable to locate required key: %s\n\r", SET_DIGITAL_OUTPUT_PARAMS[i]); #endif retval = ERR_DOUT_PARSE_MISSING_KEY; /* Failed to locate a key */ } if (retval != ERR_FUNCTION_OK) { break; } } if (retval == ERR_FUNCTION_OK) { if (output != NULL_CHANNEL) { Digital_Output_t* dig_output = GetDigitalOutputByNumber(output); if (dig_output != NULL) { if (dig_output->added == CHANNEL_ADDED) { /* Set the state */ uint8_t control = TLE7232_ReadRegister(TLE7232_REG_CTL, dig_output->output % 8U); uint8_t channel = dig_output->output; if (channel >= 8) { /* Convert the channel to a single chip index */ channel -= 8; } uint8_t mask = 0x01 << channel; /* Move the bit flag to the correct position */ if (level == OUTPUT_ON) { control |= mask; /* Set the appropriate bit */ } else { control ^= mask; /* Clear the appropriate bit */ } TLE7232_WriteRegister(TLE7232_REG_CTL, control, dig_output->output %8U); /* Update the register */ } else { #ifdef DIGITALOUTPUT_DEBUG printf("[Digital Output] Tried to change the state of an output which has not been added.\n\r"); #endif retval = ERR_DOUT_DOES_NOT_EXIST; } } else { /* The output could not be found */ retval = ERR_DOUT_OUTPUT_NOT_FOUND; } } else { /* A valid input was never specified */ retval = ERR_DOUT_OUTPUT_UNSPECIFIED; } } return retval; }
/** * Creates a new digital output data structure from the supplied parameters and adds it to the board's relevant * output list. * * @param keys char** Array of strings containing the command line keys. Indexed with values. * @param values char** Array of strings containing the command line values. Indexed with keys. * @param count uint8_t The number of parameters passed on the command line. * @retval uint8_t The error status code. */ Tekdaqc_Function_Error_t CreateDigitalOutput(char keys[][MAX_COMMANDPART_LENGTH], char values[][MAX_COMMANDPART_LENGTH], int count) { Tekdaqc_Function_Error_t retval = ERR_FUNCTION_OK; char* param; int8_t index = -1; uint8_t output = NULL_CHANNEL; /* The physical input */ char name[MAX_DIGITAL_OUTPUT_NAME_LENGTH]; /* The name */ strcpy(name, "NONE"); for (int i = 0; i < NUM_ADD_DIGITAL_OUTPUT_PARAMS; ++i) { index = GetIndexOfArgument(keys, ADD_DIGITAL_OUTPUT_PARAMS[i], count); if (index >= 0) { /* We found the key in the list */ param = values[index]; /* We use the discovered index for this key */ switch (i) { /* Switch on the key not position in arguments list */ case 0: { /* OUTPUT key */ char* testPtr = NULL; uint8_t out = (uint8_t) strtol(param, &testPtr, 10); if (testPtr == param) { retval = ERR_DOUT_PARSE_ERROR; } else { if (out >= 0U && out <= NUM_DIGITAL_OUTPUTS) { /* A valid input number */ output = out; } else { /* Input number out of range */ #ifdef DIGITALOUTPUT_DEBUG printf("[Digital Output] The requested output number is invalid.\n\r"); #endif retval = ERR_DOUT_OUTPUT_OUTOFRANGE; } } break; } case 1: /* NAME key */ strcpy(name, param); break; default: /* Return an error */ retval = ERR_DOUT_PARSE_ERROR; } } else if (i == 1) { /* The NAME key is not strictly required, apply the defaults */ continue; } else { /* Somehow an error happened */ #ifdef DIGITALOUTPUT_DEBUG printf("[Digital Output] Unable to locate required key: %s\n\r", ADD_DIGITAL_OUTPUT_PARAMS[i]); #endif retval = ERR_DOUT_PARSE_MISSING_KEY; /* Failed to locate a key */ } if (retval != ERR_FUNCTION_OK) { break; } } if (retval == ERR_FUNCTION_OK) { if (output != NULL_CHANNEL) { Digital_Output_t* dig_output = GetDigitalOutputByNumber(output); if (dig_output != NULL) { if (dig_output->added != CHANNEL_NOTADDED) { dig_output->output = output; strcpy(dig_output->name, name); dig_output->level = LOGIC_LOW; dig_output->timestamp = 0U; AddDigitalOutput(dig_output); } else { retval = ERR_DOUT_OUTPUT_EXISTS; } } else { /* The output could not be found */ retval = ERR_DOUT_OUTPUT_NOT_FOUND; } } else { /* A valid input was never specified */ retval = ERR_DOUT_OUTPUT_UNSPECIFIED; } } return retval; }
/** * Writes a gain calibration value into the FLASH calibration table. * * @param keys char[][] C-String of the command parameter keys. * @param values char[][] C-String of the command parameter values. * @param count uint8_t The number of command parameters. * @retval Tekdaqc_Function_Error_t The function error status. */ Tekdaqc_Function_Error_t Tekdaqc_WriteGainCalibrationValue(char keys[][MAX_COMMANDPART_LENGTH], char values[][MAX_COMMANDPART_LENGTH], uint8_t count) { Tekdaqc_Function_Error_t retval = ERR_FUNCTION_OK; char* param; int8_t index = -1; /* Set the default parameters */ float value; ADS1256_BUFFER_t buffer; ADS1256_SPS_t rate; ADS1256_PGA_t gain; ANALOG_INPUT_SCALE_t scale; uint8_t temperature; uint_fast8_t i = 0U; for (; i < NUM_WRITE_GAIN_CALIBRATION_VALUE_PARAMS; ++i) { index = GetIndexOfArgument(keys, WRITE_GAIN_CALIBRATION_VALUE_PARAMS[i], count); if (index >= 0) { /* We found the key in the list */ param = values[index]; /* We use the discovered index for this key */ switch (i) { /* Switch on the key not position in arguments list */ case 0U: { /* VALUE key */ char* testPtr = NULL; value = strtof(param, &testPtr); if (testPtr == param) { retval = ERR_CALIBRATION_PARSE_ERROR; } break; } case 1U: /* GAIN key */ gain = ADS1256_StringToPGA(param); break; case 2U: /* RATE key */ rate = ADS1256_StringToDataRate(param); break; case 3U: /* BUFFER key */ buffer = ADS1256_StringToBuffer(param); break; case 4U: /* SCALE key */ scale = Tekdaqc_StringToAnalogInputScale(param); break; case 5U: /* INDEX key */ errno = 0; /* Set the global error number to 0 so we get a valid check */ temperature = (uint8_t) strtol(values[index], NULL, 0); if (errno != 0) { retval = ERR_CALIBRATION_PARSE_ERROR; break; } break; default: retval = ERR_CALIBRATION_PARSE_ERROR; } } else { /* Somehow an error happened */ #ifdef CALIBRATION_DEBUG printf("[Calibration Process] Unable to locate required key: %s\n\r", WRITE_GAIN_CALIBRATION_VALUE_PARAMS_PARAMS[i]); #endif retval = ERR_CALIBRATION_MISSING_KEY; /* Failed to locate a key */ } if (retval != ERR_FUNCTION_OK) { break; } } if (retval == ERR_FUNCTION_OK) { printf("Writing gain calibration value 0x%0.4f for Rate: %s, Gain %s, Buffer: %s\n\r", value, ADS1256_StringFromSPS(rate), ADS1256_StringFromPGA(gain), ADS1256_StringFromBuffer(buffer)); if (Tekdaqc_SetGainCalibration(value, rate, gain, buffer, scale, temperature) != FLASH_COMPLETE) { retval = ERR_CALIBRATION_WRITE_FAILED; } } return retval; /* Return the status */ }