Mouse code for the MacroRat
mbed-dev/targets/TARGET_Atmel/TARGET_SAM_CortexM0P/gpio_api.c@18:6a4db94011d3, 2017-05-14 (annotated)
- Committer:
- sahilmgandhi
- Date:
- Sun May 14 23:18:57 2017 +0000
- Revision:
- 18:6a4db94011d3
Publishing again
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sahilmgandhi | 18:6a4db94011d3 | 1 | /* mbed Microcontroller Library |
sahilmgandhi | 18:6a4db94011d3 | 2 | * Copyright (c) 2006-2015 ARM Limited |
sahilmgandhi | 18:6a4db94011d3 | 3 | * |
sahilmgandhi | 18:6a4db94011d3 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
sahilmgandhi | 18:6a4db94011d3 | 5 | * you may not use this file except in compliance with the License. |
sahilmgandhi | 18:6a4db94011d3 | 6 | * You may obtain a copy of the License at |
sahilmgandhi | 18:6a4db94011d3 | 7 | * |
sahilmgandhi | 18:6a4db94011d3 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
sahilmgandhi | 18:6a4db94011d3 | 9 | * |
sahilmgandhi | 18:6a4db94011d3 | 10 | * Unless required by applicable law or agreed to in writing, software |
sahilmgandhi | 18:6a4db94011d3 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
sahilmgandhi | 18:6a4db94011d3 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
sahilmgandhi | 18:6a4db94011d3 | 13 | * See the License for the specific language governing permissions and |
sahilmgandhi | 18:6a4db94011d3 | 14 | * limitations under the License. |
sahilmgandhi | 18:6a4db94011d3 | 15 | */ |
sahilmgandhi | 18:6a4db94011d3 | 16 | #include "mbed_assert.h" |
sahilmgandhi | 18:6a4db94011d3 | 17 | #include "PinNames.h" |
sahilmgandhi | 18:6a4db94011d3 | 18 | #include "gpio_object.h" |
sahilmgandhi | 18:6a4db94011d3 | 19 | #include "gpio_api.h" |
sahilmgandhi | 18:6a4db94011d3 | 20 | #include "compiler.h" |
sahilmgandhi | 18:6a4db94011d3 | 21 | #include "port.h" |
sahilmgandhi | 18:6a4db94011d3 | 22 | |
sahilmgandhi | 18:6a4db94011d3 | 23 | uint32_t gpio_set(PinName pin) |
sahilmgandhi | 18:6a4db94011d3 | 24 | { |
sahilmgandhi | 18:6a4db94011d3 | 25 | MBED_ASSERT(pin != (PinName)NC); |
sahilmgandhi | 18:6a4db94011d3 | 26 | return (1UL << (pin % 32)); |
sahilmgandhi | 18:6a4db94011d3 | 27 | } |
sahilmgandhi | 18:6a4db94011d3 | 28 | |
sahilmgandhi | 18:6a4db94011d3 | 29 | void gpio_init(gpio_t *obj, PinName pin) |
sahilmgandhi | 18:6a4db94011d3 | 30 | { |
sahilmgandhi | 18:6a4db94011d3 | 31 | MBED_ASSERT(pin != (PinName)NC); |
sahilmgandhi | 18:6a4db94011d3 | 32 | struct port_config pin_conf; |
sahilmgandhi | 18:6a4db94011d3 | 33 | PortGroup *const port_base = (PortGroup*)port_get_group_from_gpio_pin(pin); |
sahilmgandhi | 18:6a4db94011d3 | 34 | |
sahilmgandhi | 18:6a4db94011d3 | 35 | obj->pin = pin; |
sahilmgandhi | 18:6a4db94011d3 | 36 | if (pin == (PinName)NC) |
sahilmgandhi | 18:6a4db94011d3 | 37 | return; |
sahilmgandhi | 18:6a4db94011d3 | 38 | |
sahilmgandhi | 18:6a4db94011d3 | 39 | obj->mask = gpio_set(pin); |
sahilmgandhi | 18:6a4db94011d3 | 40 | port_get_config_defaults(&pin_conf); |
sahilmgandhi | 18:6a4db94011d3 | 41 | obj->powersave = pin_conf.powersave; |
sahilmgandhi | 18:6a4db94011d3 | 42 | obj->direction = PORT_PIN_DIR_INPUT; |
sahilmgandhi | 18:6a4db94011d3 | 43 | obj->mode = PORT_PIN_PULL_UP; |
sahilmgandhi | 18:6a4db94011d3 | 44 | port_pin_set_config(pin, &pin_conf); |
sahilmgandhi | 18:6a4db94011d3 | 45 | |
sahilmgandhi | 18:6a4db94011d3 | 46 | obj->OUTCLR = &port_base->OUTCLR.reg; |
sahilmgandhi | 18:6a4db94011d3 | 47 | obj->OUTSET = &port_base->OUTSET.reg; |
sahilmgandhi | 18:6a4db94011d3 | 48 | obj->IN = &port_base->IN.reg; |
sahilmgandhi | 18:6a4db94011d3 | 49 | obj->OUT = &port_base->OUT.reg; |
sahilmgandhi | 18:6a4db94011d3 | 50 | } |
sahilmgandhi | 18:6a4db94011d3 | 51 | |
sahilmgandhi | 18:6a4db94011d3 | 52 | void gpio_mode(gpio_t *obj, PinMode mode) |
sahilmgandhi | 18:6a4db94011d3 | 53 | { |
sahilmgandhi | 18:6a4db94011d3 | 54 | MBED_ASSERT(obj->pin != (PinName)NC); |
sahilmgandhi | 18:6a4db94011d3 | 55 | struct port_config pin_conf; |
sahilmgandhi | 18:6a4db94011d3 | 56 | |
sahilmgandhi | 18:6a4db94011d3 | 57 | obj->mode = mode; |
sahilmgandhi | 18:6a4db94011d3 | 58 | pin_conf.direction = (enum port_pin_dir)obj->direction; |
sahilmgandhi | 18:6a4db94011d3 | 59 | pin_conf.powersave = obj->powersave; |
sahilmgandhi | 18:6a4db94011d3 | 60 | switch (mode) { |
sahilmgandhi | 18:6a4db94011d3 | 61 | case PullNone : |
sahilmgandhi | 18:6a4db94011d3 | 62 | pin_conf.input_pull = PORT_PIN_PULL_NONE; |
sahilmgandhi | 18:6a4db94011d3 | 63 | break; |
sahilmgandhi | 18:6a4db94011d3 | 64 | case PullUp: |
sahilmgandhi | 18:6a4db94011d3 | 65 | pin_conf.input_pull = PORT_PIN_PULL_UP; |
sahilmgandhi | 18:6a4db94011d3 | 66 | break; |
sahilmgandhi | 18:6a4db94011d3 | 67 | case PullDown: |
sahilmgandhi | 18:6a4db94011d3 | 68 | pin_conf.input_pull = PORT_PIN_PULL_DOWN; |
sahilmgandhi | 18:6a4db94011d3 | 69 | break; |
sahilmgandhi | 18:6a4db94011d3 | 70 | } |
sahilmgandhi | 18:6a4db94011d3 | 71 | port_pin_set_config(obj->pin, &pin_conf); |
sahilmgandhi | 18:6a4db94011d3 | 72 | } |
sahilmgandhi | 18:6a4db94011d3 | 73 | |
sahilmgandhi | 18:6a4db94011d3 | 74 | void gpio_dir(gpio_t *obj, PinDirection direction) |
sahilmgandhi | 18:6a4db94011d3 | 75 | { |
sahilmgandhi | 18:6a4db94011d3 | 76 | MBED_ASSERT(obj->pin != (PinName)NC); |
sahilmgandhi | 18:6a4db94011d3 | 77 | struct port_config pin_conf; |
sahilmgandhi | 18:6a4db94011d3 | 78 | obj->direction = direction; |
sahilmgandhi | 18:6a4db94011d3 | 79 | pin_conf.input_pull = (enum port_pin_pull)obj->mode; |
sahilmgandhi | 18:6a4db94011d3 | 80 | pin_conf.powersave = obj->powersave; |
sahilmgandhi | 18:6a4db94011d3 | 81 | switch (direction) { |
sahilmgandhi | 18:6a4db94011d3 | 82 | case PIN_INPUT : |
sahilmgandhi | 18:6a4db94011d3 | 83 | pin_conf.direction = PORT_PIN_DIR_INPUT; |
sahilmgandhi | 18:6a4db94011d3 | 84 | break; |
sahilmgandhi | 18:6a4db94011d3 | 85 | case PIN_OUTPUT: |
sahilmgandhi | 18:6a4db94011d3 | 86 | pin_conf.direction = PORT_PIN_DIR_OUTPUT; |
sahilmgandhi | 18:6a4db94011d3 | 87 | break; |
sahilmgandhi | 18:6a4db94011d3 | 88 | case PIN_INPUT_OUTPUT: |
sahilmgandhi | 18:6a4db94011d3 | 89 | pin_conf.direction = PORT_PIN_DIR_OUTPUT_WTH_READBACK; |
sahilmgandhi | 18:6a4db94011d3 | 90 | break; |
sahilmgandhi | 18:6a4db94011d3 | 91 | } |
sahilmgandhi | 18:6a4db94011d3 | 92 | port_pin_set_config(obj->pin, &pin_conf); |
sahilmgandhi | 18:6a4db94011d3 | 93 | } |