fix for mbed lib issue 3 (i2c problem) see also https://mbed.org/users/mbed_official/code/mbed/issues/3 affected implementations: LPC812, LPC11U24, LPC1768, LPC2368, LPC4088

Fork of mbed-src by mbed official

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers port_api.c Source File

port_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 "port_api.h"
00017 #include "pinmap.h"
00018 #include "gpio_api.h"
00019 
00020 PinName port_pin(PortName port, int pin_n) {
00021     return (PinName)(LPC_GPIO0_BASE + ((port << PORT_SHIFT) | pin_n));
00022 }
00023 
00024 void port_init(port_t *obj, PortName port, int mask, PinDirection dir) {
00025     obj->port = port;
00026     obj->mask = mask;
00027     
00028     LPC_GPIO_TypeDef *port_reg = (LPC_GPIO_TypeDef *)(LPC_GPIO0_BASE + ((int)port * 0x20));
00029     
00030     // Do not use masking, because it prevents the use of the unmasked pins
00031     // port_reg->FIOMASK = ~mask;
00032     
00033     obj->reg_out = &port_reg->FIOPIN;
00034     obj->reg_in  = &port_reg->FIOPIN;
00035     obj->reg_dir  = &port_reg->FIODIR;
00036     
00037     uint32_t i;
00038     // The function is set per pin: reuse gpio logic
00039     for (i=0; i<32; i++) {
00040         if (obj->mask & (1<<i)) {
00041             gpio_set(port_pin(obj->port, i));
00042         }
00043     }
00044     
00045     port_dir(obj, dir);
00046 }
00047 
00048 void port_mode(port_t *obj, PinMode mode) {
00049     uint32_t i;
00050     // The mode is set per pin: reuse pinmap logic
00051     for (i=0; i<32; i++) {
00052         if (obj->mask & (1<<i)) {
00053             pin_mode(port_pin(obj->port, i), mode);
00054         }
00055     }
00056 }
00057 
00058 void port_dir(port_t *obj, PinDirection dir) {
00059     switch (dir) {
00060         case PIN_INPUT : *obj->reg_dir &= ~obj->mask; break;
00061         case PIN_OUTPUT: *obj->reg_dir |=  obj->mask; break;
00062     }
00063 }
00064 
00065 void port_write(port_t *obj, int value) {
00066     *obj->reg_out = (*obj->reg_in & ~obj->mask) | (value & obj->mask);
00067 }
00068 
00069 int port_read(port_t *obj) {
00070     return (*obj->reg_in & obj->mask);
00071 }