t
Fork of mbed-dev by
targets/TARGET_NUVOTON/TARGET_NUC472/port_api.c@178:c26431f84b0d, 2017-11-09 (annotated)
- Committer:
- amithy
- Date:
- Thu Nov 09 22:14:37 2017 +0000
- Revision:
- 178:c26431f84b0d
- Parent:
- 149:156823d33999
test export
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
<> | 144:ef7eb2e8f9f7 | 1 | /* mbed Microcontroller Library |
<> | 144:ef7eb2e8f9f7 | 2 | * Copyright (c) 2015-2016 Nuvoton |
<> | 144:ef7eb2e8f9f7 | 3 | * |
<> | 144:ef7eb2e8f9f7 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
<> | 144:ef7eb2e8f9f7 | 5 | * you may not use this file except in compliance with the License. |
<> | 144:ef7eb2e8f9f7 | 6 | * You may obtain a copy of the License at |
<> | 144:ef7eb2e8f9f7 | 7 | * |
<> | 144:ef7eb2e8f9f7 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
<> | 144:ef7eb2e8f9f7 | 9 | * |
<> | 144:ef7eb2e8f9f7 | 10 | * Unless required by applicable law or agreed to in writing, software |
<> | 144:ef7eb2e8f9f7 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
<> | 144:ef7eb2e8f9f7 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
<> | 144:ef7eb2e8f9f7 | 13 | * See the License for the specific language governing permissions and |
<> | 144:ef7eb2e8f9f7 | 14 | * limitations under the License. |
<> | 144:ef7eb2e8f9f7 | 15 | */ |
<> | 144:ef7eb2e8f9f7 | 16 | |
<> | 144:ef7eb2e8f9f7 | 17 | #include "port_api.h" |
<> | 144:ef7eb2e8f9f7 | 18 | #include "gpio_api.h" |
<> | 144:ef7eb2e8f9f7 | 19 | #include "pinmap.h" |
<> | 144:ef7eb2e8f9f7 | 20 | #include "mbed_error.h" |
<> | 144:ef7eb2e8f9f7 | 21 | |
<> | 144:ef7eb2e8f9f7 | 22 | #if DEVICE_PORTIN || DEVICE_PORTOUT || DEVICE_PORTINOUT |
<> | 144:ef7eb2e8f9f7 | 23 | |
<> | 144:ef7eb2e8f9f7 | 24 | PinName port_pin(PortName port, int pin_n) |
<> | 144:ef7eb2e8f9f7 | 25 | { |
<> | 144:ef7eb2e8f9f7 | 26 | return (PinName) NU_PORT_N_PIN_TO_PINNAME(port, pin_n); |
<> | 144:ef7eb2e8f9f7 | 27 | } |
<> | 144:ef7eb2e8f9f7 | 28 | |
<> | 144:ef7eb2e8f9f7 | 29 | void port_init(port_t *obj, PortName port, int mask, PinDirection dir) |
<> | 144:ef7eb2e8f9f7 | 30 | { |
<> | 144:ef7eb2e8f9f7 | 31 | obj->port = port; |
<> | 144:ef7eb2e8f9f7 | 32 | obj->mask = mask; |
<> | 144:ef7eb2e8f9f7 | 33 | obj->direction = dir; |
<> | 144:ef7eb2e8f9f7 | 34 | |
<> | 144:ef7eb2e8f9f7 | 35 | uint32_t i; |
<> | 144:ef7eb2e8f9f7 | 36 | obj->direction = dir; |
<> | 144:ef7eb2e8f9f7 | 37 | for (i = 0; i < GPIO_PIN_MAX; i++) { |
<> | 144:ef7eb2e8f9f7 | 38 | if (obj->mask & (1 << i)) { |
<> | 144:ef7eb2e8f9f7 | 39 | gpio_set(port_pin(port, i)); |
<> | 144:ef7eb2e8f9f7 | 40 | } |
<> | 144:ef7eb2e8f9f7 | 41 | } |
<> | 144:ef7eb2e8f9f7 | 42 | |
<> | 144:ef7eb2e8f9f7 | 43 | port_dir(obj, dir); |
<> | 144:ef7eb2e8f9f7 | 44 | } |
<> | 144:ef7eb2e8f9f7 | 45 | |
<> | 144:ef7eb2e8f9f7 | 46 | void port_dir(port_t *obj, PinDirection dir) |
<> | 144:ef7eb2e8f9f7 | 47 | { |
<> | 144:ef7eb2e8f9f7 | 48 | uint32_t i; |
<> | 144:ef7eb2e8f9f7 | 49 | obj->direction = dir; |
<> | 144:ef7eb2e8f9f7 | 50 | for (i = 0; i < GPIO_PIN_MAX; i++) { |
<> | 144:ef7eb2e8f9f7 | 51 | if (obj->mask & (1 << i)) { |
<> | 144:ef7eb2e8f9f7 | 52 | if (dir == PIN_OUTPUT) { |
<> | 144:ef7eb2e8f9f7 | 53 | GPIO_SetMode(NU_PORT_BASE(obj->port), 1 << i, GPIO_MODE_OUTPUT); |
<> | 144:ef7eb2e8f9f7 | 54 | } else { // PIN_INPUT |
<> | 144:ef7eb2e8f9f7 | 55 | GPIO_SetMode(NU_PORT_BASE(obj->port), 1 << i, GPIO_MODE_INPUT); |
<> | 144:ef7eb2e8f9f7 | 56 | } |
<> | 144:ef7eb2e8f9f7 | 57 | } |
<> | 144:ef7eb2e8f9f7 | 58 | } |
<> | 144:ef7eb2e8f9f7 | 59 | } |
<> | 144:ef7eb2e8f9f7 | 60 | |
<> | 144:ef7eb2e8f9f7 | 61 | void port_mode(port_t *obj, PinMode mode) |
<> | 144:ef7eb2e8f9f7 | 62 | { |
<> | 144:ef7eb2e8f9f7 | 63 | uint32_t i; |
<> | 144:ef7eb2e8f9f7 | 64 | |
<> | 144:ef7eb2e8f9f7 | 65 | for (i = 0; i < GPIO_PIN_MAX; i++) { |
<> | 144:ef7eb2e8f9f7 | 66 | if (obj->mask & (1 << i)) { |
<> | 144:ef7eb2e8f9f7 | 67 | pin_mode(port_pin(obj->port, i), mode); |
<> | 144:ef7eb2e8f9f7 | 68 | } |
<> | 144:ef7eb2e8f9f7 | 69 | } |
<> | 144:ef7eb2e8f9f7 | 70 | } |
<> | 144:ef7eb2e8f9f7 | 71 | |
<> | 144:ef7eb2e8f9f7 | 72 | void port_write(port_t *obj, int value) |
<> | 144:ef7eb2e8f9f7 | 73 | { |
<> | 144:ef7eb2e8f9f7 | 74 | uint32_t i; |
<> | 144:ef7eb2e8f9f7 | 75 | uint32_t port_index = obj->port; |
<> | 144:ef7eb2e8f9f7 | 76 | |
<> | 144:ef7eb2e8f9f7 | 77 | for (i = 0; i < GPIO_PIN_MAX; i++) { |
<> | 144:ef7eb2e8f9f7 | 78 | if (obj->mask & (1 << i)) { |
<> | 144:ef7eb2e8f9f7 | 79 | GPIO_PIN_ADDR(port_index, i) = (value & obj->mask) ? 1 : 0; |
<> | 144:ef7eb2e8f9f7 | 80 | } |
<> | 144:ef7eb2e8f9f7 | 81 | } |
<> | 144:ef7eb2e8f9f7 | 82 | } |
<> | 144:ef7eb2e8f9f7 | 83 | |
<> | 144:ef7eb2e8f9f7 | 84 | int port_read(port_t *obj) |
<> | 144:ef7eb2e8f9f7 | 85 | { |
<> | 144:ef7eb2e8f9f7 | 86 | uint32_t i; |
<> | 144:ef7eb2e8f9f7 | 87 | uint32_t port_index = obj->port; |
<> | 144:ef7eb2e8f9f7 | 88 | int value = 0; |
<> | 144:ef7eb2e8f9f7 | 89 | |
<> | 144:ef7eb2e8f9f7 | 90 | for (i = 0; i < GPIO_PIN_MAX; i++) { |
<> | 144:ef7eb2e8f9f7 | 91 | if (obj->mask & (1 << i)) { |
<> | 144:ef7eb2e8f9f7 | 92 | value = value | (GPIO_PIN_ADDR(port_index, i) << i); |
<> | 144:ef7eb2e8f9f7 | 93 | } |
<> | 144:ef7eb2e8f9f7 | 94 | } |
<> | 144:ef7eb2e8f9f7 | 95 | |
<> | 144:ef7eb2e8f9f7 | 96 | return value; |
<> | 144:ef7eb2e8f9f7 | 97 | } |
<> | 144:ef7eb2e8f9f7 | 98 | |
<> | 144:ef7eb2e8f9f7 | 99 | #endif |