summaryrefslogtreecommitdiff
path: root/bsp/acm32/acm32f0x0-nucleo/libraries/HAL_Driver/Src/HAL_EXTI.c
blob: d316a8806e909b2eae351ed35a028fffe87ee0cb (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
  ******************************************************************************
  * @file    HAL_EXTI.c
  * @version V1.0.0
  * @date    2020
  * @brief   EXTI HAL module driver.
  *          This file provides firmware functions to manage the following
  *          functionalities of the General Purpose Input/Output (EXTI) peripheral:
  *           + Initialization functions
  *           + IO operation functions
  ******************************************************************************
*/
#include "ACM32Fxx_HAL.h"

/*********************************************************************************
* Function    : HAL_EXTI_IRQHandler
* Description : Handle EXTI interrupt request.
* Input       : huart: EXTI handle.
* Output      :
* Author      : Chris_Kyle                         Data : 2020年
**********************************************************************************/
void HAL_EXTI_IRQHandler(EXTI_HandleTypeDef *hexti)
{
    if (EXTI->PDR & hexti->u32_Line)
    {
        EXTI->PDR = hexti->u32_Line;
    }
}

/*********************************************************************************
* Function    : HAL_EXTI_SetConfigLine
* Description :
* Input       :
* Outpu       :
* Author      : Chris_Kyle                         Data : 2020年
**********************************************************************************/
HAL_StatusTypeDef HAL_EXTI_SetConfigLine(EXTI_HandleTypeDef *hexti)
{
    uint32_t lu32_IndexLine;

#if (USE_FULL_ASSERT == 1)
    if (!IS_EXTI_ALL_LINE(hexti->u32_Line))      return HAL_ERROR;
    if (!IS_EXTI_MODE(hexti->u32_Mode))          return HAL_ERROR;
    if (!IS_EXTI_TRIGGER(hexti->u32_Trigger))    return HAL_ERROR;

    /* Line0 ~ 15 trigger from GPIO */
    if (!(hexti->u32_Line >> 16))
    {
        if (!IS_EXTI_GPIOSEL(hexti->u32_GPIOSel))    return HAL_ERROR;
    }
#endif

    lu32_IndexLine = hexti->u32_Line;

    /* Interrupt Mode */
    if (hexti->u32_Mode == EXTI_MODE_INTERRUPT)
    {
        EXTI->IENR |=  lu32_IndexLine;
        EXTI->EENR &= ~lu32_IndexLine;

        NVIC_ClearPendingIRQ(EXTI_IRQn);
        NVIC_EnableIRQ(EXTI_IRQn);
    }
    /* Event Mode */
    else if (hexti->u32_Mode == EXTI_MODE_EVENT)
    {
        EXTI->EENR |=  lu32_IndexLine;
        EXTI->IENR &= ~lu32_IndexLine;
    }


    if (hexti->u32_Trigger == EXTI_TRIGGER_RISING)
    {
        EXTI->RTENR |=  lu32_IndexLine;
        EXTI->FTENR &= ~lu32_IndexLine;
    }
    else if (hexti->u32_Trigger == EXTI_TRIGGER_FALLING)
    {
        EXTI->FTENR |=  lu32_IndexLine;
        EXTI->RTENR &= ~lu32_IndexLine;
    }
    else
    {
        EXTI->FTENR |= lu32_IndexLine;
        EXTI->RTENR |= lu32_IndexLine;
    }

    /* Line0 ~ 15 trigger from GPIO */
    if (!(hexti->u32_Line >> 16))
    {
        lu32_IndexLine = 0;

        while(hexti->u32_Line >> lu32_IndexLine != 0x01)
        {
            lu32_IndexLine++;
        }

        /* Line0 ~ 7 */
        if (lu32_IndexLine < 8)
        {
            EXTI->EXTICR1 = (EXTI->EXTICR1 & ~(0x0F << (lu32_IndexLine * 4))) | hexti->u32_GPIOSel << (lu32_IndexLine * 4);
        }
        /* Line8 ~ 15 */
        else
        {
            lu32_IndexLine -= 8;

            EXTI->EXTICR2 = (EXTI->EXTICR2 & ~(0x0F << (lu32_IndexLine * 4))) | hexti->u32_GPIOSel << (lu32_IndexLine * 4);
        }
    }

    return HAL_OK;
}

/*********************************************************************************
* Function    : HAL_EXTI_SoftTrigger
* Description : Software trigger EXTI
* Input       :
* Outpu       :
* Author      : Chris_Kyle                         Data : 2020年
**********************************************************************************/
void HAL_EXTI_SoftTrigger(EXTI_HandleTypeDef *hexti)
{
#if (USE_FULL_ASSERT == 1)
    if (!IS_EXTI_ALL_LINE(hexti->u32_Line))    return;
#endif

    /* Set pending BIT */
    EXTI->SWIER |= hexti->u32_Line;
}

/*********************************************************************************
* Function    : HAL_EXTI_GetPending
* Description : Get interrupt pending bit of a dedicated line.
* Input       :
* Outpu       :
* Author      : Chris_Kyle                         Data : 2020年
**********************************************************************************/
bool HAL_EXTI_GetPending(EXTI_HandleTypeDef *hexti)
{
#if (USE_FULL_ASSERT == 1)
    if (!IS_EXTI_ALL_LINE(hexti->u32_Line))    return HAL_ERROR;
#endif

    if (hexti->u32_Line & EXTI->PDR)
    {
        return true;
    }
    else
    {
        return false;
    }
}

/*********************************************************************************
* Function    : HAL_EXTI_ClearPending
* Description : Clear interrupt pending bit of a dedicated line.
* Input       :
* Outpu       :
* Author      : Chris_Kyle                         Data : 2020年
**********************************************************************************/
void HAL_EXTI_ClearPending(EXTI_HandleTypeDef *hexti)
{
#if (USE_FULL_ASSERT == 1)
    if (!IS_EXTI_ALL_LINE(hexti->u32_Line))    return;
#endif

    /* Clear pending status */
    EXTI->PDR |= hexti->u32_Line;
}

/*********************************************************************************
* Function    : HAL_EXTI_ClearAllPending
* Description : Clear all interrupt pending bit.
* Input       :
* Outpu       :
* Author      : xwl                         Data : 2021年
**********************************************************************************/
void HAL_EXTI_ClearAllPending(void)
{
    /* Clear pending status */
    EXTI->PDR |= EXTI_LINE_MASK;
}