xref: /btstack/port/renesas-ek-ra6m4a-da14531/e2-project/ra/fsp/src/bsp/mcu/all/bsp_group_irq.c (revision c30869498fb8e98c1408c9db0e7624f02f483b73)
1 /***********************************************************************************************************************
2  * Copyright [2020-2022] Renesas Electronics Corporation and/or its affiliates.  All Rights Reserved.
3  *
4  * This software and documentation are supplied by Renesas Electronics America Inc. and may only be used with products
5  * of Renesas Electronics Corp. and its affiliates ("Renesas").  No other uses are authorized.  Renesas products are
6  * sold pursuant to Renesas terms and conditions of sale.  Purchasers are solely responsible for the selection and use
7  * of Renesas products and Renesas assumes no liability.  No license, express or implied, to any intellectual property
8  * right is granted by Renesas. This software is protected under all applicable laws, including copyright laws. Renesas
9  * reserves the right to change or discontinue this software and/or this documentation. THE SOFTWARE AND DOCUMENTATION
10  * IS DELIVERED TO YOU "AS IS," AND RENESAS MAKES NO REPRESENTATIONS OR WARRANTIES, AND TO THE FULLEST EXTENT
11  * PERMISSIBLE UNDER APPLICABLE LAW, DISCLAIMS ALL WARRANTIES, WHETHER EXPLICITLY OR IMPLICITLY, INCLUDING WARRANTIES
12  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NONINFRINGEMENT, WITH RESPECT TO THE SOFTWARE OR
13  * DOCUMENTATION.  RENESAS SHALL HAVE NO LIABILITY ARISING OUT OF ANY SECURITY VULNERABILITY OR BREACH.  TO THE MAXIMUM
14  * EXTENT PERMITTED BY LAW, IN NO EVENT WILL RENESAS BE LIABLE TO YOU IN CONNECTION WITH THE SOFTWARE OR DOCUMENTATION
15  * (OR ANY PERSON OR ENTITY CLAIMING RIGHTS DERIVED FROM YOU) FOR ANY LOSS, DAMAGES, OR CLAIMS WHATSOEVER, INCLUDING,
16  * WITHOUT LIMITATION, ANY DIRECT, CONSEQUENTIAL, SPECIAL, INDIRECT, PUNITIVE, OR INCIDENTAL DAMAGES; ANY LOST PROFITS,
17  * OTHER ECONOMIC DAMAGE, PROPERTY DAMAGE, OR PERSONAL INJURY; AND EVEN IF RENESAS HAS BEEN ADVISED OF THE POSSIBILITY
18  * OF SUCH LOSS, DAMAGES, CLAIMS OR COSTS.
19  **********************************************************************************************************************/
20 
21 /***********************************************************************************************************************
22  * Includes   <System Includes> , "Project Includes"
23  **********************************************************************************************************************/
24 #include "bsp_api.h"
25 
26 /***********************************************************************************************************************
27  * Macro definitions
28  **********************************************************************************************************************/
29 #define BSP_GRP_IRQ_TOTAL_ITEMS    (16U)
30 
31 /***********************************************************************************************************************
32  * Typedef definitions
33  **********************************************************************************************************************/
34 
35 /***********************************************************************************************************************
36  * Exported global variables (to be accessed by other files)
37  **********************************************************************************************************************/
38 
39 /***********************************************************************************************************************
40  * Private global variables and functions
41  **********************************************************************************************************************/
42 
43 /** This array holds callback functions. */
44 bsp_grp_irq_cb_t g_bsp_group_irq_sources[BSP_GRP_IRQ_TOTAL_ITEMS] BSP_SECTION_EARLY_INIT;
45 
46 void        NMI_Handler(void);
47 static void bsp_group_irq_call(bsp_grp_irq_t irq);
48 
49 /*******************************************************************************************************************//**
50  * Calls the callback function for an interrupt if a callback has been registered.
51  *
52  * @param[in]   irq         Which interrupt to check and possibly call.
53  *
54  * @retval FSP_SUCCESS              Callback was called.
55  * @retval FSP_ERR_INVALID_ARGUMENT No valid callback has been registered for this interrupt source.
56  *
57  * @warning This function is called from within an interrupt
58  **********************************************************************************************************************/
bsp_group_irq_call(bsp_grp_irq_t irq)59 static void bsp_group_irq_call (bsp_grp_irq_t irq)
60 {
61     /** Check for valid callback */
62     if (NULL != g_bsp_group_irq_sources[irq])
63     {
64         /** Callback has been found. Call it. */
65         g_bsp_group_irq_sources[irq](irq);
66     }
67 }
68 
69 /*******************************************************************************************************************//**
70  * @addtogroup BSP_MCU
71  *
72  * @{
73  **********************************************************************************************************************/
74 
75 /*******************************************************************************************************************//**
76  * Register a callback function for supported interrupts. If NULL is passed for the callback argument then any
77  * previously registered callbacks are unregistered.
78  *
79  * @param[in]  irq          Interrupt for which  to register a callback.
80  * @param[in]  p_callback   Pointer to function to call when interrupt occurs.
81  *
82  * @retval     FSP_SUCCESS                  Callback registered
83  * @retval     FSP_ERR_ASSERTION            Callback pointer is NULL
84  **********************************************************************************************************************/
R_BSP_GroupIrqWrite(bsp_grp_irq_t irq,void (* p_callback)(bsp_grp_irq_t irq))85 fsp_err_t R_BSP_GroupIrqWrite (bsp_grp_irq_t irq, void (* p_callback)(bsp_grp_irq_t irq))
86 {
87 #if BSP_CFG_PARAM_CHECKING_ENABLE
88 
89     /* Check pointer for NULL value. */
90     FSP_ASSERT(p_callback);
91 #endif
92 
93     /* Register callback. */
94     g_bsp_group_irq_sources[irq] = p_callback;
95 
96     return FSP_SUCCESS;
97 }
98 
99 /*******************************************************************************************************************//**
100  * Non-maskable interrupt handler. This exception is defined by the BSP, unlike other system exceptions, because
101  * there are many sources that map to the NMI exception.
102  **********************************************************************************************************************/
NMI_Handler(void)103 void NMI_Handler (void)
104 {
105     uint16_t nmisr = R_ICU->NMISR;
106 
107     /* Loop over all NMI status flags */
108     for (bsp_grp_irq_t irq = BSP_GRP_IRQ_IWDT_ERROR; irq <= BSP_GRP_IRQ_CACHE_PARITY; irq++)
109     {
110         /* If the current irq status register is set call the irq callback. */
111         if (0U != (nmisr & (1U << irq)))
112         {
113             (void) bsp_group_irq_call(irq);
114         }
115     }
116 
117     /* Clear status flags that have been handled. */
118     R_ICU->NMICLR = nmisr;
119 }
120 
121 /** @} (end addtogroup BSP_MCU) */
122