mbed library sources
Dependents: frdm_kl05z_gpio_test
Fork of mbed-src by
targets/hal/TARGET_Freescale/TARGET_K20D50M/gpio_irq_api.c@323:9e901b0a5aa1, 2014-09-13 (annotated)
- Committer:
- shaoziyang
- Date:
- Sat Sep 13 14:25:46 2014 +0000
- Revision:
- 323:9e901b0a5aa1
- Parent:
- 285:31249416b6f9
test with CLOCK_SETUP = 0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 255:20b371a9491b | 1 | /* mbed Microcontroller Library |
mbed_official | 255:20b371a9491b | 2 | * Copyright (c) 2006-2013 ARM Limited |
mbed_official | 255:20b371a9491b | 3 | * |
mbed_official | 255:20b371a9491b | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
mbed_official | 255:20b371a9491b | 5 | * you may not use this file except in compliance with the License. |
mbed_official | 255:20b371a9491b | 6 | * You may obtain a copy of the License at |
mbed_official | 255:20b371a9491b | 7 | * |
mbed_official | 255:20b371a9491b | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
mbed_official | 255:20b371a9491b | 9 | * |
mbed_official | 255:20b371a9491b | 10 | * Unless required by applicable law or agreed to in writing, software |
mbed_official | 255:20b371a9491b | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
mbed_official | 255:20b371a9491b | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
mbed_official | 255:20b371a9491b | 13 | * See the License for the specific language governing permissions and |
mbed_official | 255:20b371a9491b | 14 | * limitations under the License. |
mbed_official | 255:20b371a9491b | 15 | */ |
mbed_official | 255:20b371a9491b | 16 | #include <stddef.h> |
mbed_official | 255:20b371a9491b | 17 | #include "cmsis.h" |
mbed_official | 255:20b371a9491b | 18 | |
mbed_official | 255:20b371a9491b | 19 | #include "gpio_irq_api.h" |
mbed_official | 285:31249416b6f9 | 20 | #include "mbed_error.h" |
mbed_official | 255:20b371a9491b | 21 | |
mbed_official | 255:20b371a9491b | 22 | #define CHANNEL_NUM 160 |
mbed_official | 255:20b371a9491b | 23 | |
mbed_official | 255:20b371a9491b | 24 | static uint32_t channel_ids[CHANNEL_NUM] = {0}; |
mbed_official | 255:20b371a9491b | 25 | static gpio_irq_handler irq_handler; |
mbed_official | 255:20b371a9491b | 26 | |
mbed_official | 255:20b371a9491b | 27 | #define IRQ_DISABLED (0) |
mbed_official | 255:20b371a9491b | 28 | #define IRQ_RAISING_EDGE PORT_PCR_IRQC(9) |
mbed_official | 255:20b371a9491b | 29 | #define IRQ_FALLING_EDGE PORT_PCR_IRQC(10) |
mbed_official | 255:20b371a9491b | 30 | #define IRQ_EITHER_EDGE PORT_PCR_IRQC(11) |
mbed_official | 255:20b371a9491b | 31 | |
mbed_official | 255:20b371a9491b | 32 | static void handle_interrupt_in(PORT_Type *port, int ch_base) { |
mbed_official | 255:20b371a9491b | 33 | uint32_t isfr; |
mbed_official | 255:20b371a9491b | 34 | uint32_t pin; |
mbed_official | 255:20b371a9491b | 35 | |
mbed_official | 255:20b371a9491b | 36 | while ((isfr = port->ISFR) != 0) { |
mbed_official | 255:20b371a9491b | 37 | pin = 31 - __CLZ(isfr); |
mbed_official | 255:20b371a9491b | 38 | uint32_t id = channel_ids[ch_base + pin]; |
mbed_official | 255:20b371a9491b | 39 | if (id == 0) { |
mbed_official | 255:20b371a9491b | 40 | continue; |
mbed_official | 255:20b371a9491b | 41 | } |
mbed_official | 255:20b371a9491b | 42 | |
mbed_official | 255:20b371a9491b | 43 | GPIO_Type *gpio = PTA; |
mbed_official | 255:20b371a9491b | 44 | gpio_irq_event event = IRQ_NONE; |
mbed_official | 255:20b371a9491b | 45 | uint32_t port_num = (port - PORTA) >> 12; |
mbed_official | 255:20b371a9491b | 46 | |
mbed_official | 255:20b371a9491b | 47 | switch (port->PCR[pin] & PORT_PCR_IRQC_MASK) { |
mbed_official | 255:20b371a9491b | 48 | case IRQ_RAISING_EDGE: |
mbed_official | 255:20b371a9491b | 49 | event = IRQ_RISE; |
mbed_official | 255:20b371a9491b | 50 | break; |
mbed_official | 255:20b371a9491b | 51 | case IRQ_FALLING_EDGE: |
mbed_official | 255:20b371a9491b | 52 | event = IRQ_FALL; |
mbed_official | 255:20b371a9491b | 53 | break; |
mbed_official | 255:20b371a9491b | 54 | case IRQ_EITHER_EDGE: |
mbed_official | 255:20b371a9491b | 55 | gpio += (port_num * 0x40); |
mbed_official | 255:20b371a9491b | 56 | event = (gpio->PDIR & (1 << pin)) ? (IRQ_RISE) : (IRQ_FALL); |
mbed_official | 255:20b371a9491b | 57 | break; |
mbed_official | 255:20b371a9491b | 58 | } |
mbed_official | 255:20b371a9491b | 59 | if (event != IRQ_NONE) { |
mbed_official | 255:20b371a9491b | 60 | irq_handler(id, event); |
mbed_official | 255:20b371a9491b | 61 | } |
mbed_official | 255:20b371a9491b | 62 | port->ISFR = 1 << pin; |
mbed_official | 255:20b371a9491b | 63 | } |
mbed_official | 255:20b371a9491b | 64 | } |
mbed_official | 255:20b371a9491b | 65 | |
mbed_official | 255:20b371a9491b | 66 | void gpio_irqA(void) { |
mbed_official | 255:20b371a9491b | 67 | handle_interrupt_in(PORTA, 0); |
mbed_official | 255:20b371a9491b | 68 | } |
mbed_official | 255:20b371a9491b | 69 | |
mbed_official | 255:20b371a9491b | 70 | void gpio_irqB(void) |
mbed_official | 255:20b371a9491b | 71 | { |
mbed_official | 255:20b371a9491b | 72 | handle_interrupt_in(PORTB, 32); |
mbed_official | 255:20b371a9491b | 73 | } |
mbed_official | 255:20b371a9491b | 74 | |
mbed_official | 255:20b371a9491b | 75 | void gpio_irqC(void) |
mbed_official | 255:20b371a9491b | 76 | { |
mbed_official | 255:20b371a9491b | 77 | handle_interrupt_in(PORTC, 64); |
mbed_official | 255:20b371a9491b | 78 | } |
mbed_official | 255:20b371a9491b | 79 | |
mbed_official | 255:20b371a9491b | 80 | void gpio_irqD(void) |
mbed_official | 255:20b371a9491b | 81 | { |
mbed_official | 255:20b371a9491b | 82 | handle_interrupt_in(PORTD, 96); |
mbed_official | 255:20b371a9491b | 83 | } |
mbed_official | 255:20b371a9491b | 84 | |
mbed_official | 255:20b371a9491b | 85 | void gpio_irqE(void) |
mbed_official | 255:20b371a9491b | 86 | { |
mbed_official | 255:20b371a9491b | 87 | handle_interrupt_in(PORTE, 128); |
mbed_official | 255:20b371a9491b | 88 | } |
mbed_official | 255:20b371a9491b | 89 | |
mbed_official | 255:20b371a9491b | 90 | |
mbed_official | 255:20b371a9491b | 91 | int gpio_irq_init(gpio_irq_t *obj, PinName pin, gpio_irq_handler handler, uint32_t id) { |
mbed_official | 255:20b371a9491b | 92 | if (pin == NC) |
mbed_official | 255:20b371a9491b | 93 | return -1; |
mbed_official | 255:20b371a9491b | 94 | |
mbed_official | 255:20b371a9491b | 95 | irq_handler = handler; |
mbed_official | 255:20b371a9491b | 96 | |
mbed_official | 255:20b371a9491b | 97 | obj->port = pin >> PORT_SHIFT; |
mbed_official | 255:20b371a9491b | 98 | obj->pin = (pin & 0x7F) >> 2; |
mbed_official | 255:20b371a9491b | 99 | |
mbed_official | 255:20b371a9491b | 100 | uint32_t ch_base, vector; |
mbed_official | 255:20b371a9491b | 101 | IRQn_Type irq_n; |
mbed_official | 255:20b371a9491b | 102 | switch (obj->port) { |
mbed_official | 255:20b371a9491b | 103 | case PortA: |
mbed_official | 255:20b371a9491b | 104 | ch_base = 0; |
mbed_official | 255:20b371a9491b | 105 | irq_n = PORTA_IRQn; |
mbed_official | 255:20b371a9491b | 106 | vector = (uint32_t)gpio_irqA; |
mbed_official | 255:20b371a9491b | 107 | break; |
mbed_official | 255:20b371a9491b | 108 | case PortB: |
mbed_official | 255:20b371a9491b | 109 | ch_base = 32; |
mbed_official | 255:20b371a9491b | 110 | irq_n = PORTB_IRQn; |
mbed_official | 255:20b371a9491b | 111 | vector = (uint32_t)gpio_irqB; |
mbed_official | 255:20b371a9491b | 112 | break; |
mbed_official | 255:20b371a9491b | 113 | case PortC: |
mbed_official | 255:20b371a9491b | 114 | ch_base = 64; |
mbed_official | 255:20b371a9491b | 115 | irq_n = PORTC_IRQn; |
mbed_official | 255:20b371a9491b | 116 | vector = (uint32_t)gpio_irqC; |
mbed_official | 255:20b371a9491b | 117 | break; |
mbed_official | 255:20b371a9491b | 118 | case PortD: |
mbed_official | 255:20b371a9491b | 119 | ch_base = 96; |
mbed_official | 255:20b371a9491b | 120 | irq_n = PORTD_IRQn; vector = (uint32_t)gpio_irqD; |
mbed_official | 255:20b371a9491b | 121 | break; |
mbed_official | 255:20b371a9491b | 122 | case PortE: |
mbed_official | 255:20b371a9491b | 123 | ch_base = 128; |
mbed_official | 255:20b371a9491b | 124 | irq_n = PORTE_IRQn; |
mbed_official | 255:20b371a9491b | 125 | vector = (uint32_t)gpio_irqE; |
mbed_official | 255:20b371a9491b | 126 | break; |
mbed_official | 255:20b371a9491b | 127 | |
mbed_official | 255:20b371a9491b | 128 | default: |
mbed_official | 255:20b371a9491b | 129 | error("gpio_irq only supported on port A-E."); |
mbed_official | 255:20b371a9491b | 130 | break; |
mbed_official | 255:20b371a9491b | 131 | } |
mbed_official | 255:20b371a9491b | 132 | NVIC_SetVector(irq_n, vector); |
mbed_official | 255:20b371a9491b | 133 | NVIC_EnableIRQ(irq_n); |
mbed_official | 255:20b371a9491b | 134 | |
mbed_official | 255:20b371a9491b | 135 | obj->ch = ch_base + obj->pin; |
mbed_official | 255:20b371a9491b | 136 | channel_ids[obj->ch] = id; |
mbed_official | 255:20b371a9491b | 137 | |
mbed_official | 255:20b371a9491b | 138 | return 0; |
mbed_official | 255:20b371a9491b | 139 | } |
mbed_official | 255:20b371a9491b | 140 | |
mbed_official | 255:20b371a9491b | 141 | void gpio_irq_free(gpio_irq_t *obj) { |
mbed_official | 255:20b371a9491b | 142 | channel_ids[obj->ch] = 0; |
mbed_official | 255:20b371a9491b | 143 | } |
mbed_official | 255:20b371a9491b | 144 | |
mbed_official | 255:20b371a9491b | 145 | void gpio_irq_set(gpio_irq_t *obj, gpio_irq_event event, uint32_t enable) { |
mbed_official | 255:20b371a9491b | 146 | PORT_Type *port = (PORT_Type *)(PORTA_BASE + 0x1000 * obj->port); |
mbed_official | 255:20b371a9491b | 147 | |
mbed_official | 255:20b371a9491b | 148 | uint32_t irq_settings = IRQ_DISABLED; |
mbed_official | 255:20b371a9491b | 149 | |
mbed_official | 255:20b371a9491b | 150 | switch (port->PCR[obj->pin] & PORT_PCR_IRQC_MASK) { |
mbed_official | 255:20b371a9491b | 151 | case IRQ_DISABLED: |
mbed_official | 255:20b371a9491b | 152 | if (enable) |
mbed_official | 255:20b371a9491b | 153 | irq_settings = (event == IRQ_RISE) ? (IRQ_RAISING_EDGE) : (IRQ_FALLING_EDGE); |
mbed_official | 255:20b371a9491b | 154 | break; |
mbed_official | 255:20b371a9491b | 155 | |
mbed_official | 255:20b371a9491b | 156 | case IRQ_RAISING_EDGE: |
mbed_official | 255:20b371a9491b | 157 | if (enable) { |
mbed_official | 255:20b371a9491b | 158 | irq_settings = (event == IRQ_RISE) ? (IRQ_RAISING_EDGE) : (IRQ_EITHER_EDGE); |
mbed_official | 255:20b371a9491b | 159 | } else { |
mbed_official | 255:20b371a9491b | 160 | if (event == IRQ_FALL) |
mbed_official | 255:20b371a9491b | 161 | irq_settings = IRQ_RAISING_EDGE; |
mbed_official | 255:20b371a9491b | 162 | } |
mbed_official | 255:20b371a9491b | 163 | break; |
mbed_official | 255:20b371a9491b | 164 | |
mbed_official | 255:20b371a9491b | 165 | case IRQ_FALLING_EDGE: |
mbed_official | 255:20b371a9491b | 166 | if (enable) { |
mbed_official | 255:20b371a9491b | 167 | irq_settings = (event == IRQ_FALL) ? (IRQ_FALLING_EDGE) : (IRQ_EITHER_EDGE); |
mbed_official | 255:20b371a9491b | 168 | } else { |
mbed_official | 255:20b371a9491b | 169 | if (event == IRQ_RISE) |
mbed_official | 255:20b371a9491b | 170 | irq_settings = IRQ_FALLING_EDGE; |
mbed_official | 255:20b371a9491b | 171 | } |
mbed_official | 255:20b371a9491b | 172 | break; |
mbed_official | 255:20b371a9491b | 173 | |
mbed_official | 255:20b371a9491b | 174 | case IRQ_EITHER_EDGE: |
mbed_official | 255:20b371a9491b | 175 | if (enable) { |
mbed_official | 255:20b371a9491b | 176 | irq_settings = IRQ_EITHER_EDGE; |
mbed_official | 255:20b371a9491b | 177 | } else { |
mbed_official | 255:20b371a9491b | 178 | irq_settings = (event == IRQ_RISE) ? (IRQ_FALLING_EDGE) : (IRQ_RAISING_EDGE); |
mbed_official | 255:20b371a9491b | 179 | } |
mbed_official | 255:20b371a9491b | 180 | break; |
mbed_official | 255:20b371a9491b | 181 | } |
mbed_official | 255:20b371a9491b | 182 | |
mbed_official | 255:20b371a9491b | 183 | // Interrupt configuration and clear interrupt |
mbed_official | 255:20b371a9491b | 184 | port->PCR[obj->pin] = (port->PCR[obj->pin] & ~PORT_PCR_IRQC_MASK) | irq_settings | PORT_PCR_ISF_MASK; |
mbed_official | 255:20b371a9491b | 185 | } |
mbed_official | 255:20b371a9491b | 186 | |
mbed_official | 255:20b371a9491b | 187 | void gpio_irq_enable(gpio_irq_t *obj) { |
mbed_official | 255:20b371a9491b | 188 | switch (obj->port) { |
mbed_official | 255:20b371a9491b | 189 | case PortA: |
mbed_official | 255:20b371a9491b | 190 | NVIC_EnableIRQ(PORTA_IRQn); |
mbed_official | 255:20b371a9491b | 191 | break; |
mbed_official | 255:20b371a9491b | 192 | case PortB: |
mbed_official | 255:20b371a9491b | 193 | NVIC_EnableIRQ(PORTB_IRQn); |
mbed_official | 255:20b371a9491b | 194 | break; |
mbed_official | 255:20b371a9491b | 195 | case PortC: |
mbed_official | 255:20b371a9491b | 196 | NVIC_EnableIRQ(PORTC_IRQn); |
mbed_official | 255:20b371a9491b | 197 | break; |
mbed_official | 255:20b371a9491b | 198 | case PortD: |
mbed_official | 255:20b371a9491b | 199 | NVIC_EnableIRQ(PORTD_IRQn); |
mbed_official | 255:20b371a9491b | 200 | break; |
mbed_official | 255:20b371a9491b | 201 | case PortE: |
mbed_official | 255:20b371a9491b | 202 | NVIC_EnableIRQ(PORTE_IRQn); |
mbed_official | 255:20b371a9491b | 203 | break; |
mbed_official | 255:20b371a9491b | 204 | } |
mbed_official | 255:20b371a9491b | 205 | } |
mbed_official | 255:20b371a9491b | 206 | |
mbed_official | 255:20b371a9491b | 207 | void gpio_irq_disable(gpio_irq_t *obj) { |
mbed_official | 255:20b371a9491b | 208 | switch (obj->port) { |
mbed_official | 255:20b371a9491b | 209 | case PortA: |
mbed_official | 255:20b371a9491b | 210 | NVIC_DisableIRQ(PORTA_IRQn); |
mbed_official | 255:20b371a9491b | 211 | break; |
mbed_official | 255:20b371a9491b | 212 | case PortB: |
mbed_official | 255:20b371a9491b | 213 | NVIC_DisableIRQ(PORTB_IRQn); |
mbed_official | 255:20b371a9491b | 214 | break; |
mbed_official | 255:20b371a9491b | 215 | case PortC: |
mbed_official | 255:20b371a9491b | 216 | NVIC_DisableIRQ(PORTC_IRQn); |
mbed_official | 255:20b371a9491b | 217 | break; |
mbed_official | 255:20b371a9491b | 218 | case PortD: |
mbed_official | 255:20b371a9491b | 219 | NVIC_DisableIRQ(PORTD_IRQn); |
mbed_official | 255:20b371a9491b | 220 | break; |
mbed_official | 255:20b371a9491b | 221 | case PortE: |
mbed_official | 255:20b371a9491b | 222 | NVIC_DisableIRQ(PORTE_IRQn); |
mbed_official | 255:20b371a9491b | 223 | break; |
mbed_official | 255:20b371a9491b | 224 | } |
mbed_official | 255:20b371a9491b | 225 | } |