rau cha / mbed-src-I2CWaitFix

Fork of mbed-src by mbed official

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers gpio_api.c Source File

gpio_api.c

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 #include "gpio_api.h"
00017 #include "pinmap.h"
00018 
00019 static int  gpio_enabled = 0;
00020 static void gpio_enable(void) {
00021     gpio_enabled = 1;
00022     
00023     /* Enable AHB clock to the GPIO domain. */
00024     LPC_SYSCON->SYSAHBCLKCTRL |= (1<<6);
00025     
00026     /* Peripheral reset control to GPIO and GPIO INT, a "1" bring it out of reset. */
00027     LPC_SYSCON->PRESETCTRL &= ~(0x1<<10);
00028     LPC_SYSCON->PRESETCTRL |=  (0x1<<10);
00029 }
00030 
00031 uint32_t gpio_set(PinName pin) {
00032     int f = 0;
00033     
00034     if (!gpio_enabled)
00035          gpio_enable();
00036     
00037     pin_function(pin, f);
00038     
00039     return (1 << ((int)pin & 0x1F));
00040 }
00041 
00042 void gpio_init(gpio_t *obj, PinName pin, PinDirection direction) {
00043     if(pin == NC) return;
00044     
00045     obj->pin = pin;
00046     obj->mask = gpio_set(pin);
00047     
00048     obj->reg_set = &LPC_GPIO_PORT->SET0;
00049     obj->reg_clr = &LPC_GPIO_PORT->CLR0;
00050     obj->reg_in  = &LPC_GPIO_PORT->PIN0;
00051     obj->reg_dir = &LPC_GPIO_PORT->DIR0;
00052     
00053     gpio_dir(obj, direction);
00054     switch (direction) {
00055         case PIN_OUTPUT: pin_mode(pin, PullNone); break;
00056         case PIN_INPUT : pin_mode(pin, PullDown); break;
00057     }
00058 }
00059 
00060 void gpio_mode(gpio_t *obj, PinMode mode) {
00061     pin_mode(obj->pin, mode);
00062 }
00063 
00064 void gpio_dir(gpio_t *obj, PinDirection direction) {
00065     switch (direction) {
00066         case PIN_INPUT : *obj->reg_dir &= ~obj->mask; break;
00067         case PIN_OUTPUT: *obj->reg_dir |=  obj->mask; break;
00068     }
00069 }