updated max1932, modified ltc2620

This commit is contained in:
2019-01-09 17:41:10 +01:00
parent 81a49babda
commit b1570bde9c
14 changed files with 257 additions and 469 deletions

View File

@ -3,7 +3,6 @@
#include "commonServerFunctions.h" // blackfin.h, ansi.h
#include "common.h"
#include "math.h"
#include <string.h>
/* LTC2620 DAC DEFINES */
@ -25,9 +24,9 @@
#define LTC2620_NUMBITS (24)
#define LTC2620_DAISY_CHAIN_NUMBITS (32) // due to shift register FIXME: was 33 earlier
#define LTC2620_NUMCHANNELS (8)
#define LTC2620_MIN_MV (0)
#define LTC2620_MAX_STEPS (pow(2,12)) // 4096
#define LTC2620_PWR_DOWN_VAL (-100)
#define LTC2620_MIN_VAL (0)
#define LTC2620_MAX_VAL (4095) // 12 bits
uint32_t LTC2620_Reg = 0x0;
uint32_t LTC2620_CsMask = 0x0;
@ -35,7 +34,8 @@ uint32_t LTC2620_ClkMask = 0x0;
uint32_t LTC2620_DigMask = 0x0;
int LTC2620_DigOffset = 0x0;
int LTC2620_Ndac = 0;
int LTC2620_MaxMV = 0;
int LTC2620_MinVoltage = 0;
int LTC2620_MaxVoltage = 0;
/**
* Set Defines
@ -45,16 +45,18 @@ int LTC2620_MaxMV = 0;
* @param dmsk digital output mask
* @param dofst digital output offset
* @param nd total number of dacs for this board (for dac channel and daisy chain chip id)
* @param mv maximum voltage in mV
* @param minMV minimum voltage determined by hardware
* @param maxMV maximum voltage determined by hardware
*/
void LTC2620_SetDefines(uint32_t reg, uint32_t cmsk, uint32_t clkmsk, uint32_t dmsk, int dofst, int nd, int mv) {
void LTC2620_SetDefines(uint32_t reg, uint32_t cmsk, uint32_t clkmsk, uint32_t dmsk, int dofst, int nd, int minMV, int maxMV) {
LTC2620_Reg = reg;
LTC2620_CsMask = cmsk;
LTC2620_ClkMask = clkmsk;
LTC2620_DigMask = dmsk;
LTC2620_DigOffset = dofst;
LTC2620_Ndac = nd;
LTC2620_MaxMV = mv;
LTC2620_MinVoltage = minMV;
LTC2620_MaxVoltage = maxMV;
}
@ -76,7 +78,9 @@ void LTC2620_Disable() {
* @returns FAIL when voltage outside limits, OK if conversion successful
*/
int LTC2620_VoltageToDac(int voltage, int* dacval) {
return Common_VoltageToDac(voltage, dacval, LTC2620_MIN_MV, LTC2620_MaxMV, LTC2620_MAX_STEPS);
return ConvertToDifferentRange(LTC2620_MinVoltage, LTC2620_MaxVoltage,
LTC2620_MIN_VAL, LTC2620_MAX_VAL,
voltage, dacval);
}
@ -87,7 +91,9 @@ int LTC2620_VoltageToDac(int voltage, int* dacval) {
* @returns FAIL when voltage outside limits, OK if conversion successful
*/
int LTC2620_DacToVoltage(int dacval, int* voltage) {
return Common_DacToVoltage(dacval, voltage, LTC2620_MIN_MV, LTC2620_MaxMV, LTC2620_MAX_STEPS);
return ConvertToDifferentRange( LTC2620_MIN_VAL, LTC2620_MAX_VAL,
LTC2620_MinVoltage, LTC2620_MaxVoltage,
dacval, voltage);
}

View File

@ -7,14 +7,19 @@
#define MAX1932_HV_NUMBITS (8)
#define MAX1932_HV_DATA_OFST (0)
#define MAX1932_HV_DATA_MSK (0x000000FF << MAX1932_HV_DATA_OFST)
// on power up, dac = 0xff (1.25)
// higher voltage requires lower dac value, 0 is off
#define MAX1932_MIN_DAC_VAL (0xFF)
#define MAX1932_MAX_DAC_VAL (0x1)
#define MAX1932_POWER_OFF_DAC_VAL (0x0)
uint32_t MAX1932_Reg = 0x0;
uint32_t MAX1932_CsMask = 0x0;
uint32_t MAX1932_ClkMask = 0x0;
uint32_t MAX1932_DigMask = 0x0;
int MAX1932_DigOffset = 0x0;
int MAX1932_MinVoltage = 0;
int MAX1932_MaxVoltage = 0;
/**
* Set Defines
@ -23,15 +28,21 @@ int MAX1932_DigOffset = 0x0;
* @param clkmsk clock output mask
* @param dmsk digital output mask
* @param dofst digital output offset
* @param minMV minimum voltage determined by hardware
* @param maxMV maximum voltage determined by hardware
*/
void MAX1932_SetDefines(uint32_t reg, uint32_t cmsk, uint32_t clkmsk, uint32_t dmsk, int dofst) {
void MAX1932_SetDefines(uint32_t reg, uint32_t cmsk, uint32_t clkmsk, uint32_t dmsk, int dofst,
int minMV, int maxMV) {
MAX1932_Reg = reg;
MAX1932_CsMask = cmsk;
MAX1932_ClkMask = clkmsk;
MAX1932_DigMask = dmsk;
MAX1932_DigOffset = dofst;
MAX1932_MinVoltage = minMV;
MAX1932_MaxVoltage = maxMV;
}
/**
* Disable SPI
*/
@ -42,14 +53,6 @@ void MAX1932_Disable() {
& ~(MAX1932_DigMask));
}
/**
* Configure
*/
void MAX1932_Configure(){
FILE_LOG(logINFOBLUE, ("Configuring MAX1932\n"));
}
/**
* Set value
@ -57,8 +60,35 @@ void MAX1932_Configure(){
*/
void MAX1932_Set (int val) {
FILE_LOG(logDEBUG1, ("\tSetting high voltage to %d\n", val));
if (val < 0)
return FAIL;
uint32_t dacvalue = 0;
// limit values (normally < 60 => 0 (off))
if (val < MAX1932_MinVoltage) {
dacvalue = MAX1932_POWER_OFF_DAC_VAL;
val = 0;
}
// limit values (normally > 200 => 0x1 (max))
else if (val > MAX1932_MaxVoltage) {
dacvalue = MAX1932_MAX_DAC_VAL;
val = MAX1932_MaxVoltage;
}
// convert value
else {
// no failure in conversion as limits handled (range from 0x1 to 0xFF)
ConvertToDifferentRange(MAX1932_MinVoltage, MAX1932_MaxVoltage,
MAX1932_MIN_DAC_VAL, MAX1932_MAX_DAC_VAL,
val, &dacvalue);
dacvalue &= MAX1932_HV_DATA_MSK;
}
FILE_LOG(logINFO, ("\t%dV (dacval %d)\n", val, dacvalue));
serializeToSPI(MAX1932_Reg, dacvalue, MAX1932_CsMask, MAX1932_HV_NUMBITS,
MAX1932_ClkMask, MAX1932_DigMask, MAX1932_DigOffset);
}

View File

@ -1,44 +1,42 @@
#pragma once
/**
* Convert voltage to dac units
* @param voltage value in mv
* @param dacval pointer to value converted to dac units
* @param vmin minimum voltage in mV
* @param vmax maximum voltage in mV
* @param maximum number of steps
* @returns FAIL when voltage outside limits, OK if conversion successful
* Convert a value from a range to a different range (eg voltage to dac or vice versa)
* @param inputMin input minimum
* @param inputMax input maximum
* @param outputMin output minimum
* @param outputMax output maximum
* @param inputValue input value
* @param outputValue pointer to output value
* @returns FAIL if input value is out of bounds, else OK
*/
int Common_VoltageToDac(int voltage, int* dacval, int vmin, int vmax, int nsteps) {
int ConvertToDifferentRange(int inputMin, int inputMax, int outputMin, int outputMax,
int inputValue, int* outputValue) {
FILE_LOG(logDEBUG1, ("\tInput Value: %d\n", inputValue));
// validate
if ((voltage < vmin) || (voltage > vmax)) {
FILE_LOG(logERROR, ("Voltage value (to convert to dac value) is outside bounds (%d to %d mV): %d\n", vmin, vmax, voltage));
// validate within bounds
// eg. MAX1932 range is v(60 - 200) to dac(255 - 1), here inputMin > inputMax (when dac to voltage)
int smaller = inputMin;
int bigger = inputMax;
if (smaller > bigger) {
smaller = inputMax;
bigger = inputMin;
}
if ((inputValue < smaller) || (inputValue > bigger)) {
FILE_LOG(logERROR, ("Input Value is outside bounds (%d to %d): %d\n", inputValue, smaller, bigger));
*outputValue = -1;
return FAIL;
}
// convert
*dacval = (int)(((voltage - vmin) / (vmax - vmin)) * (nsteps - 1) + 0.5);
return OK;
}
/**
* Convert dac units to voltage
* @param dacval dac units
* @param voltage pointer to value converted to mV
* @param vmin minimum voltage in mV
* @param vmax maximum voltage in mV
* @param maximum number of steps
* @returns FAIL when voltage outside limits, OK if conversion successful
*/
int Common_DacToVoltage(int dacval, int* voltage, int vmin, int vmax, int nsteps) {
double value = double((inputValue - inputMin) * (outputMax - outputMin))
/ double(inputMax - inputMin) + outputMin;
// validate
if ((dacval < 0) || (dacval >= nsteps)) {
FILE_LOG(logERROR, ("Dac units (to convert to voltage) is outside bounds (0 to %d): %d\n", nsteps - 1, dacval));
return FAIL;
// double to integer conversion (if decimal places, round to integer)
if ((value - (int)value) > 0.0001) {
value += 0.5;
}
*outputValue = value;
// convert
*voltage = vmin + (vmax - vmin) * dacval / (nsteps - 1);
FILE_LOG(logDEBUG1, ("\tConverted Ouput Value: %d\n", *outputValue));
return OK;
}