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