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 /* mbed Microcontroller Library
Zaitsev 10:41552d038a69 2 * Copyright (c) 2016 u-blox
Zaitsev 10:41552d038a69 3 *
Zaitsev 10:41552d038a69 4 * Licensed under the Apache License, Version 2.0 (the "License");
Zaitsev 10:41552d038a69 5 * you may not use this file except in compliance with the License.
Zaitsev 10:41552d038a69 6 * You may obtain a copy of the License at
Zaitsev 10:41552d038a69 7 *
Zaitsev 10:41552d038a69 8 * http://www.apache.org/licenses/LICENSE-2.0
Zaitsev 10:41552d038a69 9 *
Zaitsev 10:41552d038a69 10 * Unless required by applicable law or agreed to in writing, software
Zaitsev 10:41552d038a69 11 * distributed under the License is distributed on an "AS IS" BASIS,
Zaitsev 10:41552d038a69 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Zaitsev 10:41552d038a69 13 * See the License for the specific language governing permissions and
Zaitsev 10:41552d038a69 14 * limitations under the License.
Zaitsev 10:41552d038a69 15 */
Zaitsev 10:41552d038a69 16
Zaitsev 10:41552d038a69 17 /* As well as claiming and setting pins, the functions here also need
Zaitsev 10:41552d038a69 18 * to take into account the way the pins are powered. On the Boudica
Zaitsev 10:41552d038a69 19 * chip they are arranged in three banks, PIO 0:5, PIO 6:10 and
Zaitsev 10:41552d038a69 20 * PIO 11:19.
Zaitsev 10:41552d038a69 21 *
Zaitsev 10:41552d038a69 22 * The arrangement for which PIO bank is powered is determined by the module
Zaitsev 10:41552d038a69 23 * in which the HI2110 chip is mounted, hence the use of conditional
Zaitsev 10:41552d038a69 24 * compilation below.
Zaitsev 10:41552d038a69 25 */
Zaitsev 10:41552d038a69 26
Zaitsev 10:41552d038a69 27 #include "stdbool.h"
Zaitsev 10:41552d038a69 28 #include "mbed_assert.h"
Zaitsev 10:41552d038a69 29 #include "mbed_error.h"
Zaitsev 10:41552d038a69 30 #include "pinmap.h"
Zaitsev 10:41552d038a69 31
Zaitsev 10:41552d038a69 32 /* ----------------------------------------------------------------
Zaitsev 10:41552d038a69 33 * MACROS
Zaitsev 10:41552d038a69 34 * ----------------------------------------------------------------*/
Zaitsev 10:41552d038a69 35
Zaitsev 10:41552d038a69 36 #define HAL_PIO_MASK_FUNC (0xFF)
Zaitsev 10:41552d038a69 37 #define HAL_PIO_MODULO_4_MASK (0x3)
Zaitsev 10:41552d038a69 38
Zaitsev 10:41552d038a69 39 /* ----------------------------------------------------------------
Zaitsev 10:41552d038a69 40 * GLOBAL VARIABLES
Zaitsev 10:41552d038a69 41 * ----------------------------------------------------------------*/
Zaitsev 10:41552d038a69 42
Zaitsev 10:41552d038a69 43 /* ----------------------------------------------------------------
Zaitsev 10:41552d038a69 44 * FUNCTION PROTOTYPES
Zaitsev 10:41552d038a69 45 * ----------------------------------------------------------------*/
Zaitsev 10:41552d038a69 46
Zaitsev 10:41552d038a69 47 static inline uint32_t clr_mask (PinName pin);
Zaitsev 10:41552d038a69 48 static inline uint32_t set_mask (PinName pin, int function);
Zaitsev 10:41552d038a69 49 static inline volatile uint32_t * func_reg (PinName pin);
Zaitsev 10:41552d038a69 50
Zaitsev 10:41552d038a69 51 /* ----------------------------------------------------------------
Zaitsev 10:41552d038a69 52 * NON-API FUNCTIONS
Zaitsev 10:41552d038a69 53 * ----------------------------------------------------------------*/
Zaitsev 10:41552d038a69 54
Zaitsev 10:41552d038a69 55 // Return the clear mask for a pin
Zaitsev 10:41552d038a69 56 static inline uint32_t clr_mask (PinName pin)
Zaitsev 10:41552d038a69 57 {
Zaitsev 10:41552d038a69 58 return HAL_PIO_MASK_FUNC << ((pin & HAL_PIO_MODULO_4_MASK) << 3);
Zaitsev 10:41552d038a69 59 }
Zaitsev 10:41552d038a69 60
Zaitsev 10:41552d038a69 61 // Return the set mask for a pin and a given function
Zaitsev 10:41552d038a69 62 static inline uint32_t set_mask (PinName pin, int function)
Zaitsev 10:41552d038a69 63 {
Zaitsev 10:41552d038a69 64 return function << ((pin & HAL_PIO_MODULO_4_MASK) << 3);
Zaitsev 10:41552d038a69 65 }
Zaitsev 10:41552d038a69 66
Zaitsev 10:41552d038a69 67 // Return the function register for a pin
Zaitsev 10:41552d038a69 68 static inline volatile uint32_t * func_reg (PinName pin)
Zaitsev 10:41552d038a69 69 {
Zaitsev 10:41552d038a69 70 return &PIO_FUNC0 + (pin >> 2);
Zaitsev 10:41552d038a69 71 }
Zaitsev 10:41552d038a69 72
Zaitsev 10:41552d038a69 73 // Return the owner of a pin
Zaitsev 10:41552d038a69 74 // 0: None
Zaitsev 10:41552d038a69 75 // 1: security core
Zaitsev 10:41552d038a69 76 // 2: protocol core
Zaitsev 10:41552d038a69 77 // 3: apps core
Zaitsev 10:41552d038a69 78 static inline uint8_t get_owner(PinName pin)
Zaitsev 10:41552d038a69 79 {
Zaitsev 10:41552d038a69 80 uint8_t pio_owner_shift = (pin & 0x0F) << 1;
Zaitsev 10:41552d038a69 81 volatile uint32_t * pio_owner_reg = (&PIO_OWNER0 + (pin >> 4));
Zaitsev 10:41552d038a69 82
Zaitsev 10:41552d038a69 83 return 0x03 & (*pio_owner_reg >> pio_owner_shift);
Zaitsev 10:41552d038a69 84 }
Zaitsev 10:41552d038a69 85
Zaitsev 10:41552d038a69 86 /* ----------------------------------------------------------------
Zaitsev 10:41552d038a69 87 * MBED "INTERNAL" API CALLS
Zaitsev 10:41552d038a69 88 * ----------------------------------------------------------------*/
Zaitsev 10:41552d038a69 89
Zaitsev 10:41552d038a69 90 void pin_function(PinName pin, int function)
Zaitsev 10:41552d038a69 91 {
Zaitsev 10:41552d038a69 92 volatile uint32_t *pio_func_reg;
Zaitsev 10:41552d038a69 93
Zaitsev 10:41552d038a69 94 /* Set the function for the given pin */
Zaitsev 10:41552d038a69 95 pio_func_reg = func_reg (pin);
Zaitsev 10:41552d038a69 96 *pio_func_reg = (*pio_func_reg & ~(clr_mask(pin))) | set_mask(pin, function);
Zaitsev 10:41552d038a69 97
Zaitsev 10:41552d038a69 98 /* Power the pin */
Zaitsev 10:41552d038a69 99 #ifdef TARGET_SARA_NBIOT
Zaitsev 10:41552d038a69 100 /* On Sara NBIoT, GPIO pin 19 has to be high to power GPIO pins 0 to 10 */
Zaitsev 10:41552d038a69 101 if ((pin >= p0) && (pin <= p10)) {
Zaitsev 10:41552d038a69 102 /* Grab pin 19 as a GPIO if we don't have it already */
Zaitsev 10:41552d038a69 103 if (get_owner(p19) != 0x03) {
Zaitsev 10:41552d038a69 104 pio_func_reg = func_reg (p19);
Zaitsev 10:41552d038a69 105 *pio_func_reg = (*pio_func_reg & ~(clr_mask(p19))) | set_mask(p19, 1); /* 1 == PIN_FUNCTION_GPIO */
Zaitsev 10:41552d038a69 106
Zaitsev 10:41552d038a69 107 MBED_ASSERT (get_owner(p19) == 0x03);
Zaitsev 10:41552d038a69 108 }
Zaitsev 10:41552d038a69 109
Zaitsev 10:41552d038a69 110 /* Set pin 19 to be an output and to be high */
Zaitsev 10:41552d038a69 111 GPIO_DIR |= (1ul << p19);
Zaitsev 10:41552d038a69 112 GPIO_OUT_BITSET = (1ul << p19);
Zaitsev 10:41552d038a69 113
Zaitsev 10:41552d038a69 114 /* Note: the level on pins 6 to 10 is controlled by the protocol
Zaitsev 10:41552d038a69 115 * processor to be the VCC level required by the SIM. The
Zaitsev 10:41552d038a69 116 * application has no control over this. */
Zaitsev 10:41552d038a69 117 }
Zaitsev 10:41552d038a69 118 /* The power to GPIOs 11 to 19 is fed directly from pin 51 of the module */
Zaitsev 10:41552d038a69 119 #endif
Zaitsev 10:41552d038a69 120 }
Zaitsev 10:41552d038a69 121
Zaitsev 10:41552d038a69 122 void pin_mode(PinName pin, PinMode mode)
Zaitsev 10:41552d038a69 123 {
Zaitsev 10:41552d038a69 124 MBED_ASSERT(pin != (PinName)NC);
Zaitsev 10:41552d038a69 125
Zaitsev 10:41552d038a69 126 switch (mode) {
Zaitsev 10:41552d038a69 127 case PullUp:
Zaitsev 10:41552d038a69 128 {
Zaitsev 10:41552d038a69 129 MBED_ASSERT(false); /* Not currently supported on HI2100 */
Zaitsev 10:41552d038a69 130 }
Zaitsev 10:41552d038a69 131 break;
Zaitsev 10:41552d038a69 132 case PullDown:
Zaitsev 10:41552d038a69 133 {
Zaitsev 10:41552d038a69 134 GPIO_PULLEN_BITSET = 1U << pin;
Zaitsev 10:41552d038a69 135 }
Zaitsev 10:41552d038a69 136 break;
Zaitsev 10:41552d038a69 137 case PullNone:
Zaitsev 10:41552d038a69 138 {
Zaitsev 10:41552d038a69 139 GPIO_PULLEN_BITCLR = 1U << pin;
Zaitsev 10:41552d038a69 140 }
Zaitsev 10:41552d038a69 141 break;
Zaitsev 10:41552d038a69 142 default:
Zaitsev 10:41552d038a69 143 break;
Zaitsev 10:41552d038a69 144 }
Zaitsev 10:41552d038a69 145 }