]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/mbed/targets/hal/TARGET_STM/TARGET_STM32F3XX/pinmap.c
Merge commit '5a0132f1c1c9a14fd2941f0a5e29bbf5e31da20c' into master-core-pull
[max/tmk_keyboard.git] / tmk_core / tool / mbed / mbed-sdk / libraries / mbed / targets / hal / TARGET_STM / TARGET_STM32F3XX / pinmap.c
1 /* mbed Microcontroller Library
2  *******************************************************************************
3  * Copyright (c) 2014, STMicroelectronics
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
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.
17  *
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  *******************************************************************************
29  */
30 #include "mbed_assert.h"
31 #include "pinmap.h"
32 #include "PortNames.h"
33 #include "mbed_error.h"
34
35 // Enable GPIO clock and return GPIO base address
36 uint32_t Set_GPIO_Clock(uint32_t port_idx) {
37     uint32_t gpio_add;
38     switch (port_idx) {
39         case PortA:
40             gpio_add = GPIOA_BASE;
41             RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
42             break;
43         case PortB:
44             gpio_add = GPIOB_BASE;
45             RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
46             break;
47         case PortC:
48             gpio_add = GPIOC_BASE;
49             RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);
50             break;
51         case PortD:
52             gpio_add = GPIOD_BASE;
53             RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOD, ENABLE);
54             break;
55         case PortE:
56             gpio_add = GPIOE_BASE;
57             RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOE, ENABLE);
58             break;
59         case PortF:
60             gpio_add = GPIOF_BASE;
61             RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOF, ENABLE);
62             break;
63         default:
64             gpio_add = 0;
65             error("Port number is not correct.");
66             break;
67     }
68     return gpio_add;
69 }
70
71 /**
72  * Configure pin (mode, speed, output type and pull-up/pull-down)
73  */
74 void pin_function(PinName pin, int data) {
75     MBED_ASSERT(pin != (PinName)NC);
76
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);
82
83     uint32_t port_index = STM_PORT(pin);
84     uint32_t pin_index  = STM_PIN(pin);
85
86     // Enable GPIO clock
87     uint32_t gpio_add = Set_GPIO_Clock(port_index);
88     GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add;
89
90     // Configure Alternate Function
91     // Warning: Must be done before the GPIO is initialized
92     if (afnum != 0xFF) {
93         GPIO_PinAFConfig(gpio, (uint16_t)pin_index, afnum);
94     }
95
96     // Configure GPIO
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);
104
105     // [TODO] Disconnect JTAG-DP + SW-DP signals.
106     // Warning: Need to reconnect under reset
107     //if ((pin == PA_13) || (pin == PA_14)) {
108     //
109     //}
110     //if ((pin == PA_15) || (pin == PB_3) || (pin == PB_4)) {
111     //
112     //}
113 }
114
115 /**
116  * Configure pin pull-up/pull-down
117  */
118 void pin_mode(PinName pin, PinMode mode) {
119     MBED_ASSERT(pin != (PinName)NC);
120
121     uint32_t port_index = STM_PORT(pin);
122     uint32_t pin_index  = STM_PIN(pin);
123
124     // Enable GPIO clock
125     uint32_t gpio_add = Set_GPIO_Clock(port_index);
126     GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add;
127
128     // Configure pull-up/pull-down resistors
129     uint32_t pupd = (uint32_t)mode;
130     if (pupd > 2)
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));
134
135 }