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