Electronic Load 1.0 Programmable Constant Current Sink

Data Structures | Macros | Functions | Variables
spi.h File Reference
Include dependency graph for spi.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

## Data Structures
struct | ADC_RcvStruct_t
| Raw values for ADC, DAC and LEDs. More...
struct | SPI_XmtStruct_t
| Raw values for ADC, DAC and LEDs. More...
struct | SPIADCData_t

## Macros
#define | ADCCSPIN 4
#define | ADCPORT PORTB
#define | DACCSPIN 3
#define | DACPORT PORTB

## Functions
double | ADCGetAs (void)
| Gets the Ampere Seconds consumed. More...
double | ADCGetIavg (void)
| Returns the averaged Value for Load Current. More...
double | ADCGetIByVal (uint16_t iRaw, bool isHigh)
| Returns the Current for a given Value. More...
uint16_t | ADCGetIHighAvgRaw ()
| Returns the averaged Value for the Current. More...
uint16_t | ADCGetIhighLifeRaw ()
| Returns the Life Value for the Current. More...
uint16_t | ADCGetILowAvgRaw ()
| Returns the averaged Value for the Current. More...
uint16_t | ADCGetIlowLifeRaw ()
| Returns the Life Value for the Current. More...
double | ADCGetVavg (void)
| Returns the averaged Value for the Sink Voltage. More...
uint16_t | ADCGetVAvgRaw ()
| Returns the averaged Value for the Sink Voltage. More...
uint16_t | ADCGetVLifeRaw ()
| Returns the Life Value for the Sink Voltage. More...
void | CalcIMaxLookupTable (void)
| Calculates the Lookup Table for IMax. More...
void | CalcRChannel (void)
void | DACSetAmps (double i)
| Sets the DAC to a certain Current. More...
void | DACSetAmpsByStr (char *str)
| Sets the sink to the value of a string. More...
void | DACSetRaw (uint16_t value)
| Sets the raw Value of the DAC. More...
static bool | IsHighCurrentMode (void)
| Returns True if High Current Mode is active. More...
void | SetHighCurrentMode (void)
| Sets High Current Mode. More...
void | SetLowCurrentMode (void)
| Sets Low Current Mode. More...
static bool | SPIADCBusy (void)
| Returns True if SPI is occupied by ADC. More...
static void | SPIDeselectADC (void)
| Revoke ADC CS. More...
static void | SPIDeselectDAC (void)
| Revoke DAC CS. More...
void | SpiGetADC (void)
void | SpiGetADC2 (void)
| Faster Alternative. More...
void | SPIInit (void)
| Initializes the SPI Interface. More...
static void | SpiSelectADC (void)
| Asserts ADC CS. More...
static void | SpiSelectDAC (void)
| Asserts DAC CS. More...
uint8_t | SpiSendAndReceiveByte (uint8_t b)
| Sends and receives a Byte over the SPI Bus. More...
void | SPISetLEDs (uint8_t mask)
| Sets the State of the 8 LEDs on the SPI-Shift-Register. More...

## Variables
volatile ADC_RcvStruct_t | ADC_RcvStruct
uint32_t | ahCnt
| Number of average values added to ahRaw;. More...
uint64_t | ahRaw
| The sum of current values. More...
uint32_t | ahTickCnt
| The number of 100Hz interrupts since ahRaw is counting. More...
uint16_t | derating
| Temperature Derating factor *65535. Maximum Power has to be derated by multiplication with derating. More...
uint16_t | soaIMaxLUT [128]
| Lookup Table for maximum DAC-Value versus ADC-Voltage. More...
volatile SPIADCData_t | SPIADCAvg
| Averaged Values over 16 measurements. These values may get up to 14 bits of precision. More...

## Macro Definition Documentation

## ◆ DACPORT

#define DACPORT PORTB

file spi.h

## ◆ DACCSPIN

#define DACCSPIN 3

## ◆ ADCPORT

#define ADCPORT PORTB

## ◆ ADCCSPIN

#define ADCCSPIN 4

## Function Documentation

## ◆ SPIInit()

