Skip to content

proposed change of gpio_api #197

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions libraries/mbed/api/DigitalIn.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ class DigitalIn {
/** Create a DigitalIn connected to the specified pin
*
* @param pin DigitalIn pin to connect to
* @param name (optional) A string to identify the object
* @param mode the initial mode of the pin
*/
DigitalIn(PinName pin) {
gpio_init(&gpio, pin, PIN_INPUT);
DigitalIn(PinName pin, PinMode mode = PullDefault) {
GPIO_INIT_IN(&gpio, pin, mode);
}

/** Read the input, represented as 0 or 1 (int)
Expand Down
12 changes: 10 additions & 2 deletions libraries/mbed/api/DigitalInOut.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,17 @@ class DigitalInOut {
/** Create a DigitalInOut connected to the specified pin
*
* @param pin DigitalInOut pin to connect to
* @param direction the initial direction of the pin
* @param mode the initial mode of the pin
* @param value the initial value of the pin if is an output
*/
DigitalInOut(PinName pin) {
gpio_init(&gpio, pin, PIN_INPUT);
DigitalInOut(PinName pin, PinDirection direction = PIN_INPUT, PinMode mode = PullDefault, int value = 0) {
if (direction == PIN_INPUT) {
GPIO_INIT_IN(&gpio, pin, mode);
gpio_write(&gpio, value); // we prepare the value in case it is switched later
} else {
GPIO_INIT_OUT(&gpio, pin, mode, value);
}
}

/** Set the output, specified as 0 or 1 (int)
Expand Down
5 changes: 3 additions & 2 deletions libraries/mbed/api/DigitalOut.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ class DigitalOut {
/** Create a DigitalOut connected to the specified pin
*
* @param pin DigitalOut pin to connect to
* @param value the initial pin value
*/
DigitalOut(PinName pin) {
gpio_init(&gpio, pin, PIN_OUTPUT);
DigitalOut(PinName pin, int value = 0) {
GPIO_INIT_OUT(&gpio, pin, PullNone, value);
}

/** Set the output, specified as 0 or 1 (int)
Expand Down
2 changes: 1 addition & 1 deletion libraries/mbed/common/InterruptIn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace mbed {

InterruptIn::InterruptIn(PinName pin) {
gpio_irq_init(&gpio_irq, pin, (&InterruptIn::_irq_handler), (uint32_t)this);
gpio_init(&gpio, pin, PIN_INPUT);
GPIO_INIT_IN(&gpio, pin, PullDefault);
}

InterruptIn::~InterruptIn() {
Expand Down
14 changes: 7 additions & 7 deletions libraries/mbed/common/board.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@
WEAK void mbed_die(void);
WEAK void mbed_die(void) {
__disable_irq(); // dont allow interrupts to disturb the flash pattern

#if (DEVICE_ERROR_RED == 1)
gpio_t led_red; gpio_init(&led_red, LED_RED, PIN_OUTPUT);

#if (DEVICE_ERROR_RED == 1)
gpio_t led_red; GPIO_INIT_OUT(&led_red, LED_RED, PullNone, 0);

#elif (DEVICE_ERROR_PATTERN == 1)
gpio_t led_1; gpio_init(&led_1, LED1, PIN_OUTPUT);
gpio_t led_2; gpio_init(&led_2, LED2, PIN_OUTPUT);
gpio_t led_3; gpio_init(&led_3, LED3, PIN_OUTPUT);
gpio_t led_4; gpio_init(&led_4, LED4, PIN_OUTPUT);
gpio_t led_1; GPIO_INIT_OUT(&led_1, LED1, PullNone, 0);
gpio_t led_2; GPIO_INIT_OUT(&led_2, LED2, PullNone, 0);
gpio_t led_3; GPIO_INIT_OUT(&led_3, LED3, PullNone, 0);
gpio_t led_4; GPIO_INIT_OUT(&led_4, LED4, PullNone, 0);
#endif

while (1) {
Expand Down
13 changes: 12 additions & 1 deletion libraries/mbed/hal/gpio_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,25 @@ extern "C" {
uint32_t gpio_set(PinName pin);

/* GPIO object */
void gpio_init (gpio_t *obj, PinName pin, PinDirection direction);
void gpio_init(gpio_t *obj, PinName pin);

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

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

#define GPIO_INIT_IN(obj, pin, mode) \
gpio_init(obj, pin), \
gpio_mode(obj, mode), \
gpio_dir(obj, PIN_INPUT)

#define GPIO_INIT_OUT(obj, pin, mode, value) \
gpio_init(obj, pin), \
gpio_write(obj, value), \
gpio_dir(obj, PIN_OUTPUT), \
gpio_mode(obj, mode)

#ifdef __cplusplus
}
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ typedef enum {
PullNone = 0,
PullDown = 2,
PullUp = 3,
PullDefault = PullUp
} PinMode;

#ifdef __cplusplus
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ uint32_t gpio_set(PinName pin) {
return 1 << ((pin & 0x7F) >> 2);
}

void gpio_init(gpio_t *obj, PinName pin, PinDirection direction) {
void gpio_init(gpio_t *obj, PinName pin) {
if(pin == NC)
return;

Expand All @@ -35,16 +35,6 @@ void gpio_init(gpio_t *obj, PinName pin, PinDirection direction) {
obj->reg_clr = &reg->PCOR;
obj->reg_in = &reg->PDIR;
obj->reg_dir = &reg->PDDR;

gpio_dir(obj, direction);
switch (direction) {
case PIN_OUTPUT:
pin_mode(pin, PullNone);
break;
case PIN_INPUT :
pin_mode(pin, PullUp);
break;
}
}

void gpio_mode(gpio_t *obj, PinMode mode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ typedef enum {
typedef enum {
PullNone = 0,
PullUp = 2,
PullDefault = PullUp
} PinMode;

#ifdef __cplusplus
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "cmsis.h"

#include "gpio_irq_api.h"
#include "gpio_api.h"
#include "error.h"

#define CHANNEL_NUM 64 // 31 pins on 2 ports
Expand Down Expand Up @@ -176,9 +177,8 @@ void gpio_irq_disable(gpio_irq_t *obj) {
// Change the NMI pin to an input. This allows NMI pin to
// be used as a low power mode wakeup. The application will
// need to change the pin back to NMI_b or wakeup only occurs once!
extern void gpio_init(gpio_t *obj, PinName pin, PinDirection direction);
void NMI_Handler(void)
{
gpio_t gpio;
gpio_init(&gpio, PTB5, PIN_INPUT);
GPIO_INIT_IN(&gpio, PTA4, PIN_INPUT, PullDefault);
}
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ typedef enum {
typedef enum {
PullNone = 0,
PullUp = 2,
PullDefault = PullUp
} PinMode;

#ifdef __cplusplus
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "cmsis.h"

#include "gpio_irq_api.h"
#include "gpio_api.h"
#include "error.h"

#define CHANNEL_NUM 64
Expand Down Expand Up @@ -166,9 +167,8 @@ void gpio_irq_disable(gpio_irq_t *obj) {
// Change the NMI pin to an input. This allows NMI pin to
// be used as a low power mode wakeup. The application will
// need to change the pin back to NMI_b or wakeup only occurs once!
extern void gpio_init(gpio_t *obj, PinName pin, PinDirection direction);
void NMI_Handler(void)
{
gpio_t gpio;
gpio_init(&gpio, PTA4, PIN_INPUT);
GPIO_INIT_IN(&gpio, PTA4, PullDefault);
}
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,8 @@ typedef enum {
typedef enum {
PullNone = 0,
PullDown = 2,
PullUp = 3
PullUp = 3,
PullDefault = PullUp
} PinMode;

#ifdef __cplusplus
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "cmsis.h"

#include "gpio_irq_api.h"
#include "gpio_api.h"
#include "error.h"

#define CHANNEL_NUM 96
Expand Down Expand Up @@ -186,9 +187,8 @@ void gpio_irq_disable(gpio_irq_t *obj) {
// Change the NMI pin to an input. This allows NMI pin to
// be used as a low power mode wakeup. The application will
// need to change the pin back to NMI_b or wakeup only occurs once!
extern void gpio_init(gpio_t *obj, PinName pin, PinDirection direction);
void NMI_Handler(void)
{
gpio_t gpio;
gpio_init(&gpio, PTA4, PIN_INPUT);
GPIO_INIT_IN(&gpio, PTA4, PIN_INPUT, PullDefault);
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ uint32_t gpio_set(PinName pin) {
return 1 << ((pin & 0x7F) >> 2);
}

void gpio_init(gpio_t *obj, PinName pin, PinDirection direction) {
void gpio_init(gpio_t *obj, PinName pin) {
if(pin == (PinName)NC) return;

obj->pin = pin;
Expand All @@ -34,12 +34,6 @@ void gpio_init(gpio_t *obj, PinName pin, PinDirection direction) {
obj->reg_clr = &reg->PCOR;
obj->reg_in = &reg->PDIR;
obj->reg_dir = &reg->PDDR;

gpio_dir(obj, direction);
switch (direction) {
case PIN_OUTPUT: pin_mode(pin, PullNone); break;
case PIN_INPUT : pin_mode(pin, PullUp); break;
}
}

void gpio_mode(gpio_t *obj, PinMode mode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ typedef enum {
typedef enum {
PullNone = 0,
PullDown = 1,
PullUp = 3
PullUp = 3,
PullDefault = PullUp
} PinMode;

#ifdef __cplusplus
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#include "gpio_api.h"
#include "pinmap.h"

void gpio_init(gpio_t *obj, PinName pin, PinDirection direction) {
void gpio_init(gpio_t *obj, PinName pin) {
if(pin == NC) return;

obj->pin = pin;
Expand All @@ -26,12 +26,6 @@ void gpio_init(gpio_t *obj, PinName pin, PinDirection direction) {
obj->reg_clr = &NRF_GPIO->OUTCLR;
obj->reg_in = &NRF_GPIO->IN;
obj->reg_dir = &NRF_GPIO->DIR;

gpio_dir(obj, direction);
switch (direction) {
case PIN_OUTPUT: pin_mode(pin, PullNone); break;
case PIN_INPUT : pin_mode(pin, PullUp); break;
}
}

void gpio_mode(gpio_t *obj, PinMode mode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ typedef enum {
PullDown = 1,
PullNone = 0,
Repeater = 3,
OpenDrain = 4
OpenDrain = 4,
PullDefault = PullDown
} PinMode;

#ifdef __cplusplus
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,8 @@ typedef enum {
PullDown = 1,
PullNone = 0,
Repeater = 3,
OpenDrain = 4
OpenDrain = 4,
PullDefault = PullDown
} PinMode;

#ifdef __cplusplus
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,8 @@ typedef enum {
PullDown = 1,
PullNone = 0,
Repeater = 3,
OpenDrain = 4
OpenDrain = 4,
PullDefault = PullDown
} PinMode;

#ifdef __cplusplus
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ uint32_t gpio_set(PinName pin) {
return (1 << ((int)pin & 0x1F));
}

void gpio_init(gpio_t *obj, PinName pin, PinDirection direction) {
void gpio_init(gpio_t *obj, PinName pin) {
if(pin == NC) return;

obj->pin = pin;
Expand All @@ -39,12 +39,6 @@ void gpio_init(gpio_t *obj, PinName pin, PinDirection direction) {
obj->reg_clr = &LPC_GPIO->CLR[port];
obj->reg_in = &LPC_GPIO->PIN[port];
obj->reg_dir = &LPC_GPIO->DIR[port];

gpio_dir(obj, direction);
switch (direction) {
case PIN_OUTPUT: pin_mode(pin, PullNone); break;
case PIN_INPUT : pin_mode(pin, PullDown); break;
}
}

void gpio_mode(gpio_t *obj, PinMode mode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,8 @@ typedef enum {
PullDown = 1,
PullNone = 0,
Repeater = 3,
OpenDrain = 4
OpenDrain = 4,
PullDefault = PullDown
} PinMode;

#ifdef __cplusplus
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,8 @@ typedef enum {
PullDown = 1,
PullNone = 0,
Repeater = 3,
OpenDrain = 4
OpenDrain = 4,
PullDefault = PullDown
} PinMode;

#ifdef __cplusplus
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ uint32_t gpio_set(PinName pin) {
return ((pin & 0x0F00) >> 8);
}

void gpio_init(gpio_t *obj, PinName pin, PinDirection direction) {
void gpio_init(gpio_t *obj, PinName pin) {
if(pin == NC) return;

obj->pin = pin;
Expand All @@ -43,13 +43,6 @@ void gpio_init(gpio_t *obj, PinName pin, PinDirection direction) {
obj->reg_mask_read = &port_reg->MASKED_ACCESS[1 << gpio_set(pin)];
obj->reg_dir = &port_reg->DIR;
obj->reg_write = &port_reg->DATA;

gpio_dir(obj, direction);

switch (direction) {
case PIN_OUTPUT: pin_mode(pin, PullNone); break;
case PIN_INPUT : pin_mode(pin, PullDown); break;
}
}

void gpio_mode(gpio_t *obj, PinMode mode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ typedef enum {
PullDown = 1,
PullNone = 0,
Repeater = 3,
OpenDrain = 4
OpenDrain = 4,
PullDefault = PullDown
} PinMode;

#ifdef __cplusplus
Expand Down
Loading