mbed library sources(for async_print)
Fork of mbed-src by
Revision 36:ab3ee77451e7, committed 2013-10-23
- Comitter:
- mbed_official
- Date:
- Wed Oct 23 14:15:04 2013 +0100
- Parent:
- 35:371630885ad6
- Child:
- 37:88e9030f311f
- Commit message:
- Synchronized with git revision 2404dc0092fa583d899df3d9021a4ddb4510b011
Changed in this revision
--- a/api/InterruptIn.h Mon Oct 21 11:45:04 2013 +0100 +++ b/api/InterruptIn.h Wed Oct 23 14:15:04 2013 +0100 @@ -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 @@ /** 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); - 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); + void rise(T* tptr, void (T::*mptr)(void)) { + _rise.attach(tptr, mptr); + gpio_irq_set(&gpio_irq, IRQ_RISE, 1); } - /** 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); - 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); + void fall(T* tptr, void (T::*mptr)(void)) { + _fall.attach(tptr, mptr); + gpio_irq_set(&gpio_irq, IRQ_FALL, 1); } - /** 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 @@ 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
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/api/RawSerial.h Wed Oct 23 14:15:04 2013 +0100 @@ -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
--- a/api/Serial.h Mon Oct 21 11:45:04 2013 +0100 +++ b/api/Serial.h Wed Oct 23 14:15:04 2013 +0100 @@ -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 @@ * } * @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 @@ */ 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
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/api/SerialBase.h Wed Oct 23 14:15:04 2013 +0100 @@ -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
--- a/api/Ticker.h Mon Oct 21 11:45:04 2013 +0100 +++ b/api/Ticker.h Wed Oct 23 14:15:04 2013 +0100 @@ -18,7 +18,6 @@ #include "TimerEvent.h" #include "FunctionPointer.h" -#include "CallChain.h" namespace mbed { @@ -63,34 +62,9 @@ * * @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 @@ * @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 @@ * @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
--- a/common/CallChain.cpp Mon Oct 21 11:45:04 2013 +0100 +++ b/common/CallChain.cpp Wed Oct 23 14:15:04 2013 +0100 @@ -38,13 +38,11 @@ } 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 @@ 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::_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_front(pFunctionPointer_t pf) { _check_size(); - __disable_irq(); memmove(_chain + 1, _chain, _elements * sizeof(pFunctionPointer_t)); _chain[0] = pf; _elements ++; - __enable_irq(); return pf; }
--- a/common/InterruptIn.cpp Mon Oct 21 11:45:04 2013 +0100 +++ b/common/InterruptIn.cpp Wed Oct 23 14:15:04 2013 +0100 @@ -36,58 +36,22 @@ 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) {
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/common/RawSerial.cpp Wed Oct 23 14:15:04 2013 +0100 @@ -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
--- a/common/Serial.cpp Mon Oct 21 11:45:04 2013 +0100 +++ b/common/Serial.cpp Wed Oct 23 14:15:04 2013 +0100 @@ -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
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/common/SerialBase.cpp Wed Oct 23 14:15:04 2013 +0100 @@ -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
--- a/common/Ticker.cpp Mon Oct 21 11:45:04 2013 +0100 +++ b/common/Ticker.cpp Wed Oct 23 14:15:04 2013 +0100 @@ -22,7 +22,7 @@ void Ticker::detach() { remove(); - _chain.clear(); + _function.attach(0); } void Ticker::setup(unsigned int t) { @@ -33,13 +33,7 @@ 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
--- a/common/Timeout.cpp Mon Oct 21 11:45:04 2013 +0100 +++ b/common/Timeout.cpp Wed Oct 23 14:15:04 2013 +0100 @@ -18,7 +18,7 @@ namespace mbed { void Timeout::handler() { - _chain.call(); + _function.call(); } } // namespace mbed