mirror of https://github.com/ARMmbed/mbed-os.git
drivers: astyle update
parent
58fa28b9b2
commit
700e6df834
|
|
@ -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();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -18,12 +18,13 @@
|
||||||
|
|
||||||
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
|
||||||
_nc_mask = 0;
|
_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;
|
_pin[i] = (pins[i] != NC) ? new DigitalIn(pins[i]) : 0;
|
||||||
if (pins[i] != NC) {
|
if (pins[i] != NC) {
|
||||||
_nc_mask |= (1 << i);
|
_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
|
// 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++) {
|
||||||
_pin[i] = (pins[i] != NC) ? new DigitalIn(pins[i]) : 0;
|
_pin[i] = (pins[i] != NC) ? new DigitalIn(pins[i]) : 0;
|
||||||
if (pins[i] != NC) {
|
if (pins[i] != NC) {
|
||||||
_nc_mask |= (1 << i);
|
_nc_mask |= (1 << i);
|
||||||
|
|
@ -42,19 +44,21 @@ 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) {
|
||||||
delete _pin[i];
|
delete _pin[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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++) {
|
||||||
if (_pin[i] != 0) {
|
if (_pin[i] != 0) {
|
||||||
v |= _pin[i]->read() << i;
|
v |= _pin[i]->read() << i;
|
||||||
}
|
}
|
||||||
|
|
@ -63,9 +67,10 @@ 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) {
|
||||||
_pin[i]->mode(pull);
|
_pin[i]->mode(pull);
|
||||||
}
|
}
|
||||||
|
|
@ -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]);
|
||||||
|
|
|
||||||
|
|
@ -62,12 +62,12 @@ public:
|
||||||
PinName p8 = NC, PinName p9 = NC, PinName p10 = NC, PinName p11 = NC,
|
PinName p8 = NC, PinName p9 = NC, PinName p10 = NC, PinName p11 = NC,
|
||||||
PinName p12 = NC, PinName p13 = NC, PinName p14 = NC, PinName p15 = NC);
|
PinName p12 = NC, PinName p13 = NC, PinName p14 = NC, PinName p15 = NC);
|
||||||
|
|
||||||
|
|
||||||
/** Create an BusIn, connected to the specified pins
|
/** Create an BusIn, connected to the specified pins
|
||||||
*
|
*
|
||||||
* @param pins An array of pins to connect to bus bit
|
* @param pins An array of pins to connect to bus bit
|
||||||
*/
|
*/
|
||||||
BusIn(PinName pins[16]);
|
BusIn(PinName pins[16]);
|
||||||
|
|
||||||
virtual ~BusIn();
|
virtual ~BusIn();
|
||||||
|
|
||||||
|
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
@ -103,10 +104,10 @@ public:
|
||||||
/** Access to particular bit in random-iterator fashion
|
/** Access to particular bit in random-iterator fashion
|
||||||
* @param index Position of bit
|
* @param index Position of bit
|
||||||
*/
|
*/
|
||||||
DigitalIn & operator[] (int index);
|
DigitalIn &operator[](int index);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
DigitalIn* _pin[16];
|
DigitalIn *_pin[16];
|
||||||
|
|
||||||
/* Mask of bus's NC pins
|
/* Mask of bus's NC pins
|
||||||
* If bit[n] is set to 1 - pin is connected
|
* If bit[n] is set to 1 - pin is connected
|
||||||
|
|
|
||||||
|
|
@ -18,12 +18,13 @@
|
||||||
|
|
||||||
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
|
||||||
_nc_mask = 0;
|
_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;
|
_pin[i] = (pins[i] != NC) ? new DigitalInOut(pins[i]) : 0;
|
||||||
if (pins[i] != NC) {
|
if (pins[i] != NC) {
|
||||||
_nc_mask |= (1 << i);
|
_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
|
// 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++) {
|
||||||
_pin[i] = (pins[i] != NC) ? new DigitalInOut(pins[i]) : 0;
|
_pin[i] = (pins[i] != NC) ? new DigitalInOut(pins[i]) : 0;
|
||||||
if (pins[i] != NC) {
|
if (pins[i] != NC) {
|
||||||
_nc_mask |= (1 << i);
|
_nc_mask |= (1 << i);
|
||||||
|
|
@ -42,18 +44,20 @@ 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) {
|
||||||
delete _pin[i];
|
delete _pin[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
||||||
_pin[i]->write((value >> i) & 1);
|
_pin[i]->write((value >> i) & 1);
|
||||||
}
|
}
|
||||||
|
|
@ -61,10 +65,11 @@ 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++) {
|
||||||
if (_pin[i] != 0) {
|
if (_pin[i] != 0) {
|
||||||
v |= _pin[i]->read() << i;
|
v |= _pin[i]->read() << i;
|
||||||
}
|
}
|
||||||
|
|
@ -73,9 +78,10 @@ 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) {
|
||||||
_pin[i]->output();
|
_pin[i]->output();
|
||||||
}
|
}
|
||||||
|
|
@ -83,9 +89,10 @@ 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) {
|
||||||
_pin[i]->input();
|
_pin[i]->input();
|
||||||
}
|
}
|
||||||
|
|
@ -93,9 +100,10 @@ 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) {
|
||||||
_pin[i]->mode(pull);
|
_pin[i]->mode(pull);
|
||||||
}
|
}
|
||||||
|
|
@ -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();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -103,21 +103,22 @@ 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** A shorthand for write()
|
/** A shorthand for write()
|
||||||
* \sa BusInOut::write()
|
* \sa BusInOut::write()
|
||||||
*/
|
*/
|
||||||
BusInOut& operator= (int v);
|
BusInOut &operator= (int v);
|
||||||
BusInOut& operator= (BusInOut& rhs);
|
BusInOut &operator= (BusInOut &rhs);
|
||||||
|
|
||||||
/** Access to particular bit in random-iterator fashion
|
/** Access to particular bit in random-iterator fashion
|
||||||
* @param index Bit Position
|
* @param index Bit Position
|
||||||
*/
|
*/
|
||||||
DigitalInOut& operator[] (int index);
|
DigitalInOut &operator[](int index);
|
||||||
|
|
||||||
/** A shorthand for read()
|
/** A shorthand for read()
|
||||||
* \sa BusInOut::read()
|
* \sa BusInOut::read()
|
||||||
|
|
@ -127,7 +128,7 @@ public:
|
||||||
protected:
|
protected:
|
||||||
virtual void lock();
|
virtual void lock();
|
||||||
virtual void unlock();
|
virtual void unlock();
|
||||||
DigitalInOut* _pin[16];
|
DigitalInOut *_pin[16];
|
||||||
|
|
||||||
/* Mask of bus's NC pins
|
/* Mask of bus's NC pins
|
||||||
* If bit[n] is set to 1 - pin is connected
|
* If bit[n] is set to 1 - pin is connected
|
||||||
|
|
|
||||||
|
|
@ -18,12 +18,13 @@
|
||||||
|
|
||||||
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
|
||||||
_nc_mask = 0;
|
_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;
|
_pin[i] = (pins[i] != NC) ? new DigitalOut(pins[i]) : 0;
|
||||||
if (pins[i] != NC) {
|
if (pins[i] != NC) {
|
||||||
_nc_mask |= (1 << i);
|
_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
|
// 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++) {
|
||||||
_pin[i] = (pins[i] != NC) ? new DigitalOut(pins[i]) : 0;
|
_pin[i] = (pins[i] != NC) ? new DigitalOut(pins[i]) : 0;
|
||||||
if (pins[i] != NC) {
|
if (pins[i] != NC) {
|
||||||
_nc_mask |= (1 << i);
|
_nc_mask |= (1 << i);
|
||||||
|
|
@ -42,18 +44,20 @@ 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) {
|
||||||
delete _pin[i];
|
delete _pin[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
||||||
_pin[i]->write((value >> i) & 1);
|
_pin[i]->write((value >> i) & 1);
|
||||||
}
|
}
|
||||||
|
|
@ -61,10 +65,11 @@ 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++) {
|
||||||
if (_pin[i] != 0) {
|
if (_pin[i] != 0) {
|
||||||
v |= _pin[i]->read() << i;
|
v |= _pin[i]->read() << 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();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
@ -95,13 +96,13 @@ public:
|
||||||
/** A shorthand for write()
|
/** A shorthand for write()
|
||||||
* \sa BusOut::write()
|
* \sa BusOut::write()
|
||||||
*/
|
*/
|
||||||
BusOut& operator= (int v);
|
BusOut &operator= (int v);
|
||||||
BusOut& operator= (BusOut& rhs);
|
BusOut &operator= (BusOut &rhs);
|
||||||
|
|
||||||
/** Access to particular bit in random-iterator fashion
|
/** Access to particular bit in random-iterator fashion
|
||||||
* @param index Bit Position
|
* @param index Bit Position
|
||||||
*/
|
*/
|
||||||
DigitalOut& operator[] (int index);
|
DigitalOut &operator[](int index);
|
||||||
|
|
||||||
/** A shorthand for read()
|
/** A shorthand for read()
|
||||||
* \sa BusOut::read()
|
* \sa BusOut::read()
|
||||||
|
|
@ -111,7 +112,7 @@ public:
|
||||||
protected:
|
protected:
|
||||||
virtual void lock();
|
virtual void lock();
|
||||||
virtual void unlock();
|
virtual void unlock();
|
||||||
DigitalOut* _pin[16];
|
DigitalOut *_pin[16];
|
||||||
|
|
||||||
/* Mask of bus's NC pins
|
/* Mask of bus's NC pins
|
||||||
* If bit[n] is set to 1 - pin is connected
|
* If bit[n] is set to 1 - pin is connected
|
||||||
|
|
|
||||||
|
|
@ -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();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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,12 +55,13 @@ 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;
|
{
|
||||||
type = _type;
|
len = _len & 0xF;
|
||||||
format = _format;
|
type = _type;
|
||||||
id = _id;
|
format = _format;
|
||||||
memcpy(data, _data, _len);
|
id = _id;
|
||||||
|
memcpy(data, _data, _len);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Creates CAN remote message.
|
/** Creates CAN remote message.
|
||||||
|
|
@ -67,12 +69,13 @@ 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;
|
{
|
||||||
type = CANRemote;
|
len = 0;
|
||||||
format = _format;
|
type = CANRemote;
|
||||||
id = _id;
|
format = _format;
|
||||||
memset(data, 0, 8);
|
id = _id;
|
||||||
|
memset(data, 0, 8);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -235,48 +238,50 @@ public:
|
||||||
|
|
||||||
/** Attach a function to call whenever a CAN frame received interrupt is
|
/** Attach a function to call whenever a CAN frame received interrupt is
|
||||||
* generated.
|
* generated.
|
||||||
*
|
*
|
||||||
* This function locks the deep sleep while a callback is attached
|
* This function locks the deep sleep while a callback is attached
|
||||||
*
|
*
|
||||||
* @param func A pointer to a void function, or 0 to set as none
|
* @param 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)
|
* @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
|
/** Attach a member function to call whenever a CAN frame received interrupt
|
||||||
* is generated.
|
* is generated.
|
||||||
*
|
*
|
||||||
* @param obj pointer to the object to call the member function on
|
* @param obj pointer to the object to call the member function on
|
||||||
* @param method pointer to the member function to be called
|
* @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)
|
* @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
|
* @deprecated
|
||||||
* 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).
|
||||||
*/
|
*/
|
||||||
template<typename T>
|
template<typename T>
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Attach a member function to call whenever a CAN frame received interrupt
|
/** Attach a member function to call whenever a CAN frame received interrupt
|
||||||
* is generated.
|
* is generated.
|
||||||
*
|
*
|
||||||
* @param obj pointer to the object to call the member function on
|
* @param obj pointer to the object to call the member function on
|
||||||
* @param method pointer to the member function to be called
|
* @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)
|
* @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
|
* @deprecated
|
||||||
* 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).
|
||||||
*/
|
*/
|
||||||
template<typename T>
|
template<typename T>
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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);
|
||||||
|
|
|
||||||
|
|
@ -99,7 +99,7 @@ int FlashIAP::program(const void *buffer, uint32_t addr, uint32_t size)
|
||||||
|
|
||||||
// addr should be aligned to page size
|
// addr should be aligned to page size
|
||||||
if (!is_aligned(addr, page_size) || (!buffer) ||
|
if (!is_aligned(addr, page_size) || (!buffer) ||
|
||||||
((addr + size) > (flash_start_addr + flash_size))) {
|
((addr + size) > (flash_start_addr + flash_size))) {
|
||||||
return -1;
|
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);
|
uint32_t current_sector_size = flash_get_sector_size(&_flash, addr);
|
||||||
if (!is_aligned(size, current_sector_size) ||
|
if (!is_aligned(size, current_sector_size) ||
|
||||||
!is_aligned(addr, current_sector_size)) {
|
!is_aligned(addr, current_sector_size)) {
|
||||||
return false;
|
return false;
|
||||||
} else {
|
} else {
|
||||||
return true;
|
return true;
|
||||||
|
|
@ -160,7 +160,7 @@ int FlashIAP::erase(uint32_t addr, uint32_t size)
|
||||||
|
|
||||||
if (erase_end_addr > flash_end_addr) {
|
if (erase_end_addr > flash_end_addr) {
|
||||||
return -1;
|
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);
|
uint32_t following_sector_size = flash_get_sector_size(&_flash, erase_end_addr);
|
||||||
if (!is_aligned(erase_end_addr, following_sector_size)) {
|
if (!is_aligned(erase_end_addr, following_sector_size)) {
|
||||||
return -1;
|
return -1;
|
||||||
|
|
|
||||||
|
|
@ -56,7 +56,7 @@ public:
|
||||||
*/
|
*/
|
||||||
int deinit();
|
int deinit();
|
||||||
|
|
||||||
/** Read data from a flash device.
|
/** Read data from a flash device.
|
||||||
*
|
*
|
||||||
* This method invokes memcpy - reads number of bytes from the address
|
* This method invokes memcpy - reads number of bytes from the address
|
||||||
*
|
*
|
||||||
|
|
@ -90,7 +90,7 @@ public:
|
||||||
|
|
||||||
/** Get the sector size at the defined address
|
/** Get the sector size at the defined address
|
||||||
*
|
*
|
||||||
* Sector size might differ at address ranges.
|
* Sector size might differ at address ranges.
|
||||||
* An example <0-0x1000, sector size=1024; 0x10000-0x20000, size=2048>
|
* An example <0-0x1000, sector size=1024; 0x10000-0x20000, size=2048>
|
||||||
*
|
*
|
||||||
* @param addr Address of or inside the sector to query
|
* @param addr Address of or inside the sector to query
|
||||||
|
|
@ -98,15 +98,15 @@ public:
|
||||||
*/
|
*/
|
||||||
uint32_t get_sector_size(uint32_t addr) const;
|
uint32_t get_sector_size(uint32_t addr) const;
|
||||||
|
|
||||||
/** Get the flash start address
|
/** Get the flash start address
|
||||||
*
|
*
|
||||||
* @return Flash start address
|
* @return Flash start address
|
||||||
*/
|
*/
|
||||||
uint32_t get_flash_start() const;
|
uint32_t get_flash_start() const;
|
||||||
|
|
||||||
/** Get the flash size
|
/** Get the flash size
|
||||||
*
|
*
|
||||||
* @return Flash size
|
* @return Flash size
|
||||||
*/
|
*/
|
||||||
uint32_t get_flash_size() const;
|
uint32_t get_flash_size() const;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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,29 +111,33 @@ 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();
|
||||||
}
|
}
|
||||||
|
|
||||||
#if DEVICE_I2C_ASYNCH
|
#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();
|
lock();
|
||||||
if (i2c_active(&_i2c)) {
|
if (i2c_active(&_i2c)) {
|
||||||
|
|
|
||||||
|
|
@ -151,7 +151,8 @@ public:
|
||||||
*/
|
*/
|
||||||
virtual void unlock(void);
|
virtual void unlock(void);
|
||||||
|
|
||||||
virtual ~I2C() {
|
virtual ~I2C()
|
||||||
|
{
|
||||||
// Do nothing
|
// Do nothing
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -160,7 +161,7 @@ public:
|
||||||
/** Start non-blocking I2C transfer.
|
/** Start non-blocking I2C transfer.
|
||||||
*
|
*
|
||||||
* This function locks the deep sleep until any event has occurred
|
* This function locks the deep sleep until any event has occurred
|
||||||
*
|
*
|
||||||
* @param address 8/10 bit I2C slave address
|
* @param address 8/10 bit I2C slave address
|
||||||
* @param tx_buffer The TX buffer with data to be transfered
|
* @param tx_buffer The TX buffer with data to be transfered
|
||||||
* @param tx_length The length of TX buffer in bytes
|
* @param tx_length The length of TX buffer in bytes
|
||||||
|
|
@ -171,13 +172,13 @@ public:
|
||||||
* @param repeated Repeated start, true - do not send stop at end
|
* @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
|
* @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
|
/** Abort the on-going I2C transfer
|
||||||
*/
|
*/
|
||||||
void abort_transfer();
|
void abort_transfer();
|
||||||
|
|
||||||
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();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -24,45 +24,52 @@ namespace mbed {
|
||||||
// If not for that, we could simplify by having only the 2-param
|
// If not for that, we could simplify by having only the 2-param
|
||||||
// constructor, with a default value for the PinMode.
|
// constructor, with a default value for the PinMode.
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
InterruptIn::InterruptIn(PinName pin, PinMode mode) :
|
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,36 +94,41 @@ 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:
|
||||||
if (handler->_rise) {
|
if (handler->_rise) {
|
||||||
handler->_rise();
|
handler->_rise();
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case IRQ_FALL:
|
case IRQ_FALL:
|
||||||
if (handler->_fall) {
|
if (handler->_fall) {
|
||||||
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();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -106,9 +106,10 @@ public:
|
||||||
*/
|
*/
|
||||||
template<typename T, typename M>
|
template<typename T, typename M>
|
||||||
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();
|
||||||
|
|
@ -130,9 +131,10 @@ public:
|
||||||
*/
|
*/
|
||||||
template<typename T, typename M>
|
template<typename T, typename M>
|
||||||
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();
|
||||||
|
|
|
||||||
|
|
@ -32,12 +32,13 @@ namespace mbed {
|
||||||
|
|
||||||
typedef void (*pvoidf)(void);
|
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();
|
||||||
|
|
||||||
// Atomically set _instance
|
// Atomically set _instance
|
||||||
core_util_critical_section_enter();
|
core_util_critical_section_enter();
|
||||||
|
|
@ -55,28 +56,33 @@ 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
|
||||||
if (NULL != _instance) {
|
if (NULL != _instance) {
|
||||||
delete _instance;
|
delete _instance;
|
||||||
_instance = (InterruptManager*)NULL;
|
_instance = (InterruptManager *)NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
InterruptManager::~InterruptManager() {
|
InterruptManager::~InterruptManager()
|
||||||
for(int i = 0; i < NVIC_NUM_VECTORS; i++)
|
{
|
||||||
if (NULL != _chains[i])
|
for (int i = 0; i < NVIC_NUM_VECTORS; 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();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -58,26 +58,26 @@ namespace mbed {
|
||||||
class InterruptManager : private NonCopyable<InterruptManager> {
|
class InterruptManager : private NonCopyable<InterruptManager> {
|
||||||
public:
|
public:
|
||||||
/** Get the instance of InterruptManager Class
|
/** Get the instance of InterruptManager Class
|
||||||
* @deprecated
|
* @deprecated
|
||||||
* Do not use this function, this class is not part of the public API of mbed-os and is being removed in the future.
|
* Do not use this function, this class is not part of the public API of mbed-os and is being removed in the future.
|
||||||
*
|
*
|
||||||
* @return the only instance of this class
|
* @return the only instance of this class
|
||||||
*/
|
*/
|
||||||
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.")
|
||||||
static InterruptManager* get();
|
static InterruptManager *get();
|
||||||
|
|
||||||
/** Destroy the current instance of the interrupt manager
|
/** Destroy the current instance of the interrupt manager
|
||||||
* @deprecated
|
* @deprecated
|
||||||
* Do not use this function, this class is not part of the public API of mbed-os and is being removed in the future.
|
* Do not use this function, this class is not part of the public API of mbed-os and is being removed in the future.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
MBED_DEPRECATED_SINCE("mbed-os-5.6", "This class is not part of the "
|
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();
|
static void destroy();
|
||||||
|
|
||||||
/** Add a handler for an interrupt at the end of the handler list
|
/** Add a handler for an interrupt at the end of the handler list
|
||||||
* @deprecated
|
* @deprecated
|
||||||
* Do not use this function, this class is not part of the public API of mbed-os and is being removed in the future.
|
* Do not use this function, this class is not part of the public API of mbed-os and is being removed in the future.
|
||||||
*
|
*
|
||||||
* @param function the handler to add
|
* @param function the handler to add
|
||||||
|
|
@ -87,14 +87,15 @@ public:
|
||||||
* The function object created for 'function'
|
* The function object created for 'function'
|
||||||
*/
|
*/
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Add a handler for an interrupt at the beginning of the handler list
|
/** Add a handler for an interrupt at the beginning of the handler list
|
||||||
* @deprecated
|
* @deprecated
|
||||||
* Do not use this function, this class is not part of the public API of mbed-os and is being removed in the future.
|
* Do not use this function, this class is not part of the public API of mbed-os and is being removed in the future.
|
||||||
*
|
*
|
||||||
* @param function the handler to add
|
* @param function the handler to add
|
||||||
|
|
@ -104,14 +105,15 @@ public:
|
||||||
* The function object created for 'function'
|
* The function object created for 'function'
|
||||||
*/
|
*/
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Add a handler for an interrupt at the end of the handler list
|
/** Add a handler for an interrupt at the end of the handler list
|
||||||
* @deprecated
|
* @deprecated
|
||||||
* Do not use this function, this class is not part of the public API of mbed-os and is being removed in the future.
|
* Do not use this function, this class is not part of the public API of mbed-os and is being removed in the future.
|
||||||
*
|
*
|
||||||
* @param tptr pointer to the object that has the handler function
|
* @param tptr pointer to the object that has the handler function
|
||||||
|
|
@ -123,14 +125,15 @@ 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Add a handler for an interrupt at the beginning of the handler list
|
/** Add a handler for an interrupt at the beginning of the handler list
|
||||||
* @deprecated
|
* @deprecated
|
||||||
* Do not use this function, this class is not part of the public API of mbed-os and is being removed in the future.
|
* Do not use this function, this class is not part of the public API of mbed-os and is being removed in the future.
|
||||||
*
|
*
|
||||||
* @param tptr pointer to the object that has the handler function
|
* @param tptr pointer to the object that has the handler function
|
||||||
|
|
@ -142,14 +145,15 @@ 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Remove a handler from an interrupt
|
/** Remove a handler from an interrupt
|
||||||
* @deprecated
|
* @deprecated
|
||||||
* Do not use this function, this class is not part of the public API of mbed-os and is being removed in the future.
|
* Do not use this function, this class is not part of the public API of mbed-os and is being removed in the future.
|
||||||
*
|
*
|
||||||
* @param handler the function object for the handler to remove
|
* @param handler the function object for the handler to remove
|
||||||
|
|
@ -159,7 +163,7 @@ public:
|
||||||
* true if the handler was found and removed, false otherwise
|
* true if the handler was found and removed, false otherwise
|
||||||
*/
|
*/
|
||||||
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.")
|
||||||
bool remove_handler(pFunctionPointer_t handler, IRQn_Type irq);
|
bool remove_handler(pFunctionPointer_t handler, IRQn_Type irq);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
@ -170,27 +174,29 @@ 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
bool must_replace_vector(IRQn_Type irq);
|
||||||
int get_irq_index(IRQn_Type irq);
|
int get_irq_index(IRQn_Type irq);
|
||||||
void irq_helper();
|
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();
|
static void static_irq_helper();
|
||||||
|
|
||||||
CallChain* _chains[NVIC_NUM_VECTORS];
|
CallChain *_chains[NVIC_NUM_VECTORS];
|
||||||
static InterruptManager* _instance;
|
static InterruptManager *_instance;
|
||||||
PlatformMutex _mutex;
|
PlatformMutex _mutex;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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()
|
||||||
|
{
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -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())
|
||||||
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -26,40 +26,40 @@ namespace mbed {
|
||||||
*/
|
*/
|
||||||
template<>
|
template<>
|
||||||
MbedCRC<POLY_32BIT_ANSI, 32>::MbedCRC(uint32_t initial_xor, uint32_t final_xor, bool reflect_data, bool reflect_remainder):
|
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),
|
_initial_value(initial_xor), _final_xor(final_xor), _reflect_data(reflect_data), _reflect_remainder(reflect_remainder),
|
||||||
_crc_table((uint32_t *)Table_CRC_32bit_ANSI)
|
_crc_table((uint32_t *)Table_CRC_32bit_ANSI)
|
||||||
{
|
{
|
||||||
mbed_crc_ctor();
|
mbed_crc_ctor();
|
||||||
}
|
}
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
MbedCRC<POLY_8BIT_CCITT, 8>::MbedCRC(uint32_t initial_xor, uint32_t final_xor, bool reflect_data, bool reflect_remainder):
|
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),
|
_initial_value(initial_xor), _final_xor(final_xor), _reflect_data(reflect_data), _reflect_remainder(reflect_remainder),
|
||||||
_crc_table((uint32_t *)Table_CRC_8bit_CCITT)
|
_crc_table((uint32_t *)Table_CRC_8bit_CCITT)
|
||||||
{
|
{
|
||||||
mbed_crc_ctor();
|
mbed_crc_ctor();
|
||||||
}
|
}
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
MbedCRC<POLY_7BIT_SD, 7>::MbedCRC(uint32_t initial_xor, uint32_t final_xor, bool reflect_data, bool reflect_remainder):
|
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),
|
_initial_value(initial_xor), _final_xor(final_xor), _reflect_data(reflect_data), _reflect_remainder(reflect_remainder),
|
||||||
_crc_table((uint32_t *)Table_CRC_7Bit_SD)
|
_crc_table((uint32_t *)Table_CRC_7Bit_SD)
|
||||||
{
|
{
|
||||||
mbed_crc_ctor();
|
mbed_crc_ctor();
|
||||||
}
|
}
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
MbedCRC<POLY_16BIT_CCITT, 16>::MbedCRC(uint32_t initial_xor, uint32_t final_xor, bool reflect_data, bool reflect_remainder):
|
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),
|
_initial_value(initial_xor), _final_xor(final_xor), _reflect_data(reflect_data), _reflect_remainder(reflect_remainder),
|
||||||
_crc_table((uint32_t *)Table_CRC_16bit_CCITT)
|
_crc_table((uint32_t *)Table_CRC_16bit_CCITT)
|
||||||
{
|
{
|
||||||
mbed_crc_ctor();
|
mbed_crc_ctor();
|
||||||
}
|
}
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
MbedCRC<POLY_16BIT_IBM, 16>::MbedCRC(uint32_t initial_xor, uint32_t final_xor, bool reflect_data, bool reflect_remainder):
|
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),
|
_initial_value(initial_xor), _final_xor(final_xor), _reflect_data(reflect_data), _reflect_remainder(reflect_remainder),
|
||||||
_crc_table((uint32_t *)Table_CRC_16bit_IBM)
|
_crc_table((uint32_t *)Table_CRC_16bit_IBM)
|
||||||
{
|
{
|
||||||
mbed_crc_ctor();
|
mbed_crc_ctor();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -92,9 +92,8 @@ namespace mbed {
|
||||||
* @ingroup drivers
|
* @ingroup drivers
|
||||||
*/
|
*/
|
||||||
|
|
||||||
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 };
|
||||||
|
|
||||||
|
|
@ -107,7 +106,7 @@ public:
|
||||||
* @param final_xor Final Xor value
|
* @param final_xor Final Xor value
|
||||||
* @param reflect_data
|
* @param reflect_data
|
||||||
* @param reflect_remainder
|
* @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 <POLY_7BIT_SD, 7> ct; --- Valid POLY_7BIT_SD
|
||||||
* MbedCRC <0x1021, 16> ct; --- Valid POLY_16BIT_CCITT
|
* MbedCRC <0x1021, 16> ct; --- Valid POLY_16BIT_CCITT
|
||||||
* MbedCRC <POLY_16BIT_CCITT, 32> ct; --- Invalid, compilation error
|
* 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) :
|
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),
|
_initial_value(initial_xor), _final_xor(final_xor), _reflect_data(reflect_data),
|
||||||
_reflect_remainder(reflect_remainder), _crc_table(NULL)
|
_reflect_remainder(reflect_remainder), _crc_table(NULL)
|
||||||
{
|
{
|
||||||
mbed_crc_ctor();
|
mbed_crc_ctor();
|
||||||
}
|
}
|
||||||
|
|
@ -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);
|
||||||
|
|
@ -332,7 +330,7 @@ private:
|
||||||
*/
|
*/
|
||||||
uint32_t reflect_bytes(uint32_t data) const
|
uint32_t reflect_bytes(uint32_t data) const
|
||||||
{
|
{
|
||||||
if(_reflect_data) {
|
if (_reflect_data) {
|
||||||
uint32_t reflection = 0x0;
|
uint32_t reflection = 0x0;
|
||||||
|
|
||||||
for (uint8_t bit = 0; bit < 8; ++bit) {
|
for (uint8_t bit = 0; bit < 8; ++bit) {
|
||||||
|
|
@ -368,7 +366,7 @@ private:
|
||||||
data_byte = reflect_bytes(data[byte]);
|
data_byte = reflect_bytes(data[byte]);
|
||||||
for (uint8_t bit = 8; bit > 0; --bit) {
|
for (uint8_t bit = 8; bit > 0; --bit) {
|
||||||
p_crc <<= 1;
|
p_crc <<= 1;
|
||||||
if (( data_byte ^ p_crc) & get_top_bit()) {
|
if ((data_byte ^ p_crc) & get_top_bit()) {
|
||||||
p_crc ^= polynomial;
|
p_crc ^= polynomial;
|
||||||
}
|
}
|
||||||
data_byte <<= 1;
|
data_byte <<= 1;
|
||||||
|
|
@ -392,13 +390,13 @@ private:
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** CRC computation using ROM tables
|
/** CRC computation using ROM tables
|
||||||
*
|
*
|
||||||
* @param buffer data buffer
|
* @param buffer data buffer
|
||||||
* @param size size of the data
|
* @param size size of the data
|
||||||
* @param crc CRC value is filled in, but the value is not the final
|
* @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
|
* @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
|
int32_t table_compute_partial(const void *buffer, crc_data_size_t size, uint32_t *crc) const
|
||||||
{
|
{
|
||||||
MBED_ASSERT(crc != NULL);
|
MBED_ASSERT(crc != NULL);
|
||||||
|
|
|
||||||
|
|
@ -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();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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,16 +151,18 @@ 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();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 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;
|
||||||
|
|
@ -169,8 +180,9 @@ 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;
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -29,23 +29,25 @@ CircularBuffer<Transaction<SPI>, TRANSACTION_QUEUE_SIZE_SPI> SPI::_transaction_b
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
SPI::SPI(PinName mosi, PinName miso, PinName sclk, PinName ssel) :
|
SPI::SPI(PinName mosi, PinName miso, PinName sclk, PinName ssel) :
|
||||||
_spi(),
|
_spi(),
|
||||||
#if DEVICE_SPI_ASYNCH
|
#if DEVICE_SPI_ASYNCH
|
||||||
_irq(this),
|
_irq(this),
|
||||||
_usage(DMA_USAGE_NEVER),
|
_usage(DMA_USAGE_NEVER),
|
||||||
_deep_sleep_locked(false),
|
_deep_sleep_locked(false),
|
||||||
#endif
|
#endif
|
||||||
_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
|
||||||
|
|
@ -74,13 +77,14 @@ void SPI::frequency(int hz) {
|
||||||
unlock();
|
unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
SPI* SPI::_owner = NULL;
|
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);
|
||||||
spi_frequency(&_spi, _hz);
|
spi_frequency(&_spi, _hz);
|
||||||
_owner = this;
|
_owner = this;
|
||||||
|
|
@ -89,15 +93,17 @@ 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);
|
||||||
_owner = this;
|
_owner = this;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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();
|
||||||
|
|
@ -129,7 +139,7 @@ void SPI::set_default_write_value(char data) {
|
||||||
|
|
||||||
#if DEVICE_SPI_ASYNCH
|
#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)) {
|
if (spi_active(&_spi)) {
|
||||||
return queue_transfer(tx_buffer, tx_length, rx_buffer, rx_length, bit_width, callback, event);
|
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;
|
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
|
#if TRANSACTION_QUEUE_SIZE_SPI
|
||||||
transaction_t t;
|
transaction_t t;
|
||||||
|
|
@ -199,13 +209,13 @@ int SPI::queue_transfer(const void *tx_buffer, int tx_length, void *rx_buffer, i
|
||||||
#endif
|
#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();
|
lock_deep_sleep();
|
||||||
_acquire();
|
_acquire();
|
||||||
_callback = callback;
|
_callback = callback;
|
||||||
_irq.callback(&SPI::irq_handler_asynch);
|
_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()
|
void SPI::lock_deep_sleep()
|
||||||
|
|
@ -235,8 +245,8 @@ void SPI::dequeue_transaction()
|
||||||
{
|
{
|
||||||
Transaction<SPI> t;
|
Transaction<SPI> t;
|
||||||
if (_transaction_buffer.pop(t)) {
|
if (_transaction_buffer.pop(t)) {
|
||||||
SPI* obj = t.get_object();
|
SPI *obj = t.get_object();
|
||||||
transaction_t* data = t.get_transaction();
|
transaction_t *data = t.get_transaction();
|
||||||
obj->start_transaction(data);
|
obj->start_transaction(data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -86,7 +86,7 @@ public:
|
||||||
* @param sclk SPI Clock pin
|
* @param sclk SPI Clock pin
|
||||||
* @param ssel SPI chip select 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
|
/** Configure the data transmission format
|
||||||
*
|
*
|
||||||
|
|
@ -157,7 +157,7 @@ public:
|
||||||
/** Start non-blocking SPI transfer using 8bit buffers.
|
/** Start non-blocking SPI transfer using 8bit buffers.
|
||||||
*
|
*
|
||||||
* This function locks the deep sleep until any event has occurred
|
* This function locks the deep sleep until any event has occurred
|
||||||
*
|
*
|
||||||
* @param tx_buffer The TX buffer with data to be transfered. If NULL is passed,
|
* @param tx_buffer The TX buffer with data to be transfered. If NULL is passed,
|
||||||
* the default SPI value is sent
|
* the default SPI value is sent
|
||||||
* @param tx_length The length of TX buffer in bytes
|
* @param tx_length The length of TX buffer in bytes
|
||||||
|
|
@ -169,11 +169,12 @@ public:
|
||||||
* @return Zero if the transfer has started, or -1 if SPI peripheral is busy
|
* @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);
|
||||||
}
|
}
|
||||||
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -215,7 +216,7 @@ protected:
|
||||||
* @param event The logical OR of events to modify
|
* @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
|
* @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
|
* @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
|
* @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
|
/** Configures a callback, spi peripheral and initiate a new transfer
|
||||||
*
|
*
|
||||||
|
|
@ -244,7 +245,7 @@ protected:
|
||||||
* @param callback The event callback function
|
* @param callback The event callback function
|
||||||
* @param event The logical OR of events to modify
|
* @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:
|
private:
|
||||||
/** Lock deep sleep only if it is not yet locked */
|
/** Lock deep sleep only if it is not yet locked */
|
||||||
|
|
@ -272,7 +273,8 @@ private:
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
public:
|
public:
|
||||||
virtual ~SPI() {
|
virtual ~SPI()
|
||||||
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
|
||||||
|
|
@ -24,32 +24,37 @@ SPISlave::SPISlave(PinName mosi, PinName miso, PinName sclk, PinName ssel) :
|
||||||
_bits(8),
|
_bits(8),
|
||||||
_mode(0),
|
_mode(0),
|
||||||
_hz(1000000)
|
_hz(1000000)
|
||||||
{
|
{
|
||||||
spi_init(&_spi, mosi, miso, sclk, ssel);
|
spi_init(&_spi, mosi, miso, sclk, ssel);
|
||||||
spi_format(&_spi, _bits, _mode, 1);
|
spi_format(&_spi, _bits, _mode, 1);
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -68,7 +68,7 @@ public:
|
||||||
* @note
|
* @note
|
||||||
* Either tx or rx may be specified as NC if unused
|
* 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
|
/** Create a Serial port, connected to the specified transmit and receive pins, with the specified baud
|
||||||
|
|
|
||||||
|
|
@ -24,11 +24,12 @@ namespace mbed {
|
||||||
|
|
||||||
SerialBase::SerialBase(PinName tx, PinName rx, int baud) :
|
SerialBase::SerialBase(PinName tx, PinName rx, int baud) :
|
||||||
#if DEVICE_SERIAL_ASYNCH
|
#if DEVICE_SERIAL_ASYNCH
|
||||||
_thunk_irq(this), _tx_usage(DMA_USAGE_NEVER),
|
_thunk_irq(this), _tx_usage(DMA_USAGE_NEVER),
|
||||||
_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();
|
||||||
|
|
@ -76,14 +82,14 @@ void SerialBase::attach(Callback<void()> func, IrqType type) {
|
||||||
// lock deep sleep only the first time
|
// lock deep sleep only the first time
|
||||||
if (!_irq[type]) {
|
if (!_irq[type]) {
|
||||||
sleep_manager_lock_deep_sleep();
|
sleep_manager_lock_deep_sleep();
|
||||||
}
|
}
|
||||||
_irq[type] = func;
|
_irq[type] = func;
|
||||||
serial_irq_set(&_serial, (SerialIrq)type, 1);
|
serial_irq_set(&_serial, (SerialIrq)type, 1);
|
||||||
} else {
|
} else {
|
||||||
// unlock deep sleep only the first time
|
// unlock deep sleep only the first time
|
||||||
if (_irq[type]) {
|
if (_irq[type]) {
|
||||||
sleep_manager_unlock_deep_sleep();
|
sleep_manager_unlock_deep_sleep();
|
||||||
}
|
}
|
||||||
_irq[type] = NULL;
|
_irq[type] = NULL;
|
||||||
serial_irq_set(&_serial, (SerialIrq)type, 0);
|
serial_irq_set(&_serial, (SerialIrq)type, 0);
|
||||||
}
|
}
|
||||||
|
|
@ -91,45 +97,51 @@ 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
|
||||||
// ensure that we keep the break active for at least one frame.
|
// ensure that we keep the break active for at least one frame.
|
||||||
// We consider a full frame (1 start bit + 8 data bits bits +
|
// We consider a full frame (1 start bit + 8 data bits bits +
|
||||||
// 1 parity bit + 2 stop bits = 12 bits) for computation.
|
// 1 parity bit + 2 stop bits = 12 bits) for computation.
|
||||||
// One bit time (in us) = 1000000/_baud
|
// One bit time (in us) = 1000000/_baud
|
||||||
// Twelve bits: 12000000/baud delay
|
// Twelve bits: 12000000/baud delay
|
||||||
// 1.5 frames: 18000000/baud delay
|
// 1.5 frames: 18000000/baud delay
|
||||||
serial_break_set(&_serial);
|
serial_break_set(&_serial);
|
||||||
wait_us(18000000/_baud);
|
wait_us(18000000 / _baud);
|
||||||
serial_break_clear(&_serial);
|
serial_break_clear(&_serial);
|
||||||
unlock();
|
unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
void SerialBase::lock() {
|
void SerialBase::lock()
|
||||||
|
{
|
||||||
// Stub
|
// Stub
|
||||||
}
|
}
|
||||||
|
|
||||||
void SerialBase:: unlock() {
|
void SerialBase:: unlock()
|
||||||
|
{
|
||||||
// Stub
|
// Stub
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -144,10 +156,11 @@ 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) {
|
||||||
case RTS:
|
case RTS:
|
||||||
serial_set_flow_control(&_serial, flow_type, flow1, NC);
|
serial_set_flow_control(&_serial, flow_type, flow1, NC);
|
||||||
break;
|
break;
|
||||||
|
|
@ -170,7 +183,7 @@ void SerialBase::set_flow_control(Flow type, PinName flow1, PinName flow2) {
|
||||||
|
|
||||||
#if DEVICE_SERIAL_ASYNCH
|
#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)) {
|
if (serial_tx_active(&_serial)) {
|
||||||
return -1; // transaction ongoing
|
return -1; // transaction ongoing
|
||||||
|
|
@ -179,7 +192,7 @@ int SerialBase::write(const uint8_t *buffer, int length, const event_callback_t&
|
||||||
return 0;
|
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)) {
|
if (serial_tx_active(&_serial)) {
|
||||||
return -1; // transaction ongoing
|
return -1; // transaction ongoing
|
||||||
|
|
@ -188,7 +201,7 @@ int SerialBase::write(const uint16_t *buffer, int length, const event_callback_t
|
||||||
return 0;
|
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;
|
_tx_callback = callback;
|
||||||
|
|
||||||
|
|
@ -235,27 +248,27 @@ int SerialBase::set_dma_usage_rx(DMAUsage usage)
|
||||||
return 0;
|
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)) {
|
if (serial_rx_active(&_serial)) {
|
||||||
return -1; // transaction ongoing
|
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;
|
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)) {
|
if (serial_rx_active(&_serial)) {
|
||||||
return -1; // transaction ongoing
|
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;
|
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;
|
_rx_callback = callback;
|
||||||
_thunk_irq.callback(&SerialBase::interrupt_handler_asynch);
|
_thunk_irq.callback(&SerialBase::interrupt_handler_asynch);
|
||||||
|
|
|
||||||
|
|
@ -76,7 +76,7 @@ public:
|
||||||
* @param parity The parity used (SerialBase::None, SerialBase::Odd, SerialBase::Even, SerialBase::Forced1, SerialBase::Forced0; default = SerialBase::None)
|
* @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)
|
* @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
|
/** 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 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)
|
* @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
|
/** Attach a member function to call whenever a serial interrupt is generated
|
||||||
*
|
*
|
||||||
|
|
@ -112,9 +112,10 @@ public:
|
||||||
*/
|
*/
|
||||||
template<typename T>
|
template<typename T>
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -129,9 +130,10 @@ public:
|
||||||
*/
|
*/
|
||||||
template<typename T>
|
template<typename T>
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -158,7 +160,7 @@ public:
|
||||||
* @param flow1 the first flow control pin (RTS for RTS or RTSCTS, CTS for CTS)
|
* @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)
|
* @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
|
#endif
|
||||||
|
|
||||||
static void _irq_handler(uint32_t id, SerialIrq irq_type);
|
static void _irq_handler(uint32_t id, SerialIrq irq_type);
|
||||||
|
|
@ -168,24 +170,24 @@ public:
|
||||||
/** Begin asynchronous write using 8bit buffer. The completition invokes registered TX event callback
|
/** Begin asynchronous write using 8bit buffer. The completition invokes registered TX event callback
|
||||||
*
|
*
|
||||||
* This function locks the deep sleep until any event has occurred
|
* This function locks the deep sleep until any event has occurred
|
||||||
*
|
*
|
||||||
* @param buffer The buffer where received data will be stored
|
* @param buffer The buffer where received data will be stored
|
||||||
* @param length The buffer length in bytes
|
* @param length The buffer length in bytes
|
||||||
* @param callback The event callback function
|
* @param callback The event callback function
|
||||||
* @param event The logical OR of TX events
|
* @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
|
/** Begin asynchronous write using 16bit buffer. The completition invokes registered TX event callback
|
||||||
*
|
*
|
||||||
* This function locks the deep sleep until any event has occurred
|
* This function locks the deep sleep until any event has occurred
|
||||||
*
|
*
|
||||||
* @param buffer The buffer where received data will be stored
|
* @param buffer The buffer where received data will be stored
|
||||||
* @param length The buffer length in bytes
|
* @param length The buffer length in bytes
|
||||||
* @param callback The event callback function
|
* @param callback The event callback function
|
||||||
* @param event The logical OR of TX events
|
* @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
|
/** Abort the on-going write transfer
|
||||||
*/
|
*/
|
||||||
|
|
@ -194,26 +196,26 @@ public:
|
||||||
/** Begin asynchronous reading using 8bit buffer. The completition invokes registred RX event callback.
|
/** Begin asynchronous reading using 8bit buffer. The completition invokes registred RX event callback.
|
||||||
*
|
*
|
||||||
* This function locks the deep sleep until any event has occurred
|
* This function locks the deep sleep until any event has occurred
|
||||||
*
|
*
|
||||||
* @param buffer The buffer where received data will be stored
|
* @param buffer The buffer where received data will be stored
|
||||||
* @param length The buffer length in bytes
|
* @param length The buffer length in bytes
|
||||||
* @param callback The event callback function
|
* @param callback The event callback function
|
||||||
* @param event The logical OR of RX events
|
* @param event The logical OR of RX events
|
||||||
* @param char_match The matching character
|
* @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.
|
/** Begin asynchronous reading using 16bit buffer. The completition invokes registred RX event callback.
|
||||||
*
|
*
|
||||||
* This function locks the deep sleep until any event has occurred
|
* This function locks the deep sleep until any event has occurred
|
||||||
*
|
*
|
||||||
* @param buffer The buffer where received data will be stored
|
* @param buffer The buffer where received data will be stored
|
||||||
* @param length The buffer length in bytes
|
* @param length The buffer length in bytes
|
||||||
* @param callback The event callback function
|
* @param callback The event callback function
|
||||||
* @param event The logical OR of RX events
|
* @param event The logical OR of RX events
|
||||||
* @param char_match The matching character
|
* @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
|
/** Abort the on-going read transfer
|
||||||
*/
|
*/
|
||||||
|
|
@ -234,8 +236,8 @@ public:
|
||||||
int set_dma_usage_rx(DMAUsage usage);
|
int set_dma_usage_rx(DMAUsage usage);
|
||||||
|
|
||||||
protected:
|
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_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_write(const void *buffer, int buffer_size, char buffer_width, const event_callback_t &callback, int event);
|
||||||
void interrupt_handler_asynch(void);
|
void interrupt_handler_asynch(void);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,7 @@
|
||||||
#include "platform/FileHandle.h"
|
#include "platform/FileHandle.h"
|
||||||
|
|
||||||
namespace mbed {
|
namespace mbed {
|
||||||
|
|
||||||
class SerialWireOutput : public FileHandle {
|
class SerialWireOutput : public FileHandle {
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
@ -71,7 +71,7 @@ public:
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace mbed
|
} // namespace mbed
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -22,11 +22,12 @@
|
||||||
|
|
||||||
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
|
||||||
if(_function && _lock_deepsleep) {
|
if (_function && _lock_deepsleep) {
|
||||||
sleep_manager_unlock_deep_sleep();
|
sleep_manager_unlock_deep_sleep();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -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();
|
||||||
|
|
|
||||||
|
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -96,9 +99,10 @@ public:
|
||||||
*/
|
*/
|
||||||
template<typename T, typename M>
|
template<typename T, typename M>
|
||||||
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,10 +116,11 @@ 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) {
|
||||||
sleep_manager_lock_deep_sleep();
|
sleep_manager_lock_deep_sleep();
|
||||||
}
|
}
|
||||||
_function = func;
|
_function = func;
|
||||||
|
|
@ -134,13 +139,15 @@ public:
|
||||||
*/
|
*/
|
||||||
template<typename T, typename M>
|
template<typename T, typename M>
|
||||||
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();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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();
|
||||||
|
|
|
||||||
|
|
@ -21,21 +21,24 @@
|
||||||
|
|
||||||
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) {
|
||||||
sleep_manager_unlock_deep_sleep();
|
sleep_manager_unlock_deep_sleep();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -43,10 +46,11 @@ 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) {
|
||||||
sleep_manager_lock_deep_sleep();
|
sleep_manager_lock_deep_sleep();
|
||||||
}
|
}
|
||||||
_start = ticker_read_us(_ticker_data);
|
_start = ticker_read_us(_ticker_data);
|
||||||
|
|
@ -55,11 +59,12 @@ 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) {
|
||||||
if(_lock_deepsleep) {
|
if (_lock_deepsleep) {
|
||||||
sleep_manager_unlock_deep_sleep();
|
sleep_manager_unlock_deep_sleep();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -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();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -29,11 +29,11 @@
|
||||||
namespace mbed {
|
namespace mbed {
|
||||||
|
|
||||||
UARTSerial::UARTSerial(PinName tx, PinName rx, int baud) :
|
UARTSerial::UARTSerial(PinName tx, PinName rx, int baud) :
|
||||||
SerialBase(tx, rx, baud),
|
SerialBase(tx, rx, baud),
|
||||||
_blocking(true),
|
_blocking(true),
|
||||||
_tx_irq_enabled(false),
|
_tx_irq_enabled(false),
|
||||||
_rx_irq_enabled(true),
|
_rx_irq_enabled(true),
|
||||||
_dcd_irq(NULL)
|
_dcd_irq(NULL)
|
||||||
{
|
{
|
||||||
/* Attatch IRQ routines to the serial device. */
|
/* Attatch IRQ routines to the serial device. */
|
||||||
SerialBase::attach(callback(this, &UARTSerial::rx_irq), RxIrq);
|
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)
|
void UARTSerial::set_data_carrier_detect(PinName dcd_pin, bool active_high)
|
||||||
{
|
{
|
||||||
delete _dcd_irq;
|
delete _dcd_irq;
|
||||||
_dcd_irq = NULL;
|
_dcd_irq = NULL;
|
||||||
|
|
||||||
if (dcd_pin != NC) {
|
if (dcd_pin != NC) {
|
||||||
|
|
@ -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) {
|
||||||
|
|
@ -133,7 +134,7 @@ void UARTSerial::sigio(Callback<void()> func) {
|
||||||
core_util_critical_section_exit();
|
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;
|
size_t data_written = 0;
|
||||||
const char *buf_ptr = static_cast<const char *>(buffer);
|
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();
|
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;
|
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;
|
short revents = 0;
|
||||||
/* Check the Circular Buffer if space available for writing out */
|
/* Check the Circular Buffer if space available for writing out */
|
||||||
|
|
|
||||||
|
|
@ -42,7 +42,7 @@ namespace mbed {
|
||||||
/** \addtogroup drivers */
|
/** \addtogroup drivers */
|
||||||
|
|
||||||
/** Class providing buffered UART communication functionality using separate circular buffer for send and receive channels
|
/** Class providing buffered UART communication functionality using separate circular buffer for send and receive channels
|
||||||
*
|
*
|
||||||
* @ingroup drivers
|
* @ingroup drivers
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
@ -81,7 +81,7 @@ public:
|
||||||
* @param length The number of bytes to write
|
* @param length The number of bytes to write
|
||||||
* @return The number of bytes written, negative error on failure
|
* @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
|
/** Read the contents of a file into a buffer
|
||||||
*
|
*
|
||||||
|
|
@ -95,7 +95,7 @@ public:
|
||||||
* @param length The number of bytes to read
|
* @param length The number of bytes to read
|
||||||
* @return The number of bytes read, 0 at end of file, negative error on failure
|
* @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
|
/** Close a file
|
||||||
*
|
*
|
||||||
|
|
@ -201,7 +201,7 @@ public:
|
||||||
* @param parity The parity used (None, Odd, Even, Forced1, Forced0; default = None)
|
* @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)
|
* @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
|
#if DEVICE_SERIAL_FC
|
||||||
// For now use the base enum - but in future we may have extra options
|
// 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 flow1 the first flow control pin (RTS for RTS or RTSCTS, CTS for CTS)
|
||||||
* @param flow2 the second flow control pin (CTS for RTSCTS)
|
* @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
|
#endif
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue