mbed library sources. Supersedes mbed-src. Edited target satm32f446 for user USART3 pins
Fork of mbed-dev by
targets/TARGET_Maxim/TARGET_MAX32620C/gpio_api.c@186:707f6e361f3e, 2018-06-22 (annotated)
- 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?
User | Revision | Line number | New contents of line |
---|---|---|---|
Anna Bridge |
186:707f6e361f3e | 1 | /******************************************************************************* |
Anna Bridge |
186:707f6e361f3e | 2 | * Copyright (C) 2018 Maxim Integrated Products, Inc., All Rights Reserved. |
Anna Bridge |
186:707f6e361f3e | 3 | * |
Anna Bridge |
186:707f6e361f3e | 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
Anna Bridge |
186:707f6e361f3e | 5 | * copy of this software and associated documentation files (the "Software"), |
Anna Bridge |
186:707f6e361f3e | 6 | * to deal in the Software without restriction, including without limitation |
Anna Bridge |
186:707f6e361f3e | 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
Anna Bridge |
186:707f6e361f3e | 8 | * and/or sell copies of the Software, and to permit persons to whom the |
Anna Bridge |
186:707f6e361f3e | 9 | * Software is furnished to do so, subject to the following conditions: |
Anna Bridge |
186:707f6e361f3e | 10 | * |
Anna Bridge |
186:707f6e361f3e | 11 | * The above copyright notice and this permission notice shall be included |
Anna Bridge |
186:707f6e361f3e | 12 | * in all copies or substantial portions of the Software. |
Anna Bridge |
186:707f6e361f3e | 13 | * |
Anna Bridge |
186:707f6e361f3e | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
Anna Bridge |
186:707f6e361f3e | 15 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
Anna Bridge |
186:707f6e361f3e | 16 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
Anna Bridge |
186:707f6e361f3e | 17 | * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES |
Anna Bridge |
186:707f6e361f3e | 18 | * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
Anna Bridge |
186:707f6e361f3e | 19 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
Anna Bridge |
186:707f6e361f3e | 20 | * OTHER DEALINGS IN THE SOFTWARE. |
Anna Bridge |
186:707f6e361f3e | 21 | * |
Anna Bridge |
186:707f6e361f3e | 22 | * Except as contained in this notice, the name of Maxim Integrated |
Anna Bridge |
186:707f6e361f3e | 23 | * Products, Inc. shall not be used except as stated in the Maxim Integrated |
Anna Bridge |
186:707f6e361f3e | 24 | * Products, Inc. Branding Policy. |
Anna Bridge |
186:707f6e361f3e | 25 | * |
Anna Bridge |
186:707f6e361f3e | 26 | * The mere transfer of this software does not imply any licenses |
Anna Bridge |
186:707f6e361f3e | 27 | * of trade secrets, proprietary technology, copyrights, patents, |
Anna Bridge |
186:707f6e361f3e | 28 | * trademarks, maskwork rights, or any other form of intellectual |
Anna Bridge |
186:707f6e361f3e | 29 | * property whatsoever. Maxim Integrated Products, Inc. retains all |
Anna Bridge |
186:707f6e361f3e | 30 | * ownership rights. |
Anna Bridge |
186:707f6e361f3e | 31 | ******************************************************************************* |
Anna Bridge |
186:707f6e361f3e | 32 | */ |
Anna Bridge |
186:707f6e361f3e | 33 | |
Anna Bridge |
186:707f6e361f3e | 34 | #include "mbed_assert.h" |
Anna Bridge |
186:707f6e361f3e | 35 | #include "gpio_api.h" |
Anna Bridge |
186:707f6e361f3e | 36 | #include "pinmap.h" |
Anna Bridge |
186:707f6e361f3e | 37 | #include "gpio_regs.h" |
Anna Bridge |
186:707f6e361f3e | 38 | #include "clkman_regs.h" |
Anna Bridge |
186:707f6e361f3e | 39 | |
Anna Bridge |
186:707f6e361f3e | 40 | uint32_t gpio_set(PinName name) |
Anna Bridge |
186:707f6e361f3e | 41 | { |
Anna Bridge |
186:707f6e361f3e | 42 | MBED_ASSERT(name != (PinName)NC); |
Anna Bridge |
186:707f6e361f3e | 43 | pin_function(name, 0); |
Anna Bridge |
186:707f6e361f3e | 44 | return 1 << PINNAME_TO_PIN(name); |
Anna Bridge |
186:707f6e361f3e | 45 | } |
Anna Bridge |
186:707f6e361f3e | 46 | |
Anna Bridge |
186:707f6e361f3e | 47 | void gpio_init(gpio_t *obj, PinName name) |
Anna Bridge |
186:707f6e361f3e | 48 | { |
Anna Bridge |
186:707f6e361f3e | 49 | obj->name = name; |
Anna Bridge |
186:707f6e361f3e | 50 | if (name == (PinName)NC) { |
Anna Bridge |
186:707f6e361f3e | 51 | return; |
Anna Bridge |
186:707f6e361f3e | 52 | } |
Anna Bridge |
186:707f6e361f3e | 53 | |
Anna Bridge |
186:707f6e361f3e | 54 | unsigned int port = PINNAME_TO_PORT(name); |
Anna Bridge |
186:707f6e361f3e | 55 | unsigned int pin = PINNAME_TO_PIN(name); |
Anna Bridge |
186:707f6e361f3e | 56 | |
Anna Bridge |
186:707f6e361f3e | 57 | obj->reg_out = (uint32_t*)BITBAND(&MXC_GPIO->out_val[port], pin); |
Anna Bridge |
186:707f6e361f3e | 58 | obj->reg_in = (uint32_t*)BITBAND(&MXC_GPIO->in_val[port], pin); |
Anna Bridge |
186:707f6e361f3e | 59 | obj->mode = PullDefault; |
Anna Bridge |
186:707f6e361f3e | 60 | |
Anna Bridge |
186:707f6e361f3e | 61 | /* Ensure that the GPIO clock is enabled */ |
Anna Bridge |
186:707f6e361f3e | 62 | MXC_CLKMAN->clk_gate_ctrl1 |= MXC_F_CLKMAN_CLK_GATE_CTRL1_GPIO_CLK_GATER; |
Anna Bridge |
186:707f6e361f3e | 63 | |
Anna Bridge |
186:707f6e361f3e | 64 | /* Ensure that the GPIO clock is enabled */ |
Anna Bridge |
186:707f6e361f3e | 65 | MXC_CLKMAN->sys_clk_ctrl_6_gpio = MXC_S_CLKMAN_CLK_SCALE_DIV_1; |
Anna Bridge |
186:707f6e361f3e | 66 | } |
Anna Bridge |
186:707f6e361f3e | 67 | |
Anna Bridge |
186:707f6e361f3e | 68 | void gpio_mode(gpio_t *obj, PinMode mode) |
Anna Bridge |
186:707f6e361f3e | 69 | { |
Anna Bridge |
186:707f6e361f3e | 70 | #ifdef OPEN_DRAIN_LEDS |
Anna Bridge |
186:707f6e361f3e | 71 | if ((obj->name == LED1) || (obj->name == LED2) || |
Anna Bridge |
186:707f6e361f3e | 72 | (obj->name == LED3) || (obj->name == LED4)) { |
Anna Bridge |
186:707f6e361f3e | 73 | mode = OpenDrain; |
Anna Bridge |
186:707f6e361f3e | 74 | #ifdef TARGET_MAX32620FTHR |
Anna Bridge |
186:707f6e361f3e | 75 | unsigned int port = PINNAME_TO_PORT(obj->name); |
Anna Bridge |
186:707f6e361f3e | 76 | unsigned int pin = PINNAME_TO_PIN(obj->name); |
Anna Bridge |
186:707f6e361f3e | 77 | if (port < 4) { |
Anna Bridge |
186:707f6e361f3e | 78 | MXC_IOMAN->use_vddioh_0 |= (1 << ((port * 8) + pin)); |
Anna Bridge |
186:707f6e361f3e | 79 | } else if (port < 7) { |
Anna Bridge |
186:707f6e361f3e | 80 | MXC_IOMAN->use_vddioh_1 |= (1 << (((port - 4) * 8) + pin)); |
Anna Bridge |
186:707f6e361f3e | 81 | } |
Anna Bridge |
186:707f6e361f3e | 82 | #endif |
Anna Bridge |
186:707f6e361f3e | 83 | } |
Anna Bridge |
186:707f6e361f3e | 84 | #endif |
Anna Bridge |
186:707f6e361f3e | 85 | |
Anna Bridge |
186:707f6e361f3e | 86 | obj->mode = mode; |
Anna Bridge |
186:707f6e361f3e | 87 | pin_mode(obj->name, mode); |
Anna Bridge |
186:707f6e361f3e | 88 | } |
Anna Bridge |
186:707f6e361f3e | 89 | |
Anna Bridge |
186:707f6e361f3e | 90 | void pin_dir_mode(PinName name, PinDirection direction, PinMode mode) |
Anna Bridge |
186:707f6e361f3e | 91 | { |
Anna Bridge |
186:707f6e361f3e | 92 | MBED_ASSERT(name != (PinName)NC); |
Anna Bridge |
186:707f6e361f3e | 93 | |
Anna Bridge |
186:707f6e361f3e | 94 | unsigned int port = PINNAME_TO_PORT(name); |
Anna Bridge |
186:707f6e361f3e | 95 | unsigned int pin = PINNAME_TO_PIN(name); |
Anna Bridge |
186:707f6e361f3e | 96 | |
Anna Bridge |
186:707f6e361f3e | 97 | /* Set function; Firmware Control (GPIO mode) */ |
Anna Bridge |
186:707f6e361f3e | 98 | MXC_GPIO->func_sel[port] &= ~(0xF << (4 * pin)); |
Anna Bridge |
186:707f6e361f3e | 99 | |
Anna Bridge |
186:707f6e361f3e | 100 | /* Normal input is always enabled */ |
Anna Bridge |
186:707f6e361f3e | 101 | MXC_GPIO->in_mode[port] &= ~(0xF << (4 * pin)); |
Anna Bridge |
186:707f6e361f3e | 102 | |
Anna Bridge |
186:707f6e361f3e | 103 | uint32_t new_mode; |
Anna Bridge |
186:707f6e361f3e | 104 | if (direction == PIN_OUTPUT) { |
Anna Bridge |
186:707f6e361f3e | 105 | // PullUp = not valid, |
Anna Bridge |
186:707f6e361f3e | 106 | // PullDown = not valid, |
Anna Bridge |
186:707f6e361f3e | 107 | // OpenDrain = MXC_V_GPIO_OUT_MODE_OD, |
Anna Bridge |
186:707f6e361f3e | 108 | // PullNone = MXC_V_GPIO_OUT_MODE_NORMAL, |
Anna Bridge |
186:707f6e361f3e | 109 | if (mode == OpenDrain) { |
Anna Bridge |
186:707f6e361f3e | 110 | new_mode = MXC_V_GPIO_OUT_MODE_OPEN_DRAIN; |
Anna Bridge |
186:707f6e361f3e | 111 | } else { |
Anna Bridge |
186:707f6e361f3e | 112 | new_mode = MXC_V_GPIO_OUT_MODE_NORMAL; |
Anna Bridge |
186:707f6e361f3e | 113 | } |
Anna Bridge |
186:707f6e361f3e | 114 | } else { |
Anna Bridge |
186:707f6e361f3e | 115 | // PullUp = MXC_V_GPIO_OUT_MODE_HIGH_Z_WEAK_PULLUP |
Anna Bridge |
186:707f6e361f3e | 116 | // PullDown = MXC_V_GPIO_OUT_MODE_HIGH_Z_WEAK_PULLDOWN |
Anna Bridge |
186:707f6e361f3e | 117 | // OpenDrain = MXC_V_GPIO_OUT_MODE_OD |
Anna Bridge |
186:707f6e361f3e | 118 | // PullNone = MXC_V_GPIO_OUT_MODE_NORMAL_HIGH_Z |
Anna Bridge |
186:707f6e361f3e | 119 | if (mode == PullUp) { |
Anna Bridge |
186:707f6e361f3e | 120 | new_mode = MXC_V_GPIO_OUT_MODE_HIGH_Z_WEAK_PULLUP; |
Anna Bridge |
186:707f6e361f3e | 121 | MXC_GPIO->out_val[port] |= 1 << pin; |
Anna Bridge |
186:707f6e361f3e | 122 | } else if (mode == PullDown) { |
Anna Bridge |
186:707f6e361f3e | 123 | new_mode = MXC_V_GPIO_OUT_MODE_HIGH_Z_WEAK_PULLDOWN; |
Anna Bridge |
186:707f6e361f3e | 124 | MXC_GPIO->out_val[port] &= ~(1 << pin); |
Anna Bridge |
186:707f6e361f3e | 125 | } else if (mode == OpenDrain) { |
Anna Bridge |
186:707f6e361f3e | 126 | new_mode = MXC_V_GPIO_OUT_MODE_OPEN_DRAIN; |
Anna Bridge |
186:707f6e361f3e | 127 | MXC_GPIO->out_val[port] |= 1 << pin; |
Anna Bridge |
186:707f6e361f3e | 128 | } else { |
Anna Bridge |
186:707f6e361f3e | 129 | new_mode = MXC_V_GPIO_OUT_MODE_NORMAL_HIGH_Z; |
Anna Bridge |
186:707f6e361f3e | 130 | MXC_GPIO->out_val[port] &= ~(1 << pin); |
Anna Bridge |
186:707f6e361f3e | 131 | } |
Anna Bridge |
186:707f6e361f3e | 132 | } |
Anna Bridge |
186:707f6e361f3e | 133 | |
Anna Bridge |
186:707f6e361f3e | 134 | /* Set new mode */ |
Anna Bridge |
186:707f6e361f3e | 135 | uint32_t out_mode = MXC_GPIO->out_mode[port]; |
Anna Bridge |
186:707f6e361f3e | 136 | out_mode &= ~(0xF << (pin * 4)); |
Anna Bridge |
186:707f6e361f3e | 137 | out_mode |= (new_mode << (pin * 4)); |
Anna Bridge |
186:707f6e361f3e | 138 | MXC_GPIO->out_mode[port] = out_mode; |
Anna Bridge |
186:707f6e361f3e | 139 | } |
Anna Bridge |
186:707f6e361f3e | 140 | |
Anna Bridge |
186:707f6e361f3e | 141 | void gpio_dir(gpio_t *obj, PinDirection direction) |
Anna Bridge |
186:707f6e361f3e | 142 | { |
Anna Bridge |
186:707f6e361f3e | 143 | pin_dir_mode(obj->name, direction, obj->mode); |
Anna Bridge |
186:707f6e361f3e | 144 | } |