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