KM3NeT CLB  2.0
KM3NeT CLB v2 Embedded Software
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
timer.c
1 /*
2  * KM3NeT CLB v2 Firmware
3  *
4  * Copyright 2013 KM3NeT Collaboration
5  *
6  * All Rights Reserved.
7  *
8  * timer.c
9  *
10  * Created on: 16 jul. 2013
11  * Author: Vincent van Beveren
12  */
13 
14 #include "kernel/timer.h"
15 #include "drv/wb/ticks.h" // poor mans clock
16 //#include "lm32soc/lm32.h"
17 
18 static uint32_t _timers[2][TIMER_SECT_COUNT];
19 static uint32_t * _timerNow = _timers[0];
20 static uint32_t * _timerLast = _timers[1];
21 
22 static uint32_t _tTot;
23 static uint32_t _tNow;
24 
25 static int _sect = TIMER_SECT_IDLE;
26 
27 void timerInit()
28 {
29  _tNow = ticks();
30  _tTot = 0;
31 }
32 
33 static uint32_t elapsed()
34 {
35  uint32_t c = ticks();
36  uint32_t e = c - _tNow;
37  _tNow = c;
38  return e;
39 }
40 
41 static inline void newRound()
42 {
43  int i;
44  // swap arrays
45  uint32_t * t = _timerNow;
46  _timerNow = _timerLast;
47  _timerLast = t;
48 
49  // clear timers
50  for (i = 0; i < TIMER_SECT_COUNT; ++i) _timerNow[i] = 0;
51 }
52 
53 int timerMark(int section)
54 {
55  uint32_t e;
56 
57  e = elapsed();
58  _tTot += e;
59  if (_tTot >= TIMER_MAX_COUNT)
60  {
61  _tTot -= TIMER_MAX_COUNT;
62  if (_sect >= 0) _timerNow[_sect] += e - _tTot;
63  e = _tTot;
64  newRound();
65  }
66  _timerNow[_sect] += e;
67  _sect = section;
68  return e;
69 }
70 
71 uint32_t * timerValues()
72 {
73  return _timerLast;
74 }
#define TIMER_SECT_COUNT
No of timers to add.
Definition: timer.h:30
int timerMark(int section)
Starts the timing of a new section, the previous section is returned, or -1 if it is the first...
Definition: timer.c:53
White Rabbit simple timer &#39;Ticks&#39; driver.
#define TIMER_SECT_IDLE
time spend being idle
Definition: timer.h:33
static uint32_t ticks()
Nr of ticks since device start up.
Definition: ticks.h:37
uint32_t * timerValues()
Returns.
Definition: timer.c:71
void timerInit()
Initializes the timer.
Definition: timer.c:27