mbed library sources. Supersedes mbed-src. Edited target satm32f446 for user USART3 pins

Dependents:   IGLOO_board

Fork of mbed-dev by mbed official

Committer:
Anna Bridge
Date:
Fri Jun 22 16:45:37 2018 +0100
Revision:
186:707f6e361f3e
mbed-dev library. Release version 162

Who changed what in which revision?

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