Skip to content

Commit be8bca4

Browse files
committed
proposed change of gpio_api
1 parent db13fa3 commit be8bca4

File tree

51 files changed

+121
-213
lines changed

Some content is hidden

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

51 files changed

+121
-213
lines changed

libraries/mbed/api/DigitalIn.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ 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
52+
* @param mode the initial mode of the pin
5353
*/
54-
DigitalIn(PinName pin) {
55-
gpio_init(&gpio, pin, PIN_INPUT);
54+
DigitalIn(PinName pin, PinMode mode = PullDefault) {
55+
GPIO_INIT_IN(&gpio, pin, mode);
5656
}
5757

5858
/** Read the input, represented as 0 or 1 (int)

libraries/mbed/api/DigitalInOut.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,17 @@ class DigitalInOut {
3030
/** Create a DigitalInOut connected to the specified pin
3131
*
3232
* @param pin DigitalInOut pin to connect to
33+
* @param direction the initial direction of the pin
34+
* @param mode the initial mode of the pin
35+
* @param value the initial value of the pin if is an output
3336
*/
34-
DigitalInOut(PinName pin) {
35-
gpio_init(&gpio, pin, PIN_INPUT);
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+
}
3644
}
3745

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

libraries/mbed/api/DigitalOut.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,10 @@ class DigitalOut {
4444
/** Create a DigitalOut connected to the specified pin
4545
*
4646
* @param pin DigitalOut pin to connect to
47+
* @param value the initial pin value
4748
*/
48-
DigitalOut(PinName pin) {
49-
gpio_init(&gpio, pin, PIN_OUTPUT);
49+
DigitalOut(PinName pin, int value = 0) {
50+
GPIO_INIT_OUT(&gpio, pin, PullNone, value);
5051
}
5152

5253
/** 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, PullDefault);
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, PullNone, 0);
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, 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);
3232
#endif
3333

3434
while (1) {

libraries/mbed/hal/gpio_api.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,25 @@ 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+
#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)
50+
4051
#ifdef __cplusplus
4152
}
4253
#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, PIN_INPUT, PullDefault);
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, PullDefault);
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, PIN_INPUT, PullDefault);
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) {

libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC11XX_11CXX/TARGET_LPC11CXX/PinNames.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,8 @@ typedef enum {
208208
PullDown = 1,
209209
PullNone = 0,
210210
Repeater = 3,
211-
OpenDrain = 4
211+
OpenDrain = 4,
212+
PullDefault = PullDown
212213
} PinMode;
213214

214215
#ifdef __cplusplus

libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC11XX_11CXX/TARGET_LPC11XX/PinNames.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,8 @@ typedef enum {
223223
PullDown = 1,
224224
PullNone = 0,
225225
Repeater = 3,
226-
OpenDrain = 4
226+
OpenDrain = 4,
227+
PullDefault = PullDown
227228
} PinMode;
228229

229230
#ifdef __cplusplus

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

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ uint32_t gpio_set(PinName pin) {
3434
return ((pin & 0x0F00) >> 8);
3535
}
3636

37-
void gpio_init(gpio_t *obj, PinName pin, PinDirection direction) {
37+
void gpio_init(gpio_t *obj, PinName pin) {
3838
if(pin == NC) return;
3939

4040
obj->pin = pin;
@@ -43,13 +43,6 @@ void gpio_init(gpio_t *obj, PinName pin, PinDirection direction) {
4343
obj->reg_mask_read = &port_reg->MASKED_ACCESS[1 << gpio_set(pin)];
4444
obj->reg_dir = &port_reg->DIR;
4545
obj->reg_write = &port_reg->DATA;
46-
47-
gpio_dir(obj, direction);
48-
49-
switch (direction) {
50-
case PIN_OUTPUT: pin_mode(pin, PullNone); break;
51-
case PIN_INPUT : pin_mode(pin, PullDown); break;
52-
}
5346
}
5447

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

libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC13XX/PinNames.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,8 @@ typedef enum {
140140
PullDown = 1,
141141
PullNone = 0,
142142
Repeater = 3,
143-
OpenDrain = 4
143+
OpenDrain = 4,
144+
PullDefault = PullDown
144145
} PinMode;
145146

146147
#ifdef __cplusplus

0 commit comments

Comments
 (0)