From b7f4d17aae5faa09513b962dd75d8cdd12bdcbbc Mon Sep 17 00:00:00 2001 From: Przemek Wirkus Date: Fri, 19 Dec 2014 13:14:53 +0000 Subject: [PATCH 01/13] Added new GPIO HAL function gpio_is_connected() used to check if gpio_t is connected or initialized with NC Simple gpio_t structure in TARGET_KPSDK_MCUS field name changed to allign to other HALs --- libraries/mbed/hal/gpio_api.h | 7 +++++++ .../TARGET_KPSDK_MCUS/gpio_object.h | 18 +++++++++++------- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/libraries/mbed/hal/gpio_api.h b/libraries/mbed/hal/gpio_api.h index e4cf7fd6f5..ab5206aa93 100644 --- a/libraries/mbed/hal/gpio_api.h +++ b/libraries/mbed/hal/gpio_api.h @@ -28,6 +28,13 @@ 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 Non zero value if port is connected to pin + * 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); diff --git a/libraries/mbed/targets/hal/TARGET_Freescale/TARGET_KPSDK_MCUS/gpio_object.h b/libraries/mbed/targets/hal/TARGET_Freescale/TARGET_KPSDK_MCUS/gpio_object.h index 780eed9e68..7cdf6662ba 100644 --- a/libraries/mbed/targets/hal/TARGET_Freescale/TARGET_KPSDK_MCUS/gpio_object.h +++ b/libraries/mbed/targets/hal/TARGET_Freescale/TARGET_KPSDK_MCUS/gpio_object.h @@ -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 From a53cd59b51f1244ef21f4ffae13bfb078d280cc8 Mon Sep 17 00:00:00 2001 From: Przemek Wirkus Date: Fri, 19 Dec 2014 13:28:47 +0000 Subject: [PATCH 02/13] Added is_connected() method to Digital I/O classes APIs (DigitalIn, DigitalOut and DigitalInOut --- libraries/mbed/api/DigitalIn.h | 10 ++++++++++ libraries/mbed/api/DigitalInOut.h | 10 ++++++++++ libraries/mbed/api/DigitalOut.h | 10 ++++++++++ libraries/mbed/hal/gpio_api.h | 3 +-- .../hal/TARGET_Freescale/TARGET_KPSDK_MCUS/gpio_api.c | 10 +++++----- 5 files changed, 36 insertions(+), 7 deletions(-) diff --git a/libraries/mbed/api/DigitalIn.h b/libraries/mbed/api/DigitalIn.h index d81038b8cf..b089de9faa 100644 --- a/libraries/mbed/api/DigitalIn.h +++ b/libraries/mbed/api/DigitalIn.h @@ -80,6 +80,16 @@ public: 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() */ diff --git a/libraries/mbed/api/DigitalInOut.h b/libraries/mbed/api/DigitalInOut.h index 5d9221b5dc..e30be0e638 100644 --- a/libraries/mbed/api/DigitalInOut.h +++ b/libraries/mbed/api/DigitalInOut.h @@ -85,6 +85,16 @@ public: 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() */ diff --git a/libraries/mbed/api/DigitalOut.h b/libraries/mbed/api/DigitalOut.h index 0281770ff8..0d66f907b0 100644 --- a/libraries/mbed/api/DigitalOut.h +++ b/libraries/mbed/api/DigitalOut.h @@ -77,6 +77,16 @@ public: 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() */ diff --git a/libraries/mbed/hal/gpio_api.h b/libraries/mbed/hal/gpio_api.h index ab5206aa93..872b547eaa 100644 --- a/libraries/mbed/hal/gpio_api.h +++ b/libraries/mbed/hal/gpio_api.h @@ -30,8 +30,7 @@ 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 Non zero value if port is connected to pin - * 0 if port is initialized with NC + * @return 0 if port is initialized with NC **/ int gpio_is_connected(const gpio_t *obj); diff --git a/libraries/mbed/targets/hal/TARGET_Freescale/TARGET_KPSDK_MCUS/gpio_api.c b/libraries/mbed/targets/hal/TARGET_Freescale/TARGET_KPSDK_MCUS/gpio_api.c index 73259e4dde..cde73ff4fd 100644 --- a/libraries/mbed/targets/hal/TARGET_Freescale/TARGET_KPSDK_MCUS/gpio_api.c +++ b/libraries/mbed/targets/hal/TARGET_Freescale/TARGET_KPSDK_MCUS/gpio_api.c @@ -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; @@ -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: From 6fa4b469f4a4aeffad33200dbcad2478bdb36ed2 Mon Sep 17 00:00:00 2001 From: Przemek Wirkus Date: Fri, 19 Dec 2014 13:37:16 +0000 Subject: [PATCH 03/13] Decorated all HALs with gpio_is_connected() function implementation --- .../targets/hal/TARGET_Freescale/TARGET_K20D50M/gpio_object.h | 4 ++++ .../targets/hal/TARGET_Freescale/TARGET_KLXX/gpio_object.h | 4 ++++ .../hal/TARGET_NORDIC/TARGET_MCU_NRF51822/gpio_object.h | 4 ++++ .../mbed/targets/hal/TARGET_NXP/TARGET_LPC11U6X/gpio_object.h | 4 ++++ .../mbed/targets/hal/TARGET_NXP/TARGET_LPC11UXX/gpio_object.h | 4 ++++ .../targets/hal/TARGET_NXP/TARGET_LPC11XX_11CXX/gpio_object.h | 4 ++++ .../mbed/targets/hal/TARGET_NXP/TARGET_LPC13XX/gpio_object.h | 4 ++++ .../mbed/targets/hal/TARGET_NXP/TARGET_LPC15XX/gpio_object.h | 4 ++++ .../mbed/targets/hal/TARGET_NXP/TARGET_LPC176X/gpio_object.h | 4 ++++ .../mbed/targets/hal/TARGET_NXP/TARGET_LPC23XX/gpio_object.h | 4 ++++ .../mbed/targets/hal/TARGET_NXP/TARGET_LPC408X/gpio_object.h | 4 ++++ .../mbed/targets/hal/TARGET_NXP/TARGET_LPC43XX/gpio_object.h | 4 ++++ .../mbed/targets/hal/TARGET_NXP/TARGET_LPC81X/gpio_object.h | 4 ++++ .../mbed/targets/hal/TARGET_NXP/TARGET_LPC82X/gpio_object.h | 4 ++++ .../targets/hal/TARGET_RENESAS/TARGET_RZ_A1H/gpio_object.h | 4 ++++ .../targets/hal/TARGET_STM/TARGET_DISCO_F051R8/gpio_object.h | 4 ++++ .../targets/hal/TARGET_STM/TARGET_DISCO_F100RB/gpio_object.h | 4 ++++ .../targets/hal/TARGET_STM/TARGET_DISCO_F303VC/gpio_object.h | 4 ++++ .../targets/hal/TARGET_STM/TARGET_DISCO_F334C8/gpio_object.h | 4 ++++ .../targets/hal/TARGET_STM/TARGET_DISCO_F429ZI/gpio_object.h | 4 ++++ .../targets/hal/TARGET_STM/TARGET_DISCO_L053C8/gpio_object.h | 4 ++++ .../hal/TARGET_STM/TARGET_MTS_MDOT_F405RG/gpio_object.h | 4 ++++ .../targets/hal/TARGET_STM/TARGET_NUCLEO_F030R8/gpio_object.h | 4 ++++ .../targets/hal/TARGET_STM/TARGET_NUCLEO_F072RB/gpio_object.h | 4 ++++ .../targets/hal/TARGET_STM/TARGET_NUCLEO_F091RC/gpio_object.h | 4 ++++ 25 files changed, 100 insertions(+) diff --git a/libraries/mbed/targets/hal/TARGET_Freescale/TARGET_K20D50M/gpio_object.h b/libraries/mbed/targets/hal/TARGET_Freescale/TARGET_K20D50M/gpio_object.h index ca2c0d64f8..b17219da0f 100644 --- a/libraries/mbed/targets/hal/TARGET_Freescale/TARGET_K20D50M/gpio_object.h +++ b/libraries/mbed/targets/hal/TARGET_Freescale/TARGET_K20D50M/gpio_object.h @@ -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 diff --git a/libraries/mbed/targets/hal/TARGET_Freescale/TARGET_KLXX/gpio_object.h b/libraries/mbed/targets/hal/TARGET_Freescale/TARGET_KLXX/gpio_object.h index 16af304736..fe6d6c1e05 100644 --- a/libraries/mbed/targets/hal/TARGET_Freescale/TARGET_KLXX/gpio_object.h +++ b/libraries/mbed/targets/hal/TARGET_Freescale/TARGET_KLXX/gpio_object.h @@ -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 diff --git a/libraries/mbed/targets/hal/TARGET_NORDIC/TARGET_MCU_NRF51822/gpio_object.h b/libraries/mbed/targets/hal/TARGET_NORDIC/TARGET_MCU_NRF51822/gpio_object.h index 16af304736..fe6d6c1e05 100644 --- a/libraries/mbed/targets/hal/TARGET_NORDIC/TARGET_MCU_NRF51822/gpio_object.h +++ b/libraries/mbed/targets/hal/TARGET_NORDIC/TARGET_MCU_NRF51822/gpio_object.h @@ -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 diff --git a/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC11U6X/gpio_object.h b/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC11U6X/gpio_object.h index 16af304736..fe6d6c1e05 100644 --- a/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC11U6X/gpio_object.h +++ b/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC11U6X/gpio_object.h @@ -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 diff --git a/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC11UXX/gpio_object.h b/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC11UXX/gpio_object.h index 16af304736..fe6d6c1e05 100644 --- a/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC11UXX/gpio_object.h +++ b/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC11UXX/gpio_object.h @@ -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 diff --git a/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC11XX_11CXX/gpio_object.h b/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC11XX_11CXX/gpio_object.h index 2d93b59850..f295911aaa 100644 --- a/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC11XX_11CXX/gpio_object.h +++ b/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC11XX_11CXX/gpio_object.h @@ -43,6 +43,10 @@ static inline int gpio_read(gpio_t *obj) { return ((*obj->reg_mask_read) ? 1 : 0); } +static inline int gpio_is_connected(const gpio_t *obj) { + return obj->pin != (PinName)NC; +} + #ifdef __cplusplus } #endif diff --git a/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC13XX/gpio_object.h b/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC13XX/gpio_object.h index 16af304736..fe6d6c1e05 100644 --- a/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC13XX/gpio_object.h +++ b/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC13XX/gpio_object.h @@ -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 diff --git a/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC15XX/gpio_object.h b/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC15XX/gpio_object.h index 35ef92f18e..0252448103 100644 --- a/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC15XX/gpio_object.h +++ b/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC15XX/gpio_object.h @@ -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 diff --git a/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC176X/gpio_object.h b/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC176X/gpio_object.h index 16af304736..fe6d6c1e05 100644 --- a/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC176X/gpio_object.h +++ b/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC176X/gpio_object.h @@ -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 diff --git a/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC23XX/gpio_object.h b/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC23XX/gpio_object.h index 16af304736..fe6d6c1e05 100644 --- a/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC23XX/gpio_object.h +++ b/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC23XX/gpio_object.h @@ -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 diff --git a/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC408X/gpio_object.h b/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC408X/gpio_object.h index 16af304736..fe6d6c1e05 100644 --- a/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC408X/gpio_object.h +++ b/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC408X/gpio_object.h @@ -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 diff --git a/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC43XX/gpio_object.h b/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC43XX/gpio_object.h index 16af304736..fe6d6c1e05 100644 --- a/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC43XX/gpio_object.h +++ b/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC43XX/gpio_object.h @@ -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 diff --git a/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC81X/gpio_object.h b/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC81X/gpio_object.h index 16af304736..fe6d6c1e05 100644 --- a/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC81X/gpio_object.h +++ b/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC81X/gpio_object.h @@ -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 diff --git a/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC82X/gpio_object.h b/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC82X/gpio_object.h index 75d9291620..eac21ab67e 100644 --- a/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC82X/gpio_object.h +++ b/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC82X/gpio_object.h @@ -47,6 +47,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 diff --git a/libraries/mbed/targets/hal/TARGET_RENESAS/TARGET_RZ_A1H/gpio_object.h b/libraries/mbed/targets/hal/TARGET_RENESAS/TARGET_RZ_A1H/gpio_object.h index 6eb8d80402..c87a2aec1b 100644 --- a/libraries/mbed/targets/hal/TARGET_RENESAS/TARGET_RZ_A1H/gpio_object.h +++ b/libraries/mbed/targets/hal/TARGET_RENESAS/TARGET_RZ_A1H/gpio_object.h @@ -41,6 +41,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 diff --git a/libraries/mbed/targets/hal/TARGET_STM/TARGET_DISCO_F051R8/gpio_object.h b/libraries/mbed/targets/hal/TARGET_STM/TARGET_DISCO_F051R8/gpio_object.h index fdc6112cb6..684d968757 100644 --- a/libraries/mbed/targets/hal/TARGET_STM/TARGET_DISCO_F051R8/gpio_object.h +++ b/libraries/mbed/targets/hal/TARGET_STM/TARGET_DISCO_F051R8/gpio_object.h @@ -64,6 +64,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 diff --git a/libraries/mbed/targets/hal/TARGET_STM/TARGET_DISCO_F100RB/gpio_object.h b/libraries/mbed/targets/hal/TARGET_STM/TARGET_DISCO_F100RB/gpio_object.h index d3142d5c71..d97ee5bf12 100644 --- a/libraries/mbed/targets/hal/TARGET_STM/TARGET_DISCO_F100RB/gpio_object.h +++ b/libraries/mbed/targets/hal/TARGET_STM/TARGET_DISCO_F100RB/gpio_object.h @@ -63,6 +63,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 diff --git a/libraries/mbed/targets/hal/TARGET_STM/TARGET_DISCO_F303VC/gpio_object.h b/libraries/mbed/targets/hal/TARGET_STM/TARGET_DISCO_F303VC/gpio_object.h index 5569efc465..bebf7db0c7 100644 --- a/libraries/mbed/targets/hal/TARGET_STM/TARGET_DISCO_F303VC/gpio_object.h +++ b/libraries/mbed/targets/hal/TARGET_STM/TARGET_DISCO_F303VC/gpio_object.h @@ -64,6 +64,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 diff --git a/libraries/mbed/targets/hal/TARGET_STM/TARGET_DISCO_F334C8/gpio_object.h b/libraries/mbed/targets/hal/TARGET_STM/TARGET_DISCO_F334C8/gpio_object.h index 5569efc465..bebf7db0c7 100644 --- a/libraries/mbed/targets/hal/TARGET_STM/TARGET_DISCO_F334C8/gpio_object.h +++ b/libraries/mbed/targets/hal/TARGET_STM/TARGET_DISCO_F334C8/gpio_object.h @@ -64,6 +64,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 diff --git a/libraries/mbed/targets/hal/TARGET_STM/TARGET_DISCO_F429ZI/gpio_object.h b/libraries/mbed/targets/hal/TARGET_STM/TARGET_DISCO_F429ZI/gpio_object.h index 5569efc465..bebf7db0c7 100644 --- a/libraries/mbed/targets/hal/TARGET_STM/TARGET_DISCO_F429ZI/gpio_object.h +++ b/libraries/mbed/targets/hal/TARGET_STM/TARGET_DISCO_F429ZI/gpio_object.h @@ -64,6 +64,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 diff --git a/libraries/mbed/targets/hal/TARGET_STM/TARGET_DISCO_L053C8/gpio_object.h b/libraries/mbed/targets/hal/TARGET_STM/TARGET_DISCO_L053C8/gpio_object.h index 75013b4188..4391135e3b 100644 --- a/libraries/mbed/targets/hal/TARGET_STM/TARGET_DISCO_L053C8/gpio_object.h +++ b/libraries/mbed/targets/hal/TARGET_STM/TARGET_DISCO_L053C8/gpio_object.h @@ -62,6 +62,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 diff --git a/libraries/mbed/targets/hal/TARGET_STM/TARGET_MTS_MDOT_F405RG/gpio_object.h b/libraries/mbed/targets/hal/TARGET_STM/TARGET_MTS_MDOT_F405RG/gpio_object.h index a8c99da3b6..1efcb162cd 100644 --- a/libraries/mbed/targets/hal/TARGET_STM/TARGET_MTS_MDOT_F405RG/gpio_object.h +++ b/libraries/mbed/targets/hal/TARGET_STM/TARGET_MTS_MDOT_F405RG/gpio_object.h @@ -62,6 +62,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 diff --git a/libraries/mbed/targets/hal/TARGET_STM/TARGET_NUCLEO_F030R8/gpio_object.h b/libraries/mbed/targets/hal/TARGET_STM/TARGET_NUCLEO_F030R8/gpio_object.h index fdc6112cb6..684d968757 100644 --- a/libraries/mbed/targets/hal/TARGET_STM/TARGET_NUCLEO_F030R8/gpio_object.h +++ b/libraries/mbed/targets/hal/TARGET_STM/TARGET_NUCLEO_F030R8/gpio_object.h @@ -64,6 +64,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 diff --git a/libraries/mbed/targets/hal/TARGET_STM/TARGET_NUCLEO_F072RB/gpio_object.h b/libraries/mbed/targets/hal/TARGET_STM/TARGET_NUCLEO_F072RB/gpio_object.h index fdc6112cb6..684d968757 100644 --- a/libraries/mbed/targets/hal/TARGET_STM/TARGET_NUCLEO_F072RB/gpio_object.h +++ b/libraries/mbed/targets/hal/TARGET_STM/TARGET_NUCLEO_F072RB/gpio_object.h @@ -64,6 +64,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 diff --git a/libraries/mbed/targets/hal/TARGET_STM/TARGET_NUCLEO_F091RC/gpio_object.h b/libraries/mbed/targets/hal/TARGET_STM/TARGET_NUCLEO_F091RC/gpio_object.h index fdc6112cb6..684d968757 100644 --- a/libraries/mbed/targets/hal/TARGET_STM/TARGET_NUCLEO_F091RC/gpio_object.h +++ b/libraries/mbed/targets/hal/TARGET_STM/TARGET_NUCLEO_F091RC/gpio_object.h @@ -64,6 +64,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 From c4fc8e68ebb3b24c125cb46bd91f93b23c5be49a Mon Sep 17 00:00:00 2001 From: Przemek Wirkus Date: Fri, 19 Dec 2014 14:06:46 +0000 Subject: [PATCH 04/13] Added operator[] for BusIn and BusOut to add access to particular bit in random-iterator fashion --- libraries/mbed/api/BusIn.h | 6 ++++++ libraries/mbed/api/BusOut.h | 6 ++++++ libraries/mbed/common/BusIn.cpp | 12 ++++++++++++ libraries/mbed/common/BusOut.cpp | 11 +++++++++++ 4 files changed, 35 insertions(+) diff --git a/libraries/mbed/api/BusIn.h b/libraries/mbed/api/BusIn.h index c5ece1df68..92c9eeb5a6 100644 --- a/libraries/mbed/api/BusIn.h +++ b/libraries/mbed/api/BusIn.h @@ -58,10 +58,16 @@ public: */ void mode(PinMode pull); + static DigitalIn din_dummy; + #ifdef MBED_OPERATORS /** A shorthand for read() */ operator int(); + + /** Access to particular bit in random-iterator fashion + */ + DigitalIn & operator[] (unsigned int index); #endif protected: diff --git a/libraries/mbed/api/BusOut.h b/libraries/mbed/api/BusOut.h index 9a6608e89e..4212c76637 100644 --- a/libraries/mbed/api/BusOut.h +++ b/libraries/mbed/api/BusOut.h @@ -56,12 +56,18 @@ public: */ int read(); + static DigitalOut dout_dummy; + #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[] (unsigned int index); + /** A shorthand for read() */ operator int(); diff --git a/libraries/mbed/common/BusIn.cpp b/libraries/mbed/common/BusIn.cpp index c1eb3dbdb1..895003c409 100644 --- a/libraries/mbed/common/BusIn.cpp +++ b/libraries/mbed/common/BusIn.cpp @@ -17,6 +17,8 @@ namespace mbed { +DigitalIn BusIn::din_dummy(NC); + 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}; @@ -61,6 +63,16 @@ void BusIn::mode(PinMode pull) { BusIn::operator int() { return read(); } + +DigitalIn& BusIn::operator[] (unsigned int index) { + //MBED_ASSERT(index >= MBED_BUS_SIZE); + //MBED_ASSERT(_pin[index]); + if (index >= 16 || _pin[index] == NULL) { + return din_dummy; + } + return *_pin[index]; +} + #endif } // namespace mbed diff --git a/libraries/mbed/common/BusOut.cpp b/libraries/mbed/common/BusOut.cpp index 7e9a5f7dbe..6cf4f3c605 100644 --- a/libraries/mbed/common/BusOut.cpp +++ b/libraries/mbed/common/BusOut.cpp @@ -17,6 +17,8 @@ namespace mbed { +DigitalOut BusOut::dout_dummy(NC); + 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}; @@ -68,6 +70,15 @@ BusOut& BusOut::operator= (BusOut& rhs) { return *this; } +DigitalOut& BusOut::operator[] (unsigned int index) { + //MBED_ASSERT(index >= MBED_BUS_SIZE); + //MBED_ASSERT(_pin[index]); + if (index >= 16 || _pin[index] == NULL) { + return dout_dummy; + } + return *_pin[index]; +} + BusOut::operator int() { return read(); } From db7e92853bd4dbb3940af973564c59c80e3de4b8 Mon Sep 17 00:00:00 2001 From: Przemek Wirkus Date: Fri, 19 Dec 2014 14:37:00 +0000 Subject: [PATCH 05/13] Added mask() function to BusIn and BusOut components You can use BusIn::mask() or BusOut::mask() to get binary mask of all connected and NC pins in bus --- libraries/mbed/api/BusIn.h | 14 ++++++++++++++ libraries/mbed/api/BusOut.h | 14 ++++++++++++++ libraries/mbed/common/BusIn.cpp | 8 ++++++++ libraries/mbed/common/BusOut.cpp | 8 ++++++++ 4 files changed, 44 insertions(+) diff --git a/libraries/mbed/api/BusIn.h b/libraries/mbed/api/BusIn.h index 92c9eeb5a6..6ef2e66c7d 100644 --- a/libraries/mbed/api/BusIn.h +++ b/libraries/mbed/api/BusIn.h @@ -58,6 +58,16 @@ public: */ 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; + } + static DigitalIn din_dummy; #ifdef MBED_OPERATORS @@ -73,6 +83,10 @@ public: protected: DigitalIn* _pin[16]; + /** Mask of NC pins, if bit [n] bit is set to 1, [n] pin in bus is in NC state + */ + int _nc_mask; + /* disallow copy constructor and assignment operators */ private: BusIn(const BusIn&); diff --git a/libraries/mbed/api/BusOut.h b/libraries/mbed/api/BusOut.h index 4212c76637..27211d8b8a 100644 --- a/libraries/mbed/api/BusOut.h +++ b/libraries/mbed/api/BusOut.h @@ -56,6 +56,16 @@ public: */ 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; + } + static DigitalOut dout_dummy; #ifdef MBED_OPERATORS @@ -76,6 +86,10 @@ public: protected: DigitalOut* _pin[16]; + /** Mask of NC pins, if bit [n] bit is set to 1, [n] pin in bus is in NC state + */ + int _nc_mask; + /* disallow copy constructor and assignment operators */ private: BusOut(const BusOut&); diff --git a/libraries/mbed/common/BusIn.cpp b/libraries/mbed/common/BusIn.cpp index 895003c409..2858ccc2ea 100644 --- a/libraries/mbed/common/BusIn.cpp +++ b/libraries/mbed/common/BusIn.cpp @@ -22,14 +22,22 @@ DigitalIn BusIn::din_dummy(NC); 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); + } } } diff --git a/libraries/mbed/common/BusOut.cpp b/libraries/mbed/common/BusOut.cpp index 6cf4f3c605..445b3f8658 100644 --- a/libraries/mbed/common/BusOut.cpp +++ b/libraries/mbed/common/BusOut.cpp @@ -22,14 +22,22 @@ DigitalOut BusOut::dout_dummy(NC); 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); + } } } From 7720989e2096a9d70476c0fcc0ff710b13637e5e Mon Sep 17 00:00:00 2001 From: Przemek Wirkus Date: Fri, 19 Dec 2014 14:45:32 +0000 Subject: [PATCH 06/13] Added operator[] and mask() function to BusInOut This change follows changes in BUsIn and BusOUt API --- libraries/mbed/api/BusInOut.h | 17 +++++++++++++++++ libraries/mbed/common/BusInOut.cpp | 20 ++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/libraries/mbed/api/BusInOut.h b/libraries/mbed/api/BusInOut.h index 63dfda1606..50e8da5258 100644 --- a/libraries/mbed/api/BusInOut.h +++ b/libraries/mbed/api/BusInOut.h @@ -73,6 +73,18 @@ public: */ 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; + } + + static DigitalInOut dinout_dummy; + #ifdef MBED_OPERATORS /** A shorthand for write() */ @@ -87,10 +99,15 @@ public: protected: DigitalInOut* _pin[16]; + /** Mask of NC pins, if bit [n] bit is set to 1, [n] pin in bus is in NC state + */ + int _nc_mask; + /* disallow copy constructor and assignment operators */ private: BusInOut(const BusInOut&); BusInOut & operator = (const BusInOut&); + DigitalInOut& operator[] (unsigned int index); }; } // namespace mbed diff --git a/libraries/mbed/common/BusInOut.cpp b/libraries/mbed/common/BusInOut.cpp index 84733a40cc..12c5063c1e 100644 --- a/libraries/mbed/common/BusInOut.cpp +++ b/libraries/mbed/common/BusInOut.cpp @@ -17,17 +17,27 @@ namespace mbed { +DigitalInOut BusInOut::dinout_dummy(NC); + 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); + } } } @@ -92,6 +102,16 @@ BusInOut& BusInOut::operator= (BusInOut& rhs) { return *this; } +DigitalInOut& BusInOut::operator[] (unsigned int index) { + //MBED_ASSERT(index >= MBED_BUS_SIZE); + //MBED_ASSERT(_pin[index]); + if (index >= 16 || _pin[index] == NULL) { + return dinout_dummy; + } + return *_pin[index]; +} + + BusInOut::operator int() { return read(); } From 11c5955959be22334ab9f28d77142fe621143175 Mon Sep 17 00:00:00 2001 From: Przemek Wirkus Date: Fri, 19 Dec 2014 14:48:39 +0000 Subject: [PATCH 07/13] Simple indent --- libraries/mbed/api/BusInOut.h | 1 - 1 file changed, 1 deletion(-) diff --git a/libraries/mbed/api/BusInOut.h b/libraries/mbed/api/BusInOut.h index 50e8da5258..919366aedb 100644 --- a/libraries/mbed/api/BusInOut.h +++ b/libraries/mbed/api/BusInOut.h @@ -51,7 +51,6 @@ public: */ void write(int value); - /** Read the value currently output on the bus * * @returns From 8690af3b7bc94289210d6303a71af6932beb6db3 Mon Sep 17 00:00:00 2001 From: Przemek Wirkus Date: Fri, 19 Dec 2014 14:53:35 +0000 Subject: [PATCH 08/13] Simple indent --- libraries/mbed/common/BusInOut.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/libraries/mbed/common/BusInOut.cpp b/libraries/mbed/common/BusInOut.cpp index 12c5063c1e..e3a1182aba 100644 --- a/libraries/mbed/common/BusInOut.cpp +++ b/libraries/mbed/common/BusInOut.cpp @@ -111,7 +111,6 @@ DigitalInOut& BusInOut::operator[] (unsigned int index) { return *_pin[index]; } - BusInOut::operator int() { return read(); } From 56e7514495c2f437894623f640e4f5fa9fc7051d Mon Sep 17 00:00:00 2001 From: Przemek Wirkus Date: Fri, 19 Dec 2014 14:59:06 +0000 Subject: [PATCH 09/13] Modiffied _ns_mack member's comment in Bus classes (BusIn, BusOUt, BusInOut) --- libraries/mbed/api/BusIn.h | 4 +++- libraries/mbed/api/BusInOut.h | 4 +++- libraries/mbed/api/BusOut.h | 4 +++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/libraries/mbed/api/BusIn.h b/libraries/mbed/api/BusIn.h index 6ef2e66c7d..837a71bcec 100644 --- a/libraries/mbed/api/BusIn.h +++ b/libraries/mbed/api/BusIn.h @@ -83,7 +83,9 @@ public: protected: DigitalIn* _pin[16]; - /** Mask of NC pins, if bit [n] bit is set to 1, [n] pin in bus is in NC state + /** 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; diff --git a/libraries/mbed/api/BusInOut.h b/libraries/mbed/api/BusInOut.h index 919366aedb..9d3c38154d 100644 --- a/libraries/mbed/api/BusInOut.h +++ b/libraries/mbed/api/BusInOut.h @@ -98,7 +98,9 @@ public: protected: DigitalInOut* _pin[16]; - /** Mask of NC pins, if bit [n] bit is set to 1, [n] pin in bus is in NC state + /** 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; diff --git a/libraries/mbed/api/BusOut.h b/libraries/mbed/api/BusOut.h index 27211d8b8a..b979b3f16a 100644 --- a/libraries/mbed/api/BusOut.h +++ b/libraries/mbed/api/BusOut.h @@ -86,7 +86,9 @@ public: protected: DigitalOut* _pin[16]; - /** Mask of NC pins, if bit [n] bit is set to 1, [n] pin in bus is in NC state + /** 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; From 32cea97577242466f4e4ccbf8860699c797c340d Mon Sep 17 00:00:00 2001 From: Przemek Wirkus Date: Fri, 19 Dec 2014 15:36:55 +0000 Subject: [PATCH 10/13] Added simple tests for BusOut component to check if we can use new features of BusOut::operator[] and DigitalOut::is_connected() --- libraries/tests/mbed/bus_out/main.cpp | 75 +++++++++++++++++++++ libraries/tests/utest/bus/busout_ut.cpp | 87 +++++++++++++++++++++++++ workspace_tools/tests.py | 16 ++++- 3 files changed, 177 insertions(+), 1 deletion(-) create mode 100644 libraries/tests/mbed/bus_out/main.cpp create mode 100644 libraries/tests/utest/bus/busout_ut.cpp diff --git a/libraries/tests/mbed/bus_out/main.cpp b/libraries/tests/mbed/bus_out/main.cpp new file mode 100644 index 0000000000..4f14348604 --- /dev/null +++ b/libraries/tests/mbed/bus_out/main.cpp @@ -0,0 +1,75 @@ +#include "mbed.h" +#include "test_env.h" + +BusOut bus_out(LED1, LED2, LED3, LED4); + +int main() +{ + notify_start(); + + bool result = false; + + for (;;) { + const int mask = bus_out.mask(); + int led_mask = 0x00; + if (LED1 != NC) led_mask |= 0x01; + if (LED2 != NC) led_mask |= 0x02; + if (LED3 != NC) led_mask |= 0x04; + if (LED4 != NC) led_mask |= 0x08; + + printf("MBED: BusIn mask: 0x%X\r\n", mask); + printf("MBED: BusIn LED mask: 0x%X\r\n", led_mask); + + // Let's check bus's connected pins mask + if (mask != led_mask) { + break; + } + + // Checking if DigitalOut is correctly set as connected + for (int i=0; i<4; i++) { + printf("MBED: BusOut.bit[%d] is %s\r\n", i, bus_out[i].is_connected() ? "connected" : "not connected"); + } + + if (LED1 != NC && bus_out[0].is_connected() == 0) { + break; + } + if (LED1 != NC && bus_out[1].is_connected() == 0) { + break; + } + if (LED1 != NC && bus_out[2].is_connected() == 0) { + break; + } + if (LED1 != NC && bus_out[3].is_connected() == 0) { + break; + } + + // Write mask all LEDs + bus_out.write(mask); // Set all LED's pins in high state + if (bus_out.read() != mask) { + break; + } + // Zero all LEDs and see if mask is correctly cleared on all bits + bus_out.write(~mask); + if (bus_out.read() != 0x00) { + break; + } + + result = true; + break; + } + + printf("MBED: Blinking LEDs...\r\n"); + + // Just a quick LED blinking... + for (int i=0; i<4; i++) { + if (bus_out[i].is_connected()) { + bus_out[i] = 1; + } + wait(0.2); + if (bus_out[i].is_connected()) { + bus_out[i] = 0; + } + } + + notify_completion(result); +} diff --git a/libraries/tests/utest/bus/busout_ut.cpp b/libraries/tests/utest/bus/busout_ut.cpp new file mode 100644 index 0000000000..d7f2a408df --- /dev/null +++ b/libraries/tests/utest/bus/busout_ut.cpp @@ -0,0 +1,87 @@ +#include "TestHarness.h" +#include +#include "mbed.h" + +TEST_GROUP(BusOut_mask) +{ +}; + +TEST(BusOut_mask, led_1_2_3) +{ + BusOut bus_data(LED1, LED2, LED3); + CHECK_EQUAL(0x07, bus_data.mask()); +} + +TEST(BusOut_mask, led_nc_nc_nc_nc) +{ + BusOut bus_data(NC, NC, NC, NC); + CHECK_EQUAL(0x00, bus_data.mask()); +} + +TEST(BusOut_mask, led_1_2_3_nc_nc) +{ + BusOut bus_data(LED1, LED2, LED3, NC, NC); + CHECK_EQUAL(0x07, bus_data.mask()); +} + +TEST(BusOut_mask, led_1_nc_2_nc_nc_3) +{ + BusOut bus_data(LED1, NC, LED2, NC, NC, LED3); + CHECK_EQUAL(0x25, bus_data.mask()); +} + +/////////////////////////////////////////////////////////////////////////////// + +TEST_GROUP(BusOut_dummy) +{ +}; + +TEST(BusOut_dummy, dummy) +{ +} + +#ifdef MBED_OPERATORS +TEST_GROUP(BusOut_digitalout_write) +{ +}; + +TEST(BusOut_digitalout_write, led_nc) +{ + BusOut bus_data(NC); + CHECK_EQUAL(false, bus_data[0].is_connected()) +} + + +TEST(BusOut_digitalout_write, led_1_2_3) +{ + BusOut bus_data(LED1, LED2, LED3); + bus_data[0].write(1); + bus_data[1].write(1); + bus_data[2].write(1); + CHECK(bus_data[0].read()); + CHECK(bus_data[1].read()); + CHECK(bus_data[2].read()); +} + +TEST(BusOut_digitalout_write, led_1_2_3_nc_nc) +{ + BusOut bus_data(LED1, LED2, LED3, NC, NC); + bus_data[0].write(0); + bus_data[1].write(0); + bus_data[2].write(0); + CHECK(bus_data[0].read() == 0); + CHECK(bus_data[1].read() == 0); + CHECK(bus_data[2].read() == 0); +} + +TEST(BusOut_digitalout_write, led_1_nc_2_nc_nc_3) +{ + BusOut bus_data(LED1, NC, LED2, NC, NC, LED3); + bus_data[0].write(1); + bus_data[2].write(0); + bus_data[5].write(0); + CHECK(bus_data[0].read()); + CHECK(bus_data[2].read() == 0); + CHECK(bus_data[5].read() == 0); +} +#endif diff --git a/workspace_tools/tests.py b/workspace_tools/tests.py index dd8ed79c90..e6d27cbbda 100644 --- a/workspace_tools/tests.py +++ b/workspace_tools/tests.py @@ -262,6 +262,14 @@ TESTS = [ "duration": 15, }, + { + "id": "MBED_BUSOUT", "description": "BusOut", + "source_dir": join(TEST_DIR, "mbed", "bus_out"), + "dependencies": [MBED_LIBRARIES, TEST_MBED_LIB], + "automated": True, + "duration": 15, + }, + # Size benchmarks { "id": "BENCHMARK_1", "description": "Size (c environment)", @@ -527,7 +535,7 @@ TESTS = [ "automated": True, "host_test": "wait_us_auto" }, - + # CMSIS RTOS tests { @@ -899,6 +907,12 @@ TESTS = [ "dependencies": [MBED_LIBRARIES, TEST_MBED_LIB, CPPUTEST_LIBRARY], "automated": False, }, + { + "id": "UT_BUSIO", "description": "BusIn BusOut", + "source_dir": join(TEST_DIR, "utest", "bus"), + "dependencies": [MBED_LIBRARIES, TEST_MBED_LIB, CPPUTEST_LIBRARY], + "automated": False, + }, # Tests used for target information purposes { From 7d54c823316f24297cd20092e8bc1305d591b351 Mon Sep 17 00:00:00 2001 From: Przemek Wirkus Date: Tue, 6 Jan 2015 08:32:16 +0000 Subject: [PATCH 11/13] * Changed Bus operator[]() parameter from unsigned int to int to match mbed code guidelines. * Uncommented assertions in operators and added check for operator[] index < 0. * Moved one operator from private to public, this was a typo thing. --- libraries/mbed/api/BusIn.h | 2 +- libraries/mbed/api/BusInOut.h | 5 ++++- libraries/mbed/api/BusOut.h | 2 +- libraries/mbed/common/BusIn.cpp | 6 +++--- libraries/mbed/common/BusInOut.cpp | 6 +++--- libraries/mbed/common/BusOut.cpp | 6 +++--- 6 files changed, 15 insertions(+), 12 deletions(-) diff --git a/libraries/mbed/api/BusIn.h b/libraries/mbed/api/BusIn.h index 837a71bcec..6f20875fa4 100644 --- a/libraries/mbed/api/BusIn.h +++ b/libraries/mbed/api/BusIn.h @@ -77,7 +77,7 @@ public: /** Access to particular bit in random-iterator fashion */ - DigitalIn & operator[] (unsigned int index); + DigitalIn & operator[] (int index); #endif protected: diff --git a/libraries/mbed/api/BusInOut.h b/libraries/mbed/api/BusInOut.h index 9d3c38154d..e9a12c5f92 100644 --- a/libraries/mbed/api/BusInOut.h +++ b/libraries/mbed/api/BusInOut.h @@ -90,6 +90,10 @@ public: 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(); @@ -108,7 +112,6 @@ protected: private: BusInOut(const BusInOut&); BusInOut & operator = (const BusInOut&); - DigitalInOut& operator[] (unsigned int index); }; } // namespace mbed diff --git a/libraries/mbed/api/BusOut.h b/libraries/mbed/api/BusOut.h index b979b3f16a..9e88e2d170 100644 --- a/libraries/mbed/api/BusOut.h +++ b/libraries/mbed/api/BusOut.h @@ -76,7 +76,7 @@ public: /** Access to particular bit in random-iterator fashion */ - DigitalOut& operator[] (unsigned int index); + DigitalOut& operator[] (int index); /** A shorthand for read() */ diff --git a/libraries/mbed/common/BusIn.cpp b/libraries/mbed/common/BusIn.cpp index 2858ccc2ea..dbecf4a9a7 100644 --- a/libraries/mbed/common/BusIn.cpp +++ b/libraries/mbed/common/BusIn.cpp @@ -72,9 +72,9 @@ BusIn::operator int() { return read(); } -DigitalIn& BusIn::operator[] (unsigned int index) { - //MBED_ASSERT(index >= MBED_BUS_SIZE); - //MBED_ASSERT(_pin[index]); +DigitalIn& BusIn::operator[] (int index) { + MBED_ASSERT(index < 0 || index >= MBED_BUS_SIZE); + MBED_ASSERT(_pin[index]); if (index >= 16 || _pin[index] == NULL) { return din_dummy; } diff --git a/libraries/mbed/common/BusInOut.cpp b/libraries/mbed/common/BusInOut.cpp index e3a1182aba..d4ed0bda12 100644 --- a/libraries/mbed/common/BusInOut.cpp +++ b/libraries/mbed/common/BusInOut.cpp @@ -102,9 +102,9 @@ BusInOut& BusInOut::operator= (BusInOut& rhs) { return *this; } -DigitalInOut& BusInOut::operator[] (unsigned int index) { - //MBED_ASSERT(index >= MBED_BUS_SIZE); - //MBED_ASSERT(_pin[index]); +DigitalInOut& BusInOut::operator[] (int index) { + MBED_ASSERT(index < 0 || index >= MBED_BUS_SIZE); + MBED_ASSERT(_pin[index]); if (index >= 16 || _pin[index] == NULL) { return dinout_dummy; } diff --git a/libraries/mbed/common/BusOut.cpp b/libraries/mbed/common/BusOut.cpp index 445b3f8658..9d94a4c9d7 100644 --- a/libraries/mbed/common/BusOut.cpp +++ b/libraries/mbed/common/BusOut.cpp @@ -78,9 +78,9 @@ BusOut& BusOut::operator= (BusOut& rhs) { return *this; } -DigitalOut& BusOut::operator[] (unsigned int index) { - //MBED_ASSERT(index >= MBED_BUS_SIZE); - //MBED_ASSERT(_pin[index]); +DigitalOut& BusOut::operator[] (int index) { + MBED_ASSERT(index < 0 || index >= MBED_BUS_SIZE); + MBED_ASSERT(_pin[index]); if (index >= 16 || _pin[index] == NULL) { return dout_dummy; } From d068a2b4794a08636fed04ea139c910082dbf1fe Mon Sep 17 00:00:00 2001 From: Przemek Wirkus Date: Tue, 6 Jan 2015 08:42:49 +0000 Subject: [PATCH 12/13] Replaced const define with hardcoded bus size of 16 --- libraries/mbed/common/BusIn.cpp | 2 +- libraries/mbed/common/BusInOut.cpp | 2 +- libraries/mbed/common/BusOut.cpp | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/libraries/mbed/common/BusIn.cpp b/libraries/mbed/common/BusIn.cpp index dbecf4a9a7..5ec8cbb8f2 100644 --- a/libraries/mbed/common/BusIn.cpp +++ b/libraries/mbed/common/BusIn.cpp @@ -73,7 +73,7 @@ BusIn::operator int() { } DigitalIn& BusIn::operator[] (int index) { - MBED_ASSERT(index < 0 || index >= MBED_BUS_SIZE); + MBED_ASSERT(index < 0 || index >= 16); MBED_ASSERT(_pin[index]); if (index >= 16 || _pin[index] == NULL) { return din_dummy; diff --git a/libraries/mbed/common/BusInOut.cpp b/libraries/mbed/common/BusInOut.cpp index d4ed0bda12..bd8bc82726 100644 --- a/libraries/mbed/common/BusInOut.cpp +++ b/libraries/mbed/common/BusInOut.cpp @@ -103,7 +103,7 @@ BusInOut& BusInOut::operator= (BusInOut& rhs) { } DigitalInOut& BusInOut::operator[] (int index) { - MBED_ASSERT(index < 0 || index >= MBED_BUS_SIZE); + MBED_ASSERT(index < 0 || index >= 16); MBED_ASSERT(_pin[index]); if (index >= 16 || _pin[index] == NULL) { return dinout_dummy; diff --git a/libraries/mbed/common/BusOut.cpp b/libraries/mbed/common/BusOut.cpp index 9d94a4c9d7..86b2acb29a 100644 --- a/libraries/mbed/common/BusOut.cpp +++ b/libraries/mbed/common/BusOut.cpp @@ -79,7 +79,7 @@ BusOut& BusOut::operator= (BusOut& rhs) { } DigitalOut& BusOut::operator[] (int index) { - MBED_ASSERT(index < 0 || index >= MBED_BUS_SIZE); + MBED_ASSERT(index < 0 || index >= 16); MBED_ASSERT(_pin[index]); if (index >= 16 || _pin[index] == NULL) { return dout_dummy; From 8c508265623e341e1147efa58d44b57d13c6ec86 Mon Sep 17 00:00:00 2001 From: Przemek Wirkus Date: Mon, 26 Jan 2015 08:47:44 +0000 Subject: [PATCH 13/13] Removed extra checks for operator[] and replaced them with MBED_ASSERT for cleaner code --- libraries/mbed/api/BusIn.h | 2 -- libraries/mbed/api/BusInOut.h | 2 -- libraries/mbed/api/BusOut.h | 2 -- libraries/mbed/common/BusIn.cpp | 7 +---- libraries/mbed/common/BusInOut.cpp | 7 +---- libraries/mbed/common/BusOut.cpp | 7 +---- libraries/tests/mbed/bus_out/main.cpp | 39 ++++++++++++++++----------- 7 files changed, 26 insertions(+), 40 deletions(-) diff --git a/libraries/mbed/api/BusIn.h b/libraries/mbed/api/BusIn.h index 6f20875fa4..d1c9a9cd4e 100644 --- a/libraries/mbed/api/BusIn.h +++ b/libraries/mbed/api/BusIn.h @@ -68,8 +68,6 @@ public: return _nc_mask; } - static DigitalIn din_dummy; - #ifdef MBED_OPERATORS /** A shorthand for read() */ diff --git a/libraries/mbed/api/BusInOut.h b/libraries/mbed/api/BusInOut.h index e9a12c5f92..54328fb021 100644 --- a/libraries/mbed/api/BusInOut.h +++ b/libraries/mbed/api/BusInOut.h @@ -82,8 +82,6 @@ public: return _nc_mask; } - static DigitalInOut dinout_dummy; - #ifdef MBED_OPERATORS /** A shorthand for write() */ diff --git a/libraries/mbed/api/BusOut.h b/libraries/mbed/api/BusOut.h index 9e88e2d170..1c55be07e9 100644 --- a/libraries/mbed/api/BusOut.h +++ b/libraries/mbed/api/BusOut.h @@ -66,8 +66,6 @@ public: return _nc_mask; } - static DigitalOut dout_dummy; - #ifdef MBED_OPERATORS /** A shorthand for write() */ diff --git a/libraries/mbed/common/BusIn.cpp b/libraries/mbed/common/BusIn.cpp index 5ec8cbb8f2..56885ba9db 100644 --- a/libraries/mbed/common/BusIn.cpp +++ b/libraries/mbed/common/BusIn.cpp @@ -17,8 +17,6 @@ namespace mbed { -DigitalIn BusIn::din_dummy(NC); - 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}; @@ -73,11 +71,8 @@ BusIn::operator int() { } DigitalIn& BusIn::operator[] (int index) { - MBED_ASSERT(index < 0 || index >= 16); + MBED_ASSERT(index >= 0 && index <= 16); MBED_ASSERT(_pin[index]); - if (index >= 16 || _pin[index] == NULL) { - return din_dummy; - } return *_pin[index]; } diff --git a/libraries/mbed/common/BusInOut.cpp b/libraries/mbed/common/BusInOut.cpp index bd8bc82726..3e56e61729 100644 --- a/libraries/mbed/common/BusInOut.cpp +++ b/libraries/mbed/common/BusInOut.cpp @@ -17,8 +17,6 @@ namespace mbed { -DigitalInOut BusInOut::dinout_dummy(NC); - 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}; @@ -103,11 +101,8 @@ BusInOut& BusInOut::operator= (BusInOut& rhs) { } DigitalInOut& BusInOut::operator[] (int index) { - MBED_ASSERT(index < 0 || index >= 16); + MBED_ASSERT(index >= 0 && index <= 16); MBED_ASSERT(_pin[index]); - if (index >= 16 || _pin[index] == NULL) { - return dinout_dummy; - } return *_pin[index]; } diff --git a/libraries/mbed/common/BusOut.cpp b/libraries/mbed/common/BusOut.cpp index 86b2acb29a..7368c00e6a 100644 --- a/libraries/mbed/common/BusOut.cpp +++ b/libraries/mbed/common/BusOut.cpp @@ -17,8 +17,6 @@ namespace mbed { -DigitalOut BusOut::dout_dummy(NC); - 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}; @@ -79,11 +77,8 @@ BusOut& BusOut::operator= (BusOut& rhs) { } DigitalOut& BusOut::operator[] (int index) { - MBED_ASSERT(index < 0 || index >= 16); + MBED_ASSERT(index >= 0 && index <= 16); MBED_ASSERT(_pin[index]); - if (index >= 16 || _pin[index] == NULL) { - return dout_dummy; - } return *_pin[index]; } diff --git a/libraries/tests/mbed/bus_out/main.cpp b/libraries/tests/mbed/bus_out/main.cpp index 4f14348604..35fccc4fa1 100644 --- a/libraries/tests/mbed/bus_out/main.cpp +++ b/libraries/tests/mbed/bus_out/main.cpp @@ -1,7 +1,10 @@ #include "mbed.h" #include "test_env.h" +namespace { BusOut bus_out(LED1, LED2, LED3, LED4); +PinName led_pins[4] = {LED1, LED2, LED3, LED4}; // Temp, used to map pins in bus_out +} int main() { @@ -26,21 +29,18 @@ int main() } // Checking if DigitalOut is correctly set as connected - for (int i=0; i<4; i++) { - printf("MBED: BusOut.bit[%d] is %s\r\n", i, bus_out[i].is_connected() ? "connected" : "not connected"); + for (int i=0; i < 4; i++) { + printf("MBED: BusOut.bit[%d] is %s\r\n", + i, + (led_pins[i] != NC && bus_out[i].is_connected()) + ? "connected" + : "not connected"); } - if (LED1 != NC && bus_out[0].is_connected() == 0) { - break; - } - if (LED1 != NC && bus_out[1].is_connected() == 0) { - break; - } - if (LED1 != NC && bus_out[2].is_connected() == 0) { - break; - } - if (LED1 != NC && bus_out[3].is_connected() == 0) { - break; + for (int i=0; i < 4; i++) { + if (led_pins[i] != NC && bus_out[0].is_connected() == 0) { + break; + } } // Write mask all LEDs @@ -58,18 +58,25 @@ int main() break; } - printf("MBED: Blinking LEDs...\r\n"); + printf("MBED: Blinking LEDs: \r\n"); // Just a quick LED blinking... for (int i=0; i<4; i++) { - if (bus_out[i].is_connected()) { + if (led_pins[i] != NC && bus_out[i].is_connected()) { bus_out[i] = 1; + printf("%c", 'A' + i); + } else { + printf("."); } wait(0.2); - if (bus_out[i].is_connected()) { + if (led_pins[i] != NC && bus_out[i].is_connected()) { bus_out[i] = 0; + printf("%c", 'a' + i); + } else { + printf("."); } } + printf("\r\n"); notify_completion(result); }