summaryrefslogtreecommitdiff
path: root/bsp/hc32/libraries/hc32_drivers/drv_irq.c
blob: 501717d5d37fb170fff8a61ff0c23c79ac04318a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
 * Copyright (C) 2022-2024, Xiaohua Semiconductor Co., Ltd.
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date             Author      Notes
 * 2022-04-28       CDT         first version
 * 2024-06-07       CDT         Modify the IRQ install implementation for F448/F472
 * 2025-07-16       CDT         Support HC32F334
 */

/*******************************************************************************
 * Include files
 ******************************************************************************/
#include <rtthread.h>
#include "drv_irq.h"

/*******************************************************************************
 * Local type definitions ('typedef')
 ******************************************************************************/

/*******************************************************************************
 * Local pre-processor symbols/macros ('#define')
 ******************************************************************************/
#if defined (HC32F448) || defined (HC32F472) || defined (HC32F334)
    /* Interrupt registration max number */
    #define HC32_INT_REG_MAX_NUM        (16U)
#endif

/*******************************************************************************
 * Global variable definitions (declared in header file with 'extern')
 ******************************************************************************/

/*******************************************************************************
 * Local function prototypes ('static')
 ******************************************************************************/

/*******************************************************************************
 * Local variable definitions ('static')
 ******************************************************************************/

/*******************************************************************************
 * Function implementation - global ('extern') and local ('static')
 ******************************************************************************/
rt_err_t hc32_install_irq_handler(struct hc32_irq_config *irq_config,
                                  void (*irq_hdr)(void),
                                  rt_bool_t irq_enable)
{
    rt_err_t result = -RT_ERROR;
    stc_irq_signin_config_t stcIrqSignConfig;

    RT_ASSERT(RT_NULL != irq_config);

#if defined (HC32F448) || defined (HC32F472) || defined (HC32F334)
    if (irq_config->irq_num < HC32_INT_REG_MAX_NUM)
    {
        RT_ASSERT(RT_NULL != irq_hdr);
        INTC_IntSrcCmd(irq_config->int_src, DISABLE);
    }
    else
    {
        INTC_IntSrcCmd(irq_config->int_src, ENABLE);
        goto nvic_config;
    }
    stcIrqSignConfig.enIRQn      = irq_config->irq_num;
    stcIrqSignConfig.enIntSrc    = irq_config->int_src;
    stcIrqSignConfig.pfnCallback = irq_hdr;
    if (LL_OK == INTC_IrqSignIn(&stcIrqSignConfig))
#elif defined (HC32F460) || defined (HC32F4A0) || defined (HC32F4A8)
    stcIrqSignConfig.enIRQn      = irq_config->irq_num;
    stcIrqSignConfig.enIntSrc    = irq_config->int_src;
    stcIrqSignConfig.pfnCallback = irq_hdr;
    if (LL_OK == INTC_IrqSignIn(&stcIrqSignConfig))
#endif
nvic_config:
    {
        NVIC_ClearPendingIRQ(irq_config->irq_num);
        NVIC_SetPriority(irq_config->irq_num, irq_config->irq_prio);
        if (RT_TRUE == irq_enable)
        {
            NVIC_EnableIRQ(irq_config->irq_num);
        }
        else
        {
            NVIC_DisableIRQ(irq_config->irq_num);
        }
        result = RT_EOK;
    }

    return result;
}

/*******************************************************************************
 * EOF (not truncated)
 ******************************************************************************/