mbed-os

Fork of mbed-os by erkin yucel

Committer:
xuaner
Date:
Thu Jul 20 14:26:57 2017 +0000
Revision:
1:3deb71413561
Parent:
0:f269e3021894
mbed_os

Who changed what in which revision?

UserRevisionLine numberNew contents of line
elessair 0:f269e3021894 1 /*******************************************************************************
elessair 0:f269e3021894 2 * Copyright (C) 2015 Maxim Integrated Products, Inc., All Rights Reserved.
elessair 0:f269e3021894 3 *
elessair 0:f269e3021894 4 * Permission is hereby granted, free of charge, to any person obtaining a
elessair 0:f269e3021894 5 * copy of this software and associated documentation files (the "Software"),
elessair 0:f269e3021894 6 * to deal in the Software without restriction, including without limitation
elessair 0:f269e3021894 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
elessair 0:f269e3021894 8 * and/or sell copies of the Software, and to permit persons to whom the
elessair 0:f269e3021894 9 * Software is furnished to do so, subject to the following conditions:
elessair 0:f269e3021894 10 *
elessair 0:f269e3021894 11 * The above copyright notice and this permission notice shall be included
elessair 0:f269e3021894 12 * in all copies or substantial portions of the Software.
elessair 0:f269e3021894 13 *
elessair 0:f269e3021894 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
elessair 0:f269e3021894 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
elessair 0:f269e3021894 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
elessair 0:f269e3021894 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
elessair 0:f269e3021894 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
elessair 0:f269e3021894 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
elessair 0:f269e3021894 20 * OTHER DEALINGS IN THE SOFTWARE.
elessair 0:f269e3021894 21 *
elessair 0:f269e3021894 22 * Except as contained in this notice, the name of Maxim Integrated
elessair 0:f269e3021894 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
elessair 0:f269e3021894 24 * Products, Inc. Branding Policy.
elessair 0:f269e3021894 25 *
elessair 0:f269e3021894 26 * The mere transfer of this software does not imply any licenses
elessair 0:f269e3021894 27 * of trade secrets, proprietary technology, copyrights, patents,
elessair 0:f269e3021894 28 * trademarks, maskwork rights, or any other form of intellectual
elessair 0:f269e3021894 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
elessair 0:f269e3021894 30 * ownership rights.
elessair 0:f269e3021894 31 *******************************************************************************
elessair 0:f269e3021894 32 */
elessair 0:f269e3021894 33
elessair 0:f269e3021894 34 #include "mbed_assert.h"
elessair 0:f269e3021894 35 #include "pinmap.h"
elessair 0:f269e3021894 36 #include "objects.h"
elessair 0:f269e3021894 37 #include "gpio_regs.h"
elessair 0:f269e3021894 38 #include "ioman_regs.h"
elessair 0:f269e3021894 39
elessair 0:f269e3021894 40 void pin_function(PinName name, int function)
elessair 0:f269e3021894 41 {
elessair 0:f269e3021894 42 MBED_ASSERT(name != (PinName)NC);
elessair 0:f269e3021894 43
elessair 0:f269e3021894 44 if ((function >= 0) && (function <= 0xF)) {
elessair 0:f269e3021894 45 unsigned int port = PINNAME_TO_PORT(name);
elessair 0:f269e3021894 46 unsigned int pin = PINNAME_TO_PIN(name);
elessair 0:f269e3021894 47 uint32_t temp = MXC_GPIO->func_sel[port] & ~(0xF << (pin*4));
elessair 0:f269e3021894 48 MXC_GPIO->func_sel[port] = temp | ((uint32_t)function << (pin*4));
elessair 0:f269e3021894 49 } else {
elessair 0:f269e3021894 50 /* Assume this is a pointer to a pin function object */
elessair 0:f269e3021894 51 pin_function_t *obj = (pin_function_t*)function;
elessair 0:f269e3021894 52
elessair 0:f269e3021894 53 if ((*obj->reg_ack & obj->ack_mask) != obj->req_val) {
elessair 0:f269e3021894 54 /* Request pin mapping */
elessair 0:f269e3021894 55 *obj->reg_req |= obj->req_val;
elessair 0:f269e3021894 56
elessair 0:f269e3021894 57 /* Check for acknowledgment */
elessair 0:f269e3021894 58 MBED_ASSERT((*obj->reg_ack & obj->ack_mask) == obj->req_val);
elessair 0:f269e3021894 59 }
elessair 0:f269e3021894 60 }
elessair 0:f269e3021894 61 }
elessair 0:f269e3021894 62
elessair 0:f269e3021894 63 void pin_mode(PinName name, PinMode mode)
elessair 0:f269e3021894 64 {
elessair 0:f269e3021894 65 MBED_ASSERT(name != (PinName)NC);
elessair 0:f269e3021894 66 unsigned int port = PINNAME_TO_PORT(name);
elessair 0:f269e3021894 67 unsigned int pin = PINNAME_TO_PIN(name);
elessair 0:f269e3021894 68
elessair 0:f269e3021894 69 /* Must set mode while retaining direction */
elessair 0:f269e3021894 70
elessair 0:f269e3021894 71 /* Get the current direction */
elessair 0:f269e3021894 72 uint32_t out_mode = MXC_GPIO->out_mode[port];
elessair 0:f269e3021894 73 uint32_t curr_mode = (out_mode >> (pin*4)) & 0xF;
elessair 0:f269e3021894 74 PinDirection dir = PIN_OUTPUT;
elessair 0:f269e3021894 75 if ((curr_mode == MXC_V_GPIO_OUT_MODE_HIGH_Z_WEAK_PULLUP) || (curr_mode == MXC_V_GPIO_OUT_MODE_HIGH_Z)) {
elessair 0:f269e3021894 76 dir = PIN_INPUT;
elessair 0:f269e3021894 77 }
elessair 0:f269e3021894 78
elessair 0:f269e3021894 79 /* Set mode based on current direction */
elessair 0:f269e3021894 80 uint32_t new_mode;
elessair 0:f269e3021894 81 if (dir == PIN_OUTPUT) {
elessair 0:f269e3021894 82 // PullUp = not valid,
elessair 0:f269e3021894 83 // OpenDrain = MXC_V_GPIO_OUT_MODE_OD,
elessair 0:f269e3021894 84 // PullNone = MXC_V_GPIO_OUT_MODE_NORMAL,
elessair 0:f269e3021894 85 if (mode == OpenDrain) {
elessair 0:f269e3021894 86 new_mode = MXC_V_GPIO_OUT_MODE_OPEN_DRAIN;
elessair 0:f269e3021894 87 } else {
elessair 0:f269e3021894 88 new_mode = MXC_V_GPIO_OUT_MODE_NORMAL_DRIVE;
elessair 0:f269e3021894 89 }
elessair 0:f269e3021894 90 } else {
elessair 0:f269e3021894 91 // PullUp = MXC_V_GPIO_OUT_MODE_HIZPU,
elessair 0:f269e3021894 92 // OpenDrain = not valid,
elessair 0:f269e3021894 93 // PullNone = MXC_V_GPIO_OUT_MODE_HIZ,
elessair 0:f269e3021894 94 if (mode == PullUp) {
elessair 0:f269e3021894 95 new_mode = MXC_V_GPIO_OUT_MODE_HIGH_Z_WEAK_PULLUP;
elessair 0:f269e3021894 96 } else {
elessair 0:f269e3021894 97 new_mode = MXC_V_GPIO_OUT_MODE_HIGH_Z;
elessair 0:f269e3021894 98 }
elessair 0:f269e3021894 99 }
elessair 0:f269e3021894 100
elessair 0:f269e3021894 101 /* Set new mode */
elessair 0:f269e3021894 102 out_mode &= ~(0xF << (pin*4));
elessair 0:f269e3021894 103 out_mode |= (new_mode << (pin*4));
elessair 0:f269e3021894 104 MXC_GPIO->out_mode[port] = out_mode;
elessair 0:f269e3021894 105 }