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 // 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 default:
Zaitsev 10:41552d038a69 75 error("Pinmap error: wrong port number.");
Zaitsev 10:41552d038a69 76 break;
Zaitsev 10:41552d038a69 77 }
Zaitsev 10:41552d038a69 78 return gpio_add;
Zaitsev 10:41552d038a69 79 }
Zaitsev 10:41552d038a69 80
Zaitsev 10:41552d038a69 81 /**
Zaitsev 10:41552d038a69 82 * Configure pin (input, output, alternate function or analog) + output speed + AF
Zaitsev 10:41552d038a69 83 */
Zaitsev 10:41552d038a69 84 void pin_function(PinName pin, int data)
Zaitsev 10:41552d038a69 85 {
Zaitsev 10:41552d038a69 86 MBED_ASSERT(pin != (PinName)NC);
Zaitsev 10:41552d038a69 87 // Get the pin informations
Zaitsev 10:41552d038a69 88 uint32_t mode = STM_PIN_MODE(data);
Zaitsev 10:41552d038a69 89 uint32_t pupd = STM_PIN_PUPD(data);
Zaitsev 10:41552d038a69 90 uint32_t afnum = STM_PIN_AFNUM(data);
Zaitsev 10:41552d038a69 91
Zaitsev 10:41552d038a69 92 uint32_t port_index = STM_PORT(pin);
Zaitsev 10:41552d038a69 93 uint32_t pin_index = STM_PIN(pin);
Zaitsev 10:41552d038a69 94
Zaitsev 10:41552d038a69 95 // Enable GPIO clock
Zaitsev 10:41552d038a69 96 uint32_t gpio_add = Set_GPIO_Clock(port_index);
Zaitsev 10:41552d038a69 97 GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add;
Zaitsev 10:41552d038a69 98
Zaitsev 10:41552d038a69 99 // Enable AFIO clock
Zaitsev 10:41552d038a69 100 __HAL_RCC_AFIO_CLK_ENABLE();
Zaitsev 10:41552d038a69 101
Zaitsev 10:41552d038a69 102 // Configure Alternate Function
Zaitsev 10:41552d038a69 103 // Warning: Must be done before the GPIO is initialized
Zaitsev 10:41552d038a69 104 if (afnum > 0) {
Zaitsev 10:41552d038a69 105 switch (afnum) {
Zaitsev 10:41552d038a69 106 case 1: // Remap SPI1
Zaitsev 10:41552d038a69 107 __HAL_AFIO_REMAP_SPI1_ENABLE();
Zaitsev 10:41552d038a69 108 break;
Zaitsev 10:41552d038a69 109 case 2: // Remap I2C1
Zaitsev 10:41552d038a69 110 __HAL_AFIO_REMAP_I2C1_ENABLE();
Zaitsev 10:41552d038a69 111 break;
Zaitsev 10:41552d038a69 112 case 3: // Remap USART1
Zaitsev 10:41552d038a69 113 __HAL_AFIO_REMAP_USART1_ENABLE();
Zaitsev 10:41552d038a69 114 break;
Zaitsev 10:41552d038a69 115 case 4: // Remap USART2
Zaitsev 10:41552d038a69 116 __HAL_AFIO_REMAP_USART2_ENABLE();
Zaitsev 10:41552d038a69 117 break;
Zaitsev 10:41552d038a69 118 case 5: // Partial Remap USART3
Zaitsev 10:41552d038a69 119 __HAL_AFIO_REMAP_USART3_PARTIAL();
Zaitsev 10:41552d038a69 120 break;
Zaitsev 10:41552d038a69 121 case 6: // Partial Remap TIM1
Zaitsev 10:41552d038a69 122 __HAL_AFIO_REMAP_TIM1_PARTIAL();
Zaitsev 10:41552d038a69 123 break;
Zaitsev 10:41552d038a69 124 case 7: // Partial Remap TIM3
Zaitsev 10:41552d038a69 125 __HAL_AFIO_REMAP_TIM3_PARTIAL();
Zaitsev 10:41552d038a69 126 break;
Zaitsev 10:41552d038a69 127 case 8: // Full Remap TIM2
Zaitsev 10:41552d038a69 128 __HAL_AFIO_REMAP_TIM2_ENABLE();
Zaitsev 10:41552d038a69 129 break;
Zaitsev 10:41552d038a69 130 case 9: // Full Remap TIM3
Zaitsev 10:41552d038a69 131 __HAL_AFIO_REMAP_TIM3_ENABLE();
Zaitsev 10:41552d038a69 132 break;
Zaitsev 10:41552d038a69 133 case 10: // CAN_RX mapped to PB8, CAN_TX mapped to PB9
Zaitsev 10:41552d038a69 134 __HAL_AFIO_REMAP_CAN1_2();
Zaitsev 10:41552d038a69 135 break;
Zaitsev 10:41552d038a69 136 default:
Zaitsev 10:41552d038a69 137 break;
Zaitsev 10:41552d038a69 138 }
Zaitsev 10:41552d038a69 139 }
Zaitsev 10:41552d038a69 140
Zaitsev 10:41552d038a69 141 // Configure GPIO
Zaitsev 10:41552d038a69 142 GPIO_InitTypeDef GPIO_InitStructure;
Zaitsev 10:41552d038a69 143 GPIO_InitStructure.Pin = (uint32_t)(1 << pin_index);
Zaitsev 10:41552d038a69 144 GPIO_InitStructure.Mode = gpio_mode[mode];
Zaitsev 10:41552d038a69 145 GPIO_InitStructure.Pull = pupd;
Zaitsev 10:41552d038a69 146 GPIO_InitStructure.Speed = GPIO_SPEED_HIGH;
Zaitsev 10:41552d038a69 147 HAL_GPIO_Init(gpio, &GPIO_InitStructure);
Zaitsev 10:41552d038a69 148
Zaitsev 10:41552d038a69 149 // Disconnect JTAG-DP + SW-DP signals.
Zaitsev 10:41552d038a69 150 // Warning: Need to reconnect under reset
Zaitsev 10:41552d038a69 151 if ((pin == PA_13) || (pin == PA_14)) {
Zaitsev 10:41552d038a69 152 __HAL_AFIO_REMAP_SWJ_DISABLE(); // JTAG-DP Disabled and SW-DP Disabled
Zaitsev 10:41552d038a69 153 }
Zaitsev 10:41552d038a69 154 if ((pin == PA_15) || (pin == PB_3) || (pin == PB_4)) {
Zaitsev 10:41552d038a69 155 __HAL_AFIO_REMAP_SWJ_NOJTAG(); // JTAG-DP Disabled and SW-DP enabled
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
Zaitsev 10:41552d038a69 166 uint32_t port_index = STM_PORT(pin);
Zaitsev 10:41552d038a69 167 uint32_t pin_index = STM_PIN(pin);
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 __IO uint32_t* gpio_reg_hl;//gpio register depends on bit index (high or low)
Zaitsev 10:41552d038a69 172 uint32_t shift;
Zaitsev 10:41552d038a69 173
Zaitsev 10:41552d038a69 174 if (pin_index < 8) {
Zaitsev 10:41552d038a69 175 shift = (pin_index * 4);
Zaitsev 10:41552d038a69 176 gpio_reg_hl = &(gpio->CRL);
Zaitsev 10:41552d038a69 177 } else {
Zaitsev 10:41552d038a69 178 shift = (pin_index % 8) * 4;
Zaitsev 10:41552d038a69 179 gpio_reg_hl = &(gpio->CRH);
Zaitsev 10:41552d038a69 180 }
Zaitsev 10:41552d038a69 181
Zaitsev 10:41552d038a69 182 // Configure open-drain and pull-up/down
Zaitsev 10:41552d038a69 183 switch (mode) {
Zaitsev 10:41552d038a69 184 case PullNone:
Zaitsev 10:41552d038a69 185 break;
Zaitsev 10:41552d038a69 186 case PullUp:
Zaitsev 10:41552d038a69 187 case PullDown:
Zaitsev 10:41552d038a69 188 // Set pull-up / pull-down for Input mode
Zaitsev 10:41552d038a69 189 if ((*gpio_reg_hl & (0x03 << shift)) == 0) { // MODE bits = Input mode
Zaitsev 10:41552d038a69 190 *gpio_reg_hl |= (0x08 << shift); // Set pull-up / pull-down
Zaitsev 10:41552d038a69 191 *gpio_reg_hl &= ~(0x04 << shift); // ENSURES GPIOx_CRL.CNFx.bit0 = 0
Zaitsev 10:41552d038a69 192 }
Zaitsev 10:41552d038a69 193 // Now it's time to setup properly if pullup or pulldown. This is done in ODR register:
Zaitsev 10:41552d038a69 194 // set pull-up => bit=1, set pull-down => bit = 0
Zaitsev 10:41552d038a69 195 if (mode == PullUp) {
Zaitsev 10:41552d038a69 196 gpio->ODR |= (0x01 << (pin_index)); // Set pull-up
Zaitsev 10:41552d038a69 197 } else {
Zaitsev 10:41552d038a69 198 gpio->ODR &= ~(0x01 << (pin_index)); // Set pull-down
Zaitsev 10:41552d038a69 199 }
Zaitsev 10:41552d038a69 200 break;
Zaitsev 10:41552d038a69 201 case OpenDrain:
Zaitsev 10:41552d038a69 202 // Set open-drain for Output mode (General Purpose or Alternate Function)
Zaitsev 10:41552d038a69 203 if ((*gpio_reg_hl & (0x03 << shift)) > 0) { // MODE bits = Output mode
Zaitsev 10:41552d038a69 204 *gpio_reg_hl |= (0x04 << shift); // Set open-drain
Zaitsev 10:41552d038a69 205 }
Zaitsev 10:41552d038a69 206 break;
Zaitsev 10:41552d038a69 207 default:
Zaitsev 10:41552d038a69 208 break;
Zaitsev 10:41552d038a69 209 }
Zaitsev 10:41552d038a69 210 }
Zaitsev 10:41552d038a69 211
Zaitsev 10:41552d038a69 212 /* Internal function for setting the gpiomode/function
Zaitsev 10:41552d038a69 213 * without changing Pull mode
Zaitsev 10:41552d038a69 214 */
Zaitsev 10:41552d038a69 215 void pin_function_gpiomode(PinName pin, uint32_t gpiomode)
Zaitsev 10:41552d038a69 216 {
Zaitsev 10:41552d038a69 217
Zaitsev 10:41552d038a69 218 /* Read current pull state from HW to avoid over-write*/
Zaitsev 10:41552d038a69 219 uint32_t port_index = STM_PORT(pin);
Zaitsev 10:41552d038a69 220 uint32_t pin_index = STM_PIN(pin);
Zaitsev 10:41552d038a69 221 GPIO_TypeDef *gpio = (GPIO_TypeDef *) Set_GPIO_Clock(port_index);
Zaitsev 10:41552d038a69 222 uint32_t pull = PullNone;
Zaitsev 10:41552d038a69 223 __IO uint32_t* gpio_reg_hl;//gpio register depends on bit index (high or low)
Zaitsev 10:41552d038a69 224 uint32_t shift;
Zaitsev 10:41552d038a69 225
Zaitsev 10:41552d038a69 226 if (pin_index < 8) {
Zaitsev 10:41552d038a69 227 shift = (pin_index * 4);
Zaitsev 10:41552d038a69 228 gpio_reg_hl = &(gpio->CRL);
Zaitsev 10:41552d038a69 229 } else {
Zaitsev 10:41552d038a69 230 shift = (pin_index % 8) * 4;
Zaitsev 10:41552d038a69 231 gpio_reg_hl = &(gpio->CRH);
Zaitsev 10:41552d038a69 232 }
Zaitsev 10:41552d038a69 233
Zaitsev 10:41552d038a69 234 /* Check if pull/pull down is active */
Zaitsev 10:41552d038a69 235 if (!(*gpio_reg_hl & (0x03 << shift))) {// input
Zaitsev 10:41552d038a69 236 if((!!(*gpio_reg_hl & (0x08 << shift))) // pull-up / down
Zaitsev 10:41552d038a69 237 && (!(*gpio_reg_hl & (0x04 << shift)))) { // GPIOx_CRL.CNFx.bit0 = 0
Zaitsev 10:41552d038a69 238 if (!!(gpio->ODR & (0x01 << pin_index))) {
Zaitsev 10:41552d038a69 239 pull = PullUp;
Zaitsev 10:41552d038a69 240 } else {
Zaitsev 10:41552d038a69 241 pull = PullDown;
Zaitsev 10:41552d038a69 242 }
Zaitsev 10:41552d038a69 243 }
Zaitsev 10:41552d038a69 244 } else { //output
Zaitsev 10:41552d038a69 245 if (!!(*gpio_reg_hl & (0x04 << shift))) { //open drain
Zaitsev 10:41552d038a69 246 pull = OpenDrain;
Zaitsev 10:41552d038a69 247 }
Zaitsev 10:41552d038a69 248 }
Zaitsev 10:41552d038a69 249
Zaitsev 10:41552d038a69 250 /* Then re-use global function for updating the mode part*/
Zaitsev 10:41552d038a69 251 pin_function(pin, STM_PIN_DATA(gpiomode, pull, 0));
Zaitsev 10:41552d038a69 252 }