Damien George | 04b9147 | 2014-05-03 23:27:38 +0100 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the Micro Python project, http://micropython.org/ |
| 3 | * |
| 4 | * The MIT License (MIT) |
| 5 | * |
| 6 | * Copyright (c) 2013, 2014 Damien P. George |
| 7 | * |
| 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | * of this software and associated documentation files (the "Software"), to deal |
| 10 | * in the Software without restriction, including without limitation the rights |
| 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | * copies of the Software, and to permit persons to whom the Software is |
| 13 | * furnished to do so, subject to the following conditions: |
| 14 | * |
| 15 | * The above copyright notice and this permission notice shall be included in |
| 16 | * all copies or substantial portions of the Software. |
| 17 | * |
| 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | * THE SOFTWARE. |
| 25 | */ |
| 26 | |
Damien George | c66d86c | 2014-04-18 22:38:09 +0100 | [diff] [blame] | 27 | #include <stdio.h> |
| 28 | #include <stddef.h> |
| 29 | #include <string.h> |
| 30 | |
| 31 | #include <stm32f4xx_hal.h> |
| 32 | |
Paul Sokolovsky | 9b71b16 | 2014-05-02 18:03:04 +0300 | [diff] [blame] | 33 | #include "mpconfig.h" |
Damien George | c66d86c | 2014-04-18 22:38:09 +0100 | [diff] [blame] | 34 | #include "nlr.h" |
| 35 | #include "misc.h" |
Damien George | c66d86c | 2014-04-18 22:38:09 +0100 | [diff] [blame] | 36 | #include "qstr.h" |
| 37 | #include "gc.h" |
| 38 | #include "obj.h" |
| 39 | #include "runtime.h" |
| 40 | |
| 41 | #include "pin.h" |
| 42 | #include "extint.h" |
| 43 | |
Damien George | 8d09640 | 2014-04-29 22:55:34 +0100 | [diff] [blame] | 44 | /// \moduleref pyb |
| 45 | /// \class ExtInt - configure I/O pins to interrupt on external events |
| 46 | /// |
| 47 | /// There are a total of 22 interrupt lines. 16 of these can come from GPIO pins |
| 48 | /// and the remaining 6 are from internal sources. |
| 49 | /// |
| 50 | /// For lines 0 thru 15, a given line can map to the corresponding line from an |
| 51 | /// arbitrary port. So line 0 can map to Px0 where x is A, B, C, ... and |
| 52 | /// line 1 can map to Px1 where x is A, B, C, ... |
| 53 | /// |
| 54 | /// def callback(line): |
| 55 | /// print("line =", line) |
| 56 | /// |
| 57 | /// Note: ExtInt will automatically configure the gpio line as an input. |
| 58 | /// |
Dave Hylands | 1145a07 | 2014-05-05 10:58:38 -0700 | [diff] [blame^] | 59 | /// extint = pyb.ExtInt(pin, pyb.ExtInt.IRQ_FALLING, pyb.Pin.PULL_UP, callback) |
Damien George | 8d09640 | 2014-04-29 22:55:34 +0100 | [diff] [blame] | 60 | /// |
| 61 | /// Now every time a falling edge is seen on the X1 pin, the callback will be |
| 62 | /// called. Caution: mechanical pushbuttons have "bounce" and pushing or |
| 63 | /// releasing a switch will often generate multiple edges. |
| 64 | /// See: http://www.eng.utah.edu/~cs5780/debouncing.pdf for a detailed |
| 65 | /// explanation, along with various techniques for debouncing. |
| 66 | /// |
| 67 | /// Trying to register 2 callbacks onto the same pin will throw an exception. |
| 68 | /// |
| 69 | /// If pin is passed as an integer, then it is assumed to map to one of the |
| 70 | /// internal interrupt sources, and must be in the range 16 thru 22. |
| 71 | /// |
| 72 | /// All other pin objects go through the pin mapper to come up with one of the |
| 73 | /// gpio pins. |
| 74 | /// |
| 75 | /// extint = pyb.ExtInt(pin, mode, pull, callback) |
| 76 | /// |
| 77 | /// Valid modes are pyb.ExtInt.IRQ_RISING, pyb.ExtInt.IRQ_FALLING, |
| 78 | /// pyb.ExtInt.IRQ_RISING_FALLING, pyb.ExtInt.EVT_RISING, |
| 79 | /// pyb.ExtInt.EVT_FALLING, and pyb.ExtInt.EVT_RISING_FALLING. |
| 80 | /// |
| 81 | /// Only the IRQ_xxx modes have been tested. The EVT_xxx modes have |
| 82 | /// something to do with sleep mode and the WFE instruction. |
| 83 | /// |
Dave Hylands | 1145a07 | 2014-05-05 10:58:38 -0700 | [diff] [blame^] | 84 | /// Valid pull values are pyb.Pin.PULL_UP, pyb.Pin.PULL_DOWN, pyb.Pin.PULL_NONE. |
Damien George | 8d09640 | 2014-04-29 22:55:34 +0100 | [diff] [blame] | 85 | /// |
| 86 | /// There is also a C API, so that drivers which require EXTI interrupt lines |
| 87 | /// can also use this code. See extint.h for the available functions and |
| 88 | /// usrsw.h for an example of using this. |
| 89 | |
Damien George | c66d86c | 2014-04-18 22:38:09 +0100 | [diff] [blame] | 90 | // TODO Add python method to change callback object. |
| 91 | |
| 92 | #define EXTI_OFFSET (EXTI_BASE - PERIPH_BASE) |
| 93 | |
| 94 | // Macro used to set/clear the bit corresponding to the line in the IMR/EMR |
| 95 | // register in an atomic fashion by using bitband addressing. |
| 96 | #define EXTI_MODE_BB(mode, line) (*(__IO uint32_t *)(PERIPH_BB_BASE + ((EXTI_OFFSET + (mode)) * 32) + ((line) * 4))) |
| 97 | |
| 98 | #define EXTI_Mode_Interrupt offsetof(EXTI_TypeDef, IMR) |
| 99 | #define EXTI_Mode_Event offsetof(EXTI_TypeDef, EMR) |
| 100 | |
| 101 | #define EXTI_SWIER_BB(line) (*(__IO uint32_t *)(PERIPH_BB_BASE + ((EXTI_OFFSET + offsetof(EXTI_TypeDef, SWIER)) * 32) + ((line) * 4))) |
| 102 | |
| 103 | typedef struct { |
| 104 | mp_obj_base_t base; |
| 105 | mp_small_int_t line; |
| 106 | } extint_obj_t; |
| 107 | |
| 108 | typedef struct { |
| 109 | mp_obj_t callback_obj; |
| 110 | void *param; |
| 111 | uint32_t mode; |
| 112 | } extint_vector_t; |
| 113 | |
| 114 | STATIC extint_vector_t extint_vector[EXTI_NUM_VECTORS]; |
| 115 | |
| 116 | #if !defined(ETH) |
| 117 | #define ETH_WKUP_IRQn 62 // The 405 doesn't have ETH, but we want a value to put in our table |
| 118 | #endif |
| 119 | |
| 120 | STATIC const uint8_t nvic_irq_channel[EXTI_NUM_VECTORS] = { |
| 121 | EXTI0_IRQn, EXTI1_IRQn, EXTI2_IRQn, EXTI3_IRQn, EXTI4_IRQn, |
| 122 | EXTI9_5_IRQn, EXTI9_5_IRQn, EXTI9_5_IRQn, EXTI9_5_IRQn, EXTI9_5_IRQn, |
| 123 | EXTI15_10_IRQn, EXTI15_10_IRQn, EXTI15_10_IRQn, EXTI15_10_IRQn, EXTI15_10_IRQn, |
| 124 | EXTI15_10_IRQn, PVD_IRQn, RTC_Alarm_IRQn, OTG_FS_WKUP_IRQn, ETH_WKUP_IRQn, |
| 125 | OTG_HS_WKUP_IRQn, TAMP_STAMP_IRQn, RTC_WKUP_IRQn |
| 126 | }; |
| 127 | |
| 128 | // Set override_callback_obj to true if you want to unconditionally set the |
| 129 | // callback function. |
| 130 | // |
| 131 | // NOTE: param is for C callers. Python can use closure to get an object bound |
| 132 | // with the function. |
Damien George | fd6925b | 2014-04-20 01:25:58 +0100 | [diff] [blame] | 133 | uint extint_register(mp_obj_t pin_obj, uint32_t mode, uint32_t pull, mp_obj_t callback_obj, bool override_callback_obj, void *param) { |
Damien George | c66d86c | 2014-04-18 22:38:09 +0100 | [diff] [blame] | 134 | const pin_obj_t *pin = NULL; |
| 135 | uint v_line; |
| 136 | |
| 137 | if (MP_OBJ_IS_INT(pin_obj)) { |
| 138 | // If an integer is passed in, then use it to identify lines 16 thru 22 |
| 139 | // We expect lines 0 thru 15 to be passed in as a pin, so that we can |
| 140 | // get both the port number and line number. |
| 141 | v_line = mp_obj_get_int(pin_obj); |
| 142 | if (v_line < 16) { |
| 143 | nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "ExtInt vector %d < 16, use a Pin object", v_line)); |
| 144 | } |
| 145 | if (v_line >= EXTI_NUM_VECTORS) { |
| 146 | nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "ExtInt vector %d >= max of %d", v_line, EXTI_NUM_VECTORS)); |
| 147 | } |
| 148 | } else { |
| 149 | pin = pin_find(pin_obj); |
| 150 | v_line = pin->pin; |
| 151 | } |
Damien George | c66d86c | 2014-04-18 22:38:09 +0100 | [diff] [blame] | 152 | if (mode != GPIO_MODE_IT_RISING && |
| 153 | mode != GPIO_MODE_IT_FALLING && |
| 154 | mode != GPIO_MODE_IT_RISING_FALLING && |
| 155 | mode != GPIO_MODE_EVT_RISING && |
| 156 | mode != GPIO_MODE_EVT_FALLING && |
| 157 | mode != GPIO_MODE_EVT_RISING_FALLING) { |
Damien George | fd6925b | 2014-04-20 01:25:58 +0100 | [diff] [blame] | 158 | nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "invalid ExtInt Mode: %d", mode)); |
Damien George | c66d86c | 2014-04-18 22:38:09 +0100 | [diff] [blame] | 159 | } |
Damien George | c66d86c | 2014-04-18 22:38:09 +0100 | [diff] [blame] | 160 | if (pull != GPIO_NOPULL && |
| 161 | pull != GPIO_PULLUP && |
| 162 | pull != GPIO_PULLDOWN) { |
Damien George | fd6925b | 2014-04-20 01:25:58 +0100 | [diff] [blame] | 163 | nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "invalid ExtInt Pull: %d", pull)); |
Damien George | c66d86c | 2014-04-18 22:38:09 +0100 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | extint_vector_t *v = &extint_vector[v_line]; |
| 167 | if (!override_callback_obj && v->callback_obj != mp_const_none && callback_obj != mp_const_none) { |
| 168 | nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "ExtInt vector %d is already in use", v_line)); |
| 169 | } |
| 170 | |
| 171 | // We need to update callback and param atomically, so we disable the line |
| 172 | // before we update anything. |
| 173 | |
| 174 | extint_disable(v_line); |
| 175 | |
| 176 | v->callback_obj = callback_obj; |
| 177 | v->param = param; |
| 178 | v->mode = (mode & 0x00010000) ? // GPIO_MODE_IT == 0x00010000 |
| 179 | EXTI_Mode_Interrupt : EXTI_Mode_Event; |
| 180 | |
| 181 | if (v->callback_obj != mp_const_none) { |
| 182 | |
| 183 | GPIO_InitTypeDef exti; |
| 184 | exti.Pin = pin->pin_mask; |
| 185 | exti.Mode = mode; |
| 186 | exti.Pull = pull; |
| 187 | exti.Speed = GPIO_SPEED_FAST; |
| 188 | HAL_GPIO_Init(pin->gpio, &exti); |
| 189 | |
| 190 | // Calling HAL_GPIO_Init does an implicit extint_enable |
| 191 | |
| 192 | /* Enable and set NVIC Interrupt to the lowest priority */ |
| 193 | HAL_NVIC_SetPriority(nvic_irq_channel[v_line], 0x0F, 0x0F); |
| 194 | HAL_NVIC_EnableIRQ(nvic_irq_channel[v_line]); |
| 195 | } |
| 196 | return v_line; |
| 197 | } |
| 198 | |
| 199 | void extint_enable(uint line) { |
| 200 | if (line >= EXTI_NUM_VECTORS) { |
| 201 | return; |
| 202 | } |
| 203 | // Since manipulating IMR/EMR is a read-modify-write, and we want this to |
| 204 | // be atomic, we use the bit-band area to just affect the bit we're |
| 205 | // interested in. |
| 206 | EXTI_MODE_BB(extint_vector[line].mode, line) = 1; |
| 207 | } |
| 208 | |
| 209 | void extint_disable(uint line) { |
| 210 | if (line >= EXTI_NUM_VECTORS) { |
| 211 | return; |
| 212 | } |
| 213 | // Since manipulating IMR/EMR is a read-modify-write, and we want this to |
| 214 | // be atomic, we use the bit-band area to just affect the bit we're |
| 215 | // interested in. |
| 216 | EXTI_MODE_BB(EXTI_Mode_Interrupt, line) = 0; |
| 217 | EXTI_MODE_BB(EXTI_Mode_Event, line) = 0; |
| 218 | } |
| 219 | |
| 220 | void extint_swint(uint line) { |
| 221 | if (line >= EXTI_NUM_VECTORS) { |
| 222 | return; |
| 223 | } |
| 224 | EXTI->SWIER = (1 << line); |
| 225 | } |
| 226 | |
Damien George | 8d09640 | 2014-04-29 22:55:34 +0100 | [diff] [blame] | 227 | /// \method line() |
| 228 | /// Return the line number that the pin is mapped to. |
Damien George | c66d86c | 2014-04-18 22:38:09 +0100 | [diff] [blame] | 229 | STATIC mp_obj_t extint_obj_line(mp_obj_t self_in) { |
| 230 | extint_obj_t *self = self_in; |
| 231 | return MP_OBJ_NEW_SMALL_INT(self->line); |
| 232 | } |
Damien George | da9f271 | 2014-04-29 23:00:48 +0100 | [diff] [blame] | 233 | STATIC MP_DEFINE_CONST_FUN_OBJ_1(extint_obj_line_obj, extint_obj_line); |
Damien George | c66d86c | 2014-04-18 22:38:09 +0100 | [diff] [blame] | 234 | |
Damien George | 8d09640 | 2014-04-29 22:55:34 +0100 | [diff] [blame] | 235 | /// \method enable() |
| 236 | /// Enable a disabled interrupt. |
Damien George | c66d86c | 2014-04-18 22:38:09 +0100 | [diff] [blame] | 237 | STATIC mp_obj_t extint_obj_enable(mp_obj_t self_in) { |
| 238 | extint_obj_t *self = self_in; |
| 239 | extint_enable(self->line); |
| 240 | return mp_const_none; |
| 241 | } |
Damien George | 8d09640 | 2014-04-29 22:55:34 +0100 | [diff] [blame] | 242 | STATIC MP_DEFINE_CONST_FUN_OBJ_1(extint_obj_enable_obj, extint_obj_enable); |
Damien George | c66d86c | 2014-04-18 22:38:09 +0100 | [diff] [blame] | 243 | |
Damien George | 8d09640 | 2014-04-29 22:55:34 +0100 | [diff] [blame] | 244 | /// \method disable() |
| 245 | /// Disable the interrupt associated with the ExtInt object. |
| 246 | /// This could be useful for debouncing. |
Damien George | c66d86c | 2014-04-18 22:38:09 +0100 | [diff] [blame] | 247 | STATIC mp_obj_t extint_obj_disable(mp_obj_t self_in) { |
| 248 | extint_obj_t *self = self_in; |
| 249 | extint_disable(self->line); |
| 250 | return mp_const_none; |
| 251 | } |
Damien George | 8d09640 | 2014-04-29 22:55:34 +0100 | [diff] [blame] | 252 | STATIC MP_DEFINE_CONST_FUN_OBJ_1(extint_obj_disable_obj, extint_obj_disable); |
Damien George | c66d86c | 2014-04-18 22:38:09 +0100 | [diff] [blame] | 253 | |
Damien George | 8d09640 | 2014-04-29 22:55:34 +0100 | [diff] [blame] | 254 | /// \method swint() |
| 255 | /// Trigger the callback from software. |
Damien George | c66d86c | 2014-04-18 22:38:09 +0100 | [diff] [blame] | 256 | STATIC mp_obj_t extint_obj_swint(mp_obj_t self_in) { |
| 257 | extint_obj_t *self = self_in; |
| 258 | extint_swint(self->line); |
| 259 | return mp_const_none; |
| 260 | } |
Damien George | 8d09640 | 2014-04-29 22:55:34 +0100 | [diff] [blame] | 261 | STATIC MP_DEFINE_CONST_FUN_OBJ_1(extint_obj_swint_obj, extint_obj_swint); |
Damien George | c66d86c | 2014-04-18 22:38:09 +0100 | [diff] [blame] | 262 | |
Damien George | 8d09640 | 2014-04-29 22:55:34 +0100 | [diff] [blame] | 263 | // TODO document as a staticmethod |
| 264 | /// \classmethod regs() |
| 265 | /// Dump the values of the EXTI registers. |
Damien George | c66d86c | 2014-04-18 22:38:09 +0100 | [diff] [blame] | 266 | STATIC mp_obj_t extint_regs(void) { |
| 267 | printf("EXTI_IMR %08lx\n", EXTI->IMR); |
| 268 | printf("EXTI_EMR %08lx\n", EXTI->EMR); |
| 269 | printf("EXTI_RTSR %08lx\n", EXTI->RTSR); |
| 270 | printf("EXTI_FTSR %08lx\n", EXTI->FTSR); |
| 271 | printf("EXTI_SWIER %08lx\n", EXTI->SWIER); |
| 272 | printf("EXTI_PR %08lx\n", EXTI->PR); |
| 273 | return mp_const_none; |
| 274 | } |
Damien George | 8d09640 | 2014-04-29 22:55:34 +0100 | [diff] [blame] | 275 | STATIC MP_DEFINE_CONST_FUN_OBJ_0(extint_regs_fun_obj, extint_regs); |
| 276 | STATIC MP_DEFINE_CONST_STATICMETHOD_OBJ(extint_regs_obj, (mp_obj_t)&extint_regs_fun_obj); |
Damien George | c66d86c | 2014-04-18 22:38:09 +0100 | [diff] [blame] | 277 | |
Damien George | 8d09640 | 2014-04-29 22:55:34 +0100 | [diff] [blame] | 278 | /// \classmethod \constructor(pin, mode, pull, callback) |
| 279 | /// Create an ExtInt object: |
| 280 | /// |
| 281 | /// - `pin` is the pin on which to enable the interrupt (can be a pin object or any valid pin name). |
| 282 | /// - `mode` can be one of: |
| 283 | /// - `ExtInt.IRQ_RISING` - trigger on a rising edge; |
| 284 | /// - `ExtInt.IRQ_FALLING` - trigger on a falling edge; |
| 285 | /// - `ExtInt.IRQ_RISING_FALLING` - trigger on a rising or falling edge. |
| 286 | /// - `pull` can be one of: |
| 287 | /// - `pyb.Pin.PULL_NONE` - no pull up or down resistors; |
| 288 | /// - `pyb.Pin.PULL_UP` - enable the pull-up resistor; |
| 289 | /// - `pyb.Pin.PULL_DOWN` - enable the pull-down resistor. |
| 290 | /// - `callback` is the function to call when the interrupt triggers. The |
| 291 | /// callback function must accept exactly 1 argument, which is the line that |
| 292 | /// triggered the interrupt. |
Damien George | dbc81df | 2014-04-26 11:19:17 +0100 | [diff] [blame] | 293 | STATIC const mp_arg_t pyb_extint_make_new_args[] = { |
| 294 | { MP_QSTR_pin, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, |
| 295 | { MP_QSTR_mode, MP_ARG_REQUIRED | MP_ARG_INT, {.u_int = 0} }, |
| 296 | { MP_QSTR_pull, MP_ARG_REQUIRED | MP_ARG_INT, {.u_int = 0} }, |
| 297 | { MP_QSTR_callback, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, |
Damien George | 0a6e9f5 | 2014-04-20 00:38:50 +0100 | [diff] [blame] | 298 | }; |
Damien George | dbc81df | 2014-04-26 11:19:17 +0100 | [diff] [blame] | 299 | #define PYB_EXTINT_MAKE_NEW_NUM_ARGS ARRAY_SIZE(pyb_extint_make_new_args) |
Damien George | 0a6e9f5 | 2014-04-20 00:38:50 +0100 | [diff] [blame] | 300 | |
Damien George | c66d86c | 2014-04-18 22:38:09 +0100 | [diff] [blame] | 301 | STATIC mp_obj_t extint_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) { |
| 302 | // type_in == extint_obj_type |
| 303 | |
Damien George | 0a6e9f5 | 2014-04-20 00:38:50 +0100 | [diff] [blame] | 304 | // parse args |
| 305 | mp_map_t kw_args; |
| 306 | mp_map_init_fixed_table(&kw_args, n_kw, args + n_args); |
Damien George | dbc81df | 2014-04-26 11:19:17 +0100 | [diff] [blame] | 307 | mp_arg_val_t vals[PYB_EXTINT_MAKE_NEW_NUM_ARGS]; |
| 308 | mp_arg_parse_all(n_args, args, &kw_args, PYB_EXTINT_MAKE_NEW_NUM_ARGS, pyb_extint_make_new_args, vals); |
Damien George | c66d86c | 2014-04-18 22:38:09 +0100 | [diff] [blame] | 309 | |
| 310 | extint_obj_t *self = m_new_obj(extint_obj_t); |
| 311 | self->base.type = type_in; |
Damien George | fd6925b | 2014-04-20 01:25:58 +0100 | [diff] [blame] | 312 | self->line = extint_register(vals[0].u_obj, vals[1].u_int, vals[2].u_int, vals[3].u_obj, false, NULL); |
Damien George | c66d86c | 2014-04-18 22:38:09 +0100 | [diff] [blame] | 313 | |
| 314 | return self; |
| 315 | } |
| 316 | |
| 317 | STATIC void extint_obj_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) { |
| 318 | extint_obj_t *self = self_in; |
| 319 | print(env, "<ExtInt line=%u>", self->line); |
| 320 | } |
| 321 | |
Damien George | c66d86c | 2014-04-18 22:38:09 +0100 | [diff] [blame] | 322 | STATIC const mp_map_elem_t extint_locals_dict_table[] = { |
| 323 | { MP_OBJ_NEW_QSTR(MP_QSTR_line), (mp_obj_t)&extint_obj_line_obj }, |
| 324 | { MP_OBJ_NEW_QSTR(MP_QSTR_enable), (mp_obj_t)&extint_obj_enable_obj }, |
| 325 | { MP_OBJ_NEW_QSTR(MP_QSTR_disable), (mp_obj_t)&extint_obj_disable_obj }, |
| 326 | { MP_OBJ_NEW_QSTR(MP_QSTR_swint), (mp_obj_t)&extint_obj_swint_obj }, |
| 327 | { MP_OBJ_NEW_QSTR(MP_QSTR_regs), (mp_obj_t)&extint_regs_obj }, |
Damien George | 8d09640 | 2014-04-29 22:55:34 +0100 | [diff] [blame] | 328 | |
| 329 | // class constants |
| 330 | /// \constant IRQ_RISING - interrupt on a rising edge |
| 331 | /// \constant IRQ_FALLING - interrupt on a falling edge |
| 332 | /// \constant IRQ_RISING_FALLING - interrupt on a rising or falling edge |
Damien George | c66d86c | 2014-04-18 22:38:09 +0100 | [diff] [blame] | 333 | { MP_OBJ_NEW_QSTR(MP_QSTR_IRQ_RISING), MP_OBJ_NEW_SMALL_INT(GPIO_MODE_IT_RISING) }, |
| 334 | { MP_OBJ_NEW_QSTR(MP_QSTR_IRQ_FALLING), MP_OBJ_NEW_SMALL_INT(GPIO_MODE_IT_FALLING) }, |
| 335 | { MP_OBJ_NEW_QSTR(MP_QSTR_IRQ_RISING_FALLING), MP_OBJ_NEW_SMALL_INT(GPIO_MODE_IT_RISING_FALLING) }, |
| 336 | { MP_OBJ_NEW_QSTR(MP_QSTR_EVT_RISING), MP_OBJ_NEW_SMALL_INT(GPIO_MODE_EVT_RISING) }, |
| 337 | { MP_OBJ_NEW_QSTR(MP_QSTR_EVT_FALLING), MP_OBJ_NEW_SMALL_INT(GPIO_MODE_EVT_FALLING) }, |
| 338 | { MP_OBJ_NEW_QSTR(MP_QSTR_EVT_RISING_FALLING), MP_OBJ_NEW_SMALL_INT(GPIO_MODE_EVT_RISING_FALLING) }, |
| 339 | }; |
| 340 | |
| 341 | STATIC MP_DEFINE_CONST_DICT(extint_locals_dict, extint_locals_dict_table); |
| 342 | |
| 343 | const mp_obj_type_t extint_type = { |
| 344 | { &mp_type_type }, |
| 345 | .name = MP_QSTR_ExtInt, |
| 346 | .print = extint_obj_print, |
| 347 | .make_new = extint_make_new, |
| 348 | .locals_dict = (mp_obj_t)&extint_locals_dict, |
| 349 | }; |
| 350 | |
| 351 | void extint_init(void) { |
| 352 | for (extint_vector_t *v = extint_vector; v < &extint_vector[EXTI_NUM_VECTORS]; v++) { |
| 353 | v->callback_obj = mp_const_none; |
| 354 | v->param = NULL; |
| 355 | v->mode = EXTI_Mode_Interrupt; |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | // Interrupt handler |
| 360 | void Handle_EXTI_Irq(uint32_t line) { |
| 361 | if (__HAL_GPIO_EXTI_GET_FLAG(1 << line)) { |
| 362 | __HAL_GPIO_EXTI_CLEAR_FLAG(1 << line); |
| 363 | if (line < EXTI_NUM_VECTORS) { |
| 364 | extint_vector_t *v = &extint_vector[line]; |
| 365 | if (v->callback_obj != mp_const_none) { |
| 366 | // When executing code within a handler we must lock the GC to prevent |
| 367 | // any memory allocations. We must also catch any exceptions. |
| 368 | gc_lock(); |
| 369 | nlr_buf_t nlr; |
| 370 | if (nlr_push(&nlr) == 0) { |
| 371 | mp_call_function_1(v->callback_obj, MP_OBJ_NEW_SMALL_INT(line)); |
| 372 | nlr_pop(); |
| 373 | } else { |
| 374 | // Uncaught exception; disable the callback so it doesn't run again. |
| 375 | v->callback_obj = mp_const_none; |
| 376 | extint_disable(line); |
| 377 | printf("Uncaught exception in ExtInt interrupt handler line %lu\n", line); |
| 378 | mp_obj_print_exception((mp_obj_t)nlr.ret_val); |
| 379 | } |
| 380 | gc_unlock(); |
| 381 | } |
| 382 | } |
| 383 | } |
| 384 | } |