1 //*****************************************************************************
2 //
3 //! @file am_hal_fault.c
4 //!
5 //! @brief Functions for interfacing with the fault control.
6 //!
7 //! @addtogroup fault_4p Fault - CPU Fault Control
8 //! @ingroup apollo4p_hal
9 //! @{
10 //
11 //*****************************************************************************
12
13 //*****************************************************************************
14 //
15 // Copyright (c) 2023, Ambiq Micro, Inc.
16 // All rights reserved.
17 //
18 // Redistribution and use in source and binary forms, with or without
19 // modification, are permitted provided that the following conditions are met:
20 //
21 // 1. Redistributions of source code must retain the above copyright notice,
22 // this list of conditions and the following disclaimer.
23 //
24 // 2. Redistributions in binary form must reproduce the above copyright
25 // notice, this list of conditions and the following disclaimer in the
26 // documentation and/or other materials provided with the distribution.
27 //
28 // 3. Neither the name of the copyright holder nor the names of its
29 // contributors may be used to endorse or promote products derived from this
30 // software without specific prior written permission.
31 //
32 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
33 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
36 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
37 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
38 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
39 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
40 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
41 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42 // POSSIBILITY OF SUCH DAMAGE.
43 //
44 // This is part of revision release_sdk_4_4_0-3c5977e664 of the AmbiqSuite Development Package.
45 //
46 //*****************************************************************************
47
48 #include <stdint.h>
49 #include <stdbool.h>
50 #include "am_mcu_apollo.h"
51
52 // ****************************************************************************
53 //
54 // am_hal_fault_capture_enable()
55 // This function is used to enable fault capture on the CPU block.
56 //
57 // ****************************************************************************
58 uint32_t
am_hal_fault_capture_enable(void)59 am_hal_fault_capture_enable(void)
60 {
61 //
62 // Enable the Fault Capture registers.
63 //
64 CPU->FAULTCAPTUREEN_b.FAULTCAPTUREEN = CPU_FAULTCAPTUREEN_FAULTCAPTUREEN_EN;
65
66 //
67 // Return success status.
68 //
69 return AM_HAL_STATUS_SUCCESS;
70 } // am_hal_fault_capture_enable()
71
72 // ****************************************************************************
73 //
74 // am_hal_fault_capture_disable()
75 // This function is used to disable fault capture on the CPU block.
76 //
77 // ****************************************************************************
78 uint32_t
am_hal_fault_capture_disable(void)79 am_hal_fault_capture_disable(void)
80 {
81 //
82 // Enable the Fault Capture registers.
83 //
84 CPU->FAULTCAPTUREEN_b.FAULTCAPTUREEN = CPU_FAULTCAPTUREEN_FAULTCAPTUREEN_DIS;
85
86 //
87 // Return success status.
88 //
89 return AM_HAL_STATUS_SUCCESS;
90 } // am_hal_fault_capture_disable()
91
92 // ****************************************************************************
93 //
94 // am_hal_fault_status_get()
95 // This function returns current fault status as obtained from the CPU block
96 // in Apollo4.
97 //
98 // ****************************************************************************
99 uint32_t
am_hal_fault_status_get(am_hal_fault_status_t * pFaultStatus)100 am_hal_fault_status_get(am_hal_fault_status_t *pFaultStatus)
101 {
102 uint32_t ui32FaultStat;
103
104 if ( pFaultStatus == NULL )
105 {
106 return AM_HAL_STATUS_INVALID_ARG;
107 }
108
109 //
110 // Read the Fault Status Register.
111 //
112 ui32FaultStat = CPU->FAULTSTATUS;
113 pFaultStatus->bICODE = (bool)(ui32FaultStat & CPU_FAULTSTATUS_ICODEFAULT_Msk);
114 pFaultStatus->bDCODE = (bool)(ui32FaultStat & CPU_FAULTSTATUS_DCODEFAULT_Msk);
115 pFaultStatus->bSYS = (bool)(ui32FaultStat & CPU_FAULTSTATUS_SYSFAULT_Msk);
116
117 //
118 // Read the DCODE fault capture address register.
119 //
120 pFaultStatus->ui32DCODE = CPU->DCODEFAULTADDR;
121
122 //
123 // Read the ICODE fault capture address register.
124 //
125 pFaultStatus->ui32ICODE |= CPU->ICODEFAULTADDR;
126
127 //
128 // Read the ICODE fault capture address register.
129 //
130 pFaultStatus->ui32SYS |= CPU->SYSFAULTADDR;
131
132 //
133 // Return success status.
134 //
135 return AM_HAL_STATUS_SUCCESS;
136 } // am_hal_fault_status_get()
137
138 //*****************************************************************************
139 //
140 // End Doxygen group.
141 //! @}
142 //
143 //*****************************************************************************
144