aboutsummaryrefslogtreecommitdiffstats
path: root/libmaple/exti.c
blob: 496f0a1ed4364739bc6bdae7a343410bea4843a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/******************************************************************************
 * The MIT License
 *
 * Copyright (c) 2010 Perry Hung.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *****************************************************************************/

/**
 * @brief External interrupt control routines
 */

#include "libmaple.h"
#include "exti.h"
#include "nvic.h"

typedef struct ExtIChannel {
    void (*handler)(void);
    uint32 irq_line;
} ExtIChannel;

static ExtIChannel exti_channels[] = {
    { .handler = NULL, .irq_line = NVIC_EXTI0     },  // EXTI0
    { .handler = NULL, .irq_line = NVIC_EXTI1     },  // EXTI1
    { .handler = NULL, .irq_line = NVIC_EXTI2     },  // EXTI2
    { .handler = NULL, .irq_line = NVIC_EXTI3     },  // EXTI3
    { .handler = NULL, .irq_line = NVIC_EXTI4     },  // EXTI4
    { .handler = NULL, .irq_line = NVIC_EXTI9_5   },  // EXTI5
    { .handler = NULL, .irq_line = NVIC_EXTI9_5   },  // EXTI6
    { .handler = NULL, .irq_line = NVIC_EXTI9_5   },  // EXTI7
    { .handler = NULL, .irq_line = NVIC_EXTI9_5   },  // EXTI8
    { .handler = NULL, .irq_line = NVIC_EXTI9_5   },  // EXTI9
    { .handler = NULL, .irq_line = NVIC_EXTI15_10 },  // EXTI10
    { .handler = NULL, .irq_line = NVIC_EXTI15_10 },  // EXTI11
    { .handler = NULL, .irq_line = NVIC_EXTI15_10 },  // EXTI12
    { .handler = NULL, .irq_line = NVIC_EXTI15_10 },  // EXTI13
    { .handler = NULL, .irq_line = NVIC_EXTI15_10 },  // EXTI14
    { .handler = NULL, .irq_line = NVIC_EXTI15_10 },  // EXTI15
};

static inline void clear_pending(int bit) {
    __set_bits(EXTI_PR, BIT(bit));
    /* If the pending bit is cleared as the last instruction in an ISR,
     * it won't actually be cleared in time and the ISR will fire again.
     * Insert a 2-cycle buffer to allow it to take effect. */
    asm volatile("nop");
    asm volatile("nop");
}

static inline void dispatch_handler(uint32 channel) {
    ASSERT(exti_channels[channel].handler);
    if (exti_channels[channel].handler) {
        (exti_channels[channel].handler)();
    }
}

/* For EXTI0 through EXTI4, only one handler
 * is associated with each channel, so we
 * don't have to keep track of which channel
 * we came from */
void EXTI0_IRQHandler(void) {
    dispatch_handler(EXTI0);
    clear_pending(EXTI0);
}

void EXTI1_IRQHandler(void) {
    dispatch_handler(EXTI1);
    clear_pending(EXTI1);
}

void EXTI2_IRQHandler(void) {
    dispatch_handler(EXTI2);
    clear_pending(EXTI2);
}

void EXTI3_IRQHandler(void) {
    dispatch_handler(EXTI3);
    clear_pending(EXTI3);
}

void EXTI4_IRQHandler(void) {
    dispatch_handler(EXTI4);
    clear_pending(EXTI4);
}

void EXTI9_5_IRQHandler(void) {
    /* Figure out which channel it came from  */
    uint32 pending;
    uint32 i;
    pending = REG_GET(EXTI_PR);
    pending = GET_BITS(pending, 5, 9);

    /* Dispatch every handler if the pending bit is set */
    for (i = 0; i < 5; i++) {
        if (pending & 0x1) {
            dispatch_handler(EXTI5 + i);
            clear_pending(EXTI5 + i);
        }
        pending >>= 1;
    }
}

void EXTI15_10_IRQHandler(void) {
    /* Figure out which channel it came from  */
    uint32 pending;
    uint32 i;
    pending = REG_GET(EXTI_PR);
    pending = GET_BITS(pending, 10, 15);

    /* Dispatch every handler if the pending bit is set */
    for (i = 0; i < 6; i++) {
        if (pending & 0x1) {
            dispatch_handler(EXTI10 + i);
            clear_pending(EXTI10 + i);
        }
        pending >>= 1;
    }
}


/**
 * @brief Register a handler to run upon external interrupt
 * @param port source port of pin (eg EXTI_CONFIG_PORTA)
 * @param pin pin number on the source port
 * @param handler function handler to execute
 * @param mode type of transition to trigger on
 */
void exti_attach_interrupt(uint32 port,
                           uint32 pin,
                           voidFuncPtr handler,
                           uint32 mode) {
    static uint32 afio_regs[] = {
        AFIO_EXTICR1,         // EXT0-3
        AFIO_EXTICR2,         // EXT4-7
        AFIO_EXTICR3,         // EXT8-11
        AFIO_EXTICR4,         // EXT12-15
    };

    /* Note: All of the following code assumes that EXTI0 = 0  */
    ASSERT(EXTI0 == 0);
    ASSERT(handler);

    uint32 channel = pin;

    /* map port to channel */
    __write(afio_regs[pin/4], (port << ((pin % 4) * 4)));

    /* Unmask appropriate interrupt line  */
    __set_bits(EXTI_IMR, BIT(channel));

    /* Set trigger mode  */
    switch (mode) {
    case EXTI_RISING:
        __set_bits(EXTI_RTSR, BIT(channel));
        break;

    case EXTI_FALLING:
        __set_bits(EXTI_FTSR, BIT(channel));
        break;

    case EXTI_RISING_FALLING:
        __set_bits(EXTI_RTSR, BIT(channel));
        __set_bits(EXTI_FTSR, BIT(channel));
        break;
    }

    /* Configure the enable interrupt bits for the NVIC  */
    nvic_irq_enable(exti_channels[channel].irq_line);

    /* Register the handler  */
    exti_channels[channel].handler = handler;
}


/**
 * @brief Unregister an external interrupt handler
 * @param channel channel to disable (eg EXTI0)
 */
void exti_detach_interrupt(uint32 channel) {
    ASSERT(channel < NR_EXTI_CHANNELS);
    ASSERT(EXTI0 == 0);

    __clear_bits(EXTI_IMR,  BIT(channel));
    __clear_bits(EXTI_FTSR, BIT(channel));
    __clear_bits(EXTI_RTSR, BIT(channel));

    nvic_irq_disable(exti_channels[channel].irq_line);

    exti_channels[channel].handler = NULL;
}