summaryrefslogtreecommitdiff
path: root/bsp/hc32f4a0/Libraries/HC32F4A0_StdPeriph_Driver/src/hc32f4a0_swdt.c
blob: 944b5521a8fda65dd6b1a36ab512c15134b2a890 (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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/**
 *******************************************************************************
 * @file  hc32f4a0_swdt.c
 * @brief This file provides firmware functions to manage the Special Watch Dog
 *        Timer(SWDT).
 @verbatim
   Change Logs:
   Date             Author          Notes
   2020-06-12       Yangjp          First version
   2020-09-04       Yangjp          Optimize timeout handling in functions
 @endverbatim
 *******************************************************************************
 * Copyright (C) 2020, Huada Semiconductor Co., Ltd. All rights reserved.
 *
 * This software component is licensed by HDSC under BSD 3-Clause license
 * (the "License"); You may not use this file except in compliance with the
 * License. You may obtain a copy of the License at:
 *                    opensource.org/licenses/BSD-3-Clause
 *
 *******************************************************************************
 */

/*******************************************************************************
 * Include files
 ******************************************************************************/
#include "hc32f4a0_swdt.h"
#include "hc32f4a0_utility.h"

/**
 * @addtogroup HC32F4A0_DDL_Driver
 * @{
 */

/**
 * @defgroup DDL_SWDT SWDT
 * @brief Special Watch Dog Timer
 * @{
 */

#if (DDL_SWDT_ENABLE == DDL_ON)

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

/*******************************************************************************
 * Local pre-processor symbols/macros ('#define')
 ******************************************************************************/
/**
 * @defgroup SWDT_Local_Macros SWDT Local Macros
 * @{
 */

/* SWDT Registers Clear Mask */
#define SWDT_CR_CLEAR_MASK              (SWDT_CR_PERI   | SWDT_CR_CKS | SWDT_CR_WDPT | \
                                         SWDT_CR_SLPOFF | SWDT_CR_ITS)

/* SWDT Refresh Key */
#define SWDT_REFRESH_KEY_START          (0x0123UL)
#define SWDT_REFRESH_KEY_END            (0x3210UL)

/* SWDT clear flag timeout(ms) */
#define SWDT_CLEAR_FLAG_TIMEOUT         (5UL)

/**
 * @defgroup SWDT_Check_Parameters_Validity SWDT Check Parameters Validity
 * @{
 */
#define IS_SWDT_COUNTER_CYCLE(x)                                               \
(   ((x) == SWDT_COUNTER_CYCLE_256)             ||                             \
    ((x) == SWDT_COUNTER_CYCLE_4096)            ||                             \
    ((x) == SWDT_COUNTER_CYCLE_16384)           ||                             \
    ((x) == SWDT_COUNTER_CYCLE_65536))

#define IS_SWDT_CLOCK_DIVISION(x)                                              \
(   ((x) == SWDT_CLOCK_DIV1)                    ||                             \
    ((x) == SWDT_CLOCK_DIV16)                   ||                             \
    ((x) == SWDT_CLOCK_DIV32)                   ||                             \
    ((x) == SWDT_CLOCK_DIV64)                   ||                             \
    ((x) == SWDT_CLOCK_DIV128)                  ||                             \
    ((x) == SWDT_CLOCK_DIV256)                  ||                             \
    ((x) == SWDT_CLOCK_DIV2048))

#define IS_SWDT_ALLOW_REFRESH_RANGE(x)                                         \
(   ((x) == SWDT_RANGE_0TO100PCT)               ||                             \
    ((x) == SWDT_RANGE_0TO25PCT)                ||                             \
    ((x) == SWDT_RANGE_25TO50PCT)               ||                             \
    ((x) == SWDT_RANGE_0TO50PCT)                ||                             \
    ((x) == SWDT_RANGE_50TO75PCT)               ||                             \
    ((x) == SWDT_RANGE_0TO25PCT_50TO75PCT)      ||                             \
    ((x) == SWDT_RANGE_25TO75PCT)               ||                             \
    ((x) == SWDT_RANGE_0TO75PCT)                ||                             \
    ((x) == SWDT_RANGE_75TO100PCT)              ||                             \
    ((x) == SWDT_RANGE_0TO25PCT_75TO100PCT)     ||                             \
    ((x) == SWDT_RANGE_25TO50PCT_75TO100PCT)    ||                             \
    ((x) == SWDT_RANGE_0TO50PCT_75TO100PCT)     ||                             \
    ((x) == SWDT_RANGE_50TO100PCT)              ||                             \
    ((x) == SWDT_RANGE_0TO25PCT_50TO100PCT)     ||                             \
    ((x) == SWDT_RANGE_25TO100PCT))

#define IS_SWDT_LPM_COUNT(x)                                                   \
(   ((x) == SWDT_LPM_COUNT_CONTINUE)            ||                             \
    ((x) == SWDT_LPM_COUNT_STOP))

