t

Fork of mbed-dev by mbed official

Committer:
bogdanm
Date:
Thu Oct 01 15:25:22 2015 +0300
Revision:
0:9b334a45a8ff
Child:
144:ef7eb2e8f9f7
Initial commit on mbed-dev

Replaces mbed-src (now inactive)

Who changed what in which revision?

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