xref: /btstack/port/renesas-tb-s1ja-cc256x/template/btstack_example/synergy/ssp/src/bsp/mcu/s1ja/bsp_group_irq.c (revision 3b5c872a8c45689e8cc17891f01530f5aa5e911c)
1 /***********************************************************************************************************************
2  * Copyright [2015-2017] Renesas Electronics Corporation and/or its licensors. All Rights Reserved.
3  *
4  * This file is part of Renesas SynergyTM Software Package (SSP)
5  *
6  * The contents of this file (the "contents") are proprietary and confidential to Renesas Electronics Corporation
7  * and/or its licensors ("Renesas") and subject to statutory and contractual protections.
8  *
9  * This file is subject to a Renesas SSP license agreement. Unless otherwise agreed in an SSP license agreement with
10  * Renesas: 1) you may not use, copy, modify, distribute, display, or perform the contents; 2) you may not use any name
11  * or mark of Renesas for advertising or publicity purposes or in connection with your use of the contents; 3) RENESAS
12  * MAKES NO WARRANTY OR REPRESENTATIONS ABOUT THE SUITABILITY OF THE CONTENTS FOR ANY PURPOSE; THE CONTENTS ARE PROVIDED
13  * "AS IS" WITHOUT ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
14  * PARTICULAR PURPOSE, AND NON-INFRINGEMENT; AND 4) RENESAS SHALL NOT BE LIABLE FOR ANY DIRECT, INDIRECT, SPECIAL, OR
15  * CONSEQUENTIAL DAMAGES, INCLUDING DAMAGES RESULTING FROM LOSS OF USE, DATA, OR PROJECTS, WHETHER IN AN ACTION OF
16  * CONTRACT OR TORT, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THE CONTENTS. Third-party contents
17  * included in this file may be subject to different terms.
18  **********************************************************************************************************************/
19 /***********************************************************************************************************************
20 * File Name    : bsp_group_irq.c
21 * Description  : This module allows callbacks to be registered for sources inside a grouped interrupt. For example, the
22 *                NMI pin, oscillation stop detect, and WDT underflow interrupts are all mapped to the NMI exception.
23 *                Since multiple modules cannot define the same handler, the BSP provides a mechanism to be alerted when
24 *                a certain exception occurs.
25 ***********************************************************************************************************************/
26 
27 /***********************************************************************************************************************
28 Includes   <System Includes> , "Project Includes"
29 ***********************************************************************************************************************/
30 #include "bsp_api.h"
31 
32 #if defined(BSP_MCU_GROUP_S1JA)
33 
34 /***********************************************************************************************************************
35 Macro definitions
36 ***********************************************************************************************************************/
37 
38 /***********************************************************************************************************************
39 Typedef definitions
40 ***********************************************************************************************************************/
41 
42 /***********************************************************************************************************************
43 Exported global variables (to be accessed by other files)
44 ***********************************************************************************************************************/
45 
46 /***********************************************************************************************************************
47 Private global variables and functions
48 ***********************************************************************************************************************/
49 /** This array holds callback functions. */
50 static bsp_grp_irq_cb_t g_bsp_group_irq_sources[BSP_GRP_IRQ_TOTAL_ITEMS];
51 
52 void NMI_Handler(void);
53 
54 /*******************************************************************************************************************//**
55  * @brief      Initialize callback function array.
56  **********************************************************************************************************************/
bsp_group_interrupt_open(void)57 void bsp_group_interrupt_open (void)
58 {
59     uint32_t i;
60 
61     for (i = (uint32_t)0; i < BSP_GRP_IRQ_TOTAL_ITEMS; i++)
62     {
63         g_bsp_group_irq_sources[i] = NULL;
64     }
65 }
66 
67 /*******************************************************************************************************************//**
68  * @brief      Calls the callback function for an interrupt if a callback has been registered.
69  *
70  * @param[in]   irq         Which interrupt to check and possibly call.
71  *
72  * @retval SSP_SUCCESS              Callback was called.
73  * @retval SSP_ERR_INVALID_ARGUMENT No valid callback has been registered for this interrupt source.
74  *
75  * @warning This function is called from within an interrupt
76  **********************************************************************************************************************/
bsp_group_irq_call(bsp_grp_irq_t irq)77 ssp_err_t bsp_group_irq_call (bsp_grp_irq_t irq)
78 {
79     ssp_err_t err;
80 
81     err = SSP_SUCCESS;
82 
83     /** Check for valid callback */
84     if ((uint32_t)g_bsp_group_irq_sources[irq] == (uint32_t)NULL)
85     {
86         err = SSP_ERR_INVALID_ARGUMENT;
87     }
88     else
89     {
90         /** Callback has been found. Call it. */
91         g_bsp_group_irq_sources[irq](irq);
92     }
93 
94     return err;
95 }
96 
97 /*******************************************************************************************************************//**
98  * @addtogroup BSP_MCU_GROUP_IRQ
99  *
100  * @{
101  **********************************************************************************************************************/
102 
103 /*******************************************************************************************************************//**
104  * @brief Register a callback function for supported interrupts. If NULL is passed for the callback argument
105  *          then any previously registered callbacks are unregistered.
106  *
107  * @param[in] irq        Interrupt for which  to register a callback.
108  * @param[in] p_callback Pointer to function to call when interrupt occurs.
109  *
110  * @retval SSP_SUCCESS          Callback registered
111  **********************************************************************************************************************/
R_BSP_GroupIrqWrite(bsp_grp_irq_t irq,void (* p_callback)(bsp_grp_irq_t irq))112 ssp_err_t R_BSP_GroupIrqWrite (bsp_grp_irq_t irq,  void (* p_callback)(bsp_grp_irq_t irq))
113 {
114     ssp_err_t err;
115 
116     err = SSP_SUCCESS;
117 
118     /** Check for valid address. */
119     if ((uint32_t)p_callback == (uint32_t)NULL)
120     {
121         /** Callback was NULL. De-register callback. */
122         g_bsp_group_irq_sources[irq] = NULL;
123     }
124     else
125     {
126         /** Register callback. */
127         g_bsp_group_irq_sources[irq] = p_callback;
128     }
129 
130     return err;
131 }
132 
133 
134 
135 /*******************************************************************************************************************//**
136  * @brief Non-maskable interrupt handler. This exception is defined by the BSP, unlike other system exceptions,
137  *        because there are many sources that map to the NMI exception.
138  **********************************************************************************************************************/
NMI_Handler(void)139 void NMI_Handler (void)
140 {
141     /** Determine what is the cause of this interrupt. */
142     if (1 == R_ICU->NMISR_b.IWDTST)
143     {
144         /** IWDT underflow/refresh error interrupt is requested. */
145         bsp_group_irq_call(BSP_GRP_IRQ_IWDT_ERROR);
146 
147         /** Clear IWDT flag. */
148         R_ICU->NMICLR_b.IWDTCLR = 1U;
149     }
150 
151     if (1 == R_ICU->NMISR_b.WDTST)
152     {
153         /** WDT underflow/refresh error interrupt is requested. */
154         bsp_group_irq_call(BSP_GRP_IRQ_WDT_ERROR);
155 
156         /** Clear WDT flag. */
157         R_ICU->NMICLR_b.WDTCLR = 1U;
158     }
159 
160     if (1 == R_ICU->NMISR_b.LVD1ST)
161     {
162         /** Voltage monitoring 1 interrupt is requested. */
163         bsp_group_irq_call(BSP_GRP_IRQ_LVD1);
164 
165         /** Clear LVD1 flag. */
166         R_ICU->NMICLR_b.LVD1CLR = 1U;
167     }
168 
169     if (1 == R_ICU->NMISR_b.LVD2ST)
170     {
171         /** Voltage monitoring 2 interrupt is requested. */
172         bsp_group_irq_call(BSP_GRP_IRQ_LVD2);
173 
174         /** Clear LVD2 flag. */
175         R_ICU->NMICLR_b.LVD2CLR = 1U;
176     }
177 
178     if (1 == R_ICU->NMISR_b.OSTST)
179     {
180         /** Oscillation stop detection interrupt is requested. */
181         bsp_group_irq_call(BSP_GRP_IRQ_OSC_STOP_DETECT);
182 
183         /** Clear oscillation stop detect flag. */
184         R_ICU->NMICLR_b.OSTCLR = 1U;
185     }
186 
187     if (1 == R_ICU->NMISR_b.NMIST)
188     {
189         /** NMI pin interrupt is requested. */
190         bsp_group_irq_call(BSP_GRP_IRQ_NMI_PIN);
191 
192         /** Clear NMI pin interrupt flag. */
193         R_ICU->NMICLR_b.NMICLR = 1U;
194     }
195 
196     if (1 == R_ICU->NMISR_b.RPEST)
197     {
198         /** RAM Parity Error interrupt is requested. */
199         bsp_group_irq_call(BSP_GRP_IRQ_RAM_PARITY);
200 
201         /** Clear RAM parity error flag. */
202         R_ICU->NMICLR_b.RPECLR = 1U;
203     }
204 
205     if (1 == R_ICU->NMISR_b.RECCST)
206     {
207         /** RAM ECC Error interrupt is requested. */
208         bsp_group_irq_call(BSP_GRP_IRQ_RAM_ECC);
209 
210         /** Clear RAM ECC error flag. */
211         R_ICU->NMICLR_b.RECCCLR = 1U;
212     }
213 
214     if (1 == R_ICU->NMISR_b.BUSSST)
215     {
216         /** MPU Bus Slave Error interrupt is requested. */
217         bsp_group_irq_call(BSP_GRP_IRQ_MPU_BUS_SLAVE);
218 
219         /** Clear MPU Bus Slave error flag. */
220         R_ICU->NMICLR_b.BUSSCLR = 1U;
221     }
222 
223     if (1 == R_ICU->NMISR_b.BUSMST)
224     {
225         /** MPU Bus Slave Error interrupt is requested. */
226         bsp_group_irq_call(BSP_GRP_IRQ_MPU_BUS_MASTER);
227 
228         /** Clear MPU Bus Master error flag. */
229         R_ICU->NMICLR_b.BUSMCLR = 1U;
230     }
231 
232     if (1 == R_ICU->NMISR_b.SPEST)
233     {
234         /** MPU Stack Error interrupt is requested. */
235         bsp_group_irq_call(BSP_GRP_IRQ_MPU_STACK);
236 
237         /** Clear MPU Stack error flag. */
238         R_ICU->NMICLR_b.SPECLR = 1U;
239     }
240 }
241 
242 #endif
243 
244 
245 /** @} (end addtogroup BSP_MCU_GROUP_IRQ) */
246