void SPIInit | ( | void |  | ) |

Initializes the SPI Interface.

Here is the call graph for this function:

## ◆ DACSetRaw()

void DACSetRaw | ( | uint16_t | value | ) |

Sets the raw Value of the DAC.

Parameters

  value | The Value

Here is the call graph for this function:

## ◆ DACSetAmps()

void DACSetAmps | ( | double | i | ) |

Sets the DAC to a certain Current.

This function also switches the current range to high or low if the desired value is higher than rangeHighLimit or lower than rangeLowLimit.

Parameters

  i | The Current

Here is the call graph for this function:

## ◆ DACSetAmpsByStr()

void DACSetAmpsByStr | ( | char * | str | ) |

Sets the sink to the value of a string.

Parameters

  str | A Pointer to the string containing the value in amperes

Here is the call graph for this function:

## ◆ SpiGetADC()

void SpiGetADC | ( | void |  | ) |

## ◆ SpiGetADC2()

void SpiGetADC2 | ( | void |  | ) |

Faster Alternative.

## ◆ ADCGetIavg()

double ADCGetIavg | ( | void |  | ) |

Returns the averaged Value for Load Current.

This function honors the range setting and returns the value measured by the active sense resistor.

Returns
  The averaged Load Current in A

Here is the call graph for this function:

## ◆ ADCGetIByVal()

double ADCGetIByVal | ( | uint16_t | iRaw,
|  | bool | isHigh
| ) |  |

Returns the Current for a given Value.

Parameters

  iRaw | The raw value from the ADC, normalized to 16 bits
  isHigh | True if the value represents a high current mode value

Returns
  The Load Current in A

## ◆ ADCGetAs()

double ADCGetAs | ( | void |  | ) |

Gets the Ampere Seconds consumed.

Returns
  The As

Here is the call graph for this function:

## ◆ ADCGetVavg()

double ADCGetVavg | ( | void |  | ) |

Returns the averaged Value for the Sink Voltage.

Note
  The voltage over the sink may be reduced by cable and/or additional resistance and may not reflect the exact value of the source voltage.

Returns
  The Voltage over the Sink in V

## ◆ SpiSendAndReceiveByte()

uint8_t SpiSendAndReceiveByte | ( | uint8_t | b | ) |

Sends and receives a Byte over the SPI Bus.

SPI implies the reception of a byte whenever one is sent. It depends on the hardware if it contains useful data.

Note
  This function blocks for the time of the transfer.

Parameters

  b | The Byte to transmit

Returns
  The received Byte

## ◆ CalcIMaxLookupTable()

void CalcIMaxLookupTable | ( | void |  | ) |

Calculates the Lookup Table for IMax.

Here is the call graph for this function:

## ◆ SPIADCBusy()

static bool SPIADCBusy | ( | void |  | ) |
| inlinestatic

Returns True if SPI is occupied by ADC.

Since the ADC is handled in the interrupt, no DAC-transfer must be made while this function returns true.

Returns
  True during ADC-Operations

## ◆ SpiSelectDAC()

static void SpiSelectDAC | ( | void |  | ) |
| inlinestatic

Asserts DAC CS.

Here is the caller graph for this function:

## ◆ SPIDeselectDAC()

static void SPIDeselectDAC | ( | void |  | ) |
| inlinestatic

Revoke DAC CS.

Here is the caller graph for this function:

## ◆ SpiSelectADC()

static void SpiSelectADC | ( | void |  | ) |
| inlinestatic

Asserts ADC CS.

Here is the caller graph for this function:

## ◆ SPIDeselectADC()

static void SPIDeselectADC | ( | void |  | ) |
| inlinestatic

Revoke ADC CS.

Here is the caller graph for this function:

## ◆ IsHighCurrentMode()

static bool IsHighCurrentMode | ( | void |  | ) |
| inlinestatic

Returns True if High Current Mode is active.

Note
  We use PORTD instead of PIND since the gate capacitance of the FET prevents PIND from changing 'instantaneously'.

Returns
  True if High Current Mode is active, False if in Low Current Mode

Here is the caller graph for this function:

