2014-05-03 23:27:38 +01:00
|
|
|
/*
|
2017-06-30 08:22:17 +01:00
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
2014-05-03 23:27:38 +01:00
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2013, 2014 Damien P. George
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2014-03-15 06:41:28 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2015-01-01 21:06:20 +00:00
|
|
|
#include "py/runtime.h"
|
|
|
|
#include "py/gc.h"
|
2015-10-30 23:03:58 +00:00
|
|
|
#include "py/mphal.h"
|
2014-03-15 06:41:28 +00:00
|
|
|
#include "pin.h"
|
2014-04-18 22:38:09 +01:00
|
|
|
#include "extint.h"
|
2015-10-31 17:44:20 +00:00
|
|
|
#include "irq.h"
|
2014-03-15 06:41:28 +00:00
|
|
|
|
2014-04-29 22:55:34 +01:00
|
|
|
/// \moduleref pyb
|
|
|
|
/// \class ExtInt - configure I/O pins to interrupt on external events
|
|
|
|
///
|
|
|
|
/// There are a total of 22 interrupt lines. 16 of these can come from GPIO pins
|
|
|
|
/// and the remaining 6 are from internal sources.
|
|
|
|
///
|
|
|
|
/// For lines 0 thru 15, a given line can map to the corresponding line from an
|
|
|
|
/// arbitrary port. So line 0 can map to Px0 where x is A, B, C, ... and
|
|
|
|
/// line 1 can map to Px1 where x is A, B, C, ...
|
|
|
|
///
|
|
|
|
/// def callback(line):
|
|
|
|
/// print("line =", line)
|
|
|
|
///
|
|
|
|
/// Note: ExtInt will automatically configure the gpio line as an input.
|
|
|
|
///
|
2014-05-05 18:58:38 +01:00
|
|
|
/// extint = pyb.ExtInt(pin, pyb.ExtInt.IRQ_FALLING, pyb.Pin.PULL_UP, callback)
|
2014-04-29 22:55:34 +01:00
|
|
|
///
|
|
|
|
/// Now every time a falling edge is seen on the X1 pin, the callback will be
|
|
|
|
/// called. Caution: mechanical pushbuttons have "bounce" and pushing or
|
|
|
|
/// releasing a switch will often generate multiple edges.
|
|
|
|
/// See: http://www.eng.utah.edu/~cs5780/debouncing.pdf for a detailed
|
|
|
|
/// explanation, along with various techniques for debouncing.
|
|
|
|
///
|
|
|
|
/// Trying to register 2 callbacks onto the same pin will throw an exception.
|
|
|
|
///
|
|
|
|
/// If pin is passed as an integer, then it is assumed to map to one of the
|
|
|
|
/// internal interrupt sources, and must be in the range 16 thru 22.
|
|
|
|
///
|
|
|
|
/// All other pin objects go through the pin mapper to come up with one of the
|
|
|
|
/// gpio pins.
|
|
|
|
///
|
|
|
|
/// extint = pyb.ExtInt(pin, mode, pull, callback)
|
|
|
|
///
|
|
|
|
/// Valid modes are pyb.ExtInt.IRQ_RISING, pyb.ExtInt.IRQ_FALLING,
|
|
|
|
/// pyb.ExtInt.IRQ_RISING_FALLING, pyb.ExtInt.EVT_RISING,
|
|
|
|
/// pyb.ExtInt.EVT_FALLING, and pyb.ExtInt.EVT_RISING_FALLING.
|
|
|
|
///
|
|
|
|
/// Only the IRQ_xxx modes have been tested. The EVT_xxx modes have
|
|
|
|
/// something to do with sleep mode and the WFE instruction.
|
|
|
|
///
|
2014-05-05 18:58:38 +01:00
|
|
|
/// Valid pull values are pyb.Pin.PULL_UP, pyb.Pin.PULL_DOWN, pyb.Pin.PULL_NONE.
|
2014-04-29 22:55:34 +01:00
|
|
|
///
|
|
|
|
/// There is also a C API, so that drivers which require EXTI interrupt lines
|
|
|
|
/// can also use this code. See extint.h for the available functions and
|
|
|
|
/// usrsw.h for an example of using this.
|
|
|
|
|
2014-03-29 01:24:44 +00:00
|
|
|
// TODO Add python method to change callback object.
|
2014-03-15 06:41:28 +00:00
|
|
|
|
2017-09-06 05:53:17 +01:00
|
|
|
#define EXTI_OFFSET (EXTI_BASE - PERIPH_BASE)
|
2014-03-15 06:41:28 +00:00
|
|
|
|
|
|
|
// Macro used to set/clear the bit corresponding to the line in the IMR/EMR
|
|
|
|
// register in an atomic fashion by using bitband addressing.
|
|
|
|
#define EXTI_MODE_BB(mode, line) (*(__IO uint32_t *)(PERIPH_BB_BASE + ((EXTI_OFFSET + (mode)) * 32) + ((line) * 4)))
|
|
|
|
|
2016-03-23 09:50:53 +00:00
|
|
|
#if defined(MCU_SERIES_L4)
|
|
|
|
// The L4 MCU supports 40 Events/IRQs lines of the type configurable and direct.
|
|
|
|
// Here we only support configurable line types. Details, see page 330 of RM0351, Rev 1.
|
|
|
|
// The USB_FS_WAKUP event is a direct type and there is no support for it.
|
|
|
|
#define EXTI_Mode_Interrupt offsetof(EXTI_TypeDef, IMR1)
|
|
|
|
#define EXTI_Mode_Event offsetof(EXTI_TypeDef, EMR1)
|
2017-06-15 08:17:36 +01:00
|
|
|
#define EXTI_RTSR EXTI->RTSR1
|
|
|
|
#define EXTI_FTSR EXTI->FTSR1
|
2016-03-23 09:50:53 +00:00
|
|
|
#else
|
2014-03-15 06:41:28 +00:00
|
|
|
#define EXTI_Mode_Interrupt offsetof(EXTI_TypeDef, IMR)
|
|
|
|
#define EXTI_Mode_Event offsetof(EXTI_TypeDef, EMR)
|
2017-06-15 08:17:36 +01:00
|
|
|
#define EXTI_RTSR EXTI->RTSR
|
|
|
|
#define EXTI_FTSR EXTI->FTSR
|
2016-03-23 09:50:53 +00:00
|
|
|
#endif
|
2014-03-15 06:41:28 +00:00
|
|
|
|
|
|
|
#define EXTI_SWIER_BB(line) (*(__IO uint32_t *)(PERIPH_BB_BASE + ((EXTI_OFFSET + offsetof(EXTI_TypeDef, SWIER)) * 32) + ((line) * 4)))
|
|
|
|
|
|
|
|
typedef struct {
|
2014-07-03 13:25:24 +01:00
|
|
|
mp_obj_base_t base;
|
|
|
|
mp_int_t line;
|
2014-04-18 22:38:09 +01:00
|
|
|
} extint_obj_t;
|
2014-03-15 06:41:28 +00:00
|
|
|
|
2017-06-15 08:17:36 +01:00
|
|
|
STATIC uint8_t pyb_extint_mode[EXTI_NUM_VECTORS];
|
|
|
|
STATIC bool pyb_extint_hard_irq[EXTI_NUM_VECTORS];
|
|
|
|
|
|
|
|
// The callback arg is a small-int or a ROM Pin object, so no need to scan by GC
|
|
|
|
STATIC mp_obj_t pyb_extint_callback_arg[EXTI_NUM_VECTORS];
|
2014-03-15 06:41:28 +00:00
|
|
|
|
|
|
|
#if !defined(ETH)
|
2015-04-18 16:17:14 +01:00
|
|
|
#define ETH_WKUP_IRQn 62 // Some MCUs don't have ETH, but we want a value to put in our table
|
|
|
|
#endif
|
|
|
|
#if !defined(OTG_HS_WKUP_IRQn)
|
|
|
|
#define OTG_HS_WKUP_IRQn 76 // Some MCUs don't have HS, but we want a value to put in our table
|
2014-03-15 06:41:28 +00:00
|
|
|
#endif
|
2016-03-23 09:50:53 +00:00
|
|
|
#if !defined(OTG_FS_WKUP_IRQn)
|
|
|
|
#define OTG_FS_WKUP_IRQn 42 // Some MCUs don't have FS IRQ, but we want a value to put in our table
|
|
|
|
#endif
|
2014-03-15 06:41:28 +00:00
|
|
|
|
2014-03-26 22:35:55 +00:00
|
|
|
STATIC const uint8_t nvic_irq_channel[EXTI_NUM_VECTORS] = {
|
2014-03-15 06:41:28 +00:00
|
|
|
EXTI0_IRQn, EXTI1_IRQn, EXTI2_IRQn, EXTI3_IRQn, EXTI4_IRQn,
|
|
|
|
EXTI9_5_IRQn, EXTI9_5_IRQn, EXTI9_5_IRQn, EXTI9_5_IRQn, EXTI9_5_IRQn,
|
|
|
|
EXTI15_10_IRQn, EXTI15_10_IRQn, EXTI15_10_IRQn, EXTI15_10_IRQn, EXTI15_10_IRQn,
|
2016-03-23 09:50:53 +00:00
|
|
|
EXTI15_10_IRQn,
|
|
|
|
#if defined(MCU_SERIES_L4)
|
|
|
|
PVD_PVM_IRQn,
|
|
|
|
#else
|
|
|
|
PVD_IRQn,
|
|
|
|
#endif
|
|
|
|
RTC_Alarm_IRQn,
|
|
|
|
OTG_FS_WKUP_IRQn,
|
|
|
|
ETH_WKUP_IRQn,
|
|
|
|
OTG_HS_WKUP_IRQn,
|
|
|
|
TAMP_STAMP_IRQn,
|
|
|
|
RTC_WKUP_IRQn,
|
2014-03-15 06:41:28 +00:00
|
|
|
};
|
|
|
|
|
2014-04-08 22:35:42 +01:00
|
|
|
// Set override_callback_obj to true if you want to unconditionally set the
|
|
|
|
// callback function.
|
2015-01-07 23:38:50 +00:00
|
|
|
uint extint_register(mp_obj_t pin_obj, uint32_t mode, uint32_t pull, mp_obj_t callback_obj, bool override_callback_obj) {
|
2014-03-15 06:41:28 +00:00
|
|
|
const pin_obj_t *pin = NULL;
|
|
|
|
uint v_line;
|
|
|
|
|
|
|
|
if (MP_OBJ_IS_INT(pin_obj)) {
|
|
|
|
// If an integer is passed in, then use it to identify lines 16 thru 22
|
|
|
|
// We expect lines 0 thru 15 to be passed in as a pin, so that we can
|
|
|
|
// get both the port number and line number.
|
|
|
|
v_line = mp_obj_get_int(pin_obj);
|
|
|
|
if (v_line < 16) {
|
2014-04-18 22:38:09 +01:00
|
|
|
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "ExtInt vector %d < 16, use a Pin object", v_line));
|
2014-03-15 06:41:28 +00:00
|
|
|
}
|
|
|
|
if (v_line >= EXTI_NUM_VECTORS) {
|
2014-04-18 22:38:09 +01:00
|
|
|
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "ExtInt vector %d >= max of %d", v_line, EXTI_NUM_VECTORS));
|
2014-03-15 06:41:28 +00:00
|
|
|
}
|
|
|
|
} else {
|
2014-04-18 22:38:09 +01:00
|
|
|
pin = pin_find(pin_obj);
|
2014-03-15 06:41:28 +00:00
|
|
|
v_line = pin->pin;
|
|
|
|
}
|
|
|
|
if (mode != GPIO_MODE_IT_RISING &&
|
|
|
|
mode != GPIO_MODE_IT_FALLING &&
|
|
|
|
mode != GPIO_MODE_IT_RISING_FALLING &&
|
|
|
|
mode != GPIO_MODE_EVT_RISING &&
|
|
|
|
mode != GPIO_MODE_EVT_FALLING &&
|
|
|
|
mode != GPIO_MODE_EVT_RISING_FALLING) {
|
2014-04-20 01:25:58 +01:00
|
|
|
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "invalid ExtInt Mode: %d", mode));
|
2014-03-15 06:41:28 +00:00
|
|
|
}
|
|
|
|
if (pull != GPIO_NOPULL &&
|
|
|
|
pull != GPIO_PULLUP &&
|
|
|
|
pull != GPIO_PULLDOWN) {
|
2014-04-20 01:25:58 +01:00
|
|
|
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "invalid ExtInt Pull: %d", pull));
|
2014-03-15 06:41:28 +00:00
|
|
|
}
|
|
|
|
|
2015-01-07 23:38:50 +00:00
|
|
|
mp_obj_t *cb = &MP_STATE_PORT(pyb_extint_callback)[v_line];
|
|
|
|
if (!override_callback_obj && *cb != mp_const_none && callback_obj != mp_const_none) {
|
2014-04-18 22:38:09 +01:00
|
|
|
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "ExtInt vector %d is already in use", v_line));
|
2014-03-15 06:41:28 +00:00
|
|
|
}
|
|
|
|
|
2015-01-07 23:38:50 +00:00
|
|
|
// We need to update callback atomically, so we disable the line
|
2014-03-15 06:41:28 +00:00
|
|
|
// before we update anything.
|
|
|
|
|
2014-04-18 22:38:09 +01:00
|
|
|
extint_disable(v_line);
|
2014-03-15 06:41:28 +00:00
|
|
|
|
2015-01-07 23:38:50 +00:00
|
|
|
*cb = callback_obj;
|
|
|
|
pyb_extint_mode[v_line] = (mode & 0x00010000) ? // GPIO_MODE_IT == 0x00010000
|
2014-03-15 06:41:28 +00:00
|
|
|
EXTI_Mode_Interrupt : EXTI_Mode_Event;
|
|
|
|
|
2015-01-07 23:38:50 +00:00
|
|
|
if (*cb != mp_const_none) {
|
2017-06-15 08:17:36 +01:00
|
|
|
pyb_extint_hard_irq[v_line] = true;
|
|
|
|
pyb_extint_callback_arg[v_line] = MP_OBJ_NEW_SMALL_INT(v_line);
|
2014-03-15 06:41:28 +00:00
|
|
|
|
2015-08-03 00:05:16 +01:00
|
|
|
mp_hal_gpio_clock_enable(pin->gpio);
|
2014-03-15 06:41:28 +00:00
|
|
|
GPIO_InitTypeDef exti;
|
|
|
|
exti.Pin = pin->pin_mask;
|
|
|
|
exti.Mode = mode;
|
|
|
|
exti.Pull = pull;
|
2018-02-13 04:37:35 +00:00
|
|
|
exti.Speed = GPIO_SPEED_FREQ_HIGH;
|
2014-03-15 06:41:28 +00:00
|
|
|
HAL_GPIO_Init(pin->gpio, &exti);
|
|
|
|
|
2014-04-18 22:38:09 +01:00
|
|
|
// Calling HAL_GPIO_Init does an implicit extint_enable
|
2014-03-15 06:41:28 +00:00
|
|
|
|
|
|
|
/* Enable and set NVIC Interrupt to the lowest priority */
|
2015-10-31 17:44:20 +00:00
|
|
|
HAL_NVIC_SetPriority(nvic_irq_channel[v_line], IRQ_PRI_EXTINT, IRQ_SUBPRI_EXTINT);
|
2014-03-15 06:41:28 +00:00
|
|
|
HAL_NVIC_EnableIRQ(nvic_irq_channel[v_line]);
|
|
|
|
}
|
|
|
|
return v_line;
|
|
|
|
}
|
|
|
|
|
2017-06-15 08:17:36 +01:00
|
|
|
// This function is intended to be used by the Pin.irq() method
|
|
|
|
void extint_register_pin(const pin_obj_t *pin, uint32_t mode, bool hard_irq, mp_obj_t callback_obj) {
|
|
|
|
uint32_t line = pin->pin;
|
|
|
|
|
|
|
|
// Check if the ExtInt line is already in use by another Pin/ExtInt
|
|
|
|
mp_obj_t *cb = &MP_STATE_PORT(pyb_extint_callback)[line];
|
|
|
|
if (*cb != mp_const_none && MP_OBJ_FROM_PTR(pin) != pyb_extint_callback_arg[line]) {
|
|
|
|
if (MP_OBJ_IS_SMALL_INT(pyb_extint_callback_arg[line])) {
|
|
|
|
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError,
|
|
|
|
"ExtInt vector %d is already in use", line));
|
|
|
|
} else {
|
|
|
|
const pin_obj_t *other_pin = (const pin_obj_t*)pyb_extint_callback_arg[line];
|
|
|
|
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError,
|
|
|
|
"IRQ resource already taken by Pin('%q')", other_pin->name));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extint_disable(line);
|
|
|
|
|
|
|
|
*cb = callback_obj;
|
|
|
|
pyb_extint_mode[line] = (mode & 0x00010000) ? // GPIO_MODE_IT == 0x00010000
|
|
|
|
EXTI_Mode_Interrupt : EXTI_Mode_Event;
|
|
|
|
|
|
|
|
if (*cb != mp_const_none) {
|
|
|
|
// Configure and enable the callback
|
|
|
|
|
|
|
|
pyb_extint_hard_irq[line] = hard_irq;
|
|
|
|
pyb_extint_callback_arg[line] = MP_OBJ_FROM_PTR(pin);
|
|
|
|
|
|
|
|
// Route the GPIO to EXTI
|
|
|
|
__HAL_RCC_SYSCFG_CLK_ENABLE();
|
|
|
|
SYSCFG->EXTICR[line >> 2] =
|
|
|
|
(SYSCFG->EXTICR[line >> 2] & ~(0x0f << (4 * (line & 0x03))))
|
|
|
|
| ((uint32_t)(GPIO_GET_INDEX(pin->gpio)) << (4 * (line & 0x03)));
|
|
|
|
|
|
|
|
// Enable or disable the rising detector
|
|
|
|
if ((mode & GPIO_MODE_IT_RISING) == GPIO_MODE_IT_RISING) {
|
|
|
|
EXTI_RTSR |= 1 << line;
|
|
|
|
} else {
|
|
|
|
EXTI_RTSR &= ~(1 << line);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Enable or disable the falling detector
|
|
|
|
if ((mode & GPIO_MODE_IT_FALLING) == GPIO_MODE_IT_FALLING) {
|
|
|
|
EXTI_FTSR |= 1 << line;
|
|
|
|
} else {
|
|
|
|
EXTI_FTSR &= ~(1 << line);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Configure the NVIC
|
|
|
|
HAL_NVIC_SetPriority(nvic_irq_channel[line], IRQ_PRI_EXTINT, IRQ_SUBPRI_EXTINT);
|
|
|
|
HAL_NVIC_EnableIRQ(nvic_irq_channel[line]);
|
|
|
|
|
|
|
|
// Enable the interrupt
|
|
|
|
extint_enable(line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-18 22:38:09 +01:00
|
|
|
void extint_enable(uint line) {
|
2014-03-15 06:41:28 +00:00
|
|
|
if (line >= EXTI_NUM_VECTORS) {
|
|
|
|
return;
|
|
|
|
}
|
2015-08-03 23:07:20 +01:00
|
|
|
#if defined(MCU_SERIES_F7)
|
2015-07-28 19:13:33 +01:00
|
|
|
// The Cortex-M7 doesn't have bitband support.
|
|
|
|
mp_uint_t irq_state = disable_irq();
|
|
|
|
if (pyb_extint_mode[line] == EXTI_Mode_Interrupt) {
|
|
|
|
EXTI->IMR |= (1 << line);
|
|
|
|
} else {
|
|
|
|
EXTI->EMR |= (1 << line);
|
|
|
|
}
|
|
|
|
enable_irq(irq_state);
|
|
|
|
#else
|
2014-03-15 06:41:28 +00:00
|
|
|
// Since manipulating IMR/EMR is a read-modify-write, and we want this to
|
|
|
|
// be atomic, we use the bit-band area to just affect the bit we're
|
|
|
|
// interested in.
|
2015-01-07 23:38:50 +00:00
|
|
|
EXTI_MODE_BB(pyb_extint_mode[line], line) = 1;
|
2015-07-28 19:13:33 +01:00
|
|
|
#endif
|
2014-03-15 06:41:28 +00:00
|
|
|
}
|
|
|
|
|
2014-04-18 22:38:09 +01:00
|
|
|
void extint_disable(uint line) {
|
2014-03-15 06:41:28 +00:00
|
|
|
if (line >= EXTI_NUM_VECTORS) {
|
|
|
|
return;
|
|
|
|
}
|
2015-07-28 19:13:33 +01:00
|
|
|
|
2015-08-03 23:07:20 +01:00
|
|
|
#if defined(MCU_SERIES_F7)
|
2015-07-28 19:13:33 +01:00
|
|
|
// The Cortex-M7 doesn't have bitband support.
|
|
|
|
mp_uint_t irq_state = disable_irq();
|
|
|
|
EXTI->IMR &= ~(1 << line);
|
|
|
|
EXTI->EMR &= ~(1 << line);
|
|
|
|
enable_irq(irq_state);
|
|
|
|
#else
|
2014-03-15 06:41:28 +00:00
|
|
|
// Since manipulating IMR/EMR is a read-modify-write, and we want this to
|
|
|
|
// be atomic, we use the bit-band area to just affect the bit we're
|
|
|
|
// interested in.
|
|
|
|
EXTI_MODE_BB(EXTI_Mode_Interrupt, line) = 0;
|
|
|
|
EXTI_MODE_BB(EXTI_Mode_Event, line) = 0;
|
2015-07-28 19:13:33 +01:00
|
|
|
#endif
|
2014-03-15 06:41:28 +00:00
|
|
|
}
|
|
|
|
|
2014-04-18 22:38:09 +01:00
|
|
|
void extint_swint(uint line) {
|
2014-03-15 06:41:28 +00:00
|
|
|
if (line >= EXTI_NUM_VECTORS) {
|
|
|
|
return;
|
|
|
|
}
|
2016-09-06 16:22:43 +01:00
|
|
|
// we need 0 to 1 transition to trigger the interrupt
|
2016-03-23 09:50:53 +00:00
|
|
|
#if defined(MCU_SERIES_L4)
|
2016-09-06 16:22:43 +01:00
|
|
|
EXTI->SWIER1 &= ~(1 << line);
|
|
|
|
EXTI->SWIER1 |= (1 << line);
|
2016-03-23 09:50:53 +00:00
|
|
|
#else
|
2016-09-06 16:22:43 +01:00
|
|
|
EXTI->SWIER &= ~(1 << line);
|
|
|
|
EXTI->SWIER |= (1 << line);
|
2016-03-23 09:50:53 +00:00
|
|
|
#endif
|
2014-03-15 06:41:28 +00:00
|
|
|
}
|
|
|
|
|
2014-04-29 22:55:34 +01:00
|
|
|
/// \method line()
|
|
|
|
/// Return the line number that the pin is mapped to.
|
2014-04-18 22:38:09 +01:00
|
|
|
STATIC mp_obj_t extint_obj_line(mp_obj_t self_in) {
|
|
|
|
extint_obj_t *self = self_in;
|
2014-03-15 06:41:28 +00:00
|
|
|
return MP_OBJ_NEW_SMALL_INT(self->line);
|
|
|
|
}
|
2014-04-29 23:00:48 +01:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(extint_obj_line_obj, extint_obj_line);
|
2014-03-15 06:41:28 +00:00
|
|
|
|
2014-04-29 22:55:34 +01:00
|
|
|
/// \method enable()
|
|
|
|
/// Enable a disabled interrupt.
|
2014-04-18 22:38:09 +01:00
|
|
|
STATIC mp_obj_t extint_obj_enable(mp_obj_t self_in) {
|
|
|
|
extint_obj_t *self = self_in;
|
|
|
|
extint_enable(self->line);
|
2014-03-15 06:41:28 +00:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
2014-04-29 22:55:34 +01:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(extint_obj_enable_obj, extint_obj_enable);
|
2014-03-15 06:41:28 +00:00
|
|
|
|
2014-04-29 22:55:34 +01:00
|
|
|
/// \method disable()
|
|
|
|
/// Disable the interrupt associated with the ExtInt object.
|
|
|
|
/// This could be useful for debouncing.
|
2014-04-18 22:38:09 +01:00
|
|
|
STATIC mp_obj_t extint_obj_disable(mp_obj_t self_in) {
|
|
|
|
extint_obj_t *self = self_in;
|
|
|
|
extint_disable(self->line);
|
2014-03-15 06:41:28 +00:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
2014-04-29 22:55:34 +01:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(extint_obj_disable_obj, extint_obj_disable);
|
2014-03-15 06:41:28 +00:00
|
|
|
|
2014-04-29 22:55:34 +01:00
|
|
|
/// \method swint()
|
|
|
|
/// Trigger the callback from software.
|
2014-04-18 22:38:09 +01:00
|
|
|
STATIC mp_obj_t extint_obj_swint(mp_obj_t self_in) {
|
|
|
|
extint_obj_t *self = self_in;
|
|
|
|
extint_swint(self->line);
|
2014-03-15 06:41:28 +00:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
2014-04-29 22:55:34 +01:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(extint_obj_swint_obj, extint_obj_swint);
|
2014-03-15 06:41:28 +00:00
|
|
|
|
2014-04-29 22:55:34 +01:00
|
|
|
// TODO document as a staticmethod
|
|
|
|
/// \classmethod regs()
|
|
|
|
/// Dump the values of the EXTI registers.
|
2014-04-18 22:38:09 +01:00
|
|
|
STATIC mp_obj_t extint_regs(void) {
|
2016-03-23 09:50:53 +00:00
|
|
|
#if defined(MCU_SERIES_L4)
|
|
|
|
printf("EXTI_IMR1 %08lx\n", EXTI->IMR1);
|
|
|
|
printf("EXTI_IMR2 %08lx\n", EXTI->IMR2);
|
|
|
|
printf("EXTI_EMR1 %08lx\n", EXTI->EMR1);
|
|
|
|
printf("EXTI_EMR2 %08lx\n", EXTI->EMR2);
|
|
|
|
printf("EXTI_RTSR1 %08lx\n", EXTI->RTSR1);
|
|
|
|
printf("EXTI_RTSR2 %08lx\n", EXTI->RTSR2);
|
|
|
|
printf("EXTI_FTSR1 %08lx\n", EXTI->FTSR1);
|
|
|
|
printf("EXTI_FTSR2 %08lx\n", EXTI->FTSR2);
|
|
|
|
printf("EXTI_SWIER1 %08lx\n", EXTI->SWIER1);
|
|
|
|
printf("EXTI_SWIER2 %08lx\n", EXTI->SWIER2);
|
|
|
|
printf("EXTI_PR1 %08lx\n", EXTI->PR1);
|
|
|
|
printf("EXTI_PR2 %08lx\n", EXTI->PR2);
|
|
|
|
#else
|
2014-03-15 06:41:28 +00:00
|
|
|
printf("EXTI_IMR %08lx\n", EXTI->IMR);
|
|
|
|
printf("EXTI_EMR %08lx\n", EXTI->EMR);
|
|
|
|
printf("EXTI_RTSR %08lx\n", EXTI->RTSR);
|
|
|
|
printf("EXTI_FTSR %08lx\n", EXTI->FTSR);
|
|
|
|
printf("EXTI_SWIER %08lx\n", EXTI->SWIER);
|
|
|
|
printf("EXTI_PR %08lx\n", EXTI->PR);
|
2016-03-23 09:50:53 +00:00
|
|
|
#endif
|
2014-03-15 06:41:28 +00:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
2014-04-29 22:55:34 +01:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_0(extint_regs_fun_obj, extint_regs);
|
|
|
|
STATIC MP_DEFINE_CONST_STATICMETHOD_OBJ(extint_regs_obj, (mp_obj_t)&extint_regs_fun_obj);
|
2014-03-15 06:41:28 +00:00
|
|
|
|
2014-04-29 22:55:34 +01:00
|
|
|
/// \classmethod \constructor(pin, mode, pull, callback)
|
|
|
|
/// Create an ExtInt object:
|
|
|
|
///
|
|
|
|
/// - `pin` is the pin on which to enable the interrupt (can be a pin object or any valid pin name).
|
|
|
|
/// - `mode` can be one of:
|
|
|
|
/// - `ExtInt.IRQ_RISING` - trigger on a rising edge;
|
|
|
|
/// - `ExtInt.IRQ_FALLING` - trigger on a falling edge;
|
|
|
|
/// - `ExtInt.IRQ_RISING_FALLING` - trigger on a rising or falling edge.
|
|
|
|
/// - `pull` can be one of:
|
|
|
|
/// - `pyb.Pin.PULL_NONE` - no pull up or down resistors;
|
|
|
|
/// - `pyb.Pin.PULL_UP` - enable the pull-up resistor;
|
|
|
|
/// - `pyb.Pin.PULL_DOWN` - enable the pull-down resistor.
|
|
|
|
/// - `callback` is the function to call when the interrupt triggers. The
|
|
|
|
/// callback function must accept exactly 1 argument, which is the line that
|
|
|
|
/// triggered the interrupt.
|
2014-04-26 11:19:17 +01:00
|
|
|
STATIC const mp_arg_t pyb_extint_make_new_args[] = {
|
|
|
|
{ MP_QSTR_pin, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
|
|
|
|
{ MP_QSTR_mode, MP_ARG_REQUIRED | MP_ARG_INT, {.u_int = 0} },
|
|
|
|
{ MP_QSTR_pull, MP_ARG_REQUIRED | MP_ARG_INT, {.u_int = 0} },
|
|
|
|
{ MP_QSTR_callback, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
|
2014-04-20 00:38:50 +01:00
|
|
|
};
|
2014-06-19 17:54:34 +01:00
|
|
|
#define PYB_EXTINT_MAKE_NEW_NUM_ARGS MP_ARRAY_SIZE(pyb_extint_make_new_args)
|
2014-04-20 00:38:50 +01:00
|
|
|
|
2017-01-04 13:10:42 +00:00
|
|
|
STATIC mp_obj_t extint_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
|
2014-04-18 22:38:09 +01:00
|
|
|
// type_in == extint_obj_type
|
2014-03-15 06:41:28 +00:00
|
|
|
|
2014-04-20 00:38:50 +01:00
|
|
|
// parse args
|
2014-04-26 11:19:17 +01:00
|
|
|
mp_arg_val_t vals[PYB_EXTINT_MAKE_NEW_NUM_ARGS];
|
2014-05-06 17:38:54 +01:00
|
|
|
mp_arg_parse_all_kw_array(n_args, n_kw, args, PYB_EXTINT_MAKE_NEW_NUM_ARGS, pyb_extint_make_new_args, vals);
|
2014-03-15 06:41:28 +00:00
|
|
|
|
2014-04-18 22:38:09 +01:00
|
|
|
extint_obj_t *self = m_new_obj(extint_obj_t);
|
2016-01-03 15:55:55 +00:00
|
|
|
self->base.type = type;
|
2015-01-07 23:38:50 +00:00
|
|
|
self->line = extint_register(vals[0].u_obj, vals[1].u_int, vals[2].u_int, vals[3].u_obj, false);
|
2014-03-15 06:41:28 +00:00
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2015-04-09 23:56:15 +01:00
|
|
|
STATIC void extint_obj_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
|
2014-04-18 22:38:09 +01:00
|
|
|
extint_obj_t *self = self_in;
|
2015-04-09 23:56:15 +01:00
|
|
|
mp_printf(print, "<ExtInt line=%u>", self->line);
|
2014-03-15 06:41:28 +00:00
|
|
|
}
|
|
|
|
|
2017-05-06 08:03:40 +01:00
|
|
|
STATIC const mp_rom_map_elem_t extint_locals_dict_table[] = {
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_line), MP_ROM_PTR(&extint_obj_line_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_enable), MP_ROM_PTR(&extint_obj_enable_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_disable), MP_ROM_PTR(&extint_obj_disable_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_swint), MP_ROM_PTR(&extint_obj_swint_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_regs), MP_ROM_PTR(&extint_regs_obj) },
|
2014-04-29 22:55:34 +01:00
|
|
|
|
|
|
|
// class constants
|
|
|
|
/// \constant IRQ_RISING - interrupt on a rising edge
|
|
|
|
/// \constant IRQ_FALLING - interrupt on a falling edge
|
|
|
|
/// \constant IRQ_RISING_FALLING - interrupt on a rising or falling edge
|
2017-05-06 08:03:40 +01:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_IRQ_RISING), MP_ROM_INT(GPIO_MODE_IT_RISING) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_IRQ_FALLING), MP_ROM_INT(GPIO_MODE_IT_FALLING) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_IRQ_RISING_FALLING), MP_ROM_INT(GPIO_MODE_IT_RISING_FALLING) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_EVT_RISING), MP_ROM_INT(GPIO_MODE_EVT_RISING) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_EVT_FALLING), MP_ROM_INT(GPIO_MODE_EVT_FALLING) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_EVT_RISING_FALLING), MP_ROM_INT(GPIO_MODE_EVT_RISING_FALLING) },
|
2014-04-18 22:38:09 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
STATIC MP_DEFINE_CONST_DICT(extint_locals_dict, extint_locals_dict_table);
|
|
|
|
|
|
|
|
const mp_obj_type_t extint_type = {
|
2014-03-26 22:35:55 +00:00
|
|
|
{ &mp_type_type },
|
2014-04-18 22:38:09 +01:00
|
|
|
.name = MP_QSTR_ExtInt,
|
|
|
|
.print = extint_obj_print,
|
|
|
|
.make_new = extint_make_new,
|
2017-05-06 08:03:40 +01:00
|
|
|
.locals_dict = (mp_obj_dict_t*)&extint_locals_dict,
|
2014-03-15 06:41:28 +00:00
|
|
|
};
|
|
|
|
|
2014-08-04 11:09:51 +01:00
|
|
|
void extint_init0(void) {
|
2015-01-07 23:38:50 +00:00
|
|
|
for (int i = 0; i < PYB_EXTI_NUM_VECTORS; i++) {
|
|
|
|
MP_STATE_PORT(pyb_extint_callback)[i] = mp_const_none;
|
|
|
|
pyb_extint_mode[i] = EXTI_Mode_Interrupt;
|
|
|
|
}
|
2014-03-15 06:41:28 +00:00
|
|
|
}
|
|
|
|
|
2014-04-08 15:21:26 +01:00
|
|
|
// Interrupt handler
|
2014-03-15 06:41:28 +00:00
|
|
|
void Handle_EXTI_Irq(uint32_t line) {
|
|
|
|
if (__HAL_GPIO_EXTI_GET_FLAG(1 << line)) {
|
|
|
|
__HAL_GPIO_EXTI_CLEAR_FLAG(1 << line);
|
|
|
|
if (line < EXTI_NUM_VECTORS) {
|
2015-01-07 23:38:50 +00:00
|
|
|
mp_obj_t *cb = &MP_STATE_PORT(pyb_extint_callback)[line];
|
|
|
|
if (*cb != mp_const_none) {
|
2017-06-15 08:17:36 +01:00
|
|
|
// If it's a soft IRQ handler then just schedule callback for later
|
|
|
|
if (!pyb_extint_hard_irq[line]) {
|
|
|
|
mp_sched_schedule(*cb, pyb_extint_callback_arg[line]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-02-15 12:04:53 +00:00
|
|
|
mp_sched_lock();
|
2014-04-08 15:21:26 +01:00
|
|
|
// When executing code within a handler we must lock the GC to prevent
|
|
|
|
// any memory allocations. We must also catch any exceptions.
|
|
|
|
gc_lock();
|
|
|
|
nlr_buf_t nlr;
|
|
|
|
if (nlr_push(&nlr) == 0) {
|
2017-06-15 08:17:36 +01:00
|
|
|
mp_call_function_1(*cb, pyb_extint_callback_arg[line]);
|
2014-04-08 15:21:26 +01:00
|
|
|
nlr_pop();
|
|
|
|
} else {
|
|
|
|
// Uncaught exception; disable the callback so it doesn't run again.
|
2015-01-07 23:38:50 +00:00
|
|
|
*cb = mp_const_none;
|
2014-04-18 22:38:09 +01:00
|
|
|
extint_disable(line);
|
|
|
|
printf("Uncaught exception in ExtInt interrupt handler line %lu\n", line);
|
2015-04-09 23:56:15 +01:00
|
|
|
mp_obj_print_exception(&mp_plat_print, (mp_obj_t)nlr.ret_val);
|
2014-04-08 15:21:26 +01:00
|
|
|
}
|
|
|
|
gc_unlock();
|
2017-02-15 12:04:53 +00:00
|
|
|
mp_sched_unlock();
|
2014-03-15 06:41:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|