mbed.h library with any bug fixes AV finds.

Dependents:   micromouse4_encoder_testing PID_Test Lab1_Test WorkingPID ... more

Committer:
aravindsv
Date:
Mon Nov 02 03:07:12 2015 +0000
Revision:
1:ebce2ad32f95
Parent:
0:ba7650f404af
Changed the RCC timeout value to 500 ms, so total code startup time before program starts running is ~1s. Hopefully no side-effects from lower startup timeouts

Who changed what in which revision?

UserRevisionLine numberNew contents of line
aravindsv 0:ba7650f404af 1 /* mbed Microcontroller Library
aravindsv 0:ba7650f404af 2 *******************************************************************************
aravindsv 0:ba7650f404af 3 * Copyright (c) 2014, STMicroelectronics
aravindsv 0:ba7650f404af 4 * All rights reserved.
aravindsv 0:ba7650f404af 5 *
aravindsv 0:ba7650f404af 6 * Redistribution and use in source and binary forms, with or without
aravindsv 0:ba7650f404af 7 * modification, are permitted provided that the following conditions are met:
aravindsv 0:ba7650f404af 8 *
aravindsv 0:ba7650f404af 9 * 1. Redistributions of source code must retain the above copyright notice,
aravindsv 0:ba7650f404af 10 * this list of conditions and the following disclaimer.
aravindsv 0:ba7650f404af 11 * 2. Redistributions in binary form must reproduce the above copyright notice,
aravindsv 0:ba7650f404af 12 * this list of conditions and the following disclaimer in the documentation
aravindsv 0:ba7650f404af 13 * and/or other materials provided with the distribution.
aravindsv 0:ba7650f404af 14 * 3. Neither the name of STMicroelectronics nor the names of its contributors
aravindsv 0:ba7650f404af 15 * may be used to endorse or promote products derived from this software
aravindsv 0:ba7650f404af 16 * without specific prior written permission.
aravindsv 0:ba7650f404af 17 *
aravindsv 0:ba7650f404af 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
aravindsv 0:ba7650f404af 19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
aravindsv 0:ba7650f404af 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
aravindsv 0:ba7650f404af 21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
aravindsv 0:ba7650f404af 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
aravindsv 0:ba7650f404af 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
aravindsv 0:ba7650f404af 24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
aravindsv 0:ba7650f404af 25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
aravindsv 0:ba7650f404af 26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
aravindsv 0:ba7650f404af 27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
aravindsv 0:ba7650f404af 28 *******************************************************************************
aravindsv 0:ba7650f404af 29 */
aravindsv 0:ba7650f404af 30 #include "mbed_assert.h"
aravindsv 0:ba7650f404af 31 #include "pinmap.h"
aravindsv 0:ba7650f404af 32 #include "PortNames.h"
aravindsv 0:ba7650f404af 33 #include "mbed_error.h"
aravindsv 0:ba7650f404af 34
aravindsv 0:ba7650f404af 35 // GPIO mode look-up table
aravindsv 0:ba7650f404af 36 // Warning: the elements order must be the same as the one defined in PinNames.h
aravindsv 0:ba7650f404af 37 static const uint32_t gpio_mode[13] = {
aravindsv 0:ba7650f404af 38 GPIO_MODE_INPUT, // 0 = STM_MODE_INPUT
aravindsv 0:ba7650f404af 39 GPIO_MODE_OUTPUT_PP, // 1 = STM_MODE_OUTPUT_PP
aravindsv 0:ba7650f404af 40 GPIO_MODE_OUTPUT_OD, // 2 = STM_MODE_OUTPUT_OD
aravindsv 0:ba7650f404af 41 GPIO_MODE_AF_PP, // 3 = STM_MODE_AF_PP
aravindsv 0:ba7650f404af 42 GPIO_MODE_AF_OD, // 4 = STM_MODE_AF_OD
aravindsv 0:ba7650f404af 43 GPIO_MODE_ANALOG, // 5 = STM_MODE_ANALOG
aravindsv 0:ba7650f404af 44 GPIO_MODE_IT_RISING, // 6 = STM_MODE_IT_RISING
aravindsv 0:ba7650f404af 45 GPIO_MODE_IT_FALLING, // 7 = STM_MODE_IT_FALLING
aravindsv 0:ba7650f404af 46 GPIO_MODE_IT_RISING_FALLING, // 8 = STM_MODE_IT_RISING_FALLING
aravindsv 0:ba7650f404af 47 GPIO_MODE_EVT_RISING, // 9 = STM_MODE_EVT_RISING
aravindsv 0:ba7650f404af 48 GPIO_MODE_EVT_FALLING, // 10 = STM_MODE_EVT_FALLING
aravindsv 0:ba7650f404af 49 GPIO_MODE_EVT_RISING_FALLING, // 11 = STM_MODE_EVT_RISING_FALLING
aravindsv 0:ba7650f404af 50 0x10000000 // 12 = STM_MODE_IT_EVT_RESET (not in STM32Cube HAL)
aravindsv 0:ba7650f404af 51 };
aravindsv 0:ba7650f404af 52
aravindsv 0:ba7650f404af 53 // Enable GPIO clock and return GPIO base address
aravindsv 0:ba7650f404af 54 uint32_t Set_GPIO_Clock(uint32_t port_idx)
aravindsv 0:ba7650f404af 55 {
aravindsv 0:ba7650f404af 56 uint32_t gpio_add = 0;
aravindsv 0:ba7650f404af 57 switch (port_idx) {
aravindsv 0:ba7650f404af 58 case PortA:
aravindsv 0:ba7650f404af 59 gpio_add = GPIOA_BASE;
aravindsv 0:ba7650f404af 60 __GPIOA_CLK_ENABLE();
aravindsv 0:ba7650f404af 61 break;
aravindsv 0:ba7650f404af 62 case PortB:
aravindsv 0:ba7650f404af 63 gpio_add = GPIOB_BASE;
aravindsv 0:ba7650f404af 64 __GPIOB_CLK_ENABLE();
aravindsv 0:ba7650f404af 65 break;
aravindsv 0:ba7650f404af 66 case PortC:
aravindsv 0:ba7650f404af 67 gpio_add = GPIOC_BASE;
aravindsv 0:ba7650f404af 68 __GPIOC_CLK_ENABLE();
aravindsv 0:ba7650f404af 69 break;
aravindsv 0:ba7650f404af 70 case PortD:
aravindsv 0:ba7650f404af 71 gpio_add = GPIOD_BASE;
aravindsv 0:ba7650f404af 72 __GPIOD_CLK_ENABLE();
aravindsv 0:ba7650f404af 73 break;
aravindsv 0:ba7650f404af 74 default:
aravindsv 0:ba7650f404af 75 error("Pinmap error: wrong port number.");
aravindsv 0:ba7650f404af 76 break;
aravindsv 0:ba7650f404af 77 }
aravindsv 0:ba7650f404af 78 return gpio_add;
aravindsv 0:ba7650f404af 79 }
aravindsv 0:ba7650f404af 80
aravindsv 0:ba7650f404af 81 /**
aravindsv 0:ba7650f404af 82 * Configure pin (input, output, alternate function or analog) + output speed + AF
aravindsv 0:ba7650f404af 83 */
aravindsv 0:ba7650f404af 84 void pin_function(PinName pin, int data)
aravindsv 0:ba7650f404af 85 {
aravindsv 0:ba7650f404af 86 MBED_ASSERT(pin != (PinName)NC);
aravindsv 0:ba7650f404af 87 // Get the pin informations
aravindsv 0:ba7650f404af 88 uint32_t mode = STM_PIN_MODE(data);
aravindsv 0:ba7650f404af 89 uint32_t pupd = STM_PIN_PUPD(data);
aravindsv 0:ba7650f404af 90 uint32_t afnum = STM_PIN_AFNUM(data);
aravindsv 0:ba7650f404af 91
aravindsv 0:ba7650f404af 92 uint32_t port_index = STM_PORT(pin);
aravindsv 0:ba7650f404af 93 uint32_t pin_index = STM_PIN(pin);
aravindsv 0:ba7650f404af 94
aravindsv 0:ba7650f404af 95 // Enable GPIO clock
aravindsv 0:ba7650f404af 96 uint32_t gpio_add = Set_GPIO_Clock(port_index);
aravindsv 0:ba7650f404af 97 GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add;
aravindsv 0:ba7650f404af 98
aravindsv 0:ba7650f404af 99 // Enable AFIO clock
aravindsv 0:ba7650f404af 100 __HAL_RCC_AFIO_CLK_ENABLE();
aravindsv 0:ba7650f404af 101
aravindsv 0:ba7650f404af 102 // Configure Alternate Function
aravindsv 0:ba7650f404af 103 // Warning: Must be done before the GPIO is initialized
aravindsv 0:ba7650f404af 104 if (afnum > 0) {
aravindsv 0:ba7650f404af 105 switch (afnum) {
aravindsv 0:ba7650f404af 106 case 1: // Remap SPI1
aravindsv 0:ba7650f404af 107 __HAL_AFIO_REMAP_SPI1_ENABLE();
aravindsv 0:ba7650f404af 108 break;
aravindsv 0:ba7650f404af 109 case 2: // Remap I2C1
aravindsv 0:ba7650f404af 110 __HAL_AFIO_REMAP_I2C1_ENABLE();
aravindsv 0:ba7650f404af 111 break;
aravindsv 0:ba7650f404af 112 case 3: // Remap USART1
aravindsv 0:ba7650f404af 113 __HAL_AFIO_REMAP_USART1_ENABLE();
aravindsv 0:ba7650f404af 114 break;
aravindsv 0:ba7650f404af 115 case 4: // Remap USART2
aravindsv 0:ba7650f404af 116 __HAL_AFIO_REMAP_USART2_ENABLE();
aravindsv 0:ba7650f404af 117 break;
aravindsv 0:ba7650f404af 118 case 5: // Partial Remap USART3
aravindsv 0:ba7650f404af 119 __HAL_AFIO_REMAP_USART3_PARTIAL();
aravindsv 0:ba7650f404af 120 break;
aravindsv 0:ba7650f404af 121 case 6: // Partial Remap TIM1
aravindsv 0:ba7650f404af 122 __HAL_AFIO_REMAP_TIM1_PARTIAL();
aravindsv 0:ba7650f404af 123 break;
aravindsv 0:ba7650f404af 124 case 7: // Partial Remap TIM3
aravindsv 0:ba7650f404af 125 __HAL_AFIO_REMAP_TIM3_PARTIAL();
aravindsv 0:ba7650f404af 126 break;
aravindsv 0:ba7650f404af 127 case 8: // Full Remap TIM2
aravindsv 0:ba7650f404af 128 __HAL_AFIO_REMAP_TIM2_ENABLE();
aravindsv 0:ba7650f404af 129 break;
aravindsv 0:ba7650f404af 130 case 9: // Full Remap TIM3
aravindsv 0:ba7650f404af 131 __HAL_AFIO_REMAP_TIM3_ENABLE();
aravindsv 0:ba7650f404af 132 break;
aravindsv 0:ba7650f404af 133 default:
aravindsv 0:ba7650f404af 134 break;
aravindsv 0:ba7650f404af 135 }
aravindsv 0:ba7650f404af 136 }
aravindsv 0:ba7650f404af 137
aravindsv 0:ba7650f404af 138 // Configure GPIO
aravindsv 0:ba7650f404af 139 GPIO_InitTypeDef GPIO_InitStructure;
aravindsv 0:ba7650f404af 140 GPIO_InitStructure.Pin = (uint32_t)(1 << pin_index);
aravindsv 0:ba7650f404af 141 GPIO_InitStructure.Mode = gpio_mode[mode];
aravindsv 0:ba7650f404af 142 GPIO_InitStructure.Pull = pupd;
aravindsv 0:ba7650f404af 143 GPIO_InitStructure.Speed = GPIO_SPEED_HIGH;
aravindsv 0:ba7650f404af 144 HAL_GPIO_Init(gpio, &GPIO_InitStructure);
aravindsv 0:ba7650f404af 145
aravindsv 0:ba7650f404af 146 // Disconnect JTAG-DP + SW-DP signals.
aravindsv 0:ba7650f404af 147 // Warning: Need to reconnect under reset
aravindsv 0:ba7650f404af 148 if ((pin == PA_13) || (pin == PA_14)) {
aravindsv 0:ba7650f404af 149 __HAL_AFIO_REMAP_SWJ_DISABLE(); // JTAG-DP Disabled and SW-DP Disabled
aravindsv 0:ba7650f404af 150 }
aravindsv 0:ba7650f404af 151 if ((pin == PA_15) || (pin == PB_3) || (pin == PB_4)) {
aravindsv 0:ba7650f404af 152 __HAL_AFIO_REMAP_SWJ_NOJTAG(); // JTAG-DP Disabled and SW-DP enabled
aravindsv 0:ba7650f404af 153 }
aravindsv 0:ba7650f404af 154 }
aravindsv 0:ba7650f404af 155
aravindsv 0:ba7650f404af 156 /**
aravindsv 0:ba7650f404af 157 * Configure pin pull-up/pull-down
aravindsv 0:ba7650f404af 158 */
aravindsv 0:ba7650f404af 159 void pin_mode(PinName pin, PinMode mode)
aravindsv 0:ba7650f404af 160 {
aravindsv 0:ba7650f404af 161 MBED_ASSERT(pin != (PinName)NC);
aravindsv 0:ba7650f404af 162
aravindsv 0:ba7650f404af 163 uint32_t port_index = STM_PORT(pin);
aravindsv 0:ba7650f404af 164 uint32_t pin_index = STM_PIN(pin);
aravindsv 0:ba7650f404af 165
aravindsv 0:ba7650f404af 166 // Enable GPIO clock
aravindsv 0:ba7650f404af 167 uint32_t gpio_add = Set_GPIO_Clock(port_index);
aravindsv 0:ba7650f404af 168 GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add;
aravindsv 0:ba7650f404af 169
aravindsv 0:ba7650f404af 170 // Configure open-drain and pull-up/down
aravindsv 0:ba7650f404af 171 switch (mode) {
aravindsv 0:ba7650f404af 172 case PullNone:
aravindsv 0:ba7650f404af 173 break;
aravindsv 0:ba7650f404af 174 case PullUp:
aravindsv 0:ba7650f404af 175 case PullDown:
aravindsv 0:ba7650f404af 176 // Set pull-up / pull-down for Input mode
aravindsv 0:ba7650f404af 177 if (pin_index < 8) {
aravindsv 0:ba7650f404af 178 if ((gpio->CRL & (0x03 << (pin_index * 4))) == 0) { // MODE bits = Input mode
aravindsv 0:ba7650f404af 179 gpio->CRL |= (0x08 << (pin_index * 4)); // Set pull-up / pull-down
aravindsv 0:ba7650f404af 180 gpio->CRL &= ~(0x08 << ((pin_index * 4)-1)); // ENSURES GPIOx_CRL.CNFx.bit0 = 0
aravindsv 0:ba7650f404af 181 }
aravindsv 0:ba7650f404af 182 } else {
aravindsv 0:ba7650f404af 183 if ((gpio->CRH & (0x03 << ((pin_index % 8) * 4))) == 0) { // MODE bits = Input mode
aravindsv 0:ba7650f404af 184 gpio->CRH |= (0x08 << ((pin_index % 8) * 4)); // Set pull-up / pull-down
aravindsv 0:ba7650f404af 185 gpio->CRH &= ~(0x08 << (((pin_index % 8) * 4)-1)); // ENSURES GPIOx_CRH.CNFx.bit0 = 0
aravindsv 0:ba7650f404af 186 }
aravindsv 0:ba7650f404af 187 }
aravindsv 0:ba7650f404af 188 // Now it's time to setup properly if pullup or pulldown. This is done in ODR register:
aravindsv 0:ba7650f404af 189 // set pull-up => bit=1, set pull-down => bit = 0
aravindsv 0:ba7650f404af 190 if (mode == PullUp) {
aravindsv 0:ba7650f404af 191 gpio->ODR |= (0x01 << (pin_index)); // Set pull-up
aravindsv 0:ba7650f404af 192 } else{
aravindsv 0:ba7650f404af 193 gpio->ODR &= ~(0x01 << (pin_index)); // Set pull-down
aravindsv 0:ba7650f404af 194 }
aravindsv 0:ba7650f404af 195 break;
aravindsv 0:ba7650f404af 196 case OpenDrain:
aravindsv 0:ba7650f404af 197 // Set open-drain for Output mode (General Purpose or Alternate Function)
aravindsv 0:ba7650f404af 198 if (pin_index < 8) {
aravindsv 0:ba7650f404af 199 if ((gpio->CRL & (0x03 << (pin_index * 4))) > 0) { // MODE bits = Output mode
aravindsv 0:ba7650f404af 200 gpio->CRL |= (0x04 << (pin_index * 4)); // Set open-drain
aravindsv 0:ba7650f404af 201 }
aravindsv 0:ba7650f404af 202 } else {
aravindsv 0:ba7650f404af 203 if ((gpio->CRH & (0x03 << ((pin_index % 8) * 4))) > 0) { // MODE bits = Output mode
aravindsv 0:ba7650f404af 204 gpio->CRH |= (0x04 << ((pin_index % 8) * 4)); // Set open-drain
aravindsv 0:ba7650f404af 205 }
aravindsv 0:ba7650f404af 206 }
aravindsv 0:ba7650f404af 207 break;
aravindsv 0:ba7650f404af 208 default:
aravindsv 0:ba7650f404af 209 break;
aravindsv 0:ba7650f404af 210 }
aravindsv 0:ba7650f404af 211 }