mbed library sources

Dependents:   frdm_kl05z_gpio_test

Fork of mbed-src by mbed official

Committer:
shaoziyang
Date:
Sat Sep 13 14:25:46 2014 +0000
Revision:
323:9e901b0a5aa1
Parent:
320:be04b2b1e3f2
test with CLOCK_SETUP = 0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 146:f64d43ff0c18 1 /* mbed Microcontroller Library
mbed_official 146:f64d43ff0c18 2 * Copyright (c) 2006-2013 ARM Limited
mbed_official 146:f64d43ff0c18 3 *
mbed_official 146:f64d43ff0c18 4 * Licensed under the Apache License, Version 2.0 (the "License");
mbed_official 146:f64d43ff0c18 5 * you may not use this file except in compliance with the License.
mbed_official 146:f64d43ff0c18 6 * You may obtain a copy of the License at
mbed_official 146:f64d43ff0c18 7 *
mbed_official 146:f64d43ff0c18 8 * http://www.apache.org/licenses/LICENSE-2.0
mbed_official 146:f64d43ff0c18 9 *
mbed_official 146:f64d43ff0c18 10 * Unless required by applicable law or agreed to in writing, software
mbed_official 146:f64d43ff0c18 11 * distributed under the License is distributed on an "AS IS" BASIS,
mbed_official 146:f64d43ff0c18 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mbed_official 146:f64d43ff0c18 13 * See the License for the specific language governing permissions and
mbed_official 146:f64d43ff0c18 14 * limitations under the License.
mbed_official 146:f64d43ff0c18 15 */
mbed_official 146:f64d43ff0c18 16 #include <stddef.h>
mbed_official 146:f64d43ff0c18 17 #include "cmsis.h"
mbed_official 146:f64d43ff0c18 18
mbed_official 146:f64d43ff0c18 19 #include "gpio_irq_api.h"
mbed_official 267:8673334f2cbe 20
mbed_official 267:8673334f2cbe 21 #if DEVICE_INTERRUPTIN
mbed_official 267:8673334f2cbe 22
mbed_official 146:f64d43ff0c18 23 #include "gpio_api.h"
mbed_official 146:f64d43ff0c18 24 #include "fsl_gpio_hal.h"
mbed_official 146:f64d43ff0c18 25 #include "fsl_port_hal.h"
mbed_official 285:31249416b6f9 26 #include "mbed_error.h"
mbed_official 146:f64d43ff0c18 27
mbed_official 146:f64d43ff0c18 28 #define CHANNEL_NUM 160
mbed_official 146:f64d43ff0c18 29
mbed_official 146:f64d43ff0c18 30 static uint32_t channel_ids[CHANNEL_NUM] = {0};
mbed_official 146:f64d43ff0c18 31 static gpio_irq_handler irq_handler;
mbed_official 146:f64d43ff0c18 32
mbed_official 146:f64d43ff0c18 33 #define IRQ_DISABLED (0)
mbed_official 146:f64d43ff0c18 34 #define IRQ_RAISING_EDGE (9)
mbed_official 146:f64d43ff0c18 35 #define IRQ_FALLING_EDGE (10)
mbed_official 146:f64d43ff0c18 36 #define IRQ_EITHER_EDGE (11)
mbed_official 146:f64d43ff0c18 37
mbed_official 146:f64d43ff0c18 38 static void handle_interrupt_in(PortName port, int ch_base) {
mbed_official 146:f64d43ff0c18 39 uint32_t i;
mbed_official 146:f64d43ff0c18 40
mbed_official 146:f64d43ff0c18 41 for (i = 0; i < 32; i++) {
mbed_official 146:f64d43ff0c18 42 if (port_hal_read_pin_interrupt_flag(port, i)) {
mbed_official 146:f64d43ff0c18 43 uint32_t id = channel_ids[ch_base + i];
mbed_official 146:f64d43ff0c18 44 if (id == 0) {
mbed_official 146:f64d43ff0c18 45 continue;
mbed_official 146:f64d43ff0c18 46 }
mbed_official 146:f64d43ff0c18 47
mbed_official 146:f64d43ff0c18 48 gpio_irq_event event = IRQ_NONE;
mbed_official 146:f64d43ff0c18 49 switch (BR_PORT_PCRn_IRQC(port, i)) {
mbed_official 146:f64d43ff0c18 50 case IRQ_RAISING_EDGE:
mbed_official 146:f64d43ff0c18 51 event = IRQ_RISE;
mbed_official 146:f64d43ff0c18 52 break;
mbed_official 146:f64d43ff0c18 53
mbed_official 146:f64d43ff0c18 54 case IRQ_FALLING_EDGE:
mbed_official 146:f64d43ff0c18 55 event = IRQ_FALL;
mbed_official 146:f64d43ff0c18 56 break;
mbed_official 146:f64d43ff0c18 57
mbed_official 146:f64d43ff0c18 58 case IRQ_EITHER_EDGE:
mbed_official 146:f64d43ff0c18 59 event = (gpio_hal_read_pin_input(port, i)) ? (IRQ_RISE) : (IRQ_FALL);
mbed_official 146:f64d43ff0c18 60 break;
mbed_official 146:f64d43ff0c18 61 }
mbed_official 146:f64d43ff0c18 62 if (event != IRQ_NONE) {
mbed_official 146:f64d43ff0c18 63 irq_handler(id, event);
mbed_official 146:f64d43ff0c18 64 }
mbed_official 146:f64d43ff0c18 65 }
mbed_official 146:f64d43ff0c18 66 }
mbed_official 146:f64d43ff0c18 67 port_hal_clear_port_interrupt_flag(port);
mbed_official 146:f64d43ff0c18 68 }
mbed_official 146:f64d43ff0c18 69
mbed_official 146:f64d43ff0c18 70 void gpio_irqA(void) {handle_interrupt_in(PortA, 0);}
mbed_official 146:f64d43ff0c18 71 void gpio_irqB(void) {handle_interrupt_in(PortB, 32);}
mbed_official 146:f64d43ff0c18 72 void gpio_irqC(void) {handle_interrupt_in(PortC, 64);}
mbed_official 146:f64d43ff0c18 73 void gpio_irqD(void) {handle_interrupt_in(PortD, 96);}
mbed_official 146:f64d43ff0c18 74 void gpio_irqE(void) {handle_interrupt_in(PortE, 128);}
mbed_official 146:f64d43ff0c18 75
mbed_official 146:f64d43ff0c18 76 int gpio_irq_init(gpio_irq_t *obj, PinName pin, gpio_irq_handler handler, uint32_t id) {
mbed_official 146:f64d43ff0c18 77 if (pin == NC) {
mbed_official 146:f64d43ff0c18 78 return -1;
mbed_official 146:f64d43ff0c18 79 }
mbed_official 146:f64d43ff0c18 80
mbed_official 146:f64d43ff0c18 81 irq_handler = handler;
mbed_official 146:f64d43ff0c18 82 obj->port = pin >> GPIO_PORT_SHIFT;
mbed_official 146:f64d43ff0c18 83 obj->pin = pin & 0x7F;
mbed_official 146:f64d43ff0c18 84
mbed_official 146:f64d43ff0c18 85 uint32_t ch_base, vector;
mbed_official 146:f64d43ff0c18 86 IRQn_Type irq_n;
mbed_official 146:f64d43ff0c18 87 switch (obj->port) {
mbed_official 146:f64d43ff0c18 88 case PortA:
mbed_official 146:f64d43ff0c18 89 ch_base = 0;
mbed_official 146:f64d43ff0c18 90 irq_n = PORTA_IRQn;
mbed_official 146:f64d43ff0c18 91 vector = (uint32_t)gpio_irqA;
mbed_official 146:f64d43ff0c18 92 break;
mbed_official 146:f64d43ff0c18 93 case PortB:
mbed_official 146:f64d43ff0c18 94 ch_base = 32;
mbed_official 146:f64d43ff0c18 95 irq_n = PORTB_IRQn;
mbed_official 146:f64d43ff0c18 96 vector = (uint32_t)gpio_irqB;
mbed_official 146:f64d43ff0c18 97 break;
mbed_official 146:f64d43ff0c18 98 case PortC:
mbed_official 146:f64d43ff0c18 99 ch_base = 64;
mbed_official 146:f64d43ff0c18 100 irq_n = PORTC_IRQn;
mbed_official 146:f64d43ff0c18 101 vector = (uint32_t)gpio_irqC;
mbed_official 146:f64d43ff0c18 102 break;
mbed_official 146:f64d43ff0c18 103 case PortD:
mbed_official 146:f64d43ff0c18 104 ch_base = 96;
mbed_official 146:f64d43ff0c18 105 irq_n = PORTD_IRQn;
mbed_official 146:f64d43ff0c18 106 vector = (uint32_t)gpio_irqD;
mbed_official 146:f64d43ff0c18 107 break;
mbed_official 146:f64d43ff0c18 108 case PortE:
mbed_official 146:f64d43ff0c18 109 ch_base = 128;
mbed_official 146:f64d43ff0c18 110 irq_n = PORTE_IRQn;
mbed_official 146:f64d43ff0c18 111 vector = (uint32_t)gpio_irqE;
mbed_official 146:f64d43ff0c18 112 break;
mbed_official 146:f64d43ff0c18 113
mbed_official 146:f64d43ff0c18 114 default:
mbed_official 158:3121b9889f7b 115 error("gpio_irq only supported on port A-E.");
mbed_official 146:f64d43ff0c18 116 break;
mbed_official 146:f64d43ff0c18 117 }
mbed_official 146:f64d43ff0c18 118 NVIC_SetVector(irq_n, vector);
mbed_official 146:f64d43ff0c18 119 NVIC_EnableIRQ(irq_n);
mbed_official 146:f64d43ff0c18 120
mbed_official 146:f64d43ff0c18 121 obj->ch = ch_base + obj->pin;
mbed_official 146:f64d43ff0c18 122 channel_ids[obj->ch] = id;
mbed_official 146:f64d43ff0c18 123
mbed_official 146:f64d43ff0c18 124 return 0;
mbed_official 146:f64d43ff0c18 125 }
mbed_official 146:f64d43ff0c18 126
mbed_official 146:f64d43ff0c18 127 void gpio_irq_free(gpio_irq_t *obj) {
mbed_official 146:f64d43ff0c18 128 channel_ids[obj->ch] = 0;
mbed_official 146:f64d43ff0c18 129 }
mbed_official 146:f64d43ff0c18 130
mbed_official 146:f64d43ff0c18 131 void gpio_irq_set(gpio_irq_t *obj, gpio_irq_event event, uint32_t enable) {
mbed_official 146:f64d43ff0c18 132 port_interrupt_config_t irq_settings = kPortIntDisabled;
mbed_official 146:f64d43ff0c18 133
mbed_official 146:f64d43ff0c18 134 switch (BR_PORT_PCRn_IRQC(obj->port, obj->pin)) {
mbed_official 146:f64d43ff0c18 135 case IRQ_DISABLED:
mbed_official 146:f64d43ff0c18 136 if (enable)
mbed_official 146:f64d43ff0c18 137 irq_settings = (event == IRQ_RISE) ? (kPortIntRisingEdge) : (kPortIntFallingEdge);
mbed_official 146:f64d43ff0c18 138 break;
mbed_official 146:f64d43ff0c18 139
mbed_official 146:f64d43ff0c18 140 case IRQ_RAISING_EDGE:
mbed_official 146:f64d43ff0c18 141 if (enable) {
mbed_official 146:f64d43ff0c18 142 irq_settings = (event == IRQ_RISE) ? (kPortIntRisingEdge) : (kPortIntEitherEdge);
mbed_official 146:f64d43ff0c18 143 } else {
mbed_official 146:f64d43ff0c18 144 if (event == IRQ_FALL)
mbed_official 146:f64d43ff0c18 145 irq_settings = kPortIntRisingEdge;
mbed_official 146:f64d43ff0c18 146 }
mbed_official 146:f64d43ff0c18 147 break;
mbed_official 146:f64d43ff0c18 148
mbed_official 146:f64d43ff0c18 149 case IRQ_FALLING_EDGE:
mbed_official 146:f64d43ff0c18 150 if (enable) {
mbed_official 146:f64d43ff0c18 151 irq_settings = (event == IRQ_FALL) ? (kPortIntFallingEdge) : (kPortIntEitherEdge);
mbed_official 146:f64d43ff0c18 152 } else {
mbed_official 146:f64d43ff0c18 153 if (event == IRQ_RISE)
mbed_official 146:f64d43ff0c18 154 irq_settings = kPortIntFallingEdge;
mbed_official 146:f64d43ff0c18 155 }
mbed_official 146:f64d43ff0c18 156 break;
mbed_official 146:f64d43ff0c18 157
mbed_official 146:f64d43ff0c18 158 case IRQ_EITHER_EDGE:
mbed_official 146:f64d43ff0c18 159 if (enable) {
mbed_official 146:f64d43ff0c18 160 irq_settings = kPortIntEitherEdge;
mbed_official 146:f64d43ff0c18 161 } else {
mbed_official 146:f64d43ff0c18 162 irq_settings = (event == IRQ_RISE) ? (kPortIntFallingEdge) : (kPortIntRisingEdge);
mbed_official 146:f64d43ff0c18 163 }
mbed_official 146:f64d43ff0c18 164 break;
mbed_official 146:f64d43ff0c18 165 }
mbed_official 146:f64d43ff0c18 166
mbed_official 146:f64d43ff0c18 167 port_hal_configure_pin_interrupt(obj->port, obj->pin, irq_settings);
mbed_official 146:f64d43ff0c18 168 port_hal_clear_pin_interrupt_flag(obj->port, obj->pin);
mbed_official 146:f64d43ff0c18 169 }
mbed_official 146:f64d43ff0c18 170
mbed_official 146:f64d43ff0c18 171 void gpio_irq_enable(gpio_irq_t *obj) {
mbed_official 146:f64d43ff0c18 172 switch (obj->port) {
mbed_official 146:f64d43ff0c18 173 case PortA:
mbed_official 146:f64d43ff0c18 174 NVIC_EnableIRQ(PORTA_IRQn);
mbed_official 146:f64d43ff0c18 175 break;
mbed_official 146:f64d43ff0c18 176 case PortB:
mbed_official 146:f64d43ff0c18 177 NVIC_EnableIRQ(PORTB_IRQn);
mbed_official 146:f64d43ff0c18 178 break;
mbed_official 146:f64d43ff0c18 179 case PortC:
mbed_official 146:f64d43ff0c18 180 NVIC_EnableIRQ(PORTC_IRQn);
mbed_official 146:f64d43ff0c18 181 break;
mbed_official 146:f64d43ff0c18 182 case PortD:
mbed_official 146:f64d43ff0c18 183 NVIC_EnableIRQ(PORTD_IRQn);
mbed_official 146:f64d43ff0c18 184 break;
mbed_official 146:f64d43ff0c18 185 case PortE:
mbed_official 146:f64d43ff0c18 186 NVIC_EnableIRQ(PORTE_IRQn);
mbed_official 146:f64d43ff0c18 187 break;
mbed_official 146:f64d43ff0c18 188 }
mbed_official 146:f64d43ff0c18 189 }
mbed_official 146:f64d43ff0c18 190
mbed_official 146:f64d43ff0c18 191 void gpio_irq_disable(gpio_irq_t *obj) {
mbed_official 146:f64d43ff0c18 192 switch (obj->port) {
mbed_official 146:f64d43ff0c18 193 case PortA:
mbed_official 146:f64d43ff0c18 194 NVIC_DisableIRQ(PORTA_IRQn);
mbed_official 146:f64d43ff0c18 195 break;
mbed_official 146:f64d43ff0c18 196 case PortB:
mbed_official 146:f64d43ff0c18 197 NVIC_DisableIRQ(PORTB_IRQn);
mbed_official 146:f64d43ff0c18 198 break;
mbed_official 146:f64d43ff0c18 199 case PortC:
mbed_official 146:f64d43ff0c18 200 NVIC_DisableIRQ(PORTC_IRQn);
mbed_official 146:f64d43ff0c18 201 break;
mbed_official 146:f64d43ff0c18 202 case PortD:
mbed_official 146:f64d43ff0c18 203 NVIC_DisableIRQ(PORTD_IRQn);
mbed_official 146:f64d43ff0c18 204 break;
mbed_official 146:f64d43ff0c18 205 case PortE:
mbed_official 146:f64d43ff0c18 206 NVIC_DisableIRQ(PORTE_IRQn);
mbed_official 146:f64d43ff0c18 207 break;
mbed_official 146:f64d43ff0c18 208 }
mbed_official 146:f64d43ff0c18 209 }
mbed_official 146:f64d43ff0c18 210
mbed_official 267:8673334f2cbe 211 #endif