Skip to content

Pinology #808

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Jan 27, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions libraries/mbed/api/BusIn.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,35 @@ class BusIn {
*/
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() {
return _nc_mask;
}

#ifdef MBED_OPERATORS
/** A shorthand for read()
*/
operator int();

/** Access to particular bit in random-iterator fashion
*/
DigitalIn & operator[] (int index);
#endif

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;

/* disallow copy constructor and assignment operators */
private:
BusIn(const BusIn&);
Expand Down
21 changes: 20 additions & 1 deletion libraries/mbed/api/BusInOut.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ class BusInOut {
*/
void write(int value);


/** Read the value currently output on the bus
*
* @returns
Expand All @@ -73,12 +72,26 @@ class BusInOut {
*/
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() {
return _nc_mask;
}

#ifdef MBED_OPERATORS
/** 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();
Expand All @@ -87,6 +100,12 @@ class BusInOut {
protected:
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;

/* disallow copy constructor and assignment operators */
private:
BusInOut(const BusInOut&);
Expand Down
20 changes: 20 additions & 0 deletions libraries/mbed/api/BusOut.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,26 @@ class BusOut {
*/
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() {
return _nc_mask;
}

#ifdef MBED_OPERATORS
/** 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();
Expand All @@ -70,6 +84,12 @@ class BusOut {
protected:
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;

/* disallow copy constructor and assignment operators */
private:
BusOut(const BusOut&);
Expand Down
10 changes: 10 additions & 0 deletions libraries/mbed/api/DigitalIn.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,16 @@ class DigitalIn {
gpio_mode(&gpio, pull);
}

/** 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() {
return gpio_is_connected(&gpio);
}

#ifdef MBED_OPERATORS
/** An operator shorthand for read()
*/
Expand Down
10 changes: 10 additions & 0 deletions libraries/mbed/api/DigitalInOut.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,16 @@ class DigitalInOut {
gpio_mode(&gpio, pull);
}

/** 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() {
return gpio_is_connected(&gpio);
}

#ifdef MBED_OPERATORS
/** A shorthand for write()
*/
Expand Down
10 changes: 10 additions & 0 deletions libraries/mbed/api/DigitalOut.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,16 @@ class DigitalOut {
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() {
return gpio_is_connected(&gpio);
}

#ifdef MBED_OPERATORS
/** A shorthand for write()
*/
Expand Down
15 changes: 15 additions & 0 deletions libraries/mbed/common/BusIn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,22 @@ namespace mbed {
BusIn::BusIn(PinName p0, PinName p1, PinName p2, PinName p3, PinName p4, PinName p5, PinName p6, PinName p7, PinName p8, PinName p9, PinName p10, PinName p11, PinName p12, PinName p13, PinName p14, PinName p15) {
PinName pins[16] = {p0, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15};

_nc_mask = 0;
for (int i=0; i<16; i++) {
_pin[i] = (pins[i] != NC) ? new DigitalIn(pins[i]) : 0;
if (pins[i] != NC) {
_nc_mask |= (1 << i);
}
}
}

BusIn::BusIn(PinName pins[16]) {
_nc_mask = 0;
for (int i=0; i<16; i++) {
_pin[i] = (pins[i] != NC) ? new DigitalIn(pins[i]) : 0;
if (pins[i] != NC) {
_nc_mask |= (1 << i);
}
}
}

Expand Down Expand Up @@ -61,6 +69,13 @@ void BusIn::mode(PinMode pull) {
BusIn::operator int() {
return read();
}

DigitalIn& BusIn::operator[] (int index) {
MBED_ASSERT(index >= 0 && index <= 16);
MBED_ASSERT(_pin[index]);
return *_pin[index];
}

#endif

} // namespace mbed
14 changes: 14 additions & 0 deletions libraries/mbed/common/BusInOut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,22 @@ namespace mbed {
BusInOut::BusInOut(PinName p0, PinName p1, PinName p2, PinName p3, PinName p4, PinName p5, PinName p6, PinName p7, PinName p8, PinName p9, PinName p10, PinName p11, PinName p12, PinName p13, PinName p14, PinName p15) {
PinName pins[16] = {p0, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15};

_nc_mask = 0;
for (int i=0; i<16; i++) {
_pin[i] = (pins[i] != NC) ? new DigitalInOut(pins[i]) : 0;
if (pins[i] != NC) {
_nc_mask |= (1 << i);
}
}
}

BusInOut::BusInOut(PinName pins[16]) {
_nc_mask = 0;
for (int i=0; i<16; i++) {
_pin[i] = (pins[i] != NC) ? new DigitalInOut(pins[i]) : 0;
if (pins[i] != NC) {
_nc_mask |= (1 << i);
}
}
}

Expand Down Expand Up @@ -92,6 +100,12 @@ BusInOut& BusInOut::operator= (BusInOut& rhs) {
return *this;
}

DigitalInOut& BusInOut::operator[] (int index) {
MBED_ASSERT(index >= 0 && index <= 16);
MBED_ASSERT(_pin[index]);
return *_pin[index];
}

BusInOut::operator int() {
return read();
}
Expand Down
14 changes: 14 additions & 0 deletions libraries/mbed/common/BusOut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,22 @@ namespace mbed {
BusOut::BusOut(PinName p0, PinName p1, PinName p2, PinName p3, PinName p4, PinName p5, PinName p6, PinName p7, PinName p8, PinName p9, PinName p10, PinName p11, PinName p12, PinName p13, PinName p14, PinName p15) {
PinName pins[16] = {p0, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15};

_nc_mask = 0;
for (int i=0; i<16; i++) {
_pin[i] = (pins[i] != NC) ? new DigitalOut(pins[i]) : 0;
if (pins[i] != NC) {
_nc_mask |= (1 << i);
}
}
}

BusOut::BusOut(PinName pins[16]) {
_nc_mask = 0;
for (int i=0; i<16; i++) {
_pin[i] = (pins[i] != NC) ? new DigitalOut(pins[i]) : 0;
if (pins[i] != NC) {
_nc_mask |= (1 << i);
}
}
}

Expand Down Expand Up @@ -68,6 +76,12 @@ BusOut& BusOut::operator= (BusOut& rhs) {
return *this;
}

DigitalOut& BusOut::operator[] (int index) {
MBED_ASSERT(index >= 0 && index <= 16);
MBED_ASSERT(_pin[index]);
return *_pin[index];
}

BusOut::operator int() {
return read();
}
Expand Down
6 changes: 6 additions & 0 deletions libraries/mbed/hal/gpio_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ extern "C" {
**/
uint32_t gpio_set(PinName pin);

/* Checks if gpio object is connected (pin was not initialized with NC)
* @param pin The pin to be set as GPIO
* @return 0 if port is initialized with NC
**/
int gpio_is_connected(const gpio_t *obj);

/* GPIO object */
void gpio_init(gpio_t *obj, PinName pin);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ static inline int gpio_read(gpio_t *obj) {
return ((*obj->reg_in & obj->mask) ? 1 : 0);
}

static inline int gpio_is_connected(const gpio_t *obj) {
return obj->pin != (PinName)NC;
}

#ifdef __cplusplus
}
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ static inline int gpio_read(gpio_t *obj) {
return ((*obj->reg_in & obj->mask) ? 1 : 0);
}

static inline int gpio_is_connected(const gpio_t *obj) {
return obj->pin != (PinName)NC;
}

#ifdef __cplusplus
}
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ uint32_t gpio_set(PinName pin) {
}

void gpio_init(gpio_t *obj, PinName pin) {
obj->pinName = pin;
obj->pin = pin;
if (pin == (PinName)NC)
return;

Expand All @@ -42,14 +42,14 @@ void gpio_init(gpio_t *obj, PinName pin) {
}

void gpio_mode(gpio_t *obj, PinMode mode) {
pin_mode(obj->pinName, mode);
pin_mode(obj->pin, mode);
}

void gpio_dir(gpio_t *obj, PinDirection direction) {
MBED_ASSERT(obj->pinName != (PinName)NC);
uint32_t port = obj->pinName >> GPIO_PORT_SHIFT;
MBED_ASSERT(obj->pin != (PinName)NC);
uint32_t port = obj->pin >> GPIO_PORT_SHIFT;
uint32_t gpio_addrs[] = GPIO_BASE_ADDRS;
uint32_t pin_num = obj->pinName & 0xFF;
uint32_t pin_num = obj->pin & 0xFF;

switch (direction) {
case PIN_INPUT:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,31 @@ extern "C" {
#endif

typedef struct {
PinName pinName;
PinName pin;
} gpio_t;

static inline void gpio_write(gpio_t *obj, int value) {
MBED_ASSERT(obj->pinName != (PinName)NC);
uint32_t port = obj->pinName >> GPIO_PORT_SHIFT;
uint32_t pin = obj->pinName & 0xFF;
MBED_ASSERT(obj->pin != (PinName)NC);
uint32_t port = obj->pin >> GPIO_PORT_SHIFT;
uint32_t pin = obj->pin & 0xFF;
uint32_t gpio_addrs[] = GPIO_BASE_ADDRS;

GPIO_HAL_WritePinOutput(gpio_addrs[port], pin, value);
}

static inline int gpio_read(gpio_t *obj) {
MBED_ASSERT(obj->pinName != (PinName)NC);
uint32_t port = obj->pinName >> GPIO_PORT_SHIFT;
uint32_t pin = obj->pinName & 0xFF;
MBED_ASSERT(obj->pin != (PinName)NC);
uint32_t port = obj->pin >> GPIO_PORT_SHIFT;
uint32_t pin = obj->pin & 0xFF;
uint32_t gpio_addrs[] = GPIO_BASE_ADDRS;

return (int)GPIO_HAL_ReadPinInput(gpio_addrs[port], pin);
}

static inline int gpio_is_connected(const gpio_t *obj) {
return obj->pin != (PinName)NC;
}

#ifdef __cplusplus
}
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ static inline int gpio_read(gpio_t *obj) {
return ((*obj->reg_in & obj->mask) ? 1 : 0);
}

static inline int gpio_is_connected(const gpio_t *obj) {
return obj->pin != (PinName)NC;
}

#ifdef __cplusplus
}
#endif
Expand Down
Loading