Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of Application by
mbed-dev/targets/TARGET_Silicon_Labs/TARGET_EFM32/emlib/src/em_gpio.c@10:41552d038a69, 2017-01-10 (annotated)
- Committer:
- Zaitsev
- Date:
- Tue Jan 10 20:42:26 2017 +0000
- Revision:
- 10:41552d038a69
USB Serial bi-directional bridge
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| Zaitsev | 10:41552d038a69 | 1 | /***************************************************************************//** |
| Zaitsev | 10:41552d038a69 | 2 | * @file em_gpio.c |
| Zaitsev | 10:41552d038a69 | 3 | * @brief General Purpose IO (GPIO) peripheral API |
| Zaitsev | 10:41552d038a69 | 4 | * devices. |
| Zaitsev | 10:41552d038a69 | 5 | * @version 5.0.0 |
| Zaitsev | 10:41552d038a69 | 6 | ******************************************************************************* |
| Zaitsev | 10:41552d038a69 | 7 | * @section License |
| Zaitsev | 10:41552d038a69 | 8 | * <b>Copyright 2016 Silicon Laboratories, Inc. http://www.silabs.com</b> |
| Zaitsev | 10:41552d038a69 | 9 | ******************************************************************************* |
| Zaitsev | 10:41552d038a69 | 10 | * |
| Zaitsev | 10:41552d038a69 | 11 | * Permission is granted to anyone to use this software for any purpose, |
| Zaitsev | 10:41552d038a69 | 12 | * including commercial applications, and to alter it and redistribute it |
| Zaitsev | 10:41552d038a69 | 13 | * freely, subject to the following restrictions: |
| Zaitsev | 10:41552d038a69 | 14 | * |
| Zaitsev | 10:41552d038a69 | 15 | * 1. The origin of this software must not be misrepresented; you must not |
| Zaitsev | 10:41552d038a69 | 16 | * claim that you wrote the original software. |
| Zaitsev | 10:41552d038a69 | 17 | * 2. Altered source versions must be plainly marked as such, and must not be |
| Zaitsev | 10:41552d038a69 | 18 | * misrepresented as being the original software. |
| Zaitsev | 10:41552d038a69 | 19 | * 3. This notice may not be removed or altered from any source distribution. |
| Zaitsev | 10:41552d038a69 | 20 | * |
| Zaitsev | 10:41552d038a69 | 21 | * DISCLAIMER OF WARRANTY/LIMITATION OF REMEDIES: Silicon Labs has no |
| Zaitsev | 10:41552d038a69 | 22 | * obligation to support this Software. Silicon Labs is providing the |
| Zaitsev | 10:41552d038a69 | 23 | * Software "AS IS", with no express or implied warranties of any kind, |
| Zaitsev | 10:41552d038a69 | 24 | * including, but not limited to, any implied warranties of merchantability |
| Zaitsev | 10:41552d038a69 | 25 | * or fitness for any particular purpose or warranties against infringement |
| Zaitsev | 10:41552d038a69 | 26 | * of any proprietary rights of a third party. |
| Zaitsev | 10:41552d038a69 | 27 | * |
| Zaitsev | 10:41552d038a69 | 28 | * Silicon Labs will not be liable for any consequential, incidental, or |
| Zaitsev | 10:41552d038a69 | 29 | * special damages, or any other relief, or for any claim by any third party, |
| Zaitsev | 10:41552d038a69 | 30 | * arising from your use of this Software. |
| Zaitsev | 10:41552d038a69 | 31 | * |
| Zaitsev | 10:41552d038a69 | 32 | ******************************************************************************/ |
| Zaitsev | 10:41552d038a69 | 33 | |
| Zaitsev | 10:41552d038a69 | 34 | |
| Zaitsev | 10:41552d038a69 | 35 | #include "em_gpio.h" |
| Zaitsev | 10:41552d038a69 | 36 | |
| Zaitsev | 10:41552d038a69 | 37 | #if defined(GPIO_COUNT) && (GPIO_COUNT > 0) |
| Zaitsev | 10:41552d038a69 | 38 | |
| Zaitsev | 10:41552d038a69 | 39 | /***************************************************************************//** |
| Zaitsev | 10:41552d038a69 | 40 | * @addtogroup emlib |
| Zaitsev | 10:41552d038a69 | 41 | * @{ |
| Zaitsev | 10:41552d038a69 | 42 | ******************************************************************************/ |
| Zaitsev | 10:41552d038a69 | 43 | |
| Zaitsev | 10:41552d038a69 | 44 | /***************************************************************************//** |
| Zaitsev | 10:41552d038a69 | 45 | * @addtogroup GPIO |
| Zaitsev | 10:41552d038a69 | 46 | * @brief General Purpose Input/Output (GPIO) API |
| Zaitsev | 10:41552d038a69 | 47 | * @details |
| Zaitsev | 10:41552d038a69 | 48 | * This module contains functions to control the GPIO peripheral of Silicon |
| Zaitsev | 10:41552d038a69 | 49 | * Labs 32-bit MCUs and SoCs. The GPIO peripheral is used for pin configuration |
| Zaitsev | 10:41552d038a69 | 50 | * and direct pin manipulation and sensing as well as routing for peripheral |
| Zaitsev | 10:41552d038a69 | 51 | * pin connections. |
| Zaitsev | 10:41552d038a69 | 52 | * @{ |
| Zaitsev | 10:41552d038a69 | 53 | ******************************************************************************/ |
| Zaitsev | 10:41552d038a69 | 54 | |
| Zaitsev | 10:41552d038a69 | 55 | /******************************************************************************* |
| Zaitsev | 10:41552d038a69 | 56 | ******************************* DEFINES *********************************** |
| Zaitsev | 10:41552d038a69 | 57 | ******************************************************************************/ |
| Zaitsev | 10:41552d038a69 | 58 | |
| Zaitsev | 10:41552d038a69 | 59 | /** @cond DO_NOT_INCLUDE_WITH_DOXYGEN */ |
| Zaitsev | 10:41552d038a69 | 60 | |
| Zaitsev | 10:41552d038a69 | 61 | /** Validation of pin typically usable in assert statements. */ |
| Zaitsev | 10:41552d038a69 | 62 | #define GPIO_DRIVEMODE_VALID(mode) ((mode) <= 3) |
| Zaitsev | 10:41552d038a69 | 63 | #define GPIO_STRENGHT_VALID(strenght) (!((strenght) & \ |
| Zaitsev | 10:41552d038a69 | 64 | ~(_GPIO_P_CTRL_DRIVESTRENGTH_MASK \ |
| Zaitsev | 10:41552d038a69 | 65 | | _GPIO_P_CTRL_DRIVESTRENGTHALT_MASK))) |
| Zaitsev | 10:41552d038a69 | 66 | /** @endcond */ |
| Zaitsev | 10:41552d038a69 | 67 | |
| Zaitsev | 10:41552d038a69 | 68 | |
| Zaitsev | 10:41552d038a69 | 69 | /******************************************************************************* |
| Zaitsev | 10:41552d038a69 | 70 | ************************** GLOBAL FUNCTIONS ******************************* |
| Zaitsev | 10:41552d038a69 | 71 | ******************************************************************************/ |
| Zaitsev | 10:41552d038a69 | 72 | |
| Zaitsev | 10:41552d038a69 | 73 | /***************************************************************************//** |
| Zaitsev | 10:41552d038a69 | 74 | * @brief |
| Zaitsev | 10:41552d038a69 | 75 | * Sets the pin location of the debug pins (Serial Wire interface). |
| Zaitsev | 10:41552d038a69 | 76 | * |
| Zaitsev | 10:41552d038a69 | 77 | * @note |
| Zaitsev | 10:41552d038a69 | 78 | * Changing the pins used for debugging uncontrolled, may result in a lockout. |
| Zaitsev | 10:41552d038a69 | 79 | * |
| Zaitsev | 10:41552d038a69 | 80 | * @param[in] location |
| Zaitsev | 10:41552d038a69 | 81 | * The debug pin location to use (0-3). |
| Zaitsev | 10:41552d038a69 | 82 | ******************************************************************************/ |
| Zaitsev | 10:41552d038a69 | 83 | void GPIO_DbgLocationSet(unsigned int location) |
| Zaitsev | 10:41552d038a69 | 84 | { |
| Zaitsev | 10:41552d038a69 | 85 | #if defined ( _GPIO_ROUTE_SWLOCATION_MASK ) |
| Zaitsev | 10:41552d038a69 | 86 | EFM_ASSERT(location < AFCHANLOC_MAX); |
| Zaitsev | 10:41552d038a69 | 87 | |
| Zaitsev | 10:41552d038a69 | 88 | GPIO->ROUTE = (GPIO->ROUTE & ~_GPIO_ROUTE_SWLOCATION_MASK) | |
| Zaitsev | 10:41552d038a69 | 89 | (location << _GPIO_ROUTE_SWLOCATION_SHIFT); |
| Zaitsev | 10:41552d038a69 | 90 | #else |
| Zaitsev | 10:41552d038a69 | 91 | (void)location; |
| Zaitsev | 10:41552d038a69 | 92 | #endif |
| Zaitsev | 10:41552d038a69 | 93 | } |
| Zaitsev | 10:41552d038a69 | 94 | |
| Zaitsev | 10:41552d038a69 | 95 | #if defined (_GPIO_P_CTRL_DRIVEMODE_MASK) |
| Zaitsev | 10:41552d038a69 | 96 | /***************************************************************************//** |
| Zaitsev | 10:41552d038a69 | 97 | * @brief |
| Zaitsev | 10:41552d038a69 | 98 | * Sets the drive mode for a GPIO port. |
| Zaitsev | 10:41552d038a69 | 99 | * |
| Zaitsev | 10:41552d038a69 | 100 | * @param[in] port |
| Zaitsev | 10:41552d038a69 | 101 | * The GPIO port to access. |
| Zaitsev | 10:41552d038a69 | 102 | * |
| Zaitsev | 10:41552d038a69 | 103 | * @param[in] mode |
| Zaitsev | 10:41552d038a69 | 104 | * Drive mode to use for port. |
| Zaitsev | 10:41552d038a69 | 105 | ******************************************************************************/ |
| Zaitsev | 10:41552d038a69 | 106 | void GPIO_DriveModeSet(GPIO_Port_TypeDef port, GPIO_DriveMode_TypeDef mode) |
| Zaitsev | 10:41552d038a69 | 107 | { |
| Zaitsev | 10:41552d038a69 | 108 | EFM_ASSERT(GPIO_PORT_VALID(port) && GPIO_DRIVEMODE_VALID(mode)); |
| Zaitsev | 10:41552d038a69 | 109 | |
| Zaitsev | 10:41552d038a69 | 110 | GPIO->P[port].CTRL = (GPIO->P[port].CTRL & ~(_GPIO_P_CTRL_DRIVEMODE_MASK)) |
| Zaitsev | 10:41552d038a69 | 111 | | (mode << _GPIO_P_CTRL_DRIVEMODE_SHIFT); |
| Zaitsev | 10:41552d038a69 | 112 | } |
| Zaitsev | 10:41552d038a69 | 113 | #endif |
| Zaitsev | 10:41552d038a69 | 114 | |
| Zaitsev | 10:41552d038a69 | 115 | #if defined (_GPIO_P_CTRL_DRIVESTRENGTH_MASK) |
| Zaitsev | 10:41552d038a69 | 116 | /***************************************************************************//** |
| Zaitsev | 10:41552d038a69 | 117 | * @brief |
| Zaitsev | 10:41552d038a69 | 118 | * Sets the drive strength for a GPIO port. |
| Zaitsev | 10:41552d038a69 | 119 | * |
| Zaitsev | 10:41552d038a69 | 120 | * @param[in] port |
| Zaitsev | 10:41552d038a69 | 121 | * The GPIO port to access. |
| Zaitsev | 10:41552d038a69 | 122 | * |
| Zaitsev | 10:41552d038a69 | 123 | * @param[in] strength |
| Zaitsev | 10:41552d038a69 | 124 | * Drive strength to use for port. |
| Zaitsev | 10:41552d038a69 | 125 | ******************************************************************************/ |
| Zaitsev | 10:41552d038a69 | 126 | void GPIO_DriveStrengthSet(GPIO_Port_TypeDef port, |
| Zaitsev | 10:41552d038a69 | 127 | GPIO_DriveStrength_TypeDef strength) |
| Zaitsev | 10:41552d038a69 | 128 | { |
| Zaitsev | 10:41552d038a69 | 129 | EFM_ASSERT(GPIO_PORT_VALID(port) && GPIO_STRENGHT_VALID(strength)); |
| Zaitsev | 10:41552d038a69 | 130 | BUS_RegMaskedWrite(&GPIO->P[port].CTRL, |
| Zaitsev | 10:41552d038a69 | 131 | _GPIO_P_CTRL_DRIVESTRENGTH_MASK | _GPIO_P_CTRL_DRIVESTRENGTHALT_MASK, |
| Zaitsev | 10:41552d038a69 | 132 | strength); |
| Zaitsev | 10:41552d038a69 | 133 | } |
| Zaitsev | 10:41552d038a69 | 134 | #endif |
| Zaitsev | 10:41552d038a69 | 135 | |
| Zaitsev | 10:41552d038a69 | 136 | /***************************************************************************//** |
| Zaitsev | 10:41552d038a69 | 137 | * @brief |
| Zaitsev | 10:41552d038a69 | 138 | * Configure GPIO external pin interrupt. |
| Zaitsev | 10:41552d038a69 | 139 | * |
| Zaitsev | 10:41552d038a69 | 140 | * @details |
| Zaitsev | 10:41552d038a69 | 141 | * If reconfiguring a GPIO interrupt that is already enabled, it is generally |
| Zaitsev | 10:41552d038a69 | 142 | * recommended to disable it first, see GPIO_Disable(). |
| Zaitsev | 10:41552d038a69 | 143 | * |
| Zaitsev | 10:41552d038a69 | 144 | * The actual GPIO interrupt handler must be in place before enabling the |
| Zaitsev | 10:41552d038a69 | 145 | * interrupt. |
| Zaitsev | 10:41552d038a69 | 146 | * |
| Zaitsev | 10:41552d038a69 | 147 | * Notice that any pending interrupt for the selected interrupt is cleared |
| Zaitsev | 10:41552d038a69 | 148 | * by this function. |
| Zaitsev | 10:41552d038a69 | 149 | * |
| Zaitsev | 10:41552d038a69 | 150 | * @note |
| Zaitsev | 10:41552d038a69 | 151 | * On platform 1 devices the pin number parameter is not used. The |
| Zaitsev | 10:41552d038a69 | 152 | * pin number used on these devices is hardwired to the interrupt with the |
| Zaitsev | 10:41552d038a69 | 153 | * same number. @n |
| Zaitsev | 10:41552d038a69 | 154 | * On platform 2 devices, pin number can be selected freely within a group. |
| Zaitsev | 10:41552d038a69 | 155 | * Interrupt numbers are divided into 4 groups (intNo / 4) and valid pin |
| Zaitsev | 10:41552d038a69 | 156 | * number within the interrupt groups are: |
| Zaitsev | 10:41552d038a69 | 157 | * 0: pins 0-3 |
| Zaitsev | 10:41552d038a69 | 158 | * 1: pins 4-7 |
| Zaitsev | 10:41552d038a69 | 159 | * 2: pins 8-11 |
| Zaitsev | 10:41552d038a69 | 160 | * 3: pins 12-15 |
| Zaitsev | 10:41552d038a69 | 161 | * |
| Zaitsev | 10:41552d038a69 | 162 | * @param[in] port |
| Zaitsev | 10:41552d038a69 | 163 | * The port to associate with @p pin. |
| Zaitsev | 10:41552d038a69 | 164 | * |
| Zaitsev | 10:41552d038a69 | 165 | * @param[in] pin |
| Zaitsev | 10:41552d038a69 | 166 | * The pin number on the port. |
| Zaitsev | 10:41552d038a69 | 167 | * |
| Zaitsev | 10:41552d038a69 | 168 | * @param[in] intNo |
| Zaitsev | 10:41552d038a69 | 169 | * The interrupt number to trigger. |
| Zaitsev | 10:41552d038a69 | 170 | * |
| Zaitsev | 10:41552d038a69 | 171 | * @param[in] risingEdge |
| Zaitsev | 10:41552d038a69 | 172 | * Set to true if interrupts shall be enabled on rising edge, otherwise false. |
| Zaitsev | 10:41552d038a69 | 173 | * |
| Zaitsev | 10:41552d038a69 | 174 | * @param[in] fallingEdge |
| Zaitsev | 10:41552d038a69 | 175 | * Set to true if interrupts shall be enabled on falling edge, otherwise false. |
| Zaitsev | 10:41552d038a69 | 176 | * |
| Zaitsev | 10:41552d038a69 | 177 | * @param[in] enable |
| Zaitsev | 10:41552d038a69 | 178 | * Set to true if interrupt shall be enabled after configuration completed, |
| Zaitsev | 10:41552d038a69 | 179 | * false to leave disabled. See GPIO_IntDisable() and GPIO_IntEnable(). |
| Zaitsev | 10:41552d038a69 | 180 | ******************************************************************************/ |
| Zaitsev | 10:41552d038a69 | 181 | void GPIO_ExtIntConfig(GPIO_Port_TypeDef port, |
| Zaitsev | 10:41552d038a69 | 182 | unsigned int pin, |
| Zaitsev | 10:41552d038a69 | 183 | unsigned int intNo, |
| Zaitsev | 10:41552d038a69 | 184 | bool risingEdge, |
| Zaitsev | 10:41552d038a69 | 185 | bool fallingEdge, |
| Zaitsev | 10:41552d038a69 | 186 | bool enable) |
| Zaitsev | 10:41552d038a69 | 187 | { |
| Zaitsev | 10:41552d038a69 | 188 | uint32_t tmp; |
| Zaitsev | 10:41552d038a69 | 189 | #if !defined(_GPIO_EXTIPINSELL_MASK) |
| Zaitsev | 10:41552d038a69 | 190 | (void)pin; |
| Zaitsev | 10:41552d038a69 | 191 | #endif |
| Zaitsev | 10:41552d038a69 | 192 | |
| Zaitsev | 10:41552d038a69 | 193 | EFM_ASSERT(GPIO_PORT_PIN_VALID(port, pin)); |
| Zaitsev | 10:41552d038a69 | 194 | #if defined(_GPIO_EXTIPINSELL_MASK) |
| Zaitsev | 10:41552d038a69 | 195 | EFM_ASSERT(GPIO_INTNO_PIN_VALID(intNo, pin)); |
| Zaitsev | 10:41552d038a69 | 196 | #endif |
| Zaitsev | 10:41552d038a69 | 197 | |
| Zaitsev | 10:41552d038a69 | 198 | /* There are two registers controlling the interrupt configuration: |
| Zaitsev | 10:41552d038a69 | 199 | * The EXTIPSELL register controls pins 0-7 and EXTIPSELH controls |
| Zaitsev | 10:41552d038a69 | 200 | * pins 8-15. */ |
| Zaitsev | 10:41552d038a69 | 201 | if (intNo < 8) |
| Zaitsev | 10:41552d038a69 | 202 | { |
| Zaitsev | 10:41552d038a69 | 203 | BUS_RegMaskedWrite(&GPIO->EXTIPSELL, |
| Zaitsev | 10:41552d038a69 | 204 | _GPIO_EXTIPSELL_EXTIPSEL0_MASK |
| Zaitsev | 10:41552d038a69 | 205 | << (_GPIO_EXTIPSELL_EXTIPSEL1_SHIFT * intNo), |
| Zaitsev | 10:41552d038a69 | 206 | port << (_GPIO_EXTIPSELL_EXTIPSEL1_SHIFT * intNo)); |
| Zaitsev | 10:41552d038a69 | 207 | } |
| Zaitsev | 10:41552d038a69 | 208 | else |
| Zaitsev | 10:41552d038a69 | 209 | { |
| Zaitsev | 10:41552d038a69 | 210 | tmp = intNo - 8; |
| Zaitsev | 10:41552d038a69 | 211 | BUS_RegMaskedWrite(&GPIO->EXTIPSELH, |
| Zaitsev | 10:41552d038a69 | 212 | _GPIO_EXTIPSELH_EXTIPSEL8_MASK |
| Zaitsev | 10:41552d038a69 | 213 | << (_GPIO_EXTIPSELH_EXTIPSEL9_SHIFT * tmp), |
| Zaitsev | 10:41552d038a69 | 214 | port << (_GPIO_EXTIPSELH_EXTIPSEL9_SHIFT * tmp)); |
| Zaitsev | 10:41552d038a69 | 215 | } |
| Zaitsev | 10:41552d038a69 | 216 | |
| Zaitsev | 10:41552d038a69 | 217 | #if defined(_GPIO_EXTIPINSELL_MASK) |
| Zaitsev | 10:41552d038a69 | 218 | /* There are two registers controlling the interrupt/pin number mapping: |
| Zaitsev | 10:41552d038a69 | 219 | * The EXTIPINSELL register controls interrupt 0-7 and EXTIPINSELH controls |
| Zaitsev | 10:41552d038a69 | 220 | * interrupt 8-15. */ |
| Zaitsev | 10:41552d038a69 | 221 | if (intNo < 8) |
| Zaitsev | 10:41552d038a69 | 222 | { |
| Zaitsev | 10:41552d038a69 | 223 | BUS_RegMaskedWrite(&GPIO->EXTIPINSELL, |
| Zaitsev | 10:41552d038a69 | 224 | _GPIO_EXTIPINSELL_EXTIPINSEL0_MASK |
| Zaitsev | 10:41552d038a69 | 225 | << (_GPIO_EXTIPINSELL_EXTIPINSEL1_SHIFT * intNo), |
| Zaitsev | 10:41552d038a69 | 226 | ((pin % 4) & _GPIO_EXTIPINSELL_EXTIPINSEL0_MASK) |
| Zaitsev | 10:41552d038a69 | 227 | << (_GPIO_EXTIPINSELL_EXTIPINSEL1_SHIFT * intNo)); |
| Zaitsev | 10:41552d038a69 | 228 | } |
| Zaitsev | 10:41552d038a69 | 229 | else |
| Zaitsev | 10:41552d038a69 | 230 | { |
| Zaitsev | 10:41552d038a69 | 231 | BUS_RegMaskedWrite(&GPIO->EXTIPINSELH, |
| Zaitsev | 10:41552d038a69 | 232 | _GPIO_EXTIPINSELH_EXTIPINSEL8_MASK |
| Zaitsev | 10:41552d038a69 | 233 | << (_GPIO_EXTIPINSELH_EXTIPINSEL9_SHIFT * tmp), |
| Zaitsev | 10:41552d038a69 | 234 | ((pin % 4) & _GPIO_EXTIPINSELH_EXTIPINSEL8_MASK) |
| Zaitsev | 10:41552d038a69 | 235 | << (_GPIO_EXTIPSELH_EXTIPSEL9_SHIFT * tmp)); |
| Zaitsev | 10:41552d038a69 | 236 | } |
| Zaitsev | 10:41552d038a69 | 237 | #endif |
| Zaitsev | 10:41552d038a69 | 238 | |
| Zaitsev | 10:41552d038a69 | 239 | /* Enable/disable rising edge */ |
| Zaitsev | 10:41552d038a69 | 240 | BUS_RegBitWrite(&(GPIO->EXTIRISE), intNo, risingEdge); |
| Zaitsev | 10:41552d038a69 | 241 | |
| Zaitsev | 10:41552d038a69 | 242 | /* Enable/disable falling edge */ |
| Zaitsev | 10:41552d038a69 | 243 | BUS_RegBitWrite(&(GPIO->EXTIFALL), intNo, fallingEdge); |
| Zaitsev | 10:41552d038a69 | 244 | |
| Zaitsev | 10:41552d038a69 | 245 | /* Clear any pending interrupt */ |
| Zaitsev | 10:41552d038a69 | 246 | GPIO->IFC = 1 << intNo; |
| Zaitsev | 10:41552d038a69 | 247 | |
| Zaitsev | 10:41552d038a69 | 248 | /* Finally enable/disable interrupt */ |
| Zaitsev | 10:41552d038a69 | 249 | BUS_RegBitWrite(&(GPIO->IEN), intNo, enable); |
| Zaitsev | 10:41552d038a69 | 250 | } |
| Zaitsev | 10:41552d038a69 | 251 | |
| Zaitsev | 10:41552d038a69 | 252 | /***************************************************************************//** |
| Zaitsev | 10:41552d038a69 | 253 | * @brief |
| Zaitsev | 10:41552d038a69 | 254 | * Set the mode for a GPIO pin. |
| Zaitsev | 10:41552d038a69 | 255 | * |
| Zaitsev | 10:41552d038a69 | 256 | * @param[in] port |
| Zaitsev | 10:41552d038a69 | 257 | * The GPIO port to access. |
| Zaitsev | 10:41552d038a69 | 258 | * |
| Zaitsev | 10:41552d038a69 | 259 | * @param[in] pin |
| Zaitsev | 10:41552d038a69 | 260 | * The pin number in the port. |
| Zaitsev | 10:41552d038a69 | 261 | * |
| Zaitsev | 10:41552d038a69 | 262 | * @param[in] mode |
| Zaitsev | 10:41552d038a69 | 263 | * The desired pin mode. |
| Zaitsev | 10:41552d038a69 | 264 | * |
| Zaitsev | 10:41552d038a69 | 265 | * @param[in] out |
| Zaitsev | 10:41552d038a69 | 266 | * Value to set for pin in DOUT register. The DOUT setting is important for |
| Zaitsev | 10:41552d038a69 | 267 | * even some input mode configurations, determining pull-up/down direction. |
| Zaitsev | 10:41552d038a69 | 268 | ******************************************************************************/ |
| Zaitsev | 10:41552d038a69 | 269 | void GPIO_PinModeSet(GPIO_Port_TypeDef port, |
| Zaitsev | 10:41552d038a69 | 270 | unsigned int pin, |
| Zaitsev | 10:41552d038a69 | 271 | GPIO_Mode_TypeDef mode, |
| Zaitsev | 10:41552d038a69 | 272 | unsigned int out) |
| Zaitsev | 10:41552d038a69 | 273 | { |
| Zaitsev | 10:41552d038a69 | 274 | EFM_ASSERT(GPIO_PORT_PIN_VALID(port, pin)); |
| Zaitsev | 10:41552d038a69 | 275 | |
| Zaitsev | 10:41552d038a69 | 276 | /* If disabling pin, do not modify DOUT in order to reduce chance for */ |
| Zaitsev | 10:41552d038a69 | 277 | /* glitch/spike (may not be sufficient precaution in all use cases) */ |
| Zaitsev | 10:41552d038a69 | 278 | if (mode != gpioModeDisabled) |
| Zaitsev | 10:41552d038a69 | 279 | { |
| Zaitsev | 10:41552d038a69 | 280 | if (out) |
| Zaitsev | 10:41552d038a69 | 281 | { |
| Zaitsev | 10:41552d038a69 | 282 | GPIO_PinOutSet(port, pin); |
| Zaitsev | 10:41552d038a69 | 283 | } |
| Zaitsev | 10:41552d038a69 | 284 | else |
| Zaitsev | 10:41552d038a69 | 285 | { |
| Zaitsev | 10:41552d038a69 | 286 | GPIO_PinOutClear(port, pin); |
| Zaitsev | 10:41552d038a69 | 287 | } |
| Zaitsev | 10:41552d038a69 | 288 | } |
| Zaitsev | 10:41552d038a69 | 289 | |
| Zaitsev | 10:41552d038a69 | 290 | /* There are two registers controlling the pins for each port. The MODEL |
| Zaitsev | 10:41552d038a69 | 291 | * register controls pins 0-7 and MODEH controls pins 8-15. */ |
| Zaitsev | 10:41552d038a69 | 292 | if (pin < 8) |
| Zaitsev | 10:41552d038a69 | 293 | { |
| Zaitsev | 10:41552d038a69 | 294 | GPIO->P[port].MODEL = (GPIO->P[port].MODEL & ~(0xF << (pin * 4))) |
| Zaitsev | 10:41552d038a69 | 295 | | (mode << (pin * 4)); |
| Zaitsev | 10:41552d038a69 | 296 | } |
| Zaitsev | 10:41552d038a69 | 297 | else |
| Zaitsev | 10:41552d038a69 | 298 | { |
| Zaitsev | 10:41552d038a69 | 299 | GPIO->P[port].MODEH = (GPIO->P[port].MODEH & ~(0xF << ((pin - 8) * 4))) |
| Zaitsev | 10:41552d038a69 | 300 | | (mode << ((pin - 8) * 4)); |
| Zaitsev | 10:41552d038a69 | 301 | } |
| Zaitsev | 10:41552d038a69 | 302 | |
| Zaitsev | 10:41552d038a69 | 303 | if (mode == gpioModeDisabled) |
| Zaitsev | 10:41552d038a69 | 304 | { |
| Zaitsev | 10:41552d038a69 | 305 | if (out) |
| Zaitsev | 10:41552d038a69 | 306 | { |
| Zaitsev | 10:41552d038a69 | 307 | GPIO_PinOutSet(port, pin); |
| Zaitsev | 10:41552d038a69 | 308 | } |
| Zaitsev | 10:41552d038a69 | 309 | else |
| Zaitsev | 10:41552d038a69 | 310 | { |
| Zaitsev | 10:41552d038a69 | 311 | GPIO_PinOutClear(port, pin); |
| Zaitsev | 10:41552d038a69 | 312 | } |
| Zaitsev | 10:41552d038a69 | 313 | } |
| Zaitsev | 10:41552d038a69 | 314 | } |
| Zaitsev | 10:41552d038a69 | 315 | |
| Zaitsev | 10:41552d038a69 | 316 | /***************************************************************************//** |
| Zaitsev | 10:41552d038a69 | 317 | * @brief |
| Zaitsev | 10:41552d038a69 | 318 | * Get the mode for a GPIO pin. |
| Zaitsev | 10:41552d038a69 | 319 | * |
| Zaitsev | 10:41552d038a69 | 320 | * @param[in] port |
| Zaitsev | 10:41552d038a69 | 321 | * The GPIO port to access. |
| Zaitsev | 10:41552d038a69 | 322 | * |
| Zaitsev | 10:41552d038a69 | 323 | * @param[in] pin |
| Zaitsev | 10:41552d038a69 | 324 | * The pin number in the port. |
| Zaitsev | 10:41552d038a69 | 325 | * |
| Zaitsev | 10:41552d038a69 | 326 | * @return |
| Zaitsev | 10:41552d038a69 | 327 | * The pin mode. |
| Zaitsev | 10:41552d038a69 | 328 | ******************************************************************************/ |
| Zaitsev | 10:41552d038a69 | 329 | GPIO_Mode_TypeDef GPIO_PinModeGet(GPIO_Port_TypeDef port, |
| Zaitsev | 10:41552d038a69 | 330 | unsigned int pin) |
| Zaitsev | 10:41552d038a69 | 331 | { |
| Zaitsev | 10:41552d038a69 | 332 | EFM_ASSERT(GPIO_PORT_PIN_VALID(port, pin)); |
| Zaitsev | 10:41552d038a69 | 333 | |
| Zaitsev | 10:41552d038a69 | 334 | if (pin < 8) |
| Zaitsev | 10:41552d038a69 | 335 | { |
| Zaitsev | 10:41552d038a69 | 336 | return (GPIO_Mode_TypeDef) ((GPIO->P[port].MODEL >> (pin * 4)) & 0xF); |
| Zaitsev | 10:41552d038a69 | 337 | } |
| Zaitsev | 10:41552d038a69 | 338 | else |
| Zaitsev | 10:41552d038a69 | 339 | { |
| Zaitsev | 10:41552d038a69 | 340 | return (GPIO_Mode_TypeDef) ((GPIO->P[port].MODEH >> ((pin - 8) * 4)) & 0xF); |
| Zaitsev | 10:41552d038a69 | 341 | } |
| Zaitsev | 10:41552d038a69 | 342 | } |
| Zaitsev | 10:41552d038a69 | 343 | |
| Zaitsev | 10:41552d038a69 | 344 | #if defined( _GPIO_EM4WUEN_MASK ) |
| Zaitsev | 10:41552d038a69 | 345 | /**************************************************************************//** |
| Zaitsev | 10:41552d038a69 | 346 | * @brief |
| Zaitsev | 10:41552d038a69 | 347 | * Enable GPIO pin wake-up from EM4. When the function exits, |
| Zaitsev | 10:41552d038a69 | 348 | * EM4 mode can be safely entered. |
| Zaitsev | 10:41552d038a69 | 349 | * |
| Zaitsev | 10:41552d038a69 | 350 | * @note |
| Zaitsev | 10:41552d038a69 | 351 | * It is assumed that the GPIO pin modes are set correctly. |
| Zaitsev | 10:41552d038a69 | 352 | * Valid modes are @ref gpioModeInput and @ref gpioModeInputPull. |
| Zaitsev | 10:41552d038a69 | 353 | * |
| Zaitsev | 10:41552d038a69 | 354 | * @param[in] pinmask |
| Zaitsev | 10:41552d038a69 | 355 | * Bitmask containing the bitwise logic OR of which GPIO pin(s) to enable. |
| Zaitsev | 10:41552d038a69 | 356 | * Refer to Reference Manuals for pinmask to GPIO port/pin mapping. |
| Zaitsev | 10:41552d038a69 | 357 | * @param[in] polaritymask |
| Zaitsev | 10:41552d038a69 | 358 | * Bitmask containing the bitwise logic OR of GPIO pin(s) wake-up polarity. |
| Zaitsev | 10:41552d038a69 | 359 | * Refer to Reference Manuals for pinmask to GPIO port/pin mapping. |
| Zaitsev | 10:41552d038a69 | 360 | *****************************************************************************/ |
| Zaitsev | 10:41552d038a69 | 361 | void GPIO_EM4EnablePinWakeup(uint32_t pinmask, uint32_t polaritymask) |
| Zaitsev | 10:41552d038a69 | 362 | { |
| Zaitsev | 10:41552d038a69 | 363 | EFM_ASSERT((pinmask & ~_GPIO_EM4WUEN_MASK) == 0); |
| Zaitsev | 10:41552d038a69 | 364 | |
| Zaitsev | 10:41552d038a69 | 365 | #if defined( _GPIO_EM4WUPOL_MASK ) |
| Zaitsev | 10:41552d038a69 | 366 | EFM_ASSERT((polaritymask & ~_GPIO_EM4WUPOL_MASK) == 0); |
| Zaitsev | 10:41552d038a69 | 367 | GPIO->EM4WUPOL &= ~pinmask; /* Set wakeup polarity */ |
| Zaitsev | 10:41552d038a69 | 368 | GPIO->EM4WUPOL |= pinmask & polaritymask; |
| Zaitsev | 10:41552d038a69 | 369 | #elif defined( _GPIO_EXTILEVEL_MASK ) |
| Zaitsev | 10:41552d038a69 | 370 | EFM_ASSERT((polaritymask & ~_GPIO_EXTILEVEL_MASK) == 0); |
| Zaitsev | 10:41552d038a69 | 371 | GPIO->EXTILEVEL &= ~pinmask; |
| Zaitsev | 10:41552d038a69 | 372 | GPIO->EXTILEVEL |= pinmask & polaritymask; |
| Zaitsev | 10:41552d038a69 | 373 | #endif |
| Zaitsev | 10:41552d038a69 | 374 | GPIO->EM4WUEN |= pinmask; /* Enable wakeup */ |
| Zaitsev | 10:41552d038a69 | 375 | |
| Zaitsev | 10:41552d038a69 | 376 | GPIO_EM4SetPinRetention(true); /* Enable pin retention */ |
| Zaitsev | 10:41552d038a69 | 377 | |
| Zaitsev | 10:41552d038a69 | 378 | #if defined( _GPIO_CMD_EM4WUCLR_MASK ) |
| Zaitsev | 10:41552d038a69 | 379 | GPIO->CMD = GPIO_CMD_EM4WUCLR; /* Clear wake-up logic */ |
| Zaitsev | 10:41552d038a69 | 380 | #elif defined( _GPIO_IFC_EM4WU_MASK ) |
| Zaitsev | 10:41552d038a69 | 381 | GPIO_IntClear(pinmask); |
| Zaitsev | 10:41552d038a69 | 382 | #endif |
| Zaitsev | 10:41552d038a69 | 383 | } |
| Zaitsev | 10:41552d038a69 | 384 | #endif |
| Zaitsev | 10:41552d038a69 | 385 | |
| Zaitsev | 10:41552d038a69 | 386 | /** @} (end addtogroup GPIO) */ |
| Zaitsev | 10:41552d038a69 | 387 | /** @} (end addtogroup emlib) */ |
| Zaitsev | 10:41552d038a69 | 388 | |
| Zaitsev | 10:41552d038a69 | 389 | #endif /* defined(GPIO_COUNT) && (GPIO_COUNT > 0) */ |
