inport from local

Dependents:   Hobbyking_Cheetah_0511

Committer:
NYX
Date:
Mon Mar 16 06:35:48 2020 +0000
Revision:
0:85b3fd62ea1a
reinport to mbed;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
NYX 0:85b3fd62ea1a 1 /* mbed Microcontroller Library
NYX 0:85b3fd62ea1a 2 *******************************************************************************
NYX 0:85b3fd62ea1a 3 * Copyright (c) 2016, STMicroelectronics
NYX 0:85b3fd62ea1a 4 * All rights reserved.
NYX 0:85b3fd62ea1a 5 *
NYX 0:85b3fd62ea1a 6 * Redistribution and use in source and binary forms, with or without
NYX 0:85b3fd62ea1a 7 * modification, are permitted provided that the following conditions are met:
NYX 0:85b3fd62ea1a 8 *
NYX 0:85b3fd62ea1a 9 * 1. Redistributions of source code must retain the above copyright notice,
NYX 0:85b3fd62ea1a 10 * this list of conditions and the following disclaimer.
NYX 0:85b3fd62ea1a 11 * 2. Redistributions in binary form must reproduce the above copyright notice,
NYX 0:85b3fd62ea1a 12 * this list of conditions and the following disclaimer in the documentation
NYX 0:85b3fd62ea1a 13 * and/or other materials provided with the distribution.
NYX 0:85b3fd62ea1a 14 * 3. Neither the name of STMicroelectronics nor the names of its contributors
NYX 0:85b3fd62ea1a 15 * may be used to endorse or promote products derived from this software
NYX 0:85b3fd62ea1a 16 * without specific prior written permission.
NYX 0:85b3fd62ea1a 17 *
NYX 0:85b3fd62ea1a 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
NYX 0:85b3fd62ea1a 19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
NYX 0:85b3fd62ea1a 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
NYX 0:85b3fd62ea1a 21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
NYX 0:85b3fd62ea1a 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
NYX 0:85b3fd62ea1a 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
NYX 0:85b3fd62ea1a 24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
NYX 0:85b3fd62ea1a 25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
NYX 0:85b3fd62ea1a 26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
NYX 0:85b3fd62ea1a 27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
NYX 0:85b3fd62ea1a 28 *******************************************************************************
NYX 0:85b3fd62ea1a 29 */
NYX 0:85b3fd62ea1a 30 #include "port_api.h"
NYX 0:85b3fd62ea1a 31 #include "pinmap.h"
NYX 0:85b3fd62ea1a 32 #include "gpio_api.h"
NYX 0:85b3fd62ea1a 33 #include "mbed_error.h"
NYX 0:85b3fd62ea1a 34
NYX 0:85b3fd62ea1a 35 #if DEVICE_PORTIN || DEVICE_PORTOUT
NYX 0:85b3fd62ea1a 36
NYX 0:85b3fd62ea1a 37 extern GPIO_TypeDef *Set_GPIO_Clock(uint32_t port_idx);
NYX 0:85b3fd62ea1a 38
NYX 0:85b3fd62ea1a 39 // high nibble = port number (0=A, 1=B, 2=C, 3=D, 4=E, 5=F, ...)
NYX 0:85b3fd62ea1a 40 // low nibble = pin number
NYX 0:85b3fd62ea1a 41 PinName port_pin(PortName port, int pin_n)
NYX 0:85b3fd62ea1a 42 {
NYX 0:85b3fd62ea1a 43 return (PinName)(pin_n + (port << 4));
NYX 0:85b3fd62ea1a 44 }
NYX 0:85b3fd62ea1a 45
NYX 0:85b3fd62ea1a 46 void port_init(port_t *obj, PortName port, int mask, PinDirection dir)
NYX 0:85b3fd62ea1a 47 {
NYX 0:85b3fd62ea1a 48 uint32_t port_index = (uint32_t)port;
NYX 0:85b3fd62ea1a 49
NYX 0:85b3fd62ea1a 50 // Enable GPIO clock
NYX 0:85b3fd62ea1a 51 GPIO_TypeDef *gpio = Set_GPIO_Clock(port_index);
NYX 0:85b3fd62ea1a 52
NYX 0:85b3fd62ea1a 53 // Fill PORT object structure for future use
NYX 0:85b3fd62ea1a 54 obj->port = port;
NYX 0:85b3fd62ea1a 55 obj->mask = mask;
NYX 0:85b3fd62ea1a 56 obj->direction = dir;
NYX 0:85b3fd62ea1a 57 obj->reg_in = &gpio->IDR;
NYX 0:85b3fd62ea1a 58 obj->reg_out = &gpio->ODR;
NYX 0:85b3fd62ea1a 59
NYX 0:85b3fd62ea1a 60 port_dir(obj, dir);
NYX 0:85b3fd62ea1a 61 }
NYX 0:85b3fd62ea1a 62
NYX 0:85b3fd62ea1a 63 void port_dir(port_t *obj, PinDirection dir)
NYX 0:85b3fd62ea1a 64 {
NYX 0:85b3fd62ea1a 65 uint32_t i;
NYX 0:85b3fd62ea1a 66 obj->direction = dir;
NYX 0:85b3fd62ea1a 67 for (i = 0; i < 16; i++) { // Process all pins
NYX 0:85b3fd62ea1a 68 if (obj->mask & (1 << i)) { // If the pin is used
NYX 0:85b3fd62ea1a 69 if (dir == PIN_OUTPUT) {
NYX 0:85b3fd62ea1a 70 pin_function(port_pin(obj->port, i), STM_PIN_DATA(STM_MODE_OUTPUT_PP, GPIO_NOPULL, 0));
NYX 0:85b3fd62ea1a 71 } else { // PIN_INPUT
NYX 0:85b3fd62ea1a 72 pin_function(port_pin(obj->port, i), STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
NYX 0:85b3fd62ea1a 73 }
NYX 0:85b3fd62ea1a 74 }
NYX 0:85b3fd62ea1a 75 }
NYX 0:85b3fd62ea1a 76 }
NYX 0:85b3fd62ea1a 77
NYX 0:85b3fd62ea1a 78 void port_mode(port_t *obj, PinMode mode)
NYX 0:85b3fd62ea1a 79 {
NYX 0:85b3fd62ea1a 80 uint32_t i;
NYX 0:85b3fd62ea1a 81 for (i = 0; i < 16; i++) { // Process all pins
NYX 0:85b3fd62ea1a 82 if (obj->mask & (1 << i)) { // If the pin is used
NYX 0:85b3fd62ea1a 83 pin_mode(port_pin(obj->port, i), mode);
NYX 0:85b3fd62ea1a 84 }
NYX 0:85b3fd62ea1a 85 }
NYX 0:85b3fd62ea1a 86 }
NYX 0:85b3fd62ea1a 87
NYX 0:85b3fd62ea1a 88 void port_write(port_t *obj, int value)
NYX 0:85b3fd62ea1a 89 {
NYX 0:85b3fd62ea1a 90 *obj->reg_out = (*obj->reg_out & ~obj->mask) | (value & obj->mask);
NYX 0:85b3fd62ea1a 91 }
NYX 0:85b3fd62ea1a 92
NYX 0:85b3fd62ea1a 93 int port_read(port_t *obj)
NYX 0:85b3fd62ea1a 94 {
NYX 0:85b3fd62ea1a 95 if (obj->direction == PIN_OUTPUT) {
NYX 0:85b3fd62ea1a 96 return (*obj->reg_out & obj->mask);
NYX 0:85b3fd62ea1a 97 } else { // PIN_INPUT
NYX 0:85b3fd62ea1a 98 return (*obj->reg_in & obj->mask);
NYX 0:85b3fd62ea1a 99 }
NYX 0:85b3fd62ea1a 100 }
NYX 0:85b3fd62ea1a 101
NYX 0:85b3fd62ea1a 102 #endif