TUKS MCU Introductory course / TUKS-COURSE-TIMER
Committer:
elmot
Date:
Fri Feb 24 21:13:56 2017 +0000
Revision:
1:d0dfbce63a89
Ready-to-copy

Who changed what in which revision?

UserRevisionLine numberNew contents of line
elmot 1:d0dfbce63a89 1 /* mbed Microcontroller Library
elmot 1:d0dfbce63a89 2 *******************************************************************************
elmot 1:d0dfbce63a89 3 * Copyright (c) 2015, STMicroelectronics
elmot 1:d0dfbce63a89 4 * All rights reserved.
elmot 1:d0dfbce63a89 5 *
elmot 1:d0dfbce63a89 6 * Redistribution and use in source and binary forms, with or without
elmot 1:d0dfbce63a89 7 * modification, are permitted provided that the following conditions are met:
elmot 1:d0dfbce63a89 8 *
elmot 1:d0dfbce63a89 9 * 1. Redistributions of source code must retain the above copyright notice,
elmot 1:d0dfbce63a89 10 * this list of conditions and the following disclaimer.
elmot 1:d0dfbce63a89 11 * 2. Redistributions in binary form must reproduce the above copyright notice,
elmot 1:d0dfbce63a89 12 * this list of conditions and the following disclaimer in the documentation
elmot 1:d0dfbce63a89 13 * and/or other materials provided with the distribution.
elmot 1:d0dfbce63a89 14 * 3. Neither the name of STMicroelectronics nor the names of its contributors
elmot 1:d0dfbce63a89 15 * may be used to endorse or promote products derived from this software
elmot 1:d0dfbce63a89 16 * without specific prior written permission.
elmot 1:d0dfbce63a89 17 *
elmot 1:d0dfbce63a89 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
elmot 1:d0dfbce63a89 19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
elmot 1:d0dfbce63a89 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
elmot 1:d0dfbce63a89 21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
elmot 1:d0dfbce63a89 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
elmot 1:d0dfbce63a89 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
elmot 1:d0dfbce63a89 24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
elmot 1:d0dfbce63a89 25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
elmot 1:d0dfbce63a89 26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
elmot 1:d0dfbce63a89 27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
elmot 1:d0dfbce63a89 28 *******************************************************************************
elmot 1:d0dfbce63a89 29 */
elmot 1:d0dfbce63a89 30 #include "mbed_assert.h"
elmot 1:d0dfbce63a89 31 #include "gpio_api.h"
elmot 1:d0dfbce63a89 32 #include "pinmap.h"
elmot 1:d0dfbce63a89 33 #include "mbed_error.h"
elmot 1:d0dfbce63a89 34
elmot 1:d0dfbce63a89 35 extern uint32_t Set_GPIO_Clock(uint32_t port_idx);
elmot 1:d0dfbce63a89 36
elmot 1:d0dfbce63a89 37 uint32_t gpio_set(PinName pin) {
elmot 1:d0dfbce63a89 38 MBED_ASSERT(pin != (PinName)NC);
elmot 1:d0dfbce63a89 39
elmot 1:d0dfbce63a89 40 pin_function(pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
elmot 1:d0dfbce63a89 41
elmot 1:d0dfbce63a89 42 return (uint32_t)(1 << ((uint32_t)pin & 0xF)); // Return the pin mask
elmot 1:d0dfbce63a89 43 }
elmot 1:d0dfbce63a89 44
elmot 1:d0dfbce63a89 45 void gpio_init(gpio_t *obj, PinName pin) {
elmot 1:d0dfbce63a89 46 obj->pin = pin;
elmot 1:d0dfbce63a89 47 if (pin == (PinName)NC) {
elmot 1:d0dfbce63a89 48 return;
elmot 1:d0dfbce63a89 49 }
elmot 1:d0dfbce63a89 50
elmot 1:d0dfbce63a89 51 uint32_t port_index = STM_PORT(pin);
elmot 1:d0dfbce63a89 52
elmot 1:d0dfbce63a89 53 // Enable GPIO clock
elmot 1:d0dfbce63a89 54 uint32_t gpio_add = Set_GPIO_Clock(port_index);
elmot 1:d0dfbce63a89 55 GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add;
elmot 1:d0dfbce63a89 56
elmot 1:d0dfbce63a89 57 // Fill GPIO object structure for future use
elmot 1:d0dfbce63a89 58 obj->mask = gpio_set(pin);
elmot 1:d0dfbce63a89 59 obj->reg_in = &gpio->IDR;
elmot 1:d0dfbce63a89 60 obj->reg_set = &gpio->BSRR;
elmot 1:d0dfbce63a89 61 #ifdef GPIO_IP_WITHOUT_BRR
elmot 1:d0dfbce63a89 62 obj->reg_clr = &gpio->BSRR;
elmot 1:d0dfbce63a89 63 #else
elmot 1:d0dfbce63a89 64 obj->reg_clr = &gpio->BRR;
elmot 1:d0dfbce63a89 65 #endif
elmot 1:d0dfbce63a89 66 }
elmot 1:d0dfbce63a89 67
elmot 1:d0dfbce63a89 68 void gpio_mode(gpio_t *obj, PinMode mode) {
elmot 1:d0dfbce63a89 69 pin_mode(obj->pin, mode);
elmot 1:d0dfbce63a89 70 }
elmot 1:d0dfbce63a89 71
elmot 1:d0dfbce63a89 72 void gpio_dir(gpio_t *obj, PinDirection direction) {
elmot 1:d0dfbce63a89 73 MBED_ASSERT(obj->pin != (PinName)NC);
elmot 1:d0dfbce63a89 74 if (direction == PIN_OUTPUT) {
elmot 1:d0dfbce63a89 75 pin_function(obj->pin, STM_PIN_DATA(STM_MODE_OUTPUT_PP, GPIO_NOPULL, 0));
elmot 1:d0dfbce63a89 76 } else { // PIN_INPUT
elmot 1:d0dfbce63a89 77 pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
elmot 1:d0dfbce63a89 78 }
elmot 1:d0dfbce63a89 79 }