core_irq.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Copyright (c) 2022 OpenLuat & AirM2M
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  5. * this software and associated documentation files (the "Software"), to deal in
  6. * the Software without restriction, including without limitation the rights to
  7. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  8. * the Software, and to permit persons to whom the Software is furnished to do so,
  9. * subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in all
  12. * copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  16. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  17. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  18. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  19. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. */
  21. #include "user.h"
  22. #define __IRQ_IN_RAM__
  23. #ifdef __IRQ_IN_RAM__
  24. #else
  25. #define __FUNC_IN_RAM__
  26. #endif
  27. typedef struct {
  28. void (*Irq_Handler)(int32_t IrqLine, void *pData);
  29. void *pData;
  30. }Irq_Handler_t;
  31. static Irq_Handler_t Irq_Table[IRQ_LINE_MAX + 16 - IRQ_LINE_OFFSET];
  32. static void ISR_DummyHandler(int32_t IrqLine, void *pData)
  33. {
  34. }
  35. void __FUNC_IN_RAM__ ISR_GlobalHandler(void)
  36. {
  37. int IrqLine = ((SCB->ICSR & SCB_ICSR_VECTACTIVE_Msk) >> SCB_ICSR_VECTACTIVE_Pos) - IRQ_LINE_OFFSET;
  38. if (IrqLine < 0 || IrqLine >= IRQ_LINE_MAX)
  39. {
  40. return;
  41. }
  42. if (Irq_Table[IrqLine].Irq_Handler)
  43. {
  44. Irq_Table[IrqLine].Irq_Handler(IrqLine + IRQ_LINE_OFFSET - 16, Irq_Table[IrqLine].pData);
  45. }
  46. }
  47. void ISR_SetHandler(int32_t Irq, void *Handler, void *pData)
  48. {
  49. Irq_Table[Irq + 16 - IRQ_LINE_OFFSET].Irq_Handler = Handler;
  50. Irq_Table[Irq + 16 - IRQ_LINE_OFFSET].pData = pData;
  51. }
  52. void ISR_SetPriority(int32_t Irq, uint32_t PriorityLevel)
  53. {
  54. NVIC_SetPriority(Irq, PriorityLevel);
  55. }
  56. void __FUNC_IN_RAM__ ISR_OnOff(int32_t Irq, uint32_t OnOff)
  57. {
  58. if (OnOff)
  59. {
  60. if (!Irq_Table[Irq + 16 - IRQ_LINE_OFFSET].Irq_Handler)
  61. {
  62. Irq_Table[Irq + 16 - IRQ_LINE_OFFSET].Irq_Handler = ISR_DummyHandler;
  63. }
  64. NVIC_EnableIRQ(Irq);
  65. }
  66. else
  67. {
  68. NVIC_DisableIRQ(Irq);
  69. }
  70. }
  71. void __FUNC_IN_RAM__ ISR_Clear(int32_t Irq)
  72. {
  73. NVIC_ClearPendingIRQ(Irq);
  74. }
  75. uint32_t __FUNC_IN_RAM__ ISR_CheckIn(void)
  76. {
  77. return __get_IPSR();
  78. }