irq.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * This file is part of the MicroPython 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. #include "py/obj.h"
  27. #include "py/mphal.h"
  28. #include "irq.h"
  29. /// \moduleref pyb
  30. #if IRQ_ENABLE_STATS
  31. uint32_t irq_stats[FPU_IRQn + 1] = {0};
  32. #endif
  33. /// \function wfi()
  34. /// Wait for an interrupt.
  35. /// This executies a `wfi` instruction which reduces power consumption
  36. /// of the MCU until an interrupt occurs, at which point execution continues.
  37. STATIC mp_obj_t pyb_wfi(void) {
  38. __WFI();
  39. return mp_const_none;
  40. }
  41. MP_DEFINE_CONST_FUN_OBJ_0(pyb_wfi_obj, pyb_wfi);
  42. /// \function disable_irq()
  43. /// Disable interrupt requests.
  44. /// Returns the previous IRQ state: `False`/`True` for disabled/enabled IRQs
  45. /// respectively. This return value can be passed to enable_irq to restore
  46. /// the IRQ to its original state.
  47. STATIC mp_obj_t pyb_disable_irq(void) {
  48. return mp_obj_new_bool(disable_irq() == IRQ_STATE_ENABLED);
  49. }
  50. MP_DEFINE_CONST_FUN_OBJ_0(pyb_disable_irq_obj, pyb_disable_irq);
  51. /// \function enable_irq(state=True)
  52. /// Enable interrupt requests.
  53. /// If `state` is `True` (the default value) then IRQs are enabled.
  54. /// If `state` is `False` then IRQs are disabled. The most common use of
  55. /// this function is to pass it the value returned by `disable_irq` to
  56. /// exit a critical section.
  57. STATIC mp_obj_t pyb_enable_irq(uint n_args, const mp_obj_t *arg) {
  58. enable_irq((n_args == 0 || mp_obj_is_true(arg[0])) ? IRQ_STATE_ENABLED : IRQ_STATE_DISABLED);
  59. return mp_const_none;
  60. }
  61. MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_enable_irq_obj, 0, 1, pyb_enable_irq);
  62. #if IRQ_ENABLE_STATS
  63. // return a memoryview of the irq statistics array
  64. STATIC mp_obj_t pyb_irq_stats(void) {
  65. return mp_obj_new_memoryview(0x80 | 'I', MP_ARRAY_SIZE(irq_stats), &irq_stats[0]);
  66. }
  67. MP_DEFINE_CONST_FUN_OBJ_0(pyb_irq_stats_obj, pyb_irq_stats);
  68. #endif