diff --git a/drivers/AnalogIn.h b/drivers/AnalogIn.h index e81428fc21..c9ccfa25a1 100644 --- a/drivers/AnalogIn.h +++ b/drivers/AnalogIn.h @@ -57,7 +57,8 @@ public: * * @param pin AnalogIn pin to connect to */ - AnalogIn(PinName pin) { + AnalogIn(PinName pin) + { lock(); analogin_init(&_adc, pin); unlock(); @@ -67,7 +68,8 @@ public: * * @returns A floating-point value representing the current input voltage, measured as a percentage */ - float read() { + float read() + { lock(); float ret = analogin_read(&_adc); unlock(); @@ -79,7 +81,8 @@ public: * @returns * 16-bit unsigned short representing the current input voltage, normalised to a 16-bit value */ - unsigned short read_u16() { + unsigned short read_u16() + { lock(); unsigned short ret = analogin_read_u16(&_adc); unlock(); @@ -99,22 +102,26 @@ public: * if(volume > 0.25) { ... } * @endcode */ - operator float() { + operator float() + { // Underlying call is thread safe return read(); } - virtual ~AnalogIn() { + virtual ~AnalogIn() + { // Do nothing } protected: - virtual void lock() { + virtual void lock() + { _mutex->lock(); } - virtual void unlock() { + virtual void unlock() + { _mutex->unlock(); } diff --git a/drivers/AnalogOut.h b/drivers/AnalogOut.h index 5038f0f7a4..3945c580c4 100644 --- a/drivers/AnalogOut.h +++ b/drivers/AnalogOut.h @@ -57,7 +57,8 @@ public: * * @param pin AnalogOut pin to connect to */ - AnalogOut(PinName pin) { + AnalogOut(PinName pin) + { analogout_init(&_dac, pin); } @@ -68,7 +69,8 @@ public: * 0.0f (representing 0v / 0%) and 1.0f (representing 3.3v / 100%). * Values outside this range will be saturated to 0.0f or 1.0f. */ - void write(float value) { + void write(float value) + { lock(); analogout_write(&_dac, value); unlock(); @@ -79,7 +81,8 @@ public: * @param value 16-bit unsigned short representing the output voltage, * normalised to a 16-bit value (0x0000 = 0v, 0xFFFF = 3.3v) */ - void write_u16(unsigned short value) { + void write_u16(unsigned short value) + { lock(); analogout_write_u16(&_dac, value); unlock(); @@ -95,7 +98,8 @@ public: * @note * This value may not match exactly the value set by a previous write(). */ - float read() { + float read() + { lock(); float ret = analogout_read(&_dac); unlock(); @@ -105,7 +109,8 @@ public: /** An operator shorthand for write() * \sa AnalogOut::write() */ - AnalogOut& operator= (float percent) { + AnalogOut &operator= (float percent) + { // Underlying write call is thread safe write(percent); return *this; @@ -114,7 +119,8 @@ public: /** An operator shorthand for write() * \sa AnalogOut::write() */ - AnalogOut& operator= (AnalogOut& rhs) { + AnalogOut &operator= (AnalogOut &rhs) + { // Underlying write call is thread safe write(rhs.read()); return *this; @@ -123,22 +129,26 @@ public: /** An operator shorthand for read() * \sa AnalogOut::read() */ - operator float() { + operator float() + { // Underlying read call is thread safe return read(); } - virtual ~AnalogOut() { + virtual ~AnalogOut() + { // Do nothing } protected: - virtual void lock() { + virtual void lock() + { _mutex.lock(); } - virtual void unlock() { + virtual void unlock() + { _mutex.unlock(); } diff --git a/drivers/BusIn.cpp b/drivers/BusIn.cpp index 1fd0528214..f6cc933e7e 100644 --- a/drivers/BusIn.cpp +++ b/drivers/BusIn.cpp @@ -18,12 +18,13 @@ namespace mbed { -BusIn::BusIn(PinName p0, PinName p1, PinName p2, PinName p3, PinName p4, PinName p5, PinName p6, PinName p7, PinName p8, PinName p9, PinName p10, PinName p11, PinName p12, PinName p13, PinName p14, PinName p15) { +BusIn::BusIn(PinName p0, PinName p1, PinName p2, PinName p3, PinName p4, PinName p5, PinName p6, PinName p7, PinName p8, PinName p9, PinName p10, PinName p11, PinName p12, PinName p13, PinName p14, PinName p15) +{ PinName pins[16] = {p0, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15}; // No lock needed in the constructor _nc_mask = 0; - for (int i=0; i<16; i++) { + for (int i = 0; i < 16; i++) { _pin[i] = (pins[i] != NC) ? new DigitalIn(pins[i]) : 0; if (pins[i] != NC) { _nc_mask |= (1 << i); @@ -31,10 +32,11 @@ BusIn::BusIn(PinName p0, PinName p1, PinName p2, PinName p3, PinName p4, PinName } } -BusIn::BusIn(PinName pins[16]) { +BusIn::BusIn(PinName pins[16]) +{ // No lock needed in the constructor _nc_mask = 0; - for (int i=0; i<16; i++) { + for (int i = 0; i < 16; i++) { _pin[i] = (pins[i] != NC) ? new DigitalIn(pins[i]) : 0; if (pins[i] != NC) { _nc_mask |= (1 << i); @@ -42,19 +44,21 @@ BusIn::BusIn(PinName pins[16]) { } } -BusIn::~BusIn() { +BusIn::~BusIn() +{ // No lock needed in the destructor - for (int i=0; i<16; i++) { + for (int i = 0; i < 16; i++) { if (_pin[i] != 0) { delete _pin[i]; } } } -int BusIn::read() { +int BusIn::read() +{ int v = 0; lock(); - for (int i=0; i<16; i++) { + for (int i = 0; i < 16; i++) { if (_pin[i] != 0) { v |= _pin[i]->read() << i; } @@ -63,9 +67,10 @@ int BusIn::read() { return v; } -void BusIn::mode(PinMode pull) { +void BusIn::mode(PinMode pull) +{ lock(); - for (int i=0; i<16; i++) { + for (int i = 0; i < 16; i++) { if (_pin[i] != 0) { _pin[i]->mode(pull); } @@ -73,20 +78,24 @@ void BusIn::mode(PinMode pull) { unlock(); } -void BusIn::lock() { +void BusIn::lock() +{ _mutex.lock(); } -void BusIn::unlock() { +void BusIn::unlock() +{ _mutex.unlock(); } -BusIn::operator int() { +BusIn::operator int() +{ // Underlying read is thread safe return read(); } -DigitalIn& BusIn::operator[] (int index) { +DigitalIn &BusIn::operator[](int index) +{ // No lock needed since _pin is not modified outside the constructor MBED_ASSERT(index >= 0 && index <= 16); MBED_ASSERT(_pin[index]); diff --git a/drivers/BusIn.h b/drivers/BusIn.h index d015529435..80b20d72a1 100644 --- a/drivers/BusIn.h +++ b/drivers/BusIn.h @@ -62,12 +62,12 @@ public: PinName p8 = NC, PinName p9 = NC, PinName p10 = NC, PinName p11 = NC, PinName p12 = NC, PinName p13 = NC, PinName p14 = NC, PinName p15 = NC); - + /** Create an BusIn, connected to the specified pins * * @param pins An array of pins to connect to bus bit */ - BusIn(PinName pins[16]); + BusIn(PinName pins[16]); virtual ~BusIn(); @@ -90,7 +90,8 @@ public: * @returns * Binary mask of connected pins */ - int mask() { + int mask() + { // No lock needed since _nc_mask is not modified outside the constructor return _nc_mask; } @@ -103,10 +104,10 @@ public: /** Access to particular bit in random-iterator fashion * @param index Position of bit */ - DigitalIn & operator[] (int index); + DigitalIn &operator[](int index); protected: - DigitalIn* _pin[16]; + DigitalIn *_pin[16]; /* Mask of bus's NC pins * If bit[n] is set to 1 - pin is connected diff --git a/drivers/BusInOut.cpp b/drivers/BusInOut.cpp index ff244fa464..950cbb5d58 100644 --- a/drivers/BusInOut.cpp +++ b/drivers/BusInOut.cpp @@ -18,12 +18,13 @@ namespace mbed { -BusInOut::BusInOut(PinName p0, PinName p1, PinName p2, PinName p3, PinName p4, PinName p5, PinName p6, PinName p7, PinName p8, PinName p9, PinName p10, PinName p11, PinName p12, PinName p13, PinName p14, PinName p15) { +BusInOut::BusInOut(PinName p0, PinName p1, PinName p2, PinName p3, PinName p4, PinName p5, PinName p6, PinName p7, PinName p8, PinName p9, PinName p10, PinName p11, PinName p12, PinName p13, PinName p14, PinName p15) +{ PinName pins[16] = {p0, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15}; // No lock needed in the constructor _nc_mask = 0; - for (int i=0; i<16; i++) { + for (int i = 0; i < 16; i++) { _pin[i] = (pins[i] != NC) ? new DigitalInOut(pins[i]) : 0; if (pins[i] != NC) { _nc_mask |= (1 << i); @@ -31,10 +32,11 @@ BusInOut::BusInOut(PinName p0, PinName p1, PinName p2, PinName p3, PinName p4, P } } -BusInOut::BusInOut(PinName pins[16]) { +BusInOut::BusInOut(PinName pins[16]) +{ // No lock needed in the constructor _nc_mask = 0; - for (int i=0; i<16; i++) { + for (int i = 0; i < 16; i++) { _pin[i] = (pins[i] != NC) ? new DigitalInOut(pins[i]) : 0; if (pins[i] != NC) { _nc_mask |= (1 << i); @@ -42,18 +44,20 @@ BusInOut::BusInOut(PinName pins[16]) { } } -BusInOut::~BusInOut() { +BusInOut::~BusInOut() +{ // No lock needed in the destructor - for (int i=0; i<16; i++) { + for (int i = 0; i < 16; i++) { if (_pin[i] != 0) { delete _pin[i]; } } } -void BusInOut::write(int value) { +void BusInOut::write(int value) +{ lock(); - for (int i=0; i<16; i++) { + for (int i = 0; i < 16; i++) { if (_pin[i] != 0) { _pin[i]->write((value >> i) & 1); } @@ -61,10 +65,11 @@ void BusInOut::write(int value) { unlock(); } -int BusInOut::read() { +int BusInOut::read() +{ lock(); int v = 0; - for (int i=0; i<16; i++) { + for (int i = 0; i < 16; i++) { if (_pin[i] != 0) { v |= _pin[i]->read() << i; } @@ -73,9 +78,10 @@ int BusInOut::read() { return v; } -void BusInOut::output() { +void BusInOut::output() +{ lock(); - for (int i=0; i<16; i++) { + for (int i = 0; i < 16; i++) { if (_pin[i] != 0) { _pin[i]->output(); } @@ -83,9 +89,10 @@ void BusInOut::output() { unlock(); } -void BusInOut::input() { +void BusInOut::input() +{ lock(); - for (int i=0; i<16; i++) { + for (int i = 0; i < 16; i++) { if (_pin[i] != 0) { _pin[i]->input(); } @@ -93,9 +100,10 @@ void BusInOut::input() { unlock(); } -void BusInOut::mode(PinMode pull) { +void BusInOut::mode(PinMode pull) +{ lock(); - for (int i=0; i<16; i++) { + for (int i = 0; i < 16; i++) { if (_pin[i] != 0) { _pin[i]->mode(pull); } @@ -103,35 +111,41 @@ void BusInOut::mode(PinMode pull) { unlock(); } -BusInOut& BusInOut::operator= (int v) { +BusInOut &BusInOut::operator= (int v) +{ // Underlying write is thread safe write(v); return *this; } -BusInOut& BusInOut::operator= (BusInOut& rhs) { +BusInOut &BusInOut::operator= (BusInOut &rhs) +{ // Underlying read is thread safe write(rhs.read()); return *this; } -DigitalInOut& BusInOut::operator[] (int index) { +DigitalInOut &BusInOut::operator[](int index) +{ // No lock needed since _pin is not modified outside the constructor MBED_ASSERT(index >= 0 && index <= 16); MBED_ASSERT(_pin[index]); return *_pin[index]; } -BusInOut::operator int() { +BusInOut::operator int() +{ // Underlying read is thread safe return read(); } -void BusInOut::lock() { +void BusInOut::lock() +{ _mutex.lock(); } -void BusInOut::unlock() { +void BusInOut::unlock() +{ _mutex.unlock(); } diff --git a/drivers/BusInOut.h b/drivers/BusInOut.h index 0be52cacc5..9bb4e995ae 100644 --- a/drivers/BusInOut.h +++ b/drivers/BusInOut.h @@ -103,21 +103,22 @@ public: * @returns * Binary mask of connected pins */ - int mask() { + int mask() + { // No lock needed since _nc_mask is not modified outside the constructor return _nc_mask; } - /** A shorthand for write() + /** A shorthand for write() * \sa BusInOut::write() - */ - BusInOut& operator= (int v); - BusInOut& operator= (BusInOut& rhs); + */ + BusInOut &operator= (int v); + BusInOut &operator= (BusInOut &rhs); /** Access to particular bit in random-iterator fashion * @param index Bit Position */ - DigitalInOut& operator[] (int index); + DigitalInOut &operator[](int index); /** A shorthand for read() * \sa BusInOut::read() @@ -127,7 +128,7 @@ public: protected: virtual void lock(); virtual void unlock(); - DigitalInOut* _pin[16]; + DigitalInOut *_pin[16]; /* Mask of bus's NC pins * If bit[n] is set to 1 - pin is connected diff --git a/drivers/BusOut.cpp b/drivers/BusOut.cpp index 901955257a..913ba19750 100644 --- a/drivers/BusOut.cpp +++ b/drivers/BusOut.cpp @@ -18,12 +18,13 @@ namespace mbed { -BusOut::BusOut(PinName p0, PinName p1, PinName p2, PinName p3, PinName p4, PinName p5, PinName p6, PinName p7, PinName p8, PinName p9, PinName p10, PinName p11, PinName p12, PinName p13, PinName p14, PinName p15) { +BusOut::BusOut(PinName p0, PinName p1, PinName p2, PinName p3, PinName p4, PinName p5, PinName p6, PinName p7, PinName p8, PinName p9, PinName p10, PinName p11, PinName p12, PinName p13, PinName p14, PinName p15) +{ PinName pins[16] = {p0, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15}; // No lock needed in the constructor _nc_mask = 0; - for (int i=0; i<16; i++) { + for (int i = 0; i < 16; i++) { _pin[i] = (pins[i] != NC) ? new DigitalOut(pins[i]) : 0; if (pins[i] != NC) { _nc_mask |= (1 << i); @@ -31,10 +32,11 @@ BusOut::BusOut(PinName p0, PinName p1, PinName p2, PinName p3, PinName p4, PinNa } } -BusOut::BusOut(PinName pins[16]) { +BusOut::BusOut(PinName pins[16]) +{ // No lock needed in the constructor _nc_mask = 0; - for (int i=0; i<16; i++) { + for (int i = 0; i < 16; i++) { _pin[i] = (pins[i] != NC) ? new DigitalOut(pins[i]) : 0; if (pins[i] != NC) { _nc_mask |= (1 << i); @@ -42,18 +44,20 @@ BusOut::BusOut(PinName pins[16]) { } } -BusOut::~BusOut() { +BusOut::~BusOut() +{ // No lock needed in the destructor - for (int i=0; i<16; i++) { + for (int i = 0; i < 16; i++) { if (_pin[i] != 0) { delete _pin[i]; } } } -void BusOut::write(int value) { +void BusOut::write(int value) +{ lock(); - for (int i=0; i<16; i++) { + for (int i = 0; i < 16; i++) { if (_pin[i] != 0) { _pin[i]->write((value >> i) & 1); } @@ -61,10 +65,11 @@ void BusOut::write(int value) { unlock(); } -int BusOut::read() { +int BusOut::read() +{ lock(); int v = 0; - for (int i=0; i<16; i++) { + for (int i = 0; i < 16; i++) { if (_pin[i] != 0) { v |= _pin[i]->read() << i; } @@ -73,35 +78,41 @@ int BusOut::read() { return v; } -BusOut& BusOut::operator= (int v) { +BusOut &BusOut::operator= (int v) +{ // Underlying write is thread safe write(v); return *this; } -BusOut& BusOut::operator= (BusOut& rhs) { +BusOut &BusOut::operator= (BusOut &rhs) +{ // Underlying write is thread safe write(rhs.read()); return *this; } -DigitalOut& BusOut::operator[] (int index) { +DigitalOut &BusOut::operator[](int index) +{ // No lock needed since _pin is not modified outside the constructor MBED_ASSERT(index >= 0 && index <= 16); MBED_ASSERT(_pin[index]); return *_pin[index]; } -BusOut::operator int() { +BusOut::operator int() +{ // Underlying read is thread safe return read(); } -void BusOut::lock() { +void BusOut::lock() +{ _mutex.lock(); } -void BusOut::unlock() { +void BusOut::unlock() +{ _mutex.unlock(); } diff --git a/drivers/BusOut.h b/drivers/BusOut.h index d7612d4592..2f908b65d8 100644 --- a/drivers/BusOut.h +++ b/drivers/BusOut.h @@ -87,7 +87,8 @@ public: * @returns * Binary mask of connected pins */ - int mask() { + int mask() + { // No lock needed since _nc_mask is not modified outside the constructor return _nc_mask; } @@ -95,13 +96,13 @@ public: /** A shorthand for write() * \sa BusOut::write() */ - BusOut& operator= (int v); - BusOut& operator= (BusOut& rhs); + BusOut &operator= (int v); + BusOut &operator= (BusOut &rhs); /** Access to particular bit in random-iterator fashion * @param index Bit Position */ - DigitalOut& operator[] (int index); + DigitalOut &operator[](int index); /** A shorthand for read() * \sa BusOut::read() @@ -111,7 +112,7 @@ public: protected: virtual void lock(); virtual void unlock(); - DigitalOut* _pin[16]; + DigitalOut *_pin[16]; /* Mask of bus's NC pins * If bit[n] is set to 1 - pin is connected diff --git a/drivers/CAN.cpp b/drivers/CAN.cpp index a5e8e4266b..2c0a53442f 100644 --- a/drivers/CAN.cpp +++ b/drivers/CAN.cpp @@ -22,7 +22,8 @@ namespace mbed { -CAN::CAN(PinName rd, PinName td) : _can(), _irq() { +CAN::CAN(PinName rd, PinName td) : _can(), _irq() +{ // No lock needed in constructor for (size_t i = 0; i < sizeof _irq / sizeof _irq[0]; i++) { @@ -33,7 +34,8 @@ CAN::CAN(PinName rd, PinName td) : _can(), _irq() { can_irq_init(&_can, (&CAN::_irq_handler), (uint32_t)this); } -CAN::CAN(PinName rd, PinName td, int hz) : _can(), _irq() { +CAN::CAN(PinName rd, PinName td, int hz) : _can(), _irq() +{ // No lock needed in constructor for (size_t i = 0; i < sizeof _irq / sizeof _irq[0]; i++) { @@ -44,7 +46,8 @@ CAN::CAN(PinName rd, PinName td, int hz) : _can(), _irq() { can_irq_init(&_can, (&CAN::_irq_handler), (uint32_t)this); } -CAN::~CAN() { +CAN::~CAN() +{ // No lock needed in destructor // Detaching interrupts releases the sleep lock if it was locked @@ -55,68 +58,78 @@ CAN::~CAN() { can_free(&_can); } -int CAN::frequency(int f) { +int CAN::frequency(int f) +{ lock(); int ret = can_frequency(&_can, f); unlock(); return ret; } -int CAN::write(CANMessage msg) { +int CAN::write(CANMessage msg) +{ lock(); int ret = can_write(&_can, msg, 0); unlock(); return ret; } -int CAN::read(CANMessage &msg, int handle) { +int CAN::read(CANMessage &msg, int handle) +{ lock(); int ret = can_read(&_can, &msg, handle); unlock(); return ret; } -void CAN::reset() { +void CAN::reset() +{ lock(); can_reset(&_can); unlock(); } -unsigned char CAN::rderror() { +unsigned char CAN::rderror() +{ lock(); int ret = can_rderror(&_can); unlock(); return ret; } -unsigned char CAN::tderror() { +unsigned char CAN::tderror() +{ lock(); int ret = can_tderror(&_can); unlock(); return ret; } -void CAN::monitor(bool silent) { +void CAN::monitor(bool silent) +{ lock(); can_monitor(&_can, (silent) ? 1 : 0); unlock(); } -int CAN::mode(Mode mode) { +int CAN::mode(Mode mode) +{ lock(); int ret = can_mode(&_can, (CanMode)mode); unlock(); return ret; } -int CAN::filter(unsigned int id, unsigned int mask, CANFormat format, int handle) { +int CAN::filter(unsigned int id, unsigned int mask, CANFormat format, int handle) +{ lock(); int ret = can_filter(&_can, id, mask, format, handle); unlock(); return ret; } -void CAN::attach(Callback func, IrqType type) { +void CAN::attach(Callback func, IrqType type) +{ lock(); if (func) { // lock deep sleep only the first time @@ -136,18 +149,21 @@ void CAN::attach(Callback func, IrqType type) { unlock(); } -void CAN::_irq_handler(uint32_t id, CanIrqType type) { - CAN *handler = (CAN*)id; +void CAN::_irq_handler(uint32_t id, CanIrqType type) +{ + CAN *handler = (CAN *)id; if (handler->_irq[type]) { handler->_irq[type].call(); } } -void CAN::lock() { +void CAN::lock() +{ _mutex.lock(); } -void CAN::unlock() { +void CAN::unlock() +{ _mutex.unlock(); } diff --git a/drivers/CAN.h b/drivers/CAN.h index 668e32c256..5f506f68ad 100644 --- a/drivers/CAN.h +++ b/drivers/CAN.h @@ -38,7 +38,8 @@ class CANMessage : public CAN_Message { public: /** Creates empty CAN message. */ - CANMessage() : CAN_Message() { + CANMessage() : CAN_Message() + { len = 8; type = CANData; format = CANStandard; @@ -54,12 +55,13 @@ public: * @param _type Type of Data: Use enum CANType for valid parameter values * @param _format Data Format: Use enum CANFormat for valid parameter values */ - CANMessage(int _id, const char *_data, char _len = 8, CANType _type = CANData, CANFormat _format = CANStandard) { - len = _len & 0xF; - type = _type; - format = _format; - id = _id; - memcpy(data, _data, _len); + CANMessage(int _id, const char *_data, char _len = 8, CANType _type = CANData, CANFormat _format = CANStandard) + { + len = _len & 0xF; + type = _type; + format = _format; + id = _id; + memcpy(data, _data, _len); } /** Creates CAN remote message. @@ -67,12 +69,13 @@ public: * @param _id Message ID * @param _format Data Format: Use enum CANType for valid parameter values */ - CANMessage(int _id, CANFormat _format = CANStandard) { - len = 0; - type = CANRemote; - format = _format; - id = _id; - memset(data, 0, 8); + CANMessage(int _id, CANFormat _format = CANStandard) + { + len = 0; + type = CANRemote; + format = _format; + id = _id; + memset(data, 0, 8); } }; @@ -235,48 +238,50 @@ public: /** Attach a function to call whenever a CAN frame received interrupt is * generated. - * + * * This function locks the deep sleep while a callback is attached - * + * * @param func A pointer to a void function, or 0 to set as none * @param type Which CAN interrupt to attach the member function to (CAN::RxIrq for message received, CAN::TxIrq for transmitted or aborted, CAN::EwIrq for error warning, CAN::DoIrq for data overrun, CAN::WuIrq for wake-up, CAN::EpIrq for error passive, CAN::AlIrq for arbitration lost, CAN::BeIrq for bus error) */ - void attach(Callback func, IrqType type=RxIrq); + void attach(Callback func, IrqType type = RxIrq); - /** Attach a member function to call whenever a CAN frame received interrupt - * is generated. - * - * @param obj pointer to the object to call the member function on - * @param method pointer to the member function to be called - * @param type Which CAN interrupt to attach the member function to (CAN::RxIrq for message received, TxIrq for transmitted or aborted, EwIrq for error warning, DoIrq for data overrun, WuIrq for wake-up, EpIrq for error passive, AlIrq for arbitration lost, BeIrq for bus error) - * @deprecated - * The attach function does not support cv-qualifiers. Replaced by - * attach(callback(obj, method), type). - */ + /** Attach a member function to call whenever a CAN frame received interrupt + * is generated. + * + * @param obj pointer to the object to call the member function on + * @param method pointer to the member function to be called + * @param type Which CAN interrupt to attach the member function to (CAN::RxIrq for message received, TxIrq for transmitted or aborted, EwIrq for error warning, DoIrq for data overrun, WuIrq for wake-up, EpIrq for error passive, AlIrq for arbitration lost, BeIrq for bus error) + * @deprecated + * The attach function does not support cv-qualifiers. Replaced by + * attach(callback(obj, method), type). + */ template MBED_DEPRECATED_SINCE("mbed-os-5.1", - "The attach function does not support cv-qualifiers. Replaced by " - "attach(callback(obj, method), type).") - void attach(T* obj, void (T::*method)(), IrqType type=RxIrq) { + "The attach function does not support cv-qualifiers. Replaced by " + "attach(callback(obj, method), type).") + void attach(T *obj, void (T::*method)(), IrqType type = RxIrq) + { // Underlying call thread safe attach(callback(obj, method), type); } - /** Attach a member function to call whenever a CAN frame received interrupt - * is generated. - * - * @param obj pointer to the object to call the member function on - * @param method pointer to the member function to be called - * @param type Which CAN interrupt to attach the member function to (CAN::RxIrq for message received, TxIrq for transmitted or aborted, EwIrq for error warning, DoIrq for data overrun, WuIrq for wake-up, EpIrq for error passive, AlIrq for arbitration lost, BeIrq for bus error) - * @deprecated - * The attach function does not support cv-qualifiers. Replaced by - * attach(callback(obj, method), type). - */ + /** Attach a member function to call whenever a CAN frame received interrupt + * is generated. + * + * @param obj pointer to the object to call the member function on + * @param method pointer to the member function to be called + * @param type Which CAN interrupt to attach the member function to (CAN::RxIrq for message received, TxIrq for transmitted or aborted, EwIrq for error warning, DoIrq for data overrun, WuIrq for wake-up, EpIrq for error passive, AlIrq for arbitration lost, BeIrq for bus error) + * @deprecated + * The attach function does not support cv-qualifiers. Replaced by + * attach(callback(obj, method), type). + */ template MBED_DEPRECATED_SINCE("mbed-os-5.1", - "The attach function does not support cv-qualifiers. Replaced by " - "attach(callback(obj, method), type).") - void attach(T* obj, void (*method)(T*), IrqType type=RxIrq) { + "The attach function does not support cv-qualifiers. Replaced by " + "attach(callback(obj, method), type).") + void attach(T *obj, void (*method)(T *), IrqType type = RxIrq) + { // Underlying call thread safe attach(callback(obj, method), type); } diff --git a/drivers/DigitalIn.h b/drivers/DigitalIn.h index d35476c296..1a2af01fa3 100644 --- a/drivers/DigitalIn.h +++ b/drivers/DigitalIn.h @@ -55,7 +55,8 @@ public: * * @param pin DigitalIn pin to connect to */ - DigitalIn(PinName pin) : gpio() { + DigitalIn(PinName pin) : gpio() + { // No lock needed in the constructor gpio_init_in(&gpio, pin); } @@ -65,7 +66,8 @@ public: * @param pin DigitalIn pin to connect to * @param mode the initial mode of the pin */ - DigitalIn(PinName pin, PinMode mode) : gpio() { + DigitalIn(PinName pin, PinMode mode) : gpio() + { // No lock needed in the constructor gpio_init_in_ex(&gpio, pin, mode); } @@ -75,7 +77,8 @@ public: * An integer representing the state of the input pin, * 0 for logical 0, 1 for logical 1 */ - int read() { + int read() + { // Thread safe / atomic HAL call return gpio_read(&gpio); } @@ -84,7 +87,8 @@ public: * * @param pull PullUp, PullDown, PullNone, OpenDrain */ - void mode(PinMode pull) { + void mode(PinMode pull) + { core_util_critical_section_enter(); gpio_mode(&gpio, pull); core_util_critical_section_exit(); @@ -96,7 +100,8 @@ public: * Non zero value if pin is connected to uc GPIO * 0 if gpio object was initialized with NC */ - int is_connected() { + int is_connected() + { // Thread safe / atomic HAL call return gpio_is_connected(&gpio); } @@ -104,7 +109,8 @@ public: /** An operator shorthand for read() * \sa DigitalIn::read() */ - operator int() { + operator int() + { // Underlying read is thread safe return read(); } diff --git a/drivers/DigitalInOut.h b/drivers/DigitalInOut.h index cc524ff510..a11fcad2c0 100644 --- a/drivers/DigitalInOut.h +++ b/drivers/DigitalInOut.h @@ -36,7 +36,8 @@ public: * * @param pin DigitalInOut pin to connect to */ - DigitalInOut(PinName pin) : gpio() { + DigitalInOut(PinName pin) : gpio() + { // No lock needed in the constructor gpio_init_in(&gpio, pin); } @@ -48,7 +49,8 @@ public: * @param mode the initial mode of the pin * @param value the initial value of the pin if is an output */ - DigitalInOut(PinName pin, PinDirection direction, PinMode mode, int value) : gpio() { + DigitalInOut(PinName pin, PinDirection direction, PinMode mode, int value) : gpio() + { // No lock needed in the constructor gpio_init_inout(&gpio, pin, direction, mode, value); } @@ -58,7 +60,8 @@ public: * @param value An integer specifying the pin output value, * 0 for logical 0, 1 (or any other non-zero value) for logical 1 */ - void write(int value) { + void write(int value) + { // Thread safe / atomic HAL call gpio_write(&gpio, value); } @@ -69,14 +72,16 @@ public: * an integer representing the output setting of the pin if it is an output, * or read the input if set as an input */ - int read() { + int read() + { // Thread safe / atomic HAL call return gpio_read(&gpio); } /** Set as an output */ - void output() { + void output() + { core_util_critical_section_enter(); gpio_dir(&gpio, PIN_OUTPUT); core_util_critical_section_exit(); @@ -84,7 +89,8 @@ public: /** Set as an input */ - void input() { + void input() + { core_util_critical_section_enter(); gpio_dir(&gpio, PIN_INPUT); core_util_critical_section_exit(); @@ -94,7 +100,8 @@ public: * * @param pull PullUp, PullDown, PullNone, OpenDrain */ - void mode(PinMode pull) { + void mode(PinMode pull) + { core_util_critical_section_enter(); gpio_mode(&gpio, pull); core_util_critical_section_exit(); @@ -106,7 +113,8 @@ public: * Non zero value if pin is connected to uc GPIO * 0 if gpio object was initialized with NC */ - int is_connected() { + int is_connected() + { // Thread safe / atomic HAL call return gpio_is_connected(&gpio); } @@ -114,7 +122,8 @@ public: /** A shorthand for write() * \sa DigitalInOut::write() */ - DigitalInOut& operator= (int value) { + DigitalInOut &operator= (int value) + { // Underlying write is thread safe write(value); return *this; @@ -123,7 +132,8 @@ public: /** A shorthand for write() * \sa DigitalInOut::write() */ - DigitalInOut& operator= (DigitalInOut& rhs) { + DigitalInOut &operator= (DigitalInOut &rhs) + { core_util_critical_section_enter(); write(rhs.read()); core_util_critical_section_exit(); @@ -133,7 +143,8 @@ public: /** A shorthand for read() * \sa DigitalInOut::read() */ - operator int() { + operator int() + { // Underlying call is thread safe return read(); } diff --git a/drivers/DigitalOut.h b/drivers/DigitalOut.h index 06c57359e0..fb6d1be6c2 100644 --- a/drivers/DigitalOut.h +++ b/drivers/DigitalOut.h @@ -50,7 +50,8 @@ public: * * @param pin DigitalOut pin to connect to */ - DigitalOut(PinName pin) : gpio() { + DigitalOut(PinName pin) : gpio() + { // No lock needed in the constructor gpio_init_out(&gpio, pin); } @@ -60,7 +61,8 @@ public: * @param pin DigitalOut pin to connect to * @param value the initial pin value */ - DigitalOut(PinName pin, int value) : gpio() { + DigitalOut(PinName pin, int value) : gpio() + { // No lock needed in the constructor gpio_init_out_ex(&gpio, pin, value); } @@ -70,7 +72,8 @@ public: * @param value An integer specifying the pin output value, * 0 for logical 0, 1 (or any other non-zero value) for logical 1 */ - void write(int value) { + void write(int value) + { // Thread safe / atomic HAL call gpio_write(&gpio, value); } @@ -81,7 +84,8 @@ public: * an integer representing the output setting of the pin, * 0 for logical 0, 1 for logical 1 */ - int read() { + int read() + { // Thread safe / atomic HAL call return gpio_read(&gpio); } @@ -92,7 +96,8 @@ public: * Non zero value if pin is connected to uc GPIO * 0 if gpio object was initialized with NC */ - int is_connected() { + int is_connected() + { // Thread safe / atomic HAL call return gpio_is_connected(&gpio); } @@ -100,7 +105,8 @@ public: /** A shorthand for write() * \sa DigitalOut::write() */ - DigitalOut& operator= (int value) { + DigitalOut &operator= (int value) + { // Underlying write is thread safe write(value); return *this; @@ -109,7 +115,8 @@ public: /** A shorthand for write() * \sa DigitalOut::write() */ - DigitalOut& operator= (DigitalOut& rhs) { + DigitalOut &operator= (DigitalOut &rhs) + { core_util_critical_section_enter(); write(rhs.read()); core_util_critical_section_exit(); @@ -119,7 +126,8 @@ public: /** A shorthand for read() * \sa DigitalOut::read() */ - operator int() { + operator int() + { // Underlying call is thread safe return read(); } diff --git a/drivers/Ethernet.cpp b/drivers/Ethernet.cpp index c275f4a99b..6b0a2bfab6 100644 --- a/drivers/Ethernet.cpp +++ b/drivers/Ethernet.cpp @@ -21,48 +21,72 @@ namespace mbed { -Ethernet::Ethernet() { +Ethernet::Ethernet() +{ ethernet_init(); } -Ethernet::~Ethernet() { +Ethernet::~Ethernet() +{ ethernet_free(); } -int Ethernet::write(const char *data, int size) { +int Ethernet::write(const char *data, int size) +{ return ethernet_write(data, size); } -int Ethernet::send() { +int Ethernet::send() +{ return ethernet_send(); } -int Ethernet::receive() { +int Ethernet::receive() +{ return ethernet_receive(); } -int Ethernet::read(char *data, int size) { +int Ethernet::read(char *data, int size) +{ return ethernet_read(data, size); } -void Ethernet::address(char *mac) { +void Ethernet::address(char *mac) +{ return ethernet_address(mac); } -int Ethernet::link() { +int Ethernet::link() +{ return ethernet_link(); } -void Ethernet::set_link(Mode mode) { +void Ethernet::set_link(Mode mode) +{ int speed = -1; int duplex = 0; - switch(mode) { - case AutoNegotiate : speed = -1; duplex = 0; break; - case HalfDuplex10 : speed = 0; duplex = 0; break; - case FullDuplex10 : speed = 0; duplex = 1; break; - case HalfDuplex100 : speed = 1; duplex = 0; break; - case FullDuplex100 : speed = 1; duplex = 1; break; + switch (mode) { + case AutoNegotiate : + speed = -1; + duplex = 0; + break; + case HalfDuplex10 : + speed = 0; + duplex = 0; + break; + case FullDuplex10 : + speed = 0; + duplex = 1; + break; + case HalfDuplex100 : + speed = 1; + duplex = 0; + break; + case FullDuplex100 : + speed = 1; + duplex = 1; + break; } ethernet_set_link(speed, duplex); diff --git a/drivers/FlashIAP.cpp b/drivers/FlashIAP.cpp index 3c2009ef96..8f71061472 100644 --- a/drivers/FlashIAP.cpp +++ b/drivers/FlashIAP.cpp @@ -99,7 +99,7 @@ int FlashIAP::program(const void *buffer, uint32_t addr, uint32_t size) // addr should be aligned to page size if (!is_aligned(addr, page_size) || (!buffer) || - ((addr + size) > (flash_start_addr + flash_size))) { + ((addr + size) > (flash_start_addr + flash_size))) { return -1; } @@ -143,7 +143,7 @@ bool FlashIAP::is_aligned_to_sector(uint32_t addr, uint32_t size) { uint32_t current_sector_size = flash_get_sector_size(&_flash, addr); if (!is_aligned(size, current_sector_size) || - !is_aligned(addr, current_sector_size)) { + !is_aligned(addr, current_sector_size)) { return false; } else { return true; @@ -160,7 +160,7 @@ int FlashIAP::erase(uint32_t addr, uint32_t size) if (erase_end_addr > flash_end_addr) { return -1; - } else if (erase_end_addr < flash_end_addr){ + } else if (erase_end_addr < flash_end_addr) { uint32_t following_sector_size = flash_get_sector_size(&_flash, erase_end_addr); if (!is_aligned(erase_end_addr, following_sector_size)) { return -1; diff --git a/drivers/FlashIAP.h b/drivers/FlashIAP.h index a818410c55..92a9399676 100644 --- a/drivers/FlashIAP.h +++ b/drivers/FlashIAP.h @@ -56,7 +56,7 @@ public: */ int deinit(); - /** Read data from a flash device. + /** Read data from a flash device. * * This method invokes memcpy - reads number of bytes from the address * @@ -90,7 +90,7 @@ public: /** Get the sector size at the defined address * - * Sector size might differ at address ranges. + * Sector size might differ at address ranges. * An example <0-0x1000, sector size=1024; 0x10000-0x20000, size=2048> * * @param addr Address of or inside the sector to query @@ -98,15 +98,15 @@ public: */ uint32_t get_sector_size(uint32_t addr) const; - /** Get the flash start address + /** Get the flash start address * - * @return Flash start address + * @return Flash start address */ uint32_t get_flash_start() const; /** Get the flash size * - * @return Flash size + * @return Flash size */ uint32_t get_flash_size() const; diff --git a/drivers/I2C.cpp b/drivers/I2C.cpp index 148d48ee38..08d8d2320f 100644 --- a/drivers/I2C.cpp +++ b/drivers/I2C.cpp @@ -41,7 +41,8 @@ I2C::I2C(PinName sda, PinName scl) : _owner = this; } -void I2C::frequency(int hz) { +void I2C::frequency(int hz) +{ lock(); _hz = hz; @@ -53,7 +54,8 @@ void I2C::frequency(int hz) { unlock(); } -void I2C::aquire() { +void I2C::aquire() +{ lock(); if (_owner != this) { i2c_frequency(&_i2c, _hz); @@ -63,7 +65,8 @@ void I2C::aquire() { } // write - Master Transmitter Mode -int I2C::write(int address, const char* data, int length, bool repeated) { +int I2C::write(int address, const char *data, int length, bool repeated) +{ lock(); aquire(); @@ -74,7 +77,8 @@ int I2C::write(int address, const char* data, int length, bool repeated) { return length != written; } -int I2C::write(int data) { +int I2C::write(int data) +{ lock(); int ret = i2c_byte_write(&_i2c, data); unlock(); @@ -82,7 +86,8 @@ int I2C::write(int data) { } // read - Master Receiver Mode -int I2C::read(int address, char* data, int length, bool repeated) { +int I2C::read(int address, char *data, int length, bool repeated) +{ lock(); aquire(); @@ -93,7 +98,8 @@ int I2C::read(int address, char* data, int length, bool repeated) { return length != read; } -int I2C::read(int ack) { +int I2C::read(int ack) +{ lock(); int ret; if (ack) { @@ -105,29 +111,33 @@ int I2C::read(int ack) { return ret; } -void I2C::start(void) { +void I2C::start(void) +{ lock(); i2c_start(&_i2c); unlock(); } -void I2C::stop(void) { +void I2C::stop(void) +{ lock(); i2c_stop(&_i2c); unlock(); } -void I2C::lock() { +void I2C::lock() +{ _mutex->lock(); } -void I2C::unlock() { +void I2C::unlock() +{ _mutex->unlock(); } #if DEVICE_I2C_ASYNCH -int I2C::transfer(int address, const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length, const event_callback_t& callback, int event, bool repeated) +int I2C::transfer(int address, const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length, const event_callback_t &callback, int event, bool repeated) { lock(); if (i2c_active(&_i2c)) { diff --git a/drivers/I2C.h b/drivers/I2C.h index a4b0598d29..13f43186d6 100644 --- a/drivers/I2C.h +++ b/drivers/I2C.h @@ -151,7 +151,8 @@ public: */ virtual void unlock(void); - virtual ~I2C() { + virtual ~I2C() + { // Do nothing } @@ -160,7 +161,7 @@ public: /** Start non-blocking I2C transfer. * * This function locks the deep sleep until any event has occurred - * + * * @param address 8/10 bit I2C slave address * @param tx_buffer The TX buffer with data to be transfered * @param tx_length The length of TX buffer in bytes @@ -171,13 +172,13 @@ public: * @param repeated Repeated start, true - do not send stop at end * @return Zero if the transfer has started, or -1 if I2C peripheral is busy */ - int transfer(int address, const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length, const event_callback_t& callback, int event = I2C_EVENT_TRANSFER_COMPLETE, bool repeated = false); + int transfer(int address, const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length, const event_callback_t &callback, int event = I2C_EVENT_TRANSFER_COMPLETE, bool repeated = false); /** Abort the on-going I2C transfer */ void abort_transfer(); - protected: +protected: /** Lock deep sleep only if it is not yet locked */ void lock_deep_sleep(); diff --git a/drivers/I2CSlave.cpp b/drivers/I2CSlave.cpp index f168c2a6c7..b29b4e74e2 100644 --- a/drivers/I2CSlave.cpp +++ b/drivers/I2CSlave.cpp @@ -19,42 +19,51 @@ namespace mbed { -I2CSlave::I2CSlave(PinName sda, PinName scl) : _i2c() { +I2CSlave::I2CSlave(PinName sda, PinName scl) : _i2c() +{ i2c_init(&_i2c, sda, scl); i2c_frequency(&_i2c, 100000); i2c_slave_mode(&_i2c, 1); } -void I2CSlave::frequency(int hz) { +void I2CSlave::frequency(int hz) +{ i2c_frequency(&_i2c, hz); } -void I2CSlave::address(int address) { +void I2CSlave::address(int address) +{ int addr = (address & 0xFF) | 1; i2c_slave_address(&_i2c, 0, addr, 0); } -int I2CSlave::receive(void) { +int I2CSlave::receive(void) +{ return i2c_slave_receive(&_i2c); } -int I2CSlave::read(char *data, int length) { +int I2CSlave::read(char *data, int length) +{ return i2c_slave_read(&_i2c, data, length) != length; } -int I2CSlave::read(void) { +int I2CSlave::read(void) +{ return i2c_byte_read(&_i2c, 0); } -int I2CSlave::write(const char *data, int length) { +int I2CSlave::write(const char *data, int length) +{ return i2c_slave_write(&_i2c, data, length) != length; } -int I2CSlave::write(int data) { +int I2CSlave::write(int data) +{ return i2c_byte_write(&_i2c, data); } -void I2CSlave::stop(void) { +void I2CSlave::stop(void) +{ i2c_stop(&_i2c); } diff --git a/drivers/InterruptIn.cpp b/drivers/InterruptIn.cpp index 28f90419ad..51d947f7a3 100644 --- a/drivers/InterruptIn.cpp +++ b/drivers/InterruptIn.cpp @@ -24,45 +24,52 @@ namespace mbed { // If not for that, we could simplify by having only the 2-param // constructor, with a default value for the PinMode. InterruptIn::InterruptIn(PinName pin) : gpio(), - gpio_irq(), - _rise(NULL), - _fall(NULL) { + gpio_irq(), + _rise(NULL), + _fall(NULL) +{ // No lock needed in the constructor irq_init(pin); gpio_init_in(&gpio, pin); } InterruptIn::InterruptIn(PinName pin, PinMode mode) : - gpio(), - gpio_irq(), - _rise(NULL), - _fall(NULL) { + gpio(), + gpio_irq(), + _rise(NULL), + _fall(NULL) +{ // No lock needed in the constructor irq_init(pin); gpio_init_in_ex(&gpio, pin, mode); } -void InterruptIn::irq_init(PinName pin) { - gpio_irq_init(&gpio_irq, pin, (&InterruptIn::_irq_handler), (uint32_t)this); +void InterruptIn::irq_init(PinName pin) +{ + gpio_irq_init(&gpio_irq, pin, (&InterruptIn::_irq_handler), (uint32_t)this); } -InterruptIn::~InterruptIn() { +InterruptIn::~InterruptIn() +{ // No lock needed in the destructor gpio_irq_free(&gpio_irq); } -int InterruptIn::read() { +int InterruptIn::read() +{ // Read only return gpio_read(&gpio); } -void InterruptIn::mode(PinMode pull) { +void InterruptIn::mode(PinMode pull) +{ core_util_critical_section_enter(); gpio_mode(&gpio, pull); core_util_critical_section_exit(); } -void InterruptIn::rise(Callback func) { +void InterruptIn::rise(Callback func) +{ core_util_critical_section_enter(); if (func) { _rise = func; @@ -74,7 +81,8 @@ void InterruptIn::rise(Callback func) { core_util_critical_section_exit(); } -void InterruptIn::fall(Callback func) { +void InterruptIn::fall(Callback func) +{ core_util_critical_section_enter(); if (func) { _fall = func; @@ -86,36 +94,41 @@ void InterruptIn::fall(Callback func) { core_util_critical_section_exit(); } -void InterruptIn::_irq_handler(uint32_t id, gpio_irq_event event) { - InterruptIn *handler = (InterruptIn*)id; +void InterruptIn::_irq_handler(uint32_t id, gpio_irq_event event) +{ + InterruptIn *handler = (InterruptIn *)id; switch (event) { - case IRQ_RISE: + case IRQ_RISE: if (handler->_rise) { handler->_rise(); } break; - case IRQ_FALL: + case IRQ_FALL: if (handler->_fall) { - handler->_fall(); + handler->_fall(); } break; - case IRQ_NONE: break; + case IRQ_NONE: + break; } } -void InterruptIn::enable_irq() { +void InterruptIn::enable_irq() +{ core_util_critical_section_enter(); gpio_irq_enable(&gpio_irq); core_util_critical_section_exit(); } -void InterruptIn::disable_irq() { +void InterruptIn::disable_irq() +{ core_util_critical_section_enter(); gpio_irq_disable(&gpio_irq); core_util_critical_section_exit(); } -InterruptIn::operator int() { +InterruptIn::operator int() +{ // Underlying call is atomic return read(); } diff --git a/drivers/InterruptIn.h b/drivers/InterruptIn.h index e226d31993..ebc8c84fac 100644 --- a/drivers/InterruptIn.h +++ b/drivers/InterruptIn.h @@ -106,9 +106,10 @@ public: */ template MBED_DEPRECATED_SINCE("mbed-os-5.1", - "The rise function does not support cv-qualifiers. Replaced by " - "rise(callback(obj, method)).") - void rise(T *obj, M method) { + "The rise function does not support cv-qualifiers. Replaced by " + "rise(callback(obj, method)).") + void rise(T *obj, M method) + { core_util_critical_section_enter(); rise(callback(obj, method)); core_util_critical_section_exit(); @@ -130,9 +131,10 @@ public: */ template MBED_DEPRECATED_SINCE("mbed-os-5.1", - "The fall function does not support cv-qualifiers. Replaced by " - "fall(callback(obj, method)).") - void fall(T *obj, M method) { + "The fall function does not support cv-qualifiers. Replaced by " + "fall(callback(obj, method)).") + void fall(T *obj, M method) + { core_util_critical_section_enter(); fall(callback(obj, method)); core_util_critical_section_exit(); diff --git a/drivers/InterruptManager.cpp b/drivers/InterruptManager.cpp index 9c2b4c2acb..f557cf4378 100644 --- a/drivers/InterruptManager.cpp +++ b/drivers/InterruptManager.cpp @@ -32,12 +32,13 @@ namespace mbed { typedef void (*pvoidf)(void); -InterruptManager* InterruptManager::_instance = (InterruptManager*)NULL; +InterruptManager *InterruptManager::_instance = (InterruptManager *)NULL; -InterruptManager* InterruptManager::get() { +InterruptManager *InterruptManager::get() +{ if (NULL == _instance) { - InterruptManager* temp = new InterruptManager(); + InterruptManager *temp = new InterruptManager(); // Atomically set _instance core_util_critical_section_enter(); @@ -55,28 +56,33 @@ InterruptManager* InterruptManager::get() { return _instance; } -InterruptManager::InterruptManager() { +InterruptManager::InterruptManager() +{ // No mutex needed in constructor - memset(_chains, 0, NVIC_NUM_VECTORS * sizeof(CallChain*)); + memset(_chains, 0, NVIC_NUM_VECTORS * sizeof(CallChain *)); } -void InterruptManager::destroy() { +void InterruptManager::destroy() +{ // Not a good idea to call this unless NO interrupt at all // is under the control of the handler; otherwise, a system crash // is very likely to occur if (NULL != _instance) { delete _instance; - _instance = (InterruptManager*)NULL; + _instance = (InterruptManager *)NULL; } } -InterruptManager::~InterruptManager() { - for(int i = 0; i < NVIC_NUM_VECTORS; i++) - if (NULL != _chains[i]) +InterruptManager::~InterruptManager() +{ + for (int i = 0; i < NVIC_NUM_VECTORS; i++) + if (NULL != _chains[i]) { delete _chains[i]; + } } -bool InterruptManager::must_replace_vector(IRQn_Type irq) { +bool InterruptManager::must_replace_vector(IRQn_Type irq) +{ lock(); int ret = false; @@ -90,19 +96,22 @@ bool InterruptManager::must_replace_vector(IRQn_Type irq) { return ret; } -pFunctionPointer_t InterruptManager::add_common(void (*function)(void), IRQn_Type irq, bool front) { +pFunctionPointer_t InterruptManager::add_common(void (*function)(void), IRQn_Type irq, bool front) +{ lock(); int irq_pos = get_irq_index(irq); bool change = must_replace_vector(irq); pFunctionPointer_t pf = front ? _chains[irq_pos]->add_front(function) : _chains[irq_pos]->add(function); - if (change) + if (change) { NVIC_SetVector(irq, (uint32_t)&InterruptManager::static_irq_helper); + } unlock(); return pf; } -bool InterruptManager::remove_handler(pFunctionPointer_t handler, IRQn_Type irq) { +bool InterruptManager::remove_handler(pFunctionPointer_t handler, IRQn_Type irq) +{ int irq_pos = get_irq_index(irq); bool ret = false; @@ -117,24 +126,29 @@ bool InterruptManager::remove_handler(pFunctionPointer_t handler, IRQn_Type irq) return ret; } -void InterruptManager::irq_helper() { +void InterruptManager::irq_helper() +{ _chains[__get_IPSR()]->call(); } -int InterruptManager::get_irq_index(IRQn_Type irq) { +int InterruptManager::get_irq_index(IRQn_Type irq) +{ // Pure function - no lock needed return (int)irq + NVIC_USER_IRQ_OFFSET; } -void InterruptManager::static_irq_helper() { +void InterruptManager::static_irq_helper() +{ InterruptManager::get()->irq_helper(); } -void InterruptManager::lock() { +void InterruptManager::lock() +{ _mutex.lock(); } -void InterruptManager::unlock() { +void InterruptManager::unlock() +{ _mutex.unlock(); } diff --git a/drivers/InterruptManager.h b/drivers/InterruptManager.h index b787c4bda7..c65fb683e8 100644 --- a/drivers/InterruptManager.h +++ b/drivers/InterruptManager.h @@ -58,26 +58,26 @@ namespace mbed { class InterruptManager : private NonCopyable { public: /** Get the instance of InterruptManager Class - * @deprecated + * @deprecated * Do not use this function, this class is not part of the public API of mbed-os and is being removed in the future. * * @return the only instance of this class */ MBED_DEPRECATED_SINCE("mbed-os-5.6", "This class is not part of the " - "public API of mbed-os and is being removed in the future.") - static InterruptManager* get(); + "public API of mbed-os and is being removed in the future.") + static InterruptManager *get(); /** Destroy the current instance of the interrupt manager - * @deprecated + * @deprecated * Do not use this function, this class is not part of the public API of mbed-os and is being removed in the future. * */ MBED_DEPRECATED_SINCE("mbed-os-5.6", "This class is not part of the " - "public API of mbed-os and is being removed in the future.") + "public API of mbed-os and is being removed in the future.") static void destroy(); /** Add a handler for an interrupt at the end of the handler list - * @deprecated + * @deprecated * Do not use this function, this class is not part of the public API of mbed-os and is being removed in the future. * * @param function the handler to add @@ -87,14 +87,15 @@ public: * The function object created for 'function' */ MBED_DEPRECATED_SINCE("mbed-os-5.6", "This class is not part of the " - "public API of mbed-os and is being removed in the future.") - pFunctionPointer_t add_handler(void (*function)(void), IRQn_Type irq) { + "public API of mbed-os and is being removed in the future.") + pFunctionPointer_t add_handler(void (*function)(void), IRQn_Type irq) + { // Underlying call is thread safe return add_common(function, irq); } /** Add a handler for an interrupt at the beginning of the handler list - * @deprecated + * @deprecated * Do not use this function, this class is not part of the public API of mbed-os and is being removed in the future. * * @param function the handler to add @@ -104,14 +105,15 @@ public: * The function object created for 'function' */ MBED_DEPRECATED_SINCE("mbed-os-5.6", "This class is not part of the " - "public API of mbed-os and is being removed in the future.") - pFunctionPointer_t add_handler_front(void (*function)(void), IRQn_Type irq) { + "public API of mbed-os and is being removed in the future.") + pFunctionPointer_t add_handler_front(void (*function)(void), IRQn_Type irq) + { // Underlying call is thread safe return add_common(function, irq, true); } /** Add a handler for an interrupt at the end of the handler list - * @deprecated + * @deprecated * Do not use this function, this class is not part of the public API of mbed-os and is being removed in the future. * * @param tptr pointer to the object that has the handler function @@ -123,14 +125,15 @@ public: */ template MBED_DEPRECATED_SINCE("mbed-os-5.6", "This class is not part of the " - "public API of mbed-os and is being removed in the future.") - pFunctionPointer_t add_handler(T* tptr, void (T::*mptr)(void), IRQn_Type irq) { + "public API of mbed-os and is being removed in the future.") + pFunctionPointer_t add_handler(T *tptr, void (T::*mptr)(void), IRQn_Type irq) + { // Underlying call is thread safe return add_common(tptr, mptr, irq); } /** Add a handler for an interrupt at the beginning of the handler list - * @deprecated + * @deprecated * Do not use this function, this class is not part of the public API of mbed-os and is being removed in the future. * * @param tptr pointer to the object that has the handler function @@ -142,14 +145,15 @@ public: */ template MBED_DEPRECATED_SINCE("mbed-os-5.6", "This class is not part of the " - "public API of mbed-os and is being removed in the future.") - pFunctionPointer_t add_handler_front(T* tptr, void (T::*mptr)(void), IRQn_Type irq) { + "public API of mbed-os and is being removed in the future.") + pFunctionPointer_t add_handler_front(T *tptr, void (T::*mptr)(void), IRQn_Type irq) + { // Underlying call is thread safe return add_common(tptr, mptr, irq, true); } /** Remove a handler from an interrupt - * @deprecated + * @deprecated * Do not use this function, this class is not part of the public API of mbed-os and is being removed in the future. * * @param handler the function object for the handler to remove @@ -159,7 +163,7 @@ public: * true if the handler was found and removed, false otherwise */ MBED_DEPRECATED_SINCE("mbed-os-5.6", "This class is not part of the " - "public API of mbed-os and is being removed in the future.") + "public API of mbed-os and is being removed in the future.") bool remove_handler(pFunctionPointer_t handler, IRQn_Type irq); private: @@ -170,27 +174,29 @@ private: void unlock(); template - pFunctionPointer_t add_common(T *tptr, void (T::*mptr)(void), IRQn_Type irq, bool front=false) { + pFunctionPointer_t add_common(T *tptr, void (T::*mptr)(void), IRQn_Type irq, bool front = false) + { _mutex.lock(); int irq_pos = get_irq_index(irq); bool change = must_replace_vector(irq); pFunctionPointer_t pf = front ? _chains[irq_pos]->add_front(tptr, mptr) : _chains[irq_pos]->add(tptr, mptr); - if (change) + if (change) { NVIC_SetVector(irq, (uint32_t)&InterruptManager::static_irq_helper); + } _mutex.unlock(); return pf; } - pFunctionPointer_t add_common(void (*function)(void), IRQn_Type irq, bool front=false); + pFunctionPointer_t add_common(void (*function)(void), IRQn_Type irq, bool front = false); bool must_replace_vector(IRQn_Type irq); int get_irq_index(IRQn_Type irq); void irq_helper(); - void add_helper(void (*function)(void), IRQn_Type irq, bool front=false); + void add_helper(void (*function)(void), IRQn_Type irq, bool front = false); static void static_irq_helper(); - CallChain* _chains[NVIC_NUM_VECTORS]; - static InterruptManager* _instance; + CallChain *_chains[NVIC_NUM_VECTORS]; + static InterruptManager *_instance; PlatformMutex _mutex; }; diff --git a/drivers/LowPowerTicker.h b/drivers/LowPowerTicker.h index 9a4caf8218..a77307e68b 100644 --- a/drivers/LowPowerTicker.h +++ b/drivers/LowPowerTicker.h @@ -35,10 +35,12 @@ namespace mbed { class LowPowerTicker : public Ticker, private NonCopyable { public: - LowPowerTicker() : Ticker(get_lp_ticker_data()) { + LowPowerTicker() : Ticker(get_lp_ticker_data()) + { } - virtual ~LowPowerTicker() { + virtual ~LowPowerTicker() + { } }; diff --git a/drivers/LowPowerTimeout.h b/drivers/LowPowerTimeout.h index ef96006738..7c9d00e384 100644 --- a/drivers/LowPowerTimeout.h +++ b/drivers/LowPowerTimeout.h @@ -35,7 +35,8 @@ namespace mbed { class LowPowerTimeout : public LowPowerTicker, private NonCopyable { private: - virtual void handler(void) { + virtual void handler(void) + { _function.call(); } }; diff --git a/drivers/LowPowerTimer.h b/drivers/LowPowerTimer.h index 20959d6721..19bb8fdc3e 100644 --- a/drivers/LowPowerTimer.h +++ b/drivers/LowPowerTimer.h @@ -35,7 +35,8 @@ namespace mbed { class LowPowerTimer : public Timer, private NonCopyable { public: - LowPowerTimer() : Timer(get_lp_ticker_data()) { + LowPowerTimer() : Timer(get_lp_ticker_data()) + { } }; diff --git a/drivers/MbedCRC.cpp b/drivers/MbedCRC.cpp index bece2e2366..cc90945ac0 100644 --- a/drivers/MbedCRC.cpp +++ b/drivers/MbedCRC.cpp @@ -26,40 +26,40 @@ namespace mbed { */ template<> MbedCRC::MbedCRC(uint32_t initial_xor, uint32_t final_xor, bool reflect_data, bool reflect_remainder): - _initial_value(initial_xor), _final_xor(final_xor), _reflect_data(reflect_data), _reflect_remainder(reflect_remainder), - _crc_table((uint32_t *)Table_CRC_32bit_ANSI) + _initial_value(initial_xor), _final_xor(final_xor), _reflect_data(reflect_data), _reflect_remainder(reflect_remainder), + _crc_table((uint32_t *)Table_CRC_32bit_ANSI) { mbed_crc_ctor(); } template<> MbedCRC::MbedCRC(uint32_t initial_xor, uint32_t final_xor, bool reflect_data, bool reflect_remainder): - _initial_value(initial_xor), _final_xor(final_xor), _reflect_data(reflect_data), _reflect_remainder(reflect_remainder), - _crc_table((uint32_t *)Table_CRC_8bit_CCITT) + _initial_value(initial_xor), _final_xor(final_xor), _reflect_data(reflect_data), _reflect_remainder(reflect_remainder), + _crc_table((uint32_t *)Table_CRC_8bit_CCITT) { mbed_crc_ctor(); } template<> MbedCRC::MbedCRC(uint32_t initial_xor, uint32_t final_xor, bool reflect_data, bool reflect_remainder): - _initial_value(initial_xor), _final_xor(final_xor), _reflect_data(reflect_data), _reflect_remainder(reflect_remainder), - _crc_table((uint32_t *)Table_CRC_7Bit_SD) + _initial_value(initial_xor), _final_xor(final_xor), _reflect_data(reflect_data), _reflect_remainder(reflect_remainder), + _crc_table((uint32_t *)Table_CRC_7Bit_SD) { mbed_crc_ctor(); } template<> MbedCRC::MbedCRC(uint32_t initial_xor, uint32_t final_xor, bool reflect_data, bool reflect_remainder): - _initial_value(initial_xor), _final_xor(final_xor), _reflect_data(reflect_data), _reflect_remainder(reflect_remainder), - _crc_table((uint32_t *)Table_CRC_16bit_CCITT) + _initial_value(initial_xor), _final_xor(final_xor), _reflect_data(reflect_data), _reflect_remainder(reflect_remainder), + _crc_table((uint32_t *)Table_CRC_16bit_CCITT) { mbed_crc_ctor(); } template<> MbedCRC::MbedCRC(uint32_t initial_xor, uint32_t final_xor, bool reflect_data, bool reflect_remainder): - _initial_value(initial_xor), _final_xor(final_xor), _reflect_data(reflect_data), _reflect_remainder(reflect_remainder), - _crc_table((uint32_t *)Table_CRC_16bit_IBM) + _initial_value(initial_xor), _final_xor(final_xor), _reflect_data(reflect_data), _reflect_remainder(reflect_remainder), + _crc_table((uint32_t *)Table_CRC_16bit_IBM) { mbed_crc_ctor(); } diff --git a/drivers/MbedCRC.h b/drivers/MbedCRC.h index 3c37fba85b..1221c4fe0b 100644 --- a/drivers/MbedCRC.h +++ b/drivers/MbedCRC.h @@ -92,9 +92,8 @@ namespace mbed { * @ingroup drivers */ -template -class MbedCRC -{ +template +class MbedCRC { public: enum CrcMode { HARDWARE = 0, TABLE, BITWISE }; @@ -107,7 +106,7 @@ public: * @param final_xor Final Xor value * @param reflect_data * @param reflect_remainder -* @note Default constructor without any arguments is valid only for supported CRC polynomials. :: crc_polynomial_t + * @note Default constructor without any arguments is valid only for supported CRC polynomials. :: crc_polynomial_t * MbedCRC ct; --- Valid POLY_7BIT_SD * MbedCRC <0x1021, 16> ct; --- Valid POLY_16BIT_CCITT * MbedCRC ct; --- Invalid, compilation error @@ -117,8 +116,8 @@ public: * */ MbedCRC(uint32_t initial_xor, uint32_t final_xor, bool reflect_data, bool reflect_remainder) : - _initial_value(initial_xor), _final_xor(final_xor), _reflect_data(reflect_data), - _reflect_remainder(reflect_remainder), _crc_table(NULL) + _initial_value(initial_xor), _final_xor(final_xor), _reflect_data(reflect_data), + _reflect_remainder(reflect_remainder), _crc_table(NULL) { mbed_crc_ctor(); } @@ -170,8 +169,7 @@ public: */ int32_t compute_partial(void *buffer, crc_data_size_t size, uint32_t *crc) { - switch (_mode) - { + switch (_mode) { case HARDWARE: #ifdef DEVICE_CRC hal_crc_compute_partial((uint8_t *)buffer, size); @@ -332,7 +330,7 @@ private: */ uint32_t reflect_bytes(uint32_t data) const { - if(_reflect_data) { + if (_reflect_data) { uint32_t reflection = 0x0; for (uint8_t bit = 0; bit < 8; ++bit) { @@ -368,7 +366,7 @@ private: data_byte = reflect_bytes(data[byte]); for (uint8_t bit = 8; bit > 0; --bit) { p_crc <<= 1; - if (( data_byte ^ p_crc) & get_top_bit()) { + if ((data_byte ^ p_crc) & get_top_bit()) { p_crc ^= polynomial; } data_byte <<= 1; @@ -392,13 +390,13 @@ private: return 0; } - /** CRC computation using ROM tables - * - * @param buffer data buffer - * @param size size of the data - * @param crc CRC value is filled in, but the value is not the final - * @return 0 on success or a negative error code on failure - */ + /** CRC computation using ROM tables + * + * @param buffer data buffer + * @param size size of the data + * @param crc CRC value is filled in, but the value is not the final + * @return 0 on success or a negative error code on failure + */ int32_t table_compute_partial(const void *buffer, crc_data_size_t size, uint32_t *crc) const { MBED_ASSERT(crc != NULL); diff --git a/drivers/PortIn.h b/drivers/PortIn.h index 2796916fe2..62e48e1c19 100644 --- a/drivers/PortIn.h +++ b/drivers/PortIn.h @@ -60,7 +60,8 @@ public: * @param port Port to connect to (Port0-Port5) * @param mask A bitmask to identify which bits in the port should be included (0 - ignore) */ - PortIn(PortName port, int mask = 0xFFFFFFFF) { + PortIn(PortName port, int mask = 0xFFFFFFFF) + { core_util_critical_section_enter(); port_init(&_port, port, mask, PIN_INPUT); core_util_critical_section_exit(); @@ -71,7 +72,8 @@ public: * @returns * An integer with each bit corresponding to associated port pin setting */ - int read() { + int read() + { return port_read(&_port); } @@ -79,7 +81,8 @@ public: * * @param mode PullUp, PullDown, PullNone, OpenDrain */ - void mode(PinMode mode) { + void mode(PinMode mode) + { core_util_critical_section_enter(); port_mode(&_port, mode); core_util_critical_section_exit(); @@ -87,7 +90,8 @@ public: /** A shorthand for read() */ - operator int() { + operator int() + { return read(); } diff --git a/drivers/PortInOut.h b/drivers/PortInOut.h index 4eb9ecb729..00c972c2a0 100644 --- a/drivers/PortInOut.h +++ b/drivers/PortInOut.h @@ -39,7 +39,8 @@ public: * @param port Port to connect to (Port0-Port5) * @param mask A bitmask to identify which bits in the port should be included (0 - ignore) */ - PortInOut(PortName port, int mask = 0xFFFFFFFF) { + PortInOut(PortName port, int mask = 0xFFFFFFFF) + { core_util_critical_section_enter(); port_init(&_port, port, mask, PIN_INPUT); core_util_critical_section_exit(); @@ -49,7 +50,8 @@ public: * * @param value An integer specifying a bit to write for every corresponding port pin */ - void write(int value) { + void write(int value) + { port_write(&_port, value); } @@ -58,13 +60,15 @@ public: * @returns * An integer with each bit corresponding to associated port pin setting */ - int read() { + int read() + { return port_read(&_port); } /** Set as an output */ - void output() { + void output() + { core_util_critical_section_enter(); port_dir(&_port, PIN_OUTPUT); core_util_critical_section_exit(); @@ -72,7 +76,8 @@ public: /** Set as an input */ - void input() { + void input() + { core_util_critical_section_enter(); port_dir(&_port, PIN_INPUT); core_util_critical_section_exit(); @@ -82,7 +87,8 @@ public: * * @param mode PullUp, PullDown, PullNone, OpenDrain */ - void mode(PinMode mode) { + void mode(PinMode mode) + { core_util_critical_section_enter(); port_mode(&_port, mode); core_util_critical_section_exit(); @@ -91,7 +97,8 @@ public: /** A shorthand for write() * \sa PortInOut::write() */ - PortInOut& operator= (int value) { + PortInOut &operator= (int value) + { write(value); return *this; } @@ -99,7 +106,8 @@ public: /** A shorthand for write() * \sa PortInOut::write() */ - PortInOut& operator= (PortInOut& rhs) { + PortInOut &operator= (PortInOut &rhs) + { write(rhs.read()); return *this; } @@ -107,7 +115,8 @@ public: /** A shorthand for read() * \sa PortInOut::read() */ - operator int() { + operator int() + { return read(); } diff --git a/drivers/PortOut.h b/drivers/PortOut.h index 9b8a76b60c..ccdcc25dbf 100644 --- a/drivers/PortOut.h +++ b/drivers/PortOut.h @@ -59,7 +59,8 @@ public: * @param port Port to connect to (Port0-Port5) * @param mask A bitmask to identify which bits in the port should be included (0 - ignore) */ - PortOut(PortName port, int mask = 0xFFFFFFFF) { + PortOut(PortName port, int mask = 0xFFFFFFFF) + { core_util_critical_section_enter(); port_init(&_port, port, mask, PIN_OUTPUT); core_util_critical_section_exit(); @@ -69,7 +70,8 @@ public: * * @param value An integer specifying a bit to write for every corresponding PortOut pin */ - void write(int value) { + void write(int value) + { port_write(&_port, value); } @@ -78,14 +80,16 @@ public: * @returns * An integer with each bit corresponding to associated PortOut pin setting */ - int read() { + int read() + { return port_read(&_port); } /** A shorthand for write() * \sa PortOut::write() */ - PortOut& operator= (int value) { + PortOut &operator= (int value) + { write(value); return *this; } @@ -93,7 +97,8 @@ public: /** A shorthand for read() * \sa PortOut::read() */ - PortOut& operator= (PortOut& rhs) { + PortOut &operator= (PortOut &rhs) + { write(rhs.read()); return *this; } @@ -101,7 +106,8 @@ public: /** A shorthand for read() * \sa PortOut::read() */ - operator int() { + operator int() + { return read(); } diff --git a/drivers/PwmOut.h b/drivers/PwmOut.h index 5f7bb478ae..04e0a22618 100644 --- a/drivers/PwmOut.h +++ b/drivers/PwmOut.h @@ -57,13 +57,15 @@ public: * * @param pin PwmOut pin to connect to */ - PwmOut(PinName pin) : _deep_sleep_locked(false) { + PwmOut(PinName pin) : _deep_sleep_locked(false) + { core_util_critical_section_enter(); pwmout_init(&_pwm, pin); core_util_critical_section_exit(); } - ~PwmOut() { + ~PwmOut() + { core_util_critical_section_enter(); unlock_deep_sleep(); core_util_critical_section_exit(); @@ -76,7 +78,8 @@ public: * 0.0f (representing on 0%) and 1.0f (representing on 100%). * Values outside this range will be saturated to 0.0f or 1.0f. */ - void write(float value) { + void write(float value) + { core_util_critical_section_enter(); lock_deep_sleep(); pwmout_write(&_pwm, value); @@ -93,7 +96,8 @@ public: * @note * This value may not match exactly the value set by a previous write(). */ - float read() { + float read() + { core_util_critical_section_enter(); float val = pwmout_read(&_pwm); core_util_critical_section_exit(); @@ -107,7 +111,8 @@ public: * The resolution is currently in microseconds; periods smaller than this * will be set to zero. */ - void period(float seconds) { + void period(float seconds) + { core_util_critical_section_enter(); pwmout_period(&_pwm, seconds); core_util_critical_section_exit(); @@ -116,7 +121,8 @@ public: /** Set the PWM period, specified in milli-seconds (int), keeping the duty cycle the same. * @param ms Change the period of a PWM signal in milli-seconds without modifying the duty cycle */ - void period_ms(int ms) { + void period_ms(int ms) + { core_util_critical_section_enter(); pwmout_period_ms(&_pwm, ms); core_util_critical_section_exit(); @@ -125,7 +131,8 @@ public: /** Set the PWM period, specified in micro-seconds (int), keeping the duty cycle the same. * @param us Change the period of a PWM signal in micro-seconds without modifying the duty cycle */ - void period_us(int us) { + void period_us(int us) + { core_util_critical_section_enter(); pwmout_period_us(&_pwm, us); core_util_critical_section_exit(); @@ -134,7 +141,8 @@ public: /** Set the PWM pulsewidth, specified in seconds (float), keeping the period the same. * @param seconds Change the pulse width of a PWM signal specified in seconds (float) */ - void pulsewidth(float seconds) { + void pulsewidth(float seconds) + { core_util_critical_section_enter(); pwmout_pulsewidth(&_pwm, seconds); core_util_critical_section_exit(); @@ -143,16 +151,18 @@ public: /** Set the PWM pulsewidth, specified in milli-seconds (int), keeping the period the same. * @param ms Change the pulse width of a PWM signal specified in milli-seconds */ - void pulsewidth_ms(int ms) { + void pulsewidth_ms(int ms) + { core_util_critical_section_enter(); pwmout_pulsewidth_ms(&_pwm, ms); core_util_critical_section_exit(); } /** Set the PWM pulsewidth, specified in micro-seconds (int), keeping the period the same. - * @param us Change the pulse width of a PWM signal specified in micro-seconds + * @param us Change the pulse width of a PWM signal specified in micro-seconds */ - void pulsewidth_us(int us) { + void pulsewidth_us(int us) + { core_util_critical_section_enter(); pwmout_pulsewidth_us(&_pwm, us); core_util_critical_section_exit(); @@ -161,7 +171,8 @@ public: /** A operator shorthand for write() * \sa PwmOut::write() */ - PwmOut& operator= (float value) { + PwmOut &operator= (float value) + { // Underlying call is thread safe write(value); return *this; @@ -169,8 +180,9 @@ public: /** A operator shorthand for write() * \sa PwmOut::write() - */ - PwmOut& operator= (PwmOut& rhs) { + */ + PwmOut &operator= (PwmOut &rhs) + { // Underlying call is thread safe write(rhs.read()); return *this; @@ -179,14 +191,16 @@ public: /** An operator shorthand for read() * \sa PwmOut::read() */ - operator float() { + operator float() + { // Underlying call is thread safe return read(); } protected: /** Lock deep sleep only if it is not yet locked */ - void lock_deep_sleep() { + void lock_deep_sleep() + { if (_deep_sleep_locked == false) { sleep_manager_lock_deep_sleep(); _deep_sleep_locked = true; @@ -194,7 +208,8 @@ protected: } /** Unlock deep sleep in case it is locked */ - void unlock_deep_sleep() { + void unlock_deep_sleep() + { if (_deep_sleep_locked == true) { sleep_manager_unlock_deep_sleep(); _deep_sleep_locked = false; diff --git a/drivers/RawSerial.cpp b/drivers/RawSerial.cpp index 639586892a..2943bd9520 100644 --- a/drivers/RawSerial.cpp +++ b/drivers/RawSerial.cpp @@ -25,28 +25,33 @@ namespace mbed { -RawSerial::RawSerial(PinName tx, PinName rx, int baud) : SerialBase(tx, rx, baud) { +RawSerial::RawSerial(PinName tx, PinName rx, int baud) : SerialBase(tx, rx, baud) +{ // No lock needed in the constructor } -int RawSerial::getc() { +int RawSerial::getc() +{ lock(); int ret = _base_getc(); unlock(); return ret; } -int RawSerial::putc(int c) { +int RawSerial::putc(int c) +{ lock(); int ret = _base_putc(c); unlock(); return ret; } -int RawSerial::puts(const char *str) { +int RawSerial::puts(const char *str) +{ lock(); - while (*str) + while (*str) { putc(*str ++); + } unlock(); return 0; } @@ -55,7 +60,8 @@ int RawSerial::puts(const char *str) { // means we can't call printf() directly, so we use sprintf() instead. // We only call malloc() for the sprintf() buffer if the buffer // length is above a certain threshold, otherwise we use just the stack. -int RawSerial::printf(const char *format, ...) { +int RawSerial::printf(const char *format, ...) +{ lock(); std::va_list arg; va_start(arg, format); @@ -80,13 +86,15 @@ int RawSerial::printf(const char *format, ...) { /** Acquire exclusive access to this serial port */ -void RawSerial::lock() { +void RawSerial::lock() +{ // No lock used - external synchronization required } /** Release exclusive access to this serial port */ -void RawSerial::unlock() { +void RawSerial::unlock() +{ // No lock used - external synchronization required } diff --git a/drivers/SPI.cpp b/drivers/SPI.cpp index cdc1170228..fc52be754d 100644 --- a/drivers/SPI.cpp +++ b/drivers/SPI.cpp @@ -29,23 +29,25 @@ CircularBuffer, TRANSACTION_QUEUE_SIZE_SPI> SPI::_transaction_b #endif SPI::SPI(PinName mosi, PinName miso, PinName sclk, PinName ssel) : - _spi(), + _spi(), #if DEVICE_SPI_ASYNCH - _irq(this), - _usage(DMA_USAGE_NEVER), - _deep_sleep_locked(false), + _irq(this), + _usage(DMA_USAGE_NEVER), + _deep_sleep_locked(false), #endif - _bits(8), - _mode(0), - _hz(1000000), - _write_fill(SPI_FILL_CHAR) { + _bits(8), + _mode(0), + _hz(1000000), + _write_fill(SPI_FILL_CHAR) +{ // No lock needed in the constructor spi_init(&_spi, mosi, miso, sclk, ssel); _acquire(); } -void SPI::format(int bits, int mode) { +void SPI::format(int bits, int mode) +{ lock(); _bits = bits; _mode = mode; @@ -60,7 +62,8 @@ void SPI::format(int bits, int mode) { unlock(); } -void SPI::frequency(int hz) { +void SPI::frequency(int hz) +{ lock(); _hz = hz; // If changing format while you are the owner then just @@ -74,13 +77,14 @@ void SPI::frequency(int hz) { unlock(); } -SPI* SPI::_owner = NULL; +SPI *SPI::_owner = NULL; SingletonPtr SPI::_mutex; // ignore the fact there are multiple physical spis, and always update if it wasn't us last -void SPI::aquire() { +void SPI::aquire() +{ lock(); - if (_owner != this) { + if (_owner != this) { spi_format(&_spi, _bits, _mode, 0); spi_frequency(&_spi, _hz); _owner = this; @@ -89,15 +93,17 @@ void SPI::aquire() { } // Note: Private function with no locking -void SPI::_acquire() { - if (_owner != this) { +void SPI::_acquire() +{ + if (_owner != this) { spi_format(&_spi, _bits, _mode, 0); spi_frequency(&_spi, _hz); _owner = this; } } -int SPI::write(int value) { +int SPI::write(int value) +{ lock(); _acquire(); int ret = spi_master_write(&_spi, value); @@ -105,7 +111,8 @@ int SPI::write(int value) { return ret; } -int SPI::write(const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length) { +int SPI::write(const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length) +{ lock(); _acquire(); int ret = spi_master_block_write(&_spi, tx_buffer, tx_length, rx_buffer, rx_length, _write_fill); @@ -113,15 +120,18 @@ int SPI::write(const char *tx_buffer, int tx_length, char *rx_buffer, int rx_len return ret; } -void SPI::lock() { +void SPI::lock() +{ _mutex->lock(); } -void SPI::unlock() { +void SPI::unlock() +{ _mutex->unlock(); } -void SPI::set_default_write_value(char data) { +void SPI::set_default_write_value(char data) +{ lock(); _write_fill = data; unlock(); @@ -129,7 +139,7 @@ void SPI::set_default_write_value(char data) { #if DEVICE_SPI_ASYNCH -int SPI::transfer(const void *tx_buffer, int tx_length, void *rx_buffer, int rx_length, unsigned char bit_width, const event_callback_t& callback, int event) +int SPI::transfer(const void *tx_buffer, int tx_length, void *rx_buffer, int rx_length, unsigned char bit_width, const event_callback_t &callback, int event) { if (spi_active(&_spi)) { return queue_transfer(tx_buffer, tx_length, rx_buffer, rx_length, bit_width, callback, event); @@ -170,7 +180,7 @@ int SPI::set_dma_usage(DMAUsage usage) return 0; } -int SPI::queue_transfer(const void *tx_buffer, int tx_length, void *rx_buffer, int rx_length, unsigned char bit_width, const event_callback_t& callback, int event) +int SPI::queue_transfer(const void *tx_buffer, int tx_length, void *rx_buffer, int rx_length, unsigned char bit_width, const event_callback_t &callback, int event) { #if TRANSACTION_QUEUE_SIZE_SPI transaction_t t; @@ -199,13 +209,13 @@ int SPI::queue_transfer(const void *tx_buffer, int tx_length, void *rx_buffer, i #endif } -void SPI::start_transfer(const void *tx_buffer, int tx_length, void *rx_buffer, int rx_length, unsigned char bit_width, const event_callback_t& callback, int event) +void SPI::start_transfer(const void *tx_buffer, int tx_length, void *rx_buffer, int rx_length, unsigned char bit_width, const event_callback_t &callback, int event) { lock_deep_sleep(); _acquire(); _callback = callback; _irq.callback(&SPI::irq_handler_asynch); - spi_master_transfer(&_spi, tx_buffer, tx_length, rx_buffer, rx_length, bit_width, _irq.entry(), event , _usage); + spi_master_transfer(&_spi, tx_buffer, tx_length, rx_buffer, rx_length, bit_width, _irq.entry(), event, _usage); } void SPI::lock_deep_sleep() @@ -235,8 +245,8 @@ void SPI::dequeue_transaction() { Transaction t; if (_transaction_buffer.pop(t)) { - SPI* obj = t.get_object(); - transaction_t* data = t.get_transaction(); + SPI *obj = t.get_object(); + transaction_t *data = t.get_transaction(); obj->start_transaction(data); } } diff --git a/drivers/SPI.h b/drivers/SPI.h index 144191902c..8d24dcc966 100644 --- a/drivers/SPI.h +++ b/drivers/SPI.h @@ -86,7 +86,7 @@ public: * @param sclk SPI Clock pin * @param ssel SPI chip select pin */ - SPI(PinName mosi, PinName miso, PinName sclk, PinName ssel=NC); + SPI(PinName mosi, PinName miso, PinName sclk, PinName ssel = NC); /** Configure the data transmission format * @@ -157,7 +157,7 @@ public: /** Start non-blocking SPI transfer using 8bit buffers. * * This function locks the deep sleep until any event has occurred - * + * * @param tx_buffer The TX buffer with data to be transfered. If NULL is passed, * the default SPI value is sent * @param tx_length The length of TX buffer in bytes @@ -169,11 +169,12 @@ public: * @return Zero if the transfer has started, or -1 if SPI peripheral is busy */ template - int transfer(const Type *tx_buffer, int tx_length, Type *rx_buffer, int rx_length, const event_callback_t& callback, int event = SPI_EVENT_COMPLETE) { + int transfer(const Type *tx_buffer, int tx_length, Type *rx_buffer, int rx_length, const event_callback_t &callback, int event = SPI_EVENT_COMPLETE) + { if (spi_active(&_spi)) { - return queue_transfer(tx_buffer, tx_length, rx_buffer, rx_length, sizeof(Type)*8, callback, event); + return queue_transfer(tx_buffer, tx_length, rx_buffer, rx_length, sizeof(Type) * 8, callback, event); } - start_transfer(tx_buffer, tx_length, rx_buffer, rx_length, sizeof(Type)*8, callback, event); + start_transfer(tx_buffer, tx_length, rx_buffer, rx_length, sizeof(Type) * 8, callback, event); return 0; } @@ -215,7 +216,7 @@ protected: * @param event The logical OR of events to modify * @return Zero if the transfer has started or was added to the queue, or -1 if SPI peripheral is busy/buffer is full */ - int transfer(const void *tx_buffer, int tx_length, void *rx_buffer, int rx_length, unsigned char bit_width, const event_callback_t& callback, int event); + int transfer(const void *tx_buffer, int tx_length, void *rx_buffer, int rx_length, unsigned char bit_width, const event_callback_t &callback, int event); /** * @@ -230,7 +231,7 @@ protected: * @param event The logical OR of events to modify * @return Zero if a transfer was added to the queue, or -1 if the queue is full */ - int queue_transfer(const void *tx_buffer, int tx_length, void *rx_buffer, int rx_length, unsigned char bit_width, const event_callback_t& callback, int event); + int queue_transfer(const void *tx_buffer, int tx_length, void *rx_buffer, int rx_length, unsigned char bit_width, const event_callback_t &callback, int event); /** Configures a callback, spi peripheral and initiate a new transfer * @@ -244,7 +245,7 @@ protected: * @param callback The event callback function * @param event The logical OR of events to modify */ - void start_transfer(const void *tx_buffer, int tx_length, void *rx_buffer, int rx_length, unsigned char bit_width, const event_callback_t& callback, int event); + void start_transfer(const void *tx_buffer, int tx_length, void *rx_buffer, int rx_length, unsigned char bit_width, const event_callback_t &callback, int event); private: /** Lock deep sleep only if it is not yet locked */ @@ -272,7 +273,8 @@ private: #endif public: - virtual ~SPI() { + virtual ~SPI() + { } protected: diff --git a/drivers/SPISlave.cpp b/drivers/SPISlave.cpp index 8ae263e5d8..1f826bb343 100644 --- a/drivers/SPISlave.cpp +++ b/drivers/SPISlave.cpp @@ -24,32 +24,37 @@ SPISlave::SPISlave(PinName mosi, PinName miso, PinName sclk, PinName ssel) : _bits(8), _mode(0), _hz(1000000) - { +{ spi_init(&_spi, mosi, miso, sclk, ssel); spi_format(&_spi, _bits, _mode, 1); spi_frequency(&_spi, _hz); } -void SPISlave::format(int bits, int mode) { +void SPISlave::format(int bits, int mode) +{ _bits = bits; _mode = mode; spi_format(&_spi, _bits, _mode, 1); } -void SPISlave::frequency(int hz) { +void SPISlave::frequency(int hz) +{ _hz = hz; spi_frequency(&_spi, _hz); } -int SPISlave::receive(void) { - return(spi_slave_receive(&_spi)); +int SPISlave::receive(void) +{ + return (spi_slave_receive(&_spi)); } -int SPISlave::read(void) { - return(spi_slave_read(&_spi)); +int SPISlave::read(void) +{ + return (spi_slave_read(&_spi)); } -void SPISlave::reply(int value) { +void SPISlave::reply(int value) +{ spi_slave_write(&_spi, value); } diff --git a/drivers/Serial.cpp b/drivers/Serial.cpp index 8e3ce518a5..55e5a38bba 100644 --- a/drivers/Serial.cpp +++ b/drivers/Serial.cpp @@ -20,27 +20,33 @@ namespace mbed { -Serial::Serial(PinName tx, PinName rx, const char *name, int baud) : SerialBase(tx, rx, baud), Stream(name) { +Serial::Serial(PinName tx, PinName rx, const char *name, int baud) : SerialBase(tx, rx, baud), Stream(name) +{ } -Serial::Serial(PinName tx, PinName rx, int baud): SerialBase(tx, rx, baud), Stream(NULL) { +Serial::Serial(PinName tx, PinName rx, int baud): SerialBase(tx, rx, baud), Stream(NULL) +{ } -int Serial::_getc() { +int Serial::_getc() +{ // Mutex is already held return _base_getc(); } -int Serial::_putc(int c) { +int Serial::_putc(int c) +{ // Mutex is already held return _base_putc(c); } -void Serial::lock() { +void Serial::lock() +{ _mutex.lock(); } -void Serial::unlock() { +void Serial::unlock() +{ _mutex.unlock(); } diff --git a/drivers/Serial.h b/drivers/Serial.h index 549943a47c..11e237bffa 100644 --- a/drivers/Serial.h +++ b/drivers/Serial.h @@ -68,7 +68,7 @@ public: * @note * Either tx or rx may be specified as NC if unused */ - Serial(PinName tx, PinName rx, const char *name=NULL, int baud = MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE); + Serial(PinName tx, PinName rx, const char *name = NULL, int baud = MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE); /** Create a Serial port, connected to the specified transmit and receive pins, with the specified baud diff --git a/drivers/SerialBase.cpp b/drivers/SerialBase.cpp index ba5f9db3f2..b3f2399ac3 100644 --- a/drivers/SerialBase.cpp +++ b/drivers/SerialBase.cpp @@ -24,11 +24,12 @@ namespace mbed { SerialBase::SerialBase(PinName tx, PinName rx, int baud) : #if DEVICE_SERIAL_ASYNCH - _thunk_irq(this), _tx_usage(DMA_USAGE_NEVER), - _rx_usage(DMA_USAGE_NEVER), _tx_callback(NULL), - _rx_callback(NULL), + _thunk_irq(this), _tx_usage(DMA_USAGE_NEVER), + _rx_usage(DMA_USAGE_NEVER), _tx_callback(NULL), + _rx_callback(NULL), #endif - _serial(), _baud(baud) { + _serial(), _baud(baud) +{ // No lock needed in the constructor for (size_t i = 0; i < sizeof _irq / sizeof _irq[0]; i++) { @@ -40,20 +41,23 @@ SerialBase::SerialBase(PinName tx, PinName rx, int baud) : serial_irq_handler(&_serial, SerialBase::_irq_handler, (uint32_t)this); } -void SerialBase::baud(int baudrate) { +void SerialBase::baud(int baudrate) +{ lock(); serial_baud(&_serial, baudrate); _baud = baudrate; unlock(); } -void SerialBase::format(int bits, Parity parity, int stop_bits) { +void SerialBase::format(int bits, Parity parity, int stop_bits) +{ lock(); serial_format(&_serial, bits, (SerialParity)parity, stop_bits); unlock(); } -int SerialBase::readable() { +int SerialBase::readable() +{ lock(); int ret = serial_readable(&_serial); unlock(); @@ -61,14 +65,16 @@ int SerialBase::readable() { } -int SerialBase::writeable() { +int SerialBase::writeable() +{ lock(); int ret = serial_writable(&_serial); unlock(); return ret; } -void SerialBase::attach(Callback func, IrqType type) { +void SerialBase::attach(Callback func, IrqType type) +{ lock(); // Disable interrupts when attaching interrupt handler core_util_critical_section_enter(); @@ -76,14 +82,14 @@ void SerialBase::attach(Callback func, IrqType type) { // lock deep sleep only the first time if (!_irq[type]) { sleep_manager_lock_deep_sleep(); - } + } _irq[type] = func; serial_irq_set(&_serial, (SerialIrq)type, 1); } else { // unlock deep sleep only the first time if (_irq[type]) { sleep_manager_unlock_deep_sleep(); - } + } _irq[type] = NULL; serial_irq_set(&_serial, (SerialIrq)type, 0); } @@ -91,45 +97,51 @@ void SerialBase::attach(Callback func, IrqType type) { unlock(); } -void SerialBase::_irq_handler(uint32_t id, SerialIrq irq_type) { - SerialBase *handler = (SerialBase*)id; +void SerialBase::_irq_handler(uint32_t id, SerialIrq irq_type) +{ + SerialBase *handler = (SerialBase *)id; if (handler->_irq[irq_type]) { handler->_irq[irq_type](); } } -int SerialBase::_base_getc() { +int SerialBase::_base_getc() +{ // Mutex is already held return serial_getc(&_serial); } -int SerialBase::_base_putc(int c) { +int SerialBase::_base_putc(int c) +{ // Mutex is already held serial_putc(&_serial, c); return c; } -void SerialBase::send_break() { +void SerialBase::send_break() +{ lock(); - // 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); - unlock(); + // 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); + unlock(); } -void SerialBase::lock() { +void SerialBase::lock() +{ // Stub } -void SerialBase:: unlock() { +void SerialBase:: unlock() +{ // Stub } @@ -144,10 +156,11 @@ SerialBase::~SerialBase() } #if DEVICE_SERIAL_FC -void SerialBase::set_flow_control(Flow type, PinName flow1, PinName flow2) { +void SerialBase::set_flow_control(Flow type, PinName flow1, PinName flow2) +{ lock(); FlowControl flow_type = (FlowControl)type; - switch(type) { + switch (type) { case RTS: serial_set_flow_control(&_serial, flow_type, flow1, NC); break; @@ -170,7 +183,7 @@ void SerialBase::set_flow_control(Flow type, PinName flow1, PinName flow2) { #if DEVICE_SERIAL_ASYNCH -int SerialBase::write(const uint8_t *buffer, int length, const event_callback_t& callback, int event) +int SerialBase::write(const uint8_t *buffer, int length, const event_callback_t &callback, int event) { if (serial_tx_active(&_serial)) { return -1; // transaction ongoing @@ -179,7 +192,7 @@ int SerialBase::write(const uint8_t *buffer, int length, const event_callback_t& return 0; } -int SerialBase::write(const uint16_t *buffer, int length, const event_callback_t& callback, int event) +int SerialBase::write(const uint16_t *buffer, int length, const event_callback_t &callback, int event) { if (serial_tx_active(&_serial)) { return -1; // transaction ongoing @@ -188,7 +201,7 @@ int SerialBase::write(const uint16_t *buffer, int length, const event_callback_t return 0; } -void SerialBase::start_write(const void *buffer, int buffer_size, char buffer_width, const event_callback_t& callback, int event) +void SerialBase::start_write(const void *buffer, int buffer_size, char buffer_width, const event_callback_t &callback, int event) { _tx_callback = callback; @@ -235,27 +248,27 @@ int SerialBase::set_dma_usage_rx(DMAUsage usage) return 0; } -int SerialBase::read(uint8_t *buffer, int length, const event_callback_t& callback, int event, unsigned char char_match) +int SerialBase::read(uint8_t *buffer, int length, const event_callback_t &callback, int event, unsigned char char_match) { if (serial_rx_active(&_serial)) { return -1; // transaction ongoing } - start_read((void*)buffer, length, 8, callback, event, char_match); + start_read((void *)buffer, length, 8, callback, event, char_match); return 0; } -int SerialBase::read(uint16_t *buffer, int length, const event_callback_t& callback, int event, unsigned char char_match) +int SerialBase::read(uint16_t *buffer, int length, const event_callback_t &callback, int event, unsigned char char_match) { if (serial_rx_active(&_serial)) { return -1; // transaction ongoing } - start_read((void*)buffer, length, 16, callback, event, char_match); + start_read((void *)buffer, length, 16, callback, event, char_match); return 0; } -void SerialBase::start_read(void *buffer, int buffer_size, char buffer_width, const event_callback_t& callback, int event, unsigned char char_match) +void SerialBase::start_read(void *buffer, int buffer_size, char buffer_width, const event_callback_t &callback, int event, unsigned char char_match) { _rx_callback = callback; _thunk_irq.callback(&SerialBase::interrupt_handler_asynch); diff --git a/drivers/SerialBase.h b/drivers/SerialBase.h index 970cf4d19c..9fe5ddef25 100644 --- a/drivers/SerialBase.h +++ b/drivers/SerialBase.h @@ -76,7 +76,7 @@ public: * @param parity The parity used (SerialBase::None, SerialBase::Odd, SerialBase::Even, SerialBase::Forced1, SerialBase::Forced0; default = SerialBase::None) * @param stop_bits The number of stop bits (1 or 2; default = 1) */ - void format(int bits=8, Parity parity=SerialBase::None, int stop_bits=1); + void format(int bits = 8, Parity parity = SerialBase::None, int stop_bits = 1); /** Determine if there is a character available to read * @@ -99,7 +99,7 @@ public: * @param func 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(Callback func, IrqType type=RxIrq); + void attach(Callback func, IrqType type = RxIrq); /** Attach a member function to call whenever a serial interrupt is generated * @@ -112,9 +112,10 @@ public: */ template MBED_DEPRECATED_SINCE("mbed-os-5.1", - "The attach function does not support cv-qualifiers. Replaced by " - "attach(callback(obj, method), type).") - void attach(T *obj, void (T::*method)(), IrqType type=RxIrq) { + "The attach function does not support cv-qualifiers. Replaced by " + "attach(callback(obj, method), type).") + void attach(T *obj, void (T::*method)(), IrqType type = RxIrq) + { attach(callback(obj, method), type); } @@ -129,9 +130,10 @@ public: */ template MBED_DEPRECATED_SINCE("mbed-os-5.1", - "The attach function does not support cv-qualifiers. Replaced by " - "attach(callback(obj, method), type).") - void attach(T *obj, void (*method)(T*), IrqType type=RxIrq) { + "The attach function does not support cv-qualifiers. Replaced by " + "attach(callback(obj, method), type).") + void attach(T *obj, void (*method)(T *), IrqType type = RxIrq) + { attach(callback(obj, method), type); } @@ -158,7 +160,7 @@ public: * @param flow1 the first flow control pin (RTS for RTS or RTSCTS, CTS for CTS) * @param flow2 the second flow control pin (CTS for RTSCTS) */ - void set_flow_control(Flow type, PinName flow1=NC, PinName flow2=NC); + void set_flow_control(Flow type, PinName flow1 = NC, PinName flow2 = NC); #endif static void _irq_handler(uint32_t id, SerialIrq irq_type); @@ -168,24 +170,24 @@ public: /** Begin asynchronous write using 8bit buffer. The completition invokes registered TX event callback * * This function locks the deep sleep until any event has occurred - * + * * @param buffer The buffer where received data will be stored * @param length The buffer length in bytes * @param callback The event callback function * @param event The logical OR of TX events */ - int write(const uint8_t *buffer, int length, const event_callback_t& callback, int event = SERIAL_EVENT_TX_COMPLETE); + int write(const uint8_t *buffer, int length, const event_callback_t &callback, int event = SERIAL_EVENT_TX_COMPLETE); /** Begin asynchronous write using 16bit buffer. The completition invokes registered TX event callback * * This function locks the deep sleep until any event has occurred - * + * * @param buffer The buffer where received data will be stored * @param length The buffer length in bytes * @param callback The event callback function * @param event The logical OR of TX events */ - int write(const uint16_t *buffer, int length, const event_callback_t& callback, int event = SERIAL_EVENT_TX_COMPLETE); + int write(const uint16_t *buffer, int length, const event_callback_t &callback, int event = SERIAL_EVENT_TX_COMPLETE); /** Abort the on-going write transfer */ @@ -194,26 +196,26 @@ public: /** Begin asynchronous reading using 8bit buffer. The completition invokes registred RX event callback. * * This function locks the deep sleep until any event has occurred - * + * * @param buffer The buffer where received data will be stored * @param length The buffer length in bytes * @param callback The event callback function * @param event The logical OR of RX events * @param char_match The matching character */ - int read(uint8_t *buffer, int length, const event_callback_t& callback, int event = SERIAL_EVENT_RX_COMPLETE, unsigned char char_match = SERIAL_RESERVED_CHAR_MATCH); + int read(uint8_t *buffer, int length, const event_callback_t &callback, int event = SERIAL_EVENT_RX_COMPLETE, unsigned char char_match = SERIAL_RESERVED_CHAR_MATCH); /** Begin asynchronous reading using 16bit buffer. The completition invokes registred RX event callback. * * This function locks the deep sleep until any event has occurred - * + * * @param buffer The buffer where received data will be stored * @param length The buffer length in bytes * @param callback The event callback function * @param event The logical OR of RX events * @param char_match The matching character */ - int read(uint16_t *buffer, int length, const event_callback_t& callback, int event = SERIAL_EVENT_RX_COMPLETE, unsigned char char_match = SERIAL_RESERVED_CHAR_MATCH); + int read(uint16_t *buffer, int length, const event_callback_t &callback, int event = SERIAL_EVENT_RX_COMPLETE, unsigned char char_match = SERIAL_RESERVED_CHAR_MATCH); /** Abort the on-going read transfer */ @@ -234,8 +236,8 @@ public: int set_dma_usage_rx(DMAUsage usage); protected: - void start_read(void *buffer, int buffer_size, char buffer_width, const event_callback_t& callback, int event, unsigned char char_match); - void start_write(const void *buffer, int buffer_size, char buffer_width, const event_callback_t& callback, int event); + void start_read(void *buffer, int buffer_size, char buffer_width, const event_callback_t &callback, int event, unsigned char char_match); + void start_write(const void *buffer, int buffer_size, char buffer_width, const event_callback_t &callback, int event); void interrupt_handler_asynch(void); #endif diff --git a/drivers/SerialWireOutput.h b/drivers/SerialWireOutput.h index 39f0c39089..154197cf37 100644 --- a/drivers/SerialWireOutput.h +++ b/drivers/SerialWireOutput.h @@ -20,7 +20,7 @@ #include "platform/FileHandle.h" namespace mbed { - + class SerialWireOutput : public FileHandle { public: @@ -71,7 +71,7 @@ public: return 0; } }; - + } // namespace mbed #endif diff --git a/drivers/Ticker.cpp b/drivers/Ticker.cpp index c2589c0d80..efa3efb9c0 100644 --- a/drivers/Ticker.cpp +++ b/drivers/Ticker.cpp @@ -22,11 +22,12 @@ namespace mbed { -void Ticker::detach() { +void Ticker::detach() +{ core_util_critical_section_enter(); remove(); // unlocked only if we were attached (we locked it) and this is not low power ticker - if(_function && _lock_deepsleep) { + if (_function && _lock_deepsleep) { sleep_manager_unlock_deep_sleep(); } @@ -34,7 +35,8 @@ void Ticker::detach() { core_util_critical_section_exit(); } -void Ticker::setup(us_timestamp_t t) { +void Ticker::setup(us_timestamp_t t) +{ core_util_critical_section_enter(); remove(); _delay = t; @@ -42,7 +44,8 @@ void Ticker::setup(us_timestamp_t t) { core_util_critical_section_exit(); } -void Ticker::handler() { +void Ticker::handler() +{ insert_absolute(event.timestamp + _delay); if (_function) { _function(); diff --git a/drivers/Ticker.h b/drivers/Ticker.h index 59a24dff43..e8bca7cbe9 100644 --- a/drivers/Ticker.h +++ b/drivers/Ticker.h @@ -66,11 +66,13 @@ namespace mbed { class Ticker : public TimerEvent, private NonCopyable { public: - Ticker() : TimerEvent(), _function(0), _lock_deepsleep(true) { + Ticker() : TimerEvent(), _function(0), _lock_deepsleep(true) + { } // When low power ticker is in use, then do not disable deep-sleep. - Ticker(const ticker_data_t *data) : TimerEvent(data), _function(0), _lock_deepsleep(true) { + Ticker(const ticker_data_t *data) : TimerEvent(data), _function(0), _lock_deepsleep(true) + { #if DEVICE_LPTICKER _lock_deepsleep = (data != get_lp_ticker_data()); #endif @@ -81,7 +83,8 @@ public: * @param func pointer to the function to be called * @param t the time between calls in seconds */ - void attach(Callback func, float t) { + void attach(Callback func, float t) + { attach_us(func, t * 1000000.0f); } @@ -96,9 +99,10 @@ public: */ template MBED_DEPRECATED_SINCE("mbed-os-5.1", - "The attach function does not support cv-qualifiers. Replaced by " - "attach(callback(obj, method), t).") - void attach(T *obj, M method, float t) { + "The attach function does not support cv-qualifiers. Replaced by " + "attach(callback(obj, method), t).") + void attach(T *obj, M method, float t) + { attach(callback(obj, method), t); } @@ -112,10 +116,11 @@ public: * for threads scheduling. * */ - void attach_us(Callback func, us_timestamp_t t) { + void attach_us(Callback func, us_timestamp_t t) + { core_util_critical_section_enter(); // lock only for the initial callback setup and this is not low power ticker - if(!_function && _lock_deepsleep) { + if (!_function && _lock_deepsleep) { sleep_manager_lock_deep_sleep(); } _function = func; @@ -134,13 +139,15 @@ public: */ template MBED_DEPRECATED_SINCE("mbed-os-5.1", - "The attach_us function does not support cv-qualifiers. Replaced by " - "attach_us(callback(obj, method), t).") - void attach_us(T *obj, M method, us_timestamp_t t) { + "The attach_us function does not support cv-qualifiers. Replaced by " + "attach_us(callback(obj, method), t).") + void attach_us(T *obj, M method, us_timestamp_t t) + { attach_us(Callback(obj, method), t); } - virtual ~Ticker() { + virtual ~Ticker() + { detach(); } diff --git a/drivers/Timeout.cpp b/drivers/Timeout.cpp index 6fc4a7e69b..159cc0d4b4 100644 --- a/drivers/Timeout.cpp +++ b/drivers/Timeout.cpp @@ -17,7 +17,8 @@ namespace mbed { -void Timeout::handler() { +void Timeout::handler() +{ Callback local = _function; detach(); local.call(); diff --git a/drivers/Timer.cpp b/drivers/Timer.cpp index b8af5dc3d8..ccf75f6089 100644 --- a/drivers/Timer.cpp +++ b/drivers/Timer.cpp @@ -21,21 +21,24 @@ namespace mbed { -Timer::Timer() : _running(), _start(), _time(), _ticker_data(get_us_ticker_data()), _lock_deepsleep(true) { +Timer::Timer() : _running(), _start(), _time(), _ticker_data(get_us_ticker_data()), _lock_deepsleep(true) +{ reset(); } -Timer::Timer(const ticker_data_t *data) : _running(), _start(), _time(), _ticker_data(data), _lock_deepsleep(true) { +Timer::Timer(const ticker_data_t *data) : _running(), _start(), _time(), _ticker_data(data), _lock_deepsleep(true) +{ reset(); #if DEVICE_LPTICKER _lock_deepsleep = (data != get_lp_ticker_data()); #endif } -Timer::~Timer() { +Timer::~Timer() +{ core_util_critical_section_enter(); if (_running) { - if(_lock_deepsleep) { + if (_lock_deepsleep) { sleep_manager_unlock_deep_sleep(); } } @@ -43,10 +46,11 @@ Timer::~Timer() { core_util_critical_section_exit(); } -void Timer::start() { +void Timer::start() +{ core_util_critical_section_enter(); if (!_running) { - if(_lock_deepsleep) { + if (_lock_deepsleep) { sleep_manager_lock_deep_sleep(); } _start = ticker_read_us(_ticker_data); @@ -55,11 +59,12 @@ void Timer::start() { core_util_critical_section_exit(); } -void Timer::stop() { +void Timer::stop() +{ core_util_critical_section_enter(); _time += slicetime(); if (_running) { - if(_lock_deepsleep) { + if (_lock_deepsleep) { sleep_manager_unlock_deep_sleep(); } } @@ -67,26 +72,31 @@ void Timer::stop() { core_util_critical_section_exit(); } -int Timer::read_us() { +int Timer::read_us() +{ return read_high_resolution_us(); } -float Timer::read() { +float Timer::read() +{ return (float)read_us() / 1000000.0f; } -int Timer::read_ms() { +int Timer::read_ms() +{ return read_high_resolution_us() / 1000; } -us_timestamp_t Timer::read_high_resolution_us() { +us_timestamp_t Timer::read_high_resolution_us() +{ core_util_critical_section_enter(); us_timestamp_t time = _time + slicetime(); core_util_critical_section_exit(); return time; } -us_timestamp_t Timer::slicetime() { +us_timestamp_t Timer::slicetime() +{ us_timestamp_t ret = 0; core_util_critical_section_enter(); if (_running) { @@ -96,14 +106,16 @@ us_timestamp_t Timer::slicetime() { return ret; } -void Timer::reset() { +void Timer::reset() +{ core_util_critical_section_enter(); _start = ticker_read_us(_ticker_data); _time = 0; core_util_critical_section_exit(); } -Timer::operator float() { +Timer::operator float() +{ return read(); } diff --git a/drivers/TimerEvent.cpp b/drivers/TimerEvent.cpp index d7a7c1c2a4..ddf495f681 100644 --- a/drivers/TimerEvent.cpp +++ b/drivers/TimerEvent.cpp @@ -22,33 +22,40 @@ namespace mbed { -TimerEvent::TimerEvent() : event(), _ticker_data(get_us_ticker_data()) { +TimerEvent::TimerEvent() : event(), _ticker_data(get_us_ticker_data()) +{ ticker_set_handler(_ticker_data, (&TimerEvent::irq)); } -TimerEvent::TimerEvent(const ticker_data_t *data) : event(), _ticker_data(data) { +TimerEvent::TimerEvent(const ticker_data_t *data) : event(), _ticker_data(data) +{ ticker_set_handler(_ticker_data, (&TimerEvent::irq)); } -void TimerEvent::irq(uint32_t id) { - TimerEvent *timer_event = (TimerEvent*)id; +void TimerEvent::irq(uint32_t id) +{ + TimerEvent *timer_event = (TimerEvent *)id; timer_event->handler(); } -TimerEvent::~TimerEvent() { +TimerEvent::~TimerEvent() +{ remove(); } // insert in to linked list -void TimerEvent::insert(timestamp_t timestamp) { +void TimerEvent::insert(timestamp_t timestamp) +{ ticker_insert_event(_ticker_data, &event, timestamp, (uint32_t)this); } -void TimerEvent::insert_absolute(us_timestamp_t timestamp) { +void TimerEvent::insert_absolute(us_timestamp_t timestamp) +{ ticker_insert_event_us(_ticker_data, &event, timestamp, (uint32_t)this); } -void TimerEvent::remove() { +void TimerEvent::remove() +{ ticker_remove_event(_ticker_data, &event); } diff --git a/drivers/UARTSerial.cpp b/drivers/UARTSerial.cpp index c6fd37efe7..81a5913c52 100644 --- a/drivers/UARTSerial.cpp +++ b/drivers/UARTSerial.cpp @@ -29,11 +29,11 @@ namespace mbed { UARTSerial::UARTSerial(PinName tx, PinName rx, int baud) : - SerialBase(tx, rx, baud), - _blocking(true), - _tx_irq_enabled(false), - _rx_irq_enabled(true), - _dcd_irq(NULL) + SerialBase(tx, rx, baud), + _blocking(true), + _tx_irq_enabled(false), + _rx_irq_enabled(true), + _dcd_irq(NULL) { /* Attatch IRQ routines to the serial device. */ SerialBase::attach(callback(this, &UARTSerial::rx_irq), RxIrq); @@ -56,7 +56,7 @@ void UARTSerial::set_baud(int baud) void UARTSerial::set_data_carrier_detect(PinName dcd_pin, bool active_high) { - delete _dcd_irq; + delete _dcd_irq; _dcd_irq = NULL; if (dcd_pin != NC) { @@ -121,7 +121,8 @@ int UARTSerial::sync() return 0; } -void UARTSerial::sigio(Callback func) { +void UARTSerial::sigio(Callback func) +{ core_util_critical_section_enter(); _sigio_cb = func; if (_sigio_cb) { @@ -133,7 +134,7 @@ void UARTSerial::sigio(Callback func) { core_util_critical_section_exit(); } -ssize_t UARTSerial::write(const void* buffer, size_t length) +ssize_t UARTSerial::write(const void *buffer, size_t length) { size_t data_written = 0; const char *buf_ptr = static_cast(buffer); @@ -178,10 +179,10 @@ ssize_t UARTSerial::write(const void* buffer, size_t length) api_unlock(); - return data_written != 0 ? (ssize_t) data_written : (ssize_t) -EAGAIN; + return data_written != 0 ? (ssize_t) data_written : (ssize_t) - EAGAIN; } -ssize_t UARTSerial::read(void* buffer, size_t length) +ssize_t UARTSerial::read(void *buffer, size_t length) { size_t data_read = 0; @@ -235,7 +236,8 @@ void UARTSerial::wake() } } -short UARTSerial::poll(short events) const { +short UARTSerial::poll(short events) const +{ short revents = 0; /* Check the Circular Buffer if space available for writing out */ diff --git a/drivers/UARTSerial.h b/drivers/UARTSerial.h index 08c35e1445..c3a110fd65 100644 --- a/drivers/UARTSerial.h +++ b/drivers/UARTSerial.h @@ -42,7 +42,7 @@ namespace mbed { /** \addtogroup drivers */ /** Class providing buffered UART communication functionality using separate circular buffer for send and receive channels - * + * * @ingroup drivers */ @@ -81,7 +81,7 @@ public: * @param length The number of bytes to write * @return The number of bytes written, negative error on failure */ - virtual ssize_t write(const void* buffer, size_t length); + virtual ssize_t write(const void *buffer, size_t length); /** Read the contents of a file into a buffer * @@ -95,7 +95,7 @@ public: * @param length The number of bytes to read * @return The number of bytes read, 0 at end of file, negative error on failure */ - virtual ssize_t read(void* buffer, size_t length); + virtual ssize_t read(void *buffer, size_t length); /** Close a file * @@ -201,7 +201,7 @@ public: * @param parity The parity used (None, Odd, Even, Forced1, Forced0; default = None) * @param stop_bits The number of stop bits (1 or 2; default = 1) */ - void set_format(int bits=8, Parity parity=UARTSerial::None, int stop_bits=1); + void set_format(int bits = 8, Parity parity = UARTSerial::None, int stop_bits = 1); #if DEVICE_SERIAL_FC // For now use the base enum - but in future we may have extra options @@ -219,7 +219,7 @@ public: * @param flow1 the first flow control pin (RTS for RTS or RTSCTS, CTS for CTS) * @param flow2 the second flow control pin (CTS for RTSCTS) */ - void set_flow_control(Flow type, PinName flow1=NC, PinName flow2=NC); + void set_flow_control(Flow type, PinName flow1 = NC, PinName flow2 = NC); #endif private: