1 //*****************************************************************************
2 //
3 // sysexc.c - Routines for the System Exception Module.
4 //
5 // Copyright (c) 2011-2017 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 //*****************************************************************************
37 
38 //*****************************************************************************
39 //
40 //! \addtogroup sysexc_api
41 //! @{
42 //
43 //*****************************************************************************
44 
45 #include "types.h"
46 #include <stdbool.h>
47 #include <stdint.h>
48 #include "inc/hw_sysctl.h"
49 #include "inc/hw_sysexc.h"
50 #include "debug.h"
51 #include "interrupt.h"
52 
53 //*****************************************************************************
54 //
55 //! Returns the interrupt number for a system exception.
56 //!
57 //! This function returns the interrupt number for a system exception.
58 //!
59 //! \return Returns the system exception interrupt number.
60 //
61 //*****************************************************************************
62 static uint32_t
_SysExcIntNumberGet(void)63 _SysExcIntNumberGet(void)
64 {
65     uint32_t ui32Int;
66 
67     //
68     // Get the interrupt number based on the class.
69     //
70     ui32Int = INT_SYSEXC;
71     return (ui32Int);
72 }
73 
74 //*****************************************************************************
75 //
76 //! Registers an interrupt handler for the system exception interrupt.
77 //!
78 //! \param pfnHandler is a pointer to the function to be called when the system
79 //! exception interrupt occurs.
80 //!
81 //! This function places the address of the system exception interrupt handler
82 //! into the interrupt vector table in SRAM.  This function also enables the
83 //! global interrupt in the interrupt controller; specific system exception
84 //! interrupts must be enabled via SysExcIntEnable().  It is the interrupt
85 //! handler's responsibility to clear the interrupt source.
86 //!
87 //! \sa IntRegister() for important information about registering interrupt
88 //! handlers.
89 //!
90 //! \return None.
91 //
92 //*****************************************************************************
93 void
SysExcIntRegister(void (* pfnHandler)(void))94 SysExcIntRegister(void (*pfnHandler)(void))
95 {
96     uint32_t ui32Int;
97 
98     //
99     // Get the system exception interrupt number.
100     //
101     ui32Int = _SysExcIntNumberGet();
102 
103     ASSERT(ui32Int != 0);
104 
105     //
106     // Register the interrupt handler.
107     //
108     IntRegister(ui32Int, pfnHandler);
109 
110     //
111     // Enable the system exception interrupt.
112     //
113     IntEnable(ui32Int);
114 }
115 
116 //*****************************************************************************
117 //
118 //! Unregisters the system exception interrupt handler.
119 //!
120 //! This function removes the system exception interrupt handler from the
121 //! vector table in SRAM.  This function also masks off the system exception
122 //! interrupt in the interrupt controller so that the interrupt handler is no
123 //! longer called.
124 //!
125 //! \sa IntRegister() for important information about registering interrupt
126 //! handlers.
127 //!
128 //! \return None.
129 //
130 //*****************************************************************************
131 void
SysExcIntUnregister(void)132 SysExcIntUnregister(void)
133 {
134     uint32_t ui32Int;
135 
136     //
137     // Get the system exception interrupt number.
138     //
139     ui32Int = _SysExcIntNumberGet();
140 
141     ASSERT(ui32Int != 0);
142 
143     //
144     // Disable the system exception interrupt.
145     //
146     IntDisable(ui32Int);
147 
148     //
149     // Unregister the system exception interrupt handler.
150     //
151     IntUnregister(ui32Int);
152 }
153 
154 //*****************************************************************************
155 //
156 //! Enables individual system exception interrupt sources.
157 //!
158 //! \param ui32IntFlags is the bit mask of the interrupt sources to be enabled.
159 //!
160 //! This function enables the indicated system exception interrupt sources.
161 //! Only the sources that are enabled can be reflected to the processor
162 //! interrupt; disabled sources have no effect on the processor.
163 //!
164 //! The \e ui32IntFlags parameter is the logical OR of any of the following:
165 //!
166 //! - \b SYSEXC_INT_FP_IXC - Floating-point inexact exception interrupt
167 //! - \b SYSEXC_INT_FP_OFC - Floating-point overflow exception interrupt
168 //! - \b SYSEXC_INT_FP_UFC - Floating-point underflow exception interrupt
169 //! - \b SYSEXC_INT_FP_IOC - Floating-point invalid operation interrupt
170 //! - \b SYSEXC_INT_FP_DZC - Floating-point divide by zero exception interrupt
171 //! - \b SYSEXC_INT_FP_IDC - Floating-point input denormal exception interrupt
172 //!
173 //! \return None.
174 //
175 //*****************************************************************************
176 void
SysExcIntEnable(uint32_t ui32IntFlags)177 SysExcIntEnable(uint32_t ui32IntFlags)
178 {
179     //
180     // Enable the specified interrupts.
181     //
182     HWREG(SYSEXC_IM) |= ui32IntFlags;
183 }
184 
185 //*****************************************************************************
186 //
187 //! Disables individual system exception interrupt sources.
188 //!
189 //! \param ui32IntFlags is the bit mask of the interrupt sources to be
190 //! disabled.
191 //!
192 //! This function disables the indicated system exception interrupt sources.
193 //! Only sources that are enabled can be reflected to the processor interrupt;
194 //! disabled sources have no effect on the processor.
195 //!
196 //! The \e ui32IntFlags parameter is the logical OR of any of the following:
197 //!
198 //! - \b SYSEXC_INT_FP_IXC - Floating-point inexact exception interrupt
199 //! - \b SYSEXC_INT_FP_OFC - Floating-point overflow exception interrupt
200 //! - \b SYSEXC_INT_FP_UFC - Floating-point underflow exception interrupt
201 //! - \b SYSEXC_INT_FP_IOC - Floating-point invalid operation interrupt
202 //! - \b SYSEXC_INT_FP_DZC - Floating-point divide by zero exception interrupt
203 //! - \b SYSEXC_INT_FP_IDC - Floating-point input denormal exception interrupt
204 //!
205 //! \return None.
206 //
207 //*****************************************************************************
208 void
SysExcIntDisable(uint32_t ui32IntFlags)209 SysExcIntDisable(uint32_t ui32IntFlags)
210 {
211     //
212     // Disable the specified interrupts.
213     //
214     HWREG(SYSEXC_IM) &= ~(ui32IntFlags);
215 }
216 
217 //*****************************************************************************
218 //
219 //! Gets the current system exception interrupt status.
220 //!
221 //! \param bMasked is \b false if the raw interrupt status is required and
222 //! \b true if the masked interrupt status is required.
223 //!
224 //! This function returns the system exception interrupt status.  Either the
225 //! raw interrupt status or the status of interrupts that are allowed to
226 //! reflect to the processor can be returned.
227 //!
228 //! \return Returns the current system exception interrupt status, enumerated
229 //! as the logical OR of \b SYSEXC_INT_FP_IXC, \b SYSEXC_INT_FP_OFC,
230 //! \b SYSEXC_INT_FP_UFC, \b SYSEXC_INT_FP_IOC, \b SYSEXC_INT_FP_DZC, and
231 //! \b SYSEXC_INT_FP_IDC.
232 //
233 //*****************************************************************************
234 uint32_t
SysExcIntStatus(bool bMasked)235 SysExcIntStatus(bool bMasked)
236 {
237     //
238     // Return either the interrupt status or the raw interrupt status as
239     // requested.
240     //
241     if (bMasked)
242     {
243         return (HWREG(SYSEXC_MIS));
244     }
245     else
246     {
247         return (HWREG(SYSEXC_RIS));
248     }
249 }
250 
251 //*****************************************************************************
252 //
253 //! Clears system exception interrupt sources.
254 //!
255 //! \param ui32IntFlags is a bit mask of the interrupt sources to be cleared.
256 //!
257 //! This function clears the specified system exception interrupt sources, so
258 //! that they no longer assert.  This function must be called in the interrupt
259 //! handler to keep the interrupt from being recognized again immediately upon
260 //! exit.
261 //!
262 //! The \e ui32IntFlags parameter is the logical OR of any of the following:
263 //!
264 //! - \b SYSEXC_INT_FP_IXC - Floating-point inexact exception interrupt
265 //! - \b SYSEXC_INT_FP_OFC - Floating-point overflow exception interrupt
266 //! - \b SYSEXC_INT_FP_UFC - Floating-point underflow exception interrupt
267 //! - \b SYSEXC_INT_FP_IOC - Floating-point invalid operation interrupt
268 //! - \b SYSEXC_INT_FP_DZC - Floating-point divide by zero exception interrupt
269 //! - \b SYSEXC_INT_FP_IDC - Floating-point input denormal exception interrupt
270 //!
271 //! \note Because there is a write buffer in the Cortex-M processor, it may
272 //! take several clock cycles before the interrupt source is actually cleared.
273 //! Therefore, it is recommended that the interrupt source be cleared early in
274 //! the interrupt handler (as opposed to the very last action) to avoid
275 //! returning from the interrupt handler before the interrupt source is
276 //! actually cleared.  Failure to do so may result in the interrupt handler
277 //! being immediately reentered (because the interrupt controller still sees
278 //! the interrupt source asserted).
279 //!
280 //! \return None.
281 //
282 //*****************************************************************************
283 void
SysExcIntClear(uint32_t ui32IntFlags)284 SysExcIntClear(uint32_t ui32IntFlags)
285 {
286     //
287     // Clear the requested interrupt sources.
288     //
289     HWREG(SYSEXC_IC) = ui32IntFlags;
290 }
291 
292 //*****************************************************************************
293 //
294 // Close the Doxygen group.
295 //! @}
296 //
297 //*****************************************************************************
298