summaryrefslogtreecommitdiff
path: root/bsp/mm32f327x/Libraries/MM32F327x/HAL_Lib/Src/hal_exti.c
blob: 409d84d80fa504bb9a331163616da8c36ea76ee7 (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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
////////////////////////////////////////////////////////////////////////////////
/// @file     hal_exti.c
/// @author   AE TEAM
/// @brief    THIS FILE PROVIDES ALL THE EXTI FIRMWARE FUNCTIONS.
////////////////////////////////////////////////////////////////////////////////
/// @attention
///
/// THE EXISTING FIRMWARE IS ONLY FOR REFERENCE, WHICH IS DESIGNED TO PROVIDE
/// CUSTOMERS WITH CODING INFORMATION ABOUT THEIR PRODUCTS SO THEY CAN SAVE
/// TIME. THEREFORE, MINDMOTION SHALL NOT BE LIABLE FOR ANY DIRECT, INDIRECT OR
/// CONSEQUENTIAL DAMAGES ABOUT ANY CLAIMS ARISING OUT OF THE CONTENT OF SUCH
/// HARDWARE AND/OR THE USE OF THE CODING INFORMATION CONTAINED HEREIN IN
/// CONNECTION WITH PRODUCTS MADE BY CUSTOMERS.
///
/// <H2><CENTER>&COPY; COPYRIGHT MINDMOTION </CENTER></H2>
////////////////////////////////////////////////////////////////////////////////

// Define to prevent recursive inclusion
#define _HAL_EXTI_C_

// Files includes
#include "hal_exti.h"

////////////////////////////////////////////////////////////////////////////////
/// @addtogroup MM32_Hardware_Abstract_Layer
/// @{

////////////////////////////////////////////////////////////////////////////////
/// @addtogroup EXTI_HAL
/// @{

////////////////////////////////////////////////////////////////////////////////
/// @addtogroup EXTI_Exported_Functions
/// @{


////////////////////////////////////////////////////////////////////////////////
/// @brief  Deinitializes the EXTI peripheral registers to their default reset
///         values.
/// @param  None.
/// @retval None.
////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////
/// @brief  Deinitializes the EXTI registers to their default reset values.
/// @param  None.
/// @retval None.
/// @note   MEM_MODE bits are not affected by APB reset.
/// @note   MEM_MODE bits took the value from the user option bytes.
/// @note   CFGR2 register is not affected by APB reset.
/// @note   CLABBB configuration bits are locked when set.
/// @note   To unlock the configuration, perform a system reset.
////////////////////////////////////////////////////////////////////////////////
void EXTI_DeInit(void)
{
    u16 i;
    // Clear all
    exEXTI_LineDisable(~0x00000000);

    // rc_w1
    EXTI->PR = EXTI->PR;

    // Set EXTI_CFGR1 register to reset value without affecting MEM_MODE bits
    EXTI->CFGR &= EXTI_CFGR_MEMMODE;

    // Set EXTICRx registers to reset value
    for (i = 0; i < 4; i++) {
        EXTI->CR[i] = 0;
    }
}



////////////////////////////////////////////////////////////////////////////////
/// @brief  Selects the GPIO pin used as EXTI Line.
/// @param  port_source_gpio: selects the GPIO port to be used as source for EXTI lines .
/// @param  pin_source: specifies the EXTI line to be configured.
/// @note   This parameter can be pin_source where x can be:
///         For MCU: (0..15) for GPIOA, GPIOB, (13..15) for GPIOC and (0..1, 6..7) for GPIOD.
/// @retval None.
////////////////////////////////////////////////////////////////////////////////
void EXTI_LineConfig(u8 port_source_gpio, u8 pin_source)
{
    EXTI->CR[pin_source >> 0x02] &= ~(0x0F << (0x04 * (pin_source & 0x03)));
    EXTI->CR[pin_source >> 0x02] |= ((port_source_gpio) << (0x04 * (pin_source & 0x03)));
}

////////////////////////////////////////////////////////////////////////////////
/// @brief  Initializes the EXTI peripheral according to the specified
///         parameters in the init_struct.
/// @param  init_struct: pointer to a EXTI_InitTypeDef structure that
///         contains the configuration information for the EXTI peripheral.
/// @retval None.
////////////////////////////////////////////////////////////////////////////////
void EXTI_Init(EXTI_InitTypeDef* init_struct)
{
    if (init_struct->EXTI_LineCmd != DISABLE) {
        EXTI->IMR  &= ~init_struct->EXTI_Line;
        EXTI->EMR  &= ~init_struct->EXTI_Line;
        if (init_struct->EXTI_Mode == EXTI_Mode_Interrupt) {
            EXTI->IMR |= init_struct->EXTI_Line;
        }
        else {
            EXTI->EMR |= init_struct->EXTI_Line;
        }
        EXTI->RTSR &= ~init_struct->EXTI_Line;
        EXTI->FTSR &= ~init_struct->EXTI_Line;
        if (init_struct->EXTI_Trigger == EXTI_Trigger_Rising_Falling) {
            EXTI->RTSR |= init_struct->EXTI_Line;
            EXTI->FTSR |= init_struct->EXTI_Line;                               // Rising and Faling    afio
        }
        else if (init_struct->EXTI_Trigger == EXTI_Trigger_Rising) {
            EXTI->RTSR |= init_struct->EXTI_Line;
        }
        else {
            EXTI->FTSR |= init_struct->EXTI_Line;
        }
    }
    else {
        if (init_struct->EXTI_Mode == EXTI_Mode_Interrupt) {
            EXTI->IMR &= ~init_struct->EXTI_Line;
        }
        else {
            EXTI->EMR &= ~init_struct->EXTI_Line;
        }
    }
}

////////////////////////////////////////////////////////////////////////////////
/// @brief  Fills each init_struct member with its reset value.
/// @param  init_struct: pointer to a EXTI_InitTypeDef structure which will
///         be initialized.
/// @retval None.
////////////////////////////////////////////////////////////////////////////////
void EXTI_StructInit(EXTI_InitTypeDef* init_struct)
{
    init_struct->EXTI_Line    = EXTI_LineNone;
    init_struct->EXTI_Mode    = EXTI_Mode_Interrupt;
    init_struct->EXTI_Trigger = EXTI_Trigger_Falling;
    init_struct->EXTI_LineCmd = DISABLE;
}

////////////////////////////////////////////////////////////////////////////////
/// @brief  Generates a Software interrupt on selected EXTI line.
/// @param  line: specifies the EXTI line on which the software interrupt
///         will be generated.
/// @retval None.
////////////////////////////////////////////////////////////////////////////////
void EXTI_GenerateSWInterrupt(u32 line)
{
    EXTI->SWIER |= line;
}

////////////////////////////////////////////////////////////////////////////////
/// @brief  Checks whether the specified EXTI line flag is set or not.
/// @param  line: specifies the EXTI line flag to check.
/// @retval The new state of line (SET or RESET).
////////////////////////////////////////////////////////////////////////////////
FlagStatus EXTI_GetFlagStatus(u32 line)
{
    return (EXTI->PR & line) ? SET : RESET;
}

////////////////////////////////////////////////////////////////////////////////
/// @brief  Clears the EXTI's line pending flags.
/// @param  line: specifies the EXTI lines flags to clear.
/// @retval None.
////////////////////////////////////////////////////////////////////////////////
void EXTI_ClearFlag(u32 line)
{
    EXTI->PR = line;
}

////////////////////////////////////////////////////////////////////////////////
/// @brief  Checks whether the specified EXTI line is asserted or not.
/// @param  line: specifies the EXTI line to check.
/// @retval The new state of line (SET or RESET).
////////////////////////////////////////////////////////////////////////////////
ITStatus EXTI_GetITStatus(u32 line)
{
    return ((EXTI->PR & line) && (EXTI->IMR & line)) ? SET : RESET;
}

////////////////////////////////////////////////////////////////////////////////
/// @brief  Clears the EXTI's line pending bits.
/// @param  line: specifies the EXTI lines to clear.
/// @retval None.
////////////////////////////////////////////////////////////////////////////////
void EXTI_ClearITPendingBit(u32 line)
{
    EXTI->PR = line;
}

////////////////////////////////////////////////////////////////////////////////
/// @brief  EXTI Line Disable
/// @param  line: specifies the EXTI lines to clear.
/// @retval None.
////////////////////////////////////////////////////////////////////////////////
void exEXTI_LineDisable(u32 line)
{
    EXTI->IMR  &= ~line;
    EXTI->EMR  &= ~line;
    EXTI->RTSR &= ~line;
    EXTI->FTSR &= ~line;
}

////////////////////////////////////////////////////////////////////////////////
/// @brief  Clears the EXTI's line all pending bits.
/// @param  None.
/// @retval None.
////////////////////////////////////////////////////////////////////////////////
u32 exEXTI_GetAllFlagStatus(void)
{
    return EXTI->PR;
}

/// @}

/// @}

/// @}