summaryrefslogtreecommitdiff
path: root/source/hal/source/components/cmsis_device/source/handlers.c
blob: e6ff0d7f72ceb2a3c5f75d40d6c3fb4311a1c255 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
 * SPDX-FileCopyrightText: Copyright 2021 Arm Limited and/or its affiliates <open-source-office@arm.com>
 * SPDX-License-Identifier: Apache-2.0
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#ifdef __cplusplus
extern "C"
{
#endif

#include "RTE_Components.h"

#include <stdio.h>
#include <inttypes.h>

/**
 * @brief   Dump core registers on stdout
 */
static void LogCoreCPURegisters(void)
{
    printf("CTRL    : 0x%08" PRIx32 "\n", __get_CONTROL());
    printf("IPSR    : 0x%08" PRIx32 "\n", __get_IPSR());
    printf("APSR    : 0x%08" PRIx32 "\n", __get_APSR());
    printf("xPSR    : 0x%08" PRIx32 "\n", __get_xPSR());
    printf("PSP     : 0x%08" PRIx32 "\n", __get_PSP());
    printf("MSP     : 0x%08" PRIx32 "\n", __get_MSP());
    printf("PRIMASK : 0x%08" PRIx32 "\n", __get_PRIMASK());
    printf("BASEPRI : 0x%08" PRIx32 "\n", __get_BASEPRI());
    printf("FAULTMSK: 0x%08" PRIx32 "\n", __get_FAULTMASK());
}

/**
 * @brief   Default interrupt handler - an infinite loop.
 **/
__attribute__((noreturn)) static void DefaultHandler(void)
{
    LogCoreCPURegisters();
    while (1) {
        /* Without the following line, armclang may optimize away the
         * infinite loop because it'd be without side effects and thus
         * undefined behaviour. */
        __ASM volatile("");
    }
}

#define DEFAULT_HANDLER_CALL(type)              \
    do {                                        \
        printf("\n");                           \
        printf("%s caught by function %s\n",    \
             type, __FUNCTION__);               \
        DefaultHandler();                       \
    } while (0)

#define DEFAULT_ERROR_HANDLER_CALL()            \
            DEFAULT_HANDLER_CALL("Exception")

#define DEFAULT_IRQ_HANDLER_CALL()              \
            DEFAULT_HANDLER_CALL("Interrupt")

/**
 * Placeholder Exception Handlers for core interrupts.
 *
 * Weak definitions provided to be used if the user chooses not
 * to override them.
 **/

/**
 * @brief  Non maskable interrupt handler.
 **/
__attribute__((weak)) void NMI_Handler(void)
{
    DEFAULT_ERROR_HANDLER_CALL();
}

/**
 * @brief  Hardfault interrupt handler.
 **/
__attribute__((weak)) void HardFault_Handler(void)
{
    DEFAULT_ERROR_HANDLER_CALL();
}

/**
 * @brief  Memory management interrupt handler.
 **/
__attribute__((weak)) void MemManage_Handler(void)
{
    DEFAULT_IRQ_HANDLER_CALL();
}

/**
 * @brief  Bus fault interrupt handler.
 **/
__attribute__((weak)) void BusFault_Handler(void)
{
    DEFAULT_ERROR_HANDLER_CALL();
}

/**
 * @brief  Usage fault interrupt handler.
 **/
__attribute__((weak)) void UsageFault_Handler(void)
{
    DEFAULT_ERROR_HANDLER_CALL();
}

/**
 * @brief  Secure access fault interrupt handler.
 **/
__attribute__((weak)) void SecureFault_Handler(void)
{
    DEFAULT_ERROR_HANDLER_CALL();
}

/**
 * @brief  Supervisor call interrupt handler.
 **/
__attribute__((weak)) void SVC_Handler(void)
{
    DEFAULT_IRQ_HANDLER_CALL();
}

/**
 * @brief  Debug monitor interrupt handler.
 **/
__attribute__((weak)) void DebugMon_Handler(void)
{
    DEFAULT_IRQ_HANDLER_CALL();
}

/**
 * @brief  Pending SV call interrupt handler.
 */
__attribute__((weak)) void PendSV_Handler(void)
{
    DEFAULT_IRQ_HANDLER_CALL();
}

#ifdef __cplusplus
}
#endif