Tim Barry / mbed_blinky_offset
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers pinmap.c Source File

pinmap.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 "pinmap.h"
00017 #include "error.h"
00018 
00019 #if defined(TARGET_LPC11U24)
00020 #define LPC_IOCON0_BASE (LPC_IOCON_BASE)
00021 #define LPC_IOCON1_BASE (LPC_IOCON_BASE + 0x60)
00022 
00023 #elif defined(TARGET_LPC812)
00024 __IO uint32_t* IOCON_REGISTERS[18] = {
00025         &LPC_IOCON->PIO0_0 , &LPC_IOCON->PIO0_1 , &LPC_IOCON->PIO0_2 ,
00026         &LPC_IOCON->PIO0_3 , &LPC_IOCON->PIO0_4 , &LPC_IOCON->PIO0_5 ,
00027         &LPC_IOCON->PIO0_6 , &LPC_IOCON->PIO0_7 , &LPC_IOCON->PIO0_8 ,
00028         &LPC_IOCON->PIO0_9 , &LPC_IOCON->PIO0_10, &LPC_IOCON->PIO0_11,
00029         &LPC_IOCON->PIO0_12, &LPC_IOCON->PIO0_13, &LPC_IOCON->PIO0_14,
00030         &LPC_IOCON->PIO0_15, &LPC_IOCON->PIO0_16, &LPC_IOCON->PIO0_17,
00031 };
00032 #endif
00033 
00034 void pin_function(PinName pin, int function) {
00035     if (pin == (uint32_t)NC) return;
00036 
00037 #if defined(TARGET_LPC1768) || defined(TARGET_LPC2368)
00038     uint32_t pin_number = (uint32_t)pin - (uint32_t)P0_0;
00039     int index = pin_number >> 4;
00040     int offset = (pin_number & 0xF) << 1;
00041 
00042     PINCONARRAY->PINSEL[index] &= ~(0x3 << offset);
00043     PINCONARRAY->PINSEL[index] |= function << offset;
00044 
00045 #elif defined(TARGET_LPC11U24)
00046     uint32_t pin_number = (uint32_t)pin;
00047 
00048     __IO uint32_t *reg = (pin_number < 32) ?
00049             (__IO uint32_t*)(LPC_IOCON0_BASE + 4 * pin_number) :
00050             (__IO uint32_t*)(LPC_IOCON1_BASE + 4 * (pin_number - 32));
00051 
00052     // pin function bits: [2:0] -> 111 = (0x7)
00053     *reg = (*reg & ~0x7) | (function & 0x7);
00054 #endif
00055 }
00056 
00057 void pin_mode(PinName pin, PinMode mode) {
00058     if (pin == (uint32_t)NC) { return; }
00059 
00060 #if defined(TARGET_LPC1768) || defined(TARGET_LPC2368)
00061     uint32_t pin_number = (uint32_t)pin - (uint32_t)P0_0;
00062     int index = pin_number >> 5;
00063     int offset = pin_number & 0x1F;
00064     uint32_t drain = ((uint32_t) mode & (uint32_t) OpenDrain) >> 2;
00065 
00066 #if defined(TARGET_LPC2368)
00067     if (mode == OpenDrain) error("OpenDrain not supported on LPC2368");
00068 
00069 #elif defined(TARGET_LPC1768)
00070     PINCONARRAY->PINMODE_OD[index] &= ~(drain << offset);
00071     PINCONARRAY->PINMODE_OD[index] |= drain << offset;
00072 #endif
00073 
00074     if (!drain) {
00075         index = pin_number >> 4;
00076         offset = (pin_number & 0xF) << 1;
00077 
00078         PINCONARRAY->PINMODE[index] &= ~(0x3 << offset);
00079         PINCONARRAY->PINMODE[index] |= (uint32_t)mode << offset;
00080     }
00081 
00082 #elif defined(TARGET_LPC11U24)
00083     uint32_t pin_number = (uint32_t)pin;
00084     uint32_t drain = ((uint32_t) mode & (uint32_t) OpenDrain) >> 2;
00085 
00086     __IO uint32_t *reg = (pin_number < 32) ?
00087             (__IO uint32_t*)(LPC_IOCON0_BASE + 4 * pin_number) :
00088             (__IO uint32_t*)(LPC_IOCON1_BASE + 4 * (pin_number - 32));
00089     uint32_t tmp = *reg;
00090 
00091     // pin mode bits: [4:3] -> 11000 = (0x3 << 3)
00092     tmp &= ~(0x3 << 3);
00093     tmp |= (mode & 0x3) << 3;
00094 
00095     // drain
00096     tmp &= ~(0x1 << 10);
00097     tmp |= drain << 10;
00098 
00099     *reg = tmp;
00100 
00101 #elif defined(TARGET_LPC812)
00102     if ((pin == 10) || (pin == 11)) {
00103         // True open-drain pins can be configured for different I2C-bus speeds
00104         return;
00105     }
00106     
00107     __IO uint32_t *reg = IOCON_REGISTERS[pin];
00108     
00109     if (mode == OpenDrain) {
00110         *reg |= (1 << 10);
00111     } else {
00112         uint32_t tmp = *reg;
00113         tmp &= ~(0x3 << 3);
00114         tmp |= (mode & 0x3) << 3;
00115         *reg = tmp;
00116     }
00117 #endif
00118 }