Electronic Load  1.0
Programmable Constant Current Sink
spi.h
Go to the documentation of this file.
1 
5 #ifndef SPI_H_
6 #define SPI_H_
7 
8 #include <avr/io.h>
9 #include <stdbool.h>
10 #include "uart.h"
11 
12 #define DACPORT PORTB
13 #define DACCSPIN 3
14 #define ADCPORT PORTB
15 #define ADCCSPIN 4
16 
17 extern uint64_t ahRaw;
18 extern uint32_t ahCnt;
19 extern uint32_t ahTickCnt;
20 
21 extern void SPIInit(void);
22 extern void DACSetRaw(uint16_t value);
23 extern void DACSetAmps(double i);
24 extern void DACSetAmpsByStr(char *str);
25 extern void SpiGetADC(void);
26 extern void SpiGetADC2(void);
27 extern double ADCGetIavg(void);
28 extern double ADCGetIByVal(uint16_t iRaw, bool isHigh);
29 extern double ADCGetAs(void);
30 extern double ADCGetVavg(void);
31 extern uint8_t SpiSendAndReceiveByte(uint8_t b);
32 void CalcIMaxLookupTable(void);
33 extern uint16_t derating;
34 extern uint16_t soaIMaxLUT[128];
35 
36 typedef struct
37 {
38  uint16_t V;
39  uint16_t Ilow;
40  uint16_t Ihigh;
41 } SPIADCData_t;
42 
53 typedef struct
54 {
55  union {
56  struct {
58  uint8_t dummy;
59  uint8_t LED;
60  uint16_t DAC;
61  uint8_t DAC_CMD;
62  } named;
63  char array[11];
64  } data;
66 
77 typedef struct
78 {
79  union {
80  struct {
81  const char ADCCmd[7];
82  uint8_t LED;
83  uint16_t DAC;
84  uint8_t DAC_CMD;
85  } named;
86  char array[11];
87  } data;
89 
90 extern volatile ADC_RcvStruct_t ADC_RcvStruct;
91 extern volatile SPIADCData_t SPIADCAvg;
92 
99 static inline bool SPIADCBusy(void)
100 {
101  return (ADCPORT&(1<<ADCCSPIN))==0;
102 }
103 
106 static inline void SpiSelectDAC(void)
107 {
108  DACPORT &= ~(1<<DACCSPIN);
109 }
110 
113 static inline void SPIDeselectDAC(void)
114 {
115  DACPORT|=(1<<DACCSPIN);
116 }
117 
120 static inline void SpiSelectADC(void)
121 {
122  ADCPORT &= ~(1<<ADCCSPIN);
123 }
124 
127 static inline void SPIDeselectADC(void)
128 {
129  ADCPORT|=(1<<ADCCSPIN);
130 }
131 
139 static inline bool IsHighCurrentMode(void)
140 {
141  bool isHigh=(PORTD & 0b00000100)!=0;
142 // hprintf_P(PSTR("IsHigh: %d\n"),isHigh);
143  return (isHigh);
144 }
145 
146 extern void SPISetLEDs(uint8_t mask);
147 extern void SetLowCurrentMode(void);
148 extern void SetHighCurrentMode(void);
149 extern uint16_t ADCGetIhighLifeRaw();
150 extern uint16_t ADCGetIlowLifeRaw();
151 extern uint16_t ADCGetVLifeRaw();
152 extern uint16_t ADCGetIHighAvgRaw();
153 extern uint16_t ADCGetILowAvgRaw();
154 extern uint16_t ADCGetVAvgRaw();
155 extern void CalcRChannel(void);
156 
157 #endif /* SPI_H_ */
void SetHighCurrentMode(void)
Sets High Current Mode.
Definition: spi.c:120
void DACSetAmps(double i)
Sets the DAC to a certain Current.
Definition: spi.c:185
void SPIInit(void)
Initializes the SPI Interface.
Definition: spi.c:366
static void SPIDeselectADC(void)
Revoke ADC CS.
Definition: spi.h:127
double ADCGetAs(void)
Gets the Ampere Seconds consumed.
Definition: spi.c:255
SPIADCData_t SPIADCData
Definition: spi.h:57
#define DACPORT
Definition: spi.h:12
Raw values for ADC, DAC and LEDs.
Definition: spi.h:77
Definition: spi.h:36
static bool IsHighCurrentMode(void)
Returns True if High Current Mode is active.
Definition: spi.h:139
#define DACCSPIN
Definition: spi.h:13
void CalcIMaxLookupTable(void)
Calculates the Lookup Table for IMax.
Definition: spi.c:75
Raw values for ADC, DAC and LEDs.
Definition: spi.h:53
uint16_t soaIMaxLUT[128]
Lookup Table for maximum DAC-Value versus ADC-Voltage.
Definition: spi.c:71
uint16_t derating
Temperature Derating factor *65535. Maximum Power has to be derated by multiplication with derating.
Definition: spi.c:57
#define ADCPORT
Definition: spi.h:14
uint8_t LED
Bitmask for LED shift register.
Definition: spi.h:82
uint16_t ADCGetIHighAvgRaw()
Returns the averaged Value for the Current.
Definition: spi.c:335
uint8_t DAC_CMD
Command Byte for DAC.
Definition: spi.h:84
double ADCGetIByVal(uint16_t iRaw, bool isHigh)
Returns the Current for a given Value.
Definition: spi.c:269
uint16_t ADCGetVLifeRaw()
Returns the Life Value for the Sink Voltage.
Definition: spi.c:323
uint16_t DAC
Raw value for the DAC.
Definition: spi.h:60
#define ADCCSPIN
Definition: spi.h:15
uint8_t SpiSendAndReceiveByte(uint8_t b)
Sends and receives a Byte over the SPI Bus.
Definition: spi.c:138
void CalcRChannel(void)
Definition: spi.c:411
volatile SPIADCData_t SPIADCAvg
Averaged Values over 16 measurements. These values may get up to 14 bits of precision.
Definition: spi.c:55
void SpiGetADC2(void)
Faster Alternative.
uint16_t Ihigh
The Current measured over the 100mR Shunt. Always valid but inaccurate if in low current mode.
Definition: spi.h:40
uint16_t V
The Voltage.
Definition: spi.h:38
void DACSetAmpsByStr(char *str)
Sets the sink to the value of a string.
Definition: spi.c:226
static void SpiSelectDAC(void)
Asserts DAC CS.
Definition: spi.h:106
void SpiGetADC(void)
volatile ADC_RcvStruct_t ADC_RcvStruct
Definition: spi.c:54
uint16_t DAC
Raw value for the DAC.
Definition: spi.h:83
uint16_t Ilow
The Current measured over both Shunts. Invalid if in high Current Range since the 3R3 is shorted by t...
Definition: spi.h:39
uint16_t ADCGetVAvgRaw()
Returns the averaged Value for the Sink Voltage.
Definition: spi.c:359
uint16_t ADCGetIlowLifeRaw()
Returns the Life Value for the Current.
Definition: spi.c:313
uint64_t ahRaw
The sum of current values.
Definition: Eload.c:72
static bool SPIADCBusy(void)
Returns True if SPI is occupied by ADC.
Definition: spi.h:99
uint32_t ahTickCnt
The number of 100Hz interrupts since ahRaw is counting.
Definition: Eload.c:74
static void SPIDeselectDAC(void)
Revoke DAC CS.
Definition: spi.h:113
uint16_t ADCGetILowAvgRaw()
Returns the averaged Value for the Current.
Definition: spi.c:349
void DACSetRaw(uint16_t value)
Sets the raw Value of the DAC.
Definition: spi.c:162
static void SpiSelectADC(void)
Asserts ADC CS.
Definition: spi.h:120
double ADCGetIavg(void)
Returns the averaged Value for Load Current.
Definition: spi.c:240
uint16_t ADCGetIhighLifeRaw()
Returns the Life Value for the Current.
Definition: spi.c:299
uint8_t LED
Bitmask for LED shift register.
Definition: spi.h:59
void SetLowCurrentMode(void)
Sets Low Current Mode.
Definition: spi.c:110
uint32_t ahCnt
Number of average values added to ahRaw;.
Definition: Eload.c:73
void SPISetLEDs(uint8_t mask)
Sets the State of the 8 LEDs on the SPI-Shift-Register.
Definition: spi.c:103
uint8_t DAC_CMD
Definition: spi.h:61
double ADCGetVavg(void)
Returns the averaged Value for the Sink Voltage.
Definition: spi.c:287
uint8_t dummy
Dummy byte.
Definition: spi.h:58