lol

Dependencies:   MMA8451Q

Fork of Application by Mateusz Kowalik

Committer:
danix
Date:
Sun Jan 21 22:28:30 2018 +0000
Revision:
12:3a30cdffa27c
Parent:
10:41552d038a69
Working acelerometer and mouse

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) 2016, 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 static const uint32_t gpio_mode[13] = {
Zaitsev 10:41552d038a69 37 0x00000000, // 0 = GPIO_MODE_INPUT
Zaitsev 10:41552d038a69 38 0x00000001, // 1 = GPIO_MODE_OUTPUT_PP
Zaitsev 10:41552d038a69 39 0x00000011, // 2 = GPIO_MODE_OUTPUT_OD
Zaitsev 10:41552d038a69 40 0x00000002, // 3 = GPIO_MODE_AF_PP
Zaitsev 10:41552d038a69 41 0x00000012, // 4 = GPIO_MODE_AF_OD
Zaitsev 10:41552d038a69 42 0x00000003, // 5 = GPIO_MODE_ANALOG
Zaitsev 10:41552d038a69 43 0x10110000, // 6 = GPIO_MODE_IT_RISING
Zaitsev 10:41552d038a69 44 0x10210000, // 7 = GPIO_MODE_IT_FALLING
Zaitsev 10:41552d038a69 45 0x10310000, // 8 = GPIO_MODE_IT_RISING_FALLING
Zaitsev 10:41552d038a69 46 0x10120000, // 9 = GPIO_MODE_EVT_RISING
Zaitsev 10:41552d038a69 47 0x10220000, // 10 = GPIO_MODE_EVT_FALLING
Zaitsev 10:41552d038a69 48 0x10320000, // 11 = GPIO_MODE_EVT_RISING_FALLING
Zaitsev 10:41552d038a69 49 0x10000000 // 12 = Reset GPIO_MODE_IT_EVT
Zaitsev 10:41552d038a69 50 };
Zaitsev 10:41552d038a69 51
Zaitsev 10:41552d038a69 52 // Enable GPIO clock and return GPIO base address
Zaitsev 10:41552d038a69 53 uint32_t Set_GPIO_Clock(uint32_t port_idx)
Zaitsev 10:41552d038a69 54 {
Zaitsev 10:41552d038a69 55 uint32_t gpio_add = 0;
Zaitsev 10:41552d038a69 56 switch (port_idx) {
Zaitsev 10:41552d038a69 57 case PortA:
Zaitsev 10:41552d038a69 58 gpio_add = GPIOA_BASE;
Zaitsev 10:41552d038a69 59 __GPIOA_CLK_ENABLE();
Zaitsev 10:41552d038a69 60 break;
Zaitsev 10:41552d038a69 61 case PortB:
Zaitsev 10:41552d038a69 62 gpio_add = GPIOB_BASE;
Zaitsev 10:41552d038a69 63 __GPIOB_CLK_ENABLE();
Zaitsev 10:41552d038a69 64 break;
Zaitsev 10:41552d038a69 65 case PortC:
Zaitsev 10:41552d038a69 66 gpio_add = GPIOC_BASE;
Zaitsev 10:41552d038a69 67 __GPIOC_CLK_ENABLE();
Zaitsev 10:41552d038a69 68 break;
Zaitsev 10:41552d038a69 69 #if defined GPIOD_BASE
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 #endif
Zaitsev 10:41552d038a69 75 #if defined GPIOE_BASE
Zaitsev 10:41552d038a69 76 case PortE:
Zaitsev 10:41552d038a69 77 gpio_add = GPIOE_BASE;
Zaitsev 10:41552d038a69 78 __GPIOE_CLK_ENABLE();
Zaitsev 10:41552d038a69 79 break;
Zaitsev 10:41552d038a69 80 #endif
Zaitsev 10:41552d038a69 81 #if defined GPIOF_BASE
Zaitsev 10:41552d038a69 82 case PortF:
Zaitsev 10:41552d038a69 83 gpio_add = GPIOF_BASE;
Zaitsev 10:41552d038a69 84 __GPIOF_CLK_ENABLE();
Zaitsev 10:41552d038a69 85 break;
Zaitsev 10:41552d038a69 86 #endif
Zaitsev 10:41552d038a69 87 #if defined GPIOG_BASE
Zaitsev 10:41552d038a69 88 case PortG:
Zaitsev 10:41552d038a69 89 gpio_add = GPIOG_BASE;
Zaitsev 10:41552d038a69 90 __GPIOG_CLK_ENABLE();
Zaitsev 10:41552d038a69 91 break;
Zaitsev 10:41552d038a69 92 #endif
Zaitsev 10:41552d038a69 93 #if defined GPIOH_BASE
Zaitsev 10:41552d038a69 94 case PortH:
Zaitsev 10:41552d038a69 95 gpio_add = GPIOH_BASE;
Zaitsev 10:41552d038a69 96 __GPIOH_CLK_ENABLE();
Zaitsev 10:41552d038a69 97 break;
Zaitsev 10:41552d038a69 98 #endif
Zaitsev 10:41552d038a69 99 #if defined GPIOI_BASE
Zaitsev 10:41552d038a69 100 case PortI:
Zaitsev 10:41552d038a69 101 gpio_add = GPIOI_BASE;
Zaitsev 10:41552d038a69 102 __GPIOI_CLK_ENABLE();
Zaitsev 10:41552d038a69 103 break;
Zaitsev 10:41552d038a69 104 #endif
Zaitsev 10:41552d038a69 105 #if defined GPIOJ_BASE
Zaitsev 10:41552d038a69 106 case PortJ:
Zaitsev 10:41552d038a69 107 gpio_add = GPIOJ_BASE;
Zaitsev 10:41552d038a69 108 __GPIOJ_CLK_ENABLE();
Zaitsev 10:41552d038a69 109 break;
Zaitsev 10:41552d038a69 110 #endif
Zaitsev 10:41552d038a69 111 #if defined GPIOK_BASE
Zaitsev 10:41552d038a69 112 case PortK:
Zaitsev 10:41552d038a69 113 gpio_add = GPIOK_BASE;
Zaitsev 10:41552d038a69 114 __GPIOK_CLK_ENABLE();
Zaitsev 10:41552d038a69 115 break;
Zaitsev 10:41552d038a69 116 #endif
Zaitsev 10:41552d038a69 117 default:
Zaitsev 10:41552d038a69 118 error("Pinmap error: wrong port number.");
Zaitsev 10:41552d038a69 119 break;
Zaitsev 10:41552d038a69 120 }
Zaitsev 10:41552d038a69 121 return gpio_add;
Zaitsev 10:41552d038a69 122 }
Zaitsev 10:41552d038a69 123
Zaitsev 10:41552d038a69 124 /**
Zaitsev 10:41552d038a69 125 * Configure pin (mode, speed, output type and pull-up/pull-down)
Zaitsev 10:41552d038a69 126 */
Zaitsev 10:41552d038a69 127 void pin_function(PinName pin, int data)
Zaitsev 10:41552d038a69 128 {
Zaitsev 10:41552d038a69 129 MBED_ASSERT(pin != (PinName)NC);
Zaitsev 10:41552d038a69 130 // Get the pin informations
Zaitsev 10:41552d038a69 131 uint32_t mode = STM_PIN_MODE(data);
Zaitsev 10:41552d038a69 132 uint32_t pupd = STM_PIN_PUPD(data);
Zaitsev 10:41552d038a69 133 uint32_t afnum = STM_PIN_AFNUM(data);
Zaitsev 10:41552d038a69 134
Zaitsev 10:41552d038a69 135 uint32_t port_index = STM_PORT(pin);
Zaitsev 10:41552d038a69 136 uint32_t pin_index = STM_PIN(pin);
Zaitsev 10:41552d038a69 137
Zaitsev 10:41552d038a69 138 // Enable GPIO clock
Zaitsev 10:41552d038a69 139 uint32_t gpio_add = Set_GPIO_Clock(port_index);
Zaitsev 10:41552d038a69 140 GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add;
Zaitsev 10:41552d038a69 141
Zaitsev 10:41552d038a69 142 // Configure GPIO
Zaitsev 10:41552d038a69 143 GPIO_InitTypeDef GPIO_InitStructure;
Zaitsev 10:41552d038a69 144 GPIO_InitStructure.Pin = (uint32_t)(1 << pin_index);
Zaitsev 10:41552d038a69 145 GPIO_InitStructure.Mode = gpio_mode[mode];
Zaitsev 10:41552d038a69 146 GPIO_InitStructure.Pull = pupd;
Zaitsev 10:41552d038a69 147 GPIO_InitStructure.Speed = GPIO_SPEED_HIGH;
Zaitsev 10:41552d038a69 148 GPIO_InitStructure.Alternate = afnum;
Zaitsev 10:41552d038a69 149 HAL_GPIO_Init(gpio, &GPIO_InitStructure);
Zaitsev 10:41552d038a69 150
Zaitsev 10:41552d038a69 151 // [TODO] Disconnect JTAG-DP + SW-DP signals.
Zaitsev 10:41552d038a69 152 // Warning: Need to reconnect under reset
Zaitsev 10:41552d038a69 153 //if ((pin == PA_13) || (pin == PA_14)) {
Zaitsev 10:41552d038a69 154 //
Zaitsev 10:41552d038a69 155 //}
Zaitsev 10:41552d038a69 156 //if ((pin == PA_15) || (pin == PB_3) || (pin == PB_4)) {
Zaitsev 10:41552d038a69 157 //
Zaitsev 10:41552d038a69 158 //}
Zaitsev 10:41552d038a69 159 }
Zaitsev 10:41552d038a69 160
Zaitsev 10:41552d038a69 161 /**
Zaitsev 10:41552d038a69 162 * Configure pin pull-up/pull-down
Zaitsev 10:41552d038a69 163 */
Zaitsev 10:41552d038a69 164 void pin_mode(PinName pin, PinMode mode)
Zaitsev 10:41552d038a69 165 {
Zaitsev 10:41552d038a69 166 MBED_ASSERT(pin != (PinName)NC);
Zaitsev 10:41552d038a69 167 uint32_t port_index = STM_PORT(pin);
Zaitsev 10:41552d038a69 168 uint32_t pin_index = STM_PIN(pin);
Zaitsev 10:41552d038a69 169
Zaitsev 10:41552d038a69 170 // Enable GPIO clock
Zaitsev 10:41552d038a69 171 uint32_t gpio_add = Set_GPIO_Clock(port_index);
Zaitsev 10:41552d038a69 172 GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add;
Zaitsev 10:41552d038a69 173
Zaitsev 10:41552d038a69 174 // Configure pull-up/pull-down resistors
Zaitsev 10:41552d038a69 175 uint32_t pupd = (uint32_t)mode;
Zaitsev 10:41552d038a69 176 if (pupd > 2) {
Zaitsev 10:41552d038a69 177 pupd = 0; // Open-drain = No pull-up/No pull-down
Zaitsev 10:41552d038a69 178 }
Zaitsev 10:41552d038a69 179 gpio->PUPDR &= (uint32_t)(~(GPIO_PUPDR_PUPDR0 << (pin_index * 2)));
Zaitsev 10:41552d038a69 180 gpio->PUPDR |= (uint32_t)(pupd << (pin_index * 2));
Zaitsev 10:41552d038a69 181 }
Zaitsev 10:41552d038a69 182
Zaitsev 10:41552d038a69 183 /* Internal function for setting the gpiomode/function
Zaitsev 10:41552d038a69 184 * without changing Pull mode
Zaitsev 10:41552d038a69 185 */
Zaitsev 10:41552d038a69 186 void pin_function_gpiomode(PinName pin, uint32_t gpiomode) {
Zaitsev 10:41552d038a69 187
Zaitsev 10:41552d038a69 188 /* Read current pull state from HW to avoid over-write*/
Zaitsev 10:41552d038a69 189 uint32_t port_index = STM_PORT(pin);
Zaitsev 10:41552d038a69 190 uint32_t pin_index = STM_PIN(pin);
Zaitsev 10:41552d038a69 191 GPIO_TypeDef *gpio = (GPIO_TypeDef *) Set_GPIO_Clock(port_index);
Zaitsev 10:41552d038a69 192 uint32_t temp = gpio->PUPDR;
Zaitsev 10:41552d038a69 193 uint32_t pull = (temp >> (pin_index * 2U)) & GPIO_PUPDR_PUPDR0;
Zaitsev 10:41552d038a69 194
Zaitsev 10:41552d038a69 195 /* Then re-use global function for updating the mode part*/
Zaitsev 10:41552d038a69 196 pin_function(pin, STM_PIN_DATA(gpiomode, pull, 0));
Zaitsev 10:41552d038a69 197 }