#define IS_SWDT_REQUEST_TYPE(x)                                                \
(   ((x) == SWDT_TRIG_EVENT_INT)                ||                             \
    ((x) == SWDT_TRIG_EVENT_RESET))

#define IS_SWDT_FLAG(x)                                                        \
(   (0UL != (x))                                &&                             \
    (0UL == ((x) & ((uint32_t)(~(SWDT_FLAG_UDF  | SWDT_FLAG_REF))))))

/**
 * @}
 */

/**
 * @}
 */

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

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

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

/*******************************************************************************
 * Function implementation - global ('extern') and local ('static')
 ******************************************************************************/
/**
 * @defgroup SWDT_Global_Functions SWDT Global Functions
 * @{
 */

/**
 * @brief  Initialize SWDT.
 * @param  [in] pstcSwdtInit            Pointer to a @ref stc_swdt_init_t structure
 * @retval An en_result_t enumeration value:
 *           - Ok: Initialize success
 *           - ErrorInvalidParameter: Invalid parameter
 */
en_result_t SWDT_Init(const stc_swdt_init_t *pstcSwdtInit)
{
    en_result_t enRet = Ok;

   if(NULL == pstcSwdtInit)
   {
       enRet = ErrorInvalidParameter;
   }
   else
    {
        /* Check parameters */
        DDL_ASSERT(IS_SWDT_COUNTER_CYCLE(pstcSwdtInit->u32CountCycle));
        DDL_ASSERT(IS_SWDT_CLOCK_DIVISION(pstcSwdtInit->u32ClockDivision));
        DDL_ASSERT(IS_SWDT_ALLOW_REFRESH_RANGE(pstcSwdtInit->u32RefreshRange));
        DDL_ASSERT(IS_SWDT_LPM_COUNT(pstcSwdtInit->u32LPModeCountEn));
        DDL_ASSERT(IS_SWDT_REQUEST_TYPE(pstcSwdtInit->u32TrigType));

        /* SWDT CR Configuration(Software Start Mode) */
        MODIFY_REG32(M4_SWDT->CR, SWDT_CR_CLEAR_MASK,
                     (pstcSwdtInit->u32CountCycle   | pstcSwdtInit->u32ClockDivision |
                      pstcSwdtInit->u32RefreshRange | pstcSwdtInit->u32LPModeCountEn |
                      pstcSwdtInit->u32TrigType));
    }

    return enRet;
}

/**
 * @brief  SWDT feed dog.
 * @note   In software startup mode, Start counter when refreshing for the first time.
 * @param  None
 * @retval None
 */
void SWDT_Feed(void)
{
    WRITE_REG32(M4_SWDT->RR, SWDT_REFRESH_KEY_START);
    WRITE_REG32(M4_SWDT->RR, SWDT_REFRESH_KEY_END);
}

/**
 * @brief  Get SWDT flag status.
 * @param  [in] u32Flag                 Specifies the SWDT flag type.
 *         This parameter can be one or any combination of the following values:
 *           @arg SWDT_FLAG_UDF:    Count Underflow flag
 *           @arg SWDT_FLAG_REF:    Refresh Error flag
 * @retval An en_flag_status_t enumeration value:
 *           - Set: Flag is set
 *           - Reset: Flag is reset
 */
en_flag_status_t SWDT_GetStatus(uint32_t u32Flag)
{
    en_flag_status_t enFlagSta = Reset;

    /* Check parameters */
    DDL_ASSERT(IS_SWDT_FLAG(u32Flag));

    if (0UL != (READ_REG32_BIT(M4_SWDT->SR, u32Flag)))
    {
        enFlagSta = Set;
    }

    return enFlagSta;
}

/**
 * @brief  Clear SWDT flag status.
 * @param  [in] u32Flag                 Specifies the SWDT flag type.
 *         This parameter can be one or any combination of the following values:
 *           @arg SWDT_FLAG_UDF:    Count Underflow flag
 *           @arg SWDT_FLAG_REF:    Refresh Error flag
 * @retval An en_result_t enumeration value:
 *           - Ok: Clear flag success
 *           - ErrorTimeout: Clear flag timeout
 */
en_result_t SWDT_ClearStatus(uint32_t u32Flag)
{
    __IO uint32_t u32Count;
    en_result_t enRet = Ok;

    /* Check parameters */
    DDL_ASSERT(IS_SWDT_FLAG(u32Flag));

    CLEAR_REG32_BIT(M4_SWDT->SR, u32Flag);
    /* Waiting for FLAG bit clear */
    u32Count = SWDT_CLEAR_FLAG_TIMEOUT * (HCLK_VALUE / 20000UL);
    while (0UL != READ_REG32_BIT(M4_SWDT->SR, u32Flag))
    {
        if (0UL == u32Count)
        {
            enRet = ErrorTimeout;
            break;
        }
        u32Count--;
    }

    return enRet;
}

/**
 * @}
 */

#endif /* DDL_SWDT_ENABLE */

/**
 * @}
 */

/**
* @}
*/

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