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