Mouse code for the MacroRat
mbed-dev/targets/TARGET_ARM_SSG/TARGET_BEETLE/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) 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 "gpio_api.h" |
sahilmgandhi | 18:6a4db94011d3 | 17 | #include "pinmap.h" |
sahilmgandhi | 18:6a4db94011d3 | 18 | |
sahilmgandhi | 18:6a4db94011d3 | 19 | // function to enable the GPIO pin |
sahilmgandhi | 18:6a4db94011d3 | 20 | uint32_t gpio_set(PinName pin) { |
sahilmgandhi | 18:6a4db94011d3 | 21 | uint32_t pin_value = 0; |
sahilmgandhi | 18:6a4db94011d3 | 22 | |
sahilmgandhi | 18:6a4db94011d3 | 23 | if(pin <= 15) { |
sahilmgandhi | 18:6a4db94011d3 | 24 | pin_value = pin; |
sahilmgandhi | 18:6a4db94011d3 | 25 | } else if (pin >= 16 && pin <= 31) { |
sahilmgandhi | 18:6a4db94011d3 | 26 | pin_value = pin-16; |
sahilmgandhi | 18:6a4db94011d3 | 27 | } else if (pin >= 1001 && pin <= 1004) { |
sahilmgandhi | 18:6a4db94011d3 | 28 | /* Emulated LEDs */ |
sahilmgandhi | 18:6a4db94011d3 | 29 | return (1); |
sahilmgandhi | 18:6a4db94011d3 | 30 | } |
sahilmgandhi | 18:6a4db94011d3 | 31 | |
sahilmgandhi | 18:6a4db94011d3 | 32 | pin_function(pin, 0); |
sahilmgandhi | 18:6a4db94011d3 | 33 | |
sahilmgandhi | 18:6a4db94011d3 | 34 | return (1 << pin_value); |
sahilmgandhi | 18:6a4db94011d3 | 35 | } |
sahilmgandhi | 18:6a4db94011d3 | 36 | |
sahilmgandhi | 18:6a4db94011d3 | 37 | //function to initialise the gpio pin |
sahilmgandhi | 18:6a4db94011d3 | 38 | // this links the board control bits for each pin |
sahilmgandhi | 18:6a4db94011d3 | 39 | // with the object created for the pin |
sahilmgandhi | 18:6a4db94011d3 | 40 | void gpio_init(gpio_t *obj, PinName pin) { |
sahilmgandhi | 18:6a4db94011d3 | 41 | if (pin == NC) { |
sahilmgandhi | 18:6a4db94011d3 | 42 | return; |
sahilmgandhi | 18:6a4db94011d3 | 43 | } else { |
sahilmgandhi | 18:6a4db94011d3 | 44 | int pin_value = 0; |
sahilmgandhi | 18:6a4db94011d3 | 45 | obj->pin = pin; |
sahilmgandhi | 18:6a4db94011d3 | 46 | if (pin <=15) { |
sahilmgandhi | 18:6a4db94011d3 | 47 | pin_value = pin; |
sahilmgandhi | 18:6a4db94011d3 | 48 | } else if (pin >= 16 && pin <= 31) { |
sahilmgandhi | 18:6a4db94011d3 | 49 | pin_value = pin-16; |
sahilmgandhi | 18:6a4db94011d3 | 50 | } else if (pin >= 1001 && pin <= 1004) { |
sahilmgandhi | 18:6a4db94011d3 | 51 | /* Emulated LEDs */ |
sahilmgandhi | 18:6a4db94011d3 | 52 | return; |
sahilmgandhi | 18:6a4db94011d3 | 53 | } |
sahilmgandhi | 18:6a4db94011d3 | 54 | |
sahilmgandhi | 18:6a4db94011d3 | 55 | obj->mask = 0x1 << pin_value; |
sahilmgandhi | 18:6a4db94011d3 | 56 | obj->pin_number = pin; |
sahilmgandhi | 18:6a4db94011d3 | 57 | if (pin <=15) { |
sahilmgandhi | 18:6a4db94011d3 | 58 | obj->reg_data = &CMSDK_GPIO0->DATAOUT; |
sahilmgandhi | 18:6a4db94011d3 | 59 | obj->reg_in = &CMSDK_GPIO0->DATA; |
sahilmgandhi | 18:6a4db94011d3 | 60 | obj->reg_dir = &CMSDK_GPIO0->OUTENABLESET; |
sahilmgandhi | 18:6a4db94011d3 | 61 | obj->reg_dirclr = &CMSDK_GPIO0->OUTENABLECLR; |
sahilmgandhi | 18:6a4db94011d3 | 62 | } else if (pin >= 16 && pin <= 31) { |
sahilmgandhi | 18:6a4db94011d3 | 63 | obj->reg_data = &CMSDK_GPIO1->DATAOUT; |
sahilmgandhi | 18:6a4db94011d3 | 64 | obj->reg_in = &CMSDK_GPIO1->DATA; |
sahilmgandhi | 18:6a4db94011d3 | 65 | obj->reg_dir = &CMSDK_GPIO1->OUTENABLESET; |
sahilmgandhi | 18:6a4db94011d3 | 66 | obj->reg_dirclr = &CMSDK_GPIO1->OUTENABLECLR; |
sahilmgandhi | 18:6a4db94011d3 | 67 | } |
sahilmgandhi | 18:6a4db94011d3 | 68 | } |
sahilmgandhi | 18:6a4db94011d3 | 69 | } |
sahilmgandhi | 18:6a4db94011d3 | 70 | |
sahilmgandhi | 18:6a4db94011d3 | 71 | void gpio_mode(gpio_t *obj, PinMode mode) { |
sahilmgandhi | 18:6a4db94011d3 | 72 | pin_mode(obj->pin, mode); |
sahilmgandhi | 18:6a4db94011d3 | 73 | } |
sahilmgandhi | 18:6a4db94011d3 | 74 | |
sahilmgandhi | 18:6a4db94011d3 | 75 | void gpio_dir(gpio_t *obj, PinDirection direction) { |
sahilmgandhi | 18:6a4db94011d3 | 76 | if(obj->pin >= 0 && obj->pin <= 31) { |
sahilmgandhi | 18:6a4db94011d3 | 77 | switch (direction) { |
sahilmgandhi | 18:6a4db94011d3 | 78 | case PIN_INPUT : *obj->reg_dirclr = obj->mask; break; |
sahilmgandhi | 18:6a4db94011d3 | 79 | case PIN_OUTPUT: *obj->reg_dir |= obj->mask; break; |
sahilmgandhi | 18:6a4db94011d3 | 80 | } |
sahilmgandhi | 18:6a4db94011d3 | 81 | } else { |
sahilmgandhi | 18:6a4db94011d3 | 82 | return; |
sahilmgandhi | 18:6a4db94011d3 | 83 | } |
sahilmgandhi | 18:6a4db94011d3 | 84 | } |
sahilmgandhi | 18:6a4db94011d3 | 85 | |
sahilmgandhi | 18:6a4db94011d3 | 86 | int gpio_is_connected(const gpio_t *obj){ |
sahilmgandhi | 18:6a4db94011d3 | 87 | if(obj->pin != (PinName)NC){ |
sahilmgandhi | 18:6a4db94011d3 | 88 | return 1; |
sahilmgandhi | 18:6a4db94011d3 | 89 | } else { |
sahilmgandhi | 18:6a4db94011d3 | 90 | return 0; |
sahilmgandhi | 18:6a4db94011d3 | 91 | } |
sahilmgandhi | 18:6a4db94011d3 | 92 | } |