mbed library sources, include can_api for nucleo-f091rc

Dependents:   CanNucleoF0_example

Fork of mbed-src by mbed official

Committer:
ptpaterson
Date:
Thu Jan 07 05:49:05 2016 +0000
Revision:
645:13c87cbecd54
Parent:
515:7467ef1f4ad8
corrected freeze on CAN_RECEIVE_IT

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 507:d4fc7603a669 1 /*******************************************************************************
mbed_official 507:d4fc7603a669 2 * Copyright (C) 2015 Maxim Integrated Products, Inc., All Rights Reserved.
mbed_official 507:d4fc7603a669 3 *
mbed_official 507:d4fc7603a669 4 * Permission is hereby granted, free of charge, to any person obtaining a
mbed_official 507:d4fc7603a669 5 * copy of this software and associated documentation files (the "Software"),
mbed_official 507:d4fc7603a669 6 * to deal in the Software without restriction, including without limitation
mbed_official 507:d4fc7603a669 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
mbed_official 507:d4fc7603a669 8 * and/or sell copies of the Software, and to permit persons to whom the
mbed_official 507:d4fc7603a669 9 * Software is furnished to do so, subject to the following conditions:
mbed_official 507:d4fc7603a669 10 *
mbed_official 507:d4fc7603a669 11 * The above copyright notice and this permission notice shall be included
mbed_official 507:d4fc7603a669 12 * in all copies or substantial portions of the Software.
mbed_official 507:d4fc7603a669 13 *
mbed_official 507:d4fc7603a669 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
mbed_official 507:d4fc7603a669 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
mbed_official 507:d4fc7603a669 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
mbed_official 507:d4fc7603a669 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
mbed_official 507:d4fc7603a669 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
mbed_official 507:d4fc7603a669 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
mbed_official 507:d4fc7603a669 20 * OTHER DEALINGS IN THE SOFTWARE.
mbed_official 507:d4fc7603a669 21 *
mbed_official 507:d4fc7603a669 22 * Except as contained in this notice, the name of Maxim Integrated
mbed_official 507:d4fc7603a669 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
mbed_official 507:d4fc7603a669 24 * Products, Inc. Branding Policy.
mbed_official 507:d4fc7603a669 25 *
mbed_official 507:d4fc7603a669 26 * The mere transfer of this software does not imply any licenses
mbed_official 507:d4fc7603a669 27 * of trade secrets, proprietary technology, copyrights, patents,
mbed_official 507:d4fc7603a669 28 * trademarks, maskwork rights, or any other form of intellectual
mbed_official 507:d4fc7603a669 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
mbed_official 507:d4fc7603a669 30 * ownership rights.
mbed_official 507:d4fc7603a669 31 *******************************************************************************
mbed_official 507:d4fc7603a669 32 */
mbed_official 507:d4fc7603a669 33
mbed_official 507:d4fc7603a669 34 #include <stddef.h>
mbed_official 507:d4fc7603a669 35 #include "cmsis.h"
mbed_official 507:d4fc7603a669 36 #include "gpio_irq_api.h"
mbed_official 507:d4fc7603a669 37 #include "mbed_error.h"
mbed_official 507:d4fc7603a669 38
mbed_official 507:d4fc7603a669 39 #define NUM_PORTS 3
mbed_official 507:d4fc7603a669 40 #define NUM_PINS_PER_PORT 8
mbed_official 507:d4fc7603a669 41
mbed_official 507:d4fc7603a669 42 static uint32_t ids[NUM_PORTS][NUM_PINS_PER_PORT] = {{0}};
mbed_official 507:d4fc7603a669 43 static gpio_irq_handler irq_handler;
mbed_official 507:d4fc7603a669 44
mbed_official 507:d4fc7603a669 45 static void handle_irq(unsigned int port)
mbed_official 507:d4fc7603a669 46 {
mbed_official 507:d4fc7603a669 47 uint32_t intfl, in_val;
mbed_official 507:d4fc7603a669 48 uint32_t mask;
mbed_official 507:d4fc7603a669 49 unsigned int pin;
mbed_official 507:d4fc7603a669 50
mbed_official 507:d4fc7603a669 51 /* Read pin state */
mbed_official 507:d4fc7603a669 52 in_val = MXC_GPIO->in_val[port];
mbed_official 507:d4fc7603a669 53
mbed_official 507:d4fc7603a669 54 /* Read interrupts */
mbed_official 507:d4fc7603a669 55 intfl = MXC_GPIO->intfl[port] & MXC_GPIO->inten[port];
mbed_official 507:d4fc7603a669 56
mbed_official 507:d4fc7603a669 57 mask = 1;
mbed_official 507:d4fc7603a669 58
mbed_official 507:d4fc7603a669 59 for (pin = 0; pin < NUM_PINS_PER_PORT; pin++) {
mbed_official 507:d4fc7603a669 60 if (intfl & mask) {
mbed_official 507:d4fc7603a669 61 if (ids[port][pin]) {
mbed_official 507:d4fc7603a669 62 if (in_val & mask) {
mbed_official 507:d4fc7603a669 63 irq_handler(ids[port][pin], IRQ_RISE);
mbed_official 507:d4fc7603a669 64 } else {
mbed_official 507:d4fc7603a669 65 irq_handler(ids[port][pin], IRQ_FALL);
mbed_official 507:d4fc7603a669 66 }
mbed_official 507:d4fc7603a669 67 }
mbed_official 507:d4fc7603a669 68 MXC_GPIO->intfl[port] = mask; /* clear interrupt */
mbed_official 507:d4fc7603a669 69 }
mbed_official 507:d4fc7603a669 70 mask <<= 1;
mbed_official 507:d4fc7603a669 71 }
mbed_official 507:d4fc7603a669 72 }
mbed_official 507:d4fc7603a669 73
mbed_official 507:d4fc7603a669 74 void gpio_irq_0(void)
mbed_official 507:d4fc7603a669 75 {
mbed_official 507:d4fc7603a669 76 handle_irq(0);
mbed_official 507:d4fc7603a669 77 }
mbed_official 507:d4fc7603a669 78
mbed_official 507:d4fc7603a669 79 void gpio_irq_1(void)
mbed_official 507:d4fc7603a669 80 {
mbed_official 507:d4fc7603a669 81 handle_irq(1);
mbed_official 507:d4fc7603a669 82 }
mbed_official 507:d4fc7603a669 83
mbed_official 507:d4fc7603a669 84 void gpio_irq_2(void)
mbed_official 507:d4fc7603a669 85 {
mbed_official 507:d4fc7603a669 86 handle_irq(2);
mbed_official 507:d4fc7603a669 87 }
mbed_official 507:d4fc7603a669 88
mbed_official 507:d4fc7603a669 89 int gpio_irq_init(gpio_irq_t *obj, PinName name, gpio_irq_handler handler, uint32_t id)
mbed_official 507:d4fc7603a669 90 {
mbed_official 507:d4fc7603a669 91 if (name == NC)
mbed_official 507:d4fc7603a669 92 return -1;
mbed_official 507:d4fc7603a669 93
mbed_official 507:d4fc7603a669 94 uint8_t port = PINNAME_TO_PORT(name);
mbed_official 507:d4fc7603a669 95 uint8_t pin = PINNAME_TO_PIN(name);
mbed_official 507:d4fc7603a669 96
mbed_official 507:d4fc7603a669 97 if ((port > NUM_PORTS) || (pin > NUM_PINS_PER_PORT)) {
mbed_official 507:d4fc7603a669 98 return 1;
mbed_official 507:d4fc7603a669 99 }
mbed_official 507:d4fc7603a669 100
mbed_official 507:d4fc7603a669 101 obj->port = port;
mbed_official 507:d4fc7603a669 102 obj->pin = pin;
mbed_official 507:d4fc7603a669 103
mbed_official 507:d4fc7603a669 104 irq_handler = handler;
mbed_official 507:d4fc7603a669 105
mbed_official 507:d4fc7603a669 106 ids[port][pin] = id;
mbed_official 507:d4fc7603a669 107
mbed_official 507:d4fc7603a669 108 /* register handlers */
mbed_official 507:d4fc7603a669 109 NVIC_SetVector(GPIO_P0_IRQn, (uint32_t)gpio_irq_0);
mbed_official 507:d4fc7603a669 110 NVIC_SetVector(GPIO_P1_IRQn, (uint32_t)gpio_irq_1);
mbed_official 507:d4fc7603a669 111 NVIC_SetVector(GPIO_P2_IRQn, (uint32_t)gpio_irq_2);
mbed_official 507:d4fc7603a669 112
mbed_official 507:d4fc7603a669 113 /* disable the interrupt locally */
mbed_official 507:d4fc7603a669 114 MXC_GPIO->int_mode[port] &= ~(0xF << (pin*4));
mbed_official 507:d4fc7603a669 115
mbed_official 507:d4fc7603a669 116 /* clear a pending request */
mbed_official 507:d4fc7603a669 117 MXC_GPIO->intfl[port] = 1 << pin;
mbed_official 507:d4fc7603a669 118
mbed_official 507:d4fc7603a669 119 /* enable the requested interrupt */
mbed_official 507:d4fc7603a669 120 MXC_GPIO->inten[port] |= (1 << pin);
mbed_official 515:7467ef1f4ad8 121 NVIC_EnableIRQ((IRQn_Type)((uint32_t)GPIO_P0_IRQn + port));
mbed_official 507:d4fc7603a669 122
mbed_official 507:d4fc7603a669 123 return 0;
mbed_official 507:d4fc7603a669 124 }
mbed_official 507:d4fc7603a669 125
mbed_official 507:d4fc7603a669 126 void gpio_irq_free(gpio_irq_t *obj)
mbed_official 507:d4fc7603a669 127 {
mbed_official 507:d4fc7603a669 128 /* disable interrupt */
mbed_official 507:d4fc7603a669 129 MXC_GPIO->inten[obj->port] &= ~(1 << obj->pin);
mbed_official 507:d4fc7603a669 130 MXC_GPIO->int_mode[obj->port] &= ~(0xF << (obj->pin*4));
mbed_official 507:d4fc7603a669 131
mbed_official 507:d4fc7603a669 132 ids[obj->port][obj->pin] = 0;
mbed_official 507:d4fc7603a669 133 }
mbed_official 507:d4fc7603a669 134
mbed_official 507:d4fc7603a669 135 void gpio_irq_set(gpio_irq_t *obj, gpio_irq_event event, uint32_t enable)
mbed_official 507:d4fc7603a669 136 {
mbed_official 507:d4fc7603a669 137 uint32_t int_mode = MXC_GPIO->int_mode[obj->port];
mbed_official 507:d4fc7603a669 138 uint32_t curr_mode = (int_mode >> (obj->pin*4)) & 0x3; /* only supporting edge interrupts */
mbed_official 507:d4fc7603a669 139
mbed_official 507:d4fc7603a669 140 uint32_t new_mode = curr_mode;
mbed_official 507:d4fc7603a669 141 if (event == IRQ_FALL) {
mbed_official 507:d4fc7603a669 142 if (enable) {
mbed_official 507:d4fc7603a669 143 new_mode |= 0x1;
mbed_official 507:d4fc7603a669 144 } else {
mbed_official 507:d4fc7603a669 145 new_mode &= ~0x1;
mbed_official 507:d4fc7603a669 146 }
mbed_official 507:d4fc7603a669 147 } else if (event == IRQ_RISE) {
mbed_official 507:d4fc7603a669 148 if (enable) {
mbed_official 507:d4fc7603a669 149 new_mode |= 0x2;
mbed_official 507:d4fc7603a669 150 } else {
mbed_official 507:d4fc7603a669 151 new_mode &= ~0x2;
mbed_official 507:d4fc7603a669 152 }
mbed_official 507:d4fc7603a669 153 }
mbed_official 507:d4fc7603a669 154
mbed_official 507:d4fc7603a669 155 int_mode &= ~(0xF << (obj->pin*4));
mbed_official 507:d4fc7603a669 156 int_mode |= (new_mode << (obj->pin*4));
mbed_official 507:d4fc7603a669 157 MXC_GPIO->int_mode[obj->port] = int_mode;
mbed_official 507:d4fc7603a669 158 }
mbed_official 507:d4fc7603a669 159
mbed_official 507:d4fc7603a669 160 void gpio_irq_enable(gpio_irq_t *obj)
mbed_official 507:d4fc7603a669 161 {
mbed_official 507:d4fc7603a669 162 MXC_GPIO->inten[obj->port] |= (1 << obj->pin);
mbed_official 507:d4fc7603a669 163 }
mbed_official 507:d4fc7603a669 164
mbed_official 507:d4fc7603a669 165 void gpio_irq_disable(gpio_irq_t *obj)
mbed_official 507:d4fc7603a669 166 {
mbed_official 507:d4fc7603a669 167 MXC_GPIO->inten[obj->port] &= ~(1 << obj->pin);
mbed_official 507:d4fc7603a669 168 }