Commit Iniziale, progetto funzionante caricato su box ETcontroller in
cantina
This commit is contained in:
132
mbed/drivers/AnalogIn.h
Normal file
132
mbed/drivers/AnalogIn.h
Normal file
@@ -0,0 +1,132 @@
|
||||
/* mbed Microcontroller Library
|
||||
* Copyright (c) 2006-2013 ARM Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#ifndef MBED_ANALOGIN_H
|
||||
#define MBED_ANALOGIN_H
|
||||
|
||||
#include "platform/platform.h"
|
||||
|
||||
#if DEVICE_ANALOGIN
|
||||
|
||||
#include "hal/analogin_api.h"
|
||||
#include "platform/SingletonPtr.h"
|
||||
#include "platform/PlatformMutex.h"
|
||||
|
||||
namespace mbed {
|
||||
/** \addtogroup drivers */
|
||||
/** @{*/
|
||||
|
||||
/** An analog input, used for reading the voltage on a pin
|
||||
*
|
||||
* @Note Synchronization level: Thread safe
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* // Print messages when the AnalogIn is greater than 50%
|
||||
*
|
||||
* #include "mbed.h"
|
||||
*
|
||||
* AnalogIn temperature(p20);
|
||||
*
|
||||
* int main() {
|
||||
* while(1) {
|
||||
* if(temperature > 0.5) {
|
||||
* printf("Too hot! (%f)", temperature.read());
|
||||
* }
|
||||
* }
|
||||
* }
|
||||
* @endcode
|
||||
*/
|
||||
class AnalogIn {
|
||||
|
||||
public:
|
||||
|
||||
/** Create an AnalogIn, connected to the specified pin
|
||||
*
|
||||
* @param pin AnalogIn pin to connect to
|
||||
* @param name (optional) A string to identify the object
|
||||
*/
|
||||
AnalogIn(PinName pin) {
|
||||
lock();
|
||||
analogin_init(&_adc, pin);
|
||||
unlock();
|
||||
}
|
||||
|
||||
/** Read the input voltage, represented as a float in the range [0.0, 1.0]
|
||||
*
|
||||
* @returns A floating-point value representing the current input voltage, measured as a percentage
|
||||
*/
|
||||
float read() {
|
||||
lock();
|
||||
float ret = analogin_read(&_adc);
|
||||
unlock();
|
||||
return ret;
|
||||
}
|
||||
|
||||
/** Read the input voltage, represented as an unsigned short in the range [0x0, 0xFFFF]
|
||||
*
|
||||
* @returns
|
||||
* 16-bit unsigned short representing the current input voltage, normalised to a 16-bit value
|
||||
*/
|
||||
unsigned short read_u16() {
|
||||
lock();
|
||||
unsigned short ret = analogin_read_u16(&_adc);
|
||||
unlock();
|
||||
return ret;
|
||||
}
|
||||
|
||||
/** An operator shorthand for read()
|
||||
*
|
||||
* The float() operator can be used as a shorthand for read() to simplify common code sequences
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* float x = volume.read();
|
||||
* float x = volume;
|
||||
*
|
||||
* if(volume.read() > 0.25) { ... }
|
||||
* if(volume > 0.25) { ... }
|
||||
* @endcode
|
||||
*/
|
||||
operator float() {
|
||||
// Underlying call is thread safe
|
||||
return read();
|
||||
}
|
||||
|
||||
virtual ~AnalogIn() {
|
||||
// Do nothing
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
virtual void lock() {
|
||||
_mutex->lock();
|
||||
}
|
||||
|
||||
virtual void unlock() {
|
||||
_mutex->unlock();
|
||||
}
|
||||
|
||||
analogin_t _adc;
|
||||
static SingletonPtr<PlatformMutex> _mutex;
|
||||
};
|
||||
|
||||
} // namespace mbed
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
/** @}*/
|
||||
150
mbed/drivers/AnalogOut.h
Normal file
150
mbed/drivers/AnalogOut.h
Normal file
@@ -0,0 +1,150 @@
|
||||
/* mbed Microcontroller Library
|
||||
* Copyright (c) 2006-2013 ARM Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#ifndef MBED_ANALOGOUT_H
|
||||
#define MBED_ANALOGOUT_H
|
||||
|
||||
#include "platform/platform.h"
|
||||
|
||||
#if DEVICE_ANALOGOUT
|
||||
|
||||
#include "hal/analogout_api.h"
|
||||
#include "platform/PlatformMutex.h"
|
||||
|
||||
namespace mbed {
|
||||
/** \addtogroup drivers */
|
||||
/** @{*/
|
||||
|
||||
/** An analog output, used for setting the voltage on a pin
|
||||
*
|
||||
* @Note Synchronization level: Thread safe
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* // Make a sawtooth output
|
||||
*
|
||||
* #include "mbed.h"
|
||||
*
|
||||
* AnalogOut tri(p18);
|
||||
* int main() {
|
||||
* while(1) {
|
||||
* tri = tri + 0.01;
|
||||
* wait_us(1);
|
||||
* if(tri == 1) {
|
||||
* tri = 0;
|
||||
* }
|
||||
* }
|
||||
* }
|
||||
* @endcode
|
||||
*/
|
||||
class AnalogOut {
|
||||
|
||||
public:
|
||||
|
||||
/** Create an AnalogOut connected to the specified pin
|
||||
*
|
||||
* @param AnalogOut pin to connect to (18)
|
||||
*/
|
||||
AnalogOut(PinName pin) {
|
||||
analogout_init(&_dac, pin);
|
||||
}
|
||||
|
||||
/** Set the output voltage, specified as a percentage (float)
|
||||
*
|
||||
* @param value A floating-point value representing the output voltage,
|
||||
* specified as a percentage. The value should lie between
|
||||
* 0.0f (representing 0v / 0%) and 1.0f (representing 3.3v / 100%).
|
||||
* Values outside this range will be saturated to 0.0f or 1.0f.
|
||||
*/
|
||||
void write(float value) {
|
||||
lock();
|
||||
analogout_write(&_dac, value);
|
||||
unlock();
|
||||
}
|
||||
|
||||
/** Set the output voltage, represented as an unsigned short in the range [0x0, 0xFFFF]
|
||||
*
|
||||
* @param value 16-bit unsigned short representing the output voltage,
|
||||
* normalised to a 16-bit value (0x0000 = 0v, 0xFFFF = 3.3v)
|
||||
*/
|
||||
void write_u16(unsigned short value) {
|
||||
lock();
|
||||
analogout_write_u16(&_dac, value);
|
||||
unlock();
|
||||
}
|
||||
|
||||
/** Return the current output voltage setting, measured as a percentage (float)
|
||||
*
|
||||
* @returns
|
||||
* A floating-point value representing the current voltage being output on the pin,
|
||||
* measured as a percentage. The returned value will lie between
|
||||
* 0.0f (representing 0v / 0%) and 1.0f (representing 3.3v / 100%).
|
||||
*
|
||||
* @note
|
||||
* This value may not match exactly the value set by a previous write().
|
||||
*/
|
||||
float read() {
|
||||
lock();
|
||||
float ret = analogout_read(&_dac);
|
||||
unlock();
|
||||
return ret;
|
||||
}
|
||||
|
||||
/** An operator shorthand for write()
|
||||
*/
|
||||
AnalogOut& operator= (float percent) {
|
||||
// Underlying write call is thread safe
|
||||
write(percent);
|
||||
return *this;
|
||||
}
|
||||
|
||||
AnalogOut& operator= (AnalogOut& rhs) {
|
||||
// Underlying write call is thread safe
|
||||
write(rhs.read());
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** An operator shorthand for read()
|
||||
*/
|
||||
operator float() {
|
||||
// Underlying read call is thread safe
|
||||
return read();
|
||||
}
|
||||
|
||||
virtual ~AnalogOut() {
|
||||
// Do nothing
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
virtual void lock() {
|
||||
_mutex.lock();
|
||||
}
|
||||
|
||||
virtual void unlock() {
|
||||
_mutex.unlock();
|
||||
}
|
||||
|
||||
dac_t _dac;
|
||||
PlatformMutex _mutex;
|
||||
};
|
||||
|
||||
} // namespace mbed
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
/** @}*/
|
||||
108
mbed/drivers/BusIn.h
Normal file
108
mbed/drivers/BusIn.h
Normal file
@@ -0,0 +1,108 @@
|
||||
/* mbed Microcontroller Library
|
||||
* Copyright (c) 2006-2013 ARM Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#ifndef MBED_BUSIN_H
|
||||
#define MBED_BUSIN_H
|
||||
|
||||
#include "platform/platform.h"
|
||||
#include "drivers/DigitalIn.h"
|
||||
#include "platform/PlatformMutex.h"
|
||||
|
||||
namespace mbed {
|
||||
/** \addtogroup drivers */
|
||||
/** @{*/
|
||||
|
||||
/** A digital input bus, used for reading the state of a collection of pins
|
||||
*
|
||||
* @Note Synchronization level: Thread safe
|
||||
*/
|
||||
class BusIn {
|
||||
|
||||
public:
|
||||
/* Group: Configuration Methods */
|
||||
|
||||
/** Create an BusIn, connected to the specified pins
|
||||
*
|
||||
* @param <n> DigitalIn pin to connect to bus bit <n> (p5-p30, NC)
|
||||
*
|
||||
* @note
|
||||
* It is only required to specify as many pin variables as is required
|
||||
* for the bus; the rest will default to NC (not connected)
|
||||
*/
|
||||
BusIn(PinName p0, PinName p1 = NC, PinName p2 = NC, PinName p3 = NC,
|
||||
PinName p4 = NC, PinName p5 = NC, PinName p6 = NC, PinName p7 = 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);
|
||||
|
||||
BusIn(PinName pins[16]);
|
||||
|
||||
virtual ~BusIn();
|
||||
|
||||
/** Read the value of the input bus
|
||||
*
|
||||
* @returns
|
||||
* An integer with each bit corresponding to the value read from the associated DigitalIn pin
|
||||
*/
|
||||
int read();
|
||||
|
||||
/** Set the input pin mode
|
||||
*
|
||||
* @param mode PullUp, PullDown, PullNone
|
||||
*/
|
||||
void mode(PinMode pull);
|
||||
|
||||
/** Binary mask of bus pins connected to actual pins (not NC pins)
|
||||
* If bus pin is in NC state make corresponding bit will be cleared (set to 0), else bit will be set to 1
|
||||
*
|
||||
* @returns
|
||||
* Binary mask of connected pins
|
||||
*/
|
||||
int mask() {
|
||||
// No lock needed since _nc_mask is not modified outside the constructor
|
||||
return _nc_mask;
|
||||
}
|
||||
|
||||
/** A shorthand for read()
|
||||
*/
|
||||
operator int();
|
||||
|
||||
/** Access to particular bit in random-iterator fashion
|
||||
*/
|
||||
DigitalIn & operator[] (int index);
|
||||
|
||||
protected:
|
||||
DigitalIn* _pin[16];
|
||||
|
||||
/** Mask of bus's NC pins
|
||||
* If bit[n] is set to 1 - pin is connected
|
||||
* if bit[n] is cleared - pin is not connected (NC)
|
||||
*/
|
||||
int _nc_mask;
|
||||
|
||||
PlatformMutex _mutex;
|
||||
|
||||
/* disallow copy constructor and assignment operators */
|
||||
private:
|
||||
virtual void lock();
|
||||
virtual void unlock();
|
||||
BusIn(const BusIn&);
|
||||
BusIn & operator = (const BusIn&);
|
||||
};
|
||||
|
||||
} // namespace mbed
|
||||
|
||||
#endif
|
||||
|
||||
/** @}*/
|
||||
127
mbed/drivers/BusInOut.h
Normal file
127
mbed/drivers/BusInOut.h
Normal file
@@ -0,0 +1,127 @@
|
||||
/* mbed Microcontroller Library
|
||||
* Copyright (c) 2006-2013 ARM Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#ifndef MBED_BUSINOUT_H
|
||||
#define MBED_BUSINOUT_H
|
||||
|
||||
#include "drivers/DigitalInOut.h"
|
||||
#include "platform/PlatformMutex.h"
|
||||
|
||||
namespace mbed {
|
||||
/** \addtogroup drivers */
|
||||
/** @{*/
|
||||
|
||||
/** A digital input output bus, used for setting the state of a collection of pins
|
||||
*
|
||||
* @Note Synchronization level: Thread safe
|
||||
*/
|
||||
class BusInOut {
|
||||
|
||||
public:
|
||||
|
||||
/** Create an BusInOut, connected to the specified pins
|
||||
*
|
||||
* @param p<n> DigitalInOut pin to connect to bus bit p<n> (p5-p30, NC)
|
||||
*
|
||||
* @note
|
||||
* It is only required to specify as many pin variables as is required
|
||||
* for the bus; the rest will default to NC (not connected)
|
||||
*/
|
||||
BusInOut(PinName p0, PinName p1 = NC, PinName p2 = NC, PinName p3 = NC,
|
||||
PinName p4 = NC, PinName p5 = NC, PinName p6 = NC, PinName p7 = 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);
|
||||
|
||||
BusInOut(PinName pins[16]);
|
||||
|
||||
virtual ~BusInOut();
|
||||
|
||||
/* Group: Access Methods */
|
||||
|
||||
/** Write the value to the output bus
|
||||
*
|
||||
* @param value An integer specifying a bit to write for every corresponding DigitalInOut pin
|
||||
*/
|
||||
void write(int value);
|
||||
|
||||
/** Read the value currently output on the bus
|
||||
*
|
||||
* @returns
|
||||
* An integer with each bit corresponding to associated DigitalInOut pin setting
|
||||
*/
|
||||
int read();
|
||||
|
||||
/** Set as an output
|
||||
*/
|
||||
void output();
|
||||
|
||||
/** Set as an input
|
||||
*/
|
||||
void input();
|
||||
|
||||
/** Set the input pin mode
|
||||
*
|
||||
* @param mode PullUp, PullDown, PullNone
|
||||
*/
|
||||
void mode(PinMode pull);
|
||||
|
||||
/** Binary mask of bus pins connected to actual pins (not NC pins)
|
||||
* If bus pin is in NC state make corresponding bit will be cleared (set to 0), else bit will be set to 1
|
||||
*
|
||||
* @returns
|
||||
* Binary mask of connected pins
|
||||
*/
|
||||
int mask() {
|
||||
// No lock needed since _nc_mask is not modified outside the constructor
|
||||
return _nc_mask;
|
||||
}
|
||||
|
||||
/** A shorthand for write()
|
||||
*/
|
||||
BusInOut& operator= (int v);
|
||||
BusInOut& operator= (BusInOut& rhs);
|
||||
|
||||
/** Access to particular bit in random-iterator fashion
|
||||
*/
|
||||
DigitalInOut& operator[] (int index);
|
||||
|
||||
/** A shorthand for read()
|
||||
*/
|
||||
operator int();
|
||||
|
||||
protected:
|
||||
virtual void lock();
|
||||
virtual void unlock();
|
||||
DigitalInOut* _pin[16];
|
||||
|
||||
/** Mask of bus's NC pins
|
||||
* If bit[n] is set to 1 - pin is connected
|
||||
* if bit[n] is cleared - pin is not connected (NC)
|
||||
*/
|
||||
int _nc_mask;
|
||||
|
||||
PlatformMutex _mutex;
|
||||
|
||||
/* disallow copy constructor and assignment operators */
|
||||
private:
|
||||
BusInOut(const BusInOut&);
|
||||
BusInOut & operator = (const BusInOut&);
|
||||
};
|
||||
|
||||
} // namespace mbed
|
||||
|
||||
#endif
|
||||
|
||||
/** @}*/
|
||||
111
mbed/drivers/BusOut.h
Normal file
111
mbed/drivers/BusOut.h
Normal file
@@ -0,0 +1,111 @@
|
||||
/* mbed Microcontroller Library
|
||||
* Copyright (c) 2006-2013 ARM Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#ifndef MBED_BUSOUT_H
|
||||
#define MBED_BUSOUT_H
|
||||
|
||||
#include "drivers/DigitalOut.h"
|
||||
#include "platform/PlatformMutex.h"
|
||||
|
||||
namespace mbed {
|
||||
/** \addtogroup drivers */
|
||||
/** @{*/
|
||||
|
||||
/** A digital output bus, used for setting the state of a collection of pins
|
||||
*/
|
||||
class BusOut {
|
||||
|
||||
public:
|
||||
|
||||
/** Create an BusOut, connected to the specified pins
|
||||
*
|
||||
* @param p<n> DigitalOut pin to connect to bus bit <n> (p5-p30, NC)
|
||||
*
|
||||
* @Note Synchronization level: Thread safe
|
||||
*
|
||||
* @note
|
||||
* It is only required to specify as many pin variables as is required
|
||||
* for the bus; the rest will default to NC (not connected)
|
||||
*/
|
||||
BusOut(PinName p0, PinName p1 = NC, PinName p2 = NC, PinName p3 = NC,
|
||||
PinName p4 = NC, PinName p5 = NC, PinName p6 = NC, PinName p7 = 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);
|
||||
|
||||
BusOut(PinName pins[16]);
|
||||
|
||||
virtual ~BusOut();
|
||||
|
||||
/** Write the value to the output bus
|
||||
*
|
||||
* @param value An integer specifying a bit to write for every corresponding DigitalOut pin
|
||||
*/
|
||||
void write(int value);
|
||||
|
||||
/** Read the value currently output on the bus
|
||||
*
|
||||
* @returns
|
||||
* An integer with each bit corresponding to associated DigitalOut pin setting
|
||||
*/
|
||||
int read();
|
||||
|
||||
/** Binary mask of bus pins connected to actual pins (not NC pins)
|
||||
* If bus pin is in NC state make corresponding bit will be cleared (set to 0), else bit will be set to 1
|
||||
*
|
||||
* @returns
|
||||
* Binary mask of connected pins
|
||||
*/
|
||||
int mask() {
|
||||
// No lock needed since _nc_mask is not modified outside the constructor
|
||||
return _nc_mask;
|
||||
}
|
||||
|
||||
/** A shorthand for write()
|
||||
*/
|
||||
BusOut& operator= (int v);
|
||||
BusOut& operator= (BusOut& rhs);
|
||||
|
||||
/** Access to particular bit in random-iterator fashion
|
||||
*/
|
||||
DigitalOut& operator[] (int index);
|
||||
|
||||
/** A shorthand for read()
|
||||
*/
|
||||
operator int();
|
||||
|
||||
protected:
|
||||
virtual void lock();
|
||||
virtual void unlock();
|
||||
DigitalOut* _pin[16];
|
||||
|
||||
/** Mask of bus's NC pins
|
||||
* If bit[n] is set to 1 - pin is connected
|
||||
* if bit[n] is cleared - pin is not connected (NC)
|
||||
*/
|
||||
int _nc_mask;
|
||||
|
||||
PlatformMutex _mutex;
|
||||
|
||||
/* disallow copy constructor and assignment operators */
|
||||
private:
|
||||
BusOut(const BusOut&);
|
||||
BusOut & operator = (const BusOut&);
|
||||
};
|
||||
|
||||
} // namespace mbed
|
||||
|
||||
#endif
|
||||
|
||||
/** @}*/
|
||||
262
mbed/drivers/CAN.h
Normal file
262
mbed/drivers/CAN.h
Normal file
@@ -0,0 +1,262 @@
|
||||
/* mbed Microcontroller Library
|
||||
* Copyright (c) 2006-2013 ARM Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#ifndef MBED_CAN_H
|
||||
#define MBED_CAN_H
|
||||
|
||||
#include "platform/platform.h"
|
||||
|
||||
#if DEVICE_CAN
|
||||
|
||||
#include "hal/can_api.h"
|
||||
#include "platform/Callback.h"
|
||||
#include "platform/PlatformMutex.h"
|
||||
|
||||
namespace mbed {
|
||||
/** \addtogroup drivers */
|
||||
/** @{*/
|
||||
|
||||
/** CANMessage class
|
||||
*
|
||||
* @Note Synchronization level: Thread safe
|
||||
*/
|
||||
class CANMessage : public CAN_Message {
|
||||
|
||||
public:
|
||||
/** Creates empty CAN message.
|
||||
*/
|
||||
CANMessage() : CAN_Message() {
|
||||
len = 8;
|
||||
type = CANData;
|
||||
format = CANStandard;
|
||||
id = 0;
|
||||
memset(data, 0, 8);
|
||||
}
|
||||
|
||||
/** Creates CAN message with specific content.
|
||||
*/
|
||||
CANMessage(int _id, const char *_data, char _len = 8, CANType _type = CANData, CANFormat _format = CANStandard) {
|
||||
len = _len & 0xF;
|
||||
type = _type;
|
||||
format = _format;
|
||||
id = _id;
|
||||
memcpy(data, _data, _len);
|
||||
}
|
||||
|
||||
/** Creates CAN remote message.
|
||||
*/
|
||||
CANMessage(int _id, CANFormat _format = CANStandard) {
|
||||
len = 0;
|
||||
type = CANRemote;
|
||||
format = _format;
|
||||
id = _id;
|
||||
memset(data, 0, 8);
|
||||
}
|
||||
};
|
||||
|
||||
/** A can bus client, used for communicating with can devices
|
||||
*/
|
||||
class CAN {
|
||||
|
||||
public:
|
||||
/** Creates an CAN interface connected to specific pins.
|
||||
*
|
||||
* @param rd read from transmitter
|
||||
* @param td transmit to transmitter
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* #include "mbed.h"
|
||||
*
|
||||
* Ticker ticker;
|
||||
* DigitalOut led1(LED1);
|
||||
* DigitalOut led2(LED2);
|
||||
* CAN can1(p9, p10);
|
||||
* CAN can2(p30, p29);
|
||||
*
|
||||
* char counter = 0;
|
||||
*
|
||||
* void send() {
|
||||
* if(can1.write(CANMessage(1337, &counter, 1))) {
|
||||
* printf("Message sent: %d\n", counter);
|
||||
* counter++;
|
||||
* }
|
||||
* led1 = !led1;
|
||||
* }
|
||||
*
|
||||
* int main() {
|
||||
* ticker.attach(&send, 1);
|
||||
* CANMessage msg;
|
||||
* while(1) {
|
||||
* if(can2.read(msg)) {
|
||||
* printf("Message received: %d\n\n", msg.data[0]);
|
||||
* led2 = !led2;
|
||||
* }
|
||||
* wait(0.2);
|
||||
* }
|
||||
* }
|
||||
* @endcode
|
||||
*/
|
||||
CAN(PinName rd, PinName td);
|
||||
virtual ~CAN();
|
||||
|
||||
/** Set the frequency of the CAN interface
|
||||
*
|
||||
* @param hz The bus frequency in hertz
|
||||
*
|
||||
* @returns
|
||||
* 1 if successful,
|
||||
* 0 otherwise
|
||||
*/
|
||||
int frequency(int hz);
|
||||
|
||||
/** Write a CANMessage to the bus.
|
||||
*
|
||||
* @param msg The CANMessage to write.
|
||||
*
|
||||
* @returns
|
||||
* 0 if write failed,
|
||||
* 1 if write was successful
|
||||
*/
|
||||
int write(CANMessage msg);
|
||||
|
||||
/** Read a CANMessage from the bus.
|
||||
*
|
||||
* @param msg A CANMessage to read to.
|
||||
* @param handle message filter handle (0 for any message)
|
||||
*
|
||||
* @returns
|
||||
* 0 if no message arrived,
|
||||
* 1 if message arrived
|
||||
*/
|
||||
int read(CANMessage &msg, int handle = 0);
|
||||
|
||||
/** Reset CAN interface.
|
||||
*
|
||||
* To use after error overflow.
|
||||
*/
|
||||
void reset();
|
||||
|
||||
/** Puts or removes the CAN interface into silent monitoring mode
|
||||
*
|
||||
* @param silent boolean indicating whether to go into silent mode or not
|
||||
*/
|
||||
void monitor(bool silent);
|
||||
|
||||
enum Mode {
|
||||
Reset = 0,
|
||||
Normal,
|
||||
Silent,
|
||||
LocalTest,
|
||||
GlobalTest,
|
||||
SilentTest
|
||||
};
|
||||
|
||||
/** Change CAN operation to the specified mode
|
||||
*
|
||||
* @param mode The new operation mode (CAN::Normal, CAN::Silent, CAN::LocalTest, CAN::GlobalTest, CAN::SilentTest)
|
||||
*
|
||||
* @returns
|
||||
* 0 if mode change failed or unsupported,
|
||||
* 1 if mode change was successful
|
||||
*/
|
||||
int mode(Mode mode);
|
||||
|
||||
/** Filter out incomming messages
|
||||
*
|
||||
* @param id the id to filter on
|
||||
* @param mask the mask applied to the id
|
||||
* @param format format to filter on (Default CANAny)
|
||||
* @param handle message filter handle (Optional)
|
||||
*
|
||||
* @returns
|
||||
* 0 if filter change failed or unsupported,
|
||||
* new filter handle if successful
|
||||
*/
|
||||
int filter(unsigned int id, unsigned int mask, CANFormat format = CANAny, int handle = 0);
|
||||
|
||||
/** Returns number of read errors to detect read overflow errors.
|
||||
*/
|
||||
unsigned char rderror();
|
||||
|
||||
/** Returns number of write errors to detect write overflow errors.
|
||||
*/
|
||||
unsigned char tderror();
|
||||
|
||||
enum IrqType {
|
||||
RxIrq = 0,
|
||||
TxIrq,
|
||||
EwIrq,
|
||||
DoIrq,
|
||||
WuIrq,
|
||||
EpIrq,
|
||||
AlIrq,
|
||||
BeIrq,
|
||||
IdIrq,
|
||||
|
||||
IrqCnt
|
||||
};
|
||||
|
||||
/** Attach a function to call whenever a CAN frame received interrupt is
|
||||
* generated.
|
||||
*
|
||||
* @param func A pointer to a void function, or 0 to set as none
|
||||
* @param event 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);
|
||||
|
||||
/** Attach a member function to call whenever a CAN frame received interrupt
|
||||
* is generated.
|
||||
*
|
||||
* @param obj pointer to the object to call the member function on
|
||||
* @param method pointer to the member function to be called
|
||||
* @param event 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)
|
||||
*/
|
||||
template<typename T>
|
||||
void attach(T* obj, void (T::*method)(), IrqType type=RxIrq) {
|
||||
// Underlying call thread safe
|
||||
attach(Callback<void()>(obj, method), type);
|
||||
}
|
||||
|
||||
/** Attach a member function to call whenever a CAN frame received interrupt
|
||||
* is generated.
|
||||
*
|
||||
* @param obj pointer to the object to call the member function on
|
||||
* @param method pointer to the member function to be called
|
||||
* @param event 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)
|
||||
*/
|
||||
template<typename T>
|
||||
void attach(T* obj, void (*method)(T*), IrqType type=RxIrq) {
|
||||
// Underlying call thread safe
|
||||
attach(Callback<void()>(obj, method), type);
|
||||
}
|
||||
|
||||
static void _irq_handler(uint32_t id, CanIrqType type);
|
||||
|
||||
protected:
|
||||
virtual void lock();
|
||||
virtual void unlock();
|
||||
can_t _can;
|
||||
Callback<void()> _irq[IrqCnt];
|
||||
PlatformMutex _mutex;
|
||||
};
|
||||
|
||||
} // namespace mbed
|
||||
|
||||
#endif
|
||||
|
||||
#endif // MBED_CAN_H
|
||||
|
||||
/** @}*/
|
||||
119
mbed/drivers/DigitalIn.h
Normal file
119
mbed/drivers/DigitalIn.h
Normal file
@@ -0,0 +1,119 @@
|
||||
/* mbed Microcontroller Library
|
||||
* Copyright (c) 2006-2013 ARM Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#ifndef MBED_DIGITALIN_H
|
||||
#define MBED_DIGITALIN_H
|
||||
|
||||
#include "platform/platform.h"
|
||||
|
||||
#include "hal/gpio_api.h"
|
||||
#include "platform/mbed_critical.h"
|
||||
|
||||
namespace mbed {
|
||||
/** \addtogroup drivers */
|
||||
/** @{*/
|
||||
|
||||
/** A digital input, used for reading the state of a pin
|
||||
*
|
||||
* @Note Synchronization level: Interrupt safe
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* // Flash an LED while a DigitalIn is true
|
||||
*
|
||||
* #include "mbed.h"
|
||||
*
|
||||
* DigitalIn enable(p5);
|
||||
* DigitalOut led(LED1);
|
||||
*
|
||||
* int main() {
|
||||
* while(1) {
|
||||
* if(enable) {
|
||||
* led = !led;
|
||||
* }
|
||||
* wait(0.25);
|
||||
* }
|
||||
* }
|
||||
* @endcode
|
||||
*/
|
||||
class DigitalIn {
|
||||
|
||||
public:
|
||||
/** Create a DigitalIn connected to the specified pin
|
||||
*
|
||||
* @param pin DigitalIn pin to connect to
|
||||
*/
|
||||
DigitalIn(PinName pin) : gpio() {
|
||||
// No lock needed in the constructor
|
||||
gpio_init_in(&gpio, pin);
|
||||
}
|
||||
|
||||
/** Create a DigitalIn connected to the specified pin
|
||||
*
|
||||
* @param pin DigitalIn pin to connect to
|
||||
* @param mode the initial mode of the pin
|
||||
*/
|
||||
DigitalIn(PinName pin, PinMode mode) : gpio() {
|
||||
// No lock needed in the constructor
|
||||
gpio_init_in_ex(&gpio, pin, mode);
|
||||
}
|
||||
/** Read the input, represented as 0 or 1 (int)
|
||||
*
|
||||
* @returns
|
||||
* An integer representing the state of the input pin,
|
||||
* 0 for logical 0, 1 for logical 1
|
||||
*/
|
||||
int read() {
|
||||
// Thread safe / atomic HAL call
|
||||
return gpio_read(&gpio);
|
||||
}
|
||||
|
||||
/** Set the input pin mode
|
||||
*
|
||||
* @param mode PullUp, PullDown, PullNone, OpenDrain
|
||||
*/
|
||||
void mode(PinMode pull) {
|
||||
core_util_critical_section_enter();
|
||||
gpio_mode(&gpio, pull);
|
||||
core_util_critical_section_exit();
|
||||
}
|
||||
|
||||
/** Return the output setting, represented as 0 or 1 (int)
|
||||
*
|
||||
* @returns
|
||||
* Non zero value if pin is connected to uc GPIO
|
||||
* 0 if gpio object was initialized with NC
|
||||
*/
|
||||
int is_connected() {
|
||||
// Thread safe / atomic HAL call
|
||||
return gpio_is_connected(&gpio);
|
||||
}
|
||||
|
||||
/** An operator shorthand for read()
|
||||
*/
|
||||
operator int() {
|
||||
// Underlying read is thread safe
|
||||
return read();
|
||||
}
|
||||
|
||||
protected:
|
||||
gpio_t gpio;
|
||||
};
|
||||
|
||||
} // namespace mbed
|
||||
|
||||
#endif
|
||||
|
||||
/** @}*/
|
||||
144
mbed/drivers/DigitalInOut.h
Normal file
144
mbed/drivers/DigitalInOut.h
Normal file
@@ -0,0 +1,144 @@
|
||||
/* mbed Microcontroller Library
|
||||
* Copyright (c) 2006-2013 ARM Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#ifndef MBED_DIGITALINOUT_H
|
||||
#define MBED_DIGITALINOUT_H
|
||||
|
||||
#include "platform/platform.h"
|
||||
|
||||
#include "hal/gpio_api.h"
|
||||
#include "platform/mbed_critical.h"
|
||||
|
||||
namespace mbed {
|
||||
/** \addtogroup drivers */
|
||||
/** @{*/
|
||||
|
||||
/** A digital input/output, used for setting or reading a bi-directional pin
|
||||
*
|
||||
* @Note Synchronization level: Interrupt safe
|
||||
*/
|
||||
class DigitalInOut {
|
||||
|
||||
public:
|
||||
/** Create a DigitalInOut connected to the specified pin
|
||||
*
|
||||
* @param pin DigitalInOut pin to connect to
|
||||
*/
|
||||
DigitalInOut(PinName pin) : gpio() {
|
||||
// No lock needed in the constructor
|
||||
gpio_init_in(&gpio, pin);
|
||||
}
|
||||
|
||||
/** Create a DigitalInOut connected to the specified pin
|
||||
*
|
||||
* @param pin DigitalInOut pin to connect to
|
||||
* @param direction the initial direction of the pin
|
||||
* @param mode the initial mode of the pin
|
||||
* @param value the initial value of the pin if is an output
|
||||
*/
|
||||
DigitalInOut(PinName pin, PinDirection direction, PinMode mode, int value) : gpio() {
|
||||
// No lock needed in the constructor
|
||||
gpio_init_inout(&gpio, pin, direction, mode, value);
|
||||
}
|
||||
|
||||
/** Set the output, specified as 0 or 1 (int)
|
||||
*
|
||||
* @param value An integer specifying the pin output value,
|
||||
* 0 for logical 0, 1 (or any other non-zero value) for logical 1
|
||||
*/
|
||||
void write(int value) {
|
||||
// Thread safe / atomic HAL call
|
||||
gpio_write(&gpio, value);
|
||||
}
|
||||
|
||||
/** Return the output setting, represented as 0 or 1 (int)
|
||||
*
|
||||
* @returns
|
||||
* an integer representing the output setting of the pin if it is an output,
|
||||
* or read the input if set as an input
|
||||
*/
|
||||
int read() {
|
||||
// Thread safe / atomic HAL call
|
||||
return gpio_read(&gpio);
|
||||
}
|
||||
|
||||
/** Set as an output
|
||||
*/
|
||||
void output() {
|
||||
core_util_critical_section_enter();
|
||||
gpio_dir(&gpio, PIN_OUTPUT);
|
||||
core_util_critical_section_exit();
|
||||
}
|
||||
|
||||
/** Set as an input
|
||||
*/
|
||||
void input() {
|
||||
core_util_critical_section_enter();
|
||||
gpio_dir(&gpio, PIN_INPUT);
|
||||
core_util_critical_section_exit();
|
||||
}
|
||||
|
||||
/** Set the input pin mode
|
||||
*
|
||||
* @param mode PullUp, PullDown, PullNone, OpenDrain
|
||||
*/
|
||||
void mode(PinMode pull) {
|
||||
core_util_critical_section_enter();
|
||||
gpio_mode(&gpio, pull);
|
||||
core_util_critical_section_exit();
|
||||
}
|
||||
|
||||
/** Return the output setting, represented as 0 or 1 (int)
|
||||
*
|
||||
* @returns
|
||||
* Non zero value if pin is connected to uc GPIO
|
||||
* 0 if gpio object was initialized with NC
|
||||
*/
|
||||
int is_connected() {
|
||||
// Thread safe / atomic HAL call
|
||||
return gpio_is_connected(&gpio);
|
||||
}
|
||||
|
||||
/** A shorthand for write()
|
||||
*/
|
||||
DigitalInOut& operator= (int value) {
|
||||
// Underlying write is thread safe
|
||||
write(value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
DigitalInOut& operator= (DigitalInOut& rhs) {
|
||||
core_util_critical_section_enter();
|
||||
write(rhs.read());
|
||||
core_util_critical_section_exit();
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** A shorthand for read()
|
||||
*/
|
||||
operator int() {
|
||||
// Underlying call is thread safe
|
||||
return read();
|
||||
}
|
||||
|
||||
protected:
|
||||
gpio_t gpio;
|
||||
};
|
||||
|
||||
} // namespace mbed
|
||||
|
||||
#endif
|
||||
|
||||
/** @}*/
|
||||
130
mbed/drivers/DigitalOut.h
Normal file
130
mbed/drivers/DigitalOut.h
Normal file
@@ -0,0 +1,130 @@
|
||||
/* mbed Microcontroller Library
|
||||
* Copyright (c) 2006-2013 ARM Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#ifndef MBED_DIGITALOUT_H
|
||||
#define MBED_DIGITALOUT_H
|
||||
|
||||
#include "platform/platform.h"
|
||||
#include "hal/gpio_api.h"
|
||||
#include "platform/mbed_critical.h"
|
||||
|
||||
namespace mbed {
|
||||
/** \addtogroup drivers */
|
||||
/** @{*/
|
||||
|
||||
/** A digital output, used for setting the state of a pin
|
||||
*
|
||||
* @Note Synchronization level: Interrupt safe
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* // Toggle a LED
|
||||
* #include "mbed.h"
|
||||
*
|
||||
* DigitalOut led(LED1);
|
||||
*
|
||||
* int main() {
|
||||
* while(1) {
|
||||
* led = !led;
|
||||
* wait(0.2);
|
||||
* }
|
||||
* }
|
||||
* @endcode
|
||||
*/
|
||||
class DigitalOut {
|
||||
|
||||
public:
|
||||
/** Create a DigitalOut connected to the specified pin
|
||||
*
|
||||
* @param pin DigitalOut pin to connect to
|
||||
*/
|
||||
DigitalOut(PinName pin) : gpio() {
|
||||
// No lock needed in the constructor
|
||||
gpio_init_out(&gpio, pin);
|
||||
}
|
||||
|
||||
/** Create a DigitalOut connected to the specified pin
|
||||
*
|
||||
* @param pin DigitalOut pin to connect to
|
||||
* @param value the initial pin value
|
||||
*/
|
||||
DigitalOut(PinName pin, int value) : gpio() {
|
||||
// No lock needed in the constructor
|
||||
gpio_init_out_ex(&gpio, pin, value);
|
||||
}
|
||||
|
||||
/** Set the output, specified as 0 or 1 (int)
|
||||
*
|
||||
* @param value An integer specifying the pin output value,
|
||||
* 0 for logical 0, 1 (or any other non-zero value) for logical 1
|
||||
*/
|
||||
void write(int value) {
|
||||
// Thread safe / atomic HAL call
|
||||
gpio_write(&gpio, value);
|
||||
}
|
||||
|
||||
/** Return the output setting, represented as 0 or 1 (int)
|
||||
*
|
||||
* @returns
|
||||
* an integer representing the output setting of the pin,
|
||||
* 0 for logical 0, 1 for logical 1
|
||||
*/
|
||||
int read() {
|
||||
// Thread safe / atomic HAL call
|
||||
return gpio_read(&gpio);
|
||||
}
|
||||
|
||||
/** Return the output setting, represented as 0 or 1 (int)
|
||||
*
|
||||
* @returns
|
||||
* Non zero value if pin is connected to uc GPIO
|
||||
* 0 if gpio object was initialized with NC
|
||||
*/
|
||||
int is_connected() {
|
||||
// Thread safe / atomic HAL call
|
||||
return gpio_is_connected(&gpio);
|
||||
}
|
||||
|
||||
/** A shorthand for write()
|
||||
*/
|
||||
DigitalOut& operator= (int value) {
|
||||
// Underlying write is thread safe
|
||||
write(value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
DigitalOut& operator= (DigitalOut& rhs) {
|
||||
core_util_critical_section_enter();
|
||||
write(rhs.read());
|
||||
core_util_critical_section_exit();
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** A shorthand for read()
|
||||
*/
|
||||
operator int() {
|
||||
// Underlying call is thread safe
|
||||
return read();
|
||||
}
|
||||
|
||||
protected:
|
||||
gpio_t gpio;
|
||||
};
|
||||
|
||||
} // namespace mbed
|
||||
|
||||
#endif
|
||||
|
||||
/** @}*/
|
||||
115
mbed/drivers/DirHandle.h
Normal file
115
mbed/drivers/DirHandle.h
Normal file
@@ -0,0 +1,115 @@
|
||||
/* mbed Microcontroller Library
|
||||
* Copyright (c) 2006-2013 ARM Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#ifndef MBED_DIRHANDLE_H
|
||||
#define MBED_DIRHANDLE_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include "platform/platform.h"
|
||||
|
||||
#include "FileHandle.h"
|
||||
|
||||
namespace mbed {
|
||||
/** \addtogroup drivers */
|
||||
/** @{*/
|
||||
|
||||
/** Represents a directory stream. Objects of this type are returned
|
||||
* by a FileSystemLike's opendir method. Implementations must define
|
||||
* at least closedir, readdir and rewinddir.
|
||||
*
|
||||
* If a FileSystemLike class defines the opendir method, then the
|
||||
* directories of an object of that type can be accessed by
|
||||
* DIR *d = opendir("/example/directory") (or opendir("/example")
|
||||
* to open the root of the filesystem), and then using readdir(d) etc.
|
||||
*
|
||||
* The root directory is considered to contain all FileLike and
|
||||
* FileSystemLike objects, so the DIR* returned by opendir("/") will
|
||||
* reflect this.
|
||||
*
|
||||
* @Note Synchronization level: Set by subclass
|
||||
*/
|
||||
class DirHandle {
|
||||
public:
|
||||
MBED_DEPRECATED_SINCE("mbed-os-5.4",
|
||||
"The mbed 2 filesystem classes have been superseeded by the FileSystem api, "
|
||||
"Replaced by File")
|
||||
DirHandle() {}
|
||||
|
||||
/** Closes the directory.
|
||||
*
|
||||
* @returns
|
||||
* 0 on success,
|
||||
* -1 on error.
|
||||
*/
|
||||
virtual int closedir()=0;
|
||||
|
||||
/** Return the directory entry at the current position, and
|
||||
* advances the position to the next entry.
|
||||
*
|
||||
* @returns
|
||||
* A pointer to a dirent structure representing the
|
||||
* directory entry at the current position, or NULL on reaching
|
||||
* end of directory or error.
|
||||
*/
|
||||
virtual struct dirent *readdir()=0;
|
||||
|
||||
/** Resets the position to the beginning of the directory.
|
||||
*/
|
||||
virtual void rewinddir()=0;
|
||||
|
||||
/** Returns the current position of the DirHandle.
|
||||
*
|
||||
* @returns
|
||||
* the current position,
|
||||
* -1 on error.
|
||||
*/
|
||||
virtual off_t telldir() { return -1; }
|
||||
|
||||
/** Sets the position of the DirHandle.
|
||||
*
|
||||
* @param location The location to seek to. Must be a value returned by telldir.
|
||||
*/
|
||||
virtual void seekdir(off_t location) { (void)location;}
|
||||
|
||||
virtual ~DirHandle() {}
|
||||
|
||||
protected:
|
||||
|
||||
/** Acquire exclusive access to this object.
|
||||
*/
|
||||
virtual void lock() {
|
||||
// Stub
|
||||
}
|
||||
|
||||
/** Release exclusive access to this object.
|
||||
*/
|
||||
virtual void unlock() {
|
||||
// Stub
|
||||
}
|
||||
|
||||
protected:
|
||||
/** Internal-only constructor to work around deprecated notices when not used
|
||||
*. due to nested deprecations and difficulty of compilers finding their way around
|
||||
* the class hierarchy
|
||||
*/
|
||||
friend class FileSystemLike;
|
||||
DirHandle(int) {}
|
||||
};
|
||||
|
||||
} // namespace mbed
|
||||
|
||||
#endif /* MBED_DIRHANDLE_H */
|
||||
|
||||
/** @}*/
|
||||
176
mbed/drivers/Ethernet.h
Normal file
176
mbed/drivers/Ethernet.h
Normal file
@@ -0,0 +1,176 @@
|
||||
/* mbed Microcontroller Library
|
||||
* Copyright (c) 2006-2013 ARM Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#ifndef MBED_ETHERNET_H
|
||||
#define MBED_ETHERNET_H
|
||||
|
||||
#include "platform/platform.h"
|
||||
|
||||
#if DEVICE_ETHERNET
|
||||
|
||||
namespace mbed {
|
||||
/** \addtogroup drivers */
|
||||
/** @{*/
|
||||
|
||||
/** An ethernet interface, to use with the ethernet pins.
|
||||
*
|
||||
* @Note Synchronization level: Not protected
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* // Read destination and source from every ethernet packet
|
||||
*
|
||||
* #include "mbed.h"
|
||||
*
|
||||
* Ethernet eth;
|
||||
*
|
||||
* int main() {
|
||||
* char buf[0x600];
|
||||
*
|
||||
* while(1) {
|
||||
* int size = eth.receive();
|
||||
* if(size > 0) {
|
||||
* eth.read(buf, size);
|
||||
* printf("Destination: %02X:%02X:%02X:%02X:%02X:%02X\n",
|
||||
* buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
|
||||
* printf("Source: %02X:%02X:%02X:%02X:%02X:%02X\n",
|
||||
* buf[6], buf[7], buf[8], buf[9], buf[10], buf[11]);
|
||||
* }
|
||||
*
|
||||
* wait(1);
|
||||
* }
|
||||
* }
|
||||
* @endcode
|
||||
*/
|
||||
class Ethernet {
|
||||
|
||||
public:
|
||||
|
||||
/** Initialise the ethernet interface.
|
||||
*/
|
||||
Ethernet();
|
||||
|
||||
/** Powers the hardware down.
|
||||
*/
|
||||
virtual ~Ethernet();
|
||||
|
||||
enum Mode {
|
||||
AutoNegotiate,
|
||||
HalfDuplex10,
|
||||
FullDuplex10,
|
||||
HalfDuplex100,
|
||||
FullDuplex100
|
||||
};
|
||||
|
||||
/** Writes into an outgoing ethernet packet.
|
||||
*
|
||||
* It will append size bytes of data to the previously written bytes.
|
||||
*
|
||||
* @param data An array to write.
|
||||
* @param size The size of data.
|
||||
*
|
||||
* @returns
|
||||
* The number of written bytes.
|
||||
*/
|
||||
int write(const char *data, int size);
|
||||
|
||||
/** Send an outgoing ethernet packet.
|
||||
*
|
||||
* After filling in the data in an ethernet packet it must be send.
|
||||
* Send will provide a new packet to write to.
|
||||
*
|
||||
* @returns
|
||||
* 0 if the sending was failed,
|
||||
* or the size of the packet successfully sent.
|
||||
*/
|
||||
int send();
|
||||
|
||||
/** Recevies an arrived ethernet packet.
|
||||
*
|
||||
* Receiving an ethernet packet will drop the last received ethernet packet
|
||||
* and make a new ethernet packet ready to read.
|
||||
* If no ethernet packet is arrived it will return 0.
|
||||
*
|
||||
* @returns
|
||||
* 0 if no ethernet packet is arrived,
|
||||
* or the size of the arrived packet.
|
||||
*/
|
||||
int receive();
|
||||
|
||||
/** Read from an recevied ethernet packet.
|
||||
*
|
||||
* After receive returnd a number bigger than 0it is
|
||||
* possible to read bytes from this packet.
|
||||
* Read will write up to size bytes into data.
|
||||
*
|
||||
* It is possible to use read multible times.
|
||||
* Each time read will start reading after the last read byte before.
|
||||
*
|
||||
* @returns
|
||||
* The number of byte read.
|
||||
*/
|
||||
int read(char *data, int size);
|
||||
|
||||
/** Gives the ethernet address of the mbed.
|
||||
*
|
||||
* @param mac Must be a pointer to a 6 byte char array to copy the ethernet address in.
|
||||
*/
|
||||
void address(char *mac);
|
||||
|
||||
/** Returns if an ethernet link is pressent or not. It takes a wile after Ethernet initializion to show up.
|
||||
*
|
||||
* @returns
|
||||
* 0 if no ethernet link is pressent,
|
||||
* 1 if an ethernet link is pressent.
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* // Using the Ethernet link function
|
||||
* #include "mbed.h"
|
||||
*
|
||||
* Ethernet eth;
|
||||
*
|
||||
* int main() {
|
||||
* wait(1); // Needed after startup.
|
||||
* if (eth.link()) {
|
||||
* printf("online\n");
|
||||
* } else {
|
||||
* printf("offline\n");
|
||||
* }
|
||||
* }
|
||||
* @endcode
|
||||
*/
|
||||
int link();
|
||||
|
||||
/** Sets the speed and duplex parameters of an ethernet link
|
||||
*
|
||||
* - AutoNegotiate Auto negotiate speed and duplex
|
||||
* - HalfDuplex10 10 Mbit, half duplex
|
||||
* - FullDuplex10 10 Mbit, full duplex
|
||||
* - HalfDuplex100 100 Mbit, half duplex
|
||||
* - FullDuplex100 100 Mbit, full duplex
|
||||
*
|
||||
* @param mode the speed and duplex mode to set the link to:
|
||||
*/
|
||||
void set_link(Mode mode);
|
||||
};
|
||||
|
||||
} // namespace mbed
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
/** @}*/
|
||||
65
mbed/drivers/FileBase.h
Normal file
65
mbed/drivers/FileBase.h
Normal file
@@ -0,0 +1,65 @@
|
||||
/* mbed Microcontroller Library
|
||||
* Copyright (c) 2006-2013 ARM Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#ifndef MBED_FILEBASE_H
|
||||
#define MBED_FILEBASE_H
|
||||
|
||||
typedef int FILEHANDLE;
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
|
||||
#include "platform/platform.h"
|
||||
#include "platform/SingletonPtr.h"
|
||||
#include "platform/PlatformMutex.h"
|
||||
|
||||
namespace mbed {
|
||||
/** \addtogroup drivers */
|
||||
/** @{*/
|
||||
|
||||
typedef enum {
|
||||
FilePathType,
|
||||
FileSystemPathType
|
||||
} PathType;
|
||||
|
||||
class FileBase {
|
||||
public:
|
||||
FileBase(const char *name, PathType t);
|
||||
virtual ~FileBase();
|
||||
|
||||
const char* getName(void);
|
||||
PathType getPathType(void);
|
||||
|
||||
static FileBase *lookup(const char *name, unsigned int len);
|
||||
|
||||
static FileBase *get(int n);
|
||||
|
||||
/* disallow copy constructor and assignment operators */
|
||||
private:
|
||||
static FileBase *_head;
|
||||
static SingletonPtr<PlatformMutex> _mutex;
|
||||
|
||||
FileBase *_next;
|
||||
const char * const _name;
|
||||
const PathType _path_type;
|
||||
FileBase(const FileBase&);
|
||||
FileBase & operator = (const FileBase&);
|
||||
};
|
||||
|
||||
} // namespace mbed
|
||||
|
||||
#endif
|
||||
|
||||
/** @}*/
|
||||
142
mbed/drivers/FileHandle.h
Normal file
142
mbed/drivers/FileHandle.h
Normal file
@@ -0,0 +1,142 @@
|
||||
/* mbed Microcontroller Library
|
||||
* Copyright (c) 2006-2013 ARM Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#ifndef MBED_FILEHANDLE_H
|
||||
#define MBED_FILEHANDLE_H
|
||||
|
||||
typedef int FILEHANDLE;
|
||||
|
||||
#include <stdio.h>
|
||||
#include "platform/platform.h"
|
||||
|
||||
namespace mbed {
|
||||
/** \addtogroup drivers */
|
||||
/** @{*/
|
||||
|
||||
/** An OO equivalent of the internal FILEHANDLE variable
|
||||
* and associated _sys_* functions.
|
||||
*
|
||||
* FileHandle is an abstract class, needing at least sys_write and
|
||||
* sys_read to be implmented for a simple interactive device.
|
||||
*
|
||||
* No one ever directly tals to/instanciates a FileHandle - it gets
|
||||
* created by FileSystem, and wrapped up by stdio.
|
||||
*
|
||||
* @Note Synchronization level: Set by subclass
|
||||
*/
|
||||
class FileHandle {
|
||||
|
||||
public:
|
||||
MBED_DEPRECATED_SINCE("mbed-os-5.4",
|
||||
"The mbed 2 filesystem classes have been superseeded by the FileSystem api, "
|
||||
"Replaced by File")
|
||||
FileHandle() {}
|
||||
|
||||
/** Write the contents of a buffer to the file
|
||||
*
|
||||
* @param buffer the buffer to write from
|
||||
* @param length the number of characters to write
|
||||
*
|
||||
* @returns
|
||||
* The number of characters written (possibly 0) on success, -1 on error.
|
||||
*/
|
||||
virtual ssize_t write(const void* buffer, size_t length) = 0;
|
||||
|
||||
/** Close the file
|
||||
*
|
||||
* @returns
|
||||
* Zero on success, -1 on error.
|
||||
*/
|
||||
virtual int close() = 0;
|
||||
|
||||
/** Function read
|
||||
* Reads the contents of the file into a buffer
|
||||
*
|
||||
* @param buffer the buffer to read in to
|
||||
* @param length the number of characters to read
|
||||
*
|
||||
* @returns
|
||||
* The number of characters read (zero at end of file) on success, -1 on error.
|
||||
*/
|
||||
virtual ssize_t read(void* buffer, size_t length) = 0;
|
||||
|
||||
/** Check if the handle is for a interactive terminal device.
|
||||
* If so, line buffered behaviour is used by default
|
||||
*
|
||||
* @returns
|
||||
* 1 if it is a terminal,
|
||||
* 0 otherwise
|
||||
*/
|
||||
virtual int isatty() = 0;
|
||||
|
||||
/** Move the file position to a given offset from a given location.
|
||||
*
|
||||
* @param offset The offset from whence to move to
|
||||
* @param whence SEEK_SET for the start of the file, SEEK_CUR for the
|
||||
* current file position, or SEEK_END for the end of the file.
|
||||
*
|
||||
* @returns
|
||||
* new file position on success,
|
||||
* -1 on failure or unsupported
|
||||
*/
|
||||
virtual off_t lseek(off_t offset, int whence) = 0;
|
||||
|
||||
/** Flush any buffers associated with the FileHandle, ensuring it
|
||||
* is up to date on disk
|
||||
*
|
||||
* @returns
|
||||
* 0 on success or un-needed,
|
||||
* -1 on error
|
||||
*/
|
||||
virtual int fsync() = 0;
|
||||
|
||||
virtual off_t flen() {
|
||||
lock();
|
||||
/* remember our current position */
|
||||
off_t pos = lseek(0, SEEK_CUR);
|
||||
if(pos == -1) {
|
||||
unlock();
|
||||
return -1;
|
||||
}
|
||||
/* seek to the end to get the file length */
|
||||
off_t res = lseek(0, SEEK_END);
|
||||
/* return to our old position */
|
||||
lseek(pos, SEEK_SET);
|
||||
unlock();
|
||||
return res;
|
||||
}
|
||||
|
||||
virtual ~FileHandle() {};
|
||||
|
||||
protected:
|
||||
|
||||
/** Acquire exclusive access to this object.
|
||||
*/
|
||||
virtual void lock() {
|
||||
// Stub
|
||||
}
|
||||
|
||||
/** Release exclusive access to this object.
|
||||
*/
|
||||
virtual void unlock() {
|
||||
// Stub
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace mbed
|
||||
|
||||
#endif
|
||||
|
||||
/** @}*/
|
||||
154
mbed/drivers/FileLike.h
Normal file
154
mbed/drivers/FileLike.h
Normal file
@@ -0,0 +1,154 @@
|
||||
/* mbed Microcontroller Library
|
||||
* Copyright (c) 2006-2013 ARM Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#ifndef MBED_FILELIKE_H
|
||||
#define MBED_FILELIKE_H
|
||||
|
||||
#include "platform/mbed_toolchain.h"
|
||||
#include "drivers/FileBase.h"
|
||||
|
||||
namespace mbed {
|
||||
/** \addtogroup drivers */
|
||||
/** @{*/
|
||||
|
||||
|
||||
/* Class FileLike
|
||||
* A file-like object is one that can be opened with fopen by
|
||||
* fopen("/name", mode).
|
||||
*
|
||||
* @Note Synchronization level: Set by subclass
|
||||
*/
|
||||
class FileLike : public FileBase {
|
||||
public:
|
||||
/** Constructor FileLike
|
||||
*
|
||||
* @param name The name to use to open the file.
|
||||
*/
|
||||
FileLike(const char *name = NULL) : FileBase(name, FilePathType) {}
|
||||
virtual ~FileLike() {}
|
||||
|
||||
/** Read the contents of a file into a buffer
|
||||
*
|
||||
* @param buffer The buffer to read in to
|
||||
* @param size The number of bytes to read
|
||||
* @return The number of bytes read, 0 at end of file, negative error on failure
|
||||
*/
|
||||
virtual ssize_t read(void *buffer, size_t len) = 0;
|
||||
|
||||
/** Write the contents of a buffer to a file
|
||||
*
|
||||
* @param buffer The buffer to write from
|
||||
* @param size The number of bytes to write
|
||||
* @return The number of bytes written, negative error on failure
|
||||
*/
|
||||
virtual ssize_t write(const void *buffer, size_t len) = 0;
|
||||
|
||||
/** Close a file
|
||||
*
|
||||
* @return 0 on success, negative error code on failure
|
||||
*/
|
||||
virtual int close() = 0;
|
||||
|
||||
/** Flush any buffers associated with the file
|
||||
*
|
||||
* @return 0 on success, negative error code on failure
|
||||
*/
|
||||
virtual int sync() = 0;
|
||||
|
||||
/** Check if the file in an interactive terminal device
|
||||
*
|
||||
* @return True if the file is a terminal
|
||||
*/
|
||||
virtual int isatty() = 0;
|
||||
|
||||
/** Move the file position to a given offset from from a given location
|
||||
*
|
||||
* @param offset The offset from whence to move to
|
||||
* @param whence The start of where to seek
|
||||
* SEEK_SET to start from beginning of file,
|
||||
* SEEK_CUR to start from current position in file,
|
||||
* SEEK_END to start from end of file
|
||||
* @return The new offset of the file
|
||||
*/
|
||||
virtual off_t seek(off_t offset, int whence = SEEK_SET) = 0;
|
||||
|
||||
/** Get the file position of the file
|
||||
*
|
||||
* @return The current offset in the file
|
||||
*/
|
||||
virtual off_t tell() = 0;
|
||||
|
||||
/** Rewind the file position to the beginning of the file
|
||||
*
|
||||
* @note This is equivalent to file_seek(file, 0, FS_SEEK_SET)
|
||||
*/
|
||||
virtual void rewind() = 0;
|
||||
|
||||
/** Get the size of the file
|
||||
*
|
||||
* @return Size of the file in bytes
|
||||
*/
|
||||
virtual size_t size() = 0;
|
||||
|
||||
/** Move the file position to a given offset from a given location.
|
||||
*
|
||||
* @param offset The offset from whence to move to
|
||||
* @param whence SEEK_SET for the start of the file, SEEK_CUR for the
|
||||
* current file position, or SEEK_END for the end of the file.
|
||||
*
|
||||
* @returns
|
||||
* new file position on success,
|
||||
* -1 on failure or unsupported
|
||||
*/
|
||||
MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by FileLike::seek")
|
||||
virtual off_t lseek(off_t offset, int whence) { return seek(offset, whence); }
|
||||
|
||||
/** Flush any buffers associated with the FileHandle, ensuring it
|
||||
* is up to date on disk
|
||||
*
|
||||
* @returns
|
||||
* 0 on success or un-needed,
|
||||
* -1 on error
|
||||
*/
|
||||
MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by FileLike::sync")
|
||||
virtual int fsync() { return sync(); }
|
||||
|
||||
/** Find the length of the file
|
||||
*
|
||||
* @returns
|
||||
* Length of the file
|
||||
*/
|
||||
MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by FileLike::size")
|
||||
virtual off_t flen() { return size(); }
|
||||
|
||||
protected:
|
||||
/** Acquire exclusive access to this object.
|
||||
*/
|
||||
virtual void lock() {
|
||||
// Stub
|
||||
}
|
||||
|
||||
/** Release exclusive access to this object.
|
||||
*/
|
||||
virtual void unlock() {
|
||||
// Stub
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/** @}*/
|
||||
} // namespace mbed
|
||||
|
||||
#endif
|
||||
52
mbed/drivers/FilePath.h
Normal file
52
mbed/drivers/FilePath.h
Normal file
@@ -0,0 +1,52 @@
|
||||
/* mbed Microcontroller Library
|
||||
* Copyright (c) 2006-2013 ARM Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#ifndef MBED_FILEPATH_H
|
||||
#define MBED_FILEPATH_H
|
||||
|
||||
#include "platform/platform.h"
|
||||
|
||||
#include "drivers/FileSystemLike.h"
|
||||
#include "drivers/FileLike.h"
|
||||
|
||||
namespace mbed {
|
||||
/** \addtogroup drivers */
|
||||
/** @{*/
|
||||
|
||||
class FileSystem;
|
||||
|
||||
class FilePath {
|
||||
public:
|
||||
FilePath(const char* file_path);
|
||||
|
||||
const char* fileName(void);
|
||||
|
||||
bool isFileSystem(void);
|
||||
FileSystem* fileSystem(void);
|
||||
|
||||
bool isFile(void);
|
||||
FileLike* file(void);
|
||||
bool exists(void);
|
||||
|
||||
private:
|
||||
const char* file_name;
|
||||
FileBase* fb;
|
||||
};
|
||||
|
||||
} // namespace mbed
|
||||
|
||||
#endif
|
||||
|
||||
/** @}*/
|
||||
124
mbed/drivers/FileSystemLike.h
Normal file
124
mbed/drivers/FileSystemLike.h
Normal file
@@ -0,0 +1,124 @@
|
||||
/* mbed Microcontroller Library
|
||||
* Copyright (c) 2006-2013 ARM Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#ifndef MBED_FILESYSTEMLIKE_H
|
||||
#define MBED_FILESYSTEMLIKE_H
|
||||
|
||||
#include "platform/platform.h"
|
||||
|
||||
#include "drivers/FileBase.h"
|
||||
#include "drivers/FileHandle.h"
|
||||
#include "drivers/DirHandle.h"
|
||||
|
||||
namespace mbed {
|
||||
/** \addtogroup drivers */
|
||||
/** @{*/
|
||||
|
||||
/** A filesystem-like object is one that can be used to open files
|
||||
* though it by fopen("/name/filename", mode)
|
||||
*
|
||||
* Implementations must define at least open (the default definitions
|
||||
* of the rest of the functions just return error values).
|
||||
*
|
||||
* @Note Synchronization level: Set by subclass
|
||||
*/
|
||||
class FileSystemLike : public FileBase {
|
||||
|
||||
public:
|
||||
/** FileSystemLike constructor
|
||||
*
|
||||
* @param name The name to use for the filesystem.
|
||||
*/
|
||||
MBED_DEPRECATED_SINCE("mbed-os-5.4",
|
||||
"The mbed 2 filesystem classes have been superseeded by the FileSystem api, "
|
||||
"Replaced by FileSystem")
|
||||
FileSystemLike(const char *name);
|
||||
|
||||
virtual ~FileSystemLike();
|
||||
|
||||
MBED_DEPRECATED_SINCE("mbed-os-5.4",
|
||||
"The mbed 2 filesystem classes have been superseeded by the FileSystem api, "
|
||||
"Replaced by FileSystem")
|
||||
static DirHandle *opendir();
|
||||
friend class BaseDirHandle;
|
||||
|
||||
/** Opens a file from the filesystem
|
||||
*
|
||||
* @param filename The name of the file to open.
|
||||
* @param flags One of O_RDONLY, O_WRONLY, or O_RDWR, OR'd with
|
||||
* zero or more of O_CREAT, O_TRUNC, or O_APPEND.
|
||||
*
|
||||
* @returns
|
||||
* A pointer to a FileHandle object representing the
|
||||
* file on success, or NULL on failure.
|
||||
*/
|
||||
virtual FileHandle *open(const char *filename, int flags) = 0;
|
||||
|
||||
/** Remove a file from the filesystem.
|
||||
*
|
||||
* @param filename the name of the file to remove.
|
||||
* @param returns 0 on success, -1 on failure.
|
||||
*/
|
||||
virtual int remove(const char *filename) { (void) filename; return -1; };
|
||||
|
||||
/** Rename a file in the filesystem.
|
||||
*
|
||||
* @param oldname the name of the file to rename.
|
||||
* @param newname the name to rename it to.
|
||||
*
|
||||
* @returns
|
||||
* 0 on success,
|
||||
* -1 on failure.
|
||||
*/
|
||||
virtual int rename(const char *oldname, const char *newname) { (void) oldname, (void) newname; return -1; };
|
||||
|
||||
/** Opens a directory in the filesystem and returns a DirHandle
|
||||
* representing the directory stream.
|
||||
*
|
||||
* @param name The name of the directory to open.
|
||||
*
|
||||
* @returns
|
||||
* A DirHandle representing the directory stream, or
|
||||
* NULL on failure.
|
||||
*/
|
||||
virtual DirHandle *opendir(const char *name) { (void) name; return NULL; };
|
||||
|
||||
/** Creates a directory in the filesystem.
|
||||
*
|
||||
* @param name The name of the directory to create.
|
||||
* @param mode The permissions to create the directory with.
|
||||
*
|
||||
* @returns
|
||||
* 0 on success,
|
||||
* -1 on failure.
|
||||
*/
|
||||
virtual int mkdir(const char *name, mode_t mode) { (void) name, (void) mode; return -1; }
|
||||
|
||||
/** Store information about file in stat structure
|
||||
*
|
||||
* @param name The name of the file to find information about
|
||||
* @param st The stat buffer to write to
|
||||
* @returns
|
||||
* 0 on success or un-needed,
|
||||
* -1 on error
|
||||
*/
|
||||
virtual int stat(const char *name, struct stat *st) { return -1; };
|
||||
};
|
||||
|
||||
} // namespace mbed
|
||||
|
||||
#endif
|
||||
|
||||
/** @}*/
|
||||
138
mbed/drivers/FlashIAP.h
Normal file
138
mbed/drivers/FlashIAP.h
Normal file
@@ -0,0 +1,138 @@
|
||||
/* mbed Microcontroller Library
|
||||
* Copyright (c) 2017 ARM Limited
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
#ifndef MBED_FLASHIAP_H
|
||||
#define MBED_FLASHIAP_H
|
||||
|
||||
#ifdef DEVICE_FLASH
|
||||
|
||||
#include "flash_api.h"
|
||||
#include "platform/SingletonPtr.h"
|
||||
#include "platform/PlatformMutex.h"
|
||||
|
||||
namespace mbed {
|
||||
|
||||
/** \addtogroup drivers */
|
||||
/** @{*/
|
||||
|
||||
/** Flash IAP driver. It invokes flash HAL functions.
|
||||
*
|
||||
* Note Synchronization level: Thread safe
|
||||
*/
|
||||
class FlashIAP {
|
||||
public:
|
||||
FlashIAP();
|
||||
~FlashIAP();
|
||||
|
||||
/** Initialize a flash IAP device
|
||||
*
|
||||
* Should be called once per lifetime of the object.
|
||||
* @return 0 on success or a negative error code on failure
|
||||
*/
|
||||
int init();
|
||||
|
||||
/** Deinitialize a flash IAP device
|
||||
*
|
||||
* @return 0 on success or a negative error code on failure
|
||||
*/
|
||||
int deinit();
|
||||
|
||||
/** Read data from a flash device.
|
||||
*
|
||||
* This method invokes memcpy - reads number of bytes from the address
|
||||
*
|
||||
* @param buffer Buffer to write to
|
||||
* @param addr Flash address to begin reading from
|
||||
* @param size Size to read in bytes
|
||||
* @return 0 on success, negative error code on failure
|
||||
*/
|
||||
int read(void *buffer, uint32_t addr, uint32_t size);
|
||||
|
||||
/** Program data to pages
|
||||
*
|
||||
* The sectors must have been erased prior to being programmed
|
||||
*
|
||||
* @param buffer Buffer of data to be written
|
||||
* @param addr Address of a page to begin writing to, must be a multiple of program and sector sizes
|
||||
* @param size Size to write in bytes, must be a multiple of program and sector sizes
|
||||
* @return 0 on success, negative error code on failure
|
||||
*/
|
||||
int program(const void *buffer, uint32_t addr, uint32_t size);
|
||||
|
||||
/** Erase sectors
|
||||
*
|
||||
* The state of an erased sector is undefined until it has been programmed
|
||||
*
|
||||
* @param addr Address of a sector to begin erasing, must be a multiple of the sector size
|
||||
* @param size Size to erase in bytes, must be a multiple of the sector size
|
||||
* @return 0 on success, negative error code on failure
|
||||
*/
|
||||
int erase(uint32_t addr, uint32_t size);
|
||||
|
||||
/** Get the sector size at the defined address
|
||||
*
|
||||
* Sector size might differ at address ranges.
|
||||
* An example <0-0x1000, sector size=1024; 0x10000-0x20000, size=2048>
|
||||
*
|
||||
* @param addr Address of or inside the sector to query
|
||||
* @return Size of a sector in bytes or MBED_FLASH_INVALID_SIZE if not mapped
|
||||
*/
|
||||
uint32_t get_sector_size(uint32_t addr) const;
|
||||
|
||||
/** Get the flash start address
|
||||
*
|
||||
* @return Flash start address
|
||||
*/
|
||||
uint32_t get_flash_start() const;
|
||||
|
||||
/** Get the flash size
|
||||
*
|
||||
* @return Flash size
|
||||
*/
|
||||
uint32_t get_flash_size() const;
|
||||
|
||||
/** Get the program page size
|
||||
*
|
||||
* @return Size of a program page in bytes
|
||||
*/
|
||||
uint32_t get_page_size() const;
|
||||
|
||||
private:
|
||||
|
||||
/** Check if address and size are aligned to a sector
|
||||
*
|
||||
* @param addr Address of block to check for alignment
|
||||
* @param size Size of block to check for alignment
|
||||
* @return true if the block is sector aligned, false otherwise
|
||||
*/
|
||||
bool is_aligned_to_sector(uint32_t addr, uint32_t size);
|
||||
|
||||
flash_t _flash;
|
||||
static SingletonPtr<PlatformMutex> _mutex;
|
||||
};
|
||||
|
||||
} /* namespace mbed */
|
||||
|
||||
#endif /* DEVICE_FLASH */
|
||||
|
||||
#endif /* MBED_FLASHIAP_H */
|
||||
|
||||
/** @}*/
|
||||
198
mbed/drivers/I2C.h
Normal file
198
mbed/drivers/I2C.h
Normal file
@@ -0,0 +1,198 @@
|
||||
/* mbed Microcontroller Library
|
||||
* Copyright (c) 2006-2015 ARM Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#ifndef MBED_I2C_H
|
||||
#define MBED_I2C_H
|
||||
|
||||
#include "platform/platform.h"
|
||||
|
||||
#if DEVICE_I2C
|
||||
|
||||
#include "hal/i2c_api.h"
|
||||
#include "platform/SingletonPtr.h"
|
||||
#include "platform/PlatformMutex.h"
|
||||
|
||||
#if DEVICE_I2C_ASYNCH
|
||||
#include "platform/CThunk.h"
|
||||
#include "hal/dma_api.h"
|
||||
#include "platform/FunctionPointer.h"
|
||||
#endif
|
||||
|
||||
namespace mbed {
|
||||
/** \addtogroup drivers */
|
||||
/** @{*/
|
||||
|
||||
/** An I2C Master, used for communicating with I2C slave devices
|
||||
*
|
||||
* @Note Synchronization level: Thread safe
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* // Read from I2C slave at address 0x62
|
||||
*
|
||||
* #include "mbed.h"
|
||||
*
|
||||
* I2C i2c(p28, p27);
|
||||
*
|
||||
* int main() {
|
||||
* int address = 0x62;
|
||||
* char data[2];
|
||||
* i2c.read(address, data, 2);
|
||||
* }
|
||||
* @endcode
|
||||
*/
|
||||
class I2C {
|
||||
|
||||
public:
|
||||
enum RxStatus {
|
||||
NoData,
|
||||
MasterGeneralCall,
|
||||
MasterWrite,
|
||||
MasterRead
|
||||
};
|
||||
|
||||
enum Acknowledge {
|
||||
NoACK = 0,
|
||||
ACK = 1
|
||||
};
|
||||
|
||||
/** Create an I2C Master interface, connected to the specified pins
|
||||
*
|
||||
* @param sda I2C data line pin
|
||||
* @param scl I2C clock line pin
|
||||
*/
|
||||
I2C(PinName sda, PinName scl);
|
||||
|
||||
/** Set the frequency of the I2C interface
|
||||
*
|
||||
* @param hz The bus frequency in hertz
|
||||
*/
|
||||
void frequency(int hz);
|
||||
|
||||
/** Read from an I2C slave
|
||||
*
|
||||
* Performs a complete read transaction. The bottom bit of
|
||||
* the address is forced to 1 to indicate a read.
|
||||
*
|
||||
* @param address 8-bit I2C slave address [ addr | 1 ]
|
||||
* @param data Pointer to the byte-array to read data in to
|
||||
* @param length Number of bytes to read
|
||||
* @param repeated Repeated start, true - don't send stop at end
|
||||
*
|
||||
* @returns
|
||||
* 0 on success (ack),
|
||||
* non-0 on failure (nack)
|
||||
*/
|
||||
int read(int address, char *data, int length, bool repeated = false);
|
||||
|
||||
/** Read a single byte from the I2C bus
|
||||
*
|
||||
* @param ack indicates if the byte is to be acknowledged (1 = acknowledge)
|
||||
*
|
||||
* @returns
|
||||
* the byte read
|
||||
*/
|
||||
int read(int ack);
|
||||
|
||||
/** Write to an I2C slave
|
||||
*
|
||||
* Performs a complete write transaction. The bottom bit of
|
||||
* the address is forced to 0 to indicate a write.
|
||||
*
|
||||
* @param address 8-bit I2C slave address [ addr | 0 ]
|
||||
* @param data Pointer to the byte-array data to send
|
||||
* @param length Number of bytes to send
|
||||
* @param repeated Repeated start, true - do not send stop at end
|
||||
*
|
||||
* @returns
|
||||
* 0 on success (ack),
|
||||
* non-0 on failure (nack)
|
||||
*/
|
||||
int write(int address, const char *data, int length, bool repeated = false);
|
||||
|
||||
/** Write single byte out on the I2C bus
|
||||
*
|
||||
* @param data data to write out on bus
|
||||
*
|
||||
* @returns
|
||||
* '0' - NAK was received
|
||||
* '1' - ACK was received,
|
||||
* '2' - timeout
|
||||
*/
|
||||
int write(int data);
|
||||
|
||||
/** Creates a start condition on the I2C bus
|
||||
*/
|
||||
|
||||
void start(void);
|
||||
|
||||
/** Creates a stop condition on the I2C bus
|
||||
*/
|
||||
void stop(void);
|
||||
|
||||
/** Acquire exclusive access to this I2C bus
|
||||
*/
|
||||
virtual void lock(void);
|
||||
|
||||
/** Release exclusive access to this I2C bus
|
||||
*/
|
||||
virtual void unlock(void);
|
||||
|
||||
virtual ~I2C() {
|
||||
// Do nothing
|
||||
}
|
||||
|
||||
#if DEVICE_I2C_ASYNCH
|
||||
|
||||
/** Start non-blocking I2C transfer.
|
||||
*
|
||||
* @param address 8/10 bit I2c slave address
|
||||
* @param tx_buffer The TX buffer with data to be transfered
|
||||
* @param tx_length The length of TX buffer in bytes
|
||||
* @param rx_buffer The RX buffer which is used for received data
|
||||
* @param rx_length The length of RX buffer in bytes
|
||||
* @param event The logical OR of events to modify
|
||||
* @param callback The event callback function
|
||||
* @param repeated Repeated start, true - do not send stop at end
|
||||
* @return Zero if the transfer has started, or -1 if I2C peripheral is busy
|
||||
*/
|
||||
int transfer(int address, const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length, const event_callback_t& callback, int event = I2C_EVENT_TRANSFER_COMPLETE, bool repeated = false);
|
||||
|
||||
/** Abort the on-going I2C transfer
|
||||
*/
|
||||
void abort_transfer();
|
||||
protected:
|
||||
void irq_handler_asynch(void);
|
||||
event_callback_t _callback;
|
||||
CThunk<I2C> _irq;
|
||||
DMAUsage _usage;
|
||||
#endif
|
||||
|
||||
protected:
|
||||
void aquire();
|
||||
|
||||
i2c_t _i2c;
|
||||
static I2C *_owner;
|
||||
int _hz;
|
||||
static SingletonPtr<PlatformMutex> _mutex;
|
||||
};
|
||||
|
||||
} // namespace mbed
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
/** @}*/
|
||||
160
mbed/drivers/I2CSlave.h
Normal file
160
mbed/drivers/I2CSlave.h
Normal file
@@ -0,0 +1,160 @@
|
||||
/* mbed Microcontroller Library
|
||||
* Copyright (c) 2006-2013 ARM Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#ifndef MBED_I2C_SLAVE_H
|
||||
#define MBED_I2C_SLAVE_H
|
||||
|
||||
#include "platform/platform.h"
|
||||
|
||||
#if DEVICE_I2CSLAVE
|
||||
|
||||
#include "hal/i2c_api.h"
|
||||
|
||||
namespace mbed {
|
||||
/** \addtogroup drivers */
|
||||
/** @{*/
|
||||
|
||||
/** An I2C Slave, used for communicating with an I2C Master device
|
||||
*
|
||||
* @Note Synchronization level: Not protected
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* // Simple I2C responder
|
||||
* #include <mbed.h>
|
||||
*
|
||||
* I2CSlave slave(p9, p10);
|
||||
*
|
||||
* int main() {
|
||||
* char buf[10];
|
||||
* char msg[] = "Slave!";
|
||||
*
|
||||
* slave.address(0xA0);
|
||||
* while (1) {
|
||||
* int i = slave.receive();
|
||||
* switch (i) {
|
||||
* case I2CSlave::ReadAddressed:
|
||||
* slave.write(msg, strlen(msg) + 1); // Includes null char
|
||||
* break;
|
||||
* case I2CSlave::WriteGeneral:
|
||||
* slave.read(buf, 10);
|
||||
* printf("Read G: %s\n", buf);
|
||||
* break;
|
||||
* case I2CSlave::WriteAddressed:
|
||||
* slave.read(buf, 10);
|
||||
* printf("Read A: %s\n", buf);
|
||||
* break;
|
||||
* }
|
||||
* for(int i = 0; i < 10; i++) buf[i] = 0; // Clear buffer
|
||||
* }
|
||||
* }
|
||||
* @endcode
|
||||
*/
|
||||
class I2CSlave {
|
||||
|
||||
public:
|
||||
enum RxStatus {
|
||||
NoData = 0,
|
||||
ReadAddressed = 1,
|
||||
WriteGeneral = 2,
|
||||
WriteAddressed = 3
|
||||
};
|
||||
|
||||
/** Create an I2C Slave interface, connected to the specified pins.
|
||||
*
|
||||
* @param sda I2C data line pin
|
||||
* @param scl I2C clock line pin
|
||||
*/
|
||||
I2CSlave(PinName sda, PinName scl);
|
||||
|
||||
/** Set the frequency of the I2C interface
|
||||
*
|
||||
* @param hz The bus frequency in hertz
|
||||
*/
|
||||
void frequency(int hz);
|
||||
|
||||
/** Checks to see if this I2C Slave has been addressed.
|
||||
*
|
||||
* @returns
|
||||
* A status indicating if the device has been addressed, and how
|
||||
* - NoData - the slave has not been addressed
|
||||
* - ReadAddressed - the master has requested a read from this slave
|
||||
* - WriteAddressed - the master is writing to this slave
|
||||
* - WriteGeneral - the master is writing to all slave
|
||||
*/
|
||||
int receive(void);
|
||||
|
||||
/** Read from an I2C master.
|
||||
*
|
||||
* @param data pointer to the byte array to read data in to
|
||||
* @param length maximum number of bytes to read
|
||||
*
|
||||
* @returns
|
||||
* 0 on success,
|
||||
* non-0 otherwise
|
||||
*/
|
||||
int read(char *data, int length);
|
||||
|
||||
/** Read a single byte from an I2C master.
|
||||
*
|
||||
* @returns
|
||||
* the byte read
|
||||
*/
|
||||
int read(void);
|
||||
|
||||
/** Write to an I2C master.
|
||||
*
|
||||
* @param data pointer to the byte array to be transmitted
|
||||
* @param length the number of bytes to transmite
|
||||
*
|
||||
* @returns
|
||||
* 0 on success,
|
||||
* non-0 otherwise
|
||||
*/
|
||||
int write(const char *data, int length);
|
||||
|
||||
/** Write a single byte to an I2C master.
|
||||
*
|
||||
* @data the byte to write
|
||||
*
|
||||
* @returns
|
||||
* '1' if an ACK was received,
|
||||
* '0' otherwise
|
||||
*/
|
||||
int write(int data);
|
||||
|
||||
/** Sets the I2C slave address.
|
||||
*
|
||||
* @param address The address to set for the slave (ignoring the least
|
||||
* signifcant bit). If set to 0, the slave will only respond to the
|
||||
* general call address.
|
||||
*/
|
||||
void address(int address);
|
||||
|
||||
/** Reset the I2C slave back into the known ready receiving state.
|
||||
*/
|
||||
void stop(void);
|
||||
|
||||
protected:
|
||||
i2c_t _i2c;
|
||||
};
|
||||
|
||||
} // namespace mbed
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
/** @}*/
|
||||
164
mbed/drivers/InterruptIn.h
Normal file
164
mbed/drivers/InterruptIn.h
Normal file
@@ -0,0 +1,164 @@
|
||||
/* mbed Microcontroller Library
|
||||
* Copyright (c) 2006-2013 ARM Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#ifndef MBED_INTERRUPTIN_H
|
||||
#define MBED_INTERRUPTIN_H
|
||||
|
||||
#include "platform/platform.h"
|
||||
|
||||
#if DEVICE_INTERRUPTIN
|
||||
|
||||
#include "hal/gpio_api.h"
|
||||
#include "hal/gpio_irq_api.h"
|
||||
#include "platform/Callback.h"
|
||||
#include "platform/mbed_critical.h"
|
||||
#include "platform/mbed_toolchain.h"
|
||||
|
||||
namespace mbed {
|
||||
/** \addtogroup drivers */
|
||||
/** @{*/
|
||||
|
||||
/** A digital interrupt input, used to call a function on a rising or falling edge
|
||||
*
|
||||
* @Note Synchronization level: Interrupt safe
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* // Flash an LED while waiting for events
|
||||
*
|
||||
* #include "mbed.h"
|
||||
*
|
||||
* InterruptIn event(p16);
|
||||
* DigitalOut led(LED1);
|
||||
*
|
||||
* void trigger() {
|
||||
* printf("triggered!\n");
|
||||
* }
|
||||
*
|
||||
* int main() {
|
||||
* event.rise(&trigger);
|
||||
* while(1) {
|
||||
* led = !led;
|
||||
* wait(0.25);
|
||||
* }
|
||||
* }
|
||||
* @endcode
|
||||
*/
|
||||
class InterruptIn {
|
||||
|
||||
public:
|
||||
|
||||
/** Create an InterruptIn connected to the specified pin
|
||||
*
|
||||
* @param pin InterruptIn pin to connect to
|
||||
* @param name (optional) A string to identify the object
|
||||
*/
|
||||
InterruptIn(PinName pin);
|
||||
virtual ~InterruptIn();
|
||||
|
||||
/** Read the input, represented as 0 or 1 (int)
|
||||
*
|
||||
* @returns
|
||||
* An integer representing the state of the input pin,
|
||||
* 0 for logical 0, 1 for logical 1
|
||||
*/
|
||||
int read();
|
||||
|
||||
/** An operator shorthand for read()
|
||||
*/
|
||||
operator int();
|
||||
|
||||
|
||||
/** Attach a function to call when a rising edge occurs on the input
|
||||
*
|
||||
* @param func A pointer to a void function, or 0 to set as none
|
||||
*/
|
||||
void rise(Callback<void()> func);
|
||||
|
||||
/** Attach a member function to call when a rising edge occurs on the input
|
||||
*
|
||||
* @param obj pointer to the object to call the member function on
|
||||
* @param method pointer to the member function to be called
|
||||
* @deprecated
|
||||
* The rise function does not support cv-qualifiers. Replaced by
|
||||
* rise(callback(obj, method)).
|
||||
*/
|
||||
template<typename T, typename M>
|
||||
MBED_DEPRECATED_SINCE("mbed-os-5.1",
|
||||
"The rise function does not support cv-qualifiers. Replaced by "
|
||||
"rise(callback(obj, method)).")
|
||||
void rise(T *obj, M method) {
|
||||
core_util_critical_section_enter();
|
||||
rise(callback(obj, method));
|
||||
core_util_critical_section_exit();
|
||||
}
|
||||
|
||||
/** Attach a function to call when a falling edge occurs on the input
|
||||
*
|
||||
* @param func A pointer to a void function, or 0 to set as none
|
||||
*/
|
||||
void fall(Callback<void()> func);
|
||||
|
||||
/** Attach a member function to call when a falling edge occurs on the input
|
||||
*
|
||||
* @param obj pointer to the object to call the member function on
|
||||
* @param method pointer to the member function to be called
|
||||
* @deprecated
|
||||
* The rise function does not support cv-qualifiers. Replaced by
|
||||
* rise(callback(obj, method)).
|
||||
*/
|
||||
template<typename T, typename M>
|
||||
MBED_DEPRECATED_SINCE("mbed-os-5.1",
|
||||
"The fall function does not support cv-qualifiers. Replaced by "
|
||||
"fall(callback(obj, method)).")
|
||||
void fall(T *obj, M method) {
|
||||
core_util_critical_section_enter();
|
||||
fall(callback(obj, method));
|
||||
core_util_critical_section_exit();
|
||||
}
|
||||
|
||||
/** Set the input pin mode
|
||||
*
|
||||
* @param mode PullUp, PullDown, PullNone
|
||||
*/
|
||||
void mode(PinMode pull);
|
||||
|
||||
/** Enable IRQ. This method depends on hw implementation, might enable one
|
||||
* port interrupts. For further information, check gpio_irq_enable().
|
||||
*/
|
||||
void enable_irq();
|
||||
|
||||
/** Disable IRQ. This method depends on hw implementation, might disable one
|
||||
* port interrupts. For further information, check gpio_irq_disable().
|
||||
*/
|
||||
void disable_irq();
|
||||
|
||||
static void _irq_handler(uint32_t id, gpio_irq_event event);
|
||||
|
||||
protected:
|
||||
gpio_t gpio;
|
||||
gpio_irq_t gpio_irq;
|
||||
|
||||
Callback<void()> _rise;
|
||||
Callback<void()> _fall;
|
||||
};
|
||||
|
||||
} // namespace mbed
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
/** @}*/
|
||||
175
mbed/drivers/InterruptManager.h
Normal file
175
mbed/drivers/InterruptManager.h
Normal file
@@ -0,0 +1,175 @@
|
||||
/* mbed Microcontroller Library
|
||||
* Copyright (c) 2006-2013 ARM Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#ifndef MBED_INTERRUPTMANAGER_H
|
||||
#define MBED_INTERRUPTMANAGER_H
|
||||
|
||||
#include "cmsis.h"
|
||||
#include "platform/CallChain.h"
|
||||
#include "platform/PlatformMutex.h"
|
||||
#include <string.h>
|
||||
|
||||
namespace mbed {
|
||||
/** \addtogroup drivers */
|
||||
/** @{*/
|
||||
|
||||
/** Use this singleton if you need to chain interrupt handlers.
|
||||
*
|
||||
* @Note Synchronization level: Thread safe
|
||||
*
|
||||
* Example (for LPC1768):
|
||||
* @code
|
||||
* #include "InterruptManager.h"
|
||||
* #include "mbed.h"
|
||||
*
|
||||
* Ticker flipper;
|
||||
* DigitalOut led1(LED1);
|
||||
* DigitalOut led2(LED2);
|
||||
*
|
||||
* void flip(void) {
|
||||
* led1 = !led1;
|
||||
* }
|
||||
*
|
||||
* void handler(void) {
|
||||
* led2 = !led1;
|
||||
* }
|
||||
*
|
||||
* int main() {
|
||||
* led1 = led2 = 0;
|
||||
* flipper.attach(&flip, 1.0);
|
||||
* InterruptManager::get()->add_handler(handler, TIMER3_IRQn);
|
||||
* }
|
||||
* @endcode
|
||||
*/
|
||||
class InterruptManager {
|
||||
public:
|
||||
/** Return the only instance of this class
|
||||
*/
|
||||
static InterruptManager* get();
|
||||
|
||||
/** Destroy the current instance of the interrupt manager
|
||||
*/
|
||||
static void destroy();
|
||||
|
||||
/** Add a handler for an interrupt at the end of the handler list
|
||||
*
|
||||
* @param function the handler to add
|
||||
* @param irq interrupt number
|
||||
*
|
||||
* @returns
|
||||
* The function object created for 'function'
|
||||
*/
|
||||
pFunctionPointer_t add_handler(void (*function)(void), IRQn_Type irq) {
|
||||
// Underlying call is thread safe
|
||||
return add_common(function, irq);
|
||||
}
|
||||
|
||||
/** Add a handler for an interrupt at the beginning of the handler list
|
||||
*
|
||||
* @param function the handler to add
|
||||
* @param irq interrupt number
|
||||
*
|
||||
* @returns
|
||||
* The function object created for 'function'
|
||||
*/
|
||||
pFunctionPointer_t add_handler_front(void (*function)(void), IRQn_Type irq) {
|
||||
// Underlying call is thread safe
|
||||
return add_common(function, irq, true);
|
||||
}
|
||||
|
||||
/** Add a handler for an interrupt at the end of the handler list
|
||||
*
|
||||
* @param tptr pointer to the object that has the handler function
|
||||
* @param mptr pointer to the actual handler function
|
||||
* @param irq interrupt number
|
||||
*
|
||||
* @returns
|
||||
* The function object created for 'tptr' and 'mptr'
|
||||
*/
|
||||
template<typename T>
|
||||
pFunctionPointer_t add_handler(T* tptr, void (T::*mptr)(void), IRQn_Type irq) {
|
||||
// Underlying call is thread safe
|
||||
return add_common(tptr, mptr, irq);
|
||||
}
|
||||
|
||||
/** Add a handler for an interrupt at the beginning of the handler list
|
||||
*
|
||||
* @param tptr pointer to the object that has the handler function
|
||||
* @param mptr pointer to the actual handler function
|
||||
* @param irq interrupt number
|
||||
*
|
||||
* @returns
|
||||
* The function object created for 'tptr' and 'mptr'
|
||||
*/
|
||||
template<typename T>
|
||||
pFunctionPointer_t add_handler_front(T* tptr, void (T::*mptr)(void), IRQn_Type irq) {
|
||||
// Underlying call is thread safe
|
||||
return add_common(tptr, mptr, irq, true);
|
||||
}
|
||||
|
||||
/** Remove a handler from an interrupt
|
||||
*
|
||||
* @param handler the function object for the handler to remove
|
||||
* @param irq the interrupt number
|
||||
*
|
||||
* @returns
|
||||
* true if the handler was found and removed, false otherwise
|
||||
*/
|
||||
bool remove_handler(pFunctionPointer_t handler, IRQn_Type irq);
|
||||
|
||||
private:
|
||||
InterruptManager();
|
||||
~InterruptManager();
|
||||
|
||||
void lock();
|
||||
void unlock();
|
||||
|
||||
// We declare the copy contructor and the assignment operator, but we don't
|
||||
// implement them. This way, if someone tries to copy/assign our instance,
|
||||
// he will get an error at compile time.
|
||||
InterruptManager(const InterruptManager&);
|
||||
InterruptManager& operator =(const InterruptManager&);
|
||||
|
||||
template<typename T>
|
||||
pFunctionPointer_t add_common(T *tptr, void (T::*mptr)(void), IRQn_Type irq, bool front=false) {
|
||||
_mutex.lock();
|
||||
int irq_pos = get_irq_index(irq);
|
||||
bool change = must_replace_vector(irq);
|
||||
|
||||
pFunctionPointer_t pf = front ? _chains[irq_pos]->add_front(tptr, mptr) : _chains[irq_pos]->add(tptr, mptr);
|
||||
if (change)
|
||||
NVIC_SetVector(irq, (uint32_t)&InterruptManager::static_irq_helper);
|
||||
_mutex.unlock();
|
||||
return pf;
|
||||
}
|
||||
|
||||
pFunctionPointer_t add_common(void (*function)(void), IRQn_Type irq, bool front=false);
|
||||
bool must_replace_vector(IRQn_Type irq);
|
||||
int get_irq_index(IRQn_Type irq);
|
||||
void irq_helper();
|
||||
void add_helper(void (*function)(void), IRQn_Type irq, bool front=false);
|
||||
static void static_irq_helper();
|
||||
|
||||
CallChain* _chains[NVIC_NUM_VECTORS];
|
||||
static InterruptManager* _instance;
|
||||
PlatformMutex _mutex;
|
||||
};
|
||||
|
||||
} // namespace mbed
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
/** @}*/
|
||||
114
mbed/drivers/LocalFileSystem.h
Normal file
114
mbed/drivers/LocalFileSystem.h
Normal file
@@ -0,0 +1,114 @@
|
||||
/* mbed Microcontroller Library
|
||||
* Copyright (c) 2006-2013 ARM Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#ifndef MBED_LOCALFILESYSTEM_H
|
||||
#define MBED_LOCALFILESYSTEM_H
|
||||
|
||||
#include "platform/platform.h"
|
||||
|
||||
#if DEVICE_LOCALFILESYSTEM
|
||||
|
||||
#include "drivers/FileSystemLike.h"
|
||||
#include "platform/PlatformMutex.h"
|
||||
|
||||
namespace mbed {
|
||||
/** \addtogroup drivers */
|
||||
/** @{*/
|
||||
|
||||
FILEHANDLE local_file_open(const char* name, int flags);
|
||||
|
||||
class LocalFileHandle : public FileHandle {
|
||||
|
||||
public:
|
||||
LocalFileHandle(FILEHANDLE fh);
|
||||
|
||||
virtual int close();
|
||||
|
||||
virtual ssize_t write(const void *buffer, size_t length);
|
||||
|
||||
virtual ssize_t read(void *buffer, size_t length);
|
||||
|
||||
virtual int isatty();
|
||||
|
||||
virtual off_t lseek(off_t position, int whence);
|
||||
|
||||
virtual int fsync();
|
||||
|
||||
virtual off_t flen();
|
||||
|
||||
protected:
|
||||
virtual void lock();
|
||||
virtual void unlock();
|
||||
FILEHANDLE _fh;
|
||||
int pos;
|
||||
PlatformMutex _mutex;
|
||||
};
|
||||
|
||||
/** A filesystem for accessing the local mbed Microcontroller USB disk drive
|
||||
*
|
||||
* This allows programs to read and write files on the same disk drive that is used to program the
|
||||
* mbed Microcontroller. Once created, the standard C file access functions are used to open,
|
||||
* read and write files.
|
||||
*
|
||||
* @Note Synchronization level: Thread safe
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* #include "mbed.h"
|
||||
*
|
||||
* LocalFileSystem local("local"); // Create the local filesystem under the name "local"
|
||||
*
|
||||
* int main() {
|
||||
* FILE *fp = fopen("/local/out.txt", "w"); // Open "out.txt" on the local file system for writing
|
||||
* fprintf(fp, "Hello World!");
|
||||
* fclose(fp);
|
||||
* remove("/local/out.txt"); // Removes the file "out.txt" from the local file system
|
||||
*
|
||||
* DIR *d = opendir("/local"); // Opens the root directory of the local file system
|
||||
* struct dirent *p;
|
||||
* while((p = readdir(d)) != NULL) { // Print the names of the files in the local file system
|
||||
* printf("%s\n", p->d_name); // to stdout.
|
||||
* }
|
||||
* closedir(d);
|
||||
* }
|
||||
* @endcode
|
||||
*
|
||||
* @note
|
||||
* If the microcontroller program makes an access to the local drive, it will be marked as "removed"
|
||||
* on the Host computer. This means it is no longer accessible from the Host Computer.
|
||||
*
|
||||
* The drive will only re-appear when the microcontroller program exists. Note that if the program does
|
||||
* not exit, you will need to hold down reset on the mbed Microcontroller to be able to see the drive again!
|
||||
*/
|
||||
class LocalFileSystem : public FileSystemLike {
|
||||
// No modifiable state
|
||||
|
||||
public:
|
||||
LocalFileSystem(const char* n) : FileSystemLike(n) {
|
||||
|
||||
}
|
||||
|
||||
virtual FileHandle *open(const char* name, int flags);
|
||||
virtual int remove(const char *filename);
|
||||
virtual DirHandle *opendir(const char *name);
|
||||
};
|
||||
|
||||
} // namespace mbed
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
/** @}*/
|
||||
50
mbed/drivers/LowPowerTicker.h
Normal file
50
mbed/drivers/LowPowerTicker.h
Normal file
@@ -0,0 +1,50 @@
|
||||
/* mbed Microcontroller Library
|
||||
* Copyright (c) 2015 ARM Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#ifndef MBED_LOWPOWERTICKER_H
|
||||
#define MBED_LOWPOWERTICKER_H
|
||||
|
||||
#include "platform/platform.h"
|
||||
#include "drivers/Ticker.h"
|
||||
|
||||
#if DEVICE_LOWPOWERTIMER
|
||||
|
||||
#include "hal/lp_ticker_api.h"
|
||||
|
||||
namespace mbed {
|
||||
/** \addtogroup drivers */
|
||||
/** @{*/
|
||||
|
||||
/** Low Power Ticker
|
||||
*
|
||||
* @Note Synchronization level: Interrupt safe
|
||||
*/
|
||||
class LowPowerTicker : public Ticker {
|
||||
|
||||
public:
|
||||
LowPowerTicker() : Ticker(get_lp_ticker_data()) {
|
||||
}
|
||||
|
||||
virtual ~LowPowerTicker() {
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace mbed
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
/** @}*/
|
||||
48
mbed/drivers/LowPowerTimeout.h
Normal file
48
mbed/drivers/LowPowerTimeout.h
Normal file
@@ -0,0 +1,48 @@
|
||||
/* mbed Microcontroller Library
|
||||
* Copyright (c) 2015 ARM Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#ifndef MBED_LOWPOWERTIMEOUT_H
|
||||
#define MBED_LOWPOWERTIMEOUT_H
|
||||
|
||||
#include "platform/platform.h"
|
||||
|
||||
#if DEVICE_LOWPOWERTIMER
|
||||
|
||||
#include "hal/lp_ticker_api.h"
|
||||
#include "drivers/LowPowerTicker.h"
|
||||
|
||||
namespace mbed {
|
||||
/** \addtogroup drivers */
|
||||
/** @{*/
|
||||
|
||||
/** Low Power Timout
|
||||
*
|
||||
* @Note Synchronization level: Interrupt safe
|
||||
*/
|
||||
class LowPowerTimeout : public LowPowerTicker {
|
||||
|
||||
private:
|
||||
virtual void handler(void) {
|
||||
_function.call();
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
/** @}*/
|
||||
48
mbed/drivers/LowPowerTimer.h
Normal file
48
mbed/drivers/LowPowerTimer.h
Normal file
@@ -0,0 +1,48 @@
|
||||
/* mbed Microcontroller Library
|
||||
* Copyright (c) 2015 ARM Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#ifndef MBED_LOWPOWERTIMER_H
|
||||
#define MBED_LOWPOWERTIMER_H
|
||||
|
||||
#include "platform/platform.h"
|
||||
#include "drivers/Timer.h"
|
||||
|
||||
#if DEVICE_LOWPOWERTIMER
|
||||
|
||||
#include "hal/lp_ticker_api.h"
|
||||
|
||||
namespace mbed {
|
||||
/** \addtogroup drivers */
|
||||
/** @{*/
|
||||
|
||||
/** Low power timer
|
||||
*
|
||||
* @Note Synchronization level: Interrupt safe
|
||||
*/
|
||||
class LowPowerTimer : public Timer {
|
||||
|
||||
public:
|
||||
LowPowerTimer() : Timer(get_lp_ticker_data()) {
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
} // namespace mbed
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
/** @}*/
|
||||
104
mbed/drivers/PortIn.h
Normal file
104
mbed/drivers/PortIn.h
Normal file
@@ -0,0 +1,104 @@
|
||||
/* mbed Microcontroller Library
|
||||
* Copyright (c) 2006-2013 ARM Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#ifndef MBED_PORTIN_H
|
||||
#define MBED_PORTIN_H
|
||||
|
||||
#include "platform/platform.h"
|
||||
|
||||
#if DEVICE_PORTIN
|
||||
|
||||
#include "hal/port_api.h"
|
||||
#include "platform/mbed_critical.h"
|
||||
|
||||
namespace mbed {
|
||||
/** \addtogroup drivers */
|
||||
/** @{*/
|
||||
|
||||
/** A multiple pin digital input
|
||||
*
|
||||
* @Note Synchronization level: Interrupt safe
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* // Switch on an LED if any of mbed pins 21-26 is high
|
||||
*
|
||||
* #include "mbed.h"
|
||||
*
|
||||
* PortIn p(Port2, 0x0000003F); // p21-p26
|
||||
* DigitalOut ind(LED4);
|
||||
*
|
||||
* int main() {
|
||||
* while(1) {
|
||||
* int pins = p.read();
|
||||
* if(pins) {
|
||||
* ind = 1;
|
||||
* } else {
|
||||
* ind = 0;
|
||||
* }
|
||||
* }
|
||||
* }
|
||||
* @endcode
|
||||
*/
|
||||
class PortIn {
|
||||
public:
|
||||
|
||||
/** Create an PortIn, connected to the specified port
|
||||
*
|
||||
* @param port Port to connect to (Port0-Port5)
|
||||
* @param mask A bitmask to identify which bits in the port should be included (0 - ignore)
|
||||
*/
|
||||
PortIn(PortName port, int mask = 0xFFFFFFFF) {
|
||||
core_util_critical_section_enter();
|
||||
port_init(&_port, port, mask, PIN_INPUT);
|
||||
core_util_critical_section_exit();
|
||||
}
|
||||
|
||||
/** Read the value currently output on the port
|
||||
*
|
||||
* @returns
|
||||
* An integer with each bit corresponding to associated port pin setting
|
||||
*/
|
||||
int read() {
|
||||
return port_read(&_port);
|
||||
}
|
||||
|
||||
/** Set the input pin mode
|
||||
*
|
||||
* @param mode PullUp, PullDown, PullNone, OpenDrain
|
||||
*/
|
||||
void mode(PinMode mode) {
|
||||
core_util_critical_section_enter();
|
||||
port_mode(&_port, mode);
|
||||
core_util_critical_section_exit();
|
||||
}
|
||||
|
||||
/** A shorthand for read()
|
||||
*/
|
||||
operator int() {
|
||||
return read();
|
||||
}
|
||||
|
||||
private:
|
||||
port_t _port;
|
||||
};
|
||||
|
||||
} // namespace mbed
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
/** @}*/
|
||||
119
mbed/drivers/PortInOut.h
Normal file
119
mbed/drivers/PortInOut.h
Normal file
@@ -0,0 +1,119 @@
|
||||
/* mbed Microcontroller Library
|
||||
* Copyright (c) 2006-2013 ARM Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#ifndef MBED_PORTINOUT_H
|
||||
#define MBED_PORTINOUT_H
|
||||
|
||||
#include "platform/platform.h"
|
||||
|
||||
#if DEVICE_PORTINOUT
|
||||
|
||||
#include "hal/port_api.h"
|
||||
#include "platform/mbed_critical.h"
|
||||
|
||||
namespace mbed {
|
||||
/** \addtogroup drivers */
|
||||
/** @{*/
|
||||
|
||||
/** A multiple pin digital in/out used to set/read multiple bi-directional pins
|
||||
*
|
||||
* @Note Synchronization level: Interrupt safe
|
||||
*/
|
||||
class PortInOut {
|
||||
public:
|
||||
|
||||
/** Create an PortInOut, connected to the specified port
|
||||
*
|
||||
* @param port Port to connect to (Port0-Port5)
|
||||
* @param mask A bitmask to identify which bits in the port should be included (0 - ignore)
|
||||
*/
|
||||
PortInOut(PortName port, int mask = 0xFFFFFFFF) {
|
||||
core_util_critical_section_enter();
|
||||
port_init(&_port, port, mask, PIN_INPUT);
|
||||
core_util_critical_section_exit();
|
||||
}
|
||||
|
||||
/** Write the value to the output port
|
||||
*
|
||||
* @param value An integer specifying a bit to write for every corresponding port pin
|
||||
*/
|
||||
void write(int value) {
|
||||
port_write(&_port, value);
|
||||
}
|
||||
|
||||
/** Read the value currently output on the port
|
||||
*
|
||||
* @returns
|
||||
* An integer with each bit corresponding to associated port pin setting
|
||||
*/
|
||||
int read() {
|
||||
return port_read(&_port);
|
||||
}
|
||||
|
||||
/** Set as an output
|
||||
*/
|
||||
void output() {
|
||||
core_util_critical_section_enter();
|
||||
port_dir(&_port, PIN_OUTPUT);
|
||||
core_util_critical_section_exit();
|
||||
}
|
||||
|
||||
/** Set as an input
|
||||
*/
|
||||
void input() {
|
||||
core_util_critical_section_enter();
|
||||
port_dir(&_port, PIN_INPUT);
|
||||
core_util_critical_section_exit();
|
||||
}
|
||||
|
||||
/** Set the input pin mode
|
||||
*
|
||||
* @param mode PullUp, PullDown, PullNone, OpenDrain
|
||||
*/
|
||||
void mode(PinMode mode) {
|
||||
core_util_critical_section_enter();
|
||||
port_mode(&_port, mode);
|
||||
core_util_critical_section_exit();
|
||||
}
|
||||
|
||||
/** A shorthand for write()
|
||||
*/
|
||||
PortInOut& operator= (int value) {
|
||||
write(value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
PortInOut& operator= (PortInOut& rhs) {
|
||||
write(rhs.read());
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** A shorthand for read()
|
||||
*/
|
||||
operator int() {
|
||||
return read();
|
||||
}
|
||||
|
||||
private:
|
||||
port_t _port;
|
||||
};
|
||||
|
||||
} // namespace mbed
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
/** @}*/
|
||||
113
mbed/drivers/PortOut.h
Normal file
113
mbed/drivers/PortOut.h
Normal file
@@ -0,0 +1,113 @@
|
||||
/* mbed Microcontroller Library
|
||||
* Copyright (c) 2006-2013 ARM Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#ifndef MBED_PORTOUT_H
|
||||
#define MBED_PORTOUT_H
|
||||
|
||||
#include "platform/platform.h"
|
||||
|
||||
#if DEVICE_PORTOUT
|
||||
|
||||
#include "hal/port_api.h"
|
||||
#include "platform/mbed_critical.h"
|
||||
|
||||
namespace mbed {
|
||||
/** \addtogroup drivers */
|
||||
/** @{*/
|
||||
/** A multiple pin digital out
|
||||
*
|
||||
* @Note Synchronization level: Interrupt safe
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* // Toggle all four LEDs
|
||||
*
|
||||
* #include "mbed.h"
|
||||
*
|
||||
* // LED1 = P1.18 LED2 = P1.20 LED3 = P1.21 LED4 = P1.23
|
||||
* #define LED_MASK 0x00B40000
|
||||
*
|
||||
* PortOut ledport(Port1, LED_MASK);
|
||||
*
|
||||
* int main() {
|
||||
* while(1) {
|
||||
* ledport = LED_MASK;
|
||||
* wait(1);
|
||||
* ledport = 0;
|
||||
* wait(1);
|
||||
* }
|
||||
* }
|
||||
* @endcode
|
||||
*/
|
||||
class PortOut {
|
||||
public:
|
||||
|
||||
/** Create an PortOut, connected to the specified port
|
||||
*
|
||||
* @param port Port to connect to (Port0-Port5)
|
||||
* @param mask A bitmask to identify which bits in the port should be included (0 - ignore)
|
||||
*/
|
||||
PortOut(PortName port, int mask = 0xFFFFFFFF) {
|
||||
core_util_critical_section_enter();
|
||||
port_init(&_port, port, mask, PIN_OUTPUT);
|
||||
core_util_critical_section_exit();
|
||||
}
|
||||
|
||||
/** Write the value to the output port
|
||||
*
|
||||
* @param value An integer specifying a bit to write for every corresponding PortOut pin
|
||||
*/
|
||||
void write(int value) {
|
||||
port_write(&_port, value);
|
||||
}
|
||||
|
||||
/** Read the value currently output on the port
|
||||
*
|
||||
* @returns
|
||||
* An integer with each bit corresponding to associated PortOut pin setting
|
||||
*/
|
||||
int read() {
|
||||
return port_read(&_port);
|
||||
}
|
||||
|
||||
/** A shorthand for write()
|
||||
*/
|
||||
PortOut& operator= (int value) {
|
||||
write(value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
PortOut& operator= (PortOut& rhs) {
|
||||
write(rhs.read());
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** A shorthand for read()
|
||||
*/
|
||||
operator int() {
|
||||
return read();
|
||||
}
|
||||
|
||||
private:
|
||||
port_t _port;
|
||||
};
|
||||
|
||||
} // namespace mbed
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
/** @}*/
|
||||
185
mbed/drivers/PwmOut.h
Normal file
185
mbed/drivers/PwmOut.h
Normal file
@@ -0,0 +1,185 @@
|
||||
/* mbed Microcontroller Library
|
||||
* Copyright (c) 2006-2013 ARM Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#ifndef MBED_PWMOUT_H
|
||||
#define MBED_PWMOUT_H
|
||||
|
||||
#include "platform/platform.h"
|
||||
|
||||
#if DEVICE_PWMOUT
|
||||
#include "hal/pwmout_api.h"
|
||||
#include "platform/mbed_critical.h"
|
||||
|
||||
namespace mbed {
|
||||
/** \addtogroup drivers */
|
||||
/** @{*/
|
||||
|
||||
/** A pulse-width modulation digital output
|
||||
*
|
||||
* @Note Synchronization level: Interrupt safe
|
||||
*
|
||||
* Example
|
||||
* @code
|
||||
* // Fade a led on.
|
||||
* #include "mbed.h"
|
||||
*
|
||||
* PwmOut led(LED1);
|
||||
*
|
||||
* int main() {
|
||||
* while(1) {
|
||||
* led = led + 0.01;
|
||||
* wait(0.2);
|
||||
* if(led == 1.0) {
|
||||
* led = 0;
|
||||
* }
|
||||
* }
|
||||
* }
|
||||
* @endcode
|
||||
*
|
||||
* @note
|
||||
* On the LPC1768 and LPC2368, the PWMs all share the same
|
||||
* period - if you change the period for one, you change it for all.
|
||||
* Although routines that change the period maintain the duty cycle
|
||||
* for its PWM, all other PWMs will require their duty cycle to be
|
||||
* refreshed.
|
||||
*/
|
||||
class PwmOut {
|
||||
|
||||
public:
|
||||
|
||||
/** Create a PwmOut connected to the specified pin
|
||||
*
|
||||
* @param pin PwmOut pin to connect to
|
||||
*/
|
||||
PwmOut(PinName pin) {
|
||||
core_util_critical_section_enter();
|
||||
pwmout_init(&_pwm, pin);
|
||||
core_util_critical_section_exit();
|
||||
}
|
||||
|
||||
/** Set the ouput duty-cycle, specified as a percentage (float)
|
||||
*
|
||||
* @param value A floating-point value representing the output duty-cycle,
|
||||
* specified as a percentage. The value should lie between
|
||||
* 0.0f (representing on 0%) and 1.0f (representing on 100%).
|
||||
* Values outside this range will be saturated to 0.0f or 1.0f.
|
||||
*/
|
||||
void write(float value) {
|
||||
core_util_critical_section_enter();
|
||||
pwmout_write(&_pwm, value);
|
||||
core_util_critical_section_exit();
|
||||
}
|
||||
|
||||
/** Return the current output duty-cycle setting, measured as a percentage (float)
|
||||
*
|
||||
* @returns
|
||||
* A floating-point value representing the current duty-cycle being output on the pin,
|
||||
* measured as a percentage. The returned value will lie between
|
||||
* 0.0f (representing on 0%) and 1.0f (representing on 100%).
|
||||
*
|
||||
* @note
|
||||
* This value may not match exactly the value set by a previous <write>.
|
||||
*/
|
||||
float read() {
|
||||
core_util_critical_section_enter();
|
||||
float val = pwmout_read(&_pwm);
|
||||
core_util_critical_section_exit();
|
||||
return val;
|
||||
}
|
||||
|
||||
/** Set the PWM period, specified in seconds (float), keeping the duty cycle the same.
|
||||
*
|
||||
* @note
|
||||
* The resolution is currently in microseconds; periods smaller than this
|
||||
* will be set to zero.
|
||||
*/
|
||||
void period(float seconds) {
|
||||
core_util_critical_section_enter();
|
||||
pwmout_period(&_pwm, seconds);
|
||||
core_util_critical_section_exit();
|
||||
}
|
||||
|
||||
/** Set the PWM period, specified in milli-seconds (int), keeping the duty cycle the same.
|
||||
*/
|
||||
void period_ms(int ms) {
|
||||
core_util_critical_section_enter();
|
||||
pwmout_period_ms(&_pwm, ms);
|
||||
core_util_critical_section_exit();
|
||||
}
|
||||
|
||||
/** Set the PWM period, specified in micro-seconds (int), keeping the duty cycle the same.
|
||||
*/
|
||||
void period_us(int us) {
|
||||
core_util_critical_section_enter();
|
||||
pwmout_period_us(&_pwm, us);
|
||||
core_util_critical_section_exit();
|
||||
}
|
||||
|
||||
/** Set the PWM pulsewidth, specified in seconds (float), keeping the period the same.
|
||||
*/
|
||||
void pulsewidth(float seconds) {
|
||||
core_util_critical_section_enter();
|
||||
pwmout_pulsewidth(&_pwm, seconds);
|
||||
core_util_critical_section_exit();
|
||||
}
|
||||
|
||||
/** Set the PWM pulsewidth, specified in milli-seconds (int), keeping the period the same.
|
||||
*/
|
||||
void pulsewidth_ms(int ms) {
|
||||
core_util_critical_section_enter();
|
||||
pwmout_pulsewidth_ms(&_pwm, ms);
|
||||
core_util_critical_section_exit();
|
||||
}
|
||||
|
||||
/** Set the PWM pulsewidth, specified in micro-seconds (int), keeping the period the same.
|
||||
*/
|
||||
void pulsewidth_us(int us) {
|
||||
core_util_critical_section_enter();
|
||||
pwmout_pulsewidth_us(&_pwm, us);
|
||||
core_util_critical_section_exit();
|
||||
}
|
||||
|
||||
/** A operator shorthand for write()
|
||||
*/
|
||||
PwmOut& operator= (float value) {
|
||||
// Underlying call is thread safe
|
||||
write(value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
PwmOut& operator= (PwmOut& rhs) {
|
||||
// Underlying call is thread safe
|
||||
write(rhs.read());
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** An operator shorthand for read()
|
||||
*/
|
||||
operator float() {
|
||||
// Underlying call is thread safe
|
||||
return read();
|
||||
}
|
||||
|
||||
protected:
|
||||
pwmout_t _pwm;
|
||||
};
|
||||
|
||||
} // namespace mbed
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
/** @}*/
|
||||
107
mbed/drivers/RawSerial.h
Normal file
107
mbed/drivers/RawSerial.h
Normal file
@@ -0,0 +1,107 @@
|
||||
/* mbed Microcontroller Library
|
||||
* Copyright (c) 2006-2013 ARM Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#ifndef MBED_RAW_SERIAL_H
|
||||
#define MBED_RAW_SERIAL_H
|
||||
|
||||
#include "platform/platform.h"
|
||||
|
||||
#if DEVICE_SERIAL
|
||||
|
||||
#include "drivers/SerialBase.h"
|
||||
#include "hal/serial_api.h"
|
||||
|
||||
namespace mbed {
|
||||
/** \addtogroup drivers */
|
||||
/** @{*/
|
||||
|
||||
/** A serial port (UART) for communication with other serial devices
|
||||
* This is a variation of the Serial class that doesn't use streams,
|
||||
* thus making it safe to use in interrupt handlers with the RTOS.
|
||||
*
|
||||
* Can be used for Full Duplex communication, or Simplex by specifying
|
||||
* one pin as NC (Not Connected)
|
||||
*
|
||||
* @Note Synchronization level: Not protected
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* // Send a char to the PC
|
||||
*
|
||||
* #include "mbed.h"
|
||||
*
|
||||
* RawSerial pc(USBTX, USBRX);
|
||||
*
|
||||
* int main() {
|
||||
* pc.putc('A');
|
||||
* }
|
||||
* @endcode
|
||||
*/
|
||||
class RawSerial: public SerialBase {
|
||||
|
||||
public:
|
||||
/** Create a RawSerial port, connected to the specified transmit and receive pins, with the specified baud.
|
||||
*
|
||||
* @param tx Transmit pin
|
||||
* @param rx Receive pin
|
||||
* @param baud The baud rate of the serial port (optional, defaults to MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE)
|
||||
*
|
||||
* @note
|
||||
* Either tx or rx may be specified as NC if unused
|
||||
*/
|
||||
RawSerial(PinName tx, PinName rx, int baud = MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE);
|
||||
|
||||
/** Write a char to the serial port
|
||||
*
|
||||
* @param c The char to write
|
||||
*
|
||||
* @returns The written char or -1 if an error occured
|
||||
*/
|
||||
int putc(int c);
|
||||
|
||||
/** Read a char from the serial port
|
||||
*
|
||||
* @returns The char read from the serial port
|
||||
*/
|
||||
int getc();
|
||||
|
||||
/** Write a string to the serial port
|
||||
*
|
||||
* @param str The string to write
|
||||
*
|
||||
* @returns 0 if the write succeeds, EOF for error
|
||||
*/
|
||||
int puts(const char *str);
|
||||
|
||||
int printf(const char *format, ...);
|
||||
|
||||
protected:
|
||||
|
||||
/** Acquire exclusive access to this serial port
|
||||
*/
|
||||
virtual void lock(void);
|
||||
|
||||
/** Release exclusive access to this serial port
|
||||
*/
|
||||
virtual void unlock(void);
|
||||
};
|
||||
|
||||
} // namespace mbed
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
/** @}*/
|
||||
265
mbed/drivers/SPI.h
Normal file
265
mbed/drivers/SPI.h
Normal file
@@ -0,0 +1,265 @@
|
||||
/* mbed Microcontroller Library
|
||||
* Copyright (c) 2006-2015 ARM Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#ifndef MBED_SPI_H
|
||||
#define MBED_SPI_H
|
||||
|
||||
#include "platform/platform.h"
|
||||
|
||||
#if DEVICE_SPI
|
||||
|
||||
#include "platform/PlatformMutex.h"
|
||||
#include "hal/spi_api.h"
|
||||
#include "platform/SingletonPtr.h"
|
||||
|
||||
#if DEVICE_SPI_ASYNCH
|
||||
#include "platform/CThunk.h"
|
||||
#include "hal/dma_api.h"
|
||||
#include "platform/CircularBuffer.h"
|
||||
#include "platform/FunctionPointer.h"
|
||||
#include "platform/Transaction.h"
|
||||
#endif
|
||||
|
||||
namespace mbed {
|
||||
/** \addtogroup drivers */
|
||||
/** @{*/
|
||||
|
||||
/** A SPI Master, used for communicating with SPI slave devices
|
||||
*
|
||||
* The default format is set to 8-bits, mode 0, and a clock frequency of 1MHz
|
||||
*
|
||||
* Most SPI devices will also require Chip Select and Reset signals. These
|
||||
* can be controlled using <DigitalOut> pins
|
||||
*
|
||||
* @Note Synchronization level: Thread safe
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* // Send a byte to a SPI slave, and record the response
|
||||
*
|
||||
* #include "mbed.h"
|
||||
*
|
||||
* // hardware ssel (where applicable)
|
||||
* //SPI device(p5, p6, p7, p8); // mosi, miso, sclk, ssel
|
||||
*
|
||||
* // software ssel
|
||||
* SPI device(p5, p6, p7); // mosi, miso, sclk
|
||||
* DigitalOut cs(p8); // ssel
|
||||
*
|
||||
* int main() {
|
||||
* // hardware ssel (where applicable)
|
||||
* //int response = device.write(0xFF);
|
||||
*
|
||||
* device.lock();
|
||||
* // software ssel
|
||||
* cs = 0;
|
||||
* int response = device.write(0xFF);
|
||||
* cs = 1;
|
||||
* device.unlock();
|
||||
*
|
||||
* }
|
||||
* @endcode
|
||||
*/
|
||||
class SPI {
|
||||
|
||||
public:
|
||||
|
||||
/** Create a SPI master connected to the specified pins
|
||||
*
|
||||
* mosi or miso can be specfied as NC if not used
|
||||
*
|
||||
* @param mosi SPI Master Out, Slave In pin
|
||||
* @param miso SPI Master In, Slave Out pin
|
||||
* @param sclk SPI Clock pin
|
||||
* @param ssel SPI chip select pin
|
||||
*/
|
||||
SPI(PinName mosi, PinName miso, PinName sclk, PinName ssel=NC);
|
||||
|
||||
/** Configure the data transmission format
|
||||
*
|
||||
* @param bits Number of bits per SPI frame (4 - 16)
|
||||
* @param mode Clock polarity and phase mode (0 - 3)
|
||||
*
|
||||
* @code
|
||||
* mode | POL PHA
|
||||
* -----+--------
|
||||
* 0 | 0 0
|
||||
* 1 | 0 1
|
||||
* 2 | 1 0
|
||||
* 3 | 1 1
|
||||
* @endcode
|
||||
*/
|
||||
void format(int bits, int mode = 0);
|
||||
|
||||
/** Set the spi bus clock frequency
|
||||
*
|
||||
* @param hz SCLK frequency in hz (default = 1MHz)
|
||||
*/
|
||||
void frequency(int hz = 1000000);
|
||||
|
||||
/** Write to the SPI Slave and return the response
|
||||
*
|
||||
* @param value Data to be sent to the SPI slave
|
||||
*
|
||||
* @returns
|
||||
* Response from the SPI slave
|
||||
*/
|
||||
virtual int write(int value);
|
||||
|
||||
/** Acquire exclusive access to this SPI bus
|
||||
*/
|
||||
virtual void lock(void);
|
||||
|
||||
/** Release exclusive access to this SPI bus
|
||||
*/
|
||||
virtual void unlock(void);
|
||||
|
||||
#if DEVICE_SPI_ASYNCH
|
||||
|
||||
/** Start non-blocking SPI transfer using 8bit buffers.
|
||||
*
|
||||
* @param tx_buffer The TX buffer with data to be transfered. If NULL is passed,
|
||||
* the default SPI value is sent
|
||||
* @param tx_length The length of TX buffer in bytes
|
||||
* @param rx_buffer The RX buffer which is used for received data. If NULL is passed,
|
||||
* received data are ignored
|
||||
* @param rx_length The length of RX buffer in bytes
|
||||
* @param callback The event callback function
|
||||
* @param event The logical OR of events to modify. Look at spi hal header file for SPI events.
|
||||
* @return Zero if the transfer has started, or -1 if SPI peripheral is busy
|
||||
*/
|
||||
template<typename Type>
|
||||
int transfer(const Type *tx_buffer, int tx_length, Type *rx_buffer, int rx_length, const event_callback_t& callback, int event = SPI_EVENT_COMPLETE) {
|
||||
if (spi_active(&_spi)) {
|
||||
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);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/** Abort the on-going SPI transfer, and continue with transfer's in the queue if any.
|
||||
*/
|
||||
void abort_transfer();
|
||||
|
||||
/** Clear the transaction buffer
|
||||
*/
|
||||
void clear_transfer_buffer();
|
||||
|
||||
/** Clear the transaction buffer and abort on-going transfer.
|
||||
*/
|
||||
void abort_all_transfers();
|
||||
|
||||
/** Configure DMA usage suggestion for non-blocking transfers
|
||||
*
|
||||
* @param usage The usage DMA hint for peripheral
|
||||
* @return Zero if the usage was set, -1 if a transaction is on-going
|
||||
*/
|
||||
int set_dma_usage(DMAUsage usage);
|
||||
|
||||
protected:
|
||||
/** SPI IRQ handler
|
||||
*
|
||||
*/
|
||||
void irq_handler_asynch(void);
|
||||
|
||||
/** Common transfer method
|
||||
*
|
||||
* @param tx_buffer The TX buffer with data to be transfered. If NULL is passed,
|
||||
* the default SPI value is sent
|
||||
* @param tx_length The length of TX buffer in bytes
|
||||
* @param rx_buffer The RX buffer which is used for received data. If NULL is passed,
|
||||
* received data are ignored
|
||||
* @param rx_length The length of RX buffer in bytes
|
||||
* @param bit_width The buffers element width
|
||||
* @param callback The event callback function
|
||||
* @param event The logical OR of events to modify
|
||||
* @return Zero if the transfer has started or was added to the queue, or -1 if SPI peripheral is busy/buffer is full
|
||||
*/
|
||||
int transfer(const void *tx_buffer, int tx_length, void *rx_buffer, int rx_length, unsigned char bit_width, const event_callback_t& callback, int event);
|
||||
|
||||
/**
|
||||
*
|
||||
* @param tx_buffer The TX buffer with data to be transfered. If NULL is passed,
|
||||
* the default SPI value is sent
|
||||
* @param tx_length The length of TX buffer in bytes
|
||||
* @param rx_buffer The RX buffer which is used for received data. If NULL is passed,
|
||||
* received data are ignored
|
||||
* @param rx_length The length of RX buffer in bytes
|
||||
* @param bit_width The buffers element width
|
||||
* @param callback The event callback function
|
||||
* @param event The logical OR of events to modify
|
||||
* @return Zero if a transfer was added to the queue, or -1 if the queue is full
|
||||
*/
|
||||
int queue_transfer(const void *tx_buffer, int tx_length, void *rx_buffer, int rx_length, unsigned char bit_width, const event_callback_t& callback, int event);
|
||||
|
||||
/** Configures a callback, spi peripheral and initiate a new transfer
|
||||
*
|
||||
* @param tx_buffer The TX buffer with data to be transfered. If NULL is passed,
|
||||
* the default SPI value is sent
|
||||
* @param tx_length The length of TX buffer in bytes
|
||||
* @param rx_buffer The RX buffer which is used for received data. If NULL is passed,
|
||||
* received data are ignored
|
||||
* @param rx_length The length of RX buffer in bytes
|
||||
* @param bit_width The buffers element width
|
||||
* @param callback The event callback function
|
||||
* @param event The logical OR of events to modify
|
||||
*/
|
||||
void start_transfer(const void *tx_buffer, int tx_length, void *rx_buffer, int rx_length, unsigned char bit_width, const event_callback_t& callback, int event);
|
||||
|
||||
#if TRANSACTION_QUEUE_SIZE_SPI
|
||||
|
||||
/** Start a new transaction
|
||||
*
|
||||
* @param data Transaction data
|
||||
*/
|
||||
void start_transaction(transaction_t *data);
|
||||
|
||||
/** Dequeue a transaction
|
||||
*
|
||||
*/
|
||||
void dequeue_transaction();
|
||||
static CircularBuffer<Transaction<SPI>, TRANSACTION_QUEUE_SIZE_SPI> _transaction_buffer;
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
public:
|
||||
virtual ~SPI() {
|
||||
}
|
||||
|
||||
protected:
|
||||
spi_t _spi;
|
||||
|
||||
#if DEVICE_SPI_ASYNCH
|
||||
CThunk<SPI> _irq;
|
||||
event_callback_t _callback;
|
||||
DMAUsage _usage;
|
||||
#endif
|
||||
|
||||
void aquire(void);
|
||||
static SPI *_owner;
|
||||
static SingletonPtr<PlatformMutex> _mutex;
|
||||
int _bits;
|
||||
int _mode;
|
||||
int _hz;
|
||||
};
|
||||
|
||||
} // namespace mbed
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
/** @}*/
|
||||
128
mbed/drivers/SPISlave.h
Normal file
128
mbed/drivers/SPISlave.h
Normal file
@@ -0,0 +1,128 @@
|
||||
/* mbed Microcontroller Library
|
||||
* Copyright (c) 2006-2013 ARM Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#ifndef MBED_SPISLAVE_H
|
||||
#define MBED_SPISLAVE_H
|
||||
|
||||
#include "platform/platform.h"
|
||||
|
||||
#if DEVICE_SPISLAVE
|
||||
|
||||
#include "hal/spi_api.h"
|
||||
|
||||
namespace mbed {
|
||||
/** \addtogroup drivers */
|
||||
/** @{*/
|
||||
|
||||
/** A SPI slave, used for communicating with a SPI Master device
|
||||
*
|
||||
* The default format is set to 8-bits, mode 0, and a clock frequency of 1MHz
|
||||
*
|
||||
* @Note Synchronization level: Not protected
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* // Reply to a SPI master as slave
|
||||
*
|
||||
* #include "mbed.h"
|
||||
*
|
||||
* SPISlave device(p5, p6, p7, p8); // mosi, miso, sclk, ssel
|
||||
*
|
||||
* int main() {
|
||||
* device.reply(0x00); // Prime SPI with first reply
|
||||
* while(1) {
|
||||
* if(device.receive()) {
|
||||
* int v = device.read(); // Read byte from master
|
||||
* v = (v + 1) % 0x100; // Add one to it, modulo 256
|
||||
* device.reply(v); // Make this the next reply
|
||||
* }
|
||||
* }
|
||||
* }
|
||||
* @endcode
|
||||
*/
|
||||
class SPISlave {
|
||||
|
||||
public:
|
||||
|
||||
/** Create a SPI slave connected to the specified pins
|
||||
*
|
||||
* mosi or miso can be specfied as NC if not used
|
||||
*
|
||||
* @param mosi SPI Master Out, Slave In pin
|
||||
* @param miso SPI Master In, Slave Out pin
|
||||
* @param sclk SPI Clock pin
|
||||
* @param ssel SPI chip select pin
|
||||
*/
|
||||
SPISlave(PinName mosi, PinName miso, PinName sclk, PinName ssel);
|
||||
|
||||
/** Configure the data transmission format
|
||||
*
|
||||
* @param bits Number of bits per SPI frame (4 - 16)
|
||||
* @param mode Clock polarity and phase mode (0 - 3)
|
||||
*
|
||||
* @code
|
||||
* mode | POL PHA
|
||||
* -----+--------
|
||||
* 0 | 0 0
|
||||
* 1 | 0 1
|
||||
* 2 | 1 0
|
||||
* 3 | 1 1
|
||||
* @endcode
|
||||
*/
|
||||
void format(int bits, int mode = 0);
|
||||
|
||||
/** Set the spi bus clock frequency
|
||||
*
|
||||
* @param hz SCLK frequency in hz (default = 1MHz)
|
||||
*/
|
||||
void frequency(int hz = 1000000);
|
||||
|
||||
/** Polls the SPI to see if data has been received
|
||||
*
|
||||
* @returns
|
||||
* 0 if no data,
|
||||
* 1 otherwise
|
||||
*/
|
||||
int receive(void);
|
||||
|
||||
/** Retrieve data from receive buffer as slave
|
||||
*
|
||||
* @returns
|
||||
* the data in the receive buffer
|
||||
*/
|
||||
int read(void);
|
||||
|
||||
/** Fill the transmission buffer with the value to be written out
|
||||
* as slave on the next received message from the master.
|
||||
*
|
||||
* @param value the data to be transmitted next
|
||||
*/
|
||||
void reply(int value);
|
||||
|
||||
protected:
|
||||
spi_t _spi;
|
||||
|
||||
int _bits;
|
||||
int _mode;
|
||||
int _hz;
|
||||
};
|
||||
|
||||
} // namespace mbed
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
/** @}*/
|
||||
99
mbed/drivers/Serial.h
Normal file
99
mbed/drivers/Serial.h
Normal file
@@ -0,0 +1,99 @@
|
||||
/* mbed Microcontroller Library
|
||||
* Copyright (c) 2006-2013 ARM Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#ifndef MBED_SERIAL_H
|
||||
#define MBED_SERIAL_H
|
||||
|
||||
#include "platform/platform.h"
|
||||
|
||||
#if DEVICE_SERIAL
|
||||
|
||||
#include "Stream.h"
|
||||
#include "SerialBase.h"
|
||||
#include "PlatformMutex.h"
|
||||
#include "serial_api.h"
|
||||
|
||||
namespace mbed {
|
||||
/** \addtogroup drivers */
|
||||
/** @{*/
|
||||
|
||||
/** A serial port (UART) for communication with other serial devices
|
||||
*
|
||||
* Can be used for Full Duplex communication, or Simplex by specifying
|
||||
* one pin as NC (Not Connected)
|
||||
*
|
||||
* @Note Synchronization level: Thread safe
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* // Print "Hello World" to the PC
|
||||
*
|
||||
* #include "mbed.h"
|
||||
*
|
||||
* Serial pc(USBTX, USBRX);
|
||||
*
|
||||
* int main() {
|
||||
* pc.printf("Hello World\n");
|
||||
* }
|
||||
* @endcode
|
||||
*/
|
||||
class Serial : public SerialBase, public Stream {
|
||||
|
||||
public:
|
||||
#if DEVICE_SERIAL_ASYNCH
|
||||
using SerialBase::read;
|
||||
using SerialBase::write;
|
||||
#endif
|
||||
|
||||
/** Create a Serial port, connected to the specified transmit and receive pins
|
||||
*
|
||||
* @param tx Transmit pin
|
||||
* @param rx Receive pin
|
||||
* @param name The name of the stream associated with this serial port (optional)
|
||||
* @param baud The baud rate of the serial port (optional, defaults to MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE)
|
||||
*
|
||||
* @note
|
||||
* Either tx or rx may be specified as NC if unused
|
||||
*/
|
||||
Serial(PinName tx, PinName rx, const char *name=NULL, int baud = MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE);
|
||||
|
||||
|
||||
/** Create a Serial port, connected to the specified transmit and receive pins, with the specified baud
|
||||
*
|
||||
* @param tx Transmit pin
|
||||
* @param rx Receive pin
|
||||
* @param baud The baud rate of the serial port
|
||||
*
|
||||
* @note
|
||||
* Either tx or rx may be specified as NC if unused
|
||||
*/
|
||||
Serial(PinName tx, PinName rx, int baud);
|
||||
|
||||
protected:
|
||||
virtual int _getc();
|
||||
virtual int _putc(int c);
|
||||
virtual void lock();
|
||||
virtual void unlock();
|
||||
|
||||
PlatformMutex _mutex;
|
||||
};
|
||||
|
||||
} // namespace mbed
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
/** @}*/
|
||||
262
mbed/drivers/SerialBase.h
Normal file
262
mbed/drivers/SerialBase.h
Normal file
@@ -0,0 +1,262 @@
|
||||
/* mbed Microcontroller Library
|
||||
* Copyright (c) 2006-2013 ARM Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#ifndef MBED_SERIALBASE_H
|
||||
#define MBED_SERIALBASE_H
|
||||
|
||||
#include "platform/platform.h"
|
||||
|
||||
#if DEVICE_SERIAL
|
||||
|
||||
#include "Stream.h"
|
||||
#include "Callback.h"
|
||||
#include "serial_api.h"
|
||||
#include "mbed_toolchain.h"
|
||||
|
||||
#if DEVICE_SERIAL_ASYNCH
|
||||
#include "CThunk.h"
|
||||
#include "dma_api.h"
|
||||
#endif
|
||||
|
||||
namespace mbed {
|
||||
/** \addtogroup drivers */
|
||||
/** @{*/
|
||||
|
||||
/** A base class for serial port implementations
|
||||
* Can't be instantiated directly (use Serial or RawSerial)
|
||||
*
|
||||
* @Note Synchronization level: Set by subclass
|
||||
*/
|
||||
class SerialBase {
|
||||
|
||||
public:
|
||||
/** Set the baud rate of the serial port
|
||||
*
|
||||
* @param baudrate The baudrate of the serial port (default = 9600).
|
||||
*/
|
||||
void baud(int baudrate);
|
||||
|
||||
enum Parity {
|
||||
None = 0,
|
||||
Odd,
|
||||
Even,
|
||||
Forced1,
|
||||
Forced0
|
||||
};
|
||||
|
||||
enum IrqType {
|
||||
RxIrq = 0,
|
||||
TxIrq,
|
||||
|
||||
IrqCnt
|
||||
};
|
||||
|
||||
enum Flow {
|
||||
Disabled = 0,
|
||||
RTS,
|
||||
CTS,
|
||||
RTSCTS
|
||||
};
|
||||
|
||||
/** Set the transmission format used by the serial port
|
||||
*
|
||||
* @param bits The number of bits in a word (5-8; default = 8)
|
||||
* @param parity The parity used (SerialBase::None, SerialBase::Odd, SerialBase::Even, SerialBase::Forced1, SerialBase::Forced0; default = SerialBase::None)
|
||||
* @param stop The number of stop bits (1 or 2; default = 1)
|
||||
*/
|
||||
void format(int bits=8, Parity parity=SerialBase::None, int stop_bits=1);
|
||||
|
||||
/** Determine if there is a character available to read
|
||||
*
|
||||
* @returns
|
||||
* 1 if there is a character available to read,
|
||||
* 0 otherwise
|
||||
*/
|
||||
int readable();
|
||||
|
||||
/** Determine if there is space available to write a character
|
||||
*
|
||||
* @returns
|
||||
* 1 if there is space to write a character,
|
||||
* 0 otherwise
|
||||
*/
|
||||
int writeable();
|
||||
|
||||
/** Attach a function to call whenever a serial interrupt is generated
|
||||
*
|
||||
* @param func A pointer to a void function, or 0 to set as none
|
||||
* @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
|
||||
*/
|
||||
void attach(Callback<void()> func, IrqType type=RxIrq);
|
||||
|
||||
/** Attach a member function to call whenever a serial interrupt is generated
|
||||
*
|
||||
* @param obj pointer to the object to call the member function on
|
||||
* @param method pointer to the member function to be called
|
||||
* @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
|
||||
* @deprecated
|
||||
* The attach function does not support cv-qualifiers. Replaced by
|
||||
* attach(callback(obj, method), type).
|
||||
*/
|
||||
template<typename T>
|
||||
MBED_DEPRECATED_SINCE("mbed-os-5.1",
|
||||
"The attach function does not support cv-qualifiers. Replaced by "
|
||||
"attach(callback(obj, method), type).")
|
||||
void attach(T *obj, void (T::*method)(), IrqType type=RxIrq) {
|
||||
attach(callback(obj, method), type);
|
||||
}
|
||||
|
||||
/** Attach a member function to call whenever a serial interrupt is generated
|
||||
*
|
||||
* @param obj pointer to the object to call the member function on
|
||||
* @param method pointer to the member function to be called
|
||||
* @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
|
||||
* @deprecated
|
||||
* The attach function does not support cv-qualifiers. Replaced by
|
||||
* attach(callback(obj, method), type).
|
||||
*/
|
||||
template<typename T>
|
||||
MBED_DEPRECATED_SINCE("mbed-os-5.1",
|
||||
"The attach function does not support cv-qualifiers. Replaced by "
|
||||
"attach(callback(obj, method), type).")
|
||||
void attach(T *obj, void (*method)(T*), IrqType type=RxIrq) {
|
||||
attach(callback(obj, method), type);
|
||||
}
|
||||
|
||||
/** Generate a break condition on the serial line
|
||||
*/
|
||||
void send_break();
|
||||
|
||||
protected:
|
||||
|
||||
/** Acquire exclusive access to this serial port
|
||||
*/
|
||||
virtual void lock(void);
|
||||
|
||||
/** Release exclusive access to this serial port
|
||||
*/
|
||||
virtual void unlock(void);
|
||||
|
||||
public:
|
||||
|
||||
#if DEVICE_SERIAL_FC
|
||||
/** Set the flow control type on the serial port
|
||||
*
|
||||
* @param type the flow control type (Disabled, RTS, CTS, RTSCTS)
|
||||
* @param flow1 the first flow control pin (RTS for RTS or RTSCTS, CTS for CTS)
|
||||
* @param flow2 the second flow control pin (CTS for RTSCTS)
|
||||
*/
|
||||
void set_flow_control(Flow type, PinName flow1=NC, PinName flow2=NC);
|
||||
#endif
|
||||
|
||||
static void _irq_handler(uint32_t id, SerialIrq irq_type);
|
||||
|
||||
#if DEVICE_SERIAL_ASYNCH
|
||||
|
||||
/** Begin asynchronous write using 8bit buffer. The completition invokes registered TX event callback
|
||||
*
|
||||
* @param buffer The buffer where received data will be stored
|
||||
* @param length The buffer length in bytes
|
||||
* @param callback The event callback function
|
||||
* @param event The logical OR of TX events
|
||||
*/
|
||||
int write(const uint8_t *buffer, int length, const event_callback_t& callback, int event = SERIAL_EVENT_TX_COMPLETE);
|
||||
|
||||
/** Begin asynchronous write using 16bit buffer. The completition invokes registered TX event callback
|
||||
*
|
||||
* @param buffer The buffer where received data will be stored
|
||||
* @param length The buffer length in bytes
|
||||
* @param callback The event callback function
|
||||
* @param event The logical OR of TX events
|
||||
*/
|
||||
int write(const uint16_t *buffer, int length, const event_callback_t& callback, int event = SERIAL_EVENT_TX_COMPLETE);
|
||||
|
||||
/** Abort the on-going write transfer
|
||||
*/
|
||||
void abort_write();
|
||||
|
||||
/** Begin asynchronous reading using 8bit buffer. The completition invokes registred RX event callback.
|
||||
*
|
||||
* @param buffer The buffer where received data will be stored
|
||||
* @param length The buffer length in bytes
|
||||
* @param callback The event callback function
|
||||
* @param event The logical OR of RX events
|
||||
* @param char_match The matching character
|
||||
*/
|
||||
int read(uint8_t *buffer, int length, const event_callback_t& callback, int event = SERIAL_EVENT_RX_COMPLETE, unsigned char char_match = SERIAL_RESERVED_CHAR_MATCH);
|
||||
|
||||
/** Begin asynchronous reading using 16bit buffer. The completition invokes registred RX event callback.
|
||||
*
|
||||
* @param buffer The buffer where received data will be stored
|
||||
* @param length The buffer length in bytes
|
||||
* @param callback The event callback function
|
||||
* @param event The logical OR of RX events
|
||||
* @param char_match The matching character
|
||||
*/
|
||||
int read(uint16_t *buffer, int length, const event_callback_t& callback, int event = SERIAL_EVENT_RX_COMPLETE, unsigned char char_match = SERIAL_RESERVED_CHAR_MATCH);
|
||||
|
||||
/** Abort the on-going read transfer
|
||||
*/
|
||||
void abort_read();
|
||||
|
||||
/** Configure DMA usage suggestion for non-blocking TX transfers
|
||||
*
|
||||
* @param usage The usage DMA hint for peripheral
|
||||
* @return Zero if the usage was set, -1 if a transaction is on-going
|
||||
*/
|
||||
int set_dma_usage_tx(DMAUsage usage);
|
||||
|
||||
/** Configure DMA usage suggestion for non-blocking RX transfers
|
||||
*
|
||||
* @param usage The usage DMA hint for peripheral
|
||||
* @return Zero if the usage was set, -1 if a transaction is on-going
|
||||
*/
|
||||
int set_dma_usage_rx(DMAUsage usage);
|
||||
|
||||
protected:
|
||||
void start_read(void *buffer, int buffer_size, char buffer_width, const event_callback_t& callback, int event, unsigned char char_match);
|
||||
void start_write(const void *buffer, int buffer_size, char buffer_width, const event_callback_t& callback, int event);
|
||||
void interrupt_handler_asynch(void);
|
||||
#endif
|
||||
|
||||
protected:
|
||||
SerialBase(PinName tx, PinName rx, int baud);
|
||||
virtual ~SerialBase() {
|
||||
}
|
||||
|
||||
int _base_getc();
|
||||
int _base_putc(int c);
|
||||
|
||||
#if DEVICE_SERIAL_ASYNCH
|
||||
CThunk<SerialBase> _thunk_irq;
|
||||
event_callback_t _tx_callback;
|
||||
event_callback_t _rx_callback;
|
||||
DMAUsage _tx_usage;
|
||||
DMAUsage _rx_usage;
|
||||
#endif
|
||||
|
||||
serial_t _serial;
|
||||
Callback<void()> _irq[IrqCnt];
|
||||
int _baud;
|
||||
|
||||
};
|
||||
|
||||
} // namespace mbed
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
/** @}*/
|
||||
79
mbed/drivers/Stream.h
Normal file
79
mbed/drivers/Stream.h
Normal file
@@ -0,0 +1,79 @@
|
||||
/* mbed Microcontroller Library
|
||||
* Copyright (c) 2006-2013 ARM Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#ifndef MBED_STREAM_H
|
||||
#define MBED_STREAM_H
|
||||
|
||||
#include "platform/platform.h"
|
||||
#include "drivers/FileLike.h"
|
||||
#include "drivers/FileHandle.h"
|
||||
#include <cstdarg>
|
||||
|
||||
namespace mbed {
|
||||
/** \addtogroup drivers */
|
||||
/** @{*/
|
||||
|
||||
extern void mbed_set_unbuffered_stream(FILE *_file);
|
||||
extern int mbed_getc(FILE *_file);
|
||||
extern char* mbed_gets(char *s, int size, FILE *_file);
|
||||
|
||||
/** File stream
|
||||
*
|
||||
* @Note Synchronization level: Set by subclass
|
||||
*/
|
||||
class Stream : public FileLike {
|
||||
|
||||
public:
|
||||
Stream(const char *name=NULL);
|
||||
virtual ~Stream();
|
||||
|
||||
int putc(int c);
|
||||
int puts(const char *s);
|
||||
int getc();
|
||||
char *gets(char *s, int size);
|
||||
int printf(const char* format, ...);
|
||||
int scanf(const char* format, ...);
|
||||
int vprintf(const char* format, std::va_list args);
|
||||
int vscanf(const char* format, std::va_list args);
|
||||
|
||||
operator std::FILE*() {return _file;}
|
||||
|
||||
protected:
|
||||
virtual int close();
|
||||
virtual ssize_t write(const void* buffer, size_t length);
|
||||
virtual ssize_t read(void* buffer, size_t length);
|
||||
virtual off_t seek(off_t offset, int whence);
|
||||
virtual off_t tell();
|
||||
virtual void rewind();
|
||||
virtual int isatty();
|
||||
virtual int sync();
|
||||
virtual size_t size();
|
||||
|
||||
virtual int _putc(int c) = 0;
|
||||
virtual int _getc() = 0;
|
||||
|
||||
std::FILE *_file;
|
||||
|
||||
/* disallow copy constructor and assignment operators */
|
||||
private:
|
||||
Stream(const Stream&);
|
||||
Stream & operator = (const Stream&);
|
||||
};
|
||||
|
||||
} // namespace mbed
|
||||
|
||||
#endif
|
||||
|
||||
/** @}*/
|
||||
146
mbed/drivers/Ticker.h
Normal file
146
mbed/drivers/Ticker.h
Normal file
@@ -0,0 +1,146 @@
|
||||
/* mbed Microcontroller Library
|
||||
* Copyright (c) 2006-2013 ARM Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#ifndef MBED_TICKER_H
|
||||
#define MBED_TICKER_H
|
||||
|
||||
#include "drivers/TimerEvent.h"
|
||||
#include "platform/Callback.h"
|
||||
#include "platform/mbed_toolchain.h"
|
||||
|
||||
namespace mbed {
|
||||
/** \addtogroup drivers */
|
||||
/** @{*/
|
||||
|
||||
/** A Ticker is used to call a function at a recurring interval
|
||||
*
|
||||
* You can use as many seperate Ticker objects as you require.
|
||||
*
|
||||
* @Note Synchronization level: Interrupt safe
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* // Toggle the blinking led after 5 seconds
|
||||
*
|
||||
* #include "mbed.h"
|
||||
*
|
||||
* Ticker timer;
|
||||
* DigitalOut led1(LED1);
|
||||
* DigitalOut led2(LED2);
|
||||
*
|
||||
* int flip = 0;
|
||||
*
|
||||
* void attime() {
|
||||
* flip = !flip;
|
||||
* }
|
||||
*
|
||||
* int main() {
|
||||
* timer.attach(&attime, 5);
|
||||
* while(1) {
|
||||
* if(flip == 0) {
|
||||
* led1 = !led1;
|
||||
* } else {
|
||||
* led2 = !led2;
|
||||
* }
|
||||
* wait(0.2);
|
||||
* }
|
||||
* }
|
||||
* @endcode
|
||||
*/
|
||||
class Ticker : public TimerEvent {
|
||||
|
||||
public:
|
||||
Ticker() : TimerEvent() {
|
||||
}
|
||||
|
||||
Ticker(const ticker_data_t *data) : TimerEvent(data) {
|
||||
data->interface->init();
|
||||
}
|
||||
|
||||
/** Attach a function to be called by the Ticker, specifiying the interval in seconds
|
||||
*
|
||||
* @param func pointer to the function to be called
|
||||
* @param t the time between calls in seconds
|
||||
*/
|
||||
void attach(Callback<void()> func, float t) {
|
||||
attach_us(func, t * 1000000.0f);
|
||||
}
|
||||
|
||||
/** Attach a member function to be called by the Ticker, specifiying the interval in seconds
|
||||
*
|
||||
* @param obj pointer to the object to call the member function on
|
||||
* @param method pointer to the member function to be called
|
||||
* @param t the time between calls in seconds
|
||||
* @deprecated
|
||||
* The attach function does not support cv-qualifiers. Replaced by
|
||||
* attach(callback(obj, method), t).
|
||||
*/
|
||||
template<typename T, typename M>
|
||||
MBED_DEPRECATED_SINCE("mbed-os-5.1",
|
||||
"The attach function does not support cv-qualifiers. Replaced by "
|
||||
"attach(callback(obj, method), t).")
|
||||
void attach(T *obj, M method, float t) {
|
||||
attach(callback(obj, method), t);
|
||||
}
|
||||
|
||||
/** Attach a function to be called by the Ticker, specifiying the interval in micro-seconds
|
||||
*
|
||||
* @param fptr pointer to the function to be called
|
||||
* @param t the time between calls in micro-seconds
|
||||
*/
|
||||
void attach_us(Callback<void()> func, timestamp_t t) {
|
||||
_function = func;
|
||||
setup(t);
|
||||
}
|
||||
|
||||
/** Attach a member function to be called by the Ticker, specifiying the interval in micro-seconds
|
||||
*
|
||||
* @param tptr pointer to the object to call the member function on
|
||||
* @param mptr pointer to the member function to be called
|
||||
* @param t the time between calls in micro-seconds
|
||||
* @deprecated
|
||||
* The attach_us function does not support cv-qualifiers. Replaced by
|
||||
* attach_us(callback(obj, method), t).
|
||||
*/
|
||||
template<typename T, typename M>
|
||||
MBED_DEPRECATED_SINCE("mbed-os-5.1",
|
||||
"The attach_us function does not support cv-qualifiers. Replaced by "
|
||||
"attach_us(callback(obj, method), t).")
|
||||
void attach_us(T *obj, M method, timestamp_t t) {
|
||||
attach_us(Callback<void()>(obj, method), t);
|
||||
}
|
||||
|
||||
virtual ~Ticker() {
|
||||
detach();
|
||||
}
|
||||
|
||||
/** Detach the function
|
||||
*/
|
||||
void detach();
|
||||
|
||||
protected:
|
||||
void setup(timestamp_t t);
|
||||
virtual void handler();
|
||||
|
||||
protected:
|
||||
timestamp_t _delay; /**< Time delay (in microseconds) for re-setting the multi-shot callback. */
|
||||
Callback<void()> _function; /**< Callback. */
|
||||
};
|
||||
|
||||
} // namespace mbed
|
||||
|
||||
#endif
|
||||
|
||||
/** @}*/
|
||||
65
mbed/drivers/Timeout.h
Normal file
65
mbed/drivers/Timeout.h
Normal file
@@ -0,0 +1,65 @@
|
||||
/* mbed Microcontroller Library
|
||||
* Copyright (c) 2006-2013 ARM Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#ifndef MBED_TIMEOUT_H
|
||||
#define MBED_TIMEOUT_H
|
||||
|
||||
#include "drivers/Ticker.h"
|
||||
|
||||
namespace mbed {
|
||||
/** \addtogroup drivers */
|
||||
/** @{*/
|
||||
|
||||
/** A Timeout is used to call a function at a point in the future
|
||||
*
|
||||
* You can use as many seperate Timeout objects as you require.
|
||||
*
|
||||
* @Note Synchronization level: Interrupt safe
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* // Blink until timeout.
|
||||
*
|
||||
* #include "mbed.h"
|
||||
*
|
||||
* Timeout timeout;
|
||||
* DigitalOut led(LED1);
|
||||
*
|
||||
* int on = 1;
|
||||
*
|
||||
* void attimeout() {
|
||||
* on = 0;
|
||||
* }
|
||||
*
|
||||
* int main() {
|
||||
* timeout.attach(&attimeout, 5);
|
||||
* while(on) {
|
||||
* led = !led;
|
||||
* wait(0.2);
|
||||
* }
|
||||
* }
|
||||
* @endcode
|
||||
*/
|
||||
class Timeout : public Ticker {
|
||||
|
||||
protected:
|
||||
virtual void handler();
|
||||
};
|
||||
|
||||
} // namespace mbed
|
||||
|
||||
#endif
|
||||
|
||||
/** @}*/
|
||||
97
mbed/drivers/Timer.h
Normal file
97
mbed/drivers/Timer.h
Normal file
@@ -0,0 +1,97 @@
|
||||
/* mbed Microcontroller Library
|
||||
* Copyright (c) 2006-2013 ARM Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#ifndef MBED_TIMER_H
|
||||
#define MBED_TIMER_H
|
||||
|
||||
#include "platform/platform.h"
|
||||
#include "hal/ticker_api.h"
|
||||
|
||||
namespace mbed {
|
||||
/** \addtogroup drivers */
|
||||
/** @{*/
|
||||
|
||||
/** A general purpose timer
|
||||
*
|
||||
* @Note Synchronization level: Interrupt safe
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* // Count the time to toggle a LED
|
||||
*
|
||||
* #include "mbed.h"
|
||||
*
|
||||
* Timer timer;
|
||||
* DigitalOut led(LED1);
|
||||
* int begin, end;
|
||||
*
|
||||
* int main() {
|
||||
* timer.start();
|
||||
* begin = timer.read_us();
|
||||
* led = !led;
|
||||
* end = timer.read_us();
|
||||
* printf("Toggle the led takes %d us", end - begin);
|
||||
* }
|
||||
* @endcode
|
||||
*/
|
||||
class Timer {
|
||||
|
||||
public:
|
||||
Timer();
|
||||
Timer(const ticker_data_t *data);
|
||||
|
||||
/** Start the timer
|
||||
*/
|
||||
void start();
|
||||
|
||||
/** Stop the timer
|
||||
*/
|
||||
void stop();
|
||||
|
||||
/** Reset the timer to 0.
|
||||
*
|
||||
* If it was already counting, it will continue
|
||||
*/
|
||||
void reset();
|
||||
|
||||
/** Get the time passed in seconds
|
||||
*/
|
||||
float read();
|
||||
|
||||
/** Get the time passed in mili-seconds
|
||||
*/
|
||||
int read_ms();
|
||||
|
||||
/** Get the time passed in micro-seconds
|
||||
*/
|
||||
int read_us();
|
||||
|
||||
/** An operator shorthand for read()
|
||||
*/
|
||||
operator float();
|
||||
|
||||
protected:
|
||||
int slicetime();
|
||||
int _running; // whether the timer is running
|
||||
unsigned int _start; // the start time of the latest slice
|
||||
int _time; // any accumulated time from previous slices
|
||||
const ticker_data_t *_ticker_data;
|
||||
};
|
||||
|
||||
} // namespace mbed
|
||||
|
||||
#endif
|
||||
|
||||
/** @}*/
|
||||
62
mbed/drivers/TimerEvent.h
Normal file
62
mbed/drivers/TimerEvent.h
Normal file
@@ -0,0 +1,62 @@
|
||||
/* mbed Microcontroller Library
|
||||
* Copyright (c) 2006-2013 ARM Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#ifndef MBED_TIMEREVENT_H
|
||||
#define MBED_TIMEREVENT_H
|
||||
|
||||
#include "hal/ticker_api.h"
|
||||
#include "hal/us_ticker_api.h"
|
||||
|
||||
namespace mbed {
|
||||
/** \addtogroup drivers */
|
||||
/** @{*/
|
||||
|
||||
/** Base abstraction for timer interrupts
|
||||
*
|
||||
* @Note Synchronization level: Interrupt safe
|
||||
*/
|
||||
class TimerEvent {
|
||||
public:
|
||||
TimerEvent();
|
||||
TimerEvent(const ticker_data_t *data);
|
||||
|
||||
/** The handler registered with the underlying timer interrupt
|
||||
*/
|
||||
static void irq(uint32_t id);
|
||||
|
||||
/** Destruction removes it...
|
||||
*/
|
||||
virtual ~TimerEvent();
|
||||
|
||||
protected:
|
||||
// The handler called to service the timer event of the derived class
|
||||
virtual void handler() = 0;
|
||||
|
||||
// insert in to linked list
|
||||
void insert(timestamp_t timestamp);
|
||||
|
||||
// remove from linked list, if in it
|
||||
void remove();
|
||||
|
||||
ticker_event_t event;
|
||||
|
||||
const ticker_data_t *_ticker_data;
|
||||
};
|
||||
|
||||
} // namespace mbed
|
||||
|
||||
#endif
|
||||
|
||||
/** @}*/
|
||||
Reference in New Issue
Block a user