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 *******************************************************************************
Zaitsev 10:41552d038a69 3 * Copyright (c) 2014, STMicroelectronics
Zaitsev 10:41552d038a69 4 * All rights reserved.
Zaitsev 10:41552d038a69 5 *
Zaitsev 10:41552d038a69 6 * Redistribution and use in source and binary forms, with or without
Zaitsev 10:41552d038a69 7 * modification, are permitted provided that the following conditions are met:
Zaitsev 10:41552d038a69 8 *
Zaitsev 10:41552d038a69 9 * 1. Redistributions of source code must retain the above copyright notice,
Zaitsev 10:41552d038a69 10 * this list of conditions and the following disclaimer.
Zaitsev 10:41552d038a69 11 * 2. Redistributions in binary form must reproduce the above copyright notice,
Zaitsev 10:41552d038a69 12 * this list of conditions and the following disclaimer in the documentation
Zaitsev 10:41552d038a69 13 * and/or other materials provided with the distribution.
Zaitsev 10:41552d038a69 14 * 3. Neither the name of STMicroelectronics nor the names of its contributors
Zaitsev 10:41552d038a69 15 * may be used to endorse or promote products derived from this software
Zaitsev 10:41552d038a69 16 * without specific prior written permission.
Zaitsev 10:41552d038a69 17 *
Zaitsev 10:41552d038a69 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Zaitsev 10:41552d038a69 19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Zaitsev 10:41552d038a69 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
Zaitsev 10:41552d038a69 21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
Zaitsev 10:41552d038a69 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Zaitsev 10:41552d038a69 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
Zaitsev 10:41552d038a69 24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
Zaitsev 10:41552d038a69 25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
Zaitsev 10:41552d038a69 26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Zaitsev 10:41552d038a69 27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Zaitsev 10:41552d038a69 28 *******************************************************************************
Zaitsev 10:41552d038a69 29 */
Zaitsev 10:41552d038a69 30 #include "mbed_assert.h"
Zaitsev 10:41552d038a69 31 #include "pinmap.h"
Zaitsev 10:41552d038a69 32 #include "PortNames.h"
Zaitsev 10:41552d038a69 33 #include "mbed_error.h"
Zaitsev 10:41552d038a69 34
Zaitsev 10:41552d038a69 35 // GPIO mode look-up table
Zaitsev 10:41552d038a69 36 static const uint32_t gpio_mode[13] = {
Zaitsev 10:41552d038a69 37 0x00000000, // 0 = GPIO_MODE_INPUT
Zaitsev 10:41552d038a69 38 0x00000001, // 1 = GPIO_MODE_OUTPUT_PP
Zaitsev 10:41552d038a69 39 0x00000011, // 2 = GPIO_MODE_OUTPUT_OD
Zaitsev 10:41552d038a69 40 0x00000002, // 3 = GPIO_MODE_AF_PP
Zaitsev 10:41552d038a69 41 0x00000012, // 4 = GPIO_MODE_AF_OD
Zaitsev 10:41552d038a69 42 0x00000003, // 5 = GPIO_MODE_ANALOG
Zaitsev 10:41552d038a69 43 0x10110000, // 6 = GPIO_MODE_IT_RISING
Zaitsev 10:41552d038a69 44 0x10210000, // 7 = GPIO_MODE_IT_FALLING
Zaitsev 10:41552d038a69 45 0x10310000, // 8 = GPIO_MODE_IT_RISING_FALLING
Zaitsev 10:41552d038a69 46 0x10120000, // 9 = GPIO_MODE_EVT_RISING
Zaitsev 10:41552d038a69 47 0x10220000, // 10 = GPIO_MODE_EVT_FALLING
Zaitsev 10:41552d038a69 48 0x10320000, // 11 = GPIO_MODE_EVT_RISING_FALLING
Zaitsev 10:41552d038a69 49 0x10000000 // 12 = Reset IT and EVT (not in STM32Cube HAL)
Zaitsev 10:41552d038a69 50 };
Zaitsev 10:41552d038a69 51
Zaitsev 10:41552d038a69 52 // Enable GPIO clock and return GPIO base address
Zaitsev 10:41552d038a69 53 uint32_t Set_GPIO_Clock(uint32_t port_idx) {
Zaitsev 10:41552d038a69 54 uint32_t gpio_add = 0;
Zaitsev 10:41552d038a69 55 switch (port_idx) {
Zaitsev 10:41552d038a69 56 case PortA:
Zaitsev 10:41552d038a69 57 gpio_add = GPIOA_BASE;
Zaitsev 10:41552d038a69 58 __GPIOA_CLK_ENABLE();
Zaitsev 10:41552d038a69 59 break;
Zaitsev 10:41552d038a69 60 case PortB:
Zaitsev 10:41552d038a69 61 gpio_add = GPIOB_BASE;
Zaitsev 10:41552d038a69 62 __GPIOB_CLK_ENABLE();
Zaitsev 10:41552d038a69 63 break;
Zaitsev 10:41552d038a69 64 #if defined(GPIOC_BASE)
Zaitsev 10:41552d038a69 65 case PortC:
Zaitsev 10:41552d038a69 66 gpio_add = GPIOC_BASE;
Zaitsev 10:41552d038a69 67 __GPIOC_CLK_ENABLE();
Zaitsev 10:41552d038a69 68 break;
Zaitsev 10:41552d038a69 69 #endif
Zaitsev 10:41552d038a69 70 #if defined(GPIOD_BASE)
Zaitsev 10:41552d038a69 71 case PortD:
Zaitsev 10:41552d038a69 72 gpio_add = GPIOD_BASE;
Zaitsev 10:41552d038a69 73 __GPIOD_CLK_ENABLE();
Zaitsev 10:41552d038a69 74 break;
Zaitsev 10:41552d038a69 75 #endif
Zaitsev 10:41552d038a69 76 #if defined(GPIOF_BASE)
Zaitsev 10:41552d038a69 77 case PortF:
Zaitsev 10:41552d038a69 78 gpio_add = GPIOF_BASE;
Zaitsev 10:41552d038a69 79 __GPIOF_CLK_ENABLE();
Zaitsev 10:41552d038a69 80 break;
Zaitsev 10:41552d038a69 81 #endif
Zaitsev 10:41552d038a69 82 default:
Zaitsev 10:41552d038a69 83 error("Pinmap error: wrong port number.");
Zaitsev 10:41552d038a69 84 break;
Zaitsev 10:41552d038a69 85 }
Zaitsev 10:41552d038a69 86 return gpio_add;
Zaitsev 10:41552d038a69 87 }
Zaitsev 10:41552d038a69 88
Zaitsev 10:41552d038a69 89 /**
Zaitsev 10:41552d038a69 90 * Configure pin (mode, speed, output type and pull-up/pull-down)
Zaitsev 10:41552d038a69 91 */
Zaitsev 10:41552d038a69 92 void pin_function(PinName pin, int data) {
Zaitsev 10:41552d038a69 93 MBED_ASSERT(pin != (PinName)NC);
Zaitsev 10:41552d038a69 94
Zaitsev 10:41552d038a69 95 // Get the pin informations
Zaitsev 10:41552d038a69 96 uint32_t mode = STM_PIN_MODE(data);
Zaitsev 10:41552d038a69 97 uint32_t pupd = STM_PIN_PUPD(data);
Zaitsev 10:41552d038a69 98 uint32_t afnum = STM_PIN_AFNUM(data);
Zaitsev 10:41552d038a69 99
Zaitsev 10:41552d038a69 100 uint32_t port_index = STM_PORT(pin);
Zaitsev 10:41552d038a69 101 uint32_t pin_index = STM_PIN(pin);
Zaitsev 10:41552d038a69 102
Zaitsev 10:41552d038a69 103 // Enable GPIO clock
Zaitsev 10:41552d038a69 104 uint32_t gpio_add = Set_GPIO_Clock(port_index);
Zaitsev 10:41552d038a69 105 GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add;
Zaitsev 10:41552d038a69 106
Zaitsev 10:41552d038a69 107 // Configure GPIO
Zaitsev 10:41552d038a69 108 GPIO_InitTypeDef GPIO_InitStructure;
Zaitsev 10:41552d038a69 109 GPIO_InitStructure.Pin = (uint32_t)(1 << pin_index);
Zaitsev 10:41552d038a69 110 GPIO_InitStructure.Mode = gpio_mode[mode];
Zaitsev 10:41552d038a69 111 GPIO_InitStructure.Pull = pupd;
Zaitsev 10:41552d038a69 112 GPIO_InitStructure.Speed = GPIO_SPEED_HIGH;
Zaitsev 10:41552d038a69 113 GPIO_InitStructure.Alternate = afnum;
Zaitsev 10:41552d038a69 114 HAL_GPIO_Init(gpio, &GPIO_InitStructure);
Zaitsev 10:41552d038a69 115
Zaitsev 10:41552d038a69 116 // [TODO] Disconnect SWDIO and SWCLK signals ?
Zaitsev 10:41552d038a69 117 // Warning: For debugging it is necessary to reconnect under reset if this is done.
Zaitsev 10:41552d038a69 118 //if ((pin == PA_13) || (pin == PA_14)) {
Zaitsev 10:41552d038a69 119 //
Zaitsev 10:41552d038a69 120 //}
Zaitsev 10:41552d038a69 121 }
Zaitsev 10:41552d038a69 122
Zaitsev 10:41552d038a69 123 /**
Zaitsev 10:41552d038a69 124 * Configure pin pull-up/pull-down
Zaitsev 10:41552d038a69 125 */
Zaitsev 10:41552d038a69 126 void pin_mode(PinName pin, PinMode mode) {
Zaitsev 10:41552d038a69 127 MBED_ASSERT(pin != (PinName)NC);
Zaitsev 10:41552d038a69 128
Zaitsev 10:41552d038a69 129 uint32_t port_index = STM_PORT(pin);
Zaitsev 10:41552d038a69 130 uint32_t pin_index = STM_PIN(pin);
Zaitsev 10:41552d038a69 131
Zaitsev 10:41552d038a69 132 // Enable GPIO clock
Zaitsev 10:41552d038a69 133 uint32_t gpio_add = Set_GPIO_Clock(port_index);
Zaitsev 10:41552d038a69 134 GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add;
Zaitsev 10:41552d038a69 135
Zaitsev 10:41552d038a69 136 // Configure pull-up/pull-down resistors
Zaitsev 10:41552d038a69 137 uint32_t pupd = (uint32_t)mode;
Zaitsev 10:41552d038a69 138 if (pupd > 2) pupd = 0; // Open-drain = No pull-up/No pull-down
Zaitsev 10:41552d038a69 139 gpio->PUPDR &= (uint32_t)(~(GPIO_PUPDR_PUPDR0 << (pin_index * 2)));
Zaitsev 10:41552d038a69 140 gpio->PUPDR |= (uint32_t)(pupd << (pin_index * 2));
Zaitsev 10:41552d038a69 141 }
Zaitsev 10:41552d038a69 142
Zaitsev 10:41552d038a69 143 /* Internal function for setting the gpiomode/function
Zaitsev 10:41552d038a69 144 * without changing Pull mode
Zaitsev 10:41552d038a69 145 */
Zaitsev 10:41552d038a69 146 void pin_function_gpiomode(PinName pin, uint32_t gpiomode) {
Zaitsev 10:41552d038a69 147
Zaitsev 10:41552d038a69 148 /* Read current pull state from HW to avoid over-write*/
Zaitsev 10:41552d038a69 149 uint32_t port_index = STM_PORT(pin);
Zaitsev 10:41552d038a69 150 uint32_t pin_index = STM_PIN(pin);
Zaitsev 10:41552d038a69 151 GPIO_TypeDef *gpio = (GPIO_TypeDef *) Set_GPIO_Clock(port_index);
Zaitsev 10:41552d038a69 152 uint32_t temp = gpio->PUPDR;
Zaitsev 10:41552d038a69 153 uint32_t pull = (temp >> (pin_index * 2U)) & GPIO_PUPDR_PUPDR0;
Zaitsev 10:41552d038a69 154
Zaitsev 10:41552d038a69 155 /* Then re-use global function for updating the mode part*/
Zaitsev 10:41552d038a69 156 pin_function(pin, STM_PIN_DATA(gpiomode, pull, 0));
Zaitsev 10:41552d038a69 157 }