KM3NeT CLB  2.0
KM3NeT CLB v2 Embedded Software
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
bps.c
1 /*
2  * KM3NeT CLB v2 Firmware
3  *
4  * Copyright 2015 KM3NeT Collaboration
5  *
6  * All Rights Reserved.
7  *
8  *
9  * File : bps.c
10  * Created : 23 feb 2015
11  * Author : Riccardo Travaglini
12  */
13 
14 #include <stddef.h>
15 #include "kernel/tm.h"
16 #include "kernel/err.h"
17 #include "string.h"
18 #include "assert.h"
19 #include "util/convert.h"
20 
21 
22 #include "drv/uart/bps.h"
23 
24 
25 #define BPS_TIMEOUT_MS 2000
26 
27 #define _BPS_SOF 0x01
28 #define _BPS_EOF 0x0
29 
30 //#define FULL_CMD_MAX_LENGTH 30
31 
32 // buffers for tx and rx storaging
33 #define BPS_MAX_RET_LEN 80
34 #define BPS_MAX_CMD_LEN 80
35 
36 static unsigned char _BpsCmdBuf[BPS_MAX_CMD_LEN];
37 static unsigned char _retBpsCmdBuf[BPS_MAX_RET_LEN];
38 
39 // fixed address
40 #define BPS_ADDR 0x30
41 
42 //size of uint16_t in the BPS command Data field is encoded with 4 bytes
43 
44 #define _BPS_USHORT_SIZE 4
45 #define _BPS_UCHAR_SIZE 2
46 
47 
48 //command list and relative command response - version 1.3
49 #define _BPSCMD_ReadSensors_Request 0x03
50 #define _BPSCMD_ReadSensors_Answer 0x04
51 #define _BPSCMD_ReleToggle_BackBone_Request 0x05
52 #define _BPSCMD_ReleToggle_BackBone_Answer 0x06
53 #define _BPSCMD_ReleToggle_12Vdc_Request 0x0B
54 #define _BPSCMD_ReleToggle_12Vdc_Answer 0x0C
55 #define _BPSCMD_PingDevice_Request 0x2F
56 #define _BPSCMD_PingDevice_Answer 0x30
57 #define _BPSCMD_Alarm_Enable_Save_Request 0x25
58 #define _BPSCMD_Alarm_Enable_Save_Answer 0x26
59 #define _BPSCMD_Alarm_Enable_Load_Request 0x27
60 #define _BPSCMD_Alarm_Enable_Load_Answer 0x28
61 #define _BPSCMD_Alarm_ClearStatus_Request 0x29
62 #define _BPSCMD_Alarm_ClearStatus_Answer 0x2A
63 #define _BPSCMD_Alarm_Threshold_Save_Request 0x2B
64 #define _BPSCMD_Alarm_Threshold_Save_Answer 0x2C
65 #define _BPSCMD_Alarm_Threshold_Load_Request 0x2D
66 #define _BPSCMD_Alarm_Threshold_Load_Answer 0x2E
67 
68 // COMMAND RETURN ERRORS
69 #define _BPSCMD_Error_Chk 0x02
70 #define _BPSCMD_Error_Start 0x04
71 #define _BPSCMD_Error_Timeout 0x08
72 
73 
74 // Sending Command structure and init macro
75 typedef struct {
76  uint8_t addr;
77  uint8_t cmd;
78  uint8_t *pData;
79  uint8_t nData;
80 }BpsCmd;
81 
82 typedef struct {
83  uint8_t sof;
84  uint8_t addr;
85  uint8_t cmd;
86  uint8_t chk;
87  uint8_t eof;
88 }BpsAnswFix;
89 
90 typedef struct {
91  BpsAnswFix hf;
92  uint8_t *pData;
93  uint8_t nData;
94 }BpsAnsw;
95 
96 
97 #define NTHRESHOLD 2
98 #define SIZE_THRESHOLD (NTHRESHOLD * _BPS_USHORT_SIZE)
99 
100 /*#define BSP_CMD_INIT \
101 { \
102  .addr = BPS_ADDR, \
103  .pData = 0x0, \
104  .nData = 0x0 \
105 }*/
106 static const BpsCmd BSP_CMD_INIT = {BPS_ADDR,0x0,0x0,0x0};
107 
108 static const CnvParams _hex = CNV_DEFAULT_HEX;
109 
110 
111 
112 /*info
113 
114 uint16 is uint8[0] LSB and uint8[1] MSB
115 uint8[0] is tx/rx before uint8[1]
116 
117 */
118 
119 
120 #ifdef BPSSIM
121 #include <stdio.h>
122 #endif
123 
124 
125 /*Utility Macros
126 * Byte order in BPS command is Little Endian (from Angelo)
127 */
128 /*
129 #if __BYTE_ORDER__==__ORDER_BIG_ENDIAN__
130 
131 #define HTOBPSS(n) (((((unsigned short)(n) & 0xFF)) << 8) | (((unsigned short)(n) & 0xFF00) >> 8))
132 #define BPSTOHS(n) (((((unsigned short)(n) & 0xFF)) << 8) | (((unsigned short)(n) & 0xFF00) >> 8))
133 #define HTOBPSL(n) (((((unsigned long)(n) & 0xFF)) << 24) | \
134  ((((unsigned long)(n) & 0xFF00)) << 8) | \
135  ((((unsigned long)(n) & 0xFF0000)) >> 8) | \
136  ((((unsigned long)(n) & 0xFF000000)) >> 24))
137 #define BPSTOHL(n) (((((unsigned long)(n) & 0xFF)) << 24) | \
138  ((((unsigned long)(n) & 0xFF00)) << 8) | \
139  ((((unsigned long)(n) & 0xFF0000)) >> 8) | \
140  ((((unsigned long)(n) & 0xFF000000)) >> 24))
141 
142 #else
143 
144 #if __BYTE_ORDER__==__ORDER_LITTLE_ENDIAN__
145 
146 #define HTOBPSS(n) (n)
147 #define BPSTOHS(n) (n)
148 #define HTOBPSL(n) (n)
149 #define BPSTOHL(n) (n)
150 
151 #else
152 
153 #error "Platform endianess could not be detected"
154 
155 #endif
156 #endif
157 
158 unsigned short htobpss(unsigned short n);
159 unsigned short bpstohs(unsigned short n);
160 unsigned long htobpsl(unsigned long n);
161 unsigned long bpstohl(unsigned long n);
162 
163 #define htobpss(n) HTOBPSS(n)
164 #define bpstohs(n) BPSTOHS(n)
165 
166 #define htobpsl(n) HTOBPSL(n)
167 #define bpstohl(n) BPSTOHL(n) */
168 
169 /* private functions prototypes and Macro */
170 bool _bpsIn(char *c );
171 void _bpsOut(char c);
172 unsigned char bpschecksum(unsigned char* buff, int num);
173 unsigned char s_bpschecksum(BpsAnsw* answ);
174 void _bpsCmdTx (int nBytes);
175 bool _bpsRxCommand(BpsAnsw* answ);
176 int _bpsBufferCmd(BpsCmd* _cmd);
177 bool _bpsCmdRxCheck(uint8_t answ, BpsAnsw* s_answ);
178 bool _bpsTxAndRx(BpsCmd *pcmd,BpsAnsw* pansw, uint8_t cansw);
179 
180 
181 
182 /*
183 rx a char from the bps uart and return false if error
184 */
185 bool _bpsIn(char *c )
186 {
187 
188  if(bfEmpty(BPS_UART->rxfifo))return false;
189 
190  __irqDisable();
191  bfRead(BPS_UART->rxfifo,(uint8_t*) c);
192  __irqEnable();
193 #ifdef BPSDEBUG
194  printf("rx -> 0x%x (%d)\n",*c,*c);
195 #endif
196  return true;
197 }
198 
199 /*
200 send a char to the edfa uart
201 */
202 void _bpsOut(char c)
203 {
204 #ifndef BPSSIM
205  suartTx(BPS_UART,c);
206 #endif
207 #ifdef BPSDEBUG
208  printf("tx -> 0x%x (%d)\n",c,c);
209 #endif
210 
211 }
212 
213 //calculate BPS-like checksum for buffer of num elements
214 
215 unsigned char bpschecksum(unsigned char* buff, int num)
216 {
217 /* unsigned short chk = 0;
218  int i=0;
219  while(i<num)
220  {
221  chk += buff[i];
222  i++;
223  }
224  while(chk >> 8){ chk = (chk & 0xFF) + (chk >> 8);} // rounding cheksum
225  chk = ~chk; //1-complement
226  return (chk & 0xFF);*/
227  unsigned char chk = 256;
228  int i=0;
229  while(i<num)
230  {
231  chk -= buff[i];
232  i++;
233  }
234 
235  return (chk );
236 }
237 
238 unsigned char s_bpschecksum(BpsAnsw* answ)
239 {
240 
241  assert(answ);
242  unsigned char chk;
243 
244  chk = bpschecksum(answ->pData, answ->nData);
245 
246  chk -= answ->hf.addr ;
247  chk -= answ->hf.cmd ;
248  return (chk );
249 }
250 
251 /*
252 send nBytes bytes of the CMD buffer, calculates anbd tx checksum and append a 0xFF at the end
253 */
254 void _bpsCmdTx (int nBytes)
255 {
256 
257  assert(nBytes < BPS_MAX_CMD_LEN);
258 //#ifndef BPSSIM
259  int i = 0;
260 
261  _bpsOut(0x0); // puts a 0 at the beginning to wakeup the rs-232 driver (FMC mezzanine issue due to MAX3227 autopowerdown)
262  _bpsOut(0x0); // tested : one more 0 is mandatory to always wake up
263  _bpsOut(_BPS_SOF);
264 
265  for(;i<nBytes;i++)
266  {
267  _bpsOut(_BpsCmdBuf[i]);
268  }
269 
270 
271  _bpsOut(bpschecksum(_BpsCmdBuf,nBytes));
272 
273 
274  _bpsOut(_BPS_EOF);
275 //#endif
276  return;
277 }
278 
279 /*
280 receive command return, put it into the _retEdfaCmdBuf buffer; expected nBytes char, set error if incomplete (< nBytes) after timeout
281 nBytes doesn't include EOF (0xFF) and checksum
282 */
283 bool _bpsRxCommand(BpsAnsw* answ)
284 {
285 
286  assert(answ);
287  assert(answ->nData + sizeof(BpsAnswFix) < BPS_MAX_RET_LEN);
288 #ifndef BPSSIM
289  uint32_t to = timeOutInit(BPS_TIMEOUT_MS);
290  int i = 0;
291  char c;
292  bool isFrame = false;
293 
294 
295  while(i< (answ->nData + sizeof(BpsAnswFix) )){
296 
297  if (timeOut(to)) {
299  return false;
300  }
301  if(_bpsIn(&c) ){
302  if(c == _BPS_SOF || isFrame){
303  _retBpsCmdBuf[i] = (unsigned char) c;
304  isFrame = true;
305  i++;
306  }
307  }
308  }
309 
310 
311 #else
312 
313 
314  _retBpsCmdBuf[0] = _BPS_SOF;
315  _retBpsCmdBuf[1] = _BpsCmdBuf[0];
316 
317  // insert emulated response here
318  if(_BpsCmdBuf[1] == _BPSCMD_ReadSensors_Request){
319  _retBpsCmdBuf[2] = _BPSCMD_ReadSensors_Answer;
320  _retBpsCmdBuf[3] = '1';
321  _retBpsCmdBuf[4] = '2';
322  _retBpsCmdBuf[5] = '3';
323  _retBpsCmdBuf[6] = '4'; //1
324  _retBpsCmdBuf[7] = '5';
325  _retBpsCmdBuf[8] = '6';
326  _retBpsCmdBuf[9] = '7';
327  _retBpsCmdBuf[10] = '8'; // 2
328  _retBpsCmdBuf[11] = '9';
329  _retBpsCmdBuf[12] = 'a';
330  _retBpsCmdBuf[13] = 'b';
331  _retBpsCmdBuf[14] = 'c'; //3
332  _retBpsCmdBuf[15] = 'd';
333  _retBpsCmdBuf[16] = 'e';
334  _retBpsCmdBuf[17] = 'f';
335  _retBpsCmdBuf[18] = '0'; // 4
336  _retBpsCmdBuf[19] = '1';
337  _retBpsCmdBuf[20] = '2';
338  _retBpsCmdBuf[21] = '3';
339  _retBpsCmdBuf[22] = '4'; //5
340  _retBpsCmdBuf[23] = '5';
341  _retBpsCmdBuf[24] = '6';
342  _retBpsCmdBuf[25] = '7';
343  _retBpsCmdBuf[26] = '8'; // 6
344  _retBpsCmdBuf[27] = '9';
345  _retBpsCmdBuf[28] = 'a';
346  _retBpsCmdBuf[29] = 'b';
347  _retBpsCmdBuf[30] = 'c'; //7
348  _retBpsCmdBuf[31] = 'd';
349  _retBpsCmdBuf[32] = 'e';
350  _retBpsCmdBuf[33] = 'f';
351  _retBpsCmdBuf[34] = '0'; //8
352  _retBpsCmdBuf[35] = bpschecksum(_retBpsCmdBuf+1,34);
353  _retBpsCmdBuf[36] = _BPS_EOF;
354 
355  }
356  else if(_BpsCmdBuf[1] == _BPSCMD_Alarm_Enable_Load_Request){
357  _retBpsCmdBuf[2] = _BPSCMD_Alarm_Enable_Load_Answer;
358  _retBpsCmdBuf[3] = '1';
359  _retBpsCmdBuf[4] = '2';
360  _retBpsCmdBuf[5] = '3';
361  _retBpsCmdBuf[6] = '4';
362  _retBpsCmdBuf[7] = bpschecksum(_retBpsCmdBuf+1,6);
363  _retBpsCmdBuf[8] = _BPS_EOF;
364  }
365  else if(_BpsCmdBuf[1] == _BPSCMD_Alarm_Enable_Save_Request){
366  _retBpsCmdBuf[2] = _BPSCMD_Alarm_Enable_Save_Answer;
367  _retBpsCmdBuf[3] = _BpsCmdBuf[2];
368  _retBpsCmdBuf[4] = _BpsCmdBuf[3];
369  _retBpsCmdBuf[5] = _BpsCmdBuf[4];
370  _retBpsCmdBuf[6] = _BpsCmdBuf[5];
371  _retBpsCmdBuf[7] = bpschecksum(_retBpsCmdBuf+1,6);
372  _retBpsCmdBuf[8] = _BPS_EOF;
373  }
374  else if(_BpsCmdBuf[1] == _BPSCMD_Alarm_ClearStatus_Request){
375  _retBpsCmdBuf[2] = _BPSCMD_Alarm_ClearStatus_Answer;
376  _retBpsCmdBuf[3] = '1';
377  _retBpsCmdBuf[4] = '2';
378  _retBpsCmdBuf[5] = '3';
379  _retBpsCmdBuf[6] = '4';
380  _retBpsCmdBuf[7] = bpschecksum(_retBpsCmdBuf+1,6);
381  _retBpsCmdBuf[8] = _BPS_EOF;
382  }
383  else if(_BpsCmdBuf[1] == _BPSCMD_Alarm_Threshold_Load_Request){
384  _retBpsCmdBuf[2] = _BPSCMD_Alarm_Threshold_Load_Answer;
385  _retBpsCmdBuf[3] = '1';
386  _retBpsCmdBuf[4] = '2';
387  _retBpsCmdBuf[5] = '3';
388  _retBpsCmdBuf[6] = '4';
389  _retBpsCmdBuf[7] = '1';
390  _retBpsCmdBuf[8] = '2';
391  _retBpsCmdBuf[9] = '3';
392  _retBpsCmdBuf[10] = '4';
393  _retBpsCmdBuf[11] = bpschecksum(_retBpsCmdBuf+1,10);
394  _retBpsCmdBuf[12] = _BPS_EOF;
395  }
396  else if(_BpsCmdBuf[1] == _BPSCMD_Alarm_Threshold_Save_Request){
397  _retBpsCmdBuf[2] = _BPSCMD_Alarm_Threshold_Save_Answer;
398  _retBpsCmdBuf[3] = _BpsCmdBuf[2];
399  _retBpsCmdBuf[4] = _BpsCmdBuf[3];
400  _retBpsCmdBuf[5] = _BpsCmdBuf[4];
401  _retBpsCmdBuf[6] = _BpsCmdBuf[5];
402  _retBpsCmdBuf[7] = _BpsCmdBuf[6];
403  _retBpsCmdBuf[8] = _BpsCmdBuf[7];
404  _retBpsCmdBuf[9] = _BpsCmdBuf[8];
405  _retBpsCmdBuf[10] = _BpsCmdBuf[9];
406  _retBpsCmdBuf[11] = bpschecksum(_retBpsCmdBuf+1,10);
407  _retBpsCmdBuf[12] = _BPS_EOF;
408  }
409  else if(_BpsCmdBuf[1] == _BPSCMD_ReleToggle_Ixx_Request){
410  _retBpsCmdBuf[2] = _BPSCMD_ReleToggle_Ixx_Answer;
411  _retBpsCmdBuf[3] = 0x31;
412  _retBpsCmdBuf[4] = 0x30;
413  _retBpsCmdBuf[5] = 0x30;
414  _retBpsCmdBuf[6] = 0x30;
415  _retBpsCmdBuf[7] = bpschecksum(_retBpsCmdBuf+1,6);
416  _retBpsCmdBuf[8] = _BPS_EOF;
417  }
418  else if(_BpsCmdBuf[1] == _BPSCMD_PingDevice_Request){
419  _retBpsCmdBuf[2] = _BPSCMD_PingDevice_Answer;
420  _retBpsCmdBuf[3] = '1';
421  _retBpsCmdBuf[4] = '2';
422  _retBpsCmdBuf[5] = '3';
423  _retBpsCmdBuf[6] = '4';
424  _retBpsCmdBuf[7] = bpschecksum(_retBpsCmdBuf+1,6);
425  _retBpsCmdBuf[8] = _BPS_EOF;
426  }
427 
428 
429 #endif
430 
431  /* now fill Answer structure */
432 
433 
434  answ->hf.sof = _retBpsCmdBuf[0];
435  answ->hf.addr = _retBpsCmdBuf[1];
436  answ->hf.cmd = _retBpsCmdBuf[2];
437  answ->pData = &_retBpsCmdBuf[3];
438  answ->hf.chk = _retBpsCmdBuf[3+answ->nData];
439  answ->hf.eof = _retBpsCmdBuf[4+answ->nData];
440 
441 
442 
443  return true;
444 }
445 
446 /* Prepare a given command into the Tx buffer*/
447 int _bpsBufferCmd(BpsCmd* _cmd)
448 {
449  assert(_cmd->nData + 2 < BPS_MAX_CMD_LEN);
450  _BpsCmdBuf[0] = _cmd->addr;
451  _BpsCmdBuf[1] = _cmd->cmd;
452  if(_cmd->nData && _cmd->pData)memcpy ( _BpsCmdBuf+2, _cmd->pData, _cmd->nData );
453  return (_cmd->nData + 2);
454 
455 }
456 
457 /* Check command response (address , command and errors)*/
458 bool _bpsCmdRxCheck(uint8_t answ, BpsAnsw* s_answ)
459 {
460 
461 
462  if(s_answ->hf.sof != _BPS_SOF){
464  return false;
465  }
466 
467  if(s_bpschecksum(s_answ) != s_answ->hf.chk ){
468  //printf("c2 = %x\n",s_bpschecksum(s_answ));
470  return false;
471  }
472  if(s_answ->hf.eof != _BPS_EOF){
474  return false;
475  }
476 
477 
478  if(BPS_ADDR != s_answ->hf.addr){
479  errSet(ERROR(E_BPS_RETADDR));
480  return false;
481  }
482  if(s_answ->hf.cmd == answ) return true;
483 
484  switch(s_answ->hf.cmd){
485  case _BPSCMD_Error_Chk:
486  errSet(ERROR(E_BPS_RETCHK));
487  break;
488  case _BPSCMD_Error_Start:
489  errSet(ERROR(E_BPS_RETSTA));
490  break;
491  case _BPSCMD_Error_Timeout:
492  errSet(ERROR(E_BPS_RETTIM));
493  break;
494  default:
495  errSet(ERROR(E_BPS_RETUNK));
496  break;
497  }
498 
499  return false;
500 }
501 
502 bool _bpsTxAndRx(BpsCmd *pcmd,BpsAnsw* pansw, uint8_t cansw)
503 {
504  _bpsCmdTx (_bpsBufferCmd(pcmd));
505  if(_bpsRxCommand(pansw)) return _bpsCmdRxCheck(cansw,pansw);
506  return false;
507 }
508 
509 
510 uint16_t _bpsHexAscii2Short(uint8_t* input)
511 {
512  int j;
513  char crev[_BPS_USHORT_SIZE];
514  uint32_t lb;
515 
516  for(j=0;j<_BPS_USHORT_SIZE;j++){
517  crev[j]=input[_BPS_USHORT_SIZE-1-j]; // order is reversed : BPS send from LSB to MSB
518 #ifdef BPSSIM
519 // printf("j = %d - %c\n",j,input[_BPS_USHORT_SIZE-1-j]);
520 #endif
521  }
522 
523  if (cnvParseU(crev, &lb, _hex) == 0)return 0x0;
524 
525  return (uint16_t) (lb & 0xFFFF);
526 }
527 
528 uint8_t _bpsHexAscii2Char(uint8_t* input)
529 {
530  int j;
531  char crev[_BPS_UCHAR_SIZE];
532  uint32_t lb;
533 
534  for(j=0;j<_BPS_UCHAR_SIZE;j++){
535  crev[j]=input[_BPS_UCHAR_SIZE-1-j]; // order is reversed : BPS send from LSB to MSB
536 #ifdef BPSSIM
537 // printf("j = %d - %c\n",j,input[_BPS_USHORT_SIZE-1-j]);
538 #endif
539  }
540 
541  if (cnvParseU(crev, &lb, _hex) == 0)return 0x0;
542 
543  return (uint8_t) (lb & 0xFFFF);
544 }
545 
546 void _bpsShort2HexAscii(uint16_t sdata, uint8_t* input)
547 {
548  assert(input);
549  char outstring[2*_BPS_USHORT_SIZE]; // safe
550  int j=0;
551  int k;
552 
553 
554  if( cnvFormatU( (int32_t) sdata, outstring,_hex) == 0)return;
555 
556  //find '\0'
557  while(outstring[j] != '\0' && j<2*_BPS_USHORT_SIZE)
558  {j++;
559 #ifdef BPSSIM
560 // printf("j = %d - %c\n",j,outstring[j]);
561 #endif
562  }
563 
564  for(k=0;k<_BPS_USHORT_SIZE;k++){
565  if(j-1-k >= 0)input[k]=outstring[j-1-k]; // order is reversed : BPS send from LSB to MSB
566  else input[k] = '0';
567 #ifdef BPSSIM
568 // printf("k = %d - %d - %c\n",k,j-1-k,input[k]);
569 #endif
570  }
571 
572 
573 }
574 
575 
576 
578 
579 #define STATUS_NDATA (sizeof(BpsRdSensAnsw) / sizeof(uint16_t))
580 #define STATUS_SIZE (_BPS_USHORT_SIZE * STATUS_NDATA)
581 
582  assert(stat);
583  BpsCmd cmd = BSP_CMD_INIT;
584  cmd.cmd = _BPSCMD_ReadSensors_Request;
585  BpsAnsw ans;
586  uint16_t pRet[STATUS_NDATA];
587  int i;
588 
589 
590 
591  ans.nData = STATUS_SIZE; //4 * sizeof(BpsRdSensAnsw) / sizeof(uitn16_t); // any uin16_t (2 bytes) is transmitted with 4 bytes
592 
593  if(!_bpsTxAndRx(&cmd,&ans, _BPSCMD_ReadSensors_Answer))return false;
594 
595  for(i=0;i<STATUS_NDATA;i++){ // i loop are over any status word (4 bytes)
596 
597  pRet[i] = _bpsHexAscii2Short(ans.pData+i*_BPS_USHORT_SIZE);
598  }
599 
600  stat->mon_i = pRet[0];
601  stat->mon1_i12 = pRet[1];
602  stat->mon2_v375 = pRet[2];
603  stat->spare = pRet[3];
604  stat->mon3_i375 = pRet[4];
605  stat->mon4_v375 = pRet[5];
606  stat->breaker = pRet[6];
607  stat->alarm = pRet[7];
608 
609  return true;
610 
611 
612 }
613 
614 bool bpsAlarmEnLoad(uint16_t *alarms){
615 
616  assert(alarms);
617 
618  BpsCmd cmd = BSP_CMD_INIT;
619  cmd.cmd = _BPSCMD_Alarm_Enable_Load_Request;
620  BpsAnsw ans;
621  ans.nData = _BPS_USHORT_SIZE;
622 
623  if(!_bpsTxAndRx(&cmd,&ans, _BPSCMD_Alarm_Enable_Load_Answer))return false;
624 
625  *alarms = _bpsHexAscii2Short(ans.pData);
626 
627  return true;
628 }
629 
630 bool bpsAlarmEnSave(uint16_t newal, uint16_t *alarms){
631 
632  assert(alarms);
633  uint8_t sData[_BPS_USHORT_SIZE];
634 
635  BpsCmd cmd = BSP_CMD_INIT;
636  cmd.cmd = _BPSCMD_Alarm_Enable_Save_Request;
637  cmd.nData = _BPS_USHORT_SIZE;
638  cmd.pData = sData;
639  BpsAnsw ans;
640  ans.nData = _BPS_USHORT_SIZE;
641 
642  _bpsShort2HexAscii(newal,sData);
643 
644  if(!_bpsTxAndRx(&cmd,&ans, _BPSCMD_Alarm_Enable_Save_Answer))return false;
645 
646 
647 
648  *alarms = _bpsHexAscii2Short(ans.pData);
649 
650 
651  return true;
652 }
653 
654 bool bpsAlarmClearStat(uint16_t *alarms){
655  assert(alarms);
656 
657  BpsCmd cmd = BSP_CMD_INIT;
658  cmd.cmd = _BPSCMD_Alarm_ClearStatus_Request;
659  BpsAnsw ans;
660  ans.nData = _BPS_USHORT_SIZE;
661 
662  if(!_bpsTxAndRx(&cmd,&ans, _BPSCMD_Alarm_ClearStatus_Answer))return false;
663 
664  *alarms = _bpsHexAscii2Short(ans.pData);
665  return true;
666 }
667 
668 
669 
670 
672 
673 
674 
675  assert(th);
676  BpsCmd cmd = BSP_CMD_INIT;
677  cmd.cmd = _BPSCMD_Alarm_Threshold_Load_Request;
678  BpsAnsw ans;
679  int i;
680  uint16_t rawth[NTHRESHOLD];
681 
682 
683  ans.nData = SIZE_THRESHOLD; //4 * sizeof(BpsRdSensAnsw) / sizeof(uitn16_t); // any uin16_t (2 bytes) is transmitted with 4 bytes
684 
685  if(!_bpsTxAndRx(&cmd,&ans, _BPSCMD_Alarm_Threshold_Load_Answer))return false;
686 
687  for(i=0;i<NTHRESHOLD;i++){ // i loop are over any status word (4 bytes)
688 
689  rawth[i] = _bpsHexAscii2Short(ans.pData+i*_BPS_USHORT_SIZE);
690  }
691 
692  th->thr_mon_375i0 = rawth[0] ;
693  th->thr_mon_12v = rawth[1];
694 
695  return true;
696 }
697 
698 bool bpsAlarmThSave(BspTh *newth, BspTh *th){
699 
700  assert(th);
701 
702  assert(newth);
703  uint8_t sData[SIZE_THRESHOLD];
704  int i;
705  uint16_t rawth[NTHRESHOLD];
706 
707  BpsCmd cmd = BSP_CMD_INIT;
708  cmd.cmd = _BPSCMD_Alarm_Threshold_Save_Request;
709  cmd.nData = SIZE_THRESHOLD;
710  cmd.pData = sData;
711  BpsAnsw ans;
712  ans.nData = SIZE_THRESHOLD;
713 
714  rawth[0] = newth->thr_mon_375i0;
715  rawth[1] = newth->thr_mon_12v;
716 
717 
718  for(i=0;i<NTHRESHOLD;i++){
719  _bpsShort2HexAscii(rawth[i],sData+i*_BPS_USHORT_SIZE);
720  }
721 
722  if(!_bpsTxAndRx(&cmd,&ans, _BPSCMD_Alarm_Threshold_Save_Answer))return false;
723 
724 
725 
726  for(i=0;i<NTHRESHOLD;i++){ // i loop are over any status word (4 bytes)
727 
728  rawth[i] = _bpsHexAscii2Short(ans.pData+i*_BPS_USHORT_SIZE);
729  }
730 
731  th->thr_mon_375i0 = rawth[0] ;
732  th->thr_mon_12v = rawth[1];
733 
734  return true;
735 }
736 
737 bool bpsRelayToggleBackBone(uint16_t *breakers){
738  assert(breakers);
739 
740 
741  BpsCmd cmd = BSP_CMD_INIT;
742  cmd.cmd = _BPSCMD_ReleToggle_BackBone_Request;
743  BpsAnsw ans;
744  ans.nData = _BPS_USHORT_SIZE;
745 
746  if(!_bpsTxAndRx(&cmd,&ans, _BPSCMD_ReleToggle_BackBone_Answer))return false;
747 
748 
749  *breakers = _bpsHexAscii2Short(ans.pData);
750 
751  return true;
752 }
753 bool bpsRelayToggle12V(uint16_t *breakers){
754  assert(breakers);
755 
756 
757  BpsCmd cmd = BSP_CMD_INIT;
758  cmd.cmd = _BPSCMD_ReleToggle_12Vdc_Request;
759  BpsAnsw ans;
760  ans.nData = _BPS_USHORT_SIZE;
761 
762  if(!_bpsTxAndRx(&cmd,&ans, _BPSCMD_ReleToggle_12Vdc_Answer))return false;
763 
764 
765  *breakers = _bpsHexAscii2Short(ans.pData);
766 
767  return true;
768 }
769 
770 bool bpsPingDevice(uint16_t *fw){
771 
772  assert(fw);
773 
774  BpsCmd cmd = BSP_CMD_INIT;
775  cmd.cmd = _BPSCMD_PingDevice_Request;
776  BpsAnsw ans;
777  ans.nData = sizeof(uint8_t);
778 
779  if(!_bpsTxAndRx(&cmd,&ans, _BPSCMD_PingDevice_Answer))return false;
780 
781 
782  *fw = (uint16_t) (ans.pData[0] & 0xFF);
783 
784 
785  return true;
786 }
787 
788 bool bpsDbgCmdReply(uint8_t cmdCode, uint8_t * cmdPData, uint8_t cmdNData,
789  uint8_t rplCode, uint8_t ** rplPData, uint8_t rplNData)
790 {
791  BpsCmd cmd = BSP_CMD_INIT ;
792  BpsAnsw ans;
793  cmd.cmd = cmdCode;
794  cmd.pData = cmdPData;
795  cmd.nData = cmdNData;
796  ans.nData = rplNData;
797 
798  if (!_bpsTxAndRx(&cmd, &ans, rplCode)) return false;
799 
800  *rplPData = ans.pData;
801 
802  return true;
803 }
804 
805 
806 
int cnvFormatU(int32_t input, char *output, CnvParams params)
Formats an unsigned integer into a character buffer.
Definition: convert.c:137
Definition: bps.h:75
BPS Uart driver (.
static bool bfEmpty(ByteFifo *const bf)
Returns whether or not the byte-fifo is empty.
Definition: bytefifo.h:72
bool bpsDbgCmdReply(uint8_t cmdCode, uint8_t *cmdPData, uint8_t cmdNData, uint8_t rplCode, uint8_t **rplPData, uint8_t rplNData)
Exposes the low-level interface to the BPS.
Definition: bps.c:788
bool bpsRelayToggleBackBone(uint16_t *breakers)
Toggle relay status.
Definition: bps.c:737
static void __irqEnable()
Enabled IRQ&#39;s on a global level.
Definition: lm32.h:75
int cnvParseU(const char *input, uint32_t *output, CnvParams params)
Parse an unsigned integer.
Definition: convert.c:61
bool bpsAlarmClearStat(uint16_t *alarms)
Clear Status Alarm.
Definition: bps.c:654
bool bpsAlarmThLoad(BspTh *th)
load already saved alarm threshold (in mA)
Definition: bps.c:671
static void __irqDisable()
Disables IRQ&#39;s on a global level.
Definition: lm32.h:62
Definition: bps.c:90
bool bpsPingDevice(uint16_t *fw)
Ping Device.
Definition: bps.c:770
bool bpsAlarmThSave(BspTh *newth, BspTh *th)
save alarm threshold (in mA)
Definition: bps.c:698
#define E_BPS_TIMEOUT
Receive timeout.
Definition: bps.h:31
static uint32_t timeOutInit(uint32_t msec)
Initializes a timeout with the specified no of msecs.
Definition: tm.h:53
static bool timeOut(uint32_t to)
Checks whether or not the timeout has expired.
Definition: tm.h:77
Manages the global system error.
Definition: bps.c:82
This structure provides information about formatting and parsing.
Definition: convert.h:37
Simple timer functions.
bool bpsReadSensor(BpsRdSensAnsw *stat)
return sensor values
Definition: bps.c:577
Definition: bps.c:75
#define E_BPS_NOSOF
no EOF received when expected
Definition: bps.h:55
#define E_BPS_CHKERR
bad checksum received
Definition: bps.h:34
bool bfRead(ByteFifo *const bf, uint8_t *const b)
Reads a byte from the byte-fifo.
Definition: bytefifo.c:28
#define E_BPS_NOEOF
no EOF received when expected
Definition: bps.h:37
bool errSet(uint32_t code, const char *error, const char *name)
Sets an error.
#define ERROR(CODE,...)
Expands an error code to an error code with a description (if ERROR_W_DESCR is declared).
bool suartTx(SUART_Descriptor *desc, char c)
Transmits a character.
Definition: suart.c:58
bool bpsAlarmEnSave(uint16_t newal, uint16_t *alarms)
save alarm enable
Definition: bps.c:630
This module implements parsing and formating of strings and integers.
bool bpsAlarmEnLoad(uint16_t *alarms)
Load already saved alarm enable.
Definition: bps.c:614