test
Fork of mbed-dev by
targets/TARGET_Realtek/TARGET_AMEBA/gpio_api.c@168:e84263d55307, 2017-06-21 (annotated)
- Committer:
- AnnaBridge
- Date:
- Wed Jun 21 17:46:44 2017 +0100
- Revision:
- 168:e84263d55307
This updates the lib to the mbed lib v 145
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AnnaBridge | 168:e84263d55307 | 1 | /* mbed Microcontroller Library |
AnnaBridge | 168:e84263d55307 | 2 | * Copyright (c) 2013-2016 Realtek Semiconductor Corp. |
AnnaBridge | 168:e84263d55307 | 3 | * |
AnnaBridge | 168:e84263d55307 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
AnnaBridge | 168:e84263d55307 | 5 | * you may not use this file except in compliance with the License. |
AnnaBridge | 168:e84263d55307 | 6 | * You may obtain a copy of the License at |
AnnaBridge | 168:e84263d55307 | 7 | * |
AnnaBridge | 168:e84263d55307 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
AnnaBridge | 168:e84263d55307 | 9 | * |
AnnaBridge | 168:e84263d55307 | 10 | * Unless required by applicable law or agreed to in writing, software |
AnnaBridge | 168:e84263d55307 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
AnnaBridge | 168:e84263d55307 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
AnnaBridge | 168:e84263d55307 | 13 | * See the License for the specific language governing permissions and |
AnnaBridge | 168:e84263d55307 | 14 | * limitations under the License. |
AnnaBridge | 168:e84263d55307 | 15 | */ |
AnnaBridge | 168:e84263d55307 | 16 | #include "objects.h" |
AnnaBridge | 168:e84263d55307 | 17 | #include "pinmap.h" |
AnnaBridge | 168:e84263d55307 | 18 | |
AnnaBridge | 168:e84263d55307 | 19 | #if CONFIG_GPIO_EN |
AnnaBridge | 168:e84263d55307 | 20 | |
AnnaBridge | 168:e84263d55307 | 21 | #include "gpio_api.h" |
AnnaBridge | 168:e84263d55307 | 22 | |
AnnaBridge | 168:e84263d55307 | 23 | // convert Mbed pin mode to HAL Pin Mode |
AnnaBridge | 168:e84263d55307 | 24 | const u8 GPIO_InPinMode[] = { |
AnnaBridge | 168:e84263d55307 | 25 | DIN_PULL_NONE, // PullNone |
AnnaBridge | 168:e84263d55307 | 26 | DIN_PULL_HIGH, // PullUp |
AnnaBridge | 168:e84263d55307 | 27 | DIN_PULL_LOW, // PullDown |
AnnaBridge | 168:e84263d55307 | 28 | DIN_PULL_NONE // OpenDrain |
AnnaBridge | 168:e84263d55307 | 29 | }; |
AnnaBridge | 168:e84263d55307 | 30 | |
AnnaBridge | 168:e84263d55307 | 31 | const u8 GPIO_SWPORT_DR_TBL[] = { |
AnnaBridge | 168:e84263d55307 | 32 | GPIO_PORTA_DR, |
AnnaBridge | 168:e84263d55307 | 33 | GPIO_PORTB_DR, |
AnnaBridge | 168:e84263d55307 | 34 | GPIO_PORTC_DR |
AnnaBridge | 168:e84263d55307 | 35 | }; |
AnnaBridge | 168:e84263d55307 | 36 | |
AnnaBridge | 168:e84263d55307 | 37 | const u8 GPIO_EXT_PORT_TBL[] = { |
AnnaBridge | 168:e84263d55307 | 38 | GPIO_EXT_PORTA, |
AnnaBridge | 168:e84263d55307 | 39 | GPIO_EXT_PORTB, |
AnnaBridge | 168:e84263d55307 | 40 | GPIO_EXT_PORTC |
AnnaBridge | 168:e84263d55307 | 41 | }; |
AnnaBridge | 168:e84263d55307 | 42 | |
AnnaBridge | 168:e84263d55307 | 43 | const u8 GPIO_SWPORT_DDR_TBL[] = { |
AnnaBridge | 168:e84263d55307 | 44 | GPIO_PORTA_DDR, |
AnnaBridge | 168:e84263d55307 | 45 | GPIO_PORTB_DDR, |
AnnaBridge | 168:e84263d55307 | 46 | GPIO_PORTC_DDR |
AnnaBridge | 168:e84263d55307 | 47 | }; |
AnnaBridge | 168:e84263d55307 | 48 | |
AnnaBridge | 168:e84263d55307 | 49 | void gpio_set_hal_pin_mode(gpio_t *obj) |
AnnaBridge | 168:e84263d55307 | 50 | { |
AnnaBridge | 168:e84263d55307 | 51 | uint32_t mode; |
AnnaBridge | 168:e84263d55307 | 52 | |
AnnaBridge | 168:e84263d55307 | 53 | mode = obj->mode; |
AnnaBridge | 168:e84263d55307 | 54 | if (obj->direction == PIN_OUTPUT) { |
AnnaBridge | 168:e84263d55307 | 55 | if (mode == OpenDrain) { |
AnnaBridge | 168:e84263d55307 | 56 | obj->hal_pin.pin_mode = DOUT_OPEN_DRAIN; |
AnnaBridge | 168:e84263d55307 | 57 | } else { |
AnnaBridge | 168:e84263d55307 | 58 | obj->hal_pin.pin_mode = DOUT_PUSH_PULL; |
AnnaBridge | 168:e84263d55307 | 59 | } |
AnnaBridge | 168:e84263d55307 | 60 | } else { |
AnnaBridge | 168:e84263d55307 | 61 | if (mode < 4) { |
AnnaBridge | 168:e84263d55307 | 62 | obj->hal_pin.pin_mode = GPIO_InPinMode[mode]; |
AnnaBridge | 168:e84263d55307 | 63 | } else { |
AnnaBridge | 168:e84263d55307 | 64 | obj->hal_pin.pin_mode = DIN_PULL_NONE; |
AnnaBridge | 168:e84263d55307 | 65 | } |
AnnaBridge | 168:e84263d55307 | 66 | } |
AnnaBridge | 168:e84263d55307 | 67 | } |
AnnaBridge | 168:e84263d55307 | 68 | |
AnnaBridge | 168:e84263d55307 | 69 | uint32_t gpio_set(PinName pin) |
AnnaBridge | 168:e84263d55307 | 70 | { |
AnnaBridge | 168:e84263d55307 | 71 | u32 ip_pin; |
AnnaBridge | 168:e84263d55307 | 72 | |
AnnaBridge | 168:e84263d55307 | 73 | DBG_ASSERT(pin != (PinName)NC); |
AnnaBridge | 168:e84263d55307 | 74 | pin_function(pin, 0); |
AnnaBridge | 168:e84263d55307 | 75 | ip_pin = HAL_GPIO_GetPinName((u32)pin); |
AnnaBridge | 168:e84263d55307 | 76 | |
AnnaBridge | 168:e84263d55307 | 77 | return ip_pin; |
AnnaBridge | 168:e84263d55307 | 78 | } |
AnnaBridge | 168:e84263d55307 | 79 | |
AnnaBridge | 168:e84263d55307 | 80 | void gpio_init(gpio_t *obj, PinName pin) |
AnnaBridge | 168:e84263d55307 | 81 | { |
AnnaBridge | 168:e84263d55307 | 82 | uint32_t pin_name; |
AnnaBridge | 168:e84263d55307 | 83 | |
AnnaBridge | 168:e84263d55307 | 84 | if (pin == (PinName)NC) |
AnnaBridge | 168:e84263d55307 | 85 | return; |
AnnaBridge | 168:e84263d55307 | 86 | |
AnnaBridge | 168:e84263d55307 | 87 | obj->pin = pin; |
AnnaBridge | 168:e84263d55307 | 88 | obj->mode = PullNone; |
AnnaBridge | 168:e84263d55307 | 89 | obj->direction = PIN_INPUT; |
AnnaBridge | 168:e84263d55307 | 90 | pin_name = gpio_set(pin); // get the IP pin name |
AnnaBridge | 168:e84263d55307 | 91 | obj->hal_pin.pin_name = pin_name; |
AnnaBridge | 168:e84263d55307 | 92 | obj->hal_pin.pin_mode = DIN_PULL_NONE; |
AnnaBridge | 168:e84263d55307 | 93 | obj->hal_port_num = HAL_GPIO_GET_PORT_BY_NAME(pin_name); |
AnnaBridge | 168:e84263d55307 | 94 | obj->hal_pin_num = HAL_GPIO_GET_PIN_BY_NAME(pin_name); |
AnnaBridge | 168:e84263d55307 | 95 | HAL_GPIO_Init(&obj->hal_pin); |
AnnaBridge | 168:e84263d55307 | 96 | } |
AnnaBridge | 168:e84263d55307 | 97 | |
AnnaBridge | 168:e84263d55307 | 98 | void gpio_mode(gpio_t *obj, PinMode mode) |
AnnaBridge | 168:e84263d55307 | 99 | { |
AnnaBridge | 168:e84263d55307 | 100 | obj->mode = mode; |
AnnaBridge | 168:e84263d55307 | 101 | gpio_set_hal_pin_mode(obj); |
AnnaBridge | 168:e84263d55307 | 102 | HAL_GPIO_Init(&obj->hal_pin); |
AnnaBridge | 168:e84263d55307 | 103 | } |
AnnaBridge | 168:e84263d55307 | 104 | |
AnnaBridge | 168:e84263d55307 | 105 | // Initial the Pin direction |
AnnaBridge | 168:e84263d55307 | 106 | void gpio_dir(gpio_t *obj, PinDirection direction) |
AnnaBridge | 168:e84263d55307 | 107 | { |
AnnaBridge | 168:e84263d55307 | 108 | obj->direction = direction; |
AnnaBridge | 168:e84263d55307 | 109 | gpio_set_hal_pin_mode(obj); |
AnnaBridge | 168:e84263d55307 | 110 | HAL_GPIO_Init(&obj->hal_pin); |
AnnaBridge | 168:e84263d55307 | 111 | } |
AnnaBridge | 168:e84263d55307 | 112 | |
AnnaBridge | 168:e84263d55307 | 113 | // Change the pin direction directly |
AnnaBridge | 168:e84263d55307 | 114 | void gpio_change_dir(gpio_t *obj, PinDirection direction) |
AnnaBridge | 168:e84263d55307 | 115 | { |
AnnaBridge | 168:e84263d55307 | 116 | uint32_t reg_value; |
AnnaBridge | 168:e84263d55307 | 117 | uint8_t port_num; |
AnnaBridge | 168:e84263d55307 | 118 | uint8_t pin_num; |
AnnaBridge | 168:e84263d55307 | 119 | |
AnnaBridge | 168:e84263d55307 | 120 | obj->direction = direction; |
AnnaBridge | 168:e84263d55307 | 121 | gpio_set_hal_pin_mode(obj); |
AnnaBridge | 168:e84263d55307 | 122 | port_num = obj->hal_port_num; |
AnnaBridge | 168:e84263d55307 | 123 | pin_num = obj->hal_pin_num; |
AnnaBridge | 168:e84263d55307 | 124 | |
AnnaBridge | 168:e84263d55307 | 125 | reg_value = HAL_READ32(GPIO_REG_BASE, GPIO_SWPORT_DDR_TBL[port_num]); |
AnnaBridge | 168:e84263d55307 | 126 | if (direction) { |
AnnaBridge | 168:e84263d55307 | 127 | // Out |
AnnaBridge | 168:e84263d55307 | 128 | reg_value |= (1 << pin_num); |
AnnaBridge | 168:e84263d55307 | 129 | } else { |
AnnaBridge | 168:e84263d55307 | 130 | // In |
AnnaBridge | 168:e84263d55307 | 131 | reg_value &= ~(1 << pin_num); |
AnnaBridge | 168:e84263d55307 | 132 | } |
AnnaBridge | 168:e84263d55307 | 133 | HAL_WRITE32(GPIO_REG_BASE, GPIO_SWPORT_DDR_TBL[port_num], reg_value); |
AnnaBridge | 168:e84263d55307 | 134 | } |
AnnaBridge | 168:e84263d55307 | 135 | |
AnnaBridge | 168:e84263d55307 | 136 | void gpio_write(gpio_t *obj, int value) |
AnnaBridge | 168:e84263d55307 | 137 | { |
AnnaBridge | 168:e84263d55307 | 138 | HAL_GPIO_PIN *hal_pin=&obj->hal_pin; |
AnnaBridge | 168:e84263d55307 | 139 | volatile uint32_t reg_value; |
AnnaBridge | 168:e84263d55307 | 140 | uint8_t port_num; |
AnnaBridge | 168:e84263d55307 | 141 | uint8_t pin_num; |
AnnaBridge | 168:e84263d55307 | 142 | |
AnnaBridge | 168:e84263d55307 | 143 | if (hal_pin->pin_mode != DOUT_OPEN_DRAIN) { |
AnnaBridge | 168:e84263d55307 | 144 | port_num = obj->hal_port_num; |
AnnaBridge | 168:e84263d55307 | 145 | pin_num = obj->hal_pin_num; |
AnnaBridge | 168:e84263d55307 | 146 | |
AnnaBridge | 168:e84263d55307 | 147 | reg_value = HAL_READ32(GPIO_REG_BASE, GPIO_SWPORT_DR_TBL[port_num]); |
AnnaBridge | 168:e84263d55307 | 148 | reg_value &= ~(1 << pin_num); |
AnnaBridge | 168:e84263d55307 | 149 | reg_value |= ((value&0x01)<< pin_num); |
AnnaBridge | 168:e84263d55307 | 150 | HAL_WRITE32(GPIO_REG_BASE, GPIO_SWPORT_DR_TBL[port_num], reg_value); |
AnnaBridge | 168:e84263d55307 | 151 | } else { |
AnnaBridge | 168:e84263d55307 | 152 | HAL_GPIO_WritePin(&obj->hal_pin, value); |
AnnaBridge | 168:e84263d55307 | 153 | } |
AnnaBridge | 168:e84263d55307 | 154 | } |
AnnaBridge | 168:e84263d55307 | 155 | |
AnnaBridge | 168:e84263d55307 | 156 | int gpio_read(gpio_t *obj) |
AnnaBridge | 168:e84263d55307 | 157 | { |
AnnaBridge | 168:e84263d55307 | 158 | volatile uint32_t reg_value; |
AnnaBridge | 168:e84263d55307 | 159 | uint8_t port_num; |
AnnaBridge | 168:e84263d55307 | 160 | uint8_t pin_num; |
AnnaBridge | 168:e84263d55307 | 161 | HAL_GPIO_PIN_MODE pin_mode; |
AnnaBridge | 168:e84263d55307 | 162 | |
AnnaBridge | 168:e84263d55307 | 163 | port_num = obj->hal_port_num; |
AnnaBridge | 168:e84263d55307 | 164 | pin_num = obj->hal_pin_num; |
AnnaBridge | 168:e84263d55307 | 165 | pin_mode = obj->hal_pin.pin_mode; |
AnnaBridge | 168:e84263d55307 | 166 | |
AnnaBridge | 168:e84263d55307 | 167 | reg_value = HAL_READ32(GPIO_REG_BASE, GPIO_EXT_PORT_TBL[port_num]); |
AnnaBridge | 168:e84263d55307 | 168 | if (pin_mode != DOUT_OPEN_DRAIN) { |
AnnaBridge | 168:e84263d55307 | 169 | return ((reg_value >> pin_num) & 0x01); |
AnnaBridge | 168:e84263d55307 | 170 | } else { |
AnnaBridge | 168:e84263d55307 | 171 | return (!((reg_value >> pin_num) & 0x01)); |
AnnaBridge | 168:e84263d55307 | 172 | } |
AnnaBridge | 168:e84263d55307 | 173 | } |
AnnaBridge | 168:e84263d55307 | 174 | |
AnnaBridge | 168:e84263d55307 | 175 | // This API only works for non-Open-Drain pin |
AnnaBridge | 168:e84263d55307 | 176 | void gpio_direct_write(gpio_t *obj, BOOL value) |
AnnaBridge | 168:e84263d55307 | 177 | { |
AnnaBridge | 168:e84263d55307 | 178 | uint8_t port_num; |
AnnaBridge | 168:e84263d55307 | 179 | uint8_t pin_num; |
AnnaBridge | 168:e84263d55307 | 180 | uint32_t reg_value; |
AnnaBridge | 168:e84263d55307 | 181 | |
AnnaBridge | 168:e84263d55307 | 182 | port_num = obj->hal_port_num; |
AnnaBridge | 168:e84263d55307 | 183 | pin_num = obj->hal_pin_num; |
AnnaBridge | 168:e84263d55307 | 184 | |
AnnaBridge | 168:e84263d55307 | 185 | reg_value = HAL_READ32(GPIO_REG_BASE, GPIO_SWPORT_DR_TBL[port_num]); |
AnnaBridge | 168:e84263d55307 | 186 | reg_value &= ~(1 << pin_num); |
AnnaBridge | 168:e84263d55307 | 187 | reg_value |= (value<< pin_num); |
AnnaBridge | 168:e84263d55307 | 188 | HAL_WRITE32(GPIO_REG_BASE, GPIO_SWPORT_DR_TBL[port_num], reg_value); |
AnnaBridge | 168:e84263d55307 | 189 | } |
AnnaBridge | 168:e84263d55307 | 190 | |
AnnaBridge | 168:e84263d55307 | 191 | void gpio_pull_ctrl(gpio_t *obj, PinMode pull_type) |
AnnaBridge | 168:e84263d55307 | 192 | { |
AnnaBridge | 168:e84263d55307 | 193 | HAL_GPIO_PullCtrl((u32) obj->pin, (u32)pull_type); |
AnnaBridge | 168:e84263d55307 | 194 | } |
AnnaBridge | 168:e84263d55307 | 195 | |
AnnaBridge | 168:e84263d55307 | 196 | |
AnnaBridge | 168:e84263d55307 | 197 | void gpio_deinit(gpio_t *obj) |
AnnaBridge | 168:e84263d55307 | 198 | { |
AnnaBridge | 168:e84263d55307 | 199 | HAL_GPIO_DeInit(&obj->hal_pin); |
AnnaBridge | 168:e84263d55307 | 200 | } |
AnnaBridge | 168:e84263d55307 | 201 | |
AnnaBridge | 168:e84263d55307 | 202 | #endif |