-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
302 lines (255 loc) · 11.2 KB
/
main.c
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/**********************************************************************************************************************
* \file main.c
* \copyright Copyright (C) Infineon Technologies AG 2024
*
* Use of this file is subject to the terms of use agreed between (i) you or the company in which ordinary course of
* business you are acting and (ii) Infineon Technologies AG or its licensees. If and as long as no such terms of use
* are agreed, use of this file is subject to following:
*
* Boost Software License - Version 1.0 - August 17th, 2003
*
* Permission is hereby granted, free of charge, to any person or organization obtaining a copy of the software and
* accompanying documentation covered by this license (the "Software") to use, reproduce, display, distribute, execute,
* and transmit the Software, and to prepare derivative works of the Software, and to permit third-parties to whom the
* Software is furnished to do so, all subject to the following:
*
* The copyright notices in the Software and this entire statement, including the above license grant, this restriction
* and the following disclaimer, must be included in all copies of the Software, in whole or in part, and all
* derivative works of the Software, unless such copies or derivative works are solely in the form of
* machine-executable object code generated by a source language processor.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.p
*********************************************************************************************************************/
/*********************************************************************************************************************/
/*-----------------------------------------------------Includes------------------------------------------------------*/
/*********************************************************************************************************************/
#include "cy_pdl.h"
#include "cybsp.h"
/*********************************************************************************************************************/
/*------------------------------------------------------Macros-------------------------------------------------------*/
/*********************************************************************************************************************/
/* WDT demo options */
#define WDT_RESET_MODE (0U)
#define WDT_INTERRUPT_MODE (1U)
/* Select WDT_DEMO as either WDT_RESET_MODE or WDT_INTERRUPT_MODE */
#define WDT_DEMO (WDT_INTERRUPT_MODE)
/* Interval between LED blinks in milliseconds */
#define DELAY_IN_MS (200U)
/* WDT interrupt priority */
#define WDT_INTERRUPT_PRIORITY (0U)
/* Lower, upper and warn limit of watchdog count */
#define LOWER_LIMIT (0U)
#define UPPER_LIMIT (40000U)
#define WARN_LIMIT (40000U)
/* LED states */
#define LED_STATE_ON (1U)
#define LED_STATE_OFF (0U)
/*********************************************************************************************************************/
/*-------------------------------------------------Global variables--------------------------------------------------*/
/*********************************************************************************************************************/
/* WDT interrupt service routine configuration */
const cy_stc_sysint_t wdt_isr_cfg =
{
.intrSrc = srss_wdt_irq_IRQn, /* Interrupt source is WDT interrupt */
.intrPriority = WDT_INTERRUPT_PRIORITY /* Interrupt priority is 0 */
};
/* Variable to check whether WDT interrupt is triggered */
volatile bool g_warn_limit_flag = false;
/*********************************************************************************************************************/
/*------------------------------------------------Function Prototypes------------------------------------------------*/
/*********************************************************************************************************************/
/* LED blinking function */
void blink_led(GPIO_PRT_Type* base, uint32_t pinNum, uint32_t milliseconds);
/* WDT initialization function */
void init_wdt(void);
/* Serve watchdog counter */
void serve_wdt(void);
/* WDT interrupt service routine */
void wdt_isr(void);
/**********************************************************************************************************************
* Function Name: main
* Summary:
* 1. Initializes BSP.
* 2. Checks whether the reset is caused due to WDT or other reset causes and blinks LED accordingly.
* 3. Initializes the WDT.
* 4. Checks continuously if interrupt is triggered due to WDT and toggles LED and also clears the interrupt.
* Parameters:
* none
* Return:
* int
**********************************************************************************************************************
*/
int main(void)
{
cy_rslt_t result;
cy_en_sysint_status_t status = CY_SYSINT_BAD_PARAM;
/* Initialize the device and board peripherals */
result = cybsp_init();
if (result != CY_RSLT_SUCCESS)
{
CY_ASSERT(0);
}
/* Enable global interrupts */
__enable_irq();
/* Check the device reset reason */
if(CY_SYSLIB_RESET_HWWDT == Cy_SysLib_GetResetReason())
{
/* If the reset caused by WDT reset, blink LED thrice */
for(uint8_t i = 0; i < 3; i++)
{
blink_led(CYBSP_USER_LED6_PORT, CYBSP_USER_LED6_NUM, DELAY_IN_MS);
}
}
/* If the reset caused by other reset types, blink LED once */
else
{
blink_led(CYBSP_USER_LED6_PORT, CYBSP_USER_LED6_NUM, DELAY_IN_MS);
}
/* Clear the reset cause register */
Cy_SysLib_ClearResetReason();
if(WDT_DEMO == WDT_INTERRUPT_MODE)
{
/* Initialize interrupt and its handler */
status = Cy_SysInt_Init(&wdt_isr_cfg, wdt_isr);
if(status != CY_SYSINT_SUCCESS)
{
CY_ASSERT(0);
}
/* Enable interrupt by enabling interrupt request and unmasking WDT interrupt */
NVIC_EnableIRQ(wdt_isr_cfg.intrSrc);
Cy_WDT_UnmaskInterrupt();
}
/* Initialize WDT */
init_wdt();
for (;;)
{
#if(WDT_DEMO == WDT_INTERRUPT_MODE)
/* Check if the WDT interrupt has been triggered */
if (g_warn_limit_flag == true)
{
/* Clear the flag */
g_warn_limit_flag = false;
/* Invert the state of LED */
Cy_GPIO_Inv(CYBSP_USER_LED6_PORT, CYBSP_USER_LED6_PIN);
/* Clear the WDT interrupt */
Cy_WDT_ClearInterrupt();
/* Unmask the WDT interrupt */
Cy_WDT_UnmaskInterrupt();
/* Service WDT */
serve_wdt();
}
#elif(WDT_DEMO == WDT_RESET_MODE)
/* Just execute an infinite loop until WDT reset occurs */
while(1);
#endif
}
}
/**********************************************************************************************************************
* Function Name: blink_led
* Summary:
* This function blinks LED once.
* Parameters:
* GPIO_PRT_Type* base, uint32_t pinNum, uint32_t milliseconds
* Return:
* none
**********************************************************************************************************************
*/
void blink_led(GPIO_PRT_Type* base, uint32_t pinNum, uint32_t milliseconds)
{
Cy_GPIO_Write(base, pinNum, LED_STATE_ON);
Cy_SysLib_Delay(milliseconds);
Cy_GPIO_Write(base, pinNum, LED_STATE_OFF);
Cy_SysLib_Delay(milliseconds);
}
/**********************************************************************************************************************
* Function Name: init_wdt
* Summary:
* This function initializes the WDT block and starts the WDT block.
* Parameters:
* none
* Return:
* none
**********************************************************************************************************************
*/
void init_wdt(void)
{
/* Unlock WDT registers */
Cy_WDT_Unlock();
/* Disable WDT before the initialization */
Cy_WDT_Disable();
/* Set each limit */
Cy_WDT_SetLowerLimit(LOWER_LIMIT);
Cy_WDT_SetUpperLimit(UPPER_LIMIT);
Cy_WDT_SetWarnLimit(WARN_LIMIT);
/* Set lower action none */
Cy_WDT_SetLowerAction(CY_WDT_LOW_UPPER_LIMIT_ACTION_NONE);
#if (WDT_DEMO == WDT_INTERRUPT_MODE)
/* Set warn action to trigger interrupt when the count reaches warn limit */
Cy_WDT_SetUpperAction(CY_WDT_LOW_UPPER_LIMIT_ACTION_NONE);
Cy_WDT_SetWarnAction(CY_WDT_WARN_ACTION_INT);
#elif(WDT_DEMO == WDT_RESET_MODE)
/* Set upper action to reset when the count reaches upper limit */
Cy_WDT_SetUpperAction(CY_WDT_LOW_UPPER_LIMIT_ACTION_RESET);
Cy_WDT_SetWarnAction(CY_WDT_WARN_ACTION_NONE);
#endif
/* Other settings that is set under WDT unlocked and disabled */
Cy_WDT_SetAutoService(CY_WDT_DISABLE);
Cy_WDT_SetDeepSleepPause(CY_WDT_DISABLE);
Cy_WDT_SetDebugRun(CY_WDT_ENABLE);
/* Enabling WDT must be done under WDT is unlocked*/
Cy_WDT_Enable();
/* It takes time to enable up to three clk_lf cycles. */
while(!Cy_WDT_IsEnabled());
/* Service WDT to reset count before going back to main routine */
Cy_WDT_SetService();
/* Lock WDT registers */
Cy_WDT_Lock();
}
/**********************************************************************************************************************
* Function Name: serve_wdt
* Summary:
* This function serve the WDT block to reset the watchdog count.
* Parameters:
* none
* Return:
* none
**********************************************************************************************************************
*/
void serve_wdt(void)
{
/* Need to unlock WDT registers before the service */
Cy_WDT_Unlock();
/* Serve WDT */
Cy_WDT_SetService();
/* Lock WDT registers */
Cy_WDT_Lock();
}
/**********************************************************************************************************************
* Function Name: wdt_isr
* Summary:
* This function is the handler for the WDT interrupt.
* It just sets flag that is used to check if WDT interrupt occurs in main routine.
* Parameters:
* handlerArg (unused)
* event (unused)
* Return:
* none
**********************************************************************************************************************
*/
void wdt_isr(void)
{
#if (WDT_DEMO == WDT_INTERRUPT_MODE)
/* Mask the WDT interrupt to prevent from further triggers */
Cy_WDT_MaskInterrupt();
/* Set the flag */
g_warn_limit_flag = true;
#elif(WDT_DEMO == WDT_RESET_MODE)
/* The interrupt never occurs if it's the reset demo */
CY_ASSERT(0);
#endif
}
/* [] END OF FILE */