mirror of https://github.com/ARMmbed/mbed-os.git
				
				
				
			address concern from watarai-san about code size and performance by creating a set of common initialization functions
							parent
							
								
									be8bca4aa0
								
							
						
					
					
						commit
						c1d3cb5aa3
					
				| 
						 | 
				
			
			@ -49,12 +49,19 @@ public:
 | 
			
		|||
    /** 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 = PullDefault) {
 | 
			
		||||
        GPIO_INIT_IN(&gpio, pin, mode);
 | 
			
		||||
    DigitalIn(PinName pin) {
 | 
			
		||||
        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
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -27,6 +27,14 @@ namespace mbed {
 | 
			
		|||
class DigitalInOut {
 | 
			
		||||
 | 
			
		||||
public:
 | 
			
		||||
    /** Create a DigitalInOut connected to the specified pin
 | 
			
		||||
     *
 | 
			
		||||
     *  @param pin DigitalInOut pin to connect to
 | 
			
		||||
     */
 | 
			
		||||
    DigitalInOut(PinName pin) {
 | 
			
		||||
		gpio_init_in(&gpio, pin);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
    /** Create a DigitalInOut connected to the specified pin
 | 
			
		||||
     *
 | 
			
		||||
     *  @param pin DigitalInOut pin to connect to
 | 
			
		||||
| 
						 | 
				
			
			@ -34,14 +42,9 @@ public:
 | 
			
		|||
     *  @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 = 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);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    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)
 | 
			
		||||
     *
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -41,14 +41,22 @@ namespace mbed {
 | 
			
		|||
class DigitalOut {
 | 
			
		||||
 | 
			
		||||
public:
 | 
			
		||||
    /** Create a DigitalOut connected to the specified pin
 | 
			
		||||
     *
 | 
			
		||||
     *  @param pin DigitalOut pin to connect to
 | 
			
		||||
     */
 | 
			
		||||
    DigitalOut(PinName pin) {
 | 
			
		||||
        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 = 0) {
 | 
			
		||||
        GPIO_INIT_OUT(&gpio, pin, PullNone, value);
 | 
			
		||||
    }
 | 
			
		||||
    DigitalOut(PinName pin, int value){
 | 
			
		||||
        gpio_init_out_ex(&gpio, pin, value);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
    /** Set the output, specified as 0 or 1 (int)
 | 
			
		||||
     *
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -21,7 +21,7 @@ namespace mbed {
 | 
			
		|||
 | 
			
		||||
InterruptIn::InterruptIn(PinName pin) {
 | 
			
		||||
    gpio_irq_init(&gpio_irq, pin, (&InterruptIn::_irq_handler), (uint32_t)this);
 | 
			
		||||
    GPIO_INIT_IN(&gpio, pin, PullDefault);
 | 
			
		||||
    gpio_init_in(&gpio, pin);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
InterruptIn::~InterruptIn() {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -22,13 +22,13 @@ 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_OUT(&led_red, LED_RED, PullNone, 0);
 | 
			
		||||
    gpio_t led_red; gpio_init_out(&led_red, LED_RED);
 | 
			
		||||
    
 | 
			
		||||
#elif (DEVICE_ERROR_PATTERN == 1)
 | 
			
		||||
    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);
 | 
			
		||||
    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) {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -0,0 +1,52 @@
 | 
			
		|||
/* 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"
 | 
			
		||||
 | 
			
		||||
#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)
 | 
			
		||||
 | 
			
		||||
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 direction is switched later
 | 
			
		||||
    } else {
 | 
			
		||||
        GPIO_INIT_OUT(gpio, pin, mode, value);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -37,16 +37,12 @@ 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)
 | 
			
		||||
// 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
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -180,5 +180,5 @@ void gpio_irq_disable(gpio_irq_t *obj) {
 | 
			
		|||
void NMI_Handler(void)
 | 
			
		||||
{
 | 
			
		||||
    gpio_t gpio;
 | 
			
		||||
    GPIO_INIT_IN(&gpio, PTA4, PIN_INPUT, PullDefault);
 | 
			
		||||
    gpio_init_in(&gpio, PTA4);
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -170,5 +170,5 @@ void gpio_irq_disable(gpio_irq_t *obj) {
 | 
			
		|||
void NMI_Handler(void)
 | 
			
		||||
{
 | 
			
		||||
    gpio_t gpio;
 | 
			
		||||
    GPIO_INIT_IN(&gpio, PTA4, PullDefault);
 | 
			
		||||
    gpio_init_in(&gpio, PTA4);
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -190,5 +190,5 @@ void gpio_irq_disable(gpio_irq_t *obj) {
 | 
			
		|||
void NMI_Handler(void)
 | 
			
		||||
{
 | 
			
		||||
    gpio_t gpio;
 | 
			
		||||
    GPIO_INIT_IN(&gpio, PTA4, PIN_INPUT, PullDefault);
 | 
			
		||||
    gpio_init_in(&gpio, PTA4);
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -9,19 +9,19 @@ void mbed_sdk_init()
 | 
			
		|||
    gpio_t gpsEn, gpsRst, led, modemRts;
 | 
			
		||||
    
 | 
			
		||||
    // start with modem disabled 
 | 
			
		||||
    GPIO_INIT_OUT(&modemEn,     MDMEN,      PullNone, 0);
 | 
			
		||||
    GPIO_INIT_OUT(&modemRst,    MDMRST,     PullNone, 1);
 | 
			
		||||
    GPIO_INIT_OUT(&modemPwrOn,  MDMPWRON,   PullNone, 1);
 | 
			
		||||
    GPIO_INIT_OUT(&modemLvlOe,  MDMLVLOE,   PullNone, 1);
 | 
			
		||||
    GPIO_INIT_OUT(&modemILvlOe, MDMILVLOE,  PullNone, 0);
 | 
			
		||||
    GPIO_INIT_OUT(&modemUsbDet, MDMUSBDET,  PullNone, 0);
 | 
			
		||||
    GPIO_INIT_OUT(&modemRts,    MDMRTS,     PullNone, 0);
 | 
			
		||||
    gpio_init_out_ex(&modemEn, 	    MDMEN, 		PullNone, 0);
 | 
			
		||||
    gpio_init_out_ex(&modemRst, 	MDMRST, 	PullNone, 1);
 | 
			
		||||
    gpio_init_out_ex(&modemPwrOn, 	MDMPWRON, 	PullNone, 1);
 | 
			
		||||
    gpio_init_out_ex(&modemLvlOe, 	MDMLVLOE, 	PullNone, 1);
 | 
			
		||||
    gpio_init_out_ex(&modemILvlOe,  MDMILVLOE, 	PullNone, 0);
 | 
			
		||||
    gpio_init_out_ex(&modemUsbDet,  MDMUSBDET, 	PullNone, 0);
 | 
			
		||||
    gpio_init_out_ex(&modemRts, 	MDMRTS, 	PullNone, 0);
 | 
			
		||||
    // start with gps disabled 
 | 
			
		||||
    GPIO_INIT_OUT(&gpsEn,       GPSEN,      PullNone, 0);
 | 
			
		||||
    GPIO_INIT_OUT(&gpsRst,      GPSRST,     PullNone, 1);
 | 
			
		||||
    gpio_init_out_ex(&gpsEn, 		GPSEN, 		PullNone, 0);
 | 
			
		||||
    gpio_init_out_ex(&gpsRst, 		GPSRST, 	PullNone, 1);
 | 
			
		||||
    // led should be off
 | 
			
		||||
    GPIO_INIT_OUT(&led,         LED,        PullNone, 0);
 | 
			
		||||
    
 | 
			
		||||
    gpio_init_out_ex(&led, 		    LED, 		PullNone, 0);
 | 
			
		||||
      
 | 
			
		||||
    wait_ms(50); // when USB cable is inserted the interface chip issues 
 | 
			
		||||
    // multiple resets to the target CPU We wait here for a short period to 
 | 
			
		||||
    // prevent those resets from propagating to the modem and other 
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -412,7 +412,7 @@ void serial_set_flow_control(serial_t *obj, FlowControl type, PinName rxflow, Pi
 | 
			
		|||
            pinmap_pinout(txflow, PinMap_UART_CTS);
 | 
			
		||||
        } else {
 | 
			
		||||
            // Can't enable in hardware, use software emulation
 | 
			
		||||
            GPIO_INIT_IN(&uart_data[index].sw_cts, txflow, PullDown);
 | 
			
		||||
            gpio_init_in(&uart_data[index].sw_cts, txflow);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    if (((FlowControlRTS == type) || (FlowControlRTSCTS == type)) && (NC != rxflow)) {
 | 
			
		||||
| 
						 | 
				
			
			@ -427,7 +427,7 @@ void serial_set_flow_control(serial_t *obj, FlowControl type, PinName rxflow, Pi
 | 
			
		|||
            uart1->MCR |= UART_MCR_RTSEN_MASK;
 | 
			
		||||
            pinmap_pinout(rxflow, PinMap_UART_RTS);
 | 
			
		||||
        } else { // can't enable in hardware, use software emulation
 | 
			
		||||
            GPIO_INIT_OUT(&uart_data[index].sw_rts, rxflow, PullNone, 0);
 | 
			
		||||
            gpio_init_out_ex(&uart_data[index].sw_rts, rxflow, 0);
 | 
			
		||||
            // Enable RX interrupt
 | 
			
		||||
            serial_flow_irq_set(obj, 1);
 | 
			
		||||
        }
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue