Lancaster University's (short term!) clone of mbed-src for micro:bit. This is a copy of the github branch https://github.com/lancaster-university/mbed-classic

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Wed Jul 08 14:45:08 2015 +0100
Revision:
585:a1ed5b41f74f
Synchronized with git revision 7a2b57896e0263b82f31ddc5a0ad2443615db184

Full URL: https://github.com/mbedmicro/mbed/commit/7a2b57896e0263b82f31ddc5a0ad2443615db184/

Add rtc_api.c

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 585:a1ed5b41f74f 1 /* mbed Microcontroller Library
mbed_official 585:a1ed5b41f74f 2 * Copyright (c) 2006-2013 ARM Limited
mbed_official 585:a1ed5b41f74f 3 *
mbed_official 585:a1ed5b41f74f 4 * Licensed under the Apache License, Version 2.0 (the "License");
mbed_official 585:a1ed5b41f74f 5 * you may not use this file except in compliance with the License.
mbed_official 585:a1ed5b41f74f 6 * You may obtain a copy of the License at
mbed_official 585:a1ed5b41f74f 7 *
mbed_official 585:a1ed5b41f74f 8 * http://www.apache.org/licenses/LICENSE-2.0
mbed_official 585:a1ed5b41f74f 9 *
mbed_official 585:a1ed5b41f74f 10 * Unless required by applicable law or agreed to in writing, software
mbed_official 585:a1ed5b41f74f 11 * distributed under the License is distributed on an "AS IS" BASIS,
mbed_official 585:a1ed5b41f74f 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mbed_official 585:a1ed5b41f74f 13 * See the License for the specific language governing permissions and
mbed_official 585:a1ed5b41f74f 14 * limitations under the License.
mbed_official 585:a1ed5b41f74f 15 */
mbed_official 585:a1ed5b41f74f 16 #include <stddef.h>
mbed_official 585:a1ed5b41f74f 17 #include "cmsis.h"
mbed_official 585:a1ed5b41f74f 18
mbed_official 585:a1ed5b41f74f 19 #include "gpio_irq_api.h"
mbed_official 585:a1ed5b41f74f 20 #include "gpio_api.h"
mbed_official 585:a1ed5b41f74f 21 #include "mbed_error.h"
mbed_official 585:a1ed5b41f74f 22
mbed_official 585:a1ed5b41f74f 23 #define CHANNEL_NUM 64
mbed_official 585:a1ed5b41f74f 24
mbed_official 585:a1ed5b41f74f 25 static uint32_t channel_ids[CHANNEL_NUM] = {0};
mbed_official 585:a1ed5b41f74f 26 static gpio_irq_handler irq_handler;
mbed_official 585:a1ed5b41f74f 27
mbed_official 585:a1ed5b41f74f 28 #define IRQ_DISABLED (0)
mbed_official 585:a1ed5b41f74f 29 #define IRQ_RAISING_EDGE PORT_PCR_IRQC(9)
mbed_official 585:a1ed5b41f74f 30 #define IRQ_FALLING_EDGE PORT_PCR_IRQC(10)
mbed_official 585:a1ed5b41f74f 31 #define IRQ_EITHER_EDGE PORT_PCR_IRQC(11)
mbed_official 585:a1ed5b41f74f 32
mbed_official 585:a1ed5b41f74f 33 const uint32_t search_bits[] = {0x0000FFFF, 0x000000FF, 0x0000000F, 0x00000003, 0x00000001};
mbed_official 585:a1ed5b41f74f 34
mbed_official 585:a1ed5b41f74f 35 static void handle_interrupt_in(PORT_Type *port, int ch_base) {
mbed_official 585:a1ed5b41f74f 36 uint32_t isfr;
mbed_official 585:a1ed5b41f74f 37 uint8_t location;
mbed_official 585:a1ed5b41f74f 38
mbed_official 585:a1ed5b41f74f 39 while((isfr = port->ISFR) != 0) {
mbed_official 585:a1ed5b41f74f 40 location = 0;
mbed_official 585:a1ed5b41f74f 41 for (int i = 0; i < 5; i++) {
mbed_official 585:a1ed5b41f74f 42 if (!(isfr & (search_bits[i] << location)))
mbed_official 585:a1ed5b41f74f 43 location += 1 << (4 - i);
mbed_official 585:a1ed5b41f74f 44 }
mbed_official 585:a1ed5b41f74f 45
mbed_official 585:a1ed5b41f74f 46 uint32_t id = channel_ids[ch_base + location];
mbed_official 585:a1ed5b41f74f 47 if (id == 0) {
mbed_official 585:a1ed5b41f74f 48 continue;
mbed_official 585:a1ed5b41f74f 49 }
mbed_official 585:a1ed5b41f74f 50
mbed_official 585:a1ed5b41f74f 51 FGPIO_Type *gpio;
mbed_official 585:a1ed5b41f74f 52 gpio_irq_event event = IRQ_NONE;
mbed_official 585:a1ed5b41f74f 53 switch (port->PCR[location] & PORT_PCR_IRQC_MASK) {
mbed_official 585:a1ed5b41f74f 54 case IRQ_RAISING_EDGE:
mbed_official 585:a1ed5b41f74f 55 event = IRQ_RISE;
mbed_official 585:a1ed5b41f74f 56 break;
mbed_official 585:a1ed5b41f74f 57
mbed_official 585:a1ed5b41f74f 58 case IRQ_FALLING_EDGE:
mbed_official 585:a1ed5b41f74f 59 event = IRQ_FALL;
mbed_official 585:a1ed5b41f74f 60 break;
mbed_official 585:a1ed5b41f74f 61
mbed_official 585:a1ed5b41f74f 62 case IRQ_EITHER_EDGE:
mbed_official 585:a1ed5b41f74f 63 gpio = (port == PORTA) ? (FPTA) : (FPTD);
mbed_official 585:a1ed5b41f74f 64 event = (gpio->PDIR & (1 << location)) ? (IRQ_RISE) : (IRQ_FALL);
mbed_official 585:a1ed5b41f74f 65 break;
mbed_official 585:a1ed5b41f74f 66 }
mbed_official 585:a1ed5b41f74f 67 if (event != IRQ_NONE) {
mbed_official 585:a1ed5b41f74f 68 irq_handler(id, event);
mbed_official 585:a1ed5b41f74f 69 }
mbed_official 585:a1ed5b41f74f 70 port->ISFR = 1 << location;
mbed_official 585:a1ed5b41f74f 71 }
mbed_official 585:a1ed5b41f74f 72 }
mbed_official 585:a1ed5b41f74f 73
mbed_official 585:a1ed5b41f74f 74 void gpio_irqA(void) {handle_interrupt_in(PORTA, 0);}
mbed_official 585:a1ed5b41f74f 75 void gpio_irqD(void) {handle_interrupt_in(PORTD, 32);}
mbed_official 585:a1ed5b41f74f 76
mbed_official 585:a1ed5b41f74f 77 int gpio_irq_init(gpio_irq_t *obj, PinName pin, gpio_irq_handler handler, uint32_t id) {
mbed_official 585:a1ed5b41f74f 78 if (pin == NC) return -1;
mbed_official 585:a1ed5b41f74f 79
mbed_official 585:a1ed5b41f74f 80 irq_handler = handler;
mbed_official 585:a1ed5b41f74f 81
mbed_official 585:a1ed5b41f74f 82 obj->port = pin >> PORT_SHIFT;
mbed_official 585:a1ed5b41f74f 83 obj->pin = (pin & 0x7F) >> 2;
mbed_official 585:a1ed5b41f74f 84
mbed_official 585:a1ed5b41f74f 85 uint32_t ch_base, vector;
mbed_official 585:a1ed5b41f74f 86 IRQn_Type irq_n;
mbed_official 585:a1ed5b41f74f 87 switch (obj->port) {
mbed_official 585:a1ed5b41f74f 88 case PortA:
mbed_official 585:a1ed5b41f74f 89 ch_base = 0; irq_n = PORTA_IRQn; vector = (uint32_t)gpio_irqA;
mbed_official 585:a1ed5b41f74f 90 break;
mbed_official 585:a1ed5b41f74f 91
mbed_official 585:a1ed5b41f74f 92 case PortD:
mbed_official 585:a1ed5b41f74f 93 ch_base = 32; irq_n = PORTD_IRQn; vector = (uint32_t)gpio_irqD;
mbed_official 585:a1ed5b41f74f 94 break;
mbed_official 585:a1ed5b41f74f 95
mbed_official 585:a1ed5b41f74f 96 default:
mbed_official 585:a1ed5b41f74f 97 error("gpio_irq only supported on port A and D");
mbed_official 585:a1ed5b41f74f 98 break;
mbed_official 585:a1ed5b41f74f 99 }
mbed_official 585:a1ed5b41f74f 100 NVIC_SetVector(irq_n, vector);
mbed_official 585:a1ed5b41f74f 101 NVIC_EnableIRQ(irq_n);
mbed_official 585:a1ed5b41f74f 102
mbed_official 585:a1ed5b41f74f 103 obj->ch = ch_base + obj->pin;
mbed_official 585:a1ed5b41f74f 104 channel_ids[obj->ch] = id;
mbed_official 585:a1ed5b41f74f 105
mbed_official 585:a1ed5b41f74f 106 return 0;
mbed_official 585:a1ed5b41f74f 107 }
mbed_official 585:a1ed5b41f74f 108
mbed_official 585:a1ed5b41f74f 109 void gpio_irq_free(gpio_irq_t *obj) {
mbed_official 585:a1ed5b41f74f 110 channel_ids[obj->ch] = 0;
mbed_official 585:a1ed5b41f74f 111 }
mbed_official 585:a1ed5b41f74f 112
mbed_official 585:a1ed5b41f74f 113 void gpio_irq_set(gpio_irq_t *obj, gpio_irq_event event, uint32_t enable) {
mbed_official 585:a1ed5b41f74f 114 PORT_Type *port = (PORT_Type *)(PORTA_BASE + 0x1000 * obj->port);
mbed_official 585:a1ed5b41f74f 115
mbed_official 585:a1ed5b41f74f 116 uint32_t irq_settings = IRQ_DISABLED;
mbed_official 585:a1ed5b41f74f 117
mbed_official 585:a1ed5b41f74f 118 switch (port->PCR[obj->pin] & PORT_PCR_IRQC_MASK) {
mbed_official 585:a1ed5b41f74f 119 case IRQ_DISABLED:
mbed_official 585:a1ed5b41f74f 120 if (enable) {
mbed_official 585:a1ed5b41f74f 121 irq_settings = (event == IRQ_RISE) ? (IRQ_RAISING_EDGE) : (IRQ_FALLING_EDGE);
mbed_official 585:a1ed5b41f74f 122 }
mbed_official 585:a1ed5b41f74f 123 break;
mbed_official 585:a1ed5b41f74f 124
mbed_official 585:a1ed5b41f74f 125 case IRQ_RAISING_EDGE:
mbed_official 585:a1ed5b41f74f 126 if (enable) {
mbed_official 585:a1ed5b41f74f 127 irq_settings = (event == IRQ_RISE) ? (IRQ_RAISING_EDGE) : (IRQ_EITHER_EDGE);
mbed_official 585:a1ed5b41f74f 128 } else {
mbed_official 585:a1ed5b41f74f 129 if (event == IRQ_FALL)
mbed_official 585:a1ed5b41f74f 130 irq_settings = IRQ_RAISING_EDGE;
mbed_official 585:a1ed5b41f74f 131 }
mbed_official 585:a1ed5b41f74f 132 break;
mbed_official 585:a1ed5b41f74f 133
mbed_official 585:a1ed5b41f74f 134 case IRQ_FALLING_EDGE:
mbed_official 585:a1ed5b41f74f 135 if (enable) {
mbed_official 585:a1ed5b41f74f 136 irq_settings = (event == IRQ_FALL) ? (IRQ_FALLING_EDGE) : (IRQ_EITHER_EDGE);
mbed_official 585:a1ed5b41f74f 137 } else {
mbed_official 585:a1ed5b41f74f 138 if (event == IRQ_RISE)
mbed_official 585:a1ed5b41f74f 139 irq_settings = IRQ_FALLING_EDGE;
mbed_official 585:a1ed5b41f74f 140 }
mbed_official 585:a1ed5b41f74f 141 break;
mbed_official 585:a1ed5b41f74f 142
mbed_official 585:a1ed5b41f74f 143 case IRQ_EITHER_EDGE:
mbed_official 585:a1ed5b41f74f 144 if (enable) {
mbed_official 585:a1ed5b41f74f 145 irq_settings = IRQ_EITHER_EDGE;
mbed_official 585:a1ed5b41f74f 146 } else {
mbed_official 585:a1ed5b41f74f 147 irq_settings = (event == IRQ_RISE) ? (IRQ_FALLING_EDGE) : (IRQ_RAISING_EDGE);
mbed_official 585:a1ed5b41f74f 148 }
mbed_official 585:a1ed5b41f74f 149 break;
mbed_official 585:a1ed5b41f74f 150 }
mbed_official 585:a1ed5b41f74f 151
mbed_official 585:a1ed5b41f74f 152 // Interrupt configuration and clear interrupt
mbed_official 585:a1ed5b41f74f 153 port->PCR[obj->pin] = (port->PCR[obj->pin] & ~PORT_PCR_IRQC_MASK) | irq_settings | PORT_PCR_ISF_MASK;
mbed_official 585:a1ed5b41f74f 154 }
mbed_official 585:a1ed5b41f74f 155
mbed_official 585:a1ed5b41f74f 156 void gpio_irq_enable(gpio_irq_t *obj) {
mbed_official 585:a1ed5b41f74f 157 if (obj->port == PortA) {
mbed_official 585:a1ed5b41f74f 158 NVIC_EnableIRQ(PORTA_IRQn);
mbed_official 585:a1ed5b41f74f 159 } else if (obj->port == PortD) {
mbed_official 585:a1ed5b41f74f 160 NVIC_EnableIRQ(PORTD_IRQn);
mbed_official 585:a1ed5b41f74f 161 }
mbed_official 585:a1ed5b41f74f 162 }
mbed_official 585:a1ed5b41f74f 163
mbed_official 585:a1ed5b41f74f 164 void gpio_irq_disable(gpio_irq_t *obj) {
mbed_official 585:a1ed5b41f74f 165 if (obj->port == PortA) {
mbed_official 585:a1ed5b41f74f 166 NVIC_DisableIRQ(PORTA_IRQn);
mbed_official 585:a1ed5b41f74f 167 } else if (obj->port == PortD) {
mbed_official 585:a1ed5b41f74f 168 NVIC_DisableIRQ(PORTD_IRQn);
mbed_official 585:a1ed5b41f74f 169 }
mbed_official 585:a1ed5b41f74f 170 }