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 {
57 SPIADCData_t SPIADCData;
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;
65 }ADC_RcvStruct_t;
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;
88 }SPI_XmtStruct_t;
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_ */
SetHighCurrentMode
void SetHighCurrentMode(void)
Sets High Current Mode.
Definition: spi.c:120
DACSetAmps
void DACSetAmps(double i)
Sets the DAC to a certain Current.
Definition: spi.c:185
SPIInit
void SPIInit(void)
Initializes the SPI Interface.
Definition: spi.c:366
uart.h
SPIDeselectADC
static void SPIDeselectADC(void)
Revoke ADC CS.
Definition: spi.h:127
ADCGetAs
double ADCGetAs(void)
Gets the Ampere Seconds consumed.
Definition: spi.c:255
ADC_RcvStruct_t::SPIADCData
SPIADCData_t SPIADCData
Definition: spi.h:57
DACPORT
#define DACPORT
Definition: spi.h:12
SPI_XmtStruct_t
Raw values for ADC, DAC and LEDs.
Definition: spi.h:77
SPIADCData_t
Definition: spi.h:36
IsHighCurrentMode
static bool IsHighCurrentMode(void)
Returns True if High Current Mode is active.
Definition: spi.h:139
DACCSPIN
#define DACCSPIN
Definition: spi.h:13
CalcIMaxLookupTable
void CalcIMaxLookupTable(void)
Calculates the Lookup Table for IMax.
Definition: spi.c:75
ADC_RcvStruct_t
Raw values for ADC, DAC and LEDs.
Definition: spi.h:53
soaIMaxLUT
uint16_t soaIMaxLUT[128]
Lookup Table for maximum DAC-Value versus ADC-Voltage.
Definition: spi.c:71
derating
uint16_t derating
Temperature Derating factor *65535. Maximum Power has to be derated by multiplication with derating.
Definition: spi.c:57
ADCPORT
#define ADCPORT
Definition: spi.h:14
SPI_XmtStruct_t::LED
uint8_t LED
Bitmask for LED shift register.
Definition: spi.h:82
ADCGetIHighAvgRaw
uint16_t ADCGetIHighAvgRaw()
Returns the averaged Value for the Current.
Definition: spi.c:335
SPI_XmtStruct_t::DAC_CMD
uint8_t DAC_CMD
Command Byte for DAC.
Definition: spi.h:84
ADCGetIByVal
double ADCGetIByVal(uint16_t iRaw, bool isHigh)
Returns the Current for a given Value.
Definition: spi.c:269
ADCGetVLifeRaw
uint16_t ADCGetVLifeRaw()
Returns the Life Value for the Sink Voltage.
Definition: spi.c:323
ADC_RcvStruct_t::DAC
uint16_t DAC
Raw value for the DAC.
Definition: spi.h:60
ADCCSPIN
#define ADCCSPIN
Definition: spi.h:15
SpiSendAndReceiveByte
uint8_t SpiSendAndReceiveByte(uint8_t b)
Sends and receives a Byte over the SPI Bus.
Definition: spi.c:138
CalcRChannel
void CalcRChannel(void)
Definition: spi.c:411
SPIADCAvg
volatile SPIADCData_t SPIADCAvg
Averaged Values over 16 measurements. These values may get up to 14 bits of precision.
Definition: spi.c:55
SpiGetADC2
void SpiGetADC2(void)
Faster Alternative.
SPIADCData_t::Ihigh
uint16_t Ihigh
The Current measured over the 100mR Shunt. Always valid but inaccurate if in low current mode.
Definition: spi.h:40
SPIADCData_t::V
uint16_t V
The Voltage.
Definition: spi.h:38
DACSetAmpsByStr
void DACSetAmpsByStr(char *str)
Sets the sink to the value of a string.
Definition: spi.c:226
SpiSelectDAC
static void SpiSelectDAC(void)
Asserts DAC CS.
Definition: spi.h:106
SpiGetADC
void SpiGetADC(void)
ADC_RcvStruct
volatile ADC_RcvStruct_t ADC_RcvStruct
Definition: spi.c:54
SPI_XmtStruct_t::DAC
uint16_t DAC
Raw value for the DAC.
Definition: spi.h:83
SPIADCData_t::Ilow
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
ADCGetVAvgRaw
uint16_t ADCGetVAvgRaw()
Returns the averaged Value for the Sink Voltage.
Definition: spi.c:359
ADCGetIlowLifeRaw
uint16_t ADCGetIlowLifeRaw()
Returns the Life Value for the Current.
Definition: spi.c:313
ahRaw
uint64_t ahRaw
The sum of current values.
Definition: Eload.c:72
SPIADCBusy
static bool SPIADCBusy(void)
Returns True if SPI is occupied by ADC.
Definition: spi.h:99
ahTickCnt
uint32_t ahTickCnt
The number of 100Hz interrupts since ahRaw is counting.
Definition: Eload.c:74
SPIDeselectDAC
static void SPIDeselectDAC(void)
Revoke DAC CS.
Definition: spi.h:113
ADCGetILowAvgRaw
uint16_t ADCGetILowAvgRaw()
Returns the averaged Value for the Current.
Definition: spi.c:349
DACSetRaw
void DACSetRaw(uint16_t value)
Sets the raw Value of the DAC.
Definition: spi.c:162
SpiSelectADC
static void SpiSelectADC(void)
Asserts ADC CS.
Definition: spi.h:120
ADCGetIavg
double ADCGetIavg(void)
Returns the averaged Value for Load Current.
Definition: spi.c:240
ADCGetIhighLifeRaw
uint16_t ADCGetIhighLifeRaw()
Returns the Life Value for the Current.
Definition: spi.c:299
ADC_RcvStruct_t::LED
uint8_t LED
Bitmask for LED shift register.
Definition: spi.h:59
SetLowCurrentMode
void SetLowCurrentMode(void)
Sets Low Current Mode.
Definition: spi.c:110
ahCnt
uint32_t ahCnt
Number of average values added to ahRaw;.
Definition: Eload.c:73
SPISetLEDs
void SPISetLEDs(uint8_t mask)
Sets the State of the 8 LEDs on the SPI-Shift-Register.
Definition: spi.c:103
ADC_RcvStruct_t::DAC_CMD
uint8_t DAC_CMD
Definition: spi.h:61
ADCGetVavg
double ADCGetVavg(void)
Returns the averaged Value for the Sink Voltage.
Definition: spi.c:287
ADC_RcvStruct_t::dummy
uint8_t dummy
Dummy byte.
Definition: spi.h:58