## ◆ SPISetLEDs()

void SPISetLEDs | ( | uint8_t | mask | ) |

Sets the State of the 8 LEDs on the SPI-Shift-Register.

Parameters

  mask | A Bitmask of the LEDs

Note
  The LEDs are not set immediately but on the next SPI interrupt covering the LED shift register. This may introduce a small delay.

## ◆ SetLowCurrentMode()

void SetLowCurrentMode | ( | void |  | ) |

Sets Low Current Mode.

## ◆ SetHighCurrentMode()

void SetHighCurrentMode | ( | void |  | ) |

Sets High Current Mode.

## ◆ ADCGetIhighLifeRaw()

uint16_t ADCGetIhighLifeRaw | ( |  | ) |

Returns the Life Value for the Current.

The current is measured over the lower of the two shunts representing the high current range. The value is normalized to 16 bit unsigned int.

Returns
  The current in the range of 0000 to ffff

## ◆ ADCGetIlowLifeRaw()

uint16_t ADCGetIlowLifeRaw | ( |  | ) |

Returns the Life Value for the Current.

The current is measured over the higher of the two shunts representing the low current range. Note that this value is garbage if the resistor is shorted by the MOSFET. The value is normalized to 16 bit unsigned int.

Returns
  The current in the range of 0000 to ffff

## ◆ ADCGetVLifeRaw()

uint16_t ADCGetVLifeRaw | ( |  | ) |

Returns the Life Value for the Sink Voltage.

The value is normalized to 16 bit unsigned int.

Returns
  The Voltage over the Sink in the range of 0000 to ffff

## ◆ ADCGetIHighAvgRaw()

uint16_t ADCGetIHighAvgRaw | ( |  | ) |

Returns the averaged Value for the Current.

The current is measured over the lower of the two shunts representing the high current range. The value is normalized to 16 bit unsigned int.

Returns
  The current in the range of 0000 to ffff

## ◆ ADCGetILowAvgRaw()

uint16_t ADCGetILowAvgRaw | ( |  | ) |

Returns the averaged Value for the Current.

The current is measured over the higher of the two shunts representing the low current range. Note that this value is garbage if the resistor is shorted by the MOSFET. The value is normalized to 16 bit unsigned int.

Returns
  The current in the range of 0000 to ffff

## ◆ ADCGetVAvgRaw()

uint16_t ADCGetVAvgRaw | ( |  | ) |

Returns the averaged Value for the Sink Voltage.

The value is normalized to 16 bit unsigned int.

Returns
  The Voltage over the Sink in the range of 0000 to ffff

## ◆ CalcRChannel()

void CalcRChannel | ( | void |  | ) |

Here is the call graph for this function:

## Variable Documentation

## ◆ ahRaw

uint64_t ahRaw

The sum of current values.

## ◆ ahCnt

uint32_t ahCnt

Number of average values added to ahRaw;.

## ◆ ahTickCnt

uint32_t ahTickCnt

The number of 100Hz interrupts since ahRaw is counting.

## ◆ derating

uint16_t derating

Temperature Derating factor *65535. Maximum Power has to be derated by multiplication with derating.

## ◆ soaIMaxLUT

uint16_t soaIMaxLUT[128]

Lookup Table for maximum DAC-Value versus ADC-Voltage.

This table contains the maximum DAC-Values for the high byte of a given ADC voltage result. So the ADC interrupt can easily decide to reduce the current with each measurement if the SOA is violated.
This table has to be calculated by CalcIMaxLookupTable() on system startup!

Note
  For now, we keep this as uint16_t but 256 values with two bytes each make a quarter of the controllers RAM. We could reduce this to only the upper byte of the DAC at the cost of usable SOA area. Further reduction is possible by using less bits of the ADC value.
  Note also that the values still have to be degraded according to the case (heatsink) temperature!

## ◆ ADC_RcvStruct

volatile ADC_RcvStruct_t ADC_RcvStruct

## ◆ SPIADCAvg

volatile SPIADCData_t SPIADCAvg

Averaged Values over 16 measurements. These values may get up to 14 bits of precision.