mbed library sources
Dependents: frdm_kl05z_gpio_test
Fork of mbed-src by
targets/hal/TARGET_NORDIC/TARGET_MCU_NRF51822/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:
- 300:55638feb26a4
test with CLOCK_SETUP = 0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 85:e1a8e879a6a9 | 1 | /* mbed Microcontroller Library |
mbed_official | 104:a6a92e2e5a92 | 2 | * Copyright (c) 2013 Nordic Semiconductor |
mbed_official | 85:e1a8e879a6a9 | 3 | * |
mbed_official | 85:e1a8e879a6a9 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
mbed_official | 85:e1a8e879a6a9 | 5 | * you may not use this file except in compliance with the License. |
mbed_official | 85:e1a8e879a6a9 | 6 | * You may obtain a copy of the License at |
mbed_official | 85:e1a8e879a6a9 | 7 | * |
mbed_official | 85:e1a8e879a6a9 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
mbed_official | 85:e1a8e879a6a9 | 9 | * |
mbed_official | 85:e1a8e879a6a9 | 10 | * Unless required by applicable law or agreed to in writing, software |
mbed_official | 85:e1a8e879a6a9 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
mbed_official | 85:e1a8e879a6a9 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
mbed_official | 85:e1a8e879a6a9 | 13 | * See the License for the specific language governing permissions and |
mbed_official | 85:e1a8e879a6a9 | 14 | * limitations under the License. |
mbed_official | 85:e1a8e879a6a9 | 15 | */ |
mbed_official | 85:e1a8e879a6a9 | 16 | #include <stddef.h> |
mbed_official | 85:e1a8e879a6a9 | 17 | #include "cmsis.h" |
mbed_official | 85:e1a8e879a6a9 | 18 | |
mbed_official | 85:e1a8e879a6a9 | 19 | #include "gpio_irq_api.h" |
mbed_official | 285:31249416b6f9 | 20 | #include "mbed_error.h" |
mbed_official | 85:e1a8e879a6a9 | 21 | |
mbed_official | 85:e1a8e879a6a9 | 22 | #define CHANNEL_NUM 31 |
mbed_official | 85:e1a8e879a6a9 | 23 | |
mbed_official | 85:e1a8e879a6a9 | 24 | static uint32_t channel_ids[CHANNEL_NUM] = {0}; //each pin will be given an id, if id is 0 the pin can be ignored. |
mbed_official | 85:e1a8e879a6a9 | 25 | static uint8_t channel_enabled[CHANNEL_NUM] = {0}; |
mbed_official | 300:55638feb26a4 | 26 | static uint32_t portRISE = 0; |
mbed_official | 300:55638feb26a4 | 27 | static uint32_t portFALL = 0; |
mbed_official | 85:e1a8e879a6a9 | 28 | static gpio_irq_handler irq_handler; |
mbed_official | 85:e1a8e879a6a9 | 29 | |
mbed_official | 85:e1a8e879a6a9 | 30 | #ifdef __cplusplus |
mbed_official | 85:e1a8e879a6a9 | 31 | extern "C" { |
mbed_official | 300:55638feb26a4 | 32 | #endif |
mbed_official | 300:55638feb26a4 | 33 | void GPIOTE_IRQHandler(void) |
mbed_official | 300:55638feb26a4 | 34 | { |
mbed_official | 300:55638feb26a4 | 35 | volatile uint32_t newVal = NRF_GPIO->IN; |
mbed_official | 300:55638feb26a4 | 36 | |
mbed_official | 300:55638feb26a4 | 37 | if ((NRF_GPIOTE->EVENTS_PORT != 0) && ((NRF_GPIOTE->INTENSET & GPIOTE_INTENSET_PORT_Msk) != 0)) { |
mbed_official | 300:55638feb26a4 | 38 | NRF_GPIOTE->EVENTS_PORT = 0; |
mbed_official | 300:55638feb26a4 | 39 | |
mbed_official | 300:55638feb26a4 | 40 | for (uint8_t i = 0; i<31; i++) { |
mbed_official | 300:55638feb26a4 | 41 | if (channel_ids[i]>0) { |
mbed_official | 300:55638feb26a4 | 42 | if (channel_enabled[i]) { |
mbed_official | 85:e1a8e879a6a9 | 43 | if( ((newVal>>i)&1) && ( ( (NRF_GPIO->PIN_CNF[i] >>GPIO_PIN_CNF_SENSE_Pos) & GPIO_PIN_CNF_SENSE_Low) != GPIO_PIN_CNF_SENSE_Low) && ( (portRISE>>i)&1) ){ |
mbed_official | 300:55638feb26a4 | 44 | irq_handler(channel_ids[i], IRQ_RISE); |
mbed_official | 300:55638feb26a4 | 45 | } else if ((((newVal >> i) & 1) == 0) && |
mbed_official | 300:55638feb26a4 | 46 | (((NRF_GPIO->PIN_CNF[i] >> GPIO_PIN_CNF_SENSE_Pos) & GPIO_PIN_CNF_SENSE_Low) == GPIO_PIN_CNF_SENSE_Low) && |
mbed_official | 300:55638feb26a4 | 47 | ((portFALL >> i) & 1)) { |
mbed_official | 300:55638feb26a4 | 48 | irq_handler(channel_ids[i], IRQ_FALL); |
mbed_official | 300:55638feb26a4 | 49 | } |
mbed_official | 85:e1a8e879a6a9 | 50 | } |
mbed_official | 300:55638feb26a4 | 51 | |
mbed_official | 300:55638feb26a4 | 52 | if (NRF_GPIO->PIN_CNF[i] & GPIO_PIN_CNF_SENSE_Msk) { |
mbed_official | 85:e1a8e879a6a9 | 53 | NRF_GPIO->PIN_CNF[i] &= ~(GPIO_PIN_CNF_SENSE_Msk); |
mbed_official | 300:55638feb26a4 | 54 | |
mbed_official | 300:55638feb26a4 | 55 | if (newVal >> i & 1) { |
mbed_official | 300:55638feb26a4 | 56 | NRF_GPIO->PIN_CNF[i] |= (GPIO_PIN_CNF_SENSE_Low << GPIO_PIN_CNF_SENSE_Pos); |
mbed_official | 300:55638feb26a4 | 57 | } else { |
mbed_official | 300:55638feb26a4 | 58 | NRF_GPIO->PIN_CNF[i] |= (GPIO_PIN_CNF_SENSE_High << GPIO_PIN_CNF_SENSE_Pos); |
mbed_official | 85:e1a8e879a6a9 | 59 | } |
mbed_official | 85:e1a8e879a6a9 | 60 | } |
mbed_official | 85:e1a8e879a6a9 | 61 | } |
mbed_official | 85:e1a8e879a6a9 | 62 | } |
mbed_official | 85:e1a8e879a6a9 | 63 | } |
mbed_official | 85:e1a8e879a6a9 | 64 | } |
mbed_official | 85:e1a8e879a6a9 | 65 | |
mbed_official | 300:55638feb26a4 | 66 | #ifdef __cplusplus |
mbed_official | 300:55638feb26a4 | 67 | } |
mbed_official | 300:55638feb26a4 | 68 | #endif |
mbed_official | 300:55638feb26a4 | 69 | |
mbed_official | 300:55638feb26a4 | 70 | int gpio_irq_init(gpio_irq_t *obj, PinName pin, gpio_irq_handler handler, uint32_t id) |
mbed_official | 300:55638feb26a4 | 71 | { |
mbed_official | 300:55638feb26a4 | 72 | if (pin == NC) { |
mbed_official | 300:55638feb26a4 | 73 | return -1; |
mbed_official | 300:55638feb26a4 | 74 | } |
mbed_official | 300:55638feb26a4 | 75 | |
mbed_official | 300:55638feb26a4 | 76 | irq_handler = handler; |
mbed_official | 300:55638feb26a4 | 77 | obj->ch = pin; |
mbed_official | 300:55638feb26a4 | 78 | NRF_GPIOTE->EVENTS_PORT = 0; |
mbed_official | 300:55638feb26a4 | 79 | channel_ids[pin] = id; |
mbed_official | 300:55638feb26a4 | 80 | channel_enabled[pin] = 1; |
mbed_official | 300:55638feb26a4 | 81 | NRF_GPIOTE->INTENSET = GPIOTE_INTENSET_PORT_Set << GPIOTE_INTENSET_PORT_Pos; |
mbed_official | 300:55638feb26a4 | 82 | |
mbed_official | 300:55638feb26a4 | 83 | NVIC_SetPriority(GPIOTE_IRQn, 3); |
mbed_official | 300:55638feb26a4 | 84 | NVIC_EnableIRQ (GPIOTE_IRQn); |
mbed_official | 300:55638feb26a4 | 85 | return 0; |
mbed_official | 300:55638feb26a4 | 86 | } |
mbed_official | 300:55638feb26a4 | 87 | |
mbed_official | 300:55638feb26a4 | 88 | void gpio_irq_free(gpio_irq_t *obj) |
mbed_official | 300:55638feb26a4 | 89 | { |
mbed_official | 300:55638feb26a4 | 90 | channel_ids[obj->ch] = 0; |
mbed_official | 300:55638feb26a4 | 91 | } |
mbed_official | 300:55638feb26a4 | 92 | |
mbed_official | 300:55638feb26a4 | 93 | void gpio_irq_set(gpio_irq_t *obj, gpio_irq_event event, uint32_t enable) |
mbed_official | 300:55638feb26a4 | 94 | { |
mbed_official | 300:55638feb26a4 | 95 | NRF_GPIO->PIN_CNF[obj->ch] &= ~(GPIO_PIN_CNF_SENSE_Msk); |
mbed_official | 300:55638feb26a4 | 96 | if (enable) { |
mbed_official | 300:55638feb26a4 | 97 | if (event == IRQ_RISE) { |
mbed_official | 300:55638feb26a4 | 98 | portRISE |= (1 << obj->ch); |
mbed_official | 300:55638feb26a4 | 99 | } else if (event == IRQ_FALL) { |
mbed_official | 300:55638feb26a4 | 100 | portFALL |= (1 << obj->ch); |
mbed_official | 300:55638feb26a4 | 101 | } |
mbed_official | 300:55638feb26a4 | 102 | } else { |
mbed_official | 300:55638feb26a4 | 103 | if (event == IRQ_RISE) { |
mbed_official | 300:55638feb26a4 | 104 | portRISE &= ~(1 << obj->ch); |
mbed_official | 300:55638feb26a4 | 105 | } else if (event == IRQ_FALL) { |
mbed_official | 300:55638feb26a4 | 106 | portFALL &= ~(1 << obj->ch); |
mbed_official | 300:55638feb26a4 | 107 | } |
mbed_official | 300:55638feb26a4 | 108 | } |
mbed_official | 300:55638feb26a4 | 109 | |
mbed_official | 300:55638feb26a4 | 110 | if (((portRISE >> obj->ch) & 1) || ((portFALL >> obj->ch) & 1)) { |
mbed_official | 300:55638feb26a4 | 111 | if ((NRF_GPIO->IN >> obj->ch) & 1) { |
mbed_official | 300:55638feb26a4 | 112 | NRF_GPIO->PIN_CNF[obj->ch] |= (GPIO_PIN_CNF_SENSE_Low << GPIO_PIN_CNF_SENSE_Pos); // | (GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos); |
mbed_official | 300:55638feb26a4 | 113 | } else { |
mbed_official | 300:55638feb26a4 | 114 | NRF_GPIO->PIN_CNF[obj->ch] |= (GPIO_PIN_CNF_SENSE_High << GPIO_PIN_CNF_SENSE_Pos); //| (GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos); |
mbed_official | 300:55638feb26a4 | 115 | } |
mbed_official | 300:55638feb26a4 | 116 | } |
mbed_official | 300:55638feb26a4 | 117 | } |
mbed_official | 300:55638feb26a4 | 118 | |
mbed_official | 300:55638feb26a4 | 119 | void gpio_irq_enable(gpio_irq_t *obj) |
mbed_official | 300:55638feb26a4 | 120 | { |
mbed_official | 85:e1a8e879a6a9 | 121 | channel_enabled[obj->ch] = 1; |
mbed_official | 85:e1a8e879a6a9 | 122 | } |
mbed_official | 85:e1a8e879a6a9 | 123 | |
mbed_official | 300:55638feb26a4 | 124 | void gpio_irq_disable(gpio_irq_t *obj) |
mbed_official | 300:55638feb26a4 | 125 | { |
mbed_official | 85:e1a8e879a6a9 | 126 | channel_enabled[obj->ch] = 0; |
mbed_official | 85:e1a8e879a6a9 | 127 | } |