Skip to content

Commit 83ece4a

Browse files
committed
Add optional virtual to DigitalIn/Out/InOut methods
This commit introduces configuration options to make certain methods of the `DigitalIn`, `DigitalOut`, and `DigitalInOut` classes virtual. This is useful when more advanced features of polymorphism are required. This is not enabled by default to optimize these classes for execution speed and code size. Enabling this option will slightly increase latency of calling the virtual methods and will increase the ROM/RAM footprint of the class. The configuration can be changed by overriding the options: `drivers.virtualize-digitalin` (and so on, for each class involved in the commit).
1 parent 83c18dd commit 83ece4a

File tree

4 files changed

+121
-3
lines changed

4 files changed

+121
-3
lines changed

drivers/include/drivers/DigitalIn.h

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* mbed Microcontroller Library
2-
* Copyright (c) 2006-2019 ARM Limited
2+
* Copyright (c) 2006-2020 ARM Limited
33
* SPDX-License-Identifier: Apache-2.0
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -21,6 +21,14 @@
2121

2222
#include "hal/gpio_api.h"
2323

24+
/**
25+
* Option to make certain class functions virtual for advanced polymorphism.
26+
* Disabled by default for size and speed optimization.
27+
*/
28+
#ifndef MBED_CONF_DRIVERS_VIRTUALIZE_DIGITALIN
29+
#define MBED_CONF_DRIVERS_VIRTUALIZE_DIGITALIN false
30+
#endif
31+
2432
namespace mbed {
2533
/**
2634
* \defgroup drivers_DigitalIn DigitalIn class
@@ -75,13 +83,28 @@ class DigitalIn {
7583
gpio_init_in_ex(&gpio, pin, mode);
7684
}
7785

86+
/** Class destructor, deinitialize the pin
87+
*/
88+
#if MBED_CONF_DRIVERS_VIRTUALIZE_DIGITALIN
89+
virtual ~DigitalIn()
90+
#else
91+
~DigitalIn()
92+
#endif
93+
{
94+
gpio_free(&gpio);
95+
}
96+
7897
/** Read the input, represented as 0 or 1 (int)
7998
*
8099
* @returns
81100
* An integer representing the state of the input pin,
82101
* 0 for logical 0, 1 for logical 1
83102
*/
103+
#if MBED_CONF_DRIVERS_VIRTUALIZE_DIGITALIN
104+
virtual int read()
105+
#else
84106
int read()
107+
#endif
85108
{
86109
// Thread safe / atomic HAL call
87110
return gpio_read(&gpio);
@@ -91,15 +114,23 @@ class DigitalIn {
91114
*
92115
* @param pull PullUp, PullDown, PullNone, OpenDrain
93116
*/
117+
#if MBED_CONF_DRIVERS_VIRTUALIZE_DIGITALIN
118+
virtual void mode(PinMode pull);
119+
#else
94120
void mode(PinMode pull);
121+
#endif
95122

96123
/** Return the output setting, represented as 0 or 1 (int)
97124
*
98125
* @returns
99126
* Non zero value if pin is connected to uc GPIO
100127
* 0 if gpio object was initialized with NC
101128
*/
129+
#if MBED_CONF_DRIVERS_VIRTUALIZE_DIGITALIN
130+
virtual int is_connected()
131+
#else
102132
int is_connected()
133+
#endif
103134
{
104135
// Thread safe / atomic HAL call
105136
return gpio_is_connected(&gpio);

drivers/include/drivers/DigitalInOut.h

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* mbed Microcontroller Library
2-
* Copyright (c) 2006-2019 ARM Limited
2+
* Copyright (c) 2006-2020 ARM Limited
33
* SPDX-License-Identifier: Apache-2.0
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -21,6 +21,14 @@
2121

2222
#include "hal/gpio_api.h"
2323

24+
/**
25+
* Option to make certain class functions virtual for advanced polymorphism.
26+
* Disabled by default for size and speed optimization.
27+
*/
28+
#ifndef MBED_CONF_DRIVERS_VIRTUALIZE_DIGITALINOUT
29+
#define MBED_CONF_DRIVERS_VIRTUALIZE_DIGITALINOUT false
30+
#endif
31+
2432
namespace mbed {
2533
/**
2634
* \defgroup drivers_DigitalInOut DigitalInOut class
@@ -58,12 +66,27 @@ class DigitalInOut {
5866
gpio_init_inout(&gpio, pin, direction, mode, value);
5967
}
6068

69+
/** Class destructor, deinitialize the pin
70+
*/
71+
#if MBED_CONF_DRIVERS_VIRTUALIZE_DIGITALINOUT
72+
virtual ~DigitalInOut()
73+
#else
74+
~DigitalInOut()
75+
#endif
76+
{
77+
gpio_free(&gpio);
78+
}
79+
6180
/** Set the output, specified as 0 or 1 (int)
6281
*
6382
* @param value An integer specifying the pin output value,
6483
* 0 for logical 0, 1 (or any other non-zero value) for logical 1
6584
*/
85+
#if MBED_CONF_DRIVERS_VIRTUALIZE_DIGITALINOUT
86+
virtual void write(int value)
87+
#else
6688
void write(int value)
89+
#endif
6790
{
6891
// Thread safe / atomic HAL call
6992
gpio_write(&gpio, value);
@@ -75,33 +98,53 @@ class DigitalInOut {
7598
* an integer representing the output setting of the pin if it is an output,
7699
* or read the input if set as an input
77100
*/
101+
#if MBED_CONF_DRIVERS_VIRTUALIZE_DIGITALINOUT
102+
virtual int read()
103+
#else
78104
int read()
105+
#endif
79106
{
80107
// Thread safe / atomic HAL call
81108
return gpio_read(&gpio);
82109
}
83110

84111
/** Set as an output
85112
*/
113+
#if MBED_CONF_DRIVERS_VIRTUALIZE_DIGITALINOUT
114+
virtual void output();
115+
#else
86116
void output();
117+
#endif
87118

88119
/** Set as an input
89120
*/
121+
#if MBED_CONF_DRIVERS_VIRTUALIZE_DIGITALINOUT
122+
virtual void input();
123+
#else
90124
void input();
125+
#endif
91126

92127
/** Set the input pin mode
93128
*
94129
* @param pull PullUp, PullDown, PullNone, OpenDrain
95130
*/
131+
#if MBED_CONF_DRIVERS_VIRTUALIZE_DIGITALINOUT
132+
virtual void mode(PinMode pull);
133+
#else
96134
void mode(PinMode pull);
135+
#endif
97136

98137
/** Return the output setting, represented as 0 or 1 (int)
99138
*
100139
* @returns
101140
* Non zero value if pin is connected to uc GPIO
102141
* 0 if gpio object was initialized with NC
103142
*/
143+
#if MBED_CONF_DRIVERS_VIRTUALIZE_DIGITALINOUT
144+
virtual int is_connected()
145+
#else
104146
int is_connected()
147+
#endif
105148
{
106149
// Thread safe / atomic HAL call
107150
return gpio_is_connected(&gpio);

drivers/include/drivers/DigitalOut.h

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* mbed Microcontroller Library
2-
* Copyright (c) 2006-2019 ARM Limited
2+
* Copyright (c) 2006-2020 ARM Limited
33
* SPDX-License-Identifier: Apache-2.0
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -18,8 +18,17 @@
1818
#define MBED_DIGITALOUT_H
1919

2020
#include "platform/platform.h"
21+
2122
#include "hal/gpio_api.h"
2223

24+
/**
25+
* Option to make certain class functions virtual for advanced polymorphism.
26+
* Disabled by default for size and speed optimization.
27+
*/
28+
#ifndef MBED_CONF_DRIVERS_VIRTUALIZE_DIGITALOUT
29+
#define MBED_CONF_DRIVERS_VIRTUALIZE_DIGITALOUT false
30+
#endif
31+
2332
namespace mbed {
2433
/**
2534
* \defgroup drivers_DigitalOut DigitalOut class
@@ -70,12 +79,27 @@ class DigitalOut {
7079
gpio_init_out_ex(&gpio, pin, value);
7180
}
7281

82+
/** Class destructor, deinitialize the pin
83+
*/
84+
#if MBED_CONF_DRIVERS_VIRTUALIZE_DIGITALOUT
85+
virtual ~DigitalOut()
86+
#else
87+
~DigitalOut()
88+
#endif
89+
{
90+
gpio_free(&gpio);
91+
}
92+
7393
/** Set the output, specified as 0 or 1 (int)
7494
*
7595
* @param value An integer specifying the pin output value,
7696
* 0 for logical 0, 1 (or any other non-zero value) for logical 1
7797
*/
98+
#if MBED_CONF_DRIVERS_VIRTUALIZE_DIGITALOUT
99+
virtual void write(int value)
100+
#else
78101
void write(int value)
102+
#endif
79103
{
80104
// Thread safe / atomic HAL call
81105
gpio_write(&gpio, value);
@@ -87,7 +111,11 @@ class DigitalOut {
87111
* an integer representing the output setting of the pin,
88112
* 0 for logical 0, 1 for logical 1
89113
*/
114+
#if MBED_CONF_DRIVERS_VIRTUALIZE_DIGITALOUT
115+
virtual int read()
116+
#else
90117
int read()
118+
#endif
91119
{
92120
// Thread safe / atomic HAL call
93121
return gpio_read(&gpio);
@@ -99,7 +127,11 @@ class DigitalOut {
99127
* Non zero value if pin is connected to uc GPIO
100128
* 0 if gpio object was initialized with NC
101129
*/
130+
#if MBED_CONF_DRIVERS_VIRTUALIZE_DIGITALOUT
131+
virtual int is_connected()
132+
#else
102133
int is_connected()
134+
#endif
103135
{
104136
// Thread safe / atomic HAL call
105137
return gpio_is_connected(&gpio);

drivers/mbed_lib.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,18 @@
8585
"ospi_dqs": {
8686
"help": "OSPI dqs pin",
8787
"value": "OSPI_FLASH1_DQS"
88+
},
89+
"virtualize-digitalout": {
90+
"help": "Enable virtual functions in the DigitalOut class",
91+
"value": false
92+
},
93+
"virtualize-digitalin": {
94+
"help": "Enable virtual functions in the DigitalIn class",
95+
"value": false
96+
},
97+
"virtualize-digitalinout": {
98+
"help": "Enable virtual functions in the DigitalInOut class",
99+
"value": false
88100
}
89101
}
90102
}

0 commit comments

Comments
 (0)