Skip to content
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

proposed change of gpio_api (new update pull request) #198

Merged
merged 4 commits into from
Mar 10, 2014
Merged
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
11 changes: 9 additions & 2 deletions libraries/mbed/api/DigitalIn.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,19 @@ 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
*/
DigitalIn(PinName pin) {
gpio_init(&gpio, pin, PIN_INPUT);
gpio_init_in(&gpio, pin);
}

/** Create a DigitalIn connected to the specified pin
*
* @param pin DigitalIn pin to connect to
* @param mode the initial mode of the pin
*/
DigitalIn(PinName pin, PinMode mode) {
gpio_init_in_ex(&gpio, pin, mode);
}
/** Read the input, represented as 0 or 1 (int)
*
* @returns
Expand Down
13 changes: 12 additions & 1 deletion libraries/mbed/api/DigitalInOut.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,18 @@ class DigitalInOut {
* @param pin DigitalInOut pin to connect to
*/
DigitalInOut(PinName pin) {
gpio_init(&gpio, pin, PIN_INPUT);
gpio_init_in(&gpio, pin);
}

/** 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, PinDirection direction, PinMode mode, int value) {
gpio_init_inout(&gpio, pin, direction, mode, value);
}

/** Set the output, specified as 0 or 1 (int)
Expand Down
11 changes: 10 additions & 1 deletion libraries/mbed/api/DigitalOut.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,16 @@ class DigitalOut {
* @param pin DigitalOut pin to connect to
*/
DigitalOut(PinName pin) {
gpio_init(&gpio, pin, PIN_OUTPUT);
gpio_init_out(&gpio, pin);
}

/** Create a DigitalOut connected to the specified pin
*
* @param pin DigitalOut pin to connect to
* @param value the initial pin value
*/
DigitalOut(PinName pin, int value){
gpio_init_out_ex(&gpio, pin, 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);
}

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);

#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);
gpio_t led_2; gpio_init_out(&led_2, LED2);
gpio_t led_3; gpio_init_out(&led_3, LED3);
gpio_t led_4; gpio_init_out(&led_4, LED4);
#endif

while (1) {
Expand Down
56 changes: 56 additions & 0 deletions libraries/mbed/common/gpio.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/* mbed Microcontroller Library
* Copyright (c) 2006-2013 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "gpio_api.h"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you replace this preprocessor usage by a function call with the static inline specifiers? Will be easier to debug.

static inline void _gpio_init_in(gpio_t* gpio, PinName pin, PinMode mode)
{
gpio_init(gpio, pin);
gpio_mode(gpio, mode);
gpio_dir(gpio, PIN_INPUT);
}

static inline void _gpio_init_out(gpio_t* gpio, PinName pin, PinMode mode, int value)
{
gpio_init(gpio, pin);
gpio_write(gpio, value);
gpio_dir(gpio, PIN_OUTPUT);
gpio_mode(gpio, mode);
}

void gpio_init_in(gpio_t* gpio, PinName pin) {
gpio_init_in_ex(gpio, pin, PullDefault);
}

void gpio_init_in_ex(gpio_t* gpio, PinName pin, PinMode mode) {
_gpio_init_in(gpio, pin, mode);
}

void gpio_init_out(gpio_t* gpio, PinName pin) {
gpio_init_out_ex(gpio, pin, 0);
}

void gpio_init_out_ex(gpio_t* gpio, PinName pin, int value) {
_gpio_init_out(gpio, pin, PullNone, value);
}

void gpio_init_inout(gpio_t* gpio, PinName pin, PinDirection direction, PinMode mode, int value) {
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);
}
}
9 changes: 8 additions & 1 deletion libraries/mbed/hal/gpio_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,21 @@ 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);

// the following set of functions are generic and are implemented in the common gpio.c file
void gpio_init_in(gpio_t* gpio, PinName pin);
void gpio_init_in_ex(gpio_t* gpio, PinName pin, PinMode mode);
void gpio_init_out(gpio_t* gpio, PinName pin);
void gpio_init_out_ex(gpio_t* gpio, PinName pin, int value);
void gpio_init_inout(gpio_t* gpio, PinName pin, PinDirection direction, PinMode mode, int value);

#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);
}
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);
}
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);
}
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
Loading