Skip to content

Commit 2e884cb

Browse files
fdcavalcantixiaoxiang781216
authored andcommitted
xtensa/esp32s2: add support for shutdown handlers
1 parent 6ac1299 commit 2e884cb

File tree

2 files changed

+182
-0
lines changed

2 files changed

+182
-0
lines changed

arch/xtensa/src/esp32s2/esp32s2_systemreset.c

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,88 @@
3131

3232
#include "xtensa.h"
3333
#include "hardware/esp32s2_rtccntl.h"
34+
#include "esp32s2_systemreset.h"
35+
36+
/****************************************************************************
37+
* Pre-processor Definitions
38+
****************************************************************************/
39+
40+
#define SHUTDOWN_HANDLERS_NO 4
41+
42+
/****************************************************************************
43+
* Private Data
44+
****************************************************************************/
45+
46+
static shutdown_handler_t shutdown_handlers[SHUTDOWN_HANDLERS_NO];
3447

3548
/****************************************************************************
3649
* Public Functions
3750
****************************************************************************/
3851

52+
/****************************************************************************
53+
* Name: esp32s2_register_shutdown_handler
54+
*
55+
* Description:
56+
* This function allows you to register a handler that gets invoked before
57+
* the application is restarted.
58+
*
59+
* Input Parameters:
60+
* handler - Function to execute on restart
61+
*
62+
* Returned Value:
63+
* OK on success (positive non-zero values are cmd-specific)
64+
* Negated errno returned on failure.
65+
*
66+
****************************************************************************/
67+
68+
int esp32s2_register_shutdown_handler(shutdown_handler_t handler)
69+
{
70+
for (int i = 0; i < SHUTDOWN_HANDLERS_NO; i++)
71+
{
72+
if (shutdown_handlers[i] == handler)
73+
{
74+
return -EEXIST;
75+
}
76+
else if (shutdown_handlers[i] == NULL)
77+
{
78+
shutdown_handlers[i] = handler;
79+
return OK;
80+
}
81+
}
82+
83+
return -ENOMEM;
84+
}
85+
86+
/****************************************************************************
87+
* Name: esp32s2_unregister_shutdown_handler
88+
*
89+
* Description:
90+
* This function allows you to unregister a handler which was previously
91+
* registered using up_register_shutdown_handler function.
92+
*
93+
* Input Parameters:
94+
* handler - Function to execute on restart
95+
*
96+
* Returned Value:
97+
* OK on success (positive non-zero values are cmd-specific)
98+
* Negated errno returned on failure.
99+
*
100+
****************************************************************************/
101+
102+
int esp32s2_unregister_shutdown_handler(shutdown_handler_t handler)
103+
{
104+
for (int i = 0; i < SHUTDOWN_HANDLERS_NO; i++)
105+
{
106+
if (shutdown_handlers[i] == handler)
107+
{
108+
shutdown_handlers[i] = NULL;
109+
return OK;
110+
}
111+
}
112+
113+
return -EINVAL;
114+
}
115+
39116
/****************************************************************************
40117
* Name: up_systemreset
41118
*
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/****************************************************************************
2+
* arch/xtensa/src/esp32s2/esp32s2_systemreset.h
3+
*
4+
* Licensed to the Apache Software Foundation (ASF) under one or more
5+
* contributor license agreements. See the NOTICE file distributed with
6+
* this work for additional information regarding copyright ownership. The
7+
* ASF licenses this file to you under the Apache License, Version 2.0 (the
8+
* "License"); you may not use this file except in compliance with the
9+
* License. You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16+
* License for the specific language governing permissions and limitations
17+
* under the License.
18+
*
19+
****************************************************************************/
20+
21+
#ifndef __ARCH_XTENSA_SRC_ESP32S3_ESP32S3_SYSTEMRESET_H
22+
#define __ARCH_XTENSA_SRC_ESP32S3_ESP32S3_SYSTEMRESET_H
23+
24+
/****************************************************************************
25+
* Included Files
26+
****************************************************************************/
27+
28+
#ifndef __ASSEMBLY__
29+
30+
#include <stdint.h>
31+
32+
#undef EXTERN
33+
#if defined(__cplusplus)
34+
#define EXTERN extern "C"
35+
extern "C"
36+
{
37+
#else
38+
#define EXTERN extern
39+
#endif
40+
41+
/****************************************************************************
42+
* Public Types
43+
****************************************************************************/
44+
45+
/* Shutdown handler type */
46+
47+
typedef void (*shutdown_handler_t)(void);
48+
49+
/****************************************************************************
50+
* Public Function Prototypes
51+
****************************************************************************/
52+
53+
/****************************************************************************
54+
* Name: esp32s2_register_shutdown_handler
55+
*
56+
* Description:
57+
* This function allows you to register a handler that gets invoked before
58+
* the application is restarted.
59+
*
60+
* Input Parameters:
61+
* handler - Function to execute on restart
62+
*
63+
* Returned Value:
64+
* OK on success (positive non-zero values are cmd-specific)
65+
* Negated errno returned on failure.
66+
*
67+
****************************************************************************/
68+
69+
int esp32s2_register_shutdown_handler(shutdown_handler_t handler);
70+
71+
/****************************************************************************
72+
* Name: esp32s2_unregister_shutdown_handler
73+
*
74+
* Description:
75+
* This function allows you to unregister a handler which was previously
76+
* registered using up_register_shutdown_handler function.
77+
*
78+
* Input Parameters:
79+
* handler - Function to execute on restart
80+
*
81+
* Returned Value:
82+
* OK on success (positive non-zero values are cmd-specific)
83+
* Negated errno returned on failure.
84+
*
85+
****************************************************************************/
86+
87+
int esp32s2_unregister_shutdown_handler(shutdown_handler_t handler);
88+
89+
/****************************************************************************
90+
* Name: up_shutdown_handler
91+
*
92+
* Description:
93+
* Process all registered shutdown callback functions.
94+
*
95+
****************************************************************************/
96+
97+
void up_shutdown_handler(void);
98+
99+
#ifdef __cplusplus
100+
}
101+
#endif
102+
#undef EXTERN
103+
104+
#endif /* __ASSEMBLY__ */
105+
#endif /* __ARCH_XTENSA_SRC_ESP32S2_ESP32S2_SYSTEMRESET_H */

0 commit comments

Comments
 (0)