t
Fork of mbed-dev by
targets/TARGET_Silicon_Labs/TARGET_EFM32/port_api.c@178:c26431f84b0d, 2017-11-09 (annotated)
- Committer:
- amithy
- Date:
- Thu Nov 09 22:14:37 2017 +0000
- Revision:
- 178:c26431f84b0d
- Parent:
- 149:156823d33999
test export
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
<> | 144:ef7eb2e8f9f7 | 1 | /***************************************************************************//** |
<> | 144:ef7eb2e8f9f7 | 2 | * @file port_api.c |
<> | 144:ef7eb2e8f9f7 | 3 | ******************************************************************************* |
<> | 144:ef7eb2e8f9f7 | 4 | * @section License |
<> | 144:ef7eb2e8f9f7 | 5 | * <b>(C) Copyright 2015 Silicon Labs, http://www.silabs.com</b> |
<> | 144:ef7eb2e8f9f7 | 6 | ******************************************************************************* |
<> | 144:ef7eb2e8f9f7 | 7 | * |
<> | 144:ef7eb2e8f9f7 | 8 | * SPDX-License-Identifier: Apache-2.0 |
<> | 144:ef7eb2e8f9f7 | 9 | * |
<> | 144:ef7eb2e8f9f7 | 10 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
<> | 144:ef7eb2e8f9f7 | 11 | * not use this file except in compliance with the License. |
<> | 144:ef7eb2e8f9f7 | 12 | * You may obtain a copy of the License at |
<> | 144:ef7eb2e8f9f7 | 13 | * |
<> | 144:ef7eb2e8f9f7 | 14 | * http://www.apache.org/licenses/LICENSE-2.0 |
<> | 144:ef7eb2e8f9f7 | 15 | * |
<> | 144:ef7eb2e8f9f7 | 16 | * Unless required by applicable law or agreed to in writing, software |
<> | 144:ef7eb2e8f9f7 | 17 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
<> | 144:ef7eb2e8f9f7 | 18 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
<> | 144:ef7eb2e8f9f7 | 19 | * See the License for the specific language governing permissions and |
<> | 144:ef7eb2e8f9f7 | 20 | * limitations under the License. |
<> | 144:ef7eb2e8f9f7 | 21 | * |
<> | 144:ef7eb2e8f9f7 | 22 | ******************************************************************************/ |
<> | 144:ef7eb2e8f9f7 | 23 | |
<> | 144:ef7eb2e8f9f7 | 24 | #include "device.h" |
<> | 144:ef7eb2e8f9f7 | 25 | #if DEVICE_PORTOUT |
<> | 144:ef7eb2e8f9f7 | 26 | |
<> | 144:ef7eb2e8f9f7 | 27 | #include "port_api.h" |
<> | 144:ef7eb2e8f9f7 | 28 | #include "pinmap.h" |
<> | 144:ef7eb2e8f9f7 | 29 | #include "gpio_api.h" |
<> | 144:ef7eb2e8f9f7 | 30 | #include "em_gpio.h" |
<> | 144:ef7eb2e8f9f7 | 31 | #include "em_cmu.h" |
<> | 144:ef7eb2e8f9f7 | 32 | |
<> | 144:ef7eb2e8f9f7 | 33 | #define PORT_NUM_PINS 16 |
<> | 144:ef7eb2e8f9f7 | 34 | |
<> | 144:ef7eb2e8f9f7 | 35 | PinName port_pin(PortName port, int pin_n) |
<> | 144:ef7eb2e8f9f7 | 36 | { |
<> | 144:ef7eb2e8f9f7 | 37 | return (PinName) (pin_n | port << 4); // Encode pin and port number in one uint32 |
<> | 144:ef7eb2e8f9f7 | 38 | } |
<> | 144:ef7eb2e8f9f7 | 39 | |
<> | 144:ef7eb2e8f9f7 | 40 | void port_init(port_t *obj, PortName port, int mask, PinDirection dir) |
<> | 144:ef7eb2e8f9f7 | 41 | { |
<> | 144:ef7eb2e8f9f7 | 42 | obj->mask = mask; |
<> | 144:ef7eb2e8f9f7 | 43 | obj->port = port; |
<> | 144:ef7eb2e8f9f7 | 44 | obj->dir = dir; |
<> | 144:ef7eb2e8f9f7 | 45 | |
<> | 144:ef7eb2e8f9f7 | 46 | port_dir(obj, obj->dir); |
<> | 144:ef7eb2e8f9f7 | 47 | } |
<> | 144:ef7eb2e8f9f7 | 48 | |
<> | 144:ef7eb2e8f9f7 | 49 | void port_mode(port_t *obj, PinMode mode) |
<> | 144:ef7eb2e8f9f7 | 50 | { |
<> | 144:ef7eb2e8f9f7 | 51 | /* Set mode for pins given by mask */ |
<> | 144:ef7eb2e8f9f7 | 52 | uint32_t pin; |
<> | 144:ef7eb2e8f9f7 | 53 | for (pin = 0; pin < PORT_NUM_PINS; pin++) { |
<> | 144:ef7eb2e8f9f7 | 54 | if (obj->mask & (1 << pin)) { |
<> | 144:ef7eb2e8f9f7 | 55 | pin_mode(port_pin(obj->port, pin), mode); |
<> | 144:ef7eb2e8f9f7 | 56 | } |
<> | 144:ef7eb2e8f9f7 | 57 | } |
<> | 144:ef7eb2e8f9f7 | 58 | } |
<> | 144:ef7eb2e8f9f7 | 59 | |
<> | 144:ef7eb2e8f9f7 | 60 | void port_dir(port_t *obj, PinDirection dir) |
<> | 144:ef7eb2e8f9f7 | 61 | { |
<> | 144:ef7eb2e8f9f7 | 62 | obj->dir = dir; |
<> | 144:ef7eb2e8f9f7 | 63 | |
<> | 144:ef7eb2e8f9f7 | 64 | /* Set default pin mode for pins given by mask */ |
<> | 144:ef7eb2e8f9f7 | 65 | switch (dir) { |
<> | 144:ef7eb2e8f9f7 | 66 | case PIN_INPUT: |
<> | 144:ef7eb2e8f9f7 | 67 | port_mode(obj, Input); |
<> | 144:ef7eb2e8f9f7 | 68 | break; |
<> | 144:ef7eb2e8f9f7 | 69 | case PIN_OUTPUT: |
<> | 144:ef7eb2e8f9f7 | 70 | port_mode(obj, PushPull); |
<> | 144:ef7eb2e8f9f7 | 71 | break; |
<> | 144:ef7eb2e8f9f7 | 72 | } |
<> | 144:ef7eb2e8f9f7 | 73 | } |
<> | 144:ef7eb2e8f9f7 | 74 | |
<> | 144:ef7eb2e8f9f7 | 75 | void port_write(port_t *obj, int value) |
<> | 144:ef7eb2e8f9f7 | 76 | { |
<> | 144:ef7eb2e8f9f7 | 77 | GPIO_PortOutSetVal(obj->port, value, obj->mask); |
<> | 144:ef7eb2e8f9f7 | 78 | } |
<> | 144:ef7eb2e8f9f7 | 79 | |
<> | 144:ef7eb2e8f9f7 | 80 | int port_read(port_t *obj) |
<> | 144:ef7eb2e8f9f7 | 81 | { |
<> | 144:ef7eb2e8f9f7 | 82 | if (obj->dir == PIN_INPUT) { |
<> | 144:ef7eb2e8f9f7 | 83 | return (int) (GPIO_PortInGet(obj->port) & obj->mask); |
<> | 144:ef7eb2e8f9f7 | 84 | } else { |
<> | 144:ef7eb2e8f9f7 | 85 | return (int) (GPIO_PortOutGet(obj->port) & obj->mask); |
<> | 144:ef7eb2e8f9f7 | 86 | } |
<> | 144:ef7eb2e8f9f7 | 87 | } |
<> | 144:ef7eb2e8f9f7 | 88 | |
<> | 144:ef7eb2e8f9f7 | 89 | #endif |