nvic.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. /**
  14. * @file common/ARMCMx/nvic.c
  15. * @brief Cortex-Mx NVIC support code.
  16. *
  17. * @addtogroup COMMON_ARMCMx_NVIC
  18. * @{
  19. */
  20. #include "hal.h"
  21. /*===========================================================================*/
  22. /* Driver local definitions. */
  23. /*===========================================================================*/
  24. /*===========================================================================*/
  25. /* Driver exported variables. */
  26. /*===========================================================================*/
  27. /*===========================================================================*/
  28. /* Driver local types. */
  29. /*===========================================================================*/
  30. /*===========================================================================*/
  31. /* Driver local variables. */
  32. /*===========================================================================*/
  33. /*===========================================================================*/
  34. /* Driver local functions. */
  35. /*===========================================================================*/
  36. /*===========================================================================*/
  37. /* Driver exported functions. */
  38. /*===========================================================================*/
  39. /**
  40. * @brief Sets the priority of an interrupt handler and enables it.
  41. *
  42. * @param[in] n the interrupt number
  43. * @param[in] prio the interrupt priority
  44. */
  45. void nvicEnableVector(uint32_t n, uint32_t prio) {
  46. #if defined(__CORE_CM0_H_GENERIC)
  47. NVIC->IP[_IP_IDX(n)] = (NVIC->IP[_IP_IDX(n)] & ~(0xFFU << _BIT_SHIFT(n))) |
  48. (NVIC_PRIORITY_MASK(prio) << _BIT_SHIFT(n));
  49. #else
  50. NVIC->IP[n] = NVIC_PRIORITY_MASK(prio);
  51. #endif
  52. NVIC->ICPR[n >> 5U] = 1U << (n & 0x1FU);
  53. NVIC->ISER[n >> 5U] = 1U << (n & 0x1FU);
  54. }
  55. /**
  56. * @brief Disables an interrupt handler.
  57. *
  58. * @param[in] n the interrupt number
  59. */
  60. void nvicDisableVector(uint32_t n) {
  61. NVIC->ICER[n >> 5U] = 1U << (n & 0x1FU);
  62. #if defined(__CORE_CM0_H_GENERIC)
  63. NVIC->IP[_IP_IDX(n)] = NVIC->IP[_IP_IDX(n)] & ~(0xFFU << _BIT_SHIFT(n));
  64. #else
  65. NVIC->IP[n] = 0U;
  66. #endif
  67. }
  68. /**
  69. * @brief Changes the priority of a system handler.
  70. *
  71. * @param[in] handler the system handler number
  72. * @param[in] prio the system handler priority
  73. */
  74. void nvicSetSystemHandlerPriority(uint32_t handler, uint32_t prio) {
  75. osalDbgCheck(handler < 12U);
  76. #if defined(__CORE_CM0_H_GENERIC)
  77. SCB->SHP[_SHP_IDX(handler)] = (SCB->SHP[_SHP_IDX(handler)] & ~(0xFFU << _BIT_SHIFT(handler))) |
  78. (NVIC_PRIORITY_MASK(prio) << _BIT_SHIFT(handler));
  79. #elif defined(__CORE_CM7_H_GENERIC)
  80. SCB->SHPR[handler] = NVIC_PRIORITY_MASK(prio);
  81. #else
  82. SCB->SHP[handler] = NVIC_PRIORITY_MASK(prio);
  83. #endif
  84. }
  85. /**
  86. * @brief Clears a pending interrupt source.
  87. *
  88. * @param[in] n the interrupt number
  89. */
  90. void nvicClearPending(uint32_t n) {
  91. NVIC->ICPR[n >> 5] = 1 << (n & 0x1F);
  92. }
  93. /** @} */