drivers: astyle update

pull/7008/head
Martin Kojtal 2018-05-24 16:58:14 +01:00
parent 58fa28b9b2
commit 700e6df834
48 changed files with 825 additions and 532 deletions

View File

@ -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();
}

View File

@ -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();
}

View File

@ -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]);

View File

@ -67,7 +67,7 @@ public:
*
* @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

View File

@ -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();
}

View File

@ -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

View File

@ -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();
}

View File

@ -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

View File

@ -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<void()> func, IrqType type) {
void CAN::attach(Callback<void()> func, IrqType type)
{
lock();
if (func) {
// lock deep sleep only the first time
@ -136,18 +149,21 @@ void CAN::attach(Callback<void()> 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();
}

View File

@ -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);
}
};
@ -241,42 +244,44 @@ public:
* @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<void()> func, IrqType type=RxIrq);
void attach(Callback<void()> 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<typename T>
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<typename T>
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);
}

View File

@ -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();
}

View File

@ -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();
}

View File

@ -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();
}

View File

@ -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);

View File

@ -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;

View File

@ -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)) {

View File

@ -151,7 +151,8 @@ public:
*/
virtual void unlock(void);
virtual ~I2C() {
virtual ~I2C()
{
// Do nothing
}
@ -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();

View File

@ -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);
}

View File

@ -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<void()> func) {
void InterruptIn::rise(Callback<void()> func)
{
core_util_critical_section_enter();
if (func) {
_rise = func;
@ -74,7 +81,8 @@ void InterruptIn::rise(Callback<void()> func) {
core_util_critical_section_exit();
}
void InterruptIn::fall(Callback<void()> func) {
void InterruptIn::fall(Callback<void()> func)
{
core_util_critical_section_enter();
if (func) {
_fall = func;
@ -86,8 +94,9 @@ void InterruptIn::fall(Callback<void()> 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:
if (handler->_rise) {
@ -99,23 +108,27 @@ void InterruptIn::_irq_handler(uint32_t id, gpio_irq_event event) {
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();
}

View File

@ -106,9 +106,10 @@ public:
*/
template<typename T, typename M>
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<typename T, typename M>
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();

View File

@ -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();
}

View File

@ -64,8 +64,8 @@ public:
* @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
@ -73,7 +73,7 @@ public:
*
*/
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
@ -87,8 +87,9 @@ 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);
}
@ -104,8 +105,9 @@ 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);
}
@ -123,8 +125,9 @@ public:
*/
template<typename T>
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);
}
@ -142,8 +145,9 @@ public:
*/
template<typename T>
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);
}
@ -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<typename T>
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;
};

View File

@ -35,10 +35,12 @@ namespace mbed {
class LowPowerTicker : public Ticker, private NonCopyable<LowPowerTicker> {
public:
LowPowerTicker() : Ticker(get_lp_ticker_data()) {
LowPowerTicker() : Ticker(get_lp_ticker_data())
{
}
virtual ~LowPowerTicker() {
virtual ~LowPowerTicker()
{
}
};

View File

@ -35,7 +35,8 @@ namespace mbed {
class LowPowerTimeout : public LowPowerTicker, private NonCopyable<LowPowerTimeout> {
private:
virtual void handler(void) {
virtual void handler(void)
{
_function.call();
}
};

View File

@ -35,7 +35,8 @@ namespace mbed {
class LowPowerTimer : public Timer, private NonCopyable<LowPowerTimer> {
public:
LowPowerTimer() : Timer(get_lp_ticker_data()) {
LowPowerTimer() : Timer(get_lp_ticker_data())
{
}
};

View File

@ -26,40 +26,40 @@ namespace mbed {
*/
template<>
MbedCRC<POLY_32BIT_ANSI, 32>::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<POLY_8BIT_CCITT, 8>::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<POLY_7BIT_SD, 7>::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<POLY_16BIT_CCITT, 16>::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<POLY_16BIT_IBM, 16>::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();
}

View File

@ -92,9 +92,8 @@ namespace mbed {
* @ingroup drivers
*/
template <uint32_t polynomial=POLY_32BIT_ANSI, uint8_t width=32>
class MbedCRC
{
template <uint32_t polynomial = POLY_32BIT_ANSI, uint8_t width = 32>
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 <POLY_7BIT_SD, 7> ct; --- Valid POLY_7BIT_SD
* MbedCRC <0x1021, 16> ct; --- Valid POLY_16BIT_CCITT
* MbedCRC <POLY_16BIT_CCITT, 32> 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);

View File

@ -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();
}

View File

@ -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();
}

View File

@ -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();
}

View File

@ -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,7 +151,8 @@ 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();
@ -152,7 +161,8 @@ public:
/** 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
*/
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;
@ -170,7 +181,8 @@ 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;

View File

@ -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
}

View File

@ -29,23 +29,25 @@ CircularBuffer<Transaction<SPI>, 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<PlatformMutex> 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<SPI> 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);
}
}

View File

@ -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
*
@ -169,11 +169,12 @@ public:
* @return Zero if the transfer has started, or -1 if SPI peripheral is busy
*/
template<typename Type>
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:

View File

@ -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);
}

View File

@ -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();
}

View File

@ -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

View File

@ -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<void()> func, IrqType type) {
void SerialBase::attach(Callback<void()> func, IrqType type)
{
lock();
// Disable interrupts when attaching interrupt handler
core_util_critical_section_enter();
@ -91,45 +97,51 @@ void SerialBase::attach(Callback<void()> 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);

View File

@ -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<void()> func, IrqType type=RxIrq);
void attach(Callback<void()> func, IrqType type = RxIrq);
/** Attach a member function to call whenever a serial interrupt is generated
*
@ -112,9 +112,10 @@ public:
*/
template<typename T>
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<typename T>
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);
@ -174,7 +176,7 @@ public:
* @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
*
@ -185,7 +187,7 @@ public:
* @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
*/
@ -201,7 +203,7 @@ public:
* @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.
*
@ -213,7 +215,7 @@ public:
* @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

View File

@ -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();

View File

@ -66,11 +66,13 @@ namespace mbed {
class Ticker : public TimerEvent, private NonCopyable<Ticker> {
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<void()> func, float t) {
void attach(Callback<void()> func, float t)
{
attach_us(func, t * 1000000.0f);
}
@ -96,9 +99,10 @@ public:
*/
template<typename T, typename M>
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<void()> func, us_timestamp_t t) {
void attach_us(Callback<void()> 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<typename T, typename M>
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<void()>(obj, method), t);
}
virtual ~Ticker() {
virtual ~Ticker()
{
detach();
}

View File

@ -17,7 +17,8 @@
namespace mbed {
void Timeout::handler() {
void Timeout::handler()
{
Callback<void()> local = _function;
detach();
local.call();

View File

@ -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();
}

View File

@ -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);
}

View File

@ -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<void()> func) {
void UARTSerial::sigio(Callback<void()> func)
{
core_util_critical_section_enter();
_sigio_cb = func;
if (_sigio_cb) {
@ -133,7 +134,7 @@ void UARTSerial::sigio(Callback<void()> 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<const char *>(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 */

View File

@ -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: