summaryrefslogtreecommitdiffstatshomepage
path: root/stmhal/irq.c
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2014-08-25 13:24:33 +0100
committerDamien George <damien.p.george@gmail.com>2014-08-25 13:24:33 +0100
commite5cbb70328239e58c820938eccd51f3c735fc312 (patch)
treec6f1f9d9f83327ab595e14276d4f4ab9f9a56c5d /stmhal/irq.c
parent9480138f0c917b2400991066e94636f7d8076a4c (diff)
downloadmicropython-e5cbb70328239e58c820938eccd51f3c735fc312.tar.gz
micropython-e5cbb70328239e58c820938eccd51f3c735fc312.zip
stmhal: Make enable_irq and disable_irq inline functions.
These functions are generally 1 machine instruction, and are used in critical code, so makes sense to have them inline. Also leave these functions uninverted (ie 0 means enable, 1 means disable) and provide macro constants if you really need to distinguish the states. This makes for smaller code as well (combined with inlining). Applied to teensy port as well.
Diffstat (limited to 'stmhal/irq.c')
-rw-r--r--stmhal/irq.c28
1 files changed, 10 insertions, 18 deletions
diff --git a/stmhal/irq.c b/stmhal/irq.c
index 2d6a990beb..c08f2fe094 100644
--- a/stmhal/irq.c
+++ b/stmhal/irq.c
@@ -33,17 +33,6 @@
#include MICROPY_HAL_H
-void enable_irq(mp_int_t enable) {
- // With __set_PRIMASK 1 = disable, 0 = enable
- __set_PRIMASK(!enable);
-}
-
-mp_int_t disable_irq(void) {
- mp_int_t enabled = !__get_PRIMASK();
- __disable_irq();
- return enabled;
-}
-
/// \function wfi()
/// Wait for an interrupt.
/// This executies a `wfi` instruction which reduces power consumption
@@ -56,19 +45,22 @@ MP_DEFINE_CONST_FUN_OBJ_0(pyb_wfi_obj, pyb_wfi);
/// \function disable_irq()
/// Disable interrupt requests.
+/// Returns the previous IRQ state: `False`/`True` for disabled/enabled IRQs
+/// respectively. This return value can be passed to enable_irq to restore
+/// the IRQ to its original state.
STATIC mp_obj_t pyb_disable_irq(void) {
- return MP_BOOL(disable_irq());
+ return MP_BOOL(disable_irq() == IRQ_STATE_ENABLED);
}
MP_DEFINE_CONST_FUN_OBJ_0(pyb_disable_irq_obj, pyb_disable_irq);
-/// \function enable_irq()
+/// \function enable_irq(state=True)
/// Enable interrupt requests.
+/// If `state` is `True` (the default value) then IRQs are enabled.
+/// If `state` is `False` then IRQs are disabled. The most common use of
+/// this function is to pass it the value returned by `disable_irq` to
+/// exit a critical section.
STATIC mp_obj_t pyb_enable_irq(uint n_args, const mp_obj_t *arg) {
- mp_int_t enabled = true;
- if (n_args > 0) {
- enabled = mp_obj_is_true(arg[0]);
- }
- enable_irq(enabled);
+ enable_irq((n_args == 0 || mp_obj_is_true(arg[0])) ? IRQ_STATE_ENABLED : IRQ_STATE_DISABLED);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_enable_irq_obj, 0, 1, pyb_enable_irq);