Tim Barry / mbed_blinky_offset
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 #if defined(TARGET_LPC2368)
00024     LPC_SC->SCS |= 1; // High speed GPIO is enabled on ports 0 and 1
00025 #elif defined(TARGET_LPC812)
00026     /* Enable AHB clock to the GPIO domain. */
00027     LPC_SYSCON->SYSAHBCLKCTRL |= (1<<6);
00028     
00029     /* Peripheral reset control to GPIO and GPIO INT, a "1" bring it out of reset. */
00030     LPC_SYSCON->PRESETCTRL &= ~(0x1<<10);
00031     LPC_SYSCON->PRESETCTRL |=  (0x1<<10);
00032 #endif
00033 }
00034 
00035 uint32_t gpio_set(PinName pin) {
00036     int f = 0;
00037     
00038     if (!gpio_enabled)
00039          gpio_enable();
00040     
00041 #if defined(TARGET_LPC11U24)
00042     f = ((pin == P0_11) ||
00043          (pin == P0_12) ||
00044          (pin == P0_13) ||
00045          (pin == P0_14)) ? (1) : (0);
00046 #endif
00047     
00048     pin_function(pin, f);
00049     
00050     return (1 << ((int)pin & 0x1F));
00051 }
00052 
00053 void gpio_init(gpio_t *obj, PinName pin, PinDirection direction) {
00054     if(pin == NC) return;
00055 
00056     obj->pin = pin;
00057     obj->mask = gpio_set(pin);
00058 
00059 #if defined(TARGET_LPC1768) || defined(TARGET_LPC2368)
00060     LPC_GPIO_TypeDef *port_reg = (LPC_GPIO_TypeDef *) ((int)pin & ~0x1F);
00061 
00062     obj->reg_set = &port_reg->FIOSET;
00063     obj->reg_clr = &port_reg->FIOCLR;
00064     obj->reg_in  = &port_reg->FIOPIN;
00065     obj->reg_dir = &port_reg->FIODIR;
00066 
00067 #elif defined(TARGET_LPC11U24)
00068     unsigned int port = (unsigned int)pin >> PORT_SHIFT;
00069 
00070     obj->reg_set = &LPC_GPIO->SET[port];
00071     obj->reg_clr = &LPC_GPIO->CLR[port];
00072     obj->reg_in  = &LPC_GPIO->PIN[port];
00073     obj->reg_dir = &LPC_GPIO->DIR[port];
00074 
00075 #elif defined(TARGET_LPC812)
00076     obj->reg_set = &LPC_GPIO_PORT->SET0;
00077     obj->reg_clr = &LPC_GPIO_PORT->CLR0;
00078     obj->reg_in  = &LPC_GPIO_PORT->PIN0;
00079     obj->reg_dir = &LPC_GPIO_PORT->DIR0;
00080 #endif
00081 
00082     gpio_dir(obj, direction);
00083     switch (direction) {
00084         case PIN_OUTPUT: pin_mode(pin, PullNone); break;
00085         case PIN_INPUT : pin_mode(pin, PullDown); break;
00086     }
00087 }
00088 
00089 void gpio_mode(gpio_t *obj, PinMode mode) {
00090     pin_mode(obj->pin, mode);
00091 }
00092 
00093 void gpio_dir(gpio_t *obj, PinDirection direction) {
00094     gpio_set(obj->pin);
00095     switch (direction) {
00096         case PIN_INPUT : *obj->reg_dir &= ~obj->mask; break;
00097         case PIN_OUTPUT: *obj->reg_dir |=  obj->mask; break;
00098     }
00099 }