test
targets/TARGET_STM/gpio_api.c@1:8a094db1347f, 2020-11-09 (annotated)
- Committer:
- elijahsj
- Date:
- Mon Nov 09 00:02:47 2020 -0500
- Revision:
- 1:8a094db1347f
test
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
elijahsj | 1:8a094db1347f | 1 | /* mbed Microcontroller Library |
elijahsj | 1:8a094db1347f | 2 | ******************************************************************************* |
elijahsj | 1:8a094db1347f | 3 | * Copyright (c) 2015, STMicroelectronics |
elijahsj | 1:8a094db1347f | 4 | * All rights reserved. |
elijahsj | 1:8a094db1347f | 5 | * |
elijahsj | 1:8a094db1347f | 6 | * Redistribution and use in source and binary forms, with or without |
elijahsj | 1:8a094db1347f | 7 | * modification, are permitted provided that the following conditions are met: |
elijahsj | 1:8a094db1347f | 8 | * |
elijahsj | 1:8a094db1347f | 9 | * 1. Redistributions of source code must retain the above copyright notice, |
elijahsj | 1:8a094db1347f | 10 | * this list of conditions and the following disclaimer. |
elijahsj | 1:8a094db1347f | 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
elijahsj | 1:8a094db1347f | 12 | * this list of conditions and the following disclaimer in the documentation |
elijahsj | 1:8a094db1347f | 13 | * and/or other materials provided with the distribution. |
elijahsj | 1:8a094db1347f | 14 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
elijahsj | 1:8a094db1347f | 15 | * may be used to endorse or promote products derived from this software |
elijahsj | 1:8a094db1347f | 16 | * without specific prior written permission. |
elijahsj | 1:8a094db1347f | 17 | * |
elijahsj | 1:8a094db1347f | 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
elijahsj | 1:8a094db1347f | 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
elijahsj | 1:8a094db1347f | 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
elijahsj | 1:8a094db1347f | 21 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
elijahsj | 1:8a094db1347f | 22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
elijahsj | 1:8a094db1347f | 23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
elijahsj | 1:8a094db1347f | 24 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
elijahsj | 1:8a094db1347f | 25 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
elijahsj | 1:8a094db1347f | 26 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
elijahsj | 1:8a094db1347f | 27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
elijahsj | 1:8a094db1347f | 28 | ******************************************************************************* |
elijahsj | 1:8a094db1347f | 29 | */ |
elijahsj | 1:8a094db1347f | 30 | #include "mbed_assert.h" |
elijahsj | 1:8a094db1347f | 31 | #include "gpio_api.h" |
elijahsj | 1:8a094db1347f | 32 | #include "pinmap.h" |
elijahsj | 1:8a094db1347f | 33 | #include "mbed_error.h" |
elijahsj | 1:8a094db1347f | 34 | #include "pin_device.h" |
elijahsj | 1:8a094db1347f | 35 | |
elijahsj | 1:8a094db1347f | 36 | extern const uint32_t ll_pin_defines[16]; |
elijahsj | 1:8a094db1347f | 37 | |
elijahsj | 1:8a094db1347f | 38 | // Enable GPIO clock and return GPIO base address |
elijahsj | 1:8a094db1347f | 39 | GPIO_TypeDef *Set_GPIO_Clock(uint32_t port_idx) { |
elijahsj | 1:8a094db1347f | 40 | uint32_t gpio_add = 0; |
elijahsj | 1:8a094db1347f | 41 | switch (port_idx) { |
elijahsj | 1:8a094db1347f | 42 | case PortA: |
elijahsj | 1:8a094db1347f | 43 | gpio_add = GPIOA_BASE; |
elijahsj | 1:8a094db1347f | 44 | __HAL_RCC_GPIOA_CLK_ENABLE(); |
elijahsj | 1:8a094db1347f | 45 | break; |
elijahsj | 1:8a094db1347f | 46 | case PortB: |
elijahsj | 1:8a094db1347f | 47 | gpio_add = GPIOB_BASE; |
elijahsj | 1:8a094db1347f | 48 | __HAL_RCC_GPIOB_CLK_ENABLE(); |
elijahsj | 1:8a094db1347f | 49 | break; |
elijahsj | 1:8a094db1347f | 50 | #if defined(GPIOC_BASE) |
elijahsj | 1:8a094db1347f | 51 | case PortC: |
elijahsj | 1:8a094db1347f | 52 | gpio_add = GPIOC_BASE; |
elijahsj | 1:8a094db1347f | 53 | __HAL_RCC_GPIOC_CLK_ENABLE(); |
elijahsj | 1:8a094db1347f | 54 | break; |
elijahsj | 1:8a094db1347f | 55 | #endif |
elijahsj | 1:8a094db1347f | 56 | #if defined GPIOD_BASE |
elijahsj | 1:8a094db1347f | 57 | case PortD: |
elijahsj | 1:8a094db1347f | 58 | gpio_add = GPIOD_BASE; |
elijahsj | 1:8a094db1347f | 59 | __HAL_RCC_GPIOD_CLK_ENABLE(); |
elijahsj | 1:8a094db1347f | 60 | break; |
elijahsj | 1:8a094db1347f | 61 | #endif |
elijahsj | 1:8a094db1347f | 62 | #if defined GPIOE_BASE |
elijahsj | 1:8a094db1347f | 63 | case PortE: |
elijahsj | 1:8a094db1347f | 64 | gpio_add = GPIOE_BASE; |
elijahsj | 1:8a094db1347f | 65 | __HAL_RCC_GPIOE_CLK_ENABLE(); |
elijahsj | 1:8a094db1347f | 66 | break; |
elijahsj | 1:8a094db1347f | 67 | #endif |
elijahsj | 1:8a094db1347f | 68 | #if defined GPIOF_BASE |
elijahsj | 1:8a094db1347f | 69 | case PortF: |
elijahsj | 1:8a094db1347f | 70 | gpio_add = GPIOF_BASE; |
elijahsj | 1:8a094db1347f | 71 | __HAL_RCC_GPIOF_CLK_ENABLE(); |
elijahsj | 1:8a094db1347f | 72 | break; |
elijahsj | 1:8a094db1347f | 73 | #endif |
elijahsj | 1:8a094db1347f | 74 | #if defined GPIOG_BASE |
elijahsj | 1:8a094db1347f | 75 | case PortG: |
elijahsj | 1:8a094db1347f | 76 | #if defined TARGET_STM32L4 |
elijahsj | 1:8a094db1347f | 77 | __HAL_RCC_PWR_CLK_ENABLE(); |
elijahsj | 1:8a094db1347f | 78 | HAL_PWREx_EnableVddIO2(); |
elijahsj | 1:8a094db1347f | 79 | #endif |
elijahsj | 1:8a094db1347f | 80 | gpio_add = GPIOG_BASE; |
elijahsj | 1:8a094db1347f | 81 | __HAL_RCC_GPIOG_CLK_ENABLE(); |
elijahsj | 1:8a094db1347f | 82 | break; |
elijahsj | 1:8a094db1347f | 83 | #endif |
elijahsj | 1:8a094db1347f | 84 | #if defined GPIOH_BASE |
elijahsj | 1:8a094db1347f | 85 | case PortH: |
elijahsj | 1:8a094db1347f | 86 | gpio_add = GPIOH_BASE; |
elijahsj | 1:8a094db1347f | 87 | __HAL_RCC_GPIOH_CLK_ENABLE(); |
elijahsj | 1:8a094db1347f | 88 | break; |
elijahsj | 1:8a094db1347f | 89 | #endif |
elijahsj | 1:8a094db1347f | 90 | #if defined GPIOI_BASE |
elijahsj | 1:8a094db1347f | 91 | case PortI: |
elijahsj | 1:8a094db1347f | 92 | gpio_add = GPIOI_BASE; |
elijahsj | 1:8a094db1347f | 93 | __HAL_RCC_GPIOI_CLK_ENABLE(); |
elijahsj | 1:8a094db1347f | 94 | break; |
elijahsj | 1:8a094db1347f | 95 | #endif |
elijahsj | 1:8a094db1347f | 96 | #if defined GPIOJ_BASE |
elijahsj | 1:8a094db1347f | 97 | case PortJ: |
elijahsj | 1:8a094db1347f | 98 | gpio_add = GPIOJ_BASE; |
elijahsj | 1:8a094db1347f | 99 | __HAL_RCC_GPIOJ_CLK_ENABLE(); |
elijahsj | 1:8a094db1347f | 100 | break; |
elijahsj | 1:8a094db1347f | 101 | #endif |
elijahsj | 1:8a094db1347f | 102 | #if defined GPIOK_BASE |
elijahsj | 1:8a094db1347f | 103 | case PortK: |
elijahsj | 1:8a094db1347f | 104 | gpio_add = GPIOK_BASE; |
elijahsj | 1:8a094db1347f | 105 | __HAL_RCC_GPIOK_CLK_ENABLE(); |
elijahsj | 1:8a094db1347f | 106 | break; |
elijahsj | 1:8a094db1347f | 107 | #endif |
elijahsj | 1:8a094db1347f | 108 | default: |
elijahsj | 1:8a094db1347f | 109 | error("Pinmap error: wrong port number."); |
elijahsj | 1:8a094db1347f | 110 | break; |
elijahsj | 1:8a094db1347f | 111 | } |
elijahsj | 1:8a094db1347f | 112 | return (GPIO_TypeDef *) gpio_add; |
elijahsj | 1:8a094db1347f | 113 | } |
elijahsj | 1:8a094db1347f | 114 | |
elijahsj | 1:8a094db1347f | 115 | uint32_t gpio_set(PinName pin) { |
elijahsj | 1:8a094db1347f | 116 | MBED_ASSERT(pin != (PinName)NC); |
elijahsj | 1:8a094db1347f | 117 | |
elijahsj | 1:8a094db1347f | 118 | pin_function(pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); |
elijahsj | 1:8a094db1347f | 119 | |
elijahsj | 1:8a094db1347f | 120 | return (uint32_t)(1 << ((uint32_t)pin & 0xF)); // Return the pin mask |
elijahsj | 1:8a094db1347f | 121 | } |
elijahsj | 1:8a094db1347f | 122 | |
elijahsj | 1:8a094db1347f | 123 | |
elijahsj | 1:8a094db1347f | 124 | void gpio_init(gpio_t *obj, PinName pin) { |
elijahsj | 1:8a094db1347f | 125 | obj->pin = pin; |
elijahsj | 1:8a094db1347f | 126 | if (pin == (PinName)NC) { |
elijahsj | 1:8a094db1347f | 127 | return; |
elijahsj | 1:8a094db1347f | 128 | } |
elijahsj | 1:8a094db1347f | 129 | |
elijahsj | 1:8a094db1347f | 130 | uint32_t port_index = STM_PORT(pin); |
elijahsj | 1:8a094db1347f | 131 | |
elijahsj | 1:8a094db1347f | 132 | // Enable GPIO clock |
elijahsj | 1:8a094db1347f | 133 | GPIO_TypeDef *gpio = Set_GPIO_Clock(port_index); |
elijahsj | 1:8a094db1347f | 134 | |
elijahsj | 1:8a094db1347f | 135 | // Fill GPIO object structure for future use |
elijahsj | 1:8a094db1347f | 136 | obj->mask = gpio_set(pin); |
elijahsj | 1:8a094db1347f | 137 | obj->gpio = gpio; |
elijahsj | 1:8a094db1347f | 138 | obj->ll_pin = ll_pin_defines[STM_PIN(obj->pin)]; |
elijahsj | 1:8a094db1347f | 139 | obj->reg_in = &gpio->IDR; |
elijahsj | 1:8a094db1347f | 140 | obj->reg_set = &gpio->BSRR; |
elijahsj | 1:8a094db1347f | 141 | #ifdef GPIO_IP_WITHOUT_BRR |
elijahsj | 1:8a094db1347f | 142 | obj->reg_clr = &gpio->BSRR; |
elijahsj | 1:8a094db1347f | 143 | #else |
elijahsj | 1:8a094db1347f | 144 | obj->reg_clr = &gpio->BRR; |
elijahsj | 1:8a094db1347f | 145 | #endif |
elijahsj | 1:8a094db1347f | 146 | } |
elijahsj | 1:8a094db1347f | 147 | |
elijahsj | 1:8a094db1347f | 148 | void gpio_mode(gpio_t *obj, PinMode mode) { |
elijahsj | 1:8a094db1347f | 149 | pin_mode(obj->pin, mode); |
elijahsj | 1:8a094db1347f | 150 | } |
elijahsj | 1:8a094db1347f | 151 | |
elijahsj | 1:8a094db1347f | 152 | inline void gpio_dir(gpio_t *obj, PinDirection direction) { |
elijahsj | 1:8a094db1347f | 153 | if (direction == PIN_INPUT) { |
elijahsj | 1:8a094db1347f | 154 | LL_GPIO_SetPinMode(obj->gpio, obj->ll_pin, LL_GPIO_MODE_INPUT); |
elijahsj | 1:8a094db1347f | 155 | } else { |
elijahsj | 1:8a094db1347f | 156 | LL_GPIO_SetPinMode(obj->gpio, obj->ll_pin, LL_GPIO_MODE_OUTPUT); |
elijahsj | 1:8a094db1347f | 157 | } |
elijahsj | 1:8a094db1347f | 158 | } |
elijahsj | 1:8a094db1347f | 159 |