KM3NeT CLB  2.0
KM3NeT CLB v2 Embedded Software
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
app.c
1 /*
2  * KM3NeT CLB v2 Firmware
3  *
4  * Copyright 2013 KM3NeT Collaboration
5  *
6  * All Rights Reserved.
7  *
8  *
9  * File : main.c
10  * Created : 17 jan 2013
11  * Author : Vincent van Beveren
12  */
13 
14 #include <stdio.h>
15 
16 
17 #include "drv/wb/suart.h"
18 #include "drv/wb/gpio.h"
19 #include "drv/wb/i2c.h"
20 #include "drv/wb/ticks.h"
21 #include "util/shell.h"
22 #include "kernel/sys.h"
23 #include "kernel/scheduler.h"
24 #include "kernel/err.h"
25 #include "net/network.h"
26 #include "drv/i2c/pca9548.h"
27 #include "remote/msg_clb.h"
28 #include "clbstate.h"
29 #include "cfg_board.h"
30 
31 #define EVENT_THROTTLE_BACKOFF_MAX 10
32 
33 static int _evtThrottleBackOff = 0;
34 static int _evtThrottleTimeOut = 0;
35 
36 #ifdef BASE
37 const char _appName[] = "KM3NeT CLB v2 BASE";
38 #else
39 const char _appName[] = "KM3NeT CLB v2 Runtime";
40 #endif
41 
42 void appPeriodicUpdate();
43 
44 // called from sys.c
45 void _appInit()
46 {
47 
48  // initialize state machine, all subsystems will go to idle
49  clbStateInit();
50 
51  shellSetPrompt("KM3NeT~>");
52 
53 
54  // register shell to scheduler.
55  int id;
56 
57  if (schdRegister(shellDo, false, &id))
58  {
59  schdRunPeriodic(id, 50);
60  } else errFatal();
61 
62  if (schdRegister(appPeriodicUpdate, false, &id))
63  {
64  schdRunPeriodic(id, 1000);
65  } else errFatal();
66 
67 }
68 
69 void resetBackoff()
70 {
71  _evtThrottleBackOff = 0;
72 }
73 
74 int _rateTimer = 0;
75 
76 void appPeriodicUpdate()
77 {
79  if (_evtThrottleTimeOut > _evtThrottleBackOff) {
80  _evtThrottleTimeOut = _evtThrottleBackOff;
81  }
82 
83 
84  if (_evtThrottleTimeOut == 0)
85  {
86  int i = netEventFailedCount();
87  if (i > 0) {
88  if (_evtThrottleBackOff < EVENT_THROTTLE_BACKOFF_MAX) _evtThrottleBackOff ++;
89  } else {
90  _evtThrottleBackOff = _evtThrottleBackOff >> 1;
91  }
92  _evtThrottleTimeOut = _evtThrottleBackOff;
93  // send the update to the shore
94 
95  // TODO remove redundant emits
97 
98  int rate = 1;
99  if (_rateTimer == 0) {
100  rate = 3;
101  } else if (_rateTimer % 10 == 0) {
102  rate = 2;
103  }
104 
105  if (_rateTimer >= 59) {
106  _rateTimer = 0;
107  } else {
108  _rateTimer += 1;
109  }
110 
111 
112  if (!evtClbEmitUpdateVars(rate)) {
113  errPrint(true);
114  }
116 
117 
118 
119 
120  if (errHas()) errPrint(true);
121  } else {
122  _evtThrottleTimeOut --;
123  }
124 
125 }
bool errHas()
Returns whether there is an error pending.
Definition: err.c:52
void _appInit()
Prototype to be implemented by higher-level function to start the application.
Definition: app.c:45
The shell is a ASCII command interpreter and effort has been made to simplify creation of additional ...
void schdRunPeriodic(int taskId, int interval)
Schedule a task to run periodically.
Definition: scheduler.c:212
GPIO Driver.
White Rabbit simple timer &#39;Ticks&#39; driver.
bool schdRegister(SchdTaskF task, bool priority, int *taskId)
Register a task with the scheduler.
Definition: scheduler.c:95
System start up and management.
bool evtClbEmitUpdate()
Emit an update.
Definition: msg_clb.c:243
White Rabbit Simple UART Driver.
Simple task scheduler for tasks.
void errFatal()
Prints the last error and halts the system.
Definition: err.c:97
CLB Remote message processor.
void clbUpdateSubsys()
Invoked the update method on each subsystem.
Definition: clbstate.c:282
PCA9548 I2C mux driver.
Manages the global system error.
void errPrint(bool clear)
Prints the last error.
Definition: err.c:79
void clbStateInit()
Initializes the state machine.
Definition: clbstate.c:327
uint32_t netEventFailedCount()
Returns the number of events failed to be transmitted since the the function was previously invoked...
Definition: network.c:667
The CLB stare module tracks is responsible for state management of the various sub-systems on the CLB...
void shellSetPrompt(const char *newPrompt)
Sets the prompt for the shell.
Definition: shell.c:85
bool evtClbEmitStateChange()
Indicates the CLB to emit a state change event to the slow control.
Definition: msg_clb.c:194
void shellDo()
Checks if there is any ASCII characters of the UART, and execute any commands issued.
Definition: shell.c:159
bool evtClbEmitUpdateVars(int rate)
Indicates it should emit an vars update.
Definition: msg_clb.c:218
Configures the board-specific peripherals, like I2C, SPI etc...
OpenCores I2C device driver.