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 * @param pin AnalogIn pin to connect to
*/ */
AnalogIn(PinName pin) { AnalogIn(PinName pin)
{
lock(); lock();
analogin_init(&_adc, pin); analogin_init(&_adc, pin);
unlock(); unlock();
@ -67,7 +68,8 @@ public:
* *
* @returns A floating-point value representing the current input voltage, measured as a percentage * @returns A floating-point value representing the current input voltage, measured as a percentage
*/ */
float read() { float read()
{
lock(); lock();
float ret = analogin_read(&_adc); float ret = analogin_read(&_adc);
unlock(); unlock();
@ -79,7 +81,8 @@ public:
* @returns * @returns
* 16-bit unsigned short representing the current input voltage, normalised to a 16-bit value * 16-bit unsigned short representing the current input voltage, normalised to a 16-bit value
*/ */
unsigned short read_u16() { unsigned short read_u16()
{
lock(); lock();
unsigned short ret = analogin_read_u16(&_adc); unsigned short ret = analogin_read_u16(&_adc);
unlock(); unlock();
@ -99,22 +102,26 @@ public:
* if(volume > 0.25) { ... } * if(volume > 0.25) { ... }
* @endcode * @endcode
*/ */
operator float() { operator float()
{
// Underlying call is thread safe // Underlying call is thread safe
return read(); return read();
} }
virtual ~AnalogIn() { virtual ~AnalogIn()
{
// Do nothing // Do nothing
} }
protected: protected:
virtual void lock() { virtual void lock()
{
_mutex->lock(); _mutex->lock();
} }
virtual void unlock() { virtual void unlock()
{
_mutex->unlock(); _mutex->unlock();
} }

View File

@ -57,7 +57,8 @@ public:
* *
* @param pin AnalogOut pin to connect to * @param pin AnalogOut pin to connect to
*/ */
AnalogOut(PinName pin) { AnalogOut(PinName pin)
{
analogout_init(&_dac, pin); analogout_init(&_dac, pin);
} }
@ -68,7 +69,8 @@ public:
* 0.0f (representing 0v / 0%) and 1.0f (representing 3.3v / 100%). * 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. * Values outside this range will be saturated to 0.0f or 1.0f.
*/ */
void write(float value) { void write(float value)
{
lock(); lock();
analogout_write(&_dac, value); analogout_write(&_dac, value);
unlock(); unlock();
@ -79,7 +81,8 @@ public:
* @param value 16-bit unsigned short representing the output voltage, * @param value 16-bit unsigned short representing the output voltage,
* normalised to a 16-bit value (0x0000 = 0v, 0xFFFF = 3.3v) * normalised to a 16-bit value (0x0000 = 0v, 0xFFFF = 3.3v)
*/ */
void write_u16(unsigned short value) { void write_u16(unsigned short value)
{
lock(); lock();
analogout_write_u16(&_dac, value); analogout_write_u16(&_dac, value);
unlock(); unlock();
@ -95,7 +98,8 @@ public:
* @note * @note
* This value may not match exactly the value set by a previous write(). * This value may not match exactly the value set by a previous write().
*/ */
float read() { float read()
{
lock(); lock();
float ret = analogout_read(&_dac); float ret = analogout_read(&_dac);
unlock(); unlock();
@ -105,7 +109,8 @@ public:
/** An operator shorthand for write() /** An operator shorthand for write()
* \sa AnalogOut::write() * \sa AnalogOut::write()
*/ */
AnalogOut& operator= (float percent) { AnalogOut &operator= (float percent)
{
// Underlying write call is thread safe // Underlying write call is thread safe
write(percent); write(percent);
return *this; return *this;
@ -114,7 +119,8 @@ public:
/** An operator shorthand for write() /** An operator shorthand for write()
* \sa AnalogOut::write() * \sa AnalogOut::write()
*/ */
AnalogOut& operator= (AnalogOut& rhs) { AnalogOut &operator= (AnalogOut &rhs)
{
// Underlying write call is thread safe // Underlying write call is thread safe
write(rhs.read()); write(rhs.read());
return *this; return *this;
@ -123,22 +129,26 @@ public:
/** An operator shorthand for read() /** An operator shorthand for read()
* \sa AnalogOut::read() * \sa AnalogOut::read()
*/ */
operator float() { operator float()
{
// Underlying read call is thread safe // Underlying read call is thread safe
return read(); return read();
} }
virtual ~AnalogOut() { virtual ~AnalogOut()
{
// Do nothing // Do nothing
} }
protected: protected:
virtual void lock() { virtual void lock()
{
_mutex.lock(); _mutex.lock();
} }
virtual void unlock() { virtual void unlock()
{
_mutex.unlock(); _mutex.unlock();
} }

View File

@ -18,7 +18,8 @@
namespace mbed { 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}; 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 // No lock needed in the constructor
@ -31,7 +32,8 @@ 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 // No lock needed in the constructor
_nc_mask = 0; _nc_mask = 0;
for (int i = 0; i < 16; i++) { for (int i = 0; i < 16; i++) {
@ -42,7 +44,8 @@ BusIn::BusIn(PinName pins[16]) {
} }
} }
BusIn::~BusIn() { BusIn::~BusIn()
{
// No lock needed in the destructor // No lock needed in the destructor
for (int i = 0; i < 16; i++) { for (int i = 0; i < 16; i++) {
if (_pin[i] != 0) { if (_pin[i] != 0) {
@ -51,7 +54,8 @@ BusIn::~BusIn() {
} }
} }
int BusIn::read() { int BusIn::read()
{
int v = 0; int v = 0;
lock(); lock();
for (int i = 0; i < 16; i++) { for (int i = 0; i < 16; i++) {
@ -63,7 +67,8 @@ int BusIn::read() {
return v; return v;
} }
void BusIn::mode(PinMode pull) { void BusIn::mode(PinMode pull)
{
lock(); lock();
for (int i = 0; i < 16; i++) { for (int i = 0; i < 16; i++) {
if (_pin[i] != 0) { if (_pin[i] != 0) {
@ -73,20 +78,24 @@ void BusIn::mode(PinMode pull) {
unlock(); unlock();
} }
void BusIn::lock() { void BusIn::lock()
{
_mutex.lock(); _mutex.lock();
} }
void BusIn::unlock() { void BusIn::unlock()
{
_mutex.unlock(); _mutex.unlock();
} }
BusIn::operator int() { BusIn::operator int()
{
// Underlying read is thread safe // Underlying read is thread safe
return read(); return read();
} }
DigitalIn& BusIn::operator[] (int index) { DigitalIn &BusIn::operator[](int index)
{
// No lock needed since _pin is not modified outside the constructor // No lock needed since _pin is not modified outside the constructor
MBED_ASSERT(index >= 0 && index <= 16); MBED_ASSERT(index >= 0 && index <= 16);
MBED_ASSERT(_pin[index]); MBED_ASSERT(_pin[index]);

View File

@ -90,7 +90,8 @@ public:
* @returns * @returns
* Binary mask of connected pins * Binary mask of connected pins
*/ */
int mask() { int mask()
{
// No lock needed since _nc_mask is not modified outside the constructor // No lock needed since _nc_mask is not modified outside the constructor
return _nc_mask; return _nc_mask;
} }

View File

@ -18,7 +18,8 @@
namespace mbed { 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}; 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 // No lock needed in the constructor
@ -31,7 +32,8 @@ 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 // No lock needed in the constructor
_nc_mask = 0; _nc_mask = 0;
for (int i = 0; i < 16; i++) { for (int i = 0; i < 16; i++) {
@ -42,7 +44,8 @@ BusInOut::BusInOut(PinName pins[16]) {
} }
} }
BusInOut::~BusInOut() { BusInOut::~BusInOut()
{
// No lock needed in the destructor // No lock needed in the destructor
for (int i = 0; i < 16; i++) { for (int i = 0; i < 16; i++) {
if (_pin[i] != 0) { if (_pin[i] != 0) {
@ -51,7 +54,8 @@ BusInOut::~BusInOut() {
} }
} }
void BusInOut::write(int value) { void BusInOut::write(int value)
{
lock(); lock();
for (int i = 0; i < 16; i++) { for (int i = 0; i < 16; i++) {
if (_pin[i] != 0) { if (_pin[i] != 0) {
@ -61,7 +65,8 @@ void BusInOut::write(int value) {
unlock(); unlock();
} }
int BusInOut::read() { int BusInOut::read()
{
lock(); lock();
int v = 0; int v = 0;
for (int i = 0; i < 16; i++) { for (int i = 0; i < 16; i++) {
@ -73,7 +78,8 @@ int BusInOut::read() {
return v; return v;
} }
void BusInOut::output() { void BusInOut::output()
{
lock(); lock();
for (int i = 0; i < 16; i++) { for (int i = 0; i < 16; i++) {
if (_pin[i] != 0) { if (_pin[i] != 0) {
@ -83,7 +89,8 @@ void BusInOut::output() {
unlock(); unlock();
} }
void BusInOut::input() { void BusInOut::input()
{
lock(); lock();
for (int i = 0; i < 16; i++) { for (int i = 0; i < 16; i++) {
if (_pin[i] != 0) { if (_pin[i] != 0) {
@ -93,7 +100,8 @@ void BusInOut::input() {
unlock(); unlock();
} }
void BusInOut::mode(PinMode pull) { void BusInOut::mode(PinMode pull)
{
lock(); lock();
for (int i = 0; i < 16; i++) { for (int i = 0; i < 16; i++) {
if (_pin[i] != 0) { if (_pin[i] != 0) {
@ -103,35 +111,41 @@ void BusInOut::mode(PinMode pull) {
unlock(); unlock();
} }
BusInOut& BusInOut::operator= (int v) { BusInOut &BusInOut::operator= (int v)
{
// Underlying write is thread safe // Underlying write is thread safe
write(v); write(v);
return *this; return *this;
} }
BusInOut& BusInOut::operator= (BusInOut& rhs) { BusInOut &BusInOut::operator= (BusInOut &rhs)
{
// Underlying read is thread safe // Underlying read is thread safe
write(rhs.read()); write(rhs.read());
return *this; return *this;
} }
DigitalInOut& BusInOut::operator[] (int index) { DigitalInOut &BusInOut::operator[](int index)
{
// No lock needed since _pin is not modified outside the constructor // No lock needed since _pin is not modified outside the constructor
MBED_ASSERT(index >= 0 && index <= 16); MBED_ASSERT(index >= 0 && index <= 16);
MBED_ASSERT(_pin[index]); MBED_ASSERT(_pin[index]);
return *_pin[index]; return *_pin[index];
} }
BusInOut::operator int() { BusInOut::operator int()
{
// Underlying read is thread safe // Underlying read is thread safe
return read(); return read();
} }
void BusInOut::lock() { void BusInOut::lock()
{
_mutex.lock(); _mutex.lock();
} }
void BusInOut::unlock() { void BusInOut::unlock()
{
_mutex.unlock(); _mutex.unlock();
} }

View File

@ -103,7 +103,8 @@ public:
* @returns * @returns
* Binary mask of connected pins * Binary mask of connected pins
*/ */
int mask() { int mask()
{
// No lock needed since _nc_mask is not modified outside the constructor // No lock needed since _nc_mask is not modified outside the constructor
return _nc_mask; return _nc_mask;
} }

View File

@ -18,7 +18,8 @@
namespace mbed { 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}; 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 // No lock needed in the constructor
@ -31,7 +32,8 @@ 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 // No lock needed in the constructor
_nc_mask = 0; _nc_mask = 0;
for (int i = 0; i < 16; i++) { for (int i = 0; i < 16; i++) {
@ -42,7 +44,8 @@ BusOut::BusOut(PinName pins[16]) {
} }
} }
BusOut::~BusOut() { BusOut::~BusOut()
{
// No lock needed in the destructor // No lock needed in the destructor
for (int i = 0; i < 16; i++) { for (int i = 0; i < 16; i++) {
if (_pin[i] != 0) { if (_pin[i] != 0) {
@ -51,7 +54,8 @@ BusOut::~BusOut() {
} }
} }
void BusOut::write(int value) { void BusOut::write(int value)
{
lock(); lock();
for (int i = 0; i < 16; i++) { for (int i = 0; i < 16; i++) {
if (_pin[i] != 0) { if (_pin[i] != 0) {
@ -61,7 +65,8 @@ void BusOut::write(int value) {
unlock(); unlock();
} }
int BusOut::read() { int BusOut::read()
{
lock(); lock();
int v = 0; int v = 0;
for (int i = 0; i < 16; i++) { for (int i = 0; i < 16; i++) {
@ -73,35 +78,41 @@ int BusOut::read() {
return v; return v;
} }
BusOut& BusOut::operator= (int v) { BusOut &BusOut::operator= (int v)
{
// Underlying write is thread safe // Underlying write is thread safe
write(v); write(v);
return *this; return *this;
} }
BusOut& BusOut::operator= (BusOut& rhs) { BusOut &BusOut::operator= (BusOut &rhs)
{
// Underlying write is thread safe // Underlying write is thread safe
write(rhs.read()); write(rhs.read());
return *this; return *this;
} }
DigitalOut& BusOut::operator[] (int index) { DigitalOut &BusOut::operator[](int index)
{
// No lock needed since _pin is not modified outside the constructor // No lock needed since _pin is not modified outside the constructor
MBED_ASSERT(index >= 0 && index <= 16); MBED_ASSERT(index >= 0 && index <= 16);
MBED_ASSERT(_pin[index]); MBED_ASSERT(_pin[index]);
return *_pin[index]; return *_pin[index];
} }
BusOut::operator int() { BusOut::operator int()
{
// Underlying read is thread safe // Underlying read is thread safe
return read(); return read();
} }
void BusOut::lock() { void BusOut::lock()
{
_mutex.lock(); _mutex.lock();
} }
void BusOut::unlock() { void BusOut::unlock()
{
_mutex.unlock(); _mutex.unlock();
} }

View File

@ -87,7 +87,8 @@ public:
* @returns * @returns
* Binary mask of connected pins * Binary mask of connected pins
*/ */
int mask() { int mask()
{
// No lock needed since _nc_mask is not modified outside the constructor // No lock needed since _nc_mask is not modified outside the constructor
return _nc_mask; return _nc_mask;
} }

View File

@ -22,7 +22,8 @@
namespace mbed { namespace mbed {
CAN::CAN(PinName rd, PinName td) : _can(), _irq() { CAN::CAN(PinName rd, PinName td) : _can(), _irq()
{
// No lock needed in constructor // No lock needed in constructor
for (size_t i = 0; i < sizeof _irq / sizeof _irq[0]; i++) { 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_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 // No lock needed in constructor
for (size_t i = 0; i < sizeof _irq / sizeof _irq[0]; i++) { 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_irq_init(&_can, (&CAN::_irq_handler), (uint32_t)this);
} }
CAN::~CAN() { CAN::~CAN()
{
// No lock needed in destructor // No lock needed in destructor
// Detaching interrupts releases the sleep lock if it was locked // Detaching interrupts releases the sleep lock if it was locked
@ -55,68 +58,78 @@ CAN::~CAN() {
can_free(&_can); can_free(&_can);
} }
int CAN::frequency(int f) { int CAN::frequency(int f)
{
lock(); lock();
int ret = can_frequency(&_can, f); int ret = can_frequency(&_can, f);
unlock(); unlock();
return ret; return ret;
} }
int CAN::write(CANMessage msg) { int CAN::write(CANMessage msg)
{
lock(); lock();
int ret = can_write(&_can, msg, 0); int ret = can_write(&_can, msg, 0);
unlock(); unlock();
return ret; return ret;
} }
int CAN::read(CANMessage &msg, int handle) { int CAN::read(CANMessage &msg, int handle)
{
lock(); lock();
int ret = can_read(&_can, &msg, handle); int ret = can_read(&_can, &msg, handle);
unlock(); unlock();
return ret; return ret;
} }
void CAN::reset() { void CAN::reset()
{
lock(); lock();
can_reset(&_can); can_reset(&_can);
unlock(); unlock();
} }
unsigned char CAN::rderror() { unsigned char CAN::rderror()
{
lock(); lock();
int ret = can_rderror(&_can); int ret = can_rderror(&_can);
unlock(); unlock();
return ret; return ret;
} }
unsigned char CAN::tderror() { unsigned char CAN::tderror()
{
lock(); lock();
int ret = can_tderror(&_can); int ret = can_tderror(&_can);
unlock(); unlock();
return ret; return ret;
} }
void CAN::monitor(bool silent) { void CAN::monitor(bool silent)
{
lock(); lock();
can_monitor(&_can, (silent) ? 1 : 0); can_monitor(&_can, (silent) ? 1 : 0);
unlock(); unlock();
} }
int CAN::mode(Mode mode) { int CAN::mode(Mode mode)
{
lock(); lock();
int ret = can_mode(&_can, (CanMode)mode); int ret = can_mode(&_can, (CanMode)mode);
unlock(); unlock();
return ret; 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(); lock();
int ret = can_filter(&_can, id, mask, format, handle); int ret = can_filter(&_can, id, mask, format, handle);
unlock(); unlock();
return ret; return ret;
} }
void CAN::attach(Callback<void()> func, IrqType type) { void CAN::attach(Callback<void()> func, IrqType type)
{
lock(); lock();
if (func) { if (func) {
// lock deep sleep only the first time // lock deep sleep only the first time
@ -136,18 +149,21 @@ void CAN::attach(Callback<void()> func, IrqType type) {
unlock(); unlock();
} }
void CAN::_irq_handler(uint32_t id, CanIrqType type) { void CAN::_irq_handler(uint32_t id, CanIrqType type)
{
CAN *handler = (CAN *)id; CAN *handler = (CAN *)id;
if (handler->_irq[type]) { if (handler->_irq[type]) {
handler->_irq[type].call(); handler->_irq[type].call();
} }
} }
void CAN::lock() { void CAN::lock()
{
_mutex.lock(); _mutex.lock();
} }
void CAN::unlock() { void CAN::unlock()
{
_mutex.unlock(); _mutex.unlock();
} }

View File

@ -38,7 +38,8 @@ class CANMessage : public CAN_Message {
public: public:
/** Creates empty CAN message. /** Creates empty CAN message.
*/ */
CANMessage() : CAN_Message() { CANMessage() : CAN_Message()
{
len = 8; len = 8;
type = CANData; type = CANData;
format = CANStandard; format = CANStandard;
@ -54,7 +55,8 @@ public:
* @param _type Type of Data: Use enum CANType for valid parameter values * @param _type Type of Data: Use enum CANType for valid parameter values
* @param _format Data Format: Use enum CANFormat 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) { CANMessage(int _id, const char *_data, char _len = 8, CANType _type = CANData, CANFormat _format = CANStandard)
{
len = _len & 0xF; len = _len & 0xF;
type = _type; type = _type;
format = _format; format = _format;
@ -67,7 +69,8 @@ public:
* @param _id Message ID * @param _id Message ID
* @param _format Data Format: Use enum CANType for valid parameter values * @param _format Data Format: Use enum CANType for valid parameter values
*/ */
CANMessage(int _id, CANFormat _format = CANStandard) { CANMessage(int _id, CANFormat _format = CANStandard)
{
len = 0; len = 0;
type = CANRemote; type = CANRemote;
format = _format; format = _format;
@ -257,7 +260,8 @@ public:
MBED_DEPRECATED_SINCE("mbed-os-5.1", MBED_DEPRECATED_SINCE("mbed-os-5.1",
"The attach function does not support cv-qualifiers. Replaced by " "The attach function does not support cv-qualifiers. Replaced by "
"attach(callback(obj, method), type).") "attach(callback(obj, method), type).")
void attach(T* obj, void (T::*method)(), IrqType type=RxIrq) { void attach(T *obj, void (T::*method)(), IrqType type = RxIrq)
{
// Underlying call thread safe // Underlying call thread safe
attach(callback(obj, method), type); attach(callback(obj, method), type);
} }
@ -276,7 +280,8 @@ public:
MBED_DEPRECATED_SINCE("mbed-os-5.1", MBED_DEPRECATED_SINCE("mbed-os-5.1",
"The attach function does not support cv-qualifiers. Replaced by " "The attach function does not support cv-qualifiers. Replaced by "
"attach(callback(obj, method), type).") "attach(callback(obj, method), type).")
void attach(T* obj, void (*method)(T*), IrqType type=RxIrq) { void attach(T *obj, void (*method)(T *), IrqType type = RxIrq)
{
// Underlying call thread safe // Underlying call thread safe
attach(callback(obj, method), type); attach(callback(obj, method), type);
} }

View File

@ -55,7 +55,8 @@ public:
* *
* @param pin DigitalIn pin to connect to * @param pin DigitalIn pin to connect to
*/ */
DigitalIn(PinName pin) : gpio() { DigitalIn(PinName pin) : gpio()
{
// No lock needed in the constructor // No lock needed in the constructor
gpio_init_in(&gpio, pin); gpio_init_in(&gpio, pin);
} }
@ -65,7 +66,8 @@ public:
* @param pin DigitalIn pin to connect to * @param pin DigitalIn pin to connect to
* @param mode the initial mode of the pin * @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 // No lock needed in the constructor
gpio_init_in_ex(&gpio, pin, mode); gpio_init_in_ex(&gpio, pin, mode);
} }
@ -75,7 +77,8 @@ public:
* An integer representing the state of the input pin, * An integer representing the state of the input pin,
* 0 for logical 0, 1 for logical 1 * 0 for logical 0, 1 for logical 1
*/ */
int read() { int read()
{
// Thread safe / atomic HAL call // Thread safe / atomic HAL call
return gpio_read(&gpio); return gpio_read(&gpio);
} }
@ -84,7 +87,8 @@ public:
* *
* @param pull PullUp, PullDown, PullNone, OpenDrain * @param pull PullUp, PullDown, PullNone, OpenDrain
*/ */
void mode(PinMode pull) { void mode(PinMode pull)
{
core_util_critical_section_enter(); core_util_critical_section_enter();
gpio_mode(&gpio, pull); gpio_mode(&gpio, pull);
core_util_critical_section_exit(); core_util_critical_section_exit();
@ -96,7 +100,8 @@ public:
* Non zero value if pin is connected to uc GPIO * Non zero value if pin is connected to uc GPIO
* 0 if gpio object was initialized with NC * 0 if gpio object was initialized with NC
*/ */
int is_connected() { int is_connected()
{
// Thread safe / atomic HAL call // Thread safe / atomic HAL call
return gpio_is_connected(&gpio); return gpio_is_connected(&gpio);
} }
@ -104,7 +109,8 @@ public:
/** An operator shorthand for read() /** An operator shorthand for read()
* \sa DigitalIn::read() * \sa DigitalIn::read()
*/ */
operator int() { operator int()
{
// Underlying read is thread safe // Underlying read is thread safe
return read(); return read();
} }

View File

@ -36,7 +36,8 @@ public:
* *
* @param pin DigitalInOut pin to connect to * @param pin DigitalInOut pin to connect to
*/ */
DigitalInOut(PinName pin) : gpio() { DigitalInOut(PinName pin) : gpio()
{
// No lock needed in the constructor // No lock needed in the constructor
gpio_init_in(&gpio, pin); gpio_init_in(&gpio, pin);
} }
@ -48,7 +49,8 @@ public:
* @param mode the initial mode of the pin * @param mode the initial mode of the pin
* @param value the initial value of the pin if is an output * @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 // No lock needed in the constructor
gpio_init_inout(&gpio, pin, direction, mode, value); gpio_init_inout(&gpio, pin, direction, mode, value);
} }
@ -58,7 +60,8 @@ public:
* @param value An integer specifying the pin output value, * @param value An integer specifying the pin output value,
* 0 for logical 0, 1 (or any other non-zero value) for logical 1 * 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 // Thread safe / atomic HAL call
gpio_write(&gpio, value); gpio_write(&gpio, value);
} }
@ -69,14 +72,16 @@ public:
* an integer representing the output setting of the pin if it is an output, * an integer representing the output setting of the pin if it is an output,
* or read the input if set as an input * or read the input if set as an input
*/ */
int read() { int read()
{
// Thread safe / atomic HAL call // Thread safe / atomic HAL call
return gpio_read(&gpio); return gpio_read(&gpio);
} }
/** Set as an output /** Set as an output
*/ */
void output() { void output()
{
core_util_critical_section_enter(); core_util_critical_section_enter();
gpio_dir(&gpio, PIN_OUTPUT); gpio_dir(&gpio, PIN_OUTPUT);
core_util_critical_section_exit(); core_util_critical_section_exit();
@ -84,7 +89,8 @@ public:
/** Set as an input /** Set as an input
*/ */
void input() { void input()
{
core_util_critical_section_enter(); core_util_critical_section_enter();
gpio_dir(&gpio, PIN_INPUT); gpio_dir(&gpio, PIN_INPUT);
core_util_critical_section_exit(); core_util_critical_section_exit();
@ -94,7 +100,8 @@ public:
* *
* @param pull PullUp, PullDown, PullNone, OpenDrain * @param pull PullUp, PullDown, PullNone, OpenDrain
*/ */
void mode(PinMode pull) { void mode(PinMode pull)
{
core_util_critical_section_enter(); core_util_critical_section_enter();
gpio_mode(&gpio, pull); gpio_mode(&gpio, pull);
core_util_critical_section_exit(); core_util_critical_section_exit();
@ -106,7 +113,8 @@ public:
* Non zero value if pin is connected to uc GPIO * Non zero value if pin is connected to uc GPIO
* 0 if gpio object was initialized with NC * 0 if gpio object was initialized with NC
*/ */
int is_connected() { int is_connected()
{
// Thread safe / atomic HAL call // Thread safe / atomic HAL call
return gpio_is_connected(&gpio); return gpio_is_connected(&gpio);
} }
@ -114,7 +122,8 @@ public:
/** A shorthand for write() /** A shorthand for write()
* \sa DigitalInOut::write() * \sa DigitalInOut::write()
*/ */
DigitalInOut& operator= (int value) { DigitalInOut &operator= (int value)
{
// Underlying write is thread safe // Underlying write is thread safe
write(value); write(value);
return *this; return *this;
@ -123,7 +132,8 @@ public:
/** A shorthand for write() /** A shorthand for write()
* \sa DigitalInOut::write() * \sa DigitalInOut::write()
*/ */
DigitalInOut& operator= (DigitalInOut& rhs) { DigitalInOut &operator= (DigitalInOut &rhs)
{
core_util_critical_section_enter(); core_util_critical_section_enter();
write(rhs.read()); write(rhs.read());
core_util_critical_section_exit(); core_util_critical_section_exit();
@ -133,7 +143,8 @@ public:
/** A shorthand for read() /** A shorthand for read()
* \sa DigitalInOut::read() * \sa DigitalInOut::read()
*/ */
operator int() { operator int()
{
// Underlying call is thread safe // Underlying call is thread safe
return read(); return read();
} }

View File

@ -50,7 +50,8 @@ public:
* *
* @param pin DigitalOut pin to connect to * @param pin DigitalOut pin to connect to
*/ */
DigitalOut(PinName pin) : gpio() { DigitalOut(PinName pin) : gpio()
{
// No lock needed in the constructor // No lock needed in the constructor
gpio_init_out(&gpio, pin); gpio_init_out(&gpio, pin);
} }
@ -60,7 +61,8 @@ public:
* @param pin DigitalOut pin to connect to * @param pin DigitalOut pin to connect to
* @param value the initial pin value * @param value the initial pin value
*/ */
DigitalOut(PinName pin, int value) : gpio() { DigitalOut(PinName pin, int value) : gpio()
{
// No lock needed in the constructor // No lock needed in the constructor
gpio_init_out_ex(&gpio, pin, value); gpio_init_out_ex(&gpio, pin, value);
} }
@ -70,7 +72,8 @@ public:
* @param value An integer specifying the pin output value, * @param value An integer specifying the pin output value,
* 0 for logical 0, 1 (or any other non-zero value) for logical 1 * 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 // Thread safe / atomic HAL call
gpio_write(&gpio, value); gpio_write(&gpio, value);
} }
@ -81,7 +84,8 @@ public:
* an integer representing the output setting of the pin, * an integer representing the output setting of the pin,
* 0 for logical 0, 1 for logical 1 * 0 for logical 0, 1 for logical 1
*/ */
int read() { int read()
{
// Thread safe / atomic HAL call // Thread safe / atomic HAL call
return gpio_read(&gpio); return gpio_read(&gpio);
} }
@ -92,7 +96,8 @@ public:
* Non zero value if pin is connected to uc GPIO * Non zero value if pin is connected to uc GPIO
* 0 if gpio object was initialized with NC * 0 if gpio object was initialized with NC
*/ */
int is_connected() { int is_connected()
{
// Thread safe / atomic HAL call // Thread safe / atomic HAL call
return gpio_is_connected(&gpio); return gpio_is_connected(&gpio);
} }
@ -100,7 +105,8 @@ public:
/** A shorthand for write() /** A shorthand for write()
* \sa DigitalOut::write() * \sa DigitalOut::write()
*/ */
DigitalOut& operator= (int value) { DigitalOut &operator= (int value)
{
// Underlying write is thread safe // Underlying write is thread safe
write(value); write(value);
return *this; return *this;
@ -109,7 +115,8 @@ public:
/** A shorthand for write() /** A shorthand for write()
* \sa DigitalOut::write() * \sa DigitalOut::write()
*/ */
DigitalOut& operator= (DigitalOut& rhs) { DigitalOut &operator= (DigitalOut &rhs)
{
core_util_critical_section_enter(); core_util_critical_section_enter();
write(rhs.read()); write(rhs.read());
core_util_critical_section_exit(); core_util_critical_section_exit();
@ -119,7 +126,8 @@ public:
/** A shorthand for read() /** A shorthand for read()
* \sa DigitalOut::read() * \sa DigitalOut::read()
*/ */
operator int() { operator int()
{
// Underlying call is thread safe // Underlying call is thread safe
return read(); return read();
} }

View File

@ -21,48 +21,72 @@
namespace mbed { namespace mbed {
Ethernet::Ethernet() { Ethernet::Ethernet()
{
ethernet_init(); ethernet_init();
} }
Ethernet::~Ethernet() { Ethernet::~Ethernet()
{
ethernet_free(); ethernet_free();
} }
int Ethernet::write(const char *data, int size) { int Ethernet::write(const char *data, int size)
{
return ethernet_write(data, size); return ethernet_write(data, size);
} }
int Ethernet::send() { int Ethernet::send()
{
return ethernet_send(); return ethernet_send();
} }
int Ethernet::receive() { int Ethernet::receive()
{
return ethernet_receive(); return ethernet_receive();
} }
int Ethernet::read(char *data, int size) { int Ethernet::read(char *data, int size)
{
return ethernet_read(data, size); return ethernet_read(data, size);
} }
void Ethernet::address(char *mac) { void Ethernet::address(char *mac)
{
return ethernet_address(mac); return ethernet_address(mac);
} }
int Ethernet::link() { int Ethernet::link()
{
return ethernet_link(); return ethernet_link();
} }
void Ethernet::set_link(Mode mode) { void Ethernet::set_link(Mode mode)
{
int speed = -1; int speed = -1;
int duplex = 0; int duplex = 0;
switch (mode) { switch (mode) {
case AutoNegotiate : speed = -1; duplex = 0; break; case AutoNegotiate :
case HalfDuplex10 : speed = 0; duplex = 0; break; speed = -1;
case FullDuplex10 : speed = 0; duplex = 1; break; duplex = 0;
case HalfDuplex100 : speed = 1; duplex = 0; break; break;
case FullDuplex100 : speed = 1; duplex = 1; 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); ethernet_set_link(speed, duplex);

View File

@ -41,7 +41,8 @@ I2C::I2C(PinName sda, PinName scl) :
_owner = this; _owner = this;
} }
void I2C::frequency(int hz) { void I2C::frequency(int hz)
{
lock(); lock();
_hz = hz; _hz = hz;
@ -53,7 +54,8 @@ void I2C::frequency(int hz) {
unlock(); unlock();
} }
void I2C::aquire() { void I2C::aquire()
{
lock(); lock();
if (_owner != this) { if (_owner != this) {
i2c_frequency(&_i2c, _hz); i2c_frequency(&_i2c, _hz);
@ -63,7 +65,8 @@ void I2C::aquire() {
} }
// write - Master Transmitter Mode // 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(); lock();
aquire(); aquire();
@ -74,7 +77,8 @@ int I2C::write(int address, const char* data, int length, bool repeated) {
return length != written; return length != written;
} }
int I2C::write(int data) { int I2C::write(int data)
{
lock(); lock();
int ret = i2c_byte_write(&_i2c, data); int ret = i2c_byte_write(&_i2c, data);
unlock(); unlock();
@ -82,7 +86,8 @@ int I2C::write(int data) {
} }
// read - Master Receiver Mode // 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(); lock();
aquire(); aquire();
@ -93,7 +98,8 @@ int I2C::read(int address, char* data, int length, bool repeated) {
return length != read; return length != read;
} }
int I2C::read(int ack) { int I2C::read(int ack)
{
lock(); lock();
int ret; int ret;
if (ack) { if (ack) {
@ -105,23 +111,27 @@ int I2C::read(int ack) {
return ret; return ret;
} }
void I2C::start(void) { void I2C::start(void)
{
lock(); lock();
i2c_start(&_i2c); i2c_start(&_i2c);
unlock(); unlock();
} }
void I2C::stop(void) { void I2C::stop(void)
{
lock(); lock();
i2c_stop(&_i2c); i2c_stop(&_i2c);
unlock(); unlock();
} }
void I2C::lock() { void I2C::lock()
{
_mutex->lock(); _mutex->lock();
} }
void I2C::unlock() { void I2C::unlock()
{
_mutex->unlock(); _mutex->unlock();
} }

View File

@ -151,7 +151,8 @@ public:
*/ */
virtual void unlock(void); virtual void unlock(void);
virtual ~I2C() { virtual ~I2C()
{
// Do nothing // Do nothing
} }

View File

@ -19,42 +19,51 @@
namespace mbed { namespace mbed {
I2CSlave::I2CSlave(PinName sda, PinName scl) : _i2c() { I2CSlave::I2CSlave(PinName sda, PinName scl) : _i2c()
{
i2c_init(&_i2c, sda, scl); i2c_init(&_i2c, sda, scl);
i2c_frequency(&_i2c, 100000); i2c_frequency(&_i2c, 100000);
i2c_slave_mode(&_i2c, 1); i2c_slave_mode(&_i2c, 1);
} }
void I2CSlave::frequency(int hz) { void I2CSlave::frequency(int hz)
{
i2c_frequency(&_i2c, hz); i2c_frequency(&_i2c, hz);
} }
void I2CSlave::address(int address) { void I2CSlave::address(int address)
{
int addr = (address & 0xFF) | 1; int addr = (address & 0xFF) | 1;
i2c_slave_address(&_i2c, 0, addr, 0); i2c_slave_address(&_i2c, 0, addr, 0);
} }
int I2CSlave::receive(void) { int I2CSlave::receive(void)
{
return i2c_slave_receive(&_i2c); 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; return i2c_slave_read(&_i2c, data, length) != length;
} }
int I2CSlave::read(void) { int I2CSlave::read(void)
{
return i2c_byte_read(&_i2c, 0); 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; return i2c_slave_write(&_i2c, data, length) != length;
} }
int I2CSlave::write(int data) { int I2CSlave::write(int data)
{
return i2c_byte_write(&_i2c, data); return i2c_byte_write(&_i2c, data);
} }
void I2CSlave::stop(void) { void I2CSlave::stop(void)
{
i2c_stop(&_i2c); i2c_stop(&_i2c);
} }

View File

@ -26,7 +26,8 @@ namespace mbed {
InterruptIn::InterruptIn(PinName pin) : gpio(), InterruptIn::InterruptIn(PinName pin) : gpio(),
gpio_irq(), gpio_irq(),
_rise(NULL), _rise(NULL),
_fall(NULL) { _fall(NULL)
{
// No lock needed in the constructor // No lock needed in the constructor
irq_init(pin); irq_init(pin);
gpio_init_in(&gpio, pin); gpio_init_in(&gpio, pin);
@ -36,33 +37,39 @@ InterruptIn::InterruptIn(PinName pin, PinMode mode) :
gpio(), gpio(),
gpio_irq(), gpio_irq(),
_rise(NULL), _rise(NULL),
_fall(NULL) { _fall(NULL)
{
// No lock needed in the constructor // No lock needed in the constructor
irq_init(pin); irq_init(pin);
gpio_init_in_ex(&gpio, pin, mode); gpio_init_in_ex(&gpio, pin, mode);
} }
void InterruptIn::irq_init(PinName pin) { void InterruptIn::irq_init(PinName pin)
{
gpio_irq_init(&gpio_irq, pin, (&InterruptIn::_irq_handler), (uint32_t)this); gpio_irq_init(&gpio_irq, pin, (&InterruptIn::_irq_handler), (uint32_t)this);
} }
InterruptIn::~InterruptIn() { InterruptIn::~InterruptIn()
{
// No lock needed in the destructor // No lock needed in the destructor
gpio_irq_free(&gpio_irq); gpio_irq_free(&gpio_irq);
} }
int InterruptIn::read() { int InterruptIn::read()
{
// Read only // Read only
return gpio_read(&gpio); return gpio_read(&gpio);
} }
void InterruptIn::mode(PinMode pull) { void InterruptIn::mode(PinMode pull)
{
core_util_critical_section_enter(); core_util_critical_section_enter();
gpio_mode(&gpio, pull); gpio_mode(&gpio, pull);
core_util_critical_section_exit(); core_util_critical_section_exit();
} }
void InterruptIn::rise(Callback<void()> func) { void InterruptIn::rise(Callback<void()> func)
{
core_util_critical_section_enter(); core_util_critical_section_enter();
if (func) { if (func) {
_rise = func; _rise = func;
@ -74,7 +81,8 @@ void InterruptIn::rise(Callback<void()> func) {
core_util_critical_section_exit(); core_util_critical_section_exit();
} }
void InterruptIn::fall(Callback<void()> func) { void InterruptIn::fall(Callback<void()> func)
{
core_util_critical_section_enter(); core_util_critical_section_enter();
if (func) { if (func) {
_fall = func; _fall = func;
@ -86,7 +94,8 @@ void InterruptIn::fall(Callback<void()> func) {
core_util_critical_section_exit(); core_util_critical_section_exit();
} }
void InterruptIn::_irq_handler(uint32_t id, gpio_irq_event event) { void InterruptIn::_irq_handler(uint32_t id, gpio_irq_event event)
{
InterruptIn *handler = (InterruptIn *)id; InterruptIn *handler = (InterruptIn *)id;
switch (event) { switch (event) {
case IRQ_RISE: case IRQ_RISE:
@ -99,23 +108,27 @@ void InterruptIn::_irq_handler(uint32_t id, gpio_irq_event event) {
handler->_fall(); handler->_fall();
} }
break; break;
case IRQ_NONE: break; case IRQ_NONE:
break;
} }
} }
void InterruptIn::enable_irq() { void InterruptIn::enable_irq()
{
core_util_critical_section_enter(); core_util_critical_section_enter();
gpio_irq_enable(&gpio_irq); gpio_irq_enable(&gpio_irq);
core_util_critical_section_exit(); core_util_critical_section_exit();
} }
void InterruptIn::disable_irq() { void InterruptIn::disable_irq()
{
core_util_critical_section_enter(); core_util_critical_section_enter();
gpio_irq_disable(&gpio_irq); gpio_irq_disable(&gpio_irq);
core_util_critical_section_exit(); core_util_critical_section_exit();
} }
InterruptIn::operator int() { InterruptIn::operator int()
{
// Underlying call is atomic // Underlying call is atomic
return read(); return read();
} }

View File

@ -108,7 +108,8 @@ public:
MBED_DEPRECATED_SINCE("mbed-os-5.1", MBED_DEPRECATED_SINCE("mbed-os-5.1",
"The rise function does not support cv-qualifiers. Replaced by " "The rise function does not support cv-qualifiers. Replaced by "
"rise(callback(obj, method)).") "rise(callback(obj, method)).")
void rise(T *obj, M method) { void rise(T *obj, M method)
{
core_util_critical_section_enter(); core_util_critical_section_enter();
rise(callback(obj, method)); rise(callback(obj, method));
core_util_critical_section_exit(); core_util_critical_section_exit();
@ -132,7 +133,8 @@ public:
MBED_DEPRECATED_SINCE("mbed-os-5.1", MBED_DEPRECATED_SINCE("mbed-os-5.1",
"The fall function does not support cv-qualifiers. Replaced by " "The fall function does not support cv-qualifiers. Replaced by "
"fall(callback(obj, method)).") "fall(callback(obj, method)).")
void fall(T *obj, M method) { void fall(T *obj, M method)
{
core_util_critical_section_enter(); core_util_critical_section_enter();
fall(callback(obj, method)); fall(callback(obj, method));
core_util_critical_section_exit(); core_util_critical_section_exit();

View File

@ -34,7 +34,8 @@ typedef void (*pvoidf)(void);
InterruptManager *InterruptManager::_instance = (InterruptManager *)NULL; InterruptManager *InterruptManager::_instance = (InterruptManager *)NULL;
InterruptManager* InterruptManager::get() { InterruptManager *InterruptManager::get()
{
if (NULL == _instance) { if (NULL == _instance) {
InterruptManager *temp = new InterruptManager(); InterruptManager *temp = new InterruptManager();
@ -55,12 +56,14 @@ InterruptManager* InterruptManager::get() {
return _instance; return _instance;
} }
InterruptManager::InterruptManager() { InterruptManager::InterruptManager()
{
// No mutex needed in constructor // 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 // Not a good idea to call this unless NO interrupt at all
// is under the control of the handler; otherwise, a system crash // is under the control of the handler; otherwise, a system crash
// is very likely to occur // is very likely to occur
@ -70,13 +73,16 @@ void InterruptManager::destroy() {
} }
} }
InterruptManager::~InterruptManager() { InterruptManager::~InterruptManager()
{
for (int i = 0; i < NVIC_NUM_VECTORS; i++) for (int i = 0; i < NVIC_NUM_VECTORS; i++)
if (NULL != _chains[i]) if (NULL != _chains[i]) {
delete _chains[i]; delete _chains[i];
} }
}
bool InterruptManager::must_replace_vector(IRQn_Type irq) { bool InterruptManager::must_replace_vector(IRQn_Type irq)
{
lock(); lock();
int ret = false; int ret = false;
@ -90,19 +96,22 @@ bool InterruptManager::must_replace_vector(IRQn_Type irq) {
return ret; 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(); lock();
int irq_pos = get_irq_index(irq); int irq_pos = get_irq_index(irq);
bool change = must_replace_vector(irq); bool change = must_replace_vector(irq);
pFunctionPointer_t pf = front ? _chains[irq_pos]->add_front(function) : _chains[irq_pos]->add(function); 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); NVIC_SetVector(irq, (uint32_t)&InterruptManager::static_irq_helper);
}
unlock(); unlock();
return pf; 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); int irq_pos = get_irq_index(irq);
bool ret = false; bool ret = false;
@ -117,24 +126,29 @@ bool InterruptManager::remove_handler(pFunctionPointer_t handler, IRQn_Type irq)
return ret; return ret;
} }
void InterruptManager::irq_helper() { void InterruptManager::irq_helper()
{
_chains[__get_IPSR()]->call(); _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 // Pure function - no lock needed
return (int)irq + NVIC_USER_IRQ_OFFSET; return (int)irq + NVIC_USER_IRQ_OFFSET;
} }
void InterruptManager::static_irq_helper() { void InterruptManager::static_irq_helper()
{
InterruptManager::get()->irq_helper(); InterruptManager::get()->irq_helper();
} }
void InterruptManager::lock() { void InterruptManager::lock()
{
_mutex.lock(); _mutex.lock();
} }
void InterruptManager::unlock() { void InterruptManager::unlock()
{
_mutex.unlock(); _mutex.unlock();
} }

View File

@ -88,7 +88,8 @@ public:
*/ */
MBED_DEPRECATED_SINCE("mbed-os-5.6", "This class is not part of the " 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.")
pFunctionPointer_t add_handler(void (*function)(void), IRQn_Type irq) { pFunctionPointer_t add_handler(void (*function)(void), IRQn_Type irq)
{
// Underlying call is thread safe // Underlying call is thread safe
return add_common(function, irq); return add_common(function, irq);
} }
@ -105,7 +106,8 @@ public:
*/ */
MBED_DEPRECATED_SINCE("mbed-os-5.6", "This class is not part of the " 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.")
pFunctionPointer_t add_handler_front(void (*function)(void), IRQn_Type irq) { pFunctionPointer_t add_handler_front(void (*function)(void), IRQn_Type irq)
{
// Underlying call is thread safe // Underlying call is thread safe
return add_common(function, irq, true); return add_common(function, irq, true);
} }
@ -124,7 +126,8 @@ public:
template<typename T> template<typename T>
MBED_DEPRECATED_SINCE("mbed-os-5.6", "This class is not part of the " 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.")
pFunctionPointer_t add_handler(T* tptr, void (T::*mptr)(void), IRQn_Type irq) { pFunctionPointer_t add_handler(T *tptr, void (T::*mptr)(void), IRQn_Type irq)
{
// Underlying call is thread safe // Underlying call is thread safe
return add_common(tptr, mptr, irq); return add_common(tptr, mptr, irq);
} }
@ -143,7 +146,8 @@ public:
template<typename T> template<typename T>
MBED_DEPRECATED_SINCE("mbed-os-5.6", "This class is not part of the " 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.")
pFunctionPointer_t add_handler_front(T* tptr, void (T::*mptr)(void), IRQn_Type irq) { pFunctionPointer_t add_handler_front(T *tptr, void (T::*mptr)(void), IRQn_Type irq)
{
// Underlying call is thread safe // Underlying call is thread safe
return add_common(tptr, mptr, irq, true); return add_common(tptr, mptr, irq, true);
} }
@ -170,14 +174,16 @@ private:
void unlock(); void unlock();
template<typename T> 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(); _mutex.lock();
int irq_pos = get_irq_index(irq); int irq_pos = get_irq_index(irq);
bool change = must_replace_vector(irq); bool change = must_replace_vector(irq);
pFunctionPointer_t pf = front ? _chains[irq_pos]->add_front(tptr, mptr) : _chains[irq_pos]->add(tptr, mptr); 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); NVIC_SetVector(irq, (uint32_t)&InterruptManager::static_irq_helper);
}
_mutex.unlock(); _mutex.unlock();
return pf; return pf;
} }

View File

@ -35,10 +35,12 @@ namespace mbed {
class LowPowerTicker : public Ticker, private NonCopyable<LowPowerTicker> { class LowPowerTicker : public Ticker, private NonCopyable<LowPowerTicker> {
public: 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> { class LowPowerTimeout : public LowPowerTicker, private NonCopyable<LowPowerTimeout> {
private: private:
virtual void handler(void) { virtual void handler(void)
{
_function.call(); _function.call();
} }
}; };

View File

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

View File

@ -93,8 +93,7 @@ namespace mbed {
*/ */
template <uint32_t polynomial = POLY_32BIT_ANSI, uint8_t width = 32> template <uint32_t polynomial = POLY_32BIT_ANSI, uint8_t width = 32>
class MbedCRC class MbedCRC {
{
public: public:
enum CrcMode { HARDWARE = 0, TABLE, BITWISE }; enum CrcMode { HARDWARE = 0, TABLE, BITWISE };
@ -170,8 +169,7 @@ public:
*/ */
int32_t compute_partial(void *buffer, crc_data_size_t size, uint32_t *crc) int32_t compute_partial(void *buffer, crc_data_size_t size, uint32_t *crc)
{ {
switch (_mode) switch (_mode) {
{
case HARDWARE: case HARDWARE:
#ifdef DEVICE_CRC #ifdef DEVICE_CRC
hal_crc_compute_partial((uint8_t *)buffer, size); hal_crc_compute_partial((uint8_t *)buffer, size);

View File

@ -60,7 +60,8 @@ public:
* @param port Port to connect to (Port0-Port5) * @param port Port to connect to (Port0-Port5)
* @param mask A bitmask to identify which bits in the port should be included (0 - ignore) * @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(); core_util_critical_section_enter();
port_init(&_port, port, mask, PIN_INPUT); port_init(&_port, port, mask, PIN_INPUT);
core_util_critical_section_exit(); core_util_critical_section_exit();
@ -71,7 +72,8 @@ public:
* @returns * @returns
* An integer with each bit corresponding to associated port pin setting * An integer with each bit corresponding to associated port pin setting
*/ */
int read() { int read()
{
return port_read(&_port); return port_read(&_port);
} }
@ -79,7 +81,8 @@ public:
* *
* @param mode PullUp, PullDown, PullNone, OpenDrain * @param mode PullUp, PullDown, PullNone, OpenDrain
*/ */
void mode(PinMode mode) { void mode(PinMode mode)
{
core_util_critical_section_enter(); core_util_critical_section_enter();
port_mode(&_port, mode); port_mode(&_port, mode);
core_util_critical_section_exit(); core_util_critical_section_exit();
@ -87,7 +90,8 @@ public:
/** A shorthand for read() /** A shorthand for read()
*/ */
operator int() { operator int()
{
return read(); return read();
} }

View File

@ -39,7 +39,8 @@ public:
* @param port Port to connect to (Port0-Port5) * @param port Port to connect to (Port0-Port5)
* @param mask A bitmask to identify which bits in the port should be included (0 - ignore) * @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(); core_util_critical_section_enter();
port_init(&_port, port, mask, PIN_INPUT); port_init(&_port, port, mask, PIN_INPUT);
core_util_critical_section_exit(); core_util_critical_section_exit();
@ -49,7 +50,8 @@ public:
* *
* @param value An integer specifying a bit to write for every corresponding port pin * @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); port_write(&_port, value);
} }
@ -58,13 +60,15 @@ public:
* @returns * @returns
* An integer with each bit corresponding to associated port pin setting * An integer with each bit corresponding to associated port pin setting
*/ */
int read() { int read()
{
return port_read(&_port); return port_read(&_port);
} }
/** Set as an output /** Set as an output
*/ */
void output() { void output()
{
core_util_critical_section_enter(); core_util_critical_section_enter();
port_dir(&_port, PIN_OUTPUT); port_dir(&_port, PIN_OUTPUT);
core_util_critical_section_exit(); core_util_critical_section_exit();
@ -72,7 +76,8 @@ public:
/** Set as an input /** Set as an input
*/ */
void input() { void input()
{
core_util_critical_section_enter(); core_util_critical_section_enter();
port_dir(&_port, PIN_INPUT); port_dir(&_port, PIN_INPUT);
core_util_critical_section_exit(); core_util_critical_section_exit();
@ -82,7 +87,8 @@ public:
* *
* @param mode PullUp, PullDown, PullNone, OpenDrain * @param mode PullUp, PullDown, PullNone, OpenDrain
*/ */
void mode(PinMode mode) { void mode(PinMode mode)
{
core_util_critical_section_enter(); core_util_critical_section_enter();
port_mode(&_port, mode); port_mode(&_port, mode);
core_util_critical_section_exit(); core_util_critical_section_exit();
@ -91,7 +97,8 @@ public:
/** A shorthand for write() /** A shorthand for write()
* \sa PortInOut::write() * \sa PortInOut::write()
*/ */
PortInOut& operator= (int value) { PortInOut &operator= (int value)
{
write(value); write(value);
return *this; return *this;
} }
@ -99,7 +106,8 @@ public:
/** A shorthand for write() /** A shorthand for write()
* \sa PortInOut::write() * \sa PortInOut::write()
*/ */
PortInOut& operator= (PortInOut& rhs) { PortInOut &operator= (PortInOut &rhs)
{
write(rhs.read()); write(rhs.read());
return *this; return *this;
} }
@ -107,7 +115,8 @@ public:
/** A shorthand for read() /** A shorthand for read()
* \sa PortInOut::read() * \sa PortInOut::read()
*/ */
operator int() { operator int()
{
return read(); return read();
} }

View File

@ -59,7 +59,8 @@ public:
* @param port Port to connect to (Port0-Port5) * @param port Port to connect to (Port0-Port5)
* @param mask A bitmask to identify which bits in the port should be included (0 - ignore) * @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(); core_util_critical_section_enter();
port_init(&_port, port, mask, PIN_OUTPUT); port_init(&_port, port, mask, PIN_OUTPUT);
core_util_critical_section_exit(); core_util_critical_section_exit();
@ -69,7 +70,8 @@ public:
* *
* @param value An integer specifying a bit to write for every corresponding PortOut pin * @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); port_write(&_port, value);
} }
@ -78,14 +80,16 @@ public:
* @returns * @returns
* An integer with each bit corresponding to associated PortOut pin setting * An integer with each bit corresponding to associated PortOut pin setting
*/ */
int read() { int read()
{
return port_read(&_port); return port_read(&_port);
} }
/** A shorthand for write() /** A shorthand for write()
* \sa PortOut::write() * \sa PortOut::write()
*/ */
PortOut& operator= (int value) { PortOut &operator= (int value)
{
write(value); write(value);
return *this; return *this;
} }
@ -93,7 +97,8 @@ public:
/** A shorthand for read() /** A shorthand for read()
* \sa PortOut::read() * \sa PortOut::read()
*/ */
PortOut& operator= (PortOut& rhs) { PortOut &operator= (PortOut &rhs)
{
write(rhs.read()); write(rhs.read());
return *this; return *this;
} }
@ -101,7 +106,8 @@ public:
/** A shorthand for read() /** A shorthand for read()
* \sa PortOut::read() * \sa PortOut::read()
*/ */
operator int() { operator int()
{
return read(); return read();
} }

View File

@ -57,13 +57,15 @@ public:
* *
* @param pin PwmOut pin to connect to * @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(); core_util_critical_section_enter();
pwmout_init(&_pwm, pin); pwmout_init(&_pwm, pin);
core_util_critical_section_exit(); core_util_critical_section_exit();
} }
~PwmOut() { ~PwmOut()
{
core_util_critical_section_enter(); core_util_critical_section_enter();
unlock_deep_sleep(); unlock_deep_sleep();
core_util_critical_section_exit(); core_util_critical_section_exit();
@ -76,7 +78,8 @@ public:
* 0.0f (representing on 0%) and 1.0f (representing on 100%). * 0.0f (representing on 0%) and 1.0f (representing on 100%).
* Values outside this range will be saturated to 0.0f or 1.0f. * 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(); core_util_critical_section_enter();
lock_deep_sleep(); lock_deep_sleep();
pwmout_write(&_pwm, value); pwmout_write(&_pwm, value);
@ -93,7 +96,8 @@ public:
* @note * @note
* This value may not match exactly the value set by a previous write(). * This value may not match exactly the value set by a previous write().
*/ */
float read() { float read()
{
core_util_critical_section_enter(); core_util_critical_section_enter();
float val = pwmout_read(&_pwm); float val = pwmout_read(&_pwm);
core_util_critical_section_exit(); core_util_critical_section_exit();
@ -107,7 +111,8 @@ public:
* The resolution is currently in microseconds; periods smaller than this * The resolution is currently in microseconds; periods smaller than this
* will be set to zero. * will be set to zero.
*/ */
void period(float seconds) { void period(float seconds)
{
core_util_critical_section_enter(); core_util_critical_section_enter();
pwmout_period(&_pwm, seconds); pwmout_period(&_pwm, seconds);
core_util_critical_section_exit(); 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. /** 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 * @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(); core_util_critical_section_enter();
pwmout_period_ms(&_pwm, ms); pwmout_period_ms(&_pwm, ms);
core_util_critical_section_exit(); 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. /** 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 * @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(); core_util_critical_section_enter();
pwmout_period_us(&_pwm, us); pwmout_period_us(&_pwm, us);
core_util_critical_section_exit(); core_util_critical_section_exit();
@ -134,7 +141,8 @@ public:
/** Set the PWM pulsewidth, specified in seconds (float), keeping the period the same. /** 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) * @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(); core_util_critical_section_enter();
pwmout_pulsewidth(&_pwm, seconds); pwmout_pulsewidth(&_pwm, seconds);
core_util_critical_section_exit(); core_util_critical_section_exit();
@ -143,7 +151,8 @@ public:
/** Set the PWM pulsewidth, specified in milli-seconds (int), keeping the period the same. /** 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 * @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(); core_util_critical_section_enter();
pwmout_pulsewidth_ms(&_pwm, ms); pwmout_pulsewidth_ms(&_pwm, ms);
core_util_critical_section_exit(); core_util_critical_section_exit();
@ -152,7 +161,8 @@ public:
/** Set the PWM pulsewidth, specified in micro-seconds (int), keeping the period the same. /** 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(); core_util_critical_section_enter();
pwmout_pulsewidth_us(&_pwm, us); pwmout_pulsewidth_us(&_pwm, us);
core_util_critical_section_exit(); core_util_critical_section_exit();
@ -161,7 +171,8 @@ public:
/** A operator shorthand for write() /** A operator shorthand for write()
* \sa PwmOut::write() * \sa PwmOut::write()
*/ */
PwmOut& operator= (float value) { PwmOut &operator= (float value)
{
// Underlying call is thread safe // Underlying call is thread safe
write(value); write(value);
return *this; return *this;
@ -170,7 +181,8 @@ public:
/** A operator shorthand for write() /** A operator shorthand for write()
* \sa PwmOut::write() * \sa PwmOut::write()
*/ */
PwmOut& operator= (PwmOut& rhs) { PwmOut &operator= (PwmOut &rhs)
{
// Underlying call is thread safe // Underlying call is thread safe
write(rhs.read()); write(rhs.read());
return *this; return *this;
@ -179,14 +191,16 @@ public:
/** An operator shorthand for read() /** An operator shorthand for read()
* \sa PwmOut::read() * \sa PwmOut::read()
*/ */
operator float() { operator float()
{
// Underlying call is thread safe // Underlying call is thread safe
return read(); return read();
} }
protected: protected:
/** Lock deep sleep only if it is not yet locked */ /** Lock deep sleep only if it is not yet locked */
void lock_deep_sleep() { void lock_deep_sleep()
{
if (_deep_sleep_locked == false) { if (_deep_sleep_locked == false) {
sleep_manager_lock_deep_sleep(); sleep_manager_lock_deep_sleep();
_deep_sleep_locked = true; _deep_sleep_locked = true;
@ -194,7 +208,8 @@ protected:
} }
/** Unlock deep sleep in case it is locked */ /** Unlock deep sleep in case it is locked */
void unlock_deep_sleep() { void unlock_deep_sleep()
{
if (_deep_sleep_locked == true) { if (_deep_sleep_locked == true) {
sleep_manager_unlock_deep_sleep(); sleep_manager_unlock_deep_sleep();
_deep_sleep_locked = false; _deep_sleep_locked = false;

View File

@ -25,28 +25,33 @@
namespace mbed { 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 // No lock needed in the constructor
} }
int RawSerial::getc() { int RawSerial::getc()
{
lock(); lock();
int ret = _base_getc(); int ret = _base_getc();
unlock(); unlock();
return ret; return ret;
} }
int RawSerial::putc(int c) { int RawSerial::putc(int c)
{
lock(); lock();
int ret = _base_putc(c); int ret = _base_putc(c);
unlock(); unlock();
return ret; return ret;
} }
int RawSerial::puts(const char *str) { int RawSerial::puts(const char *str)
{
lock(); lock();
while (*str) while (*str) {
putc(*str ++); putc(*str ++);
}
unlock(); unlock();
return 0; return 0;
} }
@ -55,7 +60,8 @@ int RawSerial::puts(const char *str) {
// means we can't call printf() directly, so we use sprintf() instead. // means we can't call printf() directly, so we use sprintf() instead.
// We only call malloc() for the sprintf() buffer if the buffer // We only call malloc() for the sprintf() buffer if the buffer
// length is above a certain threshold, otherwise we use just the stack. // 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(); lock();
std::va_list arg; std::va_list arg;
va_start(arg, format); va_start(arg, format);
@ -80,13 +86,15 @@ int RawSerial::printf(const char *format, ...) {
/** Acquire exclusive access to this serial port /** Acquire exclusive access to this serial port
*/ */
void RawSerial::lock() { void RawSerial::lock()
{
// No lock used - external synchronization required // No lock used - external synchronization required
} }
/** Release exclusive access to this serial port /** Release exclusive access to this serial port
*/ */
void RawSerial::unlock() { void RawSerial::unlock()
{
// No lock used - external synchronization required // No lock used - external synchronization required
} }

View File

@ -38,14 +38,16 @@ SPI::SPI(PinName mosi, PinName miso, PinName sclk, PinName ssel) :
_bits(8), _bits(8),
_mode(0), _mode(0),
_hz(1000000), _hz(1000000),
_write_fill(SPI_FILL_CHAR) { _write_fill(SPI_FILL_CHAR)
{
// No lock needed in the constructor // No lock needed in the constructor
spi_init(&_spi, mosi, miso, sclk, ssel); spi_init(&_spi, mosi, miso, sclk, ssel);
_acquire(); _acquire();
} }
void SPI::format(int bits, int mode) { void SPI::format(int bits, int mode)
{
lock(); lock();
_bits = bits; _bits = bits;
_mode = mode; _mode = mode;
@ -60,7 +62,8 @@ void SPI::format(int bits, int mode) {
unlock(); unlock();
} }
void SPI::frequency(int hz) { void SPI::frequency(int hz)
{
lock(); lock();
_hz = hz; _hz = hz;
// If changing format while you are the owner then just // If changing format while you are the owner then just
@ -78,7 +81,8 @@ SPI* SPI::_owner = NULL;
SingletonPtr<PlatformMutex> SPI::_mutex; SingletonPtr<PlatformMutex> SPI::_mutex;
// ignore the fact there are multiple physical spis, and always update if it wasn't us last // ignore the fact there are multiple physical spis, and always update if it wasn't us last
void SPI::aquire() { void SPI::aquire()
{
lock(); lock();
if (_owner != this) { if (_owner != this) {
spi_format(&_spi, _bits, _mode, 0); spi_format(&_spi, _bits, _mode, 0);
@ -89,7 +93,8 @@ void SPI::aquire() {
} }
// Note: Private function with no locking // Note: Private function with no locking
void SPI::_acquire() { void SPI::_acquire()
{
if (_owner != this) { if (_owner != this) {
spi_format(&_spi, _bits, _mode, 0); spi_format(&_spi, _bits, _mode, 0);
spi_frequency(&_spi, _hz); spi_frequency(&_spi, _hz);
@ -97,7 +102,8 @@ void SPI::_acquire() {
} }
} }
int SPI::write(int value) { int SPI::write(int value)
{
lock(); lock();
_acquire(); _acquire();
int ret = spi_master_write(&_spi, value); int ret = spi_master_write(&_spi, value);
@ -105,7 +111,8 @@ int SPI::write(int value) {
return ret; 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(); lock();
_acquire(); _acquire();
int ret = spi_master_block_write(&_spi, tx_buffer, tx_length, rx_buffer, rx_length, _write_fill); 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; return ret;
} }
void SPI::lock() { void SPI::lock()
{
_mutex->lock(); _mutex->lock();
} }
void SPI::unlock() { void SPI::unlock()
{
_mutex->unlock(); _mutex->unlock();
} }
void SPI::set_default_write_value(char data) { void SPI::set_default_write_value(char data)
{
lock(); lock();
_write_fill = data; _write_fill = data;
unlock(); unlock();

View File

@ -169,7 +169,8 @@ public:
* @return Zero if the transfer has started, or -1 if SPI peripheral is busy * @return Zero if the transfer has started, or -1 if SPI peripheral is busy
*/ */
template<typename Type> 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)) { 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);
} }
@ -272,7 +273,8 @@ private:
#endif #endif
public: public:
virtual ~SPI() { virtual ~SPI()
{
} }
protected: protected:

View File

@ -30,26 +30,31 @@ SPISlave::SPISlave(PinName mosi, PinName miso, PinName sclk, PinName ssel) :
spi_frequency(&_spi, _hz); spi_frequency(&_spi, _hz);
} }
void SPISlave::format(int bits, int mode) { void SPISlave::format(int bits, int mode)
{
_bits = bits; _bits = bits;
_mode = mode; _mode = mode;
spi_format(&_spi, _bits, _mode, 1); spi_format(&_spi, _bits, _mode, 1);
} }
void SPISlave::frequency(int hz) { void SPISlave::frequency(int hz)
{
_hz = hz; _hz = hz;
spi_frequency(&_spi, _hz); spi_frequency(&_spi, _hz);
} }
int SPISlave::receive(void) { int SPISlave::receive(void)
{
return (spi_slave_receive(&_spi)); return (spi_slave_receive(&_spi));
} }
int SPISlave::read(void) { int SPISlave::read(void)
{
return (spi_slave_read(&_spi)); return (spi_slave_read(&_spi));
} }
void SPISlave::reply(int value) { void SPISlave::reply(int value)
{
spi_slave_write(&_spi, value); spi_slave_write(&_spi, value);
} }

View File

@ -20,27 +20,33 @@
namespace mbed { 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 // Mutex is already held
return _base_getc(); return _base_getc();
} }
int Serial::_putc(int c) { int Serial::_putc(int c)
{
// Mutex is already held // Mutex is already held
return _base_putc(c); return _base_putc(c);
} }
void Serial::lock() { void Serial::lock()
{
_mutex.lock(); _mutex.lock();
} }
void Serial::unlock() { void Serial::unlock()
{
_mutex.unlock(); _mutex.unlock();
} }

View File

@ -28,7 +28,8 @@ SerialBase::SerialBase(PinName tx, PinName rx, int baud) :
_rx_usage(DMA_USAGE_NEVER), _tx_callback(NULL), _rx_usage(DMA_USAGE_NEVER), _tx_callback(NULL),
_rx_callback(NULL), _rx_callback(NULL),
#endif #endif
_serial(), _baud(baud) { _serial(), _baud(baud)
{
// No lock needed in the constructor // No lock needed in the constructor
for (size_t i = 0; i < sizeof _irq / sizeof _irq[0]; i++) { 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); serial_irq_handler(&_serial, SerialBase::_irq_handler, (uint32_t)this);
} }
void SerialBase::baud(int baudrate) { void SerialBase::baud(int baudrate)
{
lock(); lock();
serial_baud(&_serial, baudrate); serial_baud(&_serial, baudrate);
_baud = baudrate; _baud = baudrate;
unlock(); unlock();
} }
void SerialBase::format(int bits, Parity parity, int stop_bits) { void SerialBase::format(int bits, Parity parity, int stop_bits)
{
lock(); lock();
serial_format(&_serial, bits, (SerialParity)parity, stop_bits); serial_format(&_serial, bits, (SerialParity)parity, stop_bits);
unlock(); unlock();
} }
int SerialBase::readable() { int SerialBase::readable()
{
lock(); lock();
int ret = serial_readable(&_serial); int ret = serial_readable(&_serial);
unlock(); unlock();
@ -61,14 +65,16 @@ int SerialBase::readable() {
} }
int SerialBase::writeable() { int SerialBase::writeable()
{
lock(); lock();
int ret = serial_writable(&_serial); int ret = serial_writable(&_serial);
unlock(); unlock();
return ret; return ret;
} }
void SerialBase::attach(Callback<void()> func, IrqType type) { void SerialBase::attach(Callback<void()> func, IrqType type)
{
lock(); lock();
// Disable interrupts when attaching interrupt handler // Disable interrupts when attaching interrupt handler
core_util_critical_section_enter(); core_util_critical_section_enter();
@ -91,25 +97,29 @@ void SerialBase::attach(Callback<void()> func, IrqType type) {
unlock(); unlock();
} }
void SerialBase::_irq_handler(uint32_t id, SerialIrq irq_type) { void SerialBase::_irq_handler(uint32_t id, SerialIrq irq_type)
{
SerialBase *handler = (SerialBase *)id; SerialBase *handler = (SerialBase *)id;
if (handler->_irq[irq_type]) { if (handler->_irq[irq_type]) {
handler->_irq[irq_type](); handler->_irq[irq_type]();
} }
} }
int SerialBase::_base_getc() { int SerialBase::_base_getc()
{
// Mutex is already held // Mutex is already held
return serial_getc(&_serial); return serial_getc(&_serial);
} }
int SerialBase::_base_putc(int c) { int SerialBase::_base_putc(int c)
{
// Mutex is already held // Mutex is already held
serial_putc(&_serial, c); serial_putc(&_serial, c);
return c; return c;
} }
void SerialBase::send_break() { void SerialBase::send_break()
{
lock(); lock();
// Wait for 1.5 frames before clearing the break condition // Wait for 1.5 frames before clearing the break condition
// This will have different effects on our platforms, but should // This will have different effects on our platforms, but should
@ -125,11 +135,13 @@ void SerialBase::send_break() {
unlock(); unlock();
} }
void SerialBase::lock() { void SerialBase::lock()
{
// Stub // Stub
} }
void SerialBase:: unlock() { void SerialBase:: unlock()
{
// Stub // Stub
} }
@ -144,7 +156,8 @@ SerialBase::~SerialBase()
} }
#if DEVICE_SERIAL_FC #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(); lock();
FlowControl flow_type = (FlowControl)type; FlowControl flow_type = (FlowControl)type;
switch (type) { switch (type) {

View File

@ -114,7 +114,8 @@ public:
MBED_DEPRECATED_SINCE("mbed-os-5.1", MBED_DEPRECATED_SINCE("mbed-os-5.1",
"The attach function does not support cv-qualifiers. Replaced by " "The attach function does not support cv-qualifiers. Replaced by "
"attach(callback(obj, method), type).") "attach(callback(obj, method), type).")
void attach(T *obj, void (T::*method)(), IrqType type=RxIrq) { void attach(T *obj, void (T::*method)(), IrqType type = RxIrq)
{
attach(callback(obj, method), type); attach(callback(obj, method), type);
} }
@ -131,7 +132,8 @@ public:
MBED_DEPRECATED_SINCE("mbed-os-5.1", MBED_DEPRECATED_SINCE("mbed-os-5.1",
"The attach function does not support cv-qualifiers. Replaced by " "The attach function does not support cv-qualifiers. Replaced by "
"attach(callback(obj, method), type).") "attach(callback(obj, method), type).")
void attach(T *obj, void (*method)(T*), IrqType type=RxIrq) { void attach(T *obj, void (*method)(T *), IrqType type = RxIrq)
{
attach(callback(obj, method), type); attach(callback(obj, method), type);
} }

View File

@ -22,7 +22,8 @@
namespace mbed { namespace mbed {
void Ticker::detach() { void Ticker::detach()
{
core_util_critical_section_enter(); core_util_critical_section_enter();
remove(); remove();
// unlocked only if we were attached (we locked it) and this is not low power ticker // unlocked only if we were attached (we locked it) and this is not low power ticker
@ -34,7 +35,8 @@ void Ticker::detach() {
core_util_critical_section_exit(); core_util_critical_section_exit();
} }
void Ticker::setup(us_timestamp_t t) { void Ticker::setup(us_timestamp_t t)
{
core_util_critical_section_enter(); core_util_critical_section_enter();
remove(); remove();
_delay = t; _delay = t;
@ -42,7 +44,8 @@ void Ticker::setup(us_timestamp_t t) {
core_util_critical_section_exit(); core_util_critical_section_exit();
} }
void Ticker::handler() { void Ticker::handler()
{
insert_absolute(event.timestamp + _delay); insert_absolute(event.timestamp + _delay);
if (_function) { if (_function) {
_function(); _function();

View File

@ -66,11 +66,13 @@ namespace mbed {
class Ticker : public TimerEvent, private NonCopyable<Ticker> { class Ticker : public TimerEvent, private NonCopyable<Ticker> {
public: 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. // 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 #if DEVICE_LPTICKER
_lock_deepsleep = (data != get_lp_ticker_data()); _lock_deepsleep = (data != get_lp_ticker_data());
#endif #endif
@ -81,7 +83,8 @@ public:
* @param func pointer to the function to be called * @param func pointer to the function to be called
* @param t the time between calls in seconds * @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); attach_us(func, t * 1000000.0f);
} }
@ -98,7 +101,8 @@ public:
MBED_DEPRECATED_SINCE("mbed-os-5.1", MBED_DEPRECATED_SINCE("mbed-os-5.1",
"The attach function does not support cv-qualifiers. Replaced by " "The attach function does not support cv-qualifiers. Replaced by "
"attach(callback(obj, method), t).") "attach(callback(obj, method), t).")
void attach(T *obj, M method, float t) { void attach(T *obj, M method, float t)
{
attach(callback(obj, method), t); attach(callback(obj, method), t);
} }
@ -112,7 +116,8 @@ public:
* for threads scheduling. * 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(); core_util_critical_section_enter();
// lock only for the initial callback setup and this is not low power ticker // lock only for the initial callback setup and this is not low power ticker
if (!_function && _lock_deepsleep) { if (!_function && _lock_deepsleep) {
@ -136,11 +141,13 @@ public:
MBED_DEPRECATED_SINCE("mbed-os-5.1", MBED_DEPRECATED_SINCE("mbed-os-5.1",
"The attach_us function does not support cv-qualifiers. Replaced by " "The attach_us function does not support cv-qualifiers. Replaced by "
"attach_us(callback(obj, method), t).") "attach_us(callback(obj, method), t).")
void attach_us(T *obj, M method, us_timestamp_t t) { void attach_us(T *obj, M method, us_timestamp_t t)
{
attach_us(Callback<void()>(obj, method), t); attach_us(Callback<void()>(obj, method), t);
} }
virtual ~Ticker() { virtual ~Ticker()
{
detach(); detach();
} }

View File

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

View File

@ -21,18 +21,21 @@
namespace mbed { 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(); 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(); reset();
#if DEVICE_LPTICKER #if DEVICE_LPTICKER
_lock_deepsleep = (data != get_lp_ticker_data()); _lock_deepsleep = (data != get_lp_ticker_data());
#endif #endif
} }
Timer::~Timer() { Timer::~Timer()
{
core_util_critical_section_enter(); core_util_critical_section_enter();
if (_running) { if (_running) {
if (_lock_deepsleep) { if (_lock_deepsleep) {
@ -43,7 +46,8 @@ Timer::~Timer() {
core_util_critical_section_exit(); core_util_critical_section_exit();
} }
void Timer::start() { void Timer::start()
{
core_util_critical_section_enter(); core_util_critical_section_enter();
if (!_running) { if (!_running) {
if (_lock_deepsleep) { if (_lock_deepsleep) {
@ -55,7 +59,8 @@ void Timer::start() {
core_util_critical_section_exit(); core_util_critical_section_exit();
} }
void Timer::stop() { void Timer::stop()
{
core_util_critical_section_enter(); core_util_critical_section_enter();
_time += slicetime(); _time += slicetime();
if (_running) { if (_running) {
@ -67,26 +72,31 @@ void Timer::stop() {
core_util_critical_section_exit(); core_util_critical_section_exit();
} }
int Timer::read_us() { int Timer::read_us()
{
return read_high_resolution_us(); return read_high_resolution_us();
} }
float Timer::read() { float Timer::read()
{
return (float)read_us() / 1000000.0f; return (float)read_us() / 1000000.0f;
} }
int Timer::read_ms() { int Timer::read_ms()
{
return read_high_resolution_us() / 1000; 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(); core_util_critical_section_enter();
us_timestamp_t time = _time + slicetime(); us_timestamp_t time = _time + slicetime();
core_util_critical_section_exit(); core_util_critical_section_exit();
return time; return time;
} }
us_timestamp_t Timer::slicetime() { us_timestamp_t Timer::slicetime()
{
us_timestamp_t ret = 0; us_timestamp_t ret = 0;
core_util_critical_section_enter(); core_util_critical_section_enter();
if (_running) { if (_running) {
@ -96,14 +106,16 @@ us_timestamp_t Timer::slicetime() {
return ret; return ret;
} }
void Timer::reset() { void Timer::reset()
{
core_util_critical_section_enter(); core_util_critical_section_enter();
_start = ticker_read_us(_ticker_data); _start = ticker_read_us(_ticker_data);
_time = 0; _time = 0;
core_util_critical_section_exit(); core_util_critical_section_exit();
} }
Timer::operator float() { Timer::operator float()
{
return read(); return read();
} }

View File

@ -22,33 +22,40 @@
namespace mbed { 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)); 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)); ticker_set_handler(_ticker_data, (&TimerEvent::irq));
} }
void TimerEvent::irq(uint32_t id) { void TimerEvent::irq(uint32_t id)
{
TimerEvent *timer_event = (TimerEvent *)id; TimerEvent *timer_event = (TimerEvent *)id;
timer_event->handler(); timer_event->handler();
} }
TimerEvent::~TimerEvent() { TimerEvent::~TimerEvent()
{
remove(); remove();
} }
// insert in to linked list // 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); 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); ticker_insert_event_us(_ticker_data, &event, timestamp, (uint32_t)this);
} }
void TimerEvent::remove() { void TimerEvent::remove()
{
ticker_remove_event(_ticker_data, &event); ticker_remove_event(_ticker_data, &event);
} }

View File

@ -121,7 +121,8 @@ int UARTSerial::sync()
return 0; return 0;
} }
void UARTSerial::sigio(Callback<void()> func) { void UARTSerial::sigio(Callback<void()> func)
{
core_util_critical_section_enter(); core_util_critical_section_enter();
_sigio_cb = func; _sigio_cb = func;
if (_sigio_cb) { if (_sigio_cb) {
@ -235,7 +236,8 @@ void UARTSerial::wake()
} }
} }
short UARTSerial::poll(short events) const { short UARTSerial::poll(short events) const
{
short revents = 0; short revents = 0;
/* Check the Circular Buffer if space available for writing out */ /* Check the Circular Buffer if space available for writing out */