Skip to content

Commit 6c05438

Browse files
author
Bogdan Marinescu
committed
Interrupt chaining: added documentation, fixed a synchronization issue in CallChain.
1 parent d399e51 commit 6c05438

File tree

8 files changed

+389
-7
lines changed

8 files changed

+389
-7
lines changed

libraries/mbed/api/CallChain.h

Lines changed: 124 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2006-2013 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
116
#ifndef MBED_CALLCHAIN_H
217
#define MBED_CALLCHAIN_H
318

@@ -6,30 +21,134 @@
621

722
namespace mbed {
823

24+
/** Group one or more functions in an instance of a CallChain, then call them in
25+
* sequence using CallChain::call(). Used mostly by the interrupt chaining code,
26+
* but can be used for other purposes.
27+
*
28+
* Example:
29+
* @code
30+
* #include "mbed.h"
31+
*
32+
* CallChain chain;
33+
*
34+
* void first(void) {
35+
* printf("'first' function.\n");
36+
* }
37+
*
38+
* void second(void) {
39+
* printf("'second' function.\n");
40+
* }
41+
*
42+
* class Test {
43+
* public:
44+
* void f(void) {
45+
* printf("A::f (class member).\n");
46+
* }
47+
* };
48+
*
49+
* int main() {
50+
* Test test;
51+
*
52+
* chain.add(second);
53+
* chain.add_front(first);
54+
* chain.add(&test, &Test::f);
55+
* chain.call();
56+
* }
57+
* @endcode
58+
*/
59+
960
typedef FunctionPointer* pFunctionPointer_t;
1061

1162
class CallChain {
1263
public:
64+
/** Create an empty chain
65+
*
66+
* @param size (optional) Initial size of the chain
67+
*/
1368
CallChain(int size = 4);
14-
~CallChain();
69+
virtual ~CallChain();
1570

71+
/** Add a function at the end of the chain
72+
*
73+
* @param function A pointer to a void function
74+
*
75+
* @returns
76+
* The function object created for 'function'
77+
*/
1678
pFunctionPointer_t add(void (*function)(void));
79+
80+
/** Add a function at the end of the chain
81+
*
82+
* @param tptr pointer to the object to call the member function on
83+
* @param mptr pointer to the member function to be called
84+
*
85+
* @returns
86+
* The function object created for 'tptr' and 'mptr'
87+
*/
1788
template<typename T>
18-
pFunctionPointer_t add(T *object, void (T::*member)(void)) {
19-
return common_add(new FunctionPointer(object, member));
89+
pFunctionPointer_t add(T *tptr, void (T::*mptr)(void)) {
90+
return common_add(new FunctionPointer(tptr, mptr));
2091
}
2192

93+
/** Add a function at the beginning of the chain
94+
*
95+
* @param function A pointer to a void function
96+
*
97+
* @returns
98+
* The function object created for 'function'
99+
*/
22100
pFunctionPointer_t add_front(void (*function)(void));
101+
102+
/** Add a function at the beginning of the chain
103+
*
104+
* @param tptr pointer to the object to call the member function on
105+
* @param mptr pointer to the member function to be called
106+
*
107+
* @returns
108+
* The function object created for 'tptr' and 'mptr'
109+
*/
23110
template<typename T>
24-
pFunctionPointer_t add_front(T *object, void (T::*member)(void)) {
25-
return common_add_front(new FunctionPointer(object, member));
111+
pFunctionPointer_t add_front(T *tptr, void (T::*mptr)(void)) {
112+
return common_add_front(new FunctionPointer(tptr, mptr));
26113
}
27114

115+
/** Get the number of functions in the chain
116+
*/
28117
int size() const;
118+
119+
/** Get a function object from the chain
120+
*
121+
* @param i function object index
122+
*
123+
* @returns
124+
* The function object at position 'i' in the chain
125+
*/
29126
pFunctionPointer_t get(int i) const;
127+
128+
/** Look for a function object in the call chain
129+
*
130+
* @param f the function object to search
131+
*
132+
* @returns
133+
* The index of the function object if found, -1 otherwise.
134+
*/
30135
int find(pFunctionPointer_t f) const;
136+
137+
/** Clear the call chain (remove all functions in the chain).
138+
*/
31139
void clear();
140+
141+
/** Remove a function object from the chain
142+
*
143+
* @arg f the function object to remove
144+
*
145+
* @returns
146+
* true if the function object was found and removed, false otherwise.
147+
*/
32148
bool remove(pFunctionPointer_t f);
149+
150+
/** Call all the functions in the chain in sequence
151+
*/
33152
void call();
34153

35154
#ifdef MBED_OPERATORS

libraries/mbed/api/InterruptIn.h

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,25 @@ class InterruptIn {
7878
* The function object created for 'fptr'
7979
*/
8080
pFunctionPointer_t rise(void (*fptr)(void));
81+
82+
/** Add a function to be called when a rising edge occurs at the end of the call chain
83+
*
84+
* @param fptr the function to add
85+
*
86+
* @returns
87+
* The function object created for 'fptr'
88+
*/
8189
pFunctionPointer_t rise_add(void (*fptr)(void)) {
8290
return rise_add_common(fptr);
8391
}
92+
93+
/** Add a function to be called when a rising edge occurs at the beginning of the call chain
94+
*
95+
* @param fptr the function to add
96+
*
97+
* @returns
98+
* The function object created for 'fptr'
99+
*/
84100
pFunctionPointer_t rise_add_front(void (*fptr)(void)) {
85101
return rise_add_common(fptr, true);
86102
}
@@ -101,26 +117,68 @@ class InterruptIn {
101117
return pf;
102118
}
103119

120+
/** Add a function to be called when a rising edge occurs at the end of the call chain
121+
*
122+
* @param tptr pointer to the object to call the member function on
123+
* @param mptr pointer to the member function to be called
124+
*
125+
* @returns
126+
* The function object created for 'tptr' and 'mptr'
127+
*/
104128
template<typename T>
105129
pFunctionPointer_t rise_add(T* tptr, void (T::*mptr)(void)) {
106130
return rise_add_common(tptr, mptr);
107131
}
108132

133+
/** Add a function to be called when a rising edge occurs at the beginning of the call chain
134+
*
135+
* @param tptr pointer to the object to call the member function on
136+
* @param mptr pointer to the member function to be called
137+
*
138+
* @returns
139+
* The function object created for 'tptr' and 'mptr'
140+
*/
109141
template<typename T>
110142
pFunctionPointer_t rise_add_front(T* tptr, void (T::*mptr)(void)) {
111143
return rise_add_common(tptr, mptr, true);
112144
}
113145

146+
/** Remove a function from the list of functions to be called when a rising edge occurs
147+
*
148+
* @param pf the function object to remove
149+
*
150+
* @returns
151+
* true if the function was found and removed, false otherwise
152+
*/
114153
bool rise_remove(pFunctionPointer_t pf);
115154

116155
/** Attach a function to call when a falling edge occurs on the input
117156
*
118157
* @param fptr A pointer to a void function, or 0 to set as none
158+
*
159+
* @returns
160+
* The function object created for 'fptr'
119161
*/
120162
pFunctionPointer_t fall(void (*fptr)(void));
163+
164+
/** Add a function to be called when a falling edge occurs at the end of the call chain
165+
*
166+
* @param fptr the function to add
167+
*
168+
* @returns
169+
* The function object created for 'fptr'
170+
*/
121171
pFunctionPointer_t fall_add(void (*fptr)(void)) {
122172
return fall_add_common(fptr);
123173
}
174+
175+
/** Add a function to be called when a falling edge occurs at the beginning of the call chain
176+
*
177+
* @param fptr the function to add
178+
*
179+
* @returns
180+
* The function object created for 'fptr'
181+
*/
124182
pFunctionPointer_t fall_add_front(void (*fptr)(void)) {
125183
return fall_add_common(fptr, true);
126184
}
@@ -129,6 +187,9 @@ class InterruptIn {
129187
*
130188
* @param tptr pointer to the object to call the member function on
131189
* @param mptr pointer to the member function to be called
190+
*
191+
* @returns
192+
* The function object created for 'tptr' and 'mptr'
132193
*/
133194
template<typename T>
134195
pFunctionPointer_t fall(T* tptr, void (T::*mptr)(void)) {
@@ -137,15 +198,40 @@ class InterruptIn {
137198
gpio_irq_set(&gpio_irq, IRQ_FALL, 1);
138199
return pf;
139200
}
201+
202+
/** Add a function to be called when a falling edge occurs at the end of the call chain
203+
*
204+
* @param tptr pointer to the object to call the member function on
205+
* @param mptr pointer to the member function to be called
206+
*
207+
* @returns
208+
* The function object created for 'tptr' and 'mptr'
209+
*/
140210
template<typename T>
141211
pFunctionPointer_t fall_add(T* tptr, void (T::*mptr)(void)) {
142212
return fall_add_common(tptr, mptr);
143213
}
214+
215+
/** Add a function to be called when a falling edge occurs at the beginning of the call chain
216+
*
217+
* @param tptr pointer to the object to call the member function on
218+
* @param mptr pointer to the member function to be called
219+
*
220+
* @returns
221+
* The function object created for 'tptr' and 'mptr'
222+
*/
144223
template<typename T>
145224
pFunctionPointer_t fall_add_front(T* tptr, void (T::*mptr)(void)) {
146225
return fall_add_common(tptr, mptr, true);
147226
}
148227

228+
/** Remove a function from the list of functions to be called when a falling edge occurs
229+
*
230+
* @param pf the function object to remove
231+
*
232+
* @returns
233+
* true if the function was found and removed, false otherwise
234+
*/
149235
bool fall_remove(pFunctionPointer_t pf);
150236

151237
/** Set the input pin mode

0 commit comments

Comments
 (0)