1 #ifndef MICRODOWELL_H
2 #define MICRODOWELL_H
3 
4 #ifdef ENTERPRISE_PROTOCOL
5 
6 #include <ctype.h>
7 
8 
9 #define STX_CHAR					'['
10 #define ERR_COM_NO_CHARS      -999  // nessun carattere dalla porta seriale
11 #define ERR_MSG_TOO_SHORT     -998  // messaggio troppo breve: non arriva sino al Checksum
12 #define ERR_MSG_CHECKSUM      -997  // checksum non valido
13 #define ERR_COM_TIMEOUT       -996  // timeout in lettura
14 #define ERR_CLR_RX_BUFF			-900  // bisogna cancellare il buffer (non è un errore!)
15 #define COMMAND_NOT_VALID     -50   // comando non valido
16 #define PARAMETER_NOT_VALID   -51   // parametro non valido
17 #define ERR_UPS_NOT_FOUND     -10   // non trovo un UPS
18 #define ERR_COM_PORT_OPEN     -11   // impossibile aprire la porta di comunicazione
19 #define ERR_COM_SET_PORT      -12   // impossibile configurare la porta di comunicazione
20 #define ERR_COM_UNDEF_PORT    -20   // porta non valida o non esistente
21 #define ERR_USB_DRIVER        -13   // impossibile aprire il driver USB
22 #define ERR_USB_COMM_KO       -14   // interfaccia USB OK: gruppo spento o guasto
23 #define ERR_UPS_UNKNOWN       -30   // impossibile identificare l'UPS
24 #define ERR_PRG_THREAD        -40   // impossibile creare uno thread
25 #define ERR_PRG_INVALID_DATA  -60   // dati non validi
26 
27 #define ERR_NO_ERROR				0x00  // no errors
28 #define ERR_I2C_BUSY       	0x01	// I2C bus busy (e2prom)
29 #define ERR_CMD_CHECKSUM      0x10	// Checksum not valid
30 #define ERR_CMD_UNRECOG       0x11	// unrecognized command
31 #define ERR_EEP_NOBLOCK    	0x08	// WRITE: eeprom address not multiple of 8
32 #define ERR_EEP_OOBOUND    	0x09	// READ: eeprom address out of bound (with size)
33 #define ERR_EEP_WADDR1     	0x28	// error writing e2prom address
34 #define ERR_EEP_WSADDR1    	0x29	// error writing e2prom subaddress
35 #define ERR_EEP_RDATA      	0x2A	// error reading e2prom data
36 #define ERR_EEP_WADDR2     	0x50	// error writing e2prom address
37 #define ERR_EEP_WSADDR2    	0x51	// error reading e2prom subaddress
38 #define ERR_EEP_WDATA      	0x52	// error writing e2prom data
39 #define ERR_EEP_WADDRVER   	0x53	// error writing e2prom address during data verification
40 #define ERR_EEP_WDATAVER   	0x54	// error verification e2prom data
41 #define ERR_EEP_VERIFY     	0x55	// e2prom data are different from those in the write buffer
42 #define ERR_EEP_CHECKSUM   	0x90	// e2prom checksum error
43 
44 
45 
46 #define  CMD_ACK              0x06  // ACK
47 #define  CMD_NACK             0x15  // NACK
48 
49 #define  CMD_GET_STATUS       0x00  // comando di acquisizione STATUS
50 #define  CMD_GET_MEASURES     0x01  // comando di acquisizione MISURE
51 #define  CMD_GET_CONFIG       0x02  // comando di acquisizione CONFIGURAZIONE
52 #define  CMD_GET_BATT_STAT    0x03  // comando di acquisizione Battery+Load Status
53 #define  CMD_GET_BAT_LD       0x03  // comando di acquisizione Battery+Load Status
54 #define  CMD_GET_MASK         0x07  // comando di acquisizione MASCHERA PUNTI
55 #define  CMD_SET_TIMER        0x20  // comando di CONFIGURAZIONE TIMER
56 #define  CMD_BATT_TEST        0x21  // comando di CONFIGURAZIONE TEST BATTERIA
57 #define  CMD_GET_BATT_TEST    0x22  // comando di       LETTURA  TEST BATTERIA
58 #define  CMD_SD_ONESHOT       0x40  // comando di SCRITTURA SHUTDOWN ONESHOT
59 #define  CMD_GET_SD_ONESHOT   0x41  // comando di  LETTURA  SHUTDOWN ONESHOT
60 #define  CMD_SET_SCHEDULE     0x42  // comando di SCRITTURA SCHEDULE
61 #define  CMD_GET_SCHEDULE     0x43  // comando di  LETTURA  SCHEDULE
62 #define  CMD_GET_EEP_BLOCK    0x50  // comando di  LETTURA  BLOCCO EEPROM
63 #define  CMD_SET_EEP_BLOCK    0x51  // comando di SCRITTURA BLOCCO EEPROM
64 #define  CMD_GET_EEP_SEED     0x52  // comando di acquisizione SEME PROGR. EEPROM
65 #define  CMD_INIT             0xF0  // comando di REINIZIALIZZAZIONE UPS
66 
67 
68 #define  LEN_ACK              1  // ACK
69 #define  LEN_NACK             2  // NACK
70 
71 #define  LEN_GET_STATUS       1  // comando di acquisizione STATUS
72 #define  LEN_GET_MEASURES     1  // comando di acquisizione MISURE
73 #define  LEN_GET_CONFIG       1  // comando di acquisizione CONFIGURAZIONE
74 #define  LEN_GET_BATT_STAT    1  // comando di acquisizione Battery+Load Status
75 #define  LEN_GET_BAT_LD       1  // comando di acquisizione Battery+Load Status
76 #define  LEN_GET_MASK         1  // comando di acquisizione MASCHERA PUNTI
77 #define  LEN_SET_TIMER        5  // comando di CONFIGURAZIONE TIMER
78 #define  LEN_BATT_TEST        4  // comando di CONFIGURAZIONE TEST BATTERIA
79 #define  LEN_GET_BATT_TEST    1  // comando di       LETTURA  TEST BATTERIA
80 #define  LEN_SD_ONESHOT       8  // comando di SCRITTURA SHUTDOWN ONESHOT
81 #define  LEN_GET_SD_ONESHOT   1  // comando di  LETTURA  SHUTDOWN ONESHOT
82 #define  LEN_SET_SCHEDULE     8  // comando di SCRITTURA SCHEDULE
83 #define  LEN_GET_SCHEDULE     2  // comando di  LETTURA  SCHEDULE
84 #define  LEN_GET_EEP_BLOCK    3  // comando di SCRITTURA BLOCCO EEPROM
85 #define  LEN_SET_EEP_BLOCK    12 // comando di SCRITTURA BLOCCO EEPROM
86 #define  LEN_GET_EEP_SEED     1  // comando di acquisizione SEME PROGR. EEPROM
87 #define  LEN_INIT             1  // comando di REINIZIALIZZAZIONE UPS
88 
89 
90 // non completamente definiti!
91 #define  RET_GET_STATUS       10  // comando di acquisizione STATUS
92 #define  RET_GET_MEASURES     15  // comando di acquisizione MISURE
93 #define  RET_GET_CONFIG       11  // comando di acquisizione CONFIGURAZIONE
94 #define  RET_GET_BATT_STAT    10  // comando di acquisizione Battery+Load Status
95 #define  RET_GET_BAT_LD       10  // comando di acquisizione Battery+Load Status
96 #define  RET_GET_MASK         1  // comando di acquisizione MASCHERA PUNTI
97 #define  RET_SET_TIMER        5  // comando di CONFIGURAZIONE TIMER
98 #define  RET_BATT_TEST        4  // comando di CONFIGURAZIONE TEST BATTERIA
99 #define  RET_GET_BATT_TEST    1  // comando di       LETTURA  TEST BATTERIA
100 #define  RET_SD_ONESHOT       8  // comando di SCRITTURA SHUTDOWN ONESHOT
101 #define  RET_GET_SD_ONESHOT   1  // comando di  LETTURA  SHUTDOWN ONESHOT
102 #define  RET_SET_SCHEDULE     8  // comando di SCRITTURA SCHEDULE
103 #define  RET_GET_SCHEDULE     8  // comando di  LETTURA  SCHEDULE
104 #define  RET_GET_EEP_BLOCK    11  // comando di SCRITTURA BLOCCO EEPROM
105 #define  RET_SET_EEP_BLOCK    12 // comando di SCRITTURA BLOCCO EEPROM
106 #define  RET_GET_EEP_SEED     1  // comando di acquisizione SEME PROGR. EEPROM
107 #define  RET_INIT             1  // comando di REINIZIALIZZAZIONE UPS
108 
109 
110 //=======================================================
111 // Indirizzi delle variabili memorizzate nella EEPROM: //
112 //=======================================================
113 #define  EEP_OPT_BYTE_BLK 0x00 // Option Bytes block
114 #define  EEP_MAGIC        0x00 // magic number: deve essere a 0xAA
115 #define  EEP_CHECKSUM     0x01 // XOR longitudinale da 0x?? a 0x??
116 #define  EEP_OUT_CONFIG   0x02 // vedi documentazione
117 #define  EEP_OPT_BYTE_0   0x02 // vedi documentazione
118 #define  EEP_OPT_BYTE_1   0x03 // vedi documentazione
119 #define  EEP_STATUS       0x04 // Status UPS
120 
121 #define  EEP_THRESHOLD_0  0x08 // Blocco 0 con gli threshold
122 #define  EEP_MEAN_MIN     0x08 // UPS minimum AC voltage intervention point
123 #define  EEP_MEAN_MAX     0x0A // UPS maximum AC voltage intervention point
124 #define  EEP_AVR_MIN      0x0C // AVR minimum AC voltage intervention point
125 #define  EEP_AVR_MAX      0x0E // AVR maximum AC voltage intervention point
126 
127 #define  EEP_THRESHOLD_1  0x10 // Blocco 1 con gli threshold
128 #define  EEP_BLOW_VALUE   0x10 // Battery LOW threshold voltage
129 #define  EEP_BEND_VALUE   0x12 // Battery END threshold voltage
130 #define  EEP_VMETER_0     0x14 // Led 0 Voltage indicator threshold
131 #define  EEP_VMETER_1     0x16 // Led 1 Voltage indicator threshold
132 
133 #define  EEP_THRESHOLD_2  0x18 // Blocco 2 con gli threshold
134 #define  EEP_VMETER_2     0x18 // Led 2 Voltage indicator threshold
135 #define  EEP_VMETER_3     0x1A // Led 3 Voltage indicator threshold
136 #define  EEP_VMETER_4     0x1C // Led 4 Voltage indicator threshold
137 
138 #define  EEP_FAULT_POINTS 0x20 // number of fault points (in an half sine wave) after which USP enter battery mode
139 #define  EEP_CHARGER_MIN  0x21 // Threshold voltage for battery charger
140 
141 #define  EEP_TEMP_MAX     0x28 // Maximum temperature: over this value an alarm will be generated
142 #define  EEP_TEMP_FAULT   0x2A // Fault temperature: over this value, UPS will shut down immediately
143 #define  EEP_FANSPEED_0   0x2C // Temperature for fan activation (low speed)
144 #define  EEP_FANSPEED_1   0x2E // Temperature for fan activation (medium speed)
145 
146 #define  EEP_FANSPEED_2   0x30 // Temperature for fan activation (high speed)
147 
148 #define  EEP_IOUT         0x38 //
149 #define  EEP_IOUT_OVRLD   0x38 // Maximum current: over this value an alarm will be generated
150 #define  EEP_IOUT_FAULT   0x3A // Fault current: over this value, UPS will shut down immediately
151 #define  EEP_PWRMTR_0     0x3C // Led 0 Power indicator threshold
152 #define  EEP_PWRMTR_1     0x3E // Led 1 Power indicator threshold
153 
154 #define  EEP_PWRMTR_2     0x40 // Led 2 Power indicator threshold
155 #define  EEP_PWRMTR_3     0x42 // Led 3 Power indicator threshold
156 #define  EEP_PWRMTR_4     0x44 // Led 4 Power indicator threshold
157 #define  EEP_PWRMTR_5     0x46 // Between "Full power" and "Overload" Power indicator level
158 
159 #define  EEP_INPUT_MASK_0 0x48 // Half sine input mask (0-7 of 10 points)
160 #define  EEP_INPUT_MASK_8 0x50 // Half sine input mask (8-9 of 10 points)
161 
162 #define  EEP_MIN_VBATT    0x60 // Minimum battery voltaged reached from the last power-on
163 #define  EEP_RUNTIME_H    0x62 // Working time in minutes (max ~=32 years) (MSB first)
164 #define  EEP_BAT_TIME_M   0x65 // Time in Battery mode: seconds*2 (max ~=390 days). (MSB first)
165 
166 
167 #define  EEP_UPS_MODEL    0x80 // Model of the UPS in ASCII. For the Enterprise line, the structure is:
168                                //    ENTxxyyy
169                                // where:	 xx = Power rating in watts/100 (zero padded at left)
170                                //          yyy = model variants (if not defined, space padded to the right)
171 
172 #define  EEP_NETWRK_ID    0x88 // Network ID in ASCII (not used) - space padded
173 #define  EEP_NETWRK_NAME  0x90 // Network Name in ASCII (not used) - space padded
174 #define  EEP_SERIAL_NUM   0x98 // Serial Number in ASCII - zero padded (at left)
175 
176 #define  EEP_PROD_DATE    0xA0 //
177 #define  EEP_PROD_YEAR    0xA0 // Year of production (add 2000)
178 #define  EEP_PROD_WEEK    0xA1 // Week of production [0-51]
179 #define  EEP_PROD_HW_VER  0xA2 // Hardware version: 2 nibbles ?  Major and Minor number.
180 #define  EEP_PROD_BRD_VER 0xA3 // Board version: 2 nibbles ?  Major and Minor number.
181 #define  EEP_PROD_FW_VER  0xA4 // Firmware version: 2 nibbles ?  Major and Minor number.
182 #define  EEP_PROD_FW_SVER 0xA5 // Firmware SUBversion: 2 nibbles ?  Major and Minor number.
183 #define  EEP_PROD_BTTRS   0xA6 // number of batteries installed in the UPS
184 
185 
186 #define  EEP_BATT_SUBST   0xA8 // battery replacement block
187 #define  EEP_BATT_YEAR_0  0xA8 // Year of 1st battery replacement (add 2000)
188 #define  EEP_BATT_WEEK_0  0xA9 // Week of 1st battery replacement
189 #define  EEP_BATT_YEAR_1  0xAA // Year of 2nd battery replacement (add 2000)
190 #define  EEP_BATT_WEEK_1  0xAB // Week of 2nd battery replacement
191 #define  EEP_BATT_YEAR_2  0xAC // Year of 3rd battery replacement (add 2000)
192 #define  EEP_BATT_WEEK_2  0xAD // Week of 3rd battery replacement
193 #define  EEP_BATT_YEAR_3  0xAE // Year of 4th battery replacement (add 2000)
194 #define  EEP_BATT_WEEK_3  0xAF // Week of 4th battery replacement
195 
196 
197 #define  EEP_SCHEDULE     0xC8 // Start of SCHEDULING table
198 #define  EEP_SCHEDULE_0   0xC8 // 1st Scheduling slot
199 #define  EEP_SCHEDULE_1   0xD0 // 2nd Scheduling slot
200 #define  EEP_SCHEDULE_2   0xD8 // 3rd Scheduling slot
201 #define  EEP_SCHEDULE_3   0xE0 // 4th Scheduling slot
202 #define  EEP_SCHEDULE_4   0xE8 // 5th Scheduling slot
203 #define  EEP_SCHEDULE_5   0xF0 // 6th Scheduling slot
204 
205 
206 #define 	BIT_NO_COMM_UPS 			0x0001	// Event  2 - communications with the UPS lost
207 #define	BIT_COMM_OK 				0x0002	// Event  3 - communications with the UPS reestablished
208 #define	BIT_BATTERY_MODE 			0x0004	// Event  5 - the UPS is in Battery mode
209 #define	BIT_BATTERY_LOW   		0x0008	// Event  6 - the UPS has a LOW battery level
210 #define	BIT_MAINS_OK 				0x0010	// Event  7 - return of the mains
211 #define	BIT_OVERLOAD 				0x0020	// Event  8 - the UPS is overloaded
212 #define	BIT_HIGH_TEMPERATURE		0x0040	// Event  9 - the UPS is in overtemperature
213 #define	BIT_GENERIC_FAULT 		0x0080	// Event 11 - there is a generic fault
214 #define	BIT_SYSTEM_SHUTDOWN 		0x0100	// Event 14 - the UPS will shut down IMMEDIATELY
215 #define	BIT_STANDBY			 		0x0200	// Event 32 - the UPS went in STANDBY MODE
216 #define	BIT_BATTERY_END   		0x0400	// Event 33 - the UPS went in battery END
217 #define	BIT_EVENT_0					0x1000	// evento ??
218 #define	BIT_EVENT_1					0x2000	// evento ??
219 #define	BIT_FIRST_TIME				0x4000	// se è la prima volta che chiamo la funzione
220 #define	BIT_POLL_RESTART			0x8000	// se riesco ad identificare l'UPS dopo lo scollegamento, reinvio i dati
221 
222 
223 
224 typedef struct
225    {
226    int Port ;       // porta a cui è collegato l' UPS:
227                      //    0 = USB
228                      //    n = COMn
229    char Opened ;    // BOOL flag che identifica se la porta è aperta
230    int ErrCode ;		// ultimo codice di errore in fase di lettura
231    int ErrCount ; 	// conteggio degli errori
232    unsigned char PollFlag ;	// identifica se posso accedere all'UPS
233    									// 0x01: se TRUE, polling abilitato
234    									// 0x02: se TRUE, la routine di polling dati è attiva: devo attendere
235    									// 0x04: se TRUE, bisogna rileggere la configurazione del gruppo
236    //------------------------------------------------------------------------
237    unsigned long Counter ;    // contatore: viene incrementato ad ogni nuovo POLL
238 	unsigned char CommStatus ;	// stato delle comunicazioni
239 	unsigned char FramePointer ; // puntatore al carattere di START dei dati ricevuti
240    //------------------------------------------------------------------------
241    char UpsModel[9] ;		// modello UPS (8 caratteri)
242 	unsigned char ge_2kVA ;	// if more or equal to 2KVA
243    char SerialNumber[9] ;	// numero di serie dell'UPS
244 
245    unsigned short int  YearOfProd ;	// anno di produzione dell'UPS
246    unsigned char MonthOfProd ;		// Mese di produzione dell'UPS
247    unsigned char DayOfProd ;			// Giorno di produzione dell'UPS
248    unsigned char HW_MajorVersion ;	// Hardware: Major version
249    unsigned char HW_MinorVersion ;	// Hardware: Minor version
250    unsigned char BR_MajorVersion ;	// BoardHardware: Major version
251    unsigned char BR_MinorVersion ;	// BoardHardware: Minor version
252    unsigned char FW_MajorVersion ;	// Firmware: Major version
253    unsigned char FW_MinorVersion ;	// Firmware: Minor version
254    unsigned char FW_SubVersion ;		// Firmware: SUBVERSION (special releases
255 												//					for particular customers)
256 
257    unsigned char BatteryNumber ;		// number of batteries in UPS
258    //------------------------------------------------------------------------
259    unsigned long StatusUPS ;	// flag di stato dell'UPS 4 byte): 1=TRUE
260 									//		bit  0 => BATTERY_MODE
261 									//		bit  1 => BATTERY_LOW
262 									//		bit  2 => BATTERY_END
263 									//		bit  3 => ONLINE (funzionamento in rete)
264 									//		bit  4 => MAINS_ON (rete di ingresso presente)
265 									//		bit  5 => STANDBY (UPS in modo Standby)
266 									//		bit  6 => WAIT_MAINS	(UPS in fase di INIT + attesa rete)
267 									//		bit  7 => INIT (UPS in fase di inizializzazione)
268 									//		------
269 									//		bit  8 => MASK_OK (semionda OK)
270 									//		bit  9 => MEAN_OK (media tensione di ingresso OK)
271 									//		bit 10 => SYNC_OK (sincronizzazione semionda OK)
272 									//		bit 11 => FAULT (generico)
273 									//		bit 12 => TEMP_MAX (superato livello critico di temperatura)
274 									//		bit 13 => TEMP_FAULT (Fault da temperatura: UPS si spegne)
275 									//		bit 14 => LOAD_MAX (soglia overload superata)
276 									//		bit 15 => LOAD_FAULT (Fault da carico: UPS si spegne)
277 									//		------
278 									//		bit 16 => INV_FAULT (Fault dell'inverter)
279 									//		bit 17 => IINV_MAX (eccessiva corrente sull'inverter)
280 									//		bit 18 => IINV_FAULT (Fault sulla corrente inverter: l'UPS si spegne)
281 									//		bit 19 => 50_60Hz (1=60Hz)
282 									//		bit 20 => EEP_FAULT (problemi con la EEPROM)
283 									//		bit 21 => VOUT_FAULT (tensione uscita troppo bassa)
284 									//		bit 22 => - non definito -
285 									//		bit 23 => - non definito -
286 									//		------
287 									//		bit 24 to 31 => - NON DEFINITI -
288 
289    unsigned short int  ShortStatus ;   // the LSB 2 bytes of the status
290    unsigned char OutConfig  ; // stato uscite UPS
291    float Vinput     ; // tensione di INPUT in 1/10 di Volt
292    float Voutput    ; // tensione di OUTPUT in 1/10 di Volt
293    float Temp       ; // temperatura in 1/10 di grado
294    float InpFreq    ; // Frequenza di INPUT in 1/10 di Hz
295    float OutFreq    ; // Frequenza di OUTPUT in 1/10 di Hz
296    float OutCurrent ; // Corrente di Output in 1/10 di A
297    unsigned char LoadPerc   ; // Percentuale di carico
298    unsigned short int  LoadVA     ; // Carico in VA
299    float ChgCurrent ; // corrente carica batterie
300 
301    float Vbatt     ; // tensione delle batterie in 1/10 di Volt
302    unsigned char PercBatt  ; // percentuale di carica della batteria
303    unsigned short int  RtimeEmpty; // minuti alla scarica della batteria
304    unsigned char PercEmpty ; // percentuale alla scarica: 0%=scarica, 100%=carica
305    unsigned char OutStatus ; // stato delle uscite
306 
307    unsigned char Year      ; //
308    unsigned char Month     ; //
309    unsigned char Day       ; //
310    unsigned char WeekDay   ; // Giorno della settimana
311    unsigned char Hour      ; // Ora
312    unsigned char Min       ; // minuti
313    unsigned char Sec       ; // secondi
314 
315 	unsigned char BattLowPerc ; // percentuale carica batteria quando va in BLOW
316    unsigned long Rtime     ; // numero di minuti di accensione dell'UPS
317    unsigned long RtimeBatt ; // numero di secondi*2 in modalità batteria dall'accensione
318    //------------------------------------------------------------------------
319 	unsigned int ShutdownDelay ;	// Shutdown delay in seconds
320 	unsigned int WakeUpDelay ;		// WakeUp delay in seconds
321    } ENT_STRUCT ;
322 
323 
324 
325 #endif	// ENTERPRISE_PROTOCOL
326 
327 #endif
328