USB Serial application

Fork of USBSerial_HelloWorld by Samuel Mokrani

Committer:
Zaitsev
Date:
Sat Dec 16 10:26:48 2017 +0000
Revision:
11:b3f2a8bdac4d
Parent:
10:41552d038a69
A copy for D.S;

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) 2015, 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 // Warning: order must be the same as the one defined in PinNames.h !!!
Zaitsev 10:41552d038a69 37 static const uint32_t gpio_mode[13] = {
Zaitsev 10:41552d038a69 38 0x00000000, // 0 = GPIO_MODE_INPUT
Zaitsev 10:41552d038a69 39 0x00000001, // 1 = GPIO_MODE_OUTPUT_PP
Zaitsev 10:41552d038a69 40 0x00000011, // 2 = GPIO_MODE_OUTPUT_OD
Zaitsev 10:41552d038a69 41 0x00000002, // 3 = GPIO_MODE_AF_PP
Zaitsev 10:41552d038a69 42 0x00000012, // 4 = GPIO_MODE_AF_OD
Zaitsev 10:41552d038a69 43 0x00000003, // 5 = GPIO_MODE_ANALOG
Zaitsev 10:41552d038a69 44 0x10110000, // 6 = GPIO_MODE_IT_RISING
Zaitsev 10:41552d038a69 45 0x10210000, // 7 = GPIO_MODE_IT_FALLING
Zaitsev 10:41552d038a69 46 0x10310000, // 8 = GPIO_MODE_IT_RISING_FALLING
Zaitsev 10:41552d038a69 47 0x10120000, // 9 = GPIO_MODE_EVT_RISING
Zaitsev 10:41552d038a69 48 0x10220000, // 10 = GPIO_MODE_EVT_FALLING
Zaitsev 10:41552d038a69 49 0x10320000, // 11 = GPIO_MODE_EVT_RISING_FALLING
Zaitsev 10:41552d038a69 50 0x10000000 // 12 = Reset IT and EVT (not in STM32Cube HAL)
Zaitsev 10:41552d038a69 51 };
Zaitsev 10:41552d038a69 52
Zaitsev 10:41552d038a69 53 // Enable GPIO clock and return GPIO base address
Zaitsev 10:41552d038a69 54 uint32_t Set_GPIO_Clock(uint32_t port_idx)
Zaitsev 10:41552d038a69 55 {
Zaitsev 10:41552d038a69 56 uint32_t gpio_add = 0;
Zaitsev 10:41552d038a69 57 switch (port_idx) {
Zaitsev 10:41552d038a69 58 case PortA:
Zaitsev 10:41552d038a69 59 gpio_add = GPIOA_BASE;
Zaitsev 10:41552d038a69 60 __GPIOA_CLK_ENABLE();
Zaitsev 10:41552d038a69 61 break;
Zaitsev 10:41552d038a69 62 case PortB:
Zaitsev 10:41552d038a69 63 gpio_add = GPIOB_BASE;
Zaitsev 10:41552d038a69 64 __GPIOB_CLK_ENABLE();
Zaitsev 10:41552d038a69 65 break;
Zaitsev 10:41552d038a69 66 #if defined(GPIOC_BASE)
Zaitsev 10:41552d038a69 67 case PortC:
Zaitsev 10:41552d038a69 68 gpio_add = GPIOC_BASE;
Zaitsev 10:41552d038a69 69 __GPIOC_CLK_ENABLE();
Zaitsev 10:41552d038a69 70 break;
Zaitsev 10:41552d038a69 71 #endif
Zaitsev 10:41552d038a69 72 #if defined(GPIOD_BASE)
Zaitsev 10:41552d038a69 73 case PortD:
Zaitsev 10:41552d038a69 74 gpio_add = GPIOD_BASE;
Zaitsev 10:41552d038a69 75 __GPIOD_CLK_ENABLE();
Zaitsev 10:41552d038a69 76 break;
Zaitsev 10:41552d038a69 77 #endif
Zaitsev 10:41552d038a69 78 #if defined(GPIOH_BASE)
Zaitsev 10:41552d038a69 79 case PortH:
Zaitsev 10:41552d038a69 80 gpio_add = GPIOH_BASE;
Zaitsev 10:41552d038a69 81 __GPIOH_CLK_ENABLE();
Zaitsev 10:41552d038a69 82 break;
Zaitsev 10:41552d038a69 83 #endif
Zaitsev 10:41552d038a69 84 default:
Zaitsev 10:41552d038a69 85 error("Pinmap error: wrong port number.");
Zaitsev 10:41552d038a69 86 break;
Zaitsev 10:41552d038a69 87 }
Zaitsev 10:41552d038a69 88 return gpio_add;
Zaitsev 10:41552d038a69 89 }
Zaitsev 10:41552d038a69 90
Zaitsev 10:41552d038a69 91 /**
Zaitsev 10:41552d038a69 92 * Configure pin (mode, speed, output type and pull-up/pull-down)
Zaitsev 10:41552d038a69 93 */
Zaitsev 10:41552d038a69 94 void pin_function(PinName pin, int data)
Zaitsev 10:41552d038a69 95 {
Zaitsev 10:41552d038a69 96 MBED_ASSERT(pin != (PinName)NC);
Zaitsev 10:41552d038a69 97 // Get the pin informations
Zaitsev 10:41552d038a69 98 uint32_t mode = STM_PIN_MODE(data);
Zaitsev 10:41552d038a69 99 uint32_t pupd = STM_PIN_PUPD(data);
Zaitsev 10:41552d038a69 100 uint32_t afnum = STM_PIN_AFNUM(data);
Zaitsev 10:41552d038a69 101
Zaitsev 10:41552d038a69 102 uint32_t port_index = STM_PORT(pin);
Zaitsev 10:41552d038a69 103 uint32_t pin_index = STM_PIN(pin);
Zaitsev 10:41552d038a69 104
Zaitsev 10:41552d038a69 105 // Enable GPIO clock
Zaitsev 10:41552d038a69 106 uint32_t gpio_add = Set_GPIO_Clock(port_index);
Zaitsev 10:41552d038a69 107 GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add;
Zaitsev 10:41552d038a69 108
Zaitsev 10:41552d038a69 109 // Configure GPIO
Zaitsev 10:41552d038a69 110 GPIO_InitTypeDef GPIO_InitStructure;
Zaitsev 10:41552d038a69 111 GPIO_InitStructure.Pin = (uint32_t)(1 << pin_index);
Zaitsev 10:41552d038a69 112 GPIO_InitStructure.Mode = gpio_mode[mode];
Zaitsev 10:41552d038a69 113 GPIO_InitStructure.Pull = pupd;
Zaitsev 10:41552d038a69 114 GPIO_InitStructure.Speed = GPIO_SPEED_HIGH;
Zaitsev 10:41552d038a69 115 GPIO_InitStructure.Alternate = afnum;
Zaitsev 10:41552d038a69 116 HAL_GPIO_Init(gpio, &GPIO_InitStructure);
Zaitsev 10:41552d038a69 117
Zaitsev 10:41552d038a69 118 // [TODO] Disconnect JTAG-DP + SW-DP signals.
Zaitsev 10:41552d038a69 119 // Warning: Need to reconnect under reset
Zaitsev 10:41552d038a69 120 //if ((pin == PA_13) || (pin == PA_14)) {
Zaitsev 10:41552d038a69 121 //
Zaitsev 10:41552d038a69 122 //}
Zaitsev 10:41552d038a69 123 //if ((pin == PA_15) || (pin == PB_3) || (pin == PB_4)) {
Zaitsev 10:41552d038a69 124 //
Zaitsev 10:41552d038a69 125 //}
Zaitsev 10:41552d038a69 126 }
Zaitsev 10:41552d038a69 127
Zaitsev 10:41552d038a69 128 /**
Zaitsev 10:41552d038a69 129 * Configure pin pull-up/pull-down
Zaitsev 10:41552d038a69 130 */
Zaitsev 10:41552d038a69 131 void pin_mode(PinName pin, PinMode mode)
Zaitsev 10:41552d038a69 132 {
Zaitsev 10:41552d038a69 133 MBED_ASSERT(pin != (PinName)NC);
Zaitsev 10:41552d038a69 134 uint32_t port_index = STM_PORT(pin);
Zaitsev 10:41552d038a69 135 uint32_t pin_index = STM_PIN(pin);
Zaitsev 10:41552d038a69 136
Zaitsev 10:41552d038a69 137 // Enable GPIO clock
Zaitsev 10:41552d038a69 138 uint32_t gpio_add = Set_GPIO_Clock(port_index);
Zaitsev 10:41552d038a69 139 GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add;
Zaitsev 10:41552d038a69 140
Zaitsev 10:41552d038a69 141 // Configure pull-up/pull-down resistors
Zaitsev 10:41552d038a69 142 uint32_t pupd = (uint32_t)mode;
Zaitsev 10:41552d038a69 143 if (pupd > 2)
Zaitsev 10:41552d038a69 144 {
Zaitsev 10:41552d038a69 145 pupd = 0; // Open-drain = No pull-up/No pull-down
Zaitsev 10:41552d038a69 146 }
Zaitsev 10:41552d038a69 147 gpio->PUPDR &= (uint32_t)(~(GPIO_PUPDR_PUPD0 << (pin_index * 2)));
Zaitsev 10:41552d038a69 148 gpio->PUPDR |= (uint32_t)(pupd << (pin_index * 2));
Zaitsev 10:41552d038a69 149 }
Zaitsev 10:41552d038a69 150
Zaitsev 10:41552d038a69 151 /* Internal function for setting the gpiomode/function
Zaitsev 10:41552d038a69 152 * without changing Pull mode
Zaitsev 10:41552d038a69 153 */
Zaitsev 10:41552d038a69 154 void pin_function_gpiomode(PinName pin, uint32_t gpiomode) {
Zaitsev 10:41552d038a69 155
Zaitsev 10:41552d038a69 156 /* Read current pull state from HW to avoid over-write*/
Zaitsev 10:41552d038a69 157 uint32_t port_index = STM_PORT(pin);
Zaitsev 10:41552d038a69 158 uint32_t pin_index = STM_PIN(pin);
Zaitsev 10:41552d038a69 159 GPIO_TypeDef *gpio = (GPIO_TypeDef *) Set_GPIO_Clock(port_index);
Zaitsev 10:41552d038a69 160 uint32_t temp = gpio->PUPDR;
Zaitsev 10:41552d038a69 161 uint32_t pull = (temp >> (pin_index * 2U)) & GPIO_PUPDR_PUPD0;
Zaitsev 10:41552d038a69 162
Zaitsev 10:41552d038a69 163 /* Then re-use global function for updating the mode part*/
Zaitsev 10:41552d038a69 164 pin_function(pin, STM_PIN_DATA(gpiomode, pull, 0));
Zaitsev 10:41552d038a69 165 }