1 /*
2 * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED.
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 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 * Description: PANIC module to rom implementation
15 * Author:
16 * Create: 2020-4-16
17 */
18
19 #include "panic.h"
20
21 #ifdef __cplusplus
22 #if __cplusplus
23 extern "C" {
24 #endif
25 #endif
26
27 #define BACK_TO_REAL_CALLER 5
28
29 static panic_deal_handler g_panic_deal = panic_deal;
30
panic_register_deal_callback(panic_deal_handler callback)31 void panic_register_deal_callback(panic_deal_handler callback)
32 {
33 if (callback != NULL) {
34 g_panic_deal = callback;
35 }
36 }
37
panic_unregister_deal_callback(void)38 void panic_unregister_deal_callback(void)
39 {
40 g_panic_deal = NULL;
41 }
42
panic_trigger_deal_callback(panic_id_t origin,uint32_t code,uint32_t caller)43 static void panic_trigger_deal_callback(panic_id_t origin, uint32_t code, uint32_t caller)
44 {
45 if (g_panic_deal != NULL) {
46 g_panic_deal(origin, code, caller);
47 } else {
48 // unlike the hardfault handler 'i' does not need to be static, since we trust our stack
49 volatile uint8_t i = 1;
50 // Loop forever - or until jlink changes i
51 while (i != 0) {
52 // Spin
53 }
54 }
55 }
56
panic(panic_id_t source,uint32_t code)57 void panic(panic_id_t source, uint32_t code)
58 {
59 #if defined(__GNUC__)
60 uint32_t caller = (uint32_t)((uintptr_t)__builtin_return_address(0)) - BACK_TO_REAL_CALLER;
61 #elif defined(__ICCARM__)
62 uint32_t caller = (uint32_t)__get_LR();
63 #endif
64
65 panic_trigger_deal_callback(source, code, caller);
66 }
67
68 #ifdef __cplusplus
69 #if __cplusplus
70 }
71 #endif
72 #endif
73