USB Serial application

Fork of USBSerial_HelloWorld by Samuel Mokrani

Committer:
Zaitsev
Date:
Tue Jan 10 20:42:26 2017 +0000
Revision:
10:41552d038a69
USB Serial bi-directional bridge

Who changed what in which revision?

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