1 //*****************************************************************************
2 //
3 // sysexc.c - Routines for the System Exception Module.
4 //
5 // Copyright (c) 2011-2020 Texas Instruments Incorporated. All rights reserved.
6 // Software License Agreement
7 //
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions
10 // are met:
11 //
12 // Redistributions of source code must retain the above copyright
13 // notice, this list of conditions and the following disclaimer.
14 //
15 // Redistributions in binary form must reproduce the above copyright
16 // notice, this list of conditions and the following disclaimer in the
17 // documentation and/or other materials provided with the
18 // distribution.
19 //
20 // Neither the name of Texas Instruments Incorporated nor the names of
21 // its contributors may be used to endorse or promote products derived
22 // from this software without specific prior written permission.
23 //
24 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 //
36 // This is part of revision 2.2.0.295 of the Tiva Peripheral Driver Library.
37 //
38 //*****************************************************************************
39
40 //*****************************************************************************
41 //
42 //! \addtogroup sysexc_api
43 //! @{
44 //
45 //*****************************************************************************
46
47 #include <stdbool.h>
48 #include <stdint.h>
49 #include "inc/hw_ints.h"
50 #include "inc/hw_sysctl.h"
51 #include "inc/hw_sysexc.h"
52 #include "inc/hw_types.h"
53 #include "driverlib/debug.h"
54 #include "driverlib/interrupt.h"
55
56 //*****************************************************************************
57 //
58 //! Returns the interrupt number for a system exception.
59 //!
60 //! This function returns the interrupt number for a system exception.
61 //!
62 //! \return Returns the system exception interrupt number.
63 //
64 //*****************************************************************************
65 static uint32_t
_SysExcIntNumberGet(void)66 _SysExcIntNumberGet(void)
67 {
68 uint32_t ui32Int;
69
70 //
71 // Get the interrupt number based on the class.
72 //
73 if(CLASS_IS_TM4C123)
74 {
75 ui32Int = INT_SYSEXC_TM4C123;
76 }
77 else if(CLASS_IS_TM4C129)
78 {
79 ui32Int = INT_SYSEXC_TM4C129;
80 }
81 else
82 {
83 ui32Int = 0;
84 }
85 return(ui32Int);
86 }
87
88 //*****************************************************************************
89 //
90 //! Registers an interrupt handler for the system exception interrupt.
91 //!
92 //! \param pfnHandler is a pointer to the function to be called when the system
93 //! exception interrupt occurs.
94 //!
95 //! This function places the address of the system exception interrupt handler
96 //! into the interrupt vector table in SRAM. This function also enables the
97 //! global interrupt in the interrupt controller; specific system exception
98 //! interrupts must be enabled via SysExcIntEnable(). It is the interrupt
99 //! handler's responsibility to clear the interrupt source.
100 //!
101 //! \sa IntRegister() for important information about registering interrupt
102 //! handlers.
103 //!
104 //! \return None.
105 //
106 //*****************************************************************************
107 void
SysExcIntRegister(void (* pfnHandler)(void))108 SysExcIntRegister(void (*pfnHandler)(void))
109 {
110 uint32_t ui32Int;
111
112 //
113 // Get the system exception interrupt number.
114 //
115 ui32Int = _SysExcIntNumberGet();
116
117 ASSERT(ui32Int != 0);
118
119 //
120 // Register the interrupt handler.
121 //
122 IntRegister(ui32Int, pfnHandler);
123
124 //
125 // Enable the system exception interrupt.
126 //
127 IntEnable(ui32Int);
128 }
129
130 //*****************************************************************************
131 //
132 //! Unregisters the system exception interrupt handler.
133 //!
134 //! This function removes the system exception interrupt handler from the
135 //! vector table in SRAM. This function also masks off the system exception
136 //! interrupt in the interrupt controller so that the interrupt handler is no
137 //! longer called.
138 //!
139 //! \sa IntRegister() for important information about registering interrupt
140 //! handlers.
141 //!
142 //! \return None.
143 //
144 //*****************************************************************************
145 void
SysExcIntUnregister(void)146 SysExcIntUnregister(void)
147 {
148 uint32_t ui32Int;
149
150 //
151 // Get the system exception interrupt number.
152 //
153 ui32Int = _SysExcIntNumberGet();
154
155 ASSERT(ui32Int != 0);
156
157 //
158 // Disable the system exception interrupt.
159 //
160 IntDisable(ui32Int);
161
162 //
163 // Unregister the system exception interrupt handler.
164 //
165 IntUnregister(ui32Int);
166 }
167
168 //*****************************************************************************
169 //
170 //! Enables individual system exception interrupt sources.
171 //!
172 //! \param ui32IntFlags is the bit mask of the interrupt sources to be enabled.
173 //!
174 //! This function enables the indicated system exception interrupt sources.
175 //! Only the sources that are enabled can be reflected to the processor
176 //! interrupt; disabled sources have no effect on the processor.
177 //!
178 //! The \e ui32IntFlags parameter is the logical OR of any of the following:
179 //!
180 //! - \b SYSEXC_INT_FP_IXC - Floating-point inexact exception interrupt
181 //! - \b SYSEXC_INT_FP_OFC - Floating-point overflow exception interrupt
182 //! - \b SYSEXC_INT_FP_UFC - Floating-point underflow exception interrupt
183 //! - \b SYSEXC_INT_FP_IOC - Floating-point invalid operation interrupt
184 //! - \b SYSEXC_INT_FP_DZC - Floating-point divide by zero exception interrupt
185 //! - \b SYSEXC_INT_FP_IDC - Floating-point input denormal exception interrupt
186 //!
187 //! \return None.
188 //
189 //*****************************************************************************
190 void
SysExcIntEnable(uint32_t ui32IntFlags)191 SysExcIntEnable(uint32_t ui32IntFlags)
192 {
193 //
194 // Enable the specified interrupts.
195 //
196 HWREG(SYSEXC_IM) |= ui32IntFlags;
197 }
198
199 //*****************************************************************************
200 //
201 //! Disables individual system exception interrupt sources.
202 //!
203 //! \param ui32IntFlags is the bit mask of the interrupt sources to be
204 //! disabled.
205 //!
206 //! This function disables the indicated system exception interrupt sources.
207 //! Only sources that are enabled can be reflected to the processor interrupt;
208 //! disabled sources have no effect on the processor.
209 //!
210 //! The \e ui32IntFlags parameter is the logical OR of any of the following:
211 //!
212 //! - \b SYSEXC_INT_FP_IXC - Floating-point inexact exception interrupt
213 //! - \b SYSEXC_INT_FP_OFC - Floating-point overflow exception interrupt
214 //! - \b SYSEXC_INT_FP_UFC - Floating-point underflow exception interrupt
215 //! - \b SYSEXC_INT_FP_IOC - Floating-point invalid operation interrupt
216 //! - \b SYSEXC_INT_FP_DZC - Floating-point divide by zero exception interrupt
217 //! - \b SYSEXC_INT_FP_IDC - Floating-point input denormal exception interrupt
218 //!
219 //! \return None.
220 //
221 //*****************************************************************************
222 void
SysExcIntDisable(uint32_t ui32IntFlags)223 SysExcIntDisable(uint32_t ui32IntFlags)
224 {
225 //
226 // Disable the specified interrupts.
227 //
228 HWREG(SYSEXC_IM) &= ~(ui32IntFlags);
229 }
230
231 //*****************************************************************************
232 //
233 //! Gets the current system exception interrupt status.
234 //!
235 //! \param bMasked is \b false if the raw interrupt status is required and
236 //! \b true if the masked interrupt status is required.
237 //!
238 //! This function returns the system exception interrupt status. Either the
239 //! raw interrupt status or the status of interrupts that are allowed to
240 //! reflect to the processor can be returned.
241 //!
242 //! \return Returns the current system exception interrupt status, enumerated
243 //! as the logical OR of \b SYSEXC_INT_FP_IXC, \b SYSEXC_INT_FP_OFC,
244 //! \b SYSEXC_INT_FP_UFC, \b SYSEXC_INT_FP_IOC, \b SYSEXC_INT_FP_DZC, and
245 //! \b SYSEXC_INT_FP_IDC.
246 //
247 //*****************************************************************************
248 uint32_t
SysExcIntStatus(bool bMasked)249 SysExcIntStatus(bool bMasked)
250 {
251 //
252 // Return either the interrupt status or the raw interrupt status as
253 // requested.
254 //
255 if(bMasked)
256 {
257 return(HWREG(SYSEXC_MIS));
258 }
259 else
260 {
261 return(HWREG(SYSEXC_RIS));
262 }
263 }
264
265 //*****************************************************************************
266 //
267 //! Clears system exception interrupt sources.
268 //!
269 //! \param ui32IntFlags is a bit mask of the interrupt sources to be cleared.
270 //!
271 //! This function clears the specified system exception interrupt sources, so
272 //! that they no longer assert. This function must be called in the interrupt
273 //! handler to keep the interrupt from being recognized again immediately upon
274 //! exit.
275 //!
276 //! The \e ui32IntFlags parameter is the logical OR of any of the following:
277 //!
278 //! - \b SYSEXC_INT_FP_IXC - Floating-point inexact exception interrupt
279 //! - \b SYSEXC_INT_FP_OFC - Floating-point overflow exception interrupt
280 //! - \b SYSEXC_INT_FP_UFC - Floating-point underflow exception interrupt
281 //! - \b SYSEXC_INT_FP_IOC - Floating-point invalid operation interrupt
282 //! - \b SYSEXC_INT_FP_DZC - Floating-point divide by zero exception interrupt
283 //! - \b SYSEXC_INT_FP_IDC - Floating-point input denormal exception interrupt
284 //!
285 //! \note Because there is a write buffer in the Cortex-M processor, it may
286 //! take several clock cycles before the interrupt source is actually cleared.
287 //! Therefore, it is recommended that the interrupt source be cleared early in
288 //! the interrupt handler (as opposed to the very last action) to avoid
289 //! returning from the interrupt handler before the interrupt source is
290 //! actually cleared. Failure to do so may result in the interrupt handler
291 //! being immediately reentered (because the interrupt controller still sees
292 //! the interrupt source asserted).
293 //!
294 //! \return None.
295 //
296 //*****************************************************************************
297 void
SysExcIntClear(uint32_t ui32IntFlags)298 SysExcIntClear(uint32_t ui32IntFlags)
299 {
300 //
301 // Clear the requested interrupt sources.
302 //
303 HWREG(SYSEXC_IC) = ui32IntFlags;
304 }
305
306 //*****************************************************************************
307 //
308 // Close the Doxygen group.
309 //! @}
310 //
311 //*****************************************************************************
312