Alessandro Angelino / target-freescale-ksdk

Fork of target-freescale-ksdk by Morpheus

Committer:
Alessandro Angelino
Date:
Mon Apr 04 14:18:16 2016 +0100
Revision:
1:d01108809007
Parent:
0:e4d670b91a9a
Replace NVIC APIs with vIRQ ones

Who changed what in which revision?

UserRevisionLine numberNew contents of line
screamer 0:e4d670b91a9a 1 /* mbed Microcontroller Library
screamer 0:e4d670b91a9a 2 * Copyright (c) 2006-2013 ARM Limited
screamer 0:e4d670b91a9a 3 *
screamer 0:e4d670b91a9a 4 * Licensed under the Apache License, Version 2.0 (the "License");
screamer 0:e4d670b91a9a 5 * you may not use this file except in compliance with the License.
screamer 0:e4d670b91a9a 6 * You may obtain a copy of the License at
screamer 0:e4d670b91a9a 7 *
screamer 0:e4d670b91a9a 8 * http://www.apache.org/licenses/LICENSE-2.0
screamer 0:e4d670b91a9a 9 *
screamer 0:e4d670b91a9a 10 * Unless required by applicable law or agreed to in writing, software
screamer 0:e4d670b91a9a 11 * distributed under the License is distributed on an "AS IS" BASIS,
screamer 0:e4d670b91a9a 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
screamer 0:e4d670b91a9a 13 * See the License for the specific language governing permissions and
screamer 0:e4d670b91a9a 14 * limitations under the License.
screamer 0:e4d670b91a9a 15 */
screamer 0:e4d670b91a9a 16 #include "port_api.h"
screamer 0:e4d670b91a9a 17
screamer 0:e4d670b91a9a 18 #if DEVICE_PORTIN || DEVICE_PORTOUT
screamer 0:e4d670b91a9a 19
screamer 0:e4d670b91a9a 20 #include "pinmap.h"
screamer 0:e4d670b91a9a 21 #include "gpio_api.h"
screamer 0:e4d670b91a9a 22
screamer 0:e4d670b91a9a 23 PinName port_pin(PortName port, int pin_n) {
screamer 0:e4d670b91a9a 24 return (PinName)((port << GPIO_PORT_SHIFT) | pin_n);
screamer 0:e4d670b91a9a 25 }
screamer 0:e4d670b91a9a 26
screamer 0:e4d670b91a9a 27 void port_init(port_t *obj, PortName port, int mask, PinDirection dir) {
screamer 0:e4d670b91a9a 28 obj->port = port;
screamer 0:e4d670b91a9a 29 obj->mask = mask;
screamer 0:e4d670b91a9a 30
screamer 0:e4d670b91a9a 31 // The function is set per pin: reuse gpio logic
screamer 0:e4d670b91a9a 32 for (uint32_t i = 0; i < 32; i++) {
screamer 0:e4d670b91a9a 33 if (obj->mask & (1 << i)) {
screamer 0:e4d670b91a9a 34 gpio_set(port_pin(obj->port, i));
screamer 0:e4d670b91a9a 35 }
screamer 0:e4d670b91a9a 36 }
screamer 0:e4d670b91a9a 37
screamer 0:e4d670b91a9a 38 port_dir(obj, dir);
screamer 0:e4d670b91a9a 39 }
screamer 0:e4d670b91a9a 40
screamer 0:e4d670b91a9a 41 void port_mode(port_t *obj, PinMode mode) {
screamer 0:e4d670b91a9a 42
screamer 0:e4d670b91a9a 43 // The mode is set per pin: reuse pinmap logic
screamer 0:e4d670b91a9a 44 for (uint32_t i = 0; i < 32; i++) {
screamer 0:e4d670b91a9a 45 if (obj->mask & (1 << i)) {
screamer 0:e4d670b91a9a 46 pin_mode(port_pin(obj->port, i), mode);
screamer 0:e4d670b91a9a 47 }
screamer 0:e4d670b91a9a 48 }
screamer 0:e4d670b91a9a 49 }
screamer 0:e4d670b91a9a 50
screamer 0:e4d670b91a9a 51 void port_dir(port_t *obj, PinDirection dir) {
screamer 0:e4d670b91a9a 52 uint32_t port_addrs[] = PORT_BASE_ADDRS;
screamer 0:e4d670b91a9a 53 uint32_t direction = GPIO_HAL_GetPortDir(port_addrs[obj->port]);
screamer 0:e4d670b91a9a 54 switch (dir) {
screamer 0:e4d670b91a9a 55 case PIN_INPUT :
screamer 0:e4d670b91a9a 56 direction &= ~obj->mask;
screamer 0:e4d670b91a9a 57 GPIO_HAL_SetPortDir(port_addrs[obj->port], direction);
screamer 0:e4d670b91a9a 58 break;
screamer 0:e4d670b91a9a 59 case PIN_OUTPUT:
screamer 0:e4d670b91a9a 60 direction |= obj->mask;
screamer 0:e4d670b91a9a 61 GPIO_HAL_SetPortDir(port_addrs[obj->port], direction);
screamer 0:e4d670b91a9a 62 break;
screamer 0:e4d670b91a9a 63 }
screamer 0:e4d670b91a9a 64 }
screamer 0:e4d670b91a9a 65
screamer 0:e4d670b91a9a 66 void port_write(port_t *obj, int value) {
screamer 0:e4d670b91a9a 67 uint32_t port_addrs[] = PORT_BASE_ADDRS;
screamer 0:e4d670b91a9a 68 uint32_t input = GPIO_HAL_ReadPortInput(port_addrs[obj->port]) & ~obj->mask;
screamer 0:e4d670b91a9a 69 GPIO_HAL_WritePortOutput(port_addrs[obj->port], input | (uint32_t)(value & obj->mask));
screamer 0:e4d670b91a9a 70 }
screamer 0:e4d670b91a9a 71
screamer 0:e4d670b91a9a 72 int port_read(port_t *obj) {
screamer 0:e4d670b91a9a 73 uint32_t port_addrs[] = PORT_BASE_ADDRS;
screamer 0:e4d670b91a9a 74 return (int)(GPIO_HAL_ReadPortInput(port_addrs[obj->port]) & obj->mask);
screamer 0:e4d670b91a9a 75 }
screamer 0:e4d670b91a9a 76
screamer 0:e4d670b91a9a 77 #endif