xtensa_intr.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * FreeRTOS Kernel V10.4.6
  3. * Copyright (C) 2015-2019 Cadence Design Systems, Inc.
  4. * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  5. *
  6. * SPDX-License-Identifier: MIT
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  9. * this software and associated documentation files (the "Software"), to deal in
  10. * the Software without restriction, including without limitation the rights to
  11. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  12. * the Software, and to permit persons to whom the Software is furnished to do so,
  13. * subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in all
  16. * 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, FITNESS
  20. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  21. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  22. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  23. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. *
  25. * https://www.FreeRTOS.org
  26. * https://github.com/FreeRTOS
  27. *
  28. */
  29. /*
  30. * Xtensa-specific interrupt and exception functions for RTOS ports.
  31. * Also see xtensa_intr_asm.S.
  32. */
  33. #include <stdlib.h>
  34. #include <xtensa/config/core.h>
  35. #include "xtensa_api.h"
  36. #if XCHAL_HAVE_EXCEPTIONS
  37. /* Handler table is in xtensa_intr_asm.S */
  38. extern xt_exc_handler _xt_exception_table[XCHAL_EXCCAUSE_NUM];
  39. /*
  40. Default handler for unhandled exceptions.
  41. */
  42. void xt_unhandled_exception(XtExcFrame *frame)
  43. {
  44. exit(-1);
  45. }
  46. /*
  47. This function registers a handler for the specified exception.
  48. The function returns the address of the previous handler.
  49. On error, it returns 0.
  50. */
  51. xt_exc_handler xt_set_exception_handler(int n, xt_exc_handler f)
  52. {
  53. xt_exc_handler old;
  54. if( n < 0 || n >= XCHAL_EXCCAUSE_NUM )
  55. return 0; /* invalid exception number */
  56. old = _xt_exception_table[n];
  57. if (f) {
  58. _xt_exception_table[n] = f;
  59. }
  60. else {
  61. _xt_exception_table[n] = &xt_unhandled_exception;
  62. }
  63. return ((old == &xt_unhandled_exception) ? 0 : old);
  64. }
  65. #endif
  66. #if XCHAL_HAVE_INTERRUPTS
  67. /* Handler table is in xtensa_intr_asm.S */
  68. typedef struct xt_handler_table_entry {
  69. void * handler;
  70. void * arg;
  71. } xt_handler_table_entry;
  72. extern xt_handler_table_entry _xt_interrupt_table[XCHAL_NUM_INTERRUPTS];
  73. /*
  74. Default handler for unhandled interrupts.
  75. */
  76. void xt_unhandled_interrupt(void * arg)
  77. {
  78. exit(-1);
  79. }
  80. /*
  81. This function registers a handler for the specified interrupt. The "arg"
  82. parameter specifies the argument to be passed to the handler when it is
  83. invoked. The function returns the address of the previous handler.
  84. On error, it returns 0.
  85. */
  86. xt_handler xt_set_interrupt_handler(int n, xt_handler f, void * arg)
  87. {
  88. xt_handler_table_entry * entry;
  89. xt_handler old;
  90. if( n < 0 || n >= XCHAL_NUM_INTERRUPTS )
  91. return 0; /* invalid interrupt number */
  92. if( Xthal_intlevel[n] > XCHAL_EXCM_LEVEL )
  93. return 0; /* priority level too high to safely handle in C */
  94. entry = _xt_interrupt_table + n;
  95. old = entry->handler;
  96. if (f) {
  97. entry->handler = f;
  98. entry->arg = arg;
  99. }
  100. else {
  101. entry->handler = &xt_unhandled_interrupt;
  102. entry->arg = (void*)n;
  103. }
  104. return ((old == &xt_unhandled_interrupt) ? 0 : old);
  105. }
  106. #endif /* XCHAL_HAVE_INTERRUPTS */