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) 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 // Warning: the elements 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 GPIO_MODE_INPUT, // 0 = STM_MODE_INPUT
Zaitsev 10:41552d038a69 39 GPIO_MODE_OUTPUT_PP, // 1 = STM_MODE_OUTPUT_PP
Zaitsev 10:41552d038a69 40 GPIO_MODE_OUTPUT_OD, // 2 = STM_MODE_OUTPUT_OD
Zaitsev 10:41552d038a69 41 GPIO_MODE_AF_PP, // 3 = STM_MODE_AF_PP
Zaitsev 10:41552d038a69 42 GPIO_MODE_AF_OD, // 4 = STM_MODE_AF_OD
Zaitsev 10:41552d038a69 43 GPIO_MODE_ANALOG, // 5 = STM_MODE_ANALOG
Zaitsev 10:41552d038a69 44 GPIO_MODE_IT_RISING, // 6 = STM_MODE_IT_RISING
Zaitsev 10:41552d038a69 45 GPIO_MODE_IT_FALLING, // 7 = STM_MODE_IT_FALLING
Zaitsev 10:41552d038a69 46 GPIO_MODE_IT_RISING_FALLING, // 8 = STM_MODE_IT_RISING_FALLING
Zaitsev 10:41552d038a69 47 GPIO_MODE_EVT_RISING, // 9 = STM_MODE_EVT_RISING
Zaitsev 10:41552d038a69 48 GPIO_MODE_EVT_FALLING, // 10 = STM_MODE_EVT_FALLING
Zaitsev 10:41552d038a69 49 GPIO_MODE_EVT_RISING_FALLING, // 11 = STM_MODE_EVT_RISING_FALLING
Zaitsev 10:41552d038a69 50 0x10000000 // 12 = STM_MODE_IT_EVT_RESET (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 case PortC:
Zaitsev 10:41552d038a69 67 gpio_add = GPIOC_BASE;
Zaitsev 10:41552d038a69 68 __GPIOC_CLK_ENABLE();
Zaitsev 10:41552d038a69 69 break;
Zaitsev 10:41552d038a69 70 case PortD:
Zaitsev 10:41552d038a69 71 gpio_add = GPIOD_BASE;
Zaitsev 10:41552d038a69 72 __GPIOD_CLK_ENABLE();
Zaitsev 10:41552d038a69 73 break;
Zaitsev 10:41552d038a69 74 #if defined GPIOE_BASE
Zaitsev 10:41552d038a69 75 case PortE:
Zaitsev 10:41552d038a69 76 gpio_add = GPIOE_BASE;
Zaitsev 10:41552d038a69 77 __GPIOE_CLK_ENABLE();
Zaitsev 10:41552d038a69 78 break;
Zaitsev 10:41552d038a69 79 #endif
Zaitsev 10:41552d038a69 80 #if defined GPIOF_BASE
Zaitsev 10:41552d038a69 81 case PortF:
Zaitsev 10:41552d038a69 82 gpio_add = GPIOF_BASE;
Zaitsev 10:41552d038a69 83 __GPIOF_CLK_ENABLE();
Zaitsev 10:41552d038a69 84 break;
Zaitsev 10:41552d038a69 85 #endif
Zaitsev 10:41552d038a69 86 #if defined GPIOG_BASE
Zaitsev 10:41552d038a69 87 case PortG:
Zaitsev 10:41552d038a69 88 gpio_add = GPIOG_BASE;
Zaitsev 10:41552d038a69 89 __GPIOG_CLK_ENABLE();
Zaitsev 10:41552d038a69 90 break;
Zaitsev 10:41552d038a69 91 #endif
Zaitsev 10:41552d038a69 92 #if defined GPIOH_BASE
Zaitsev 10:41552d038a69 93 case PortH:
Zaitsev 10:41552d038a69 94 gpio_add = GPIOH_BASE;
Zaitsev 10:41552d038a69 95 __GPIOH_CLK_ENABLE();
Zaitsev 10:41552d038a69 96 break;
Zaitsev 10:41552d038a69 97 #endif
Zaitsev 10:41552d038a69 98 #if defined GPIOI_BASE
Zaitsev 10:41552d038a69 99 case PortI:
Zaitsev 10:41552d038a69 100 gpio_add = GPIOI_BASE;
Zaitsev 10:41552d038a69 101 __GPIOI_CLK_ENABLE();
Zaitsev 10:41552d038a69 102 break;
Zaitsev 10:41552d038a69 103 #endif
Zaitsev 10:41552d038a69 104 #if defined GPIOJ_BASE
Zaitsev 10:41552d038a69 105 case PortJ:
Zaitsev 10:41552d038a69 106 gpio_add = GPIOJ_BASE;
Zaitsev 10:41552d038a69 107 __GPIOJ_CLK_ENABLE();
Zaitsev 10:41552d038a69 108 break;
Zaitsev 10:41552d038a69 109 #endif
Zaitsev 10:41552d038a69 110 #if defined GPIOK_BASE
Zaitsev 10:41552d038a69 111 case PortK:
Zaitsev 10:41552d038a69 112 gpio_add = GPIOK_BASE;
Zaitsev 10:41552d038a69 113 __GPIOK_CLK_ENABLE();
Zaitsev 10:41552d038a69 114 break;
Zaitsev 10:41552d038a69 115 #endif
Zaitsev 10:41552d038a69 116
Zaitsev 10:41552d038a69 117
Zaitsev 10:41552d038a69 118 default:
Zaitsev 10:41552d038a69 119 error("Pinmap error: wrong port number.");
Zaitsev 10:41552d038a69 120 break;
Zaitsev 10:41552d038a69 121 }
Zaitsev 10:41552d038a69 122 return gpio_add;
Zaitsev 10:41552d038a69 123 }
Zaitsev 10:41552d038a69 124
Zaitsev 10:41552d038a69 125 /**
Zaitsev 10:41552d038a69 126 * Configure pin (mode, speed, output type and pull-up/pull-down)
Zaitsev 10:41552d038a69 127 */
Zaitsev 10:41552d038a69 128 void pin_function(PinName pin, int data)
Zaitsev 10:41552d038a69 129 {
Zaitsev 10:41552d038a69 130 MBED_ASSERT(pin != (PinName)NC);
Zaitsev 10:41552d038a69 131 // Get the pin informations
Zaitsev 10:41552d038a69 132 uint32_t mode = STM_PIN_MODE(data);
Zaitsev 10:41552d038a69 133 uint32_t pupd = STM_PIN_PUPD(data);
Zaitsev 10:41552d038a69 134 uint32_t afnum = STM_PIN_AFNUM(data);
Zaitsev 10:41552d038a69 135
Zaitsev 10:41552d038a69 136 uint32_t port_index = STM_PORT(pin);
Zaitsev 10:41552d038a69 137 uint32_t pin_index = STM_PIN(pin);
Zaitsev 10:41552d038a69 138
Zaitsev 10:41552d038a69 139 // Enable GPIO clock
Zaitsev 10:41552d038a69 140 uint32_t gpio_add = Set_GPIO_Clock(port_index);
Zaitsev 10:41552d038a69 141 GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add;
Zaitsev 10:41552d038a69 142
Zaitsev 10:41552d038a69 143 // Configure GPIO
Zaitsev 10:41552d038a69 144 GPIO_InitTypeDef GPIO_InitStructure;
Zaitsev 10:41552d038a69 145 GPIO_InitStructure.Pin = (uint32_t)(1 << pin_index);
Zaitsev 10:41552d038a69 146 GPIO_InitStructure.Mode = gpio_mode[mode];
Zaitsev 10:41552d038a69 147 GPIO_InitStructure.Pull = pupd;
Zaitsev 10:41552d038a69 148 GPIO_InitStructure.Speed = GPIO_SPEED_HIGH;
Zaitsev 10:41552d038a69 149 GPIO_InitStructure.Alternate = afnum;
Zaitsev 10:41552d038a69 150 HAL_GPIO_Init(gpio, &GPIO_InitStructure);
Zaitsev 10:41552d038a69 151
Zaitsev 10:41552d038a69 152 // [TODO] Disconnect JTAG-DP + SW-DP signals.
Zaitsev 10:41552d038a69 153 // Warning: Need to reconnect under reset
Zaitsev 10:41552d038a69 154 //if ((pin == PA_13) || (pin == PA_14)) {
Zaitsev 10:41552d038a69 155 //
Zaitsev 10:41552d038a69 156 //}
Zaitsev 10:41552d038a69 157 }
Zaitsev 10:41552d038a69 158
Zaitsev 10:41552d038a69 159 /**
Zaitsev 10:41552d038a69 160 * Configure pin pull-up/pull-down
Zaitsev 10:41552d038a69 161 */
Zaitsev 10:41552d038a69 162 void pin_mode(PinName pin, PinMode mode)
Zaitsev 10:41552d038a69 163 {
Zaitsev 10:41552d038a69 164 MBED_ASSERT(pin != (PinName)NC);
Zaitsev 10:41552d038a69 165 uint32_t port_index = STM_PORT(pin);
Zaitsev 10:41552d038a69 166 uint32_t pin_index = STM_PIN(pin);
Zaitsev 10:41552d038a69 167
Zaitsev 10:41552d038a69 168 // Enable GPIO clock
Zaitsev 10:41552d038a69 169 uint32_t gpio_add = Set_GPIO_Clock(port_index);
Zaitsev 10:41552d038a69 170 GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add;
Zaitsev 10:41552d038a69 171
Zaitsev 10:41552d038a69 172 // Configure pull-up/pull-down resistors
Zaitsev 10:41552d038a69 173 uint32_t pupd = (uint32_t)mode;
Zaitsev 10:41552d038a69 174 if (pupd > 2) {
Zaitsev 10:41552d038a69 175 pupd = 0; // Open-drain = No pull-up/No pull-down
Zaitsev 10:41552d038a69 176 }
Zaitsev 10:41552d038a69 177 gpio->PUPDR &= (uint32_t)(~(GPIO_PUPDR_PUPDR0 << (pin_index * 2)));
Zaitsev 10:41552d038a69 178 gpio->PUPDR |= (uint32_t)(pupd << (pin_index * 2));
Zaitsev 10:41552d038a69 179 }
Zaitsev 10:41552d038a69 180
Zaitsev 10:41552d038a69 181 /* Internal function for setting the gpiomode/function
Zaitsev 10:41552d038a69 182 * without changing Pull mode
Zaitsev 10:41552d038a69 183 */
Zaitsev 10:41552d038a69 184 void pin_function_gpiomode(PinName pin, uint32_t gpiomode) {
Zaitsev 10:41552d038a69 185
Zaitsev 10:41552d038a69 186 /* Read current pull state from HW to avoid over-write*/
Zaitsev 10:41552d038a69 187 uint32_t port_index = STM_PORT(pin);
Zaitsev 10:41552d038a69 188 uint32_t pin_index = STM_PIN(pin);
Zaitsev 10:41552d038a69 189 GPIO_TypeDef *gpio = (GPIO_TypeDef *) Set_GPIO_Clock(port_index);
Zaitsev 10:41552d038a69 190 uint32_t temp = gpio->PUPDR;
Zaitsev 10:41552d038a69 191 uint32_t pull = (temp >> (pin_index * 2U)) & GPIO_PUPDR_PUPDR0;
Zaitsev 10:41552d038a69 192
Zaitsev 10:41552d038a69 193 /* Then re-use global function for updating the mode part*/
Zaitsev 10:41552d038a69 194 pin_function(pin, STM_PIN_DATA(gpiomode, pull, 0));
Zaitsev 10:41552d038a69 195 }