t
Fork of mbed-dev by
targets/TARGET_Silicon_Labs/TARGET_EFM32/gpio_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 | /***************************************************************************//** |
<> | 144:ef7eb2e8f9f7 | 2 | * @file gpio_api.c |
<> | 144:ef7eb2e8f9f7 | 3 | ******************************************************************************* |
<> | 144:ef7eb2e8f9f7 | 4 | * @section License |
<> | 144:ef7eb2e8f9f7 | 5 | * <b>(C) Copyright 2015 Silicon Labs, http://www.silabs.com</b> |
<> | 144:ef7eb2e8f9f7 | 6 | ******************************************************************************* |
<> | 144:ef7eb2e8f9f7 | 7 | * |
<> | 144:ef7eb2e8f9f7 | 8 | * SPDX-License-Identifier: Apache-2.0 |
<> | 144:ef7eb2e8f9f7 | 9 | * |
<> | 144:ef7eb2e8f9f7 | 10 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
<> | 144:ef7eb2e8f9f7 | 11 | * not use this file except in compliance with the License. |
<> | 144:ef7eb2e8f9f7 | 12 | * You may obtain a copy of the License at |
<> | 144:ef7eb2e8f9f7 | 13 | * |
<> | 144:ef7eb2e8f9f7 | 14 | * http://www.apache.org/licenses/LICENSE-2.0 |
<> | 144:ef7eb2e8f9f7 | 15 | * |
<> | 144:ef7eb2e8f9f7 | 16 | * Unless required by applicable law or agreed to in writing, software |
<> | 144:ef7eb2e8f9f7 | 17 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
<> | 144:ef7eb2e8f9f7 | 18 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
<> | 144:ef7eb2e8f9f7 | 19 | * See the License for the specific language governing permissions and |
<> | 144:ef7eb2e8f9f7 | 20 | * limitations under the License. |
<> | 144:ef7eb2e8f9f7 | 21 | * |
<> | 144:ef7eb2e8f9f7 | 22 | ******************************************************************************/ |
<> | 144:ef7eb2e8f9f7 | 23 | |
<> | 144:ef7eb2e8f9f7 | 24 | #include "gpio_api.h" |
<> | 144:ef7eb2e8f9f7 | 25 | #include "pinmap.h" |
<> | 144:ef7eb2e8f9f7 | 26 | #include "em_cmu.h" |
<> | 144:ef7eb2e8f9f7 | 27 | #include "mbed_assert.h" |
<> | 144:ef7eb2e8f9f7 | 28 | #include "sleepmodes.h" |
<> | 144:ef7eb2e8f9f7 | 29 | |
<> | 144:ef7eb2e8f9f7 | 30 | |
<> | 144:ef7eb2e8f9f7 | 31 | void gpio_write(gpio_t *obj, int value) |
<> | 144:ef7eb2e8f9f7 | 32 | { |
<> | 144:ef7eb2e8f9f7 | 33 | if (value) { |
<> | 144:ef7eb2e8f9f7 | 34 | GPIO_PinOutSet((GPIO_Port_TypeDef)(obj->pin >> 4 & 0xF), obj->pin & 0xF); // Pin number encoded in first four bits of obj->pin |
<> | 144:ef7eb2e8f9f7 | 35 | } else { |
<> | 144:ef7eb2e8f9f7 | 36 | GPIO_PinOutClear((GPIO_Port_TypeDef)(obj->pin >> 4 & 0xF), obj->pin & 0xF); |
<> | 144:ef7eb2e8f9f7 | 37 | } |
<> | 144:ef7eb2e8f9f7 | 38 | } |
<> | 144:ef7eb2e8f9f7 | 39 | |
<> | 144:ef7eb2e8f9f7 | 40 | int gpio_read(gpio_t *obj) |
<> | 144:ef7eb2e8f9f7 | 41 | { |
<> | 144:ef7eb2e8f9f7 | 42 | if (obj->dir == PIN_INPUT) { |
<> | 144:ef7eb2e8f9f7 | 43 | return GPIO_PinInGet((GPIO_Port_TypeDef)(obj->pin >> 4 & 0xF), obj->pin & 0xF); // Pin number encoded in first four bits of obj->pin |
<> | 144:ef7eb2e8f9f7 | 44 | } else { |
<> | 144:ef7eb2e8f9f7 | 45 | return GPIO_PinOutGet((GPIO_Port_TypeDef)(obj->pin >> 4 & 0xF), obj->pin & 0xF); |
<> | 144:ef7eb2e8f9f7 | 46 | } |
<> | 144:ef7eb2e8f9f7 | 47 | } |
<> | 144:ef7eb2e8f9f7 | 48 | |
<> | 144:ef7eb2e8f9f7 | 49 | int gpio_is_connected(const gpio_t *obj) |
<> | 144:ef7eb2e8f9f7 | 50 | { |
<> | 144:ef7eb2e8f9f7 | 51 | return ((uint32_t)obj->pin | 0xFFFFFF00 ) != (uint32_t)((PinName)NC); |
<> | 144:ef7eb2e8f9f7 | 52 | } |
<> | 144:ef7eb2e8f9f7 | 53 | |
<> | 144:ef7eb2e8f9f7 | 54 | /* |
<> | 144:ef7eb2e8f9f7 | 55 | * @return the GPIO port mask for this pin |
<> | 144:ef7eb2e8f9f7 | 56 | * Pin and port index encoded in one uint32. |
<> | 144:ef7eb2e8f9f7 | 57 | * First four bits represent the pin number |
<> | 144:ef7eb2e8f9f7 | 58 | * The remaining bits represent the pin mode |
<> | 144:ef7eb2e8f9f7 | 59 | */ |
<> | 144:ef7eb2e8f9f7 | 60 | uint32_t gpio_set(PinName pin) |
<> | 144:ef7eb2e8f9f7 | 61 | { |
<> | 144:ef7eb2e8f9f7 | 62 | return 1 << ((uint32_t) pin & 0xF); |
<> | 144:ef7eb2e8f9f7 | 63 | } |
<> | 144:ef7eb2e8f9f7 | 64 | |
<> | 144:ef7eb2e8f9f7 | 65 | void gpio_init(gpio_t *obj, PinName pin) |
<> | 144:ef7eb2e8f9f7 | 66 | { |
<> | 144:ef7eb2e8f9f7 | 67 | MBED_ASSERT(pin != NC); |
<> | 144:ef7eb2e8f9f7 | 68 | |
<> | 144:ef7eb2e8f9f7 | 69 | CMU_ClockEnable(cmuClock_HFPER, true); |
<> | 144:ef7eb2e8f9f7 | 70 | CMU_ClockEnable(cmuClock_GPIO, true); |
<> | 144:ef7eb2e8f9f7 | 71 | obj->pin = pin; |
<> | 144:ef7eb2e8f9f7 | 72 | } |
<> | 144:ef7eb2e8f9f7 | 73 | |
<> | 144:ef7eb2e8f9f7 | 74 | void gpio_mode(gpio_t *obj, PinMode mode) |
<> | 144:ef7eb2e8f9f7 | 75 | { |
<> | 144:ef7eb2e8f9f7 | 76 | uint32_t pin = 1 << (obj->pin & 0xF); |
<> | 144:ef7eb2e8f9f7 | 77 | uint32_t port = (obj->pin >> 4) & 0xF; |
<> | 144:ef7eb2e8f9f7 | 78 | |
<> | 144:ef7eb2e8f9f7 | 79 | if(obj->dir == PIN_INPUT) { |
<> | 144:ef7eb2e8f9f7 | 80 | switch(mode) { |
<> | 144:ef7eb2e8f9f7 | 81 | case PullDefault: |
<> | 144:ef7eb2e8f9f7 | 82 | mode = Input; |
<> | 144:ef7eb2e8f9f7 | 83 | break; |
<> | 144:ef7eb2e8f9f7 | 84 | case PullUp: |
<> | 144:ef7eb2e8f9f7 | 85 | mode = InputPullUp; |
<> | 144:ef7eb2e8f9f7 | 86 | break; |
<> | 144:ef7eb2e8f9f7 | 87 | case PullDown: |
<> | 144:ef7eb2e8f9f7 | 88 | mode = InputPullDown; |
<> | 144:ef7eb2e8f9f7 | 89 | break; |
<> | 144:ef7eb2e8f9f7 | 90 | default: |
<> | 144:ef7eb2e8f9f7 | 91 | break; |
<> | 144:ef7eb2e8f9f7 | 92 | } |
<> | 144:ef7eb2e8f9f7 | 93 | |
<> | 144:ef7eb2e8f9f7 | 94 | //Handle DOUT setting |
<> | 144:ef7eb2e8f9f7 | 95 | if((mode & 0x10) != 0) { |
<> | 144:ef7eb2e8f9f7 | 96 | //Set DOUT |
<> | 144:ef7eb2e8f9f7 | 97 | #ifdef _GPIO_P_DOUTSET_MASK |
<> | 144:ef7eb2e8f9f7 | 98 | GPIO->P[port].DOUTSET = pin; |
<> | 144:ef7eb2e8f9f7 | 99 | #else |
<> | 144:ef7eb2e8f9f7 | 100 | GPIO->P[port].DOUT |= pin; |
<> | 144:ef7eb2e8f9f7 | 101 | #endif |
<> | 144:ef7eb2e8f9f7 | 102 | } else { |
<> | 144:ef7eb2e8f9f7 | 103 | //Clear DOUT |
<> | 144:ef7eb2e8f9f7 | 104 | #ifdef _GPIO_P_DOUTCLR_MASK |
<> | 144:ef7eb2e8f9f7 | 105 | GPIO->P[port].DOUTCLR = pin; |
<> | 144:ef7eb2e8f9f7 | 106 | #else |
<> | 144:ef7eb2e8f9f7 | 107 | GPIO->P[port].DOUT &= ~pin; |
<> | 144:ef7eb2e8f9f7 | 108 | #endif |
<> | 144:ef7eb2e8f9f7 | 109 | } |
<> | 144:ef7eb2e8f9f7 | 110 | } else { |
<> | 144:ef7eb2e8f9f7 | 111 | switch(mode) { |
<> | 144:ef7eb2e8f9f7 | 112 | case PullDefault: |
<> | 144:ef7eb2e8f9f7 | 113 | mode = PushPull; |
<> | 144:ef7eb2e8f9f7 | 114 | break; |
<> | 144:ef7eb2e8f9f7 | 115 | case PullUp: |
<> | 144:ef7eb2e8f9f7 | 116 | mode = WiredAndPullUp; |
<> | 144:ef7eb2e8f9f7 | 117 | break; |
<> | 144:ef7eb2e8f9f7 | 118 | case PullDown: |
<> | 144:ef7eb2e8f9f7 | 119 | mode = WiredOrPullDown; |
<> | 144:ef7eb2e8f9f7 | 120 | break; |
<> | 144:ef7eb2e8f9f7 | 121 | default: |
<> | 144:ef7eb2e8f9f7 | 122 | break; |
<> | 144:ef7eb2e8f9f7 | 123 | } |
<> | 144:ef7eb2e8f9f7 | 124 | } |
<> | 144:ef7eb2e8f9f7 | 125 | |
<> | 144:ef7eb2e8f9f7 | 126 | obj->mode = mode; // Update object |
<> | 144:ef7eb2e8f9f7 | 127 | pin_mode(obj->pin, mode); // Update register |
<> | 144:ef7eb2e8f9f7 | 128 | } |
<> | 144:ef7eb2e8f9f7 | 129 | |
<> | 144:ef7eb2e8f9f7 | 130 | // Used by DigitalInOut to set correct mode when direction is set |
<> | 144:ef7eb2e8f9f7 | 131 | void gpio_dir(gpio_t *obj, PinDirection direction) |
<> | 144:ef7eb2e8f9f7 | 132 | { |
<> | 144:ef7eb2e8f9f7 | 133 | obj->dir = direction; |
<> | 144:ef7eb2e8f9f7 | 134 | switch (direction) { |
<> | 144:ef7eb2e8f9f7 | 135 | case PIN_INPUT: |
<> | 144:ef7eb2e8f9f7 | 136 | gpio_mode(obj, PullDefault); |
<> | 144:ef7eb2e8f9f7 | 137 | break; |
<> | 144:ef7eb2e8f9f7 | 138 | case PIN_OUTPUT: |
<> | 144:ef7eb2e8f9f7 | 139 | gpio_mode(obj, PullNone); |
<> | 144:ef7eb2e8f9f7 | 140 | break; |
<> | 144:ef7eb2e8f9f7 | 141 | } |
<> | 144:ef7eb2e8f9f7 | 142 | } |