t
Fork of mbed-dev by
targets/TARGET_Maxim/TARGET_MAX32630/mxc/gpio.c@178:c26431f84b0d, 2017-11-09 (annotated)
- Committer:
- amithy
- Date:
- Thu Nov 09 22:14:37 2017 +0000
- Revision:
- 178:c26431f84b0d
- Parent:
- 157:ff67d9f36b67
test export
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
<> | 157:ff67d9f36b67 | 1 | /** |
<> | 157:ff67d9f36b67 | 2 | * @file |
<> | 157:ff67d9f36b67 | 3 | * @brief This file contains the function implementations for the |
<> | 157:ff67d9f36b67 | 4 | * General-Purpose Input/Output (GPIO) peripheral module. |
<> | 157:ff67d9f36b67 | 5 | */ |
<> | 157:ff67d9f36b67 | 6 | |
<> | 157:ff67d9f36b67 | 7 | /* **************************************************************************** |
<> | 157:ff67d9f36b67 | 8 | * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. |
<> | 157:ff67d9f36b67 | 9 | * |
<> | 157:ff67d9f36b67 | 10 | * Permission is hereby granted, free of charge, to any person obtaining a |
<> | 157:ff67d9f36b67 | 11 | * copy of this software and associated documentation files (the "Software"), |
<> | 157:ff67d9f36b67 | 12 | * to deal in the Software without restriction, including without limitation |
<> | 157:ff67d9f36b67 | 13 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
<> | 157:ff67d9f36b67 | 14 | * and/or sell copies of the Software, and to permit persons to whom the |
<> | 157:ff67d9f36b67 | 15 | * Software is furnished to do so, subject to the following conditions: |
<> | 157:ff67d9f36b67 | 16 | * |
<> | 157:ff67d9f36b67 | 17 | * The above copyright notice and this permission notice shall be included |
<> | 157:ff67d9f36b67 | 18 | * in all copies or substantial portions of the Software. |
<> | 157:ff67d9f36b67 | 19 | * |
<> | 157:ff67d9f36b67 | 20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
<> | 157:ff67d9f36b67 | 21 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
<> | 157:ff67d9f36b67 | 22 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
<> | 157:ff67d9f36b67 | 23 | * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES |
<> | 157:ff67d9f36b67 | 24 | * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
<> | 157:ff67d9f36b67 | 25 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
<> | 157:ff67d9f36b67 | 26 | * OTHER DEALINGS IN THE SOFTWARE. |
<> | 157:ff67d9f36b67 | 27 | * |
<> | 157:ff67d9f36b67 | 28 | * Except as contained in this notice, the name of Maxim Integrated |
<> | 157:ff67d9f36b67 | 29 | * Products, Inc. shall not be used except as stated in the Maxim Integrated |
<> | 157:ff67d9f36b67 | 30 | * Products, Inc. Branding Policy. |
<> | 157:ff67d9f36b67 | 31 | * |
<> | 157:ff67d9f36b67 | 32 | * The mere transfer of this software does not imply any licenses |
<> | 157:ff67d9f36b67 | 33 | * of trade secrets, proprietary technology, copyrights, patents, |
<> | 157:ff67d9f36b67 | 34 | * trademarks, maskwork rights, or any other form of intellectual |
<> | 157:ff67d9f36b67 | 35 | * property whatsoever. Maxim Integrated Products, Inc. retains all |
<> | 157:ff67d9f36b67 | 36 | * ownership rights. |
<> | 157:ff67d9f36b67 | 37 | * |
<> | 157:ff67d9f36b67 | 38 | * $Date: 2016-09-09 11:41:02 -0500 (Fri, 09 Sep 2016) $ |
<> | 157:ff67d9f36b67 | 39 | * $Revision: 24337 $ |
<> | 157:ff67d9f36b67 | 40 | * |
<> | 157:ff67d9f36b67 | 41 | *************************************************************************** */ |
<> | 157:ff67d9f36b67 | 42 | |
<> | 157:ff67d9f36b67 | 43 | /* **** Includes **** */ |
<> | 157:ff67d9f36b67 | 44 | #include "mxc_config.h" |
<> | 157:ff67d9f36b67 | 45 | #include "mxc_assert.h" |
<> | 157:ff67d9f36b67 | 46 | #include "mxc_sys.h" |
<> | 157:ff67d9f36b67 | 47 | #include "gpio.h" |
<> | 157:ff67d9f36b67 | 48 | #include "clkman_regs.h" |
<> | 157:ff67d9f36b67 | 49 | |
<> | 157:ff67d9f36b67 | 50 | /** |
<> | 157:ff67d9f36b67 | 51 | * @ingroup gpio |
<> | 157:ff67d9f36b67 | 52 | * @{ |
<> | 157:ff67d9f36b67 | 53 | */ |
<> | 157:ff67d9f36b67 | 54 | |
<> | 157:ff67d9f36b67 | 55 | /* **** Definitions **** */ |
<> | 157:ff67d9f36b67 | 56 | |
<> | 157:ff67d9f36b67 | 57 | /* **** Globals **** */ |
<> | 157:ff67d9f36b67 | 58 | |
<> | 157:ff67d9f36b67 | 59 | /* ************************************************************************* */ |
<> | 157:ff67d9f36b67 | 60 | static void (*callbacks[MXC_GPIO_NUM_PORTS][MXC_GPIO_MAX_PINS_PER_PORT])(void *); |
<> | 157:ff67d9f36b67 | 61 | static void *cbparam[MXC_GPIO_NUM_PORTS][MXC_GPIO_MAX_PINS_PER_PORT]; |
<> | 157:ff67d9f36b67 | 62 | |
<> | 157:ff67d9f36b67 | 63 | /* **** Functions **** */ |
<> | 157:ff67d9f36b67 | 64 | |
<> | 157:ff67d9f36b67 | 65 | /* ************************************************************************* */ |
<> | 157:ff67d9f36b67 | 66 | static int PinConfig(unsigned int port, unsigned int pin, gpio_func_t func, gpio_pad_t pad) |
<> | 157:ff67d9f36b67 | 67 | { |
<> | 157:ff67d9f36b67 | 68 | /* Check if available */ |
<> | 157:ff67d9f36b67 | 69 | if (!(MXC_GPIO->free[port] & (1 << pin))) { |
<> | 157:ff67d9f36b67 | 70 | return E_BUSY; |
<> | 157:ff67d9f36b67 | 71 | } |
<> | 157:ff67d9f36b67 | 72 | |
<> | 157:ff67d9f36b67 | 73 | /* Set function */ |
<> | 157:ff67d9f36b67 | 74 | uint32_t func_sel = MXC_GPIO->func_sel[port]; |
<> | 157:ff67d9f36b67 | 75 | func_sel &= ~(0xF << (4 * pin)); |
<> | 157:ff67d9f36b67 | 76 | func_sel |= (func << (4 * pin)); |
<> | 157:ff67d9f36b67 | 77 | MXC_GPIO->func_sel[port] = func_sel; |
<> | 157:ff67d9f36b67 | 78 | |
<> | 157:ff67d9f36b67 | 79 | /* Normal input is always enabled */ |
<> | 157:ff67d9f36b67 | 80 | MXC_GPIO->in_mode[port] &= ~(0xF << (4 * pin)); |
<> | 157:ff67d9f36b67 | 81 | |
<> | 157:ff67d9f36b67 | 82 | /* Set requested output mode */ |
<> | 157:ff67d9f36b67 | 83 | uint32_t out_mode = MXC_GPIO->out_mode[port]; |
<> | 157:ff67d9f36b67 | 84 | out_mode &= ~(0xF << (4 * pin)); |
<> | 157:ff67d9f36b67 | 85 | out_mode |= (pad << (4 * pin)); |
<> | 157:ff67d9f36b67 | 86 | MXC_GPIO->out_mode[port] = out_mode; |
<> | 157:ff67d9f36b67 | 87 | |
<> | 157:ff67d9f36b67 | 88 | /* Enable the pull up/down if necessary */ |
<> | 157:ff67d9f36b67 | 89 | if (pad == MXC_V_GPIO_OUT_MODE_HIGH_Z_WEAK_PULLUP) { |
<> | 157:ff67d9f36b67 | 90 | MXC_GPIO->out_val[port] |= (1 << pin); |
<> | 157:ff67d9f36b67 | 91 | } else if (pad == MXC_V_GPIO_OUT_MODE_HIGH_Z_WEAK_PULLDOWN) { |
<> | 157:ff67d9f36b67 | 92 | MXC_GPIO->out_val[port] &= ~(1 << pin); |
<> | 157:ff67d9f36b67 | 93 | } |
<> | 157:ff67d9f36b67 | 94 | |
<> | 157:ff67d9f36b67 | 95 | return E_NO_ERROR; |
<> | 157:ff67d9f36b67 | 96 | } |
<> | 157:ff67d9f36b67 | 97 | |
<> | 157:ff67d9f36b67 | 98 | /* ************************************************************************* */ |
<> | 157:ff67d9f36b67 | 99 | int GPIO_Config(const gpio_cfg_t *cfg) |
<> | 157:ff67d9f36b67 | 100 | { |
<> | 157:ff67d9f36b67 | 101 | unsigned int pin; |
<> | 157:ff67d9f36b67 | 102 | int err = E_NO_ERROR; |
<> | 157:ff67d9f36b67 | 103 | |
<> | 157:ff67d9f36b67 | 104 | MXC_ASSERT(cfg); |
<> | 157:ff67d9f36b67 | 105 | MXC_ASSERT(cfg->port < MXC_GPIO_NUM_PORTS); |
<> | 157:ff67d9f36b67 | 106 | |
<> | 157:ff67d9f36b67 | 107 | // Set system level configurations |
<> | 157:ff67d9f36b67 | 108 | if ((err = SYS_GPIO_Init()) != E_NO_ERROR) { |
<> | 157:ff67d9f36b67 | 109 | return err; |
<> | 157:ff67d9f36b67 | 110 | } |
<> | 157:ff67d9f36b67 | 111 | |
<> | 157:ff67d9f36b67 | 112 | // Configure each pin in the mask |
<> | 157:ff67d9f36b67 | 113 | for (pin = 0; pin < MXC_GPIO_MAX_PINS_PER_PORT; pin++) { |
<> | 157:ff67d9f36b67 | 114 | if (cfg->mask & (1 << pin)) { |
<> | 157:ff67d9f36b67 | 115 | if (PinConfig(cfg->port, pin, cfg->func, cfg->pad) != E_NO_ERROR) { |
<> | 157:ff67d9f36b67 | 116 | err = E_BUSY; |
<> | 157:ff67d9f36b67 | 117 | } |
<> | 157:ff67d9f36b67 | 118 | } |
<> | 157:ff67d9f36b67 | 119 | } |
<> | 157:ff67d9f36b67 | 120 | |
<> | 157:ff67d9f36b67 | 121 | return err; |
<> | 157:ff67d9f36b67 | 122 | } |
<> | 157:ff67d9f36b67 | 123 | |
<> | 157:ff67d9f36b67 | 124 | /* ************************************************************************* */ |
<> | 157:ff67d9f36b67 | 125 | static void IntConfig(unsigned int port, unsigned int pin, gpio_int_mode_t mode) |
<> | 157:ff67d9f36b67 | 126 | { |
<> | 157:ff67d9f36b67 | 127 | uint32_t int_mode = MXC_GPIO->int_mode[port]; |
<> | 157:ff67d9f36b67 | 128 | int_mode &= ~(0xF << (pin*4)); |
<> | 157:ff67d9f36b67 | 129 | int_mode |= (mode << (pin*4)); |
<> | 157:ff67d9f36b67 | 130 | MXC_GPIO->int_mode[port] = int_mode; |
<> | 157:ff67d9f36b67 | 131 | } |
<> | 157:ff67d9f36b67 | 132 | |
<> | 157:ff67d9f36b67 | 133 | /* ************************************************************************* */ |
<> | 157:ff67d9f36b67 | 134 | void GPIO_IntConfig(const gpio_cfg_t *cfg, gpio_int_mode_t mode) |
<> | 157:ff67d9f36b67 | 135 | { |
<> | 157:ff67d9f36b67 | 136 | unsigned int pin; |
<> | 157:ff67d9f36b67 | 137 | |
<> | 157:ff67d9f36b67 | 138 | MXC_ASSERT(cfg); |
<> | 157:ff67d9f36b67 | 139 | MXC_ASSERT(cfg->port < MXC_GPIO_NUM_PORTS); |
<> | 157:ff67d9f36b67 | 140 | |
<> | 157:ff67d9f36b67 | 141 | // Configure each pin in the mask |
<> | 157:ff67d9f36b67 | 142 | for (pin = 0; pin < MXC_GPIO_MAX_PINS_PER_PORT; pin++) { |
<> | 157:ff67d9f36b67 | 143 | if (cfg->mask & (1 << pin)) { |
<> | 157:ff67d9f36b67 | 144 | IntConfig(cfg->port, pin, mode); |
<> | 157:ff67d9f36b67 | 145 | } |
<> | 157:ff67d9f36b67 | 146 | } |
<> | 157:ff67d9f36b67 | 147 | } |
<> | 157:ff67d9f36b67 | 148 | |
<> | 157:ff67d9f36b67 | 149 | /* ************************************************************************* */ |
<> | 157:ff67d9f36b67 | 150 | void GPIO_RegisterCallback(const gpio_cfg_t *cfg, gpio_callback_fn func, void *cbdata) |
<> | 157:ff67d9f36b67 | 151 | { |
<> | 157:ff67d9f36b67 | 152 | unsigned int pin; |
<> | 157:ff67d9f36b67 | 153 | |
<> | 157:ff67d9f36b67 | 154 | MXC_ASSERT(cfg); |
<> | 157:ff67d9f36b67 | 155 | MXC_ASSERT(cfg->port < MXC_GPIO_NUM_PORTS); |
<> | 157:ff67d9f36b67 | 156 | |
<> | 157:ff67d9f36b67 | 157 | for (pin = 0; pin < MXC_GPIO_MAX_PINS_PER_PORT; pin++) { |
<> | 157:ff67d9f36b67 | 158 | if (cfg->mask & (1 << pin)) { |
<> | 157:ff67d9f36b67 | 159 | callbacks[cfg->port][pin] = func; |
<> | 157:ff67d9f36b67 | 160 | cbparam[cfg->port][pin] = cbdata; |
<> | 157:ff67d9f36b67 | 161 | } |
<> | 157:ff67d9f36b67 | 162 | } |
<> | 157:ff67d9f36b67 | 163 | } |
<> | 157:ff67d9f36b67 | 164 | |
<> | 157:ff67d9f36b67 | 165 | /* ************************************************************************* */ |
<> | 157:ff67d9f36b67 | 166 | void GPIO_Handler(unsigned int port) |
<> | 157:ff67d9f36b67 | 167 | { |
<> | 157:ff67d9f36b67 | 168 | uint8_t intfl; |
<> | 157:ff67d9f36b67 | 169 | unsigned int pin; |
<> | 157:ff67d9f36b67 | 170 | |
<> | 157:ff67d9f36b67 | 171 | MXC_ASSERT(port < MXC_GPIO_NUM_PORTS); |
<> | 157:ff67d9f36b67 | 172 | |
<> | 157:ff67d9f36b67 | 173 | // Read and clear enabled interrupts. |
<> | 157:ff67d9f36b67 | 174 | intfl = MXC_GPIO->intfl[port]; |
<> | 157:ff67d9f36b67 | 175 | intfl &= MXC_GPIO->inten[port]; |
<> | 157:ff67d9f36b67 | 176 | MXC_GPIO->intfl[port] = intfl; |
<> | 157:ff67d9f36b67 | 177 | |
<> | 157:ff67d9f36b67 | 178 | // Process each pins' interrupt |
<> | 157:ff67d9f36b67 | 179 | for (pin = 0; pin < MXC_GPIO_MAX_PINS_PER_PORT; pin++) { |
<> | 157:ff67d9f36b67 | 180 | if ((intfl & (1 << pin)) && callbacks[port][pin]) { |
<> | 157:ff67d9f36b67 | 181 | callbacks[port][pin](cbparam[port][pin]); |
<> | 157:ff67d9f36b67 | 182 | } |
<> | 157:ff67d9f36b67 | 183 | } |
<> | 157:ff67d9f36b67 | 184 | } |
<> | 157:ff67d9f36b67 | 185 | |
<> | 157:ff67d9f36b67 | 186 | /**@} end of group gpio */ |