helpfor studient
Dependents: STM32_F103-C8T6basecanblink_led
Fork of mbed-dev by
targets/TARGET_NUVOTON/TARGET_M451/gpio_api.c@182:57724642e740, 2018-02-16 (annotated)
- Committer:
- AnnaBridge
- Date:
- Fri Feb 16 16:09:33 2018 +0000
- Revision:
- 182:57724642e740
- Parent:
- 149:156823d33999
mbed-dev library. Release version 159.
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 "gpio_api.h" |
<> | 144:ef7eb2e8f9f7 | 18 | #include "mbed_assert.h" |
<> | 144:ef7eb2e8f9f7 | 19 | #include "pinmap.h" |
<> | 144:ef7eb2e8f9f7 | 20 | #include "mbed_error.h" |
<> | 144:ef7eb2e8f9f7 | 21 | #include "PeripheralPins.h" |
<> | 144:ef7eb2e8f9f7 | 22 | |
<> | 144:ef7eb2e8f9f7 | 23 | uint32_t gpio_set(PinName pin) |
<> | 144:ef7eb2e8f9f7 | 24 | { |
<> | 144:ef7eb2e8f9f7 | 25 | if (pin == (PinName) NC) { |
<> | 144:ef7eb2e8f9f7 | 26 | return 0; |
<> | 144:ef7eb2e8f9f7 | 27 | } |
<> | 144:ef7eb2e8f9f7 | 28 | |
<> | 144:ef7eb2e8f9f7 | 29 | uint32_t pin_index = NU_PINNAME_TO_PIN(pin); |
<> | 144:ef7eb2e8f9f7 | 30 | |
<> | 144:ef7eb2e8f9f7 | 31 | #if 1 |
<> | 144:ef7eb2e8f9f7 | 32 | pin_function(pin, 0 << NU_MFP_POS(pin_index)); |
<> | 144:ef7eb2e8f9f7 | 33 | #else |
<> | 144:ef7eb2e8f9f7 | 34 | pinmap_pinout(pin, PinMap_GPIO); |
<> | 144:ef7eb2e8f9f7 | 35 | #endif |
<> | 144:ef7eb2e8f9f7 | 36 | |
<> | 144:ef7eb2e8f9f7 | 37 | return (uint32_t)(1 << pin_index); // Return the pin mask |
<> | 144:ef7eb2e8f9f7 | 38 | } |
<> | 144:ef7eb2e8f9f7 | 39 | |
AnnaBridge | 182:57724642e740 | 40 | int gpio_is_connected(const gpio_t *obj) |
AnnaBridge | 182:57724642e740 | 41 | { |
AnnaBridge | 182:57724642e740 | 42 | return (obj->pin != (PinName) NC); |
AnnaBridge | 182:57724642e740 | 43 | } |
AnnaBridge | 182:57724642e740 | 44 | |
<> | 144:ef7eb2e8f9f7 | 45 | void gpio_init(gpio_t *obj, PinName pin) |
<> | 144:ef7eb2e8f9f7 | 46 | { |
<> | 144:ef7eb2e8f9f7 | 47 | obj->pin = pin; |
<> | 144:ef7eb2e8f9f7 | 48 | |
<> | 144:ef7eb2e8f9f7 | 49 | if (obj->pin == (PinName) NC) { |
<> | 144:ef7eb2e8f9f7 | 50 | return; |
<> | 144:ef7eb2e8f9f7 | 51 | } |
<> | 144:ef7eb2e8f9f7 | 52 | |
<> | 144:ef7eb2e8f9f7 | 53 | obj->mask = gpio_set(pin); |
<> | 144:ef7eb2e8f9f7 | 54 | } |
<> | 144:ef7eb2e8f9f7 | 55 | |
<> | 144:ef7eb2e8f9f7 | 56 | void gpio_mode(gpio_t *obj, PinMode mode) |
<> | 144:ef7eb2e8f9f7 | 57 | { |
<> | 144:ef7eb2e8f9f7 | 58 | if (obj->pin == (PinName) NC) { |
<> | 144:ef7eb2e8f9f7 | 59 | return; |
<> | 144:ef7eb2e8f9f7 | 60 | } |
<> | 144:ef7eb2e8f9f7 | 61 | |
<> | 144:ef7eb2e8f9f7 | 62 | pin_mode(obj->pin, mode); |
<> | 144:ef7eb2e8f9f7 | 63 | } |
<> | 144:ef7eb2e8f9f7 | 64 | |
<> | 144:ef7eb2e8f9f7 | 65 | void gpio_dir(gpio_t *obj, PinDirection direction) |
<> | 144:ef7eb2e8f9f7 | 66 | { |
<> | 144:ef7eb2e8f9f7 | 67 | if (obj->pin == (PinName) NC) { |
<> | 144:ef7eb2e8f9f7 | 68 | return; |
<> | 144:ef7eb2e8f9f7 | 69 | } |
<> | 144:ef7eb2e8f9f7 | 70 | |
<> | 144:ef7eb2e8f9f7 | 71 | uint32_t pin_index = NU_PINNAME_TO_PIN(obj->pin); |
<> | 144:ef7eb2e8f9f7 | 72 | uint32_t port_index = NU_PINNAME_TO_PORT(obj->pin); |
<> | 144:ef7eb2e8f9f7 | 73 | GPIO_T *gpio_base = NU_PORT_BASE(port_index); |
<> | 144:ef7eb2e8f9f7 | 74 | |
<> | 144:ef7eb2e8f9f7 | 75 | uint32_t mode_intern = GPIO_MODE_INPUT; |
<> | 144:ef7eb2e8f9f7 | 76 | |
<> | 144:ef7eb2e8f9f7 | 77 | switch (direction) { |
<> | 144:ef7eb2e8f9f7 | 78 | case PIN_INPUT: |
<> | 144:ef7eb2e8f9f7 | 79 | mode_intern = GPIO_MODE_INPUT; |
<> | 144:ef7eb2e8f9f7 | 80 | break; |
<> | 144:ef7eb2e8f9f7 | 81 | |
<> | 144:ef7eb2e8f9f7 | 82 | case PIN_OUTPUT: |
<> | 144:ef7eb2e8f9f7 | 83 | mode_intern = GPIO_MODE_OUTPUT; |
<> | 144:ef7eb2e8f9f7 | 84 | break; |
<> | 144:ef7eb2e8f9f7 | 85 | |
<> | 144:ef7eb2e8f9f7 | 86 | default: |
<> | 144:ef7eb2e8f9f7 | 87 | return; |
<> | 144:ef7eb2e8f9f7 | 88 | } |
<> | 144:ef7eb2e8f9f7 | 89 | |
<> | 144:ef7eb2e8f9f7 | 90 | GPIO_SetMode(gpio_base, 1 << pin_index, mode_intern); |
<> | 144:ef7eb2e8f9f7 | 91 | } |