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.
pull/92/head^2 mbed_lib_rev68
Bogdan Marinescu 2013-10-23 15:49:07 +03:00
parent 00c641cf26
commit 2404dc0092
16 changed files with 358 additions and 722 deletions

View File

@ -22,9 +22,7 @@
#include "gpio_api.h"
#include "gpio_irq_api.h"
#include "FunctionPointer.h"
#include "CallChain.h"
namespace mbed {
@ -73,167 +71,37 @@ public:
/** Attach a function to call when a rising edge occurs on the input
*
* @param fptr A pointer to a void function, or 0 to set as none
*
* @returns
* The function object created for 'fptr'
*/
pFunctionPointer_t rise(void (*fptr)(void));
/** Add a function to be called when a rising edge occurs at the end of the call chain
*
* @param fptr the function to add
*
* @returns
* The function object created for 'fptr'
*/
pFunctionPointer_t rise_add(void (*fptr)(void)) {
return rise_add_common(fptr);
}
/** Add a function to be called when a rising edge occurs at the beginning of the call chain
*
* @param fptr the function to add
*
* @returns
* The function object created for 'fptr'
*/
pFunctionPointer_t rise_add_front(void (*fptr)(void)) {
return rise_add_common(fptr, true);
}
void rise(void (*fptr)(void));
/** Attach a member function to call when a rising edge occurs on the input
*
* @param tptr pointer to the object to call the member function on
* @param mptr pointer to the member function to be called
*
* @returns
* The function object created for 'tptr' and 'mptr'
*/
template<typename T>
pFunctionPointer_t rise(T* tptr, void (T::*mptr)(void)) {
_rise.clear();
pFunctionPointer_t pf = _rise.add(tptr, mptr);
void rise(T* tptr, void (T::*mptr)(void)) {
_rise.attach(tptr, mptr);
gpio_irq_set(&gpio_irq, IRQ_RISE, 1);
return pf;
}
/** Add a function to be called when a rising edge occurs at the end of the call chain
*
* @param tptr pointer to the object to call the member function on
* @param mptr pointer to the member function to be called
*
* @returns
* The function object created for 'tptr' and 'mptr'
*/
template<typename T>
pFunctionPointer_t rise_add(T* tptr, void (T::*mptr)(void)) {
return rise_add_common(tptr, mptr);
}
/** Add a function to be called when a rising edge occurs at the beginning of the call chain
*
* @param tptr pointer to the object to call the member function on
* @param mptr pointer to the member function to be called
*
* @returns
* The function object created for 'tptr' and 'mptr'
*/
template<typename T>
pFunctionPointer_t rise_add_front(T* tptr, void (T::*mptr)(void)) {
return rise_add_common(tptr, mptr, true);
}
/** Remove a function from the list of functions to be called when a rising edge occurs
*
* @param pf the function object to remove
*
* @returns
* true if the function was found and removed, false otherwise
*/
bool rise_remove(pFunctionPointer_t pf);
/** Attach a function to call when a falling edge occurs on the input
*
* @param fptr A pointer to a void function, or 0 to set as none
*
* @returns
* The function object created for 'fptr'
*/
pFunctionPointer_t fall(void (*fptr)(void));
/** Add a function to be called when a falling edge occurs at the end of the call chain
*
* @param fptr the function to add
*
* @returns
* The function object created for 'fptr'
*/
pFunctionPointer_t fall_add(void (*fptr)(void)) {
return fall_add_common(fptr);
}
/** Add a function to be called when a falling edge occurs at the beginning of the call chain
*
* @param fptr the function to add
*
* @returns
* The function object created for 'fptr'
*/
pFunctionPointer_t fall_add_front(void (*fptr)(void)) {
return fall_add_common(fptr, true);
}
void fall(void (*fptr)(void));
/** Attach a member function to call when a falling edge occurs on the input
*
* @param tptr pointer to the object to call the member function on
* @param mptr pointer to the member function to be called
*
* @returns
* The function object created for 'tptr' and 'mptr'
*/
template<typename T>
pFunctionPointer_t fall(T* tptr, void (T::*mptr)(void)) {
_fall.clear();
pFunctionPointer_t pf = _fall.add(tptr, mptr);
void fall(T* tptr, void (T::*mptr)(void)) {
_fall.attach(tptr, mptr);
gpio_irq_set(&gpio_irq, IRQ_FALL, 1);
return pf;
}
/** Add a function to be called when a falling edge occurs at the end of the call chain
*
* @param tptr pointer to the object to call the member function on
* @param mptr pointer to the member function to be called
*
* @returns
* The function object created for 'tptr' and 'mptr'
*/
template<typename T>
pFunctionPointer_t fall_add(T* tptr, void (T::*mptr)(void)) {
return fall_add_common(tptr, mptr);
}
/** Add a function to be called when a falling edge occurs at the beginning of the call chain
*
* @param tptr pointer to the object to call the member function on
* @param mptr pointer to the member function to be called
*
* @returns
* The function object created for 'tptr' and 'mptr'
*/
template<typename T>
pFunctionPointer_t fall_add_front(T* tptr, void (T::*mptr)(void)) {
return fall_add_common(tptr, mptr, true);
}
/** Remove a function from the list of functions to be called when a falling edge occurs
*
* @param pf the function object to remove
*
* @returns
* true if the function was found and removed, false otherwise
*/
bool fall_remove(pFunctionPointer_t pf);
/** Set the input pin mode
*
* @param mode PullUp, PullDown, PullNone
@ -251,27 +119,11 @@ public:
static void _irq_handler(uint32_t id, gpio_irq_event event);
protected:
pFunctionPointer_t rise_add_common(void (*fptr)(void), bool front=false);
pFunctionPointer_t fall_add_common(void (*fptr)(void), bool front=false);
template<typename T>
pFunctionPointer_t rise_add_common(T* tptr, void (T::*mptr)(void), bool front=false) {
pFunctionPointer_t pf = front ? _rise.add_front(tptr, mptr) : _rise.add(tptr, mptr);
gpio_irq_set(&gpio_irq, IRQ_RISE, 1);
return pf;
}
template<typename T>
pFunctionPointer_t fall_add_common(T* tptr, void (T::*mptr)(void), bool front=false) {
pFunctionPointer_t pf = front ? _fall.add_front(tptr, mptr) : _fall.add(tptr, mptr);
gpio_irq_set(&gpio_irq, IRQ_FALL, 1);
return pf;
}
gpio_t gpio;
gpio_irq_t gpio_irq;
CallChain _rise;
CallChain _fall;
FunctionPointer _rise;
FunctionPointer _fall;
};
} // namespace mbed

View File

@ -0,0 +1,80 @@
/* mbed Microcontroller Library
* Copyright (c) 2006-2013 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MBED_RAW_SERIAL_H
#define MBED_RAW_SERIAL_H
#include "platform.h"
#if DEVICE_SERIAL
#include "SerialBase.h"
#include "serial_api.h"
namespace mbed {
/** A serial port (UART) for communication with other serial devices
* This is a variation of the Serial class that doesn't use streams,
* thus making it safe to use in interrupt handlers with the RTOS.
*
* Can be used for Full Duplex communication, or Simplex by specifying
* one pin as NC (Not Connected)
*
* Example:
* @code
* // Send a char to the PC
*
* #include "mbed.h"
*
* RawSerial pc(USBTX, USBRX);
*
* int main() {
* pc.putc('A');
* }
* @endcode
*/
class RawSerial: public SerialBase {
public:
/** Create a RawSerial port, connected to the specified transmit and receive pins
*
* @param tx Transmit pin
* @param rx Receive pin
*
* @note
* Either tx or rx may be specified as NC if unused
*/
RawSerial(PinName tx, PinName rx);
/** Write a char to the serial port
*
* @param c The char to write
*
* @returns The written char or -1 if an error occured
*/
int putc(int c);
/** Read a char from the serial port
*
* @returns The char read from the serial port
*/
int getc();
};
} // namespace mbed
#endif
#endif

View File

@ -21,9 +21,8 @@
#if DEVICE_SERIAL
#include "Stream.h"
#include "FunctionPointer.h"
#include "SerialBase.h"
#include "serial_api.h"
#include "CallChain.h"
namespace mbed {
@ -45,7 +44,7 @@ namespace mbed {
* }
* @endcode
*/
class Serial : public Stream {
class Serial : public SerialBase, public Stream {
public:
/** Create a Serial port, connected to the specified transmit and receive pins
@ -58,167 +57,9 @@ public:
*/
Serial(PinName tx, PinName rx, const char *name=NULL);
/** Set the baud rate of the serial port
*
* @param baudrate The baudrate of the serial port (default = 9600).
*/
void baud(int baudrate);
enum Parity {
None = 0,
Odd,
Even,
Forced1,
Forced0
};
enum IrqType {
RxIrq = 0,
TxIrq
};
/** Set the transmission format used by the Serial port
*
* @param bits The number of bits in a word (5-8; default = 8)
* @param parity The parity used (Serial::None, Serial::Odd, Serial::Even, Serial::Forced1, Serial::Forced0; default = Serial::None)
* @param stop The number of stop bits (1 or 2; default = 1)
*/
void format(int bits=8, Parity parity=Serial::None, int stop_bits=1);
/** Determine if there is a character available to read
*
* @returns
* 1 if there is a character available to read,
* 0 otherwise
*/
int readable();
/** Determine if there is space available to write a character
*
* @returns
* 1 if there is space to write a character,
* 0 otherwise
*/
int writeable();
/** Attach a function to call whenever a serial interrupt is generated
*
* @param fptr A pointer to a void function, or 0 to set as none
* @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
*
* @returns
* The function object created for 'fptr'
*/
pFunctionPointer_t attach(void (*fptr)(void), IrqType type=RxIrq);
/** Add a function to be called when a serial interrupt is generated at the end of the call chain
*
* @param fptr the function to add
* @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
*
* @returns
* The function object created for 'fptr'
*/
pFunctionPointer_t add_handler(void (*fptr)(void), IrqType type=RxIrq) {
return add_handler_helper(fptr, type);
}
/** Add a function to be called when a serial interrupt is generated at the beginning of the call chain
*
* @param fptr the function to add
* @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
*
* @returns
* The function object created for 'fptr'
*/
pFunctionPointer_t add_handler_front(void (*fptr)(void), IrqType type=RxIrq) {
return add_handler_helper(fptr, type, true);
}
/** Attach a member function to call whenever a serial interrupt is generated
*
* @param tptr pointer to the object to call the member function on
* @param mptr pointer to the member function to be called
* @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
*
* @param
* The function object created for 'tptr' and 'mptr'
*/
template<typename T>
pFunctionPointer_t attach(T* tptr, void (T::*mptr)(void), IrqType type=RxIrq) {
if((mptr != NULL) && (tptr != NULL)) {
_irq[type].clear();
pFunctionPointer_t pf = _irq[type].add(tptr, mptr);
serial_irq_set(&_serial, (SerialIrq)type, 1);
return pf;
}
else
return NULL;
}
/** Add a function to be called when a serial interrupt is generated at the end of the call chain
*
* @param tptr pointer to the object to call the member function on
* @param mptr pointer to the member function to be called
* @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
*
* @returns
* The function object created for 'fptr'
*/
template<typename T>
pFunctionPointer_t add_handler(T* tptr, void (T::*mptr)(void), IrqType type=RxIrq) {
return add_handler_helper(tptr, mptr, type);
}
/** Add a function to be called when a serial interrupt is generated at the beginning of the call chain
*
* @param tptr pointer to the object to call the member function on
* @param mptr pointer to the member function to be called
* @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
*
* @returns
* The function object created for 'fptr'
*/
template<typename T>
pFunctionPointer_t add_handler_front(T* tptr, void (T::*mptr)(void), IrqType type=RxIrq) {
return add_handler_helper(tptr, mptr, type, true);
}
/** Remove a function from the list of functions to be called when a serial interrupt is generated
*
* @param pf the function object to remove
* @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
*
* @returns
* true if the function was found and removed, false otherwise
*/
bool remove_handler(pFunctionPointer_t pf, IrqType type=RxIrq);
/** Generate a break condition on the serial line
*/
void send_break();
static void _irq_handler(uint32_t id, SerialIrq irq_type);
protected:
virtual int _getc();
virtual int _putc(int c);
pFunctionPointer_t add_handler_helper(void (*function)(void), IrqType type, bool front=false);
template<typename T>
pFunctionPointer_t add_handler_helper(T* tptr, void (T::*mptr)(void), IrqType type, bool front=false) {
if ((mptr != NULL) && (tptr != NULL)) {
pFunctionPointer_t pf = front ? _irq[type].add_front(tptr, mptr) : _irq[type].add(tptr, mptr);
serial_irq_set(&_serial, (SerialIrq)type, 1);
return pf;
}
else
return NULL;
}
serial_t _serial;
CallChain _irq[2];
int _baud;
virtual int _putc(int c);
};
} // namespace mbed

View File

@ -0,0 +1,120 @@
/* mbed Microcontroller Library
* Copyright (c) 2006-2013 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MBED_SERIALBASE_H
#define MBED_SERIALBASE_H
#include "platform.h"
#if DEVICE_SERIAL
#include "Stream.h"
#include "FunctionPointer.h"
#include "serial_api.h"
namespace mbed {
/** A base class for serial port implementations
* Can't be instantiated directly (use Serial or RawSerial)
*/
class SerialBase {
public:
/** Set the baud rate of the serial port
*
* @param baudrate The baudrate of the serial port (default = 9600).
*/
void baud(int baudrate);
enum Parity {
None = 0,
Odd,
Even,
Forced1,
Forced0
};
enum IrqType {
RxIrq = 0,
TxIrq
};
/** Set the transmission format used by the serial port
*
* @param bits The number of bits in a word (5-8; default = 8)
* @param parity The parity used (SerialBase::None, SerialBase::Odd, SerialBase::Even, SerialBase::Forced1, SerialBase::Forced0; default = SerialBase::None)
* @param stop The number of stop bits (1 or 2; default = 1)
*/
void format(int bits=8, Parity parity=SerialBase::None, int stop_bits=1);
/** Determine if there is a character available to read
*
* @returns
* 1 if there is a character available to read,
* 0 otherwise
*/
int readable();
/** Determine if there is space available to write a character
*
* @returns
* 1 if there is space to write a character,
* 0 otherwise
*/
int writeable();
/** Attach a function to call whenever a serial interrupt is generated
*
* @param fptr A pointer to a void function, or 0 to set as none
* @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
*/
void attach(void (*fptr)(void), IrqType type=RxIrq);
/** Attach a member function to call whenever a serial interrupt is generated
*
* @param tptr pointer to the object to call the member function on
* @param mptr pointer to the member function to be called
* @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
*/
template<typename T>
void attach(T* tptr, void (T::*mptr)(void), IrqType type=RxIrq) {
if((mptr != NULL) && (tptr != NULL)) {
_irq[type].attach(tptr, mptr);
serial_irq_set(&_serial, (SerialIrq)type, 1);
}
}
/** Generate a break condition on the serial line
*/
void send_break();
static void _irq_handler(uint32_t id, SerialIrq irq_type);
protected:
SerialBase(PinName tx, PinName rx);
int _base_getc();
int _base_putc(int c);
serial_t _serial;
FunctionPointer _irq[2];
int _baud;
};
} // namespace mbed
#endif
#endif

View File

@ -18,7 +18,6 @@
#include "TimerEvent.h"
#include "FunctionPointer.h"
#include "CallChain.h"
namespace mbed {
@ -63,34 +62,9 @@ public:
*
* @param fptr pointer to the function to be called
* @param t the time between calls in seconds
*
* @returns
* The function object created for 'fptr'
*/
pFunctionPointer_t attach(void (*fptr)(void), float t) {
return attach_us(fptr, t * 1000000.0f);
}
/** Add a function to be called by the Ticker at the end of the call chain
*
* @param fptr the function to add
*
* @returns
* The function object created for 'fptr'
*/
pFunctionPointer_t add_function(void (*fptr)(void)) {
return add_function_helper(fptr);
}
/** Add a function to be called by the Ticker at the beginning of the call chain
*
* @param fptr the function to add
*
* @returns
* The function object created for 'fptr'
*/
pFunctionPointer_t add_function_front(void (*fptr)(void)) {
return add_function_helper(fptr, true);
void attach(void (*fptr)(void), float t) {
attach_us(fptr, t * 1000000.0f);
}
/** Attach a member function to be called by the Ticker, specifiying the interval in seconds
@ -98,54 +72,20 @@ public:
* @param tptr pointer to the object to call the member function on
* @param mptr pointer to the member function to be called
* @param t the time between calls in seconds
*
* @returns
* The function object created for 'tptr' and 'mptr'
*/
template<typename T>
pFunctionPointer_t attach(T* tptr, void (T::*mptr)(void), float t) {
return attach_us(tptr, mptr, t * 1000000.0f);
}
/** Add a function to be called by the Ticker at the end of the call chain
*
* @param tptr pointer to the object to call the member function on
* @param mptr pointer to the member function to be called
*
* @returns
* The function object created for 'tptr' and 'mptr'
*/
template<typename T>
pFunctionPointer_t add_function(T* tptr, void (T::*mptr)(void)) {
return add_function_helper(tptr, mptr);
}
/** Add a function to be called by the Ticker at the beginning of the call chain
*
* @param tptr pointer to the object to call the member function on
* @param mptr pointer to the member function to be called
*
* @returns
* The function object created for 'tptr' and 'mptr'
*/
template<typename T>
pFunctionPointer_t add_function_front(T* tptr, void (T::*mptr)(void)) {
return add_function_helper(tptr, mptr, true);
void attach(T* tptr, void (T::*mptr)(void), float t) {
attach_us(tptr, mptr, t * 1000000.0f);
}
/** Attach a function to be called by the Ticker, specifiying the interval in micro-seconds
*
* @param fptr pointer to the function to be called
* @param t the time between calls in micro-seconds
*
* @returns
* The function object created for 'fptr'
*/
pFunctionPointer_t attach_us(void (*fptr)(void), unsigned int t) {
_chain.clear();
pFunctionPointer_t pf = _chain.add(fptr);
void attach_us(void (*fptr)(void), unsigned int t) {
_function.attach(fptr);
setup(t);
return pf;
}
/** Attach a member function to be called by the Ticker, specifiying the interval in micro-seconds
@ -153,50 +93,23 @@ public:
* @param tptr pointer to the object to call the member function on
* @param mptr pointer to the member function to be called
* @param t the time between calls in micro-seconds
*
* @returns
* The function object created for 'tptr' and 'mptr'
*/
template<typename T>
pFunctionPointer_t attach_us(T* tptr, void (T::*mptr)(void), unsigned int t) {
_chain.clear();
pFunctionPointer_t pf = _chain.add(tptr, mptr);
void attach_us(T* tptr, void (T::*mptr)(void), unsigned int t) {
_function.attach(tptr, mptr);
setup(t);
return pf;
}
/** Detach the function
*/
void detach();
/** Remove a function from the Ticker's call chain
*
* @param pf the function object to remove
*
* @returns
* true if the function was found and removed, false otherwise
*/
bool remove_function(pFunctionPointer_t pf) {
bool res = _chain.remove(pf);
if (res && _chain.size() == 0)
detach();
return res;
}
protected:
void setup(unsigned int t);
pFunctionPointer_t add_function_helper(void (*fptr)(void), bool front=false);
virtual void handler();
template<typename T>
pFunctionPointer_t add_function_helper(T* tptr, void (T::*mptr)(void), bool front=false) {
if (_chain.size() == 0)
return NULL;
return front ? _chain.add_front(tptr, mptr) : _chain.add(tptr, mptr);
}
unsigned int _delay;
CallChain _chain;
FunctionPointer _function;
};
} // namespace mbed

View File

@ -38,13 +38,11 @@ int CallChain::find(pFunctionPointer_t f) const {
}
void CallChain::clear() {
__disable_irq();
for(int i = 0; i < _elements; i ++) {
delete _chain[i];
_chain[i] = NULL;
}
_elements = 0;
__enable_irq();
}
bool CallChain::remove(pFunctionPointer_t f) {
@ -52,12 +50,10 @@ bool CallChain::remove(pFunctionPointer_t f) {
if ((i = find(f)) == -1)
return false;
__disable_irq();
if (i != _elements - 1)
memmove(_chain + i, _chain + i + 1, (_elements - i - 1) * sizeof(pFunctionPointer_t));
delete f;
_elements --;
__enable_irq();
return true;
}
@ -69,13 +65,11 @@ void CallChain::call() {
void CallChain::_check_size() {
if (_elements < _size)
return;
__disable_irq();
_size = (_size < 4) ? 4 : _size + 4;
pFunctionPointer_t* new_chain = new pFunctionPointer_t[_size]();
memcpy(new_chain, _chain, _elements * sizeof(pFunctionPointer_t));
delete _chain;
_chain = new_chain;
__enable_irq();
}
pFunctionPointer_t CallChain::common_add(pFunctionPointer_t pf) {
@ -87,11 +81,9 @@ pFunctionPointer_t CallChain::common_add(pFunctionPointer_t pf) {
pFunctionPointer_t CallChain::common_add_front(pFunctionPointer_t pf) {
_check_size();
__disable_irq();
memmove(_chain + 1, _chain, _elements * sizeof(pFunctionPointer_t));
_chain[0] = pf;
_elements ++;
__enable_irq();
return pf;
}

View File

@ -36,58 +36,22 @@ void InterruptIn::mode(PinMode pull) {
gpio_mode(&gpio, pull);
}
pFunctionPointer_t InterruptIn::rise(void (*fptr)(void)) {
pFunctionPointer_t pf = NULL;
_rise.clear();
void InterruptIn::rise(void (*fptr)(void)) {
if (fptr) {
pf = _rise.add(fptr);
_rise.attach(fptr);
gpio_irq_set(&gpio_irq, IRQ_RISE, 1);
} else {
gpio_irq_set(&gpio_irq, IRQ_RISE, 0);
}
return pf;
}
pFunctionPointer_t InterruptIn::rise_add_common(void (*fptr)(void), bool front) {
if (NULL == fptr)
return NULL;
pFunctionPointer_t pf = front ? _rise.add_front(fptr) : _rise.add(fptr);
gpio_irq_set(&gpio_irq, IRQ_RISE, 1);
return pf;
}
bool InterruptIn::rise_remove(pFunctionPointer_t pf) {
bool res = _rise.remove(pf);
if (res && _rise.size() == 0)
gpio_irq_set(&gpio_irq, IRQ_RISE, 0);
return res;
}
pFunctionPointer_t InterruptIn::fall(void (*fptr)(void)) {
pFunctionPointer_t pf = NULL;
_fall.clear();
void InterruptIn::fall(void (*fptr)(void)) {
if (fptr) {
pf = _fall.add(fptr);
_fall.attach(fptr);
gpio_irq_set(&gpio_irq, IRQ_FALL, 1);
} else {
gpio_irq_set(&gpio_irq, IRQ_FALL, 0);
}
return pf;
}
pFunctionPointer_t InterruptIn::fall_add_common(void (*fptr)(void), bool front) {
if (NULL == fptr)
return NULL;
pFunctionPointer_t pf = front ? _fall.add_front(fptr) : _fall.add(fptr);
gpio_irq_set(&gpio_irq, IRQ_FALL, 1);
return pf;
}
bool InterruptIn::fall_remove(pFunctionPointer_t pf) {
bool res = _fall.remove(pf);
if (res && _fall.size() == 0)
gpio_irq_set(&gpio_irq, IRQ_FALL, 0);
return res;
}
void InterruptIn::_irq_handler(uint32_t id, gpio_irq_event event) {

View File

@ -0,0 +1,36 @@
/* mbed Microcontroller Library
* Copyright (c) 2006-2013 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "RawSerial.h"
#include "wait_api.h"
#if DEVICE_SERIAL
namespace mbed {
RawSerial::RawSerial(PinName tx, PinName rx) : SerialBase(tx, rx) {
}
int RawSerial::getc() {
return _base_getc();
}
int RawSerial::putc(int c) {
return _base_putc(c);
}
} // namespace mbed
#endif

View File

@ -20,83 +20,15 @@
namespace mbed {
Serial::Serial(PinName tx, PinName rx, const char *name) : Stream(name) {
serial_init(&_serial, tx, rx);
_baud = 9600;
serial_irq_handler(&_serial, Serial::_irq_handler, (uint32_t)this);
}
void Serial::baud(int baudrate) {
serial_baud(&_serial, baudrate);
_baud = baudrate;
}
void Serial::format(int bits, Parity parity, int stop_bits) {
serial_format(&_serial, bits, (SerialParity)parity, stop_bits);
}
int Serial::readable() {
return serial_readable(&_serial);
}
int Serial::writeable() {
return serial_writable(&_serial);
}
pFunctionPointer_t Serial::attach(void (*fptr)(void), IrqType type) {
pFunctionPointer_t pf = NULL;
_irq[type].clear();
if (fptr) {
pf = _irq[type].add(fptr);
serial_irq_set(&_serial, (SerialIrq)type, 1);
} else {
serial_irq_set(&_serial, (SerialIrq)type, 0);
}
return pf;
}
pFunctionPointer_t Serial::add_handler_helper(void (*fptr)(void), IrqType type, bool front) {
if (NULL == fptr)
return NULL;
pFunctionPointer_t pf = front ? _irq[type].add_front(fptr) : _irq[type].add(fptr);
serial_irq_set(&_serial, (SerialIrq)type, 1);
return pf;
}
bool Serial::remove_handler(pFunctionPointer_t pf, IrqType type) {
bool res = _irq[type].remove(pf);
if (res && _irq[type].size() == 0)
serial_irq_set(&_serial, (SerialIrq)type, 0);
return res;
}
void Serial::_irq_handler(uint32_t id, SerialIrq irq_type) {
Serial *handler = (Serial*)id;
handler->_irq[irq_type].call();
Serial::Serial(PinName tx, PinName rx, const char *name) : SerialBase(tx, rx), Stream(name) {
}
int Serial::_getc() {
return serial_getc(&_serial);
return _base_getc();
}
int Serial::_putc(int c) {
serial_putc(&_serial, c);
return c;
}
void Serial::send_break() {
// Wait for 1.5 frames before clearing the break condition
// This will have different effects on our platforms, but should
// ensure that we keep the break active for at least one frame.
// We consider a full frame (1 start bit + 8 data bits bits +
// 1 parity bit + 2 stop bits = 12 bits) for computation.
// One bit time (in us) = 1000000/_baud
// Twelve bits: 12000000/baud delay
// 1.5 frames: 18000000/baud delay
serial_break_set(&_serial);
wait_us(18000000/_baud);
serial_break_clear(&_serial);
return _base_putc(c);
}
} // namespace mbed

View File

@ -0,0 +1,86 @@
/* mbed Microcontroller Library
* Copyright (c) 2006-2013 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "SerialBase.h"
#include "wait_api.h"
#if DEVICE_SERIAL
namespace mbed {
SerialBase::SerialBase(PinName tx, PinName rx) {
serial_init(&_serial, tx, rx);
_baud = 9600;
serial_irq_handler(&_serial, SerialBase::_irq_handler, (uint32_t)this);
}
void SerialBase::baud(int baudrate) {
serial_baud(&_serial, baudrate);
_baud = baudrate;
}
void SerialBase::format(int bits, Parity parity, int stop_bits) {
serial_format(&_serial, bits, (SerialParity)parity, stop_bits);
}
int SerialBase::readable() {
return serial_readable(&_serial);
}
int SerialBase::writeable() {
return serial_writable(&_serial);
}
void SerialBase::attach(void (*fptr)(void), IrqType type) {
if (fptr) {
_irq[type].attach(fptr);
serial_irq_set(&_serial, (SerialIrq)type, 1);
} else {
serial_irq_set(&_serial, (SerialIrq)type, 0);
}
}
void SerialBase::_irq_handler(uint32_t id, SerialIrq irq_type) {
SerialBase *handler = (SerialBase*)id;
handler->_irq[irq_type].call();
}
int SerialBase::_base_getc() {
return serial_getc(&_serial);
}
int SerialBase::_base_putc(int c) {
serial_putc(&_serial, c);
return c;
}
void SerialBase::send_break() {
// Wait for 1.5 frames before clearing the break condition
// This will have different effects on our platforms, but should
// ensure that we keep the break active for at least one frame.
// We consider a full frame (1 start bit + 8 data bits bits +
// 1 parity bit + 2 stop bits = 12 bits) for computation.
// One bit time (in us) = 1000000/_baud
// Twelve bits: 12000000/baud delay
// 1.5 frames: 18000000/baud delay
serial_break_set(&_serial);
wait_us(18000000/_baud);
serial_break_clear(&_serial);
}
} // namespace mbed
#endif

View File

@ -22,7 +22,7 @@ namespace mbed {
void Ticker::detach() {
remove();
_chain.clear();
_function.attach(0);
}
void Ticker::setup(unsigned int t) {
@ -33,13 +33,7 @@ void Ticker::setup(unsigned int t) {
void Ticker::handler() {
insert(event.timestamp + _delay);
_chain.call();
}
pFunctionPointer_t Ticker::add_function_helper(void (*fptr)(void), bool front) {
if (_chain.size() == 0)
return NULL;
return front ? _chain.add_front(fptr) : _chain.add(fptr);
_function.call();
}
} // namespace mbed

View File

@ -18,7 +18,7 @@
namespace mbed {
void Timeout::handler() {
_chain.call();
_function.call();
}
} // namespace mbed

View File

@ -1,121 +0,0 @@
#include "test_env.h"
DigitalOut myled(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);
volatile int checks = 0;
volatile int total = 0;
void in_handler() {
checks++;
led2 = !led2;
}
#if defined(TARGET_KL25Z)
#define PIN_OUT PTC6
#define PIN_IN PTA5
#elif defined(TARGET_KL05Z)
#define PIN_OUT PTB11
#define PIN_IN PTB1
#elif defined(TARGET_LPC812)
#define PIN_OUT D10
#define PIN_IN D11
#elif defined(TARGET_LPC4088)
#define PIN_IN (p11)
#define PIN_OUT (p12)
#elif defined(TARGET_LPC1114)
#define PIN_IN (dp1)
#define PIN_OUT (dp2)
#else
#define PIN_IN (p5)
#define PIN_OUT (p25)
#endif
DigitalOut out(PIN_OUT);
InterruptIn in(PIN_IN);
void flipper() {
for (int i = 0; i < 5; i++) {
out = 1; myled = 1; wait(0.2);
out = 0; myled = 0; wait(0.2);
}
}
void flipper2() {
led3 = !led3;
}
void handler2() {
total++;
}
int object_cnt = 0;
class Incrementer {
public:
Incrementer() : _cnt(0) {}
void inc() { _cnt++; }
int get() const { return _cnt; }
private:
int _cnt;
};
int main() {
out = 0; myled = 0;
//Test falling edges first
Incrementer i1;
in.rise(NULL);
in.fall(in_handler);
in.fall_add(handler2);
in.fall_add(flipper2);
in.fall_add_front(&i1, &Incrementer::inc);
flipper();
if(checks != 5 || i1.get() != 5) {
printf("falling edges test failed\n");
notify_completion(false);
}
//Now test rising edges
Incrementer i2;
in.rise(in_handler);
in.rise_add(handler2);
in.rise_add(&i2, &Incrementer::inc);
in.rise_add_front(flipper2);
in.fall(NULL);
flipper();
if (checks != 10 || i2.get() != 5) {
printf("raising edges test failed\n");
notify_completion(false);
}
//Finally test both
in.rise(in_handler);
in.rise_add(handler2);
pFunctionPointer_t rise_led = in.rise_add(flipper2);
in.fall(in_handler);
in.fall_add(handler2);
pFunctionPointer_t fall_led = in.fall_add(flipper2);
if (!in.rise_remove(rise_led) || !in.fall_remove(fall_led)) {
printf("remove handler failed\n");
notify_completion(false);
}
flipper();
if (checks != 20) {
printf("Simultaneous rising and falling edges failed! check=%d\n", checks);
notify_completion(false);
}
printf("Total: %d\n", total);
notify_completion(true);
return 0;
}

View File

@ -84,9 +84,7 @@ int main() {
// Test chaining inside Serial class
flipper_1.attach(&flip_1, 1.0); // the address of the function to be attached (flip) and the interval (1 second)
flipper_1.add_function_front(&s1, &Sender::send);
flipper_2.attach(&flip_2, 2.0); // the address of the function to be attached (flip) and the interval (2 seconds)
flipper_2.add_function(&s2, &Sender::send);
// Test global chaining (InterruptManager)
printf("Handler initially: %08X\n", initial_handler = NVIC_GetVector(TIMER_IRQ));

View File

@ -1,41 +0,0 @@
#include "mbed.h"
DigitalOut led1(LED1);
DigitalOut led2(LED2);
Serial computer(USBTX, USBRX);
// This function is called when a character goes into the TX buffer.
void txCallback() {
led1 = !led1;
}
// This function is called when a character goes into the RX buffer.
void rxCallback() {
led2 = !led2;
computer.putc(computer.getc());
}
class Counter {
public:
Counter(const char* name): _name(name), _cnt(0) {}
void inc() { _cnt++; }
void show() const { printf("%s: %d\n", _name, _cnt); }
int get() const { return _cnt; }
~Counter() { show(); }
private:
const char *_name;
volatile int _cnt;
};
int main() {
printf("start test\n");
Counter rx("RX bytes"), tx("TX bytes");
computer.attach(&txCallback, Serial::TxIrq);
computer.add_handler(&tx, &Counter::inc, Serial::TxIrq);
computer.attach(&rxCallback, Serial::RxIrq);
computer.add_handler_front(&rx, &Counter::inc, Serial::RxIrq);
while (rx.get() < 40);
}

View File

@ -198,13 +198,7 @@ TESTS = [
"peripherals": ["i2c_loop"]
},
{
"id": "MBED_A21", "description": "Interrupt chaining (InterruptIn)",
"source_dir": join(TEST_DIR, "mbed", "interrupt_chaining", "interruptin"),
"dependencies": [MBED_LIBRARIES, TEST_MBED_LIB],
"peripherals": ["digital_loop"]
},
{
"id": "MBED_A22", "description": "Call function before main (mbed_main)",
"id": "MBED_A21", "description": "Call function before main (mbed_main)",
"source_dir": join(TEST_DIR, "mbed", "call_before_main"),
"dependencies": [MBED_LIBRARIES, TEST_MBED_LIB],
"automated": True,
@ -382,23 +376,18 @@ TESTS = [
"peripherals": ["ADXL345"]
},
{
"id": "MBED_28", "description": "Interrupt chaining (serial)",
"source_dir": join(TEST_DIR, "mbed", "interrupt_chaining", "serial_interrupt"),
"id": "MBED_28", "description": "Interrupt chaining (InterruptManager)",
"source_dir": join(TEST_DIR, "mbed", "interrupt_chaining"),
"dependencies": [MBED_LIBRARIES, TEST_MBED_LIB],
},
{
"id": "MBED_29", "description": "Interrupt chaining (ticker + InterruptManager)",
"source_dir": join(TEST_DIR, "mbed", "interrupt_chaining", "ticker"),
"dependencies": [MBED_LIBRARIES, TEST_MBED_LIB],
},
{
"id": "MBED_30", "description": "CAN network test",
"id": "MBED_29", "description": "CAN network test",
"source_dir": join(TEST_DIR, "mbed", "can"),
"dependencies": [MBED_LIBRARIES],
"mcu": ["LPC1768", "LPC4088"]
},
{
"id": "MBED_31", "description": "CAN network test using interrupts",
"id": "MBED_30", "description": "CAN network test using interrupts",
"source_dir": join(TEST_DIR, "mbed", "can_interrupt"),
"dependencies": [MBED_LIBRARIES],
"mcu": ["LPC1768", "LPC4088"]
@ -690,6 +679,7 @@ GROUPS = {
}
GROUPS["rtos"] = [test["id"] for test in TESTS if test["id"].startswith("RTOS_")]
GROUPS["net"] = [test["id"] for test in TESTS if test["id"].startswith("NET_")]
GROUPS["automated"] = [test["id"] for test in TESTS if test.get("automated", False)]
# Look for 'TEST_GROUPS' in private_settings.py and update the GROUPS dictionary
# with the information in test_groups if found
try: