Skip to content

Commit c1d3cb5

Browse files
committed
address concern from watarai-san about code size and performance by creating a set of common initialization functions
1 parent be8bca4 commit c1d3cb5

File tree

12 files changed

+112
-46
lines changed

12 files changed

+112
-46
lines changed

libraries/mbed/api/DigitalIn.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,19 @@ class DigitalIn {
4949
/** Create a DigitalIn connected to the specified pin
5050
*
5151
* @param pin DigitalIn pin to connect to
52-
* @param mode the initial mode of the pin
5352
*/
54-
DigitalIn(PinName pin, PinMode mode = PullDefault) {
55-
GPIO_INIT_IN(&gpio, pin, mode);
53+
DigitalIn(PinName pin) {
54+
gpio_init_in(&gpio, pin);
5655
}
5756

57+
/** Create a DigitalIn connected to the specified pin
58+
*
59+
* @param pin DigitalIn pin to connect to
60+
* @param mode the initial mode of the pin
61+
*/
62+
DigitalIn(PinName pin, PinMode mode) {
63+
gpio_init_in_ex(&gpio, pin, mode);
64+
}
5865
/** Read the input, represented as 0 or 1 (int)
5966
*
6067
* @returns

libraries/mbed/api/DigitalInOut.h

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,24 @@ namespace mbed {
2727
class DigitalInOut {
2828

2929
public:
30+
/** Create a DigitalInOut connected to the specified pin
31+
*
32+
* @param pin DigitalInOut pin to connect to
33+
*/
34+
DigitalInOut(PinName pin) {
35+
gpio_init_in(&gpio, pin);
36+
}
37+
3038
/** Create a DigitalInOut connected to the specified pin
3139
*
3240
* @param pin DigitalInOut pin to connect to
3341
* @param direction the initial direction of the pin
3442
* @param mode the initial mode of the pin
3543
* @param value the initial value of the pin if is an output
3644
*/
37-
DigitalInOut(PinName pin, PinDirection direction = PIN_INPUT, PinMode mode = PullDefault, int value = 0) {
38-
if (direction == PIN_INPUT) {
39-
GPIO_INIT_IN(&gpio, pin, mode);
40-
gpio_write(&gpio, value); // we prepare the value in case it is switched later
41-
} else {
42-
GPIO_INIT_OUT(&gpio, pin, mode, value);
43-
}
44-
}
45+
DigitalInOut(PinName pin, PinDirection direction, PinMode mode, int value) {
46+
gpio_init_inout(&gpio, pin, direction, mode, value);
47+
}
4548

4649
/** Set the output, specified as 0 or 1 (int)
4750
*

libraries/mbed/api/DigitalOut.h

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,22 @@ namespace mbed {
4141
class DigitalOut {
4242

4343
public:
44+
/** Create a DigitalOut connected to the specified pin
45+
*
46+
* @param pin DigitalOut pin to connect to
47+
*/
48+
DigitalOut(PinName pin) {
49+
gpio_init_out(&gpio, pin);
50+
}
51+
4452
/** Create a DigitalOut connected to the specified pin
4553
*
4654
* @param pin DigitalOut pin to connect to
4755
* @param value the initial pin value
4856
*/
49-
DigitalOut(PinName pin, int value = 0) {
50-
GPIO_INIT_OUT(&gpio, pin, PullNone, value);
51-
}
57+
DigitalOut(PinName pin, int value){
58+
gpio_init_out_ex(&gpio, pin, value);
59+
}
5260

5361
/** Set the output, specified as 0 or 1 (int)
5462
*

libraries/mbed/common/InterruptIn.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ namespace mbed {
2121

2222
InterruptIn::InterruptIn(PinName pin) {
2323
gpio_irq_init(&gpio_irq, pin, (&InterruptIn::_irq_handler), (uint32_t)this);
24-
GPIO_INIT_IN(&gpio, pin, PullDefault);
24+
gpio_init_in(&gpio, pin);
2525
}
2626

2727
InterruptIn::~InterruptIn() {

libraries/mbed/common/board.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ WEAK void mbed_die(void) {
2222
__disable_irq(); // dont allow interrupts to disturb the flash pattern
2323

2424
#if (DEVICE_ERROR_RED == 1)
25-
gpio_t led_red; GPIO_INIT_OUT(&led_red, LED_RED, PullNone, 0);
25+
gpio_t led_red; gpio_init_out(&led_red, LED_RED);
2626

2727
#elif (DEVICE_ERROR_PATTERN == 1)
28-
gpio_t led_1; GPIO_INIT_OUT(&led_1, LED1, PullNone, 0);
29-
gpio_t led_2; GPIO_INIT_OUT(&led_2, LED2, PullNone, 0);
30-
gpio_t led_3; GPIO_INIT_OUT(&led_3, LED3, PullNone, 0);
31-
gpio_t led_4; GPIO_INIT_OUT(&led_4, LED4, PullNone, 0);
28+
gpio_t led_1; gpio_init_out(&led_1, LED1);
29+
gpio_t led_2; gpio_init_out(&led_2, LED2);
30+
gpio_t led_3; gpio_init_out(&led_3, LED3);
31+
gpio_t led_4; gpio_init_out(&led_4, LED4);
3232
#endif
3333

3434
while (1) {

libraries/mbed/common/gpio.c

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2006-2013 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
#include "gpio_api.h"
17+
18+
#define GPIO_INIT_IN(obj, pin, mode) \
19+
gpio_init(obj, pin), \
20+
gpio_mode(obj, mode), \
21+
gpio_dir(obj, PIN_INPUT)
22+
23+
#define GPIO_INIT_OUT(obj, pin, mode, value) \
24+
gpio_init(obj, pin), \
25+
gpio_write(obj, value), \
26+
gpio_dir(obj, PIN_OUTPUT), \
27+
gpio_mode(obj, mode)
28+
29+
void gpio_init_in(gpio_t* gpio, PinName pin) {
30+
gpio_init_in_ex(gpio, pin, PullDefault);
31+
}
32+
33+
void gpio_init_in_ex(gpio_t* gpio, PinName pin, PinMode mode) {
34+
GPIO_INIT_IN(gpio, pin, mode);
35+
}
36+
37+
void gpio_init_out(gpio_t* gpio, PinName pin) {
38+
gpio_init_out_ex(gpio, pin, 0);
39+
}
40+
41+
void gpio_init_out_ex(gpio_t* gpio, PinName pin, int value) {
42+
GPIO_INIT_OUT(gpio, pin, PullNone, value);
43+
}
44+
45+
void gpio_init_inout(gpio_t* gpio, PinName pin, PinDirection direction, PinMode mode, int value) {
46+
if (direction == PIN_INPUT) {
47+
GPIO_INIT_IN(gpio, pin, mode);
48+
gpio_write(gpio, value); // we prepare the value in case direction is switched later
49+
} else {
50+
GPIO_INIT_OUT(gpio, pin, mode, value);
51+
}
52+
}

libraries/mbed/hal/gpio_api.h

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,12 @@ void gpio_dir (gpio_t *obj, PinDirection direction);
3737
void gpio_write(gpio_t *obj, int value);
3838
int gpio_read (gpio_t *obj);
3939

40-
#define GPIO_INIT_IN(obj, pin, mode) \
41-
gpio_init(obj, pin), \
42-
gpio_mode(obj, mode), \
43-
gpio_dir(obj, PIN_INPUT)
44-
45-
#define GPIO_INIT_OUT(obj, pin, mode, value) \
46-
gpio_init(obj, pin), \
47-
gpio_write(obj, value), \
48-
gpio_dir(obj, PIN_OUTPUT), \
49-
gpio_mode(obj, mode)
40+
// the following set of functions are generic and are implemented in the common gpio.c file
41+
void gpio_init_in(gpio_t* gpio, PinName pin);
42+
void gpio_init_in_ex(gpio_t* gpio, PinName pin, PinMode mode);
43+
void gpio_init_out(gpio_t* gpio, PinName pin);
44+
void gpio_init_out_ex(gpio_t* gpio, PinName pin, int value);
45+
void gpio_init_inout(gpio_t* gpio, PinName pin, PinDirection direction, PinMode mode, int value);
5046

5147
#ifdef __cplusplus
5248
}

libraries/mbed/targets/hal/TARGET_Freescale/TARGET_KLXX/TARGET_KL05Z/gpio_irq_api.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,5 +180,5 @@ void gpio_irq_disable(gpio_irq_t *obj) {
180180
void NMI_Handler(void)
181181
{
182182
gpio_t gpio;
183-
GPIO_INIT_IN(&gpio, PTA4, PIN_INPUT, PullDefault);
183+
gpio_init_in(&gpio, PTA4);
184184
}

libraries/mbed/targets/hal/TARGET_Freescale/TARGET_KLXX/TARGET_KL25Z/gpio_irq_api.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,5 +170,5 @@ void gpio_irq_disable(gpio_irq_t *obj) {
170170
void NMI_Handler(void)
171171
{
172172
gpio_t gpio;
173-
GPIO_INIT_IN(&gpio, PTA4, PullDefault);
173+
gpio_init_in(&gpio, PTA4);
174174
}

libraries/mbed/targets/hal/TARGET_Freescale/TARGET_KLXX/TARGET_KL46Z/gpio_irq_api.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,5 +190,5 @@ void gpio_irq_disable(gpio_irq_t *obj) {
190190
void NMI_Handler(void)
191191
{
192192
gpio_t gpio;
193-
GPIO_INIT_IN(&gpio, PTA4, PIN_INPUT, PullDefault);
193+
gpio_init_in(&gpio, PTA4);
194194
}

libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC176X/TARGET_UBLOX_C027/platform_init.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,19 @@ void mbed_sdk_init()
99
gpio_t gpsEn, gpsRst, led, modemRts;
1010

1111
// start with modem disabled
12-
GPIO_INIT_OUT(&modemEn, MDMEN, PullNone, 0);
13-
GPIO_INIT_OUT(&modemRst, MDMRST, PullNone, 1);
14-
GPIO_INIT_OUT(&modemPwrOn, MDMPWRON, PullNone, 1);
15-
GPIO_INIT_OUT(&modemLvlOe, MDMLVLOE, PullNone, 1);
16-
GPIO_INIT_OUT(&modemILvlOe, MDMILVLOE, PullNone, 0);
17-
GPIO_INIT_OUT(&modemUsbDet, MDMUSBDET, PullNone, 0);
18-
GPIO_INIT_OUT(&modemRts, MDMRTS, PullNone, 0);
12+
gpio_init_out_ex(&modemEn, MDMEN, PullNone, 0);
13+
gpio_init_out_ex(&modemRst, MDMRST, PullNone, 1);
14+
gpio_init_out_ex(&modemPwrOn, MDMPWRON, PullNone, 1);
15+
gpio_init_out_ex(&modemLvlOe, MDMLVLOE, PullNone, 1);
16+
gpio_init_out_ex(&modemILvlOe, MDMILVLOE, PullNone, 0);
17+
gpio_init_out_ex(&modemUsbDet, MDMUSBDET, PullNone, 0);
18+
gpio_init_out_ex(&modemRts, MDMRTS, PullNone, 0);
1919
// start with gps disabled
20-
GPIO_INIT_OUT(&gpsEn, GPSEN, PullNone, 0);
21-
GPIO_INIT_OUT(&gpsRst, GPSRST, PullNone, 1);
20+
gpio_init_out_ex(&gpsEn, GPSEN, PullNone, 0);
21+
gpio_init_out_ex(&gpsRst, GPSRST, PullNone, 1);
2222
// led should be off
23-
GPIO_INIT_OUT(&led, LED, PullNone, 0);
24-
23+
gpio_init_out_ex(&led, LED, PullNone, 0);
24+
2525
wait_ms(50); // when USB cable is inserted the interface chip issues
2626
// multiple resets to the target CPU We wait here for a short period to
2727
// prevent those resets from propagating to the modem and other

libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC176X/serial_api.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ void serial_set_flow_control(serial_t *obj, FlowControl type, PinName rxflow, Pi
412412
pinmap_pinout(txflow, PinMap_UART_CTS);
413413
} else {
414414
// Can't enable in hardware, use software emulation
415-
GPIO_INIT_IN(&uart_data[index].sw_cts, txflow, PullDown);
415+
gpio_init_in(&uart_data[index].sw_cts, txflow);
416416
}
417417
}
418418
if (((FlowControlRTS == type) || (FlowControlRTSCTS == type)) && (NC != rxflow)) {
@@ -427,7 +427,7 @@ void serial_set_flow_control(serial_t *obj, FlowControl type, PinName rxflow, Pi
427427
uart1->MCR |= UART_MCR_RTSEN_MASK;
428428
pinmap_pinout(rxflow, PinMap_UART_RTS);
429429
} else { // can't enable in hardware, use software emulation
430-
GPIO_INIT_OUT(&uart_data[index].sw_rts, rxflow, PullNone, 0);
430+
gpio_init_out_ex(&uart_data[index].sw_rts, rxflow, 0);
431431
// Enable RX interrupt
432432
serial_flow_irq_set(obj, 1);
433433
}

0 commit comments

Comments
 (0)