Skip to content

Commit 2404dc0

Browse files
committed
Added new serial class, remove interrupt chaining
The new RawSerial class is a simple wrapper over the serial HAL that can be safely used from an interrupt handler. Interrupt chaining code was removed from InterruptIn, Serial and Ticker because it caused lots of issues with the RTOS. Interrupt chaining is still possible using the InterruptManager class.
1 parent 00c641c commit 2404dc0

File tree

16 files changed

+358
-722
lines changed

16 files changed

+358
-722
lines changed

libraries/mbed/api/InterruptIn.h

Lines changed: 8 additions & 156 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@
2222

2323
#include "gpio_api.h"
2424
#include "gpio_irq_api.h"
25-
2625
#include "FunctionPointer.h"
27-
#include "CallChain.h"
2826

2927
namespace mbed {
3028

@@ -73,167 +71,37 @@ class InterruptIn {
7371
/** Attach a function to call when a rising edge occurs on the input
7472
*
7573
* @param fptr A pointer to a void function, or 0 to set as none
76-
*
77-
* @returns
78-
* The function object created for 'fptr'
79-
*/
80-
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'
8874
*/
89-
pFunctionPointer_t rise_add(void (*fptr)(void)) {
90-
return rise_add_common(fptr);
91-
}
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-
*/
100-
pFunctionPointer_t rise_add_front(void (*fptr)(void)) {
101-
return rise_add_common(fptr, true);
102-
}
75+
void rise(void (*fptr)(void));
10376

10477
/** Attach a member function to call when a rising edge occurs on the input
10578
*
10679
* @param tptr pointer to the object to call the member function on
10780
* @param mptr pointer to the member function to be called
108-
*
109-
* @returns
110-
* The function object created for 'tptr' and 'mptr'
11181
*/
11282
template<typename T>
113-
pFunctionPointer_t rise(T* tptr, void (T::*mptr)(void)) {
114-
_rise.clear();
115-
pFunctionPointer_t pf = _rise.add(tptr, mptr);
83+
void rise(T* tptr, void (T::*mptr)(void)) {
84+
_rise.attach(tptr, mptr);
11685
gpio_irq_set(&gpio_irq, IRQ_RISE, 1);
117-
return pf;
11886
}
11987

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-
*/
128-
template<typename T>
129-
pFunctionPointer_t rise_add(T* tptr, void (T::*mptr)(void)) {
130-
return rise_add_common(tptr, mptr);
131-
}
132-
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-
*/
141-
template<typename T>
142-
pFunctionPointer_t rise_add_front(T* tptr, void (T::*mptr)(void)) {
143-
return rise_add_common(tptr, mptr, true);
144-
}
145-
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-
*/
153-
bool rise_remove(pFunctionPointer_t pf);
154-
15588
/** Attach a function to call when a falling edge occurs on the input
15689
*
15790
* @param fptr A pointer to a void function, or 0 to set as none
158-
*
159-
* @returns
160-
* The function object created for 'fptr'
16191
*/
162-
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-
*/
171-
pFunctionPointer_t fall_add(void (*fptr)(void)) {
172-
return fall_add_common(fptr);
173-
}
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-
*/
182-
pFunctionPointer_t fall_add_front(void (*fptr)(void)) {
183-
return fall_add_common(fptr, true);
184-
}
92+
void fall(void (*fptr)(void));
18593

18694
/** Attach a member function to call when a falling edge occurs on the input
18795
*
18896
* @param tptr pointer to the object to call the member function on
18997
* @param mptr pointer to the member function to be called
190-
*
191-
* @returns
192-
* The function object created for 'tptr' and 'mptr'
19398
*/
19499
template<typename T>
195-
pFunctionPointer_t fall(T* tptr, void (T::*mptr)(void)) {
196-
_fall.clear();
197-
pFunctionPointer_t pf = _fall.add(tptr, mptr);
100+
void fall(T* tptr, void (T::*mptr)(void)) {
101+
_fall.attach(tptr, mptr);
198102
gpio_irq_set(&gpio_irq, IRQ_FALL, 1);
199-
return pf;
200-
}
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-
*/
210-
template<typename T>
211-
pFunctionPointer_t fall_add(T* tptr, void (T::*mptr)(void)) {
212-
return fall_add_common(tptr, mptr);
213-
}
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-
*/
223-
template<typename T>
224-
pFunctionPointer_t fall_add_front(T* tptr, void (T::*mptr)(void)) {
225-
return fall_add_common(tptr, mptr, true);
226103
}
227104

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-
*/
235-
bool fall_remove(pFunctionPointer_t pf);
236-
237105
/** Set the input pin mode
238106
*
239107
* @param mode PullUp, PullDown, PullNone
@@ -251,27 +119,11 @@ class InterruptIn {
251119
static void _irq_handler(uint32_t id, gpio_irq_event event);
252120

253121
protected:
254-
pFunctionPointer_t rise_add_common(void (*fptr)(void), bool front=false);
255-
pFunctionPointer_t fall_add_common(void (*fptr)(void), bool front=false);
256-
257-
template<typename T>
258-
pFunctionPointer_t rise_add_common(T* tptr, void (T::*mptr)(void), bool front=false) {
259-
pFunctionPointer_t pf = front ? _rise.add_front(tptr, mptr) : _rise.add(tptr, mptr);
260-
gpio_irq_set(&gpio_irq, IRQ_RISE, 1);
261-
return pf;
262-
}
263-
template<typename T>
264-
pFunctionPointer_t fall_add_common(T* tptr, void (T::*mptr)(void), bool front=false) {
265-
pFunctionPointer_t pf = front ? _fall.add_front(tptr, mptr) : _fall.add(tptr, mptr);
266-
gpio_irq_set(&gpio_irq, IRQ_FALL, 1);
267-
return pf;
268-
}
269-
270122
gpio_t gpio;
271123
gpio_irq_t gpio_irq;
272124

273-
CallChain _rise;
274-
CallChain _fall;
125+
FunctionPointer _rise;
126+
FunctionPointer _fall;
275127
};
276128

277129
} // namespace mbed

libraries/mbed/api/RawSerial.h

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
*/
16+
#ifndef MBED_RAW_SERIAL_H
17+
#define MBED_RAW_SERIAL_H
18+
19+
#include "platform.h"
20+
21+
#if DEVICE_SERIAL
22+
23+
#include "SerialBase.h"
24+
#include "serial_api.h"
25+
26+
namespace mbed {
27+
28+
/** A serial port (UART) for communication with other serial devices
29+
* This is a variation of the Serial class that doesn't use streams,
30+
* thus making it safe to use in interrupt handlers with the RTOS.
31+
*
32+
* Can be used for Full Duplex communication, or Simplex by specifying
33+
* one pin as NC (Not Connected)
34+
*
35+
* Example:
36+
* @code
37+
* // Send a char to the PC
38+
*
39+
* #include "mbed.h"
40+
*
41+
* RawSerial pc(USBTX, USBRX);
42+
*
43+
* int main() {
44+
* pc.putc('A');
45+
* }
46+
* @endcode
47+
*/
48+
class RawSerial: public SerialBase {
49+
50+
public:
51+
/** Create a RawSerial port, connected to the specified transmit and receive pins
52+
*
53+
* @param tx Transmit pin
54+
* @param rx Receive pin
55+
*
56+
* @note
57+
* Either tx or rx may be specified as NC if unused
58+
*/
59+
RawSerial(PinName tx, PinName rx);
60+
61+
/** Write a char to the serial port
62+
*
63+
* @param c The char to write
64+
*
65+
* @returns The written char or -1 if an error occured
66+
*/
67+
int putc(int c);
68+
69+
/** Read a char from the serial port
70+
*
71+
* @returns The char read from the serial port
72+
*/
73+
int getc();
74+
};
75+
76+
} // namespace mbed
77+
78+
#endif
79+
80+
#endif

0 commit comments

Comments
 (0)