ads1115 only
Fork of mbed by
Diff: DigitalInOut.h
- Revision:
- 122:f9eeca106725
- Parent:
- 93:e188a91d3eaa
- Child:
- 123:b0220dba8be7
--- a/DigitalInOut.h Wed May 25 16:44:06 2016 +0100 +++ b/DigitalInOut.h Thu Jul 07 14:34:11 2016 +0100 @@ -19,10 +19,13 @@ #include "platform.h" #include "gpio_api.h" +#include "critical.h" namespace mbed { /** A digital input/output, used for setting or reading a bi-directional pin + * + * @Note Synchronization level: Interrupt safe */ class DigitalInOut { @@ -32,6 +35,7 @@ * @param pin DigitalInOut pin to connect to */ DigitalInOut(PinName pin) : gpio() { + // No lock needed in the constructor gpio_init_in(&gpio, pin); } @@ -43,6 +47,7 @@ * @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); } @@ -52,6 +57,7 @@ * 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); } @@ -62,19 +68,24 @@ * 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 @@ -82,7 +93,9 @@ * @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) @@ -92,6 +105,7 @@ * 0 if gpio object was initialized with NC */ int is_connected() { + // Thread safe / atomic HAL call return gpio_is_connected(&gpio); } @@ -99,18 +113,22 @@ /** 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(); } #endif