Skip to content

Commit 423ddcb

Browse files
committed
Merge pull request #198 from mazgch/gpio_api
proposed change of gpio_api (new update pull request)
2 parents 67b479e + 4e53124 commit 423ddcb

File tree

52 files changed

+188
-210
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+188
-210
lines changed

libraries/mbed/api/DigitalIn.h

Lines changed: 9 additions & 2 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 name (optional) A string to identify the object
5352
*/
5453
DigitalIn(PinName pin) {
55-
gpio_init(&gpio, pin, PIN_INPUT);
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: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,18 @@ class DigitalInOut {
3232
* @param pin DigitalInOut pin to connect to
3333
*/
3434
DigitalInOut(PinName pin) {
35-
gpio_init(&gpio, pin, PIN_INPUT);
35+
gpio_init_in(&gpio, pin);
36+
}
37+
38+
/** Create a DigitalInOut connected to the specified pin
39+
*
40+
* @param pin DigitalInOut pin to connect to
41+
* @param direction the initial direction of the pin
42+
* @param mode the initial mode of the pin
43+
* @param value the initial value of the pin if is an output
44+
*/
45+
DigitalInOut(PinName pin, PinDirection direction, PinMode mode, int value) {
46+
gpio_init_inout(&gpio, pin, direction, mode, value);
3647
}
3748

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

libraries/mbed/api/DigitalOut.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,16 @@ class DigitalOut {
4646
* @param pin DigitalOut pin to connect to
4747
*/
4848
DigitalOut(PinName pin) {
49-
gpio_init(&gpio, pin, PIN_OUTPUT);
49+
gpio_init_out(&gpio, pin);
50+
}
51+
52+
/** Create a DigitalOut connected to the specified pin
53+
*
54+
* @param pin DigitalOut pin to connect to
55+
* @param value the initial pin value
56+
*/
57+
DigitalOut(PinName pin, int value){
58+
gpio_init_out_ex(&gpio, pin, value);
5059
}
5160

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

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(&gpio, pin, PIN_INPUT);
24+
gpio_init_in(&gpio, pin);
2525
}
2626

2727
InterruptIn::~InterruptIn() {

libraries/mbed/common/board.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@
2020
WEAK void mbed_die(void);
2121
WEAK void mbed_die(void) {
2222
__disable_irq(); // dont allow interrupts to disturb the flash pattern
23-
24-
#if (DEVICE_ERROR_RED == 1)
25-
gpio_t led_red; gpio_init(&led_red, LED_RED, PIN_OUTPUT);
2623

24+
#if (DEVICE_ERROR_RED == 1)
25+
gpio_t led_red; gpio_init_out(&led_red, LED_RED);
26+
2727
#elif (DEVICE_ERROR_PATTERN == 1)
28-
gpio_t led_1; gpio_init(&led_1, LED1, PIN_OUTPUT);
29-
gpio_t led_2; gpio_init(&led_2, LED2, PIN_OUTPUT);
30-
gpio_t led_3; gpio_init(&led_3, LED3, PIN_OUTPUT);
31-
gpio_t led_4; gpio_init(&led_4, LED4, PIN_OUTPUT);
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: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
static inline void _gpio_init_in(gpio_t* gpio, PinName pin, PinMode mode)
19+
{
20+
gpio_init(gpio, pin);
21+
gpio_mode(gpio, mode);
22+
gpio_dir(gpio, PIN_INPUT);
23+
}
24+
25+
static inline void _gpio_init_out(gpio_t* gpio, PinName pin, PinMode mode, int value)
26+
{
27+
gpio_init(gpio, pin);
28+
gpio_write(gpio, value);
29+
gpio_dir(gpio, PIN_OUTPUT);
30+
gpio_mode(gpio, mode);
31+
}
32+
33+
void gpio_init_in(gpio_t* gpio, PinName pin) {
34+
gpio_init_in_ex(gpio, pin, PullDefault);
35+
}
36+
37+
void gpio_init_in_ex(gpio_t* gpio, PinName pin, PinMode mode) {
38+
_gpio_init_in(gpio, pin, mode);
39+
}
40+
41+
void gpio_init_out(gpio_t* gpio, PinName pin) {
42+
gpio_init_out_ex(gpio, pin, 0);
43+
}
44+
45+
void gpio_init_out_ex(gpio_t* gpio, PinName pin, int value) {
46+
_gpio_init_out(gpio, pin, PullNone, value);
47+
}
48+
49+
void gpio_init_inout(gpio_t* gpio, PinName pin, PinDirection direction, PinMode mode, int value) {
50+
if (direction == PIN_INPUT) {
51+
_gpio_init_in(gpio, pin, mode);
52+
gpio_write(gpio, value); // we prepare the value in case it is switched later
53+
} else {
54+
_gpio_init_out(gpio, pin, mode, value);
55+
}
56+
}

libraries/mbed/hal/gpio_api.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,21 @@ extern "C" {
2929
uint32_t gpio_set(PinName pin);
3030

3131
/* GPIO object */
32-
void gpio_init (gpio_t *obj, PinName pin, PinDirection direction);
32+
void gpio_init(gpio_t *obj, PinName pin);
3333

3434
void gpio_mode (gpio_t *obj, PinMode mode);
3535
void gpio_dir (gpio_t *obj, PinDirection direction);
3636

3737
void gpio_write(gpio_t *obj, int value);
3838
int gpio_read (gpio_t *obj);
3939

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);
46+
4047
#ifdef __cplusplus
4148
}
4249
#endif

libraries/mbed/targets/hal/TARGET_Freescale/TARGET_K20D5M/PinNames.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ typedef enum {
240240
PullNone = 0,
241241
PullDown = 2,
242242
PullUp = 3,
243+
PullDefault = PullUp
243244
} PinMode;
244245

245246
#ifdef __cplusplus

libraries/mbed/targets/hal/TARGET_Freescale/TARGET_K20D5M/gpio_api.c

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ uint32_t gpio_set(PinName pin) {
2121
return 1 << ((pin & 0x7F) >> 2);
2222
}
2323

24-
void gpio_init(gpio_t *obj, PinName pin, PinDirection direction) {
24+
void gpio_init(gpio_t *obj, PinName pin) {
2525
if(pin == NC)
2626
return;
2727

@@ -35,16 +35,6 @@ void gpio_init(gpio_t *obj, PinName pin, PinDirection direction) {
3535
obj->reg_clr = &reg->PCOR;
3636
obj->reg_in = &reg->PDIR;
3737
obj->reg_dir = &reg->PDDR;
38-
39-
gpio_dir(obj, direction);
40-
switch (direction) {
41-
case PIN_OUTPUT:
42-
pin_mode(pin, PullNone);
43-
break;
44-
case PIN_INPUT :
45-
pin_mode(pin, PullUp);
46-
break;
47-
}
4838
}
4939

5040
void gpio_mode(gpio_t *obj, PinMode mode) {

libraries/mbed/targets/hal/TARGET_Freescale/TARGET_KLXX/TARGET_KL05Z/PinNames.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ typedef enum {
126126
typedef enum {
127127
PullNone = 0,
128128
PullUp = 2,
129+
PullDefault = PullUp
129130
} PinMode;
130131

131132
#ifdef __cplusplus

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "cmsis.h"
1818

1919
#include "gpio_irq_api.h"
20+
#include "gpio_api.h"
2021
#include "error.h"
2122

2223
#define CHANNEL_NUM 64 // 31 pins on 2 ports
@@ -176,9 +177,8 @@ void gpio_irq_disable(gpio_irq_t *obj) {
176177
// Change the NMI pin to an input. This allows NMI pin to
177178
// be used as a low power mode wakeup. The application will
178179
// need to change the pin back to NMI_b or wakeup only occurs once!
179-
extern void gpio_init(gpio_t *obj, PinName pin, PinDirection direction);
180180
void NMI_Handler(void)
181181
{
182182
gpio_t gpio;
183-
gpio_init(&gpio, PTB5, PIN_INPUT);
183+
gpio_init_in(&gpio, PTA4);
184184
}

libraries/mbed/targets/hal/TARGET_Freescale/TARGET_KLXX/TARGET_KL25Z/PinNames.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ typedef enum {
244244
typedef enum {
245245
PullNone = 0,
246246
PullUp = 2,
247+
PullDefault = PullUp
247248
} PinMode;
248249

249250
#ifdef __cplusplus

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "cmsis.h"
1818

1919
#include "gpio_irq_api.h"
20+
#include "gpio_api.h"
2021
#include "error.h"
2122

2223
#define CHANNEL_NUM 64
@@ -166,9 +167,8 @@ void gpio_irq_disable(gpio_irq_t *obj) {
166167
// Change the NMI pin to an input. This allows NMI pin to
167168
// be used as a low power mode wakeup. The application will
168169
// need to change the pin back to NMI_b or wakeup only occurs once!
169-
extern void gpio_init(gpio_t *obj, PinName pin, PinDirection direction);
170170
void NMI_Handler(void)
171171
{
172172
gpio_t gpio;
173-
gpio_init(&gpio, PTA4, PIN_INPUT);
173+
gpio_init_in(&gpio, PTA4);
174174
}

libraries/mbed/targets/hal/TARGET_Freescale/TARGET_KLXX/TARGET_KL46Z/PinNames.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,8 @@ typedef enum {
247247
typedef enum {
248248
PullNone = 0,
249249
PullDown = 2,
250-
PullUp = 3
250+
PullUp = 3,
251+
PullDefault = PullUp
251252
} PinMode;
252253

253254
#ifdef __cplusplus

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "cmsis.h"
1818

1919
#include "gpio_irq_api.h"
20+
#include "gpio_api.h"
2021
#include "error.h"
2122

2223
#define CHANNEL_NUM 96
@@ -186,9 +187,8 @@ void gpio_irq_disable(gpio_irq_t *obj) {
186187
// Change the NMI pin to an input. This allows NMI pin to
187188
// be used as a low power mode wakeup. The application will
188189
// need to change the pin back to NMI_b or wakeup only occurs once!
189-
extern void gpio_init(gpio_t *obj, PinName pin, PinDirection direction);
190190
void NMI_Handler(void)
191191
{
192192
gpio_t gpio;
193-
gpio_init(&gpio, PTA4, PIN_INPUT);
193+
gpio_init_in(&gpio, PTA4);
194194
}

libraries/mbed/targets/hal/TARGET_Freescale/TARGET_KLXX/gpio_api.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ uint32_t gpio_set(PinName pin) {
2121
return 1 << ((pin & 0x7F) >> 2);
2222
}
2323

24-
void gpio_init(gpio_t *obj, PinName pin, PinDirection direction) {
24+
void gpio_init(gpio_t *obj, PinName pin) {
2525
if(pin == (PinName)NC) return;
2626

2727
obj->pin = pin;
@@ -34,12 +34,6 @@ void gpio_init(gpio_t *obj, PinName pin, PinDirection direction) {
3434
obj->reg_clr = &reg->PCOR;
3535
obj->reg_in = &reg->PDIR;
3636
obj->reg_dir = &reg->PDDR;
37-
38-
gpio_dir(obj, direction);
39-
switch (direction) {
40-
case PIN_OUTPUT: pin_mode(pin, PullNone); break;
41-
case PIN_INPUT : pin_mode(pin, PullUp); break;
42-
}
4337
}
4438

4539
void gpio_mode(gpio_t *obj, PinMode mode) {

libraries/mbed/targets/hal/TARGET_NORDIC/TARGET_NRF51822/PinNames.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,8 @@ typedef enum {
142142
typedef enum {
143143
PullNone = 0,
144144
PullDown = 1,
145-
PullUp = 3
145+
PullUp = 3,
146+
PullDefault = PullUp
146147
} PinMode;
147148

148149
#ifdef __cplusplus

libraries/mbed/targets/hal/TARGET_NORDIC/TARGET_NRF51822/gpio_api.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#include "gpio_api.h"
1717
#include "pinmap.h"
1818

19-
void gpio_init(gpio_t *obj, PinName pin, PinDirection direction) {
19+
void gpio_init(gpio_t *obj, PinName pin) {
2020
if(pin == NC) return;
2121

2222
obj->pin = pin;
@@ -26,12 +26,6 @@ void gpio_init(gpio_t *obj, PinName pin, PinDirection direction) {
2626
obj->reg_clr = &NRF_GPIO->OUTCLR;
2727
obj->reg_in = &NRF_GPIO->IN;
2828
obj->reg_dir = &NRF_GPIO->DIR;
29-
30-
gpio_dir(obj, direction);
31-
switch (direction) {
32-
case PIN_OUTPUT: pin_mode(pin, PullNone); break;
33-
case PIN_INPUT : pin_mode(pin, PullUp); break;
34-
}
3529
}
3630

3731
void gpio_mode(gpio_t *obj, PinMode mode) {

libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC11UXX/TARGET_LPC11U24_301/PinNames.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,8 @@ typedef enum {
155155
PullDown = 1,
156156
PullNone = 0,
157157
Repeater = 3,
158-
OpenDrain = 4
158+
OpenDrain = 4,
159+
PullDefault = PullDown
159160
} PinMode;
160161

161162
#ifdef __cplusplus

libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC11UXX/TARGET_LPC11U24_401/PinNames.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,8 @@ typedef enum {
178178
PullDown = 1,
179179
PullNone = 0,
180180
Repeater = 3,
181-
OpenDrain = 4
181+
OpenDrain = 4,
182+
PullDefault = PullDown
182183
} PinMode;
183184

184185
#ifdef __cplusplus

libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC11UXX/TARGET_LPC11U35_401/PinNames.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,8 @@ typedef enum {
150150
PullDown = 1,
151151
PullNone = 0,
152152
Repeater = 3,
153-
OpenDrain = 4
153+
OpenDrain = 4,
154+
PullDefault = PullDown
154155
} PinMode;
155156

156157
#ifdef __cplusplus

libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC11UXX/gpio_api.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ uint32_t gpio_set(PinName pin) {
2727
return (1 << ((int)pin & 0x1F));
2828
}
2929

30-
void gpio_init(gpio_t *obj, PinName pin, PinDirection direction) {
30+
void gpio_init(gpio_t *obj, PinName pin) {
3131
if(pin == NC) return;
3232

3333
obj->pin = pin;
@@ -39,12 +39,6 @@ void gpio_init(gpio_t *obj, PinName pin, PinDirection direction) {
3939
obj->reg_clr = &LPC_GPIO->CLR[port];
4040
obj->reg_in = &LPC_GPIO->PIN[port];
4141
obj->reg_dir = &LPC_GPIO->DIR[port];
42-
43-
gpio_dir(obj, direction);
44-
switch (direction) {
45-
case PIN_OUTPUT: pin_mode(pin, PullNone); break;
46-
case PIN_INPUT : pin_mode(pin, PullDown); break;
47-
}
4842
}
4943

5044
void gpio_mode(gpio_t *obj, PinMode mode) {

0 commit comments

Comments
 (0)