KM3NeT CLB  2.0
KM3NeT CLB v2 Embedded Software
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
stmach.h
Go to the documentation of this file.
1 /*
2  * KM3NeT CLB v2 Firmware
3  * ----------------------
4  *
5  * Copyright 2013 KM3NeT Collaboration
6  *
7  * All Rights Reserved.
8  *
9  *
10  * File : stmach.h
11  * Created : 28 nov 2013
12  * Author : David Calvo
13  */
14 
15 #ifndef STMACH_H_
16 #define STMACH_H_
17 
18 #include <stdint.h>
19 #include "util/macro.h"
20 
21 /**
22  * @file
23  *
24  * @ingroup wbdrivers
25  *
26  * State Machine Driver.
27  */
28 
29 #include "lm32soc/dev_soc.h"
30 
31 #include <stdint.h>
32 #include <stddef.h>
33 #include <stdbool.h>
34 
35 #define STMACH_FIFO_AES_TSLICE BIT(31) //!< AES timeslice FIFO bit
36 #define STMACH_FIFO_AES_CH BIT(0) //!< AES channel fifo bit
37 
38 #define STMACH_FIFO_TDC_TSLICE BIT(31) //!< TDC timeslice FIFO bit
39 #define STMACH_FIFO_TDC_CH_MASK MASK(31) //!< TDC channel mask
40 #define STMACH_FIFO_TDC_CH_SHIFT 0 //!< TDC channel shift
41 
42 /**
43  * Initializes the state-machine with the DOM ID and the monitoring channel CPU data pointer.
44  * Once configured the content of the pointer will be transferred to the monitoring channel
45  * packed. The buffer must be word (32 bits) aligned, and the length will be rounded up by 4 bytes.
46  *
47  *
48  * @param domId The DOM ID as transmitted with each packet.
49  * @param monbuf Pointer to the data to transfer.
50  * @param monlen Length of the data
51  * @param recoverTaskId The task invoked when the state-machine buffers are overflowing.
52  *
53  * @retval true Succesfully initialized
54  * @retval false Initialization failure, check error module for issue.
55  */
56 bool stmachInit(uint32_t domId, void * monbuf, size_t monlen);
57 
58 /**
59  * Configures the stateMachine.
60  *
61  * @param packSize The maximum packet size in bytes (will be rounded to 16 bit words);
62  * @param duration The duration in microseconds. Must fit integer number of times in
63  * one second. E.g. 100ms, 250ms is okay, but 300ms is not.
64  */
65 bool stmachConfig(uint32_t packSize, uint32_t duration);
66 
67 
68 /**
69  * Configure Monitor Update.
70  *
71  * @param intervalMs Interval to which this must occur.
72  */
73 void stmachMUCfg(uint32_t intervalMs);
74 
75 
76 /**
77  * Sets the run-number.
78  *
79  * @param runNo The run number.
80  */
81 #ifdef KM3SIM
82 void stmachSetRunNo(uint32_t runNo);
83 #else
84 static inline void stmachSetRunNo(uint32_t runNo)
85 {
86  STMACH->RUN_NUM = runNo;
87 }
88 #endif
89 
90 #define STMACH_FLUSH_OP_START 0x1
91 #define STMACH_FLUSH_OP_END 0x2
92 #define STMACH_FLUSH_OP_TOGGLE (STMACH_FLUSH_OP_START | STMACH_FLUSH_OP_END)
93 
94 /**
95  * Flushes one or more state-machine channels.
96  *
97  * @param flush An OR-ed cobination of STMACH_CH_* values
98  * @param op Flush operation to execute.
99  */
100 void stmachFlush(uint32_t flush, uint8_t op);
101 
102 /**
103  * Enables one or more STMACH channels.
104  *
105  * @param enable An OR-ed cobination of STMACH_CH_* values
106  *
107  * @retval True Success
108  * @retval False On timeout
109  */
110 bool stmachEnable(uint32_t enable);
111 
112 /**
113  * Returns which state-machine channels are enabled.
114  *
115  * @param enable An OR-ed cobination of STMACH_CH_* values
116  */
117 uint32_t stmachEnabled();
118 
119 #ifdef KM3SIM
120 uint32_t stmachDomId ();
121 #else
122 static inline uint32_t stmachDomId ()
123 {
124  return STMACH->DOM_ID;
125 }
126 #endif
127 
128 /**
129  * Returns the number of timeslice IRQs since system start.
130  */
131 uint32_t stmachTSliceCount();
132 
133 
134 /**
135  * Waits for *at least* the provided number of time-slices.
136  *
137  * @param tslice The number of time slices to wait (minimum)
138  */
139 static inline void stmachWaitForTSlices(int tslice)
140 {
141  uint32_t overflow = stmachTSliceCount() + tslice + 1;
142  while (CGT(overflow, stmachTSliceCount(), 32)) {};
143 }
144 
145 /**
146  * Stub function invoked when it is time to update the monitor channel.
147  */
148 void _stmachUpdateMonitor();
149 
150 /**
151  * Invoked when an AES or TDC fifo overflow occurs.
152  *
153  * @param tdcFifo TDC Fifo mask, each bits corresponds to a specific FIFO.
154  * @param aesFifo AES Fifo mask
155  */
156 void _stmachOverflowBegin(uint32_t tdcFifo, uint32_t aesFifo);
157 
158 /**
159  * Invoked when an AES or TDC fifo should be enabled again.
160  *
161  * @param tdcFifo TDC Fifo mask, each bits corresponds to a specific FIFO.
162  * @param aesFifo AES Fifo mask
163  */
164 void _stmachOverflowEnd(uint32_t tdcFifo, uint32_t aesFifo);
165 
166 /**
167  * Debugging stuff, don't touch!
168  */
169 void stmachFakeFull(uint32_t mask);
170 
171 /**
172  * Debug feature: Get TDC and AES state machine Fifo commits
173  *
174  * @param tdc Pointer to uint16 to be set with FIFO commit of TDC
175  * @param aes Pointer to uint16 to be set with FIFO commit of AES
176  *
177  */
178 void stmachGetStCommit(uint16_t * tdc, uint16_t * aes);
179 
180 #endif /* HYDRO_H_ */
uint32_t stmachEnabled()
Returns which state-machine channels are enabled.
Definition: stmach.c:144
bool stmachInit(uint32_t domId, void *monbuf, size_t monlen)
Initializes the state-machine with the DOM ID and the monitoring channel CPU data pointer...
Definition: stmach.c:72
bool stmachEnable(uint32_t enable)
Enables one or more STMACH channels.
Definition: stmach.c:127
static void stmachWaitForTSlices(int tslice)
Waits for at least the provided number of time-slices.
Definition: stmach.h:139
void _stmachOverflowEnd(uint32_t tdcFifo, uint32_t aesFifo)
Invoked when an AES or TDC fifo should be enabled again.
Definition: sub_sys.c:130
void stmachFlush(uint32_t flush, uint8_t op)
Flushes one or more state-machine channels.
Definition: stmach.c:113
void stmachMUCfg(uint32_t intervalMs)
Configure Monitor Update.
Definition: stmach.c:90
#define STMACH
State Machine base pointer.
Definition: dev_soc.h:61
void _stmachUpdateMonitor()
Stub function invoked when it is time to update the monitor channel.
Definition: sub_sys.c:160
uint32_t stmachTSliceCount()
Returns the number of timeslice IRQs since system start.
Definition: stmach.c:180
static void stmachSetRunNo(uint32_t runNo)
Sets the run-number.
Definition: stmach.h:84
void stmachFakeFull(uint32_t mask)
Debugging stuff, don&#39;t touch!
Definition: stmach.c:155
bool stmachConfig(uint32_t packSize, uint32_t duration)
Configures the stateMachine.
Definition: stmach.c:98
void _stmachOverflowBegin(uint32_t tdcFifo, uint32_t aesFifo)
Invoked when an AES or TDC fifo overflow occurs.
Definition: sub_sys.c:85
This file assigns all device structures to memory mapped structures.
#define CGT(A, B, BITS)
Cyclic comparison function Greater Than.
Definition: macro.h:66
Provides common macros.
void stmachGetStCommit(uint16_t *tdc, uint16_t *aes)
Debug feature: Get TDC and AES state machine Fifo commits.
Definition: stmach.c:149