Greg Steiert / pegasus_dev

Dependents:   blinky_max32630fthr

Committer:
switches
Date:
Fri Nov 11 20:59:50 2016 +0000
Revision:
0:5c4d7b2438d3
Initial commit

Who changed what in which revision?

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