KM3NeT CLB  2.0
KM3NeT CLB v2 Embedded Software
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
acdc.c
1 /*
2  * KM3NeT CLB v2 Firmware
3  *
4  * Copyright 2015 KM3NeT Collaboration
5  *
6  * All Rights Reserved.
7  *
8  *
9  * File : acdc.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 
18 
19 
20 #include "drv/uart/acdc.h"
21 
22 #include "assert.h"
23 
24 #define ACDC_START_FRAME 0xFE
25 
26 #define ACDC_TIMEOUT_MS 2000
27 
28 #ifdef ACDCSIM
29 
30 static uint8_t retval = 0;
31 #endif
32 
33 
34 
35 void acdcInit(bool active)
36 {
37  suartInit(ACDC_UART, ACDC_BAUDRATE_DEFAULT);
38  acdcEnable(active);
39 }
40 
41 /*
42 rx a char from the acdc uart and return false if error
43 */
44 bool _acdcIn(char *c )
45 {
46 
47  if(bfEmpty(ACDC_UART->rxfifo))return false;
48 
49  __irqDisable();
50  bfRead(ACDC_UART->rxfifo,(uint8_t*) c);
51  __irqEnable();
52 #ifdef ACDCDEBUG
53  printf("rx -> 0x%x\n",*c);
54 #endif
55  return true;
56 }
57 
58 void acdcEnable(bool ena)
59 {
60  __irqDisable();
61  irqMaskSet(ACDC_UART->interrupt, ena);
62  __irqEnable();
63 }
64 
65 bool _acdcGetFrame(AcdcFrame* fr)
66 {
67  assert(fr);
68 #ifndef ACDCSIM
69  char c;
70  uint32_t to = timeOutInit(ACDC_TIMEOUT_MS);
71  bool ok ;
72  int iloop = 0;
73  while (1)
74  {
75  ok = _acdcIn(&c);
76  if(ok){
77  if (c == ACDC_START_FRAME || iloop >0 )
78  {
79  if(iloop == 1) fr->temp = (uint8_t) c;
80  else if(iloop == 2) fr->volt = (uint8_t) c;
81  else if(iloop == 3) {
82  fr->current = (uint8_t) c;
83  return true;
84  }
85  iloop++;
86  }
87  }
88  if (timeOut(to)) {
90  return false;
91  }
92  }
93  return false;
94 #else
95  fr->temp = retval++;
96  fr->volt = retval++;
97  fr->current = retval++;
98 #endif
99 }
100 
101 
102 int acdcGetNFrames(AcdcFrame* framevect , int nRequested)
103 {
104  int got = 0;
105  while(got < nRequested){
106  if(!_acdcGetFrame(framevect))break;
107  framevect++;
108  got++;
109  }
110  return got;
111 }
112 
113 
114 
static bool bfEmpty(ByteFifo *const bf)
Returns whether or not the byte-fifo is empty.
Definition: bytefifo.h:72
static void __irqEnable()
Enabled IRQ&#39;s on a global level.
Definition: lm32.h:75
static void __irqDisable()
Disables IRQ&#39;s on a global level.
Definition: lm32.h:62
void acdcEnable(bool ena)
enables the ACDC rx
Definition: acdc.c:58
void suartInit(SUART_Descriptor *desc, unsigned int baudrate)
Initializes the simple UART.
Definition: suart.c:44
void irqMaskSet(int irq, bool set)
Set/clear the interrupt mask for the specified IRQ.
Definition: lm32.c:77
#define E_ACDC_TIMEOUT
Receive timeout.
Definition: acdc.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.
Simple timer functions.
void acdcInit(bool active)
Initializes the ACDC.
Definition: acdc.c:35
ACDC Uart driver (.
bool bfRead(ByteFifo *const bf, uint8_t *const b)
Reads a byte from the byte-fifo.
Definition: bytefifo.c:28
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).
Definition: acdc.h:37
#define ACDC_BAUDRATE_DEFAULT
Default Baudrate.
Definition: acdc.h:35