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