1 /* mbed Microcontroller Library
2 *******************************************************************************
3 * Copyright (c) 2014, STMicroelectronics
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 * 3. Neither the name of STMicroelectronics nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *******************************************************************************
30 #include "mbed_assert.h"
32 #include "PortNames.h"
33 #include "mbed_error.h"
35 // Enable GPIO clock and return GPIO base address
36 uint32_t Set_GPIO_Clock(uint32_t port_idx) {
40 gpio_add = GPIOA_BASE;
41 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
44 gpio_add = GPIOB_BASE;
45 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
48 gpio_add = GPIOC_BASE;
49 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);
52 gpio_add = GPIOD_BASE;
53 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOD, ENABLE);
56 gpio_add = GPIOE_BASE;
57 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOE, ENABLE);
60 gpio_add = GPIOF_BASE;
61 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOF, ENABLE);
65 error("Port number is not correct.");
72 * Configure pin (mode, speed, output type and pull-up/pull-down)
74 void pin_function(PinName pin, int data) {
75 MBED_ASSERT(pin != (PinName)NC);
77 // Get the pin informations
78 uint32_t mode = STM_PIN_MODE(data);
79 uint32_t otype = STM_PIN_OTYPE(data);
80 uint32_t pupd = STM_PIN_PUPD(data);
81 uint32_t afnum = STM_PIN_AFNUM(data);
83 uint32_t port_index = STM_PORT(pin);
84 uint32_t pin_index = STM_PIN(pin);
87 uint32_t gpio_add = Set_GPIO_Clock(port_index);
88 GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add;
90 // Configure Alternate Function
91 // Warning: Must be done before the GPIO is initialized
93 GPIO_PinAFConfig(gpio, (uint16_t)pin_index, afnum);
97 GPIO_InitTypeDef GPIO_InitStructure;
98 GPIO_InitStructure.GPIO_Pin = (uint16_t)(1 << pin_index);
99 GPIO_InitStructure.GPIO_Mode = (GPIOMode_TypeDef)mode;
100 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_3;
101 GPIO_InitStructure.GPIO_OType = (GPIOOType_TypeDef)otype;
102 GPIO_InitStructure.GPIO_PuPd = (GPIOPuPd_TypeDef)pupd;
103 GPIO_Init(gpio, &GPIO_InitStructure);
105 // [TODO] Disconnect JTAG-DP + SW-DP signals.
106 // Warning: Need to reconnect under reset
107 //if ((pin == PA_13) || (pin == PA_14)) {
110 //if ((pin == PA_15) || (pin == PB_3) || (pin == PB_4)) {
116 * Configure pin pull-up/pull-down
118 void pin_mode(PinName pin, PinMode mode) {
119 MBED_ASSERT(pin != (PinName)NC);
121 uint32_t port_index = STM_PORT(pin);
122 uint32_t pin_index = STM_PIN(pin);
125 uint32_t gpio_add = Set_GPIO_Clock(port_index);
126 GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add;
128 // Configure pull-up/pull-down resistors
129 uint32_t pupd = (uint32_t)mode;
131 pupd = 0; // Open-drain = No pull-up/No pull-down
132 gpio->PUPDR &= (uint32_t)(~(GPIO_PUPDR_PUPDR0 << (pin_index * 2)));
133 gpio->PUPDR |= (uint32_t)(pupd << (pin_index * 2));