1 /*
2  * Copyright (C) 2022-2024, Xiaohua Semiconductor Co., Ltd.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date             Author      Notes
8  * 2022-04-28       CDT         first version
9  * 2024-06-07       CDT         Modify the IRQ install implementation for F448/F472
10  * 2025-07-16       CDT         Support HC32F334
11  */
12 
13 /*******************************************************************************
14  * Include files
15  ******************************************************************************/
16 #include <rtthread.h>
17 #include "drv_irq.h"
18 
19 /*******************************************************************************
20  * Local type definitions ('typedef')
21  ******************************************************************************/
22 
23 /*******************************************************************************
24  * Local pre-processor symbols/macros ('#define')
25  ******************************************************************************/
26 #if defined (HC32F448) || defined (HC32F472) || defined (HC32F334)
27     /* Interrupt registration max number */
28     #define HC32_INT_REG_MAX_NUM        (16U)
29 #endif
30 
31 /*******************************************************************************
32  * Global variable definitions (declared in header file with 'extern')
33  ******************************************************************************/
34 
35 /*******************************************************************************
36  * Local function prototypes ('static')
37  ******************************************************************************/
38 
39 /*******************************************************************************
40  * Local variable definitions ('static')
41  ******************************************************************************/
42 
43 /*******************************************************************************
44  * Function implementation - global ('extern') and local ('static')
45  ******************************************************************************/
hc32_install_irq_handler(struct hc32_irq_config * irq_config,void (* irq_hdr)(void),rt_bool_t irq_enable)46 rt_err_t hc32_install_irq_handler(struct hc32_irq_config *irq_config,
47                                   void (*irq_hdr)(void),
48                                   rt_bool_t irq_enable)
49 {
50     rt_err_t result = -RT_ERROR;
51     stc_irq_signin_config_t stcIrqSignConfig;
52 
53     RT_ASSERT(RT_NULL != irq_config);
54 
55 #if defined (HC32F448) || defined (HC32F472) || defined (HC32F334)
56     if (irq_config->irq_num < HC32_INT_REG_MAX_NUM)
57     {
58         RT_ASSERT(RT_NULL != irq_hdr);
59         INTC_IntSrcCmd(irq_config->int_src, DISABLE);
60     }
61     else
62     {
63         INTC_IntSrcCmd(irq_config->int_src, ENABLE);
64         goto nvic_config;
65     }
66     stcIrqSignConfig.enIRQn      = irq_config->irq_num;
67     stcIrqSignConfig.enIntSrc    = irq_config->int_src;
68     stcIrqSignConfig.pfnCallback = irq_hdr;
69     if (LL_OK == INTC_IrqSignIn(&stcIrqSignConfig))
70 nvic_config:
71 #elif defined (HC32F460) || defined (HC32F4A0) || defined (HC32F4A8)
72     stcIrqSignConfig.enIRQn      = irq_config->irq_num;
73     stcIrqSignConfig.enIntSrc    = irq_config->int_src;
74     stcIrqSignConfig.pfnCallback = irq_hdr;
75     if (LL_OK == INTC_IrqSignIn(&stcIrqSignConfig))
76 #endif
77     {
78         NVIC_ClearPendingIRQ(irq_config->irq_num);
79         NVIC_SetPriority(irq_config->irq_num, irq_config->irq_prio);
80         if (RT_TRUE == irq_enable)
81         {
82             NVIC_EnableIRQ(irq_config->irq_num);
83         }
84         else
85         {
86             NVIC_DisableIRQ(irq_config->irq_num);
87         }
88         result = RT_EOK;
89     }
90 
91     return result;
92 }
93 
94 /*******************************************************************************
95  * EOF (not truncated)
96  ******************************************************************************